Add portable Ziran hashes AES and modular arithmetic
This commit is contained in:
commit
ba50e2bde7
10 files changed
+628
No files matched your search
@@ -0,0 +1,3 @@
|
||||
build/
|
||||
backup/
|
||||
ziran.local.toml
|
||||
@@ -0,0 +1,8 @@
|
||||
ZIRAN ?= ziran
|
||||
LOCKED := $(if $(wildcard ziran.local.toml),,--locked)
|
||||
.PHONY: check test
|
||||
check:
|
||||
$(ZIRAN) check --project $(LOCKED) src/crypto.zi src/aes.zi src/sha1.zi
|
||||
test:
|
||||
$(ZIRAN) build --project $(LOCKED) --target=c --entry crypto_test:Main --exe -o build/tests tests/crypto_test.zi
|
||||
env -u DISPLAY -u WAYLAND_DISPLAY build/tests/crypto_test
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
// FIPS 197 AES-256. The S-box uses fixed field arithmetic, avoiding
|
||||
// secret-indexed lookup tables. The IGE mode is Telegram's specified mode.
|
||||
Key :: struct { bytes: [240]u8; valid: bool; }
|
||||
Multiply :: (left: u8, right: u8) -> u8 {
|
||||
a := cast(u32)left; b := cast(u32)right; result: u32
|
||||
for i: 0..7 { result ^= a & (cast(u32)0 - (b & cast(u32)1)); a = ((a << cast(u32)1) ^ (cast(u32)0x11b & (cast(u32)0 - (a >> cast(u32)7)))) & cast(u32)255; b >>= cast(u32)1 }
|
||||
return cast(u8)result
|
||||
}
|
||||
Inverse :: (x: u8) -> u8 {
|
||||
a := Multiply(x,x); b := Multiply(a,a); c := Multiply(b,b); d := Multiply(c,c); e := Multiply(d,d); f := Multiply(e,e); g := Multiply(f,f)
|
||||
return Multiply(Multiply(Multiply(a,b),Multiply(c,d)),Multiply(Multiply(e,f),g))
|
||||
}
|
||||
Rotate :: (x: u8, n: u32) -> u8 { return cast(u8)((cast(u32)x << n) | (cast(u32)x >> (cast(u32)8-n))) }
|
||||
Sub :: (x: u8) -> u8 { a := Inverse(x); return a ^ Rotate(a,1) ^ Rotate(a,2) ^ Rotate(a,3) ^ Rotate(a,4) ^ cast(u8)0x63 }
|
||||
Unsub :: (x: u8) -> u8 { return Inverse(Rotate(x,1) ^ Rotate(x,3) ^ Rotate(x,6) ^ cast(u8)0x05) }
|
||||
Expand :: (bytes: []u8) -> Key {
|
||||
key: Key; if bytes.count != 32 { return key }
|
||||
for i: 0..31 { key.bytes[i] = bytes[i] }
|
||||
at: s32 = 32; rc: u8 = 1
|
||||
while at < 240 {
|
||||
word: [4]u8; for i: 0..3 { word[i] = key.bytes[at-4+i] }
|
||||
if at % 32 == 0 { first := word[0]; word[0] = Sub(word[1]) ^ rc; word[1] = Sub(word[2]); word[2] = Sub(word[3]); word[3] = Sub(first); rc = Multiply(rc,2) }
|
||||
else if at % 32 == 16 { for i: 0..3 { word[i] = Sub(word[i]) } }
|
||||
for i: 0..3 { key.bytes[at] = key.bytes[at-32] ^ word[i]; at += 1 }
|
||||
}; key.valid = true; return key
|
||||
}
|
||||
Add :: (state: []u8, key: *Key, round: s32) { for i: 0..15 { state[i] ^= key.bytes[round*16+i] } }
|
||||
Shift :: (state: []u8, decrypt: bool) {
|
||||
old: [16]u8; for i: 0..15 { old[i] = state[i] }
|
||||
for row: 0..3 { for col: 0..3 { source := (col+row)%4; if decrypt { source = (col+4-row)%4 }; state[col*4+row] = old[source*4+row] } }
|
||||
}
|
||||
Mix :: (state: []u8, decrypt: bool) {
|
||||
for col: 0..3 {
|
||||
i := col*4; a := state[i]; b := state[i+1]; c := state[i+2]; d := state[i+3]
|
||||
if decrypt {
|
||||
state[i] = Multiply(a,14)^Multiply(b,11)^Multiply(c,13)^Multiply(d,9)
|
||||
state[i+1] = Multiply(a,9)^Multiply(b,14)^Multiply(c,11)^Multiply(d,13)
|
||||
state[i+2] = Multiply(a,13)^Multiply(b,9)^Multiply(c,14)^Multiply(d,11)
|
||||
state[i+3] = Multiply(a,11)^Multiply(b,13)^Multiply(c,9)^Multiply(d,14)
|
||||
} else {
|
||||
state[i] = Multiply(a,2)^Multiply(b,3)^c^d
|
||||
state[i+1] = a^Multiply(b,2)^Multiply(c,3)^d
|
||||
state[i+2] = a^b^Multiply(c,2)^Multiply(d,3)
|
||||
state[i+3] = Multiply(a,3)^b^c^Multiply(d,2)
|
||||
}
|
||||
}
|
||||
}
|
||||
Block :: (key: *Key, input: []u8, output: []u8, decrypt: bool) -> bool {
|
||||
if !key.valid || input.count != 16 || output.count != 16 { return false }
|
||||
state: [16]u8; for i: 0..15 { state[i] = input[i] }
|
||||
if decrypt {
|
||||
Add(state[:],key,14); round: s32 = 13
|
||||
while round >= 0 { Shift(state[:],true); for i: 0..15 { state[i] = Unsub(state[i]) }; Add(state[:],key,round); if round > 0 { Mix(state[:],true) }; round -= 1 }
|
||||
} else {
|
||||
Add(state[:],key,0)
|
||||
for round: 1..14 { for i: 0..15 { state[i] = Sub(state[i]) }; Shift(state[:],false); if round < 14 { Mix(state[:],false) }; Add(state[:],key,cast(s32)round) }
|
||||
}
|
||||
for i: 0..15 { output[i] = state[i] }; return true
|
||||
}
|
||||
Ige :: (key_bytes: []u8, iv: []u8, input: []u8, output: []u8, decrypt: bool) -> bool {
|
||||
if iv.count != 32 || input.count % 16 != 0 || output.count != input.count { return false }
|
||||
key := Expand(key_bytes); if !key.valid { return false }
|
||||
previous_c: [16]u8; previous_p: [16]u8; block: [16]u8; saved: [16]u8
|
||||
for i: 0..15 { previous_c[i] = iv[i]; previous_p[i] = iv[i+16] }
|
||||
at: s32 = 0
|
||||
while at < input.count {
|
||||
for i: 0..15 { saved[i] = input[at+i]; block[i] = saved[i] ^ (ifx decrypt then previous_p[i] else previous_c[i]) }
|
||||
if !Block(*key,block[:],block[:],decrypt) { return false }
|
||||
for i: 0..15 {
|
||||
output[at+i] = block[i] ^ (ifx decrypt then previous_c[i] else previous_p[i])
|
||||
if decrypt { previous_c[i] = saved[i]; previous_p[i] = output[at+i] } else { previous_c[i] = output[at+i]; previous_p[i] = saved[i] }
|
||||
}; at += 16
|
||||
}; return true
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Fixed-width unsigned 2048-bit Montgomery arithmetic. Sixteen-bit limbs
|
||||
// keep every product within u64 on all native targets. Exponent bits select
|
||||
// results with masks; both multiplications execute for every bit.
|
||||
Integer :: struct { limbs: [128]u32; }
|
||||
Modulus :: struct { value: Integer; squared: Integer; inverse: u32; valid: bool; }
|
||||
FromBytes :: (bytes: []u8) -> Integer {
|
||||
value: Integer; if bytes.count > 256 { return value }
|
||||
for i: 0..cast(s32)bytes.count-1 { at := bytes.count-1-i; value.limbs[i/2] |= cast(u32)bytes[at] << cast(u32)((i%2)*8) }; return value
|
||||
}
|
||||
ToBytes :: (value: *Integer, bytes: []u8) -> bool {
|
||||
if bytes.count != 256 { return false }
|
||||
for i: 0..255 { bytes[255-i] = cast(u8)(value.limbs[i/2] >> cast(u32)((i%2)*8)) }; return true
|
||||
}
|
||||
Compare :: (a: *Integer, b: *Integer) -> s32 {
|
||||
i: s32 = 127
|
||||
while i >= 0 { if a.limbs[i] > b.limbs[i] { return 1 }; if a.limbs[i] < b.limbs[i] { return -1 }; i -= 1 }; return 0
|
||||
}
|
||||
Subtract :: (a: *Integer, b: *Integer) -> Integer {
|
||||
result: Integer; borrow: u64
|
||||
for i: 0..127 { v := cast(u64)a.limbs[i]-cast(u64)b.limbs[i]-borrow; result.limbs[i] = cast(u32)v & cast(u32)65535; borrow = v >> cast(u64)63 }; return result
|
||||
}
|
||||
Reduce :: (value: *Integer, modulus: *Integer, high: u32) -> Integer {
|
||||
difference := Subtract(value,modulus)
|
||||
borrow: u64
|
||||
for i: 0..127 { v := cast(u64)value.limbs[i]-cast(u64)modulus.limbs[i]-borrow; borrow = v >> cast(u64)63 }
|
||||
take := cast(u32)1-cast(u32)borrow
|
||||
if high != cast(u32)0 { take = 1 }
|
||||
mask := cast(u32)0-take; result: Integer
|
||||
for i: 0..127 { result.limbs[i] = (difference.limbs[i]&mask) | (value.limbs[i]&(~mask)) }; return result
|
||||
}
|
||||
Double :: (value: *Integer, modulus: *Integer) -> Integer {
|
||||
next: Integer; carry: u32
|
||||
for i: 0..127 { word := (value.limbs[i]<<cast(u32)1)|carry; next.limbs[i] = word&cast(u32)65535; carry = word>>cast(u32)16 }
|
||||
return Reduce(*next,modulus,carry)
|
||||
}
|
||||
Prepare :: (bytes: []u8) -> Modulus {
|
||||
result: Modulus; if bytes.count != 256 || (bytes[255]&cast(u8)1)==cast(u8)0 || bytes[0]<cast(u8)128 { return result }
|
||||
result.value = FromBytes(bytes); inverse: u32 = 1
|
||||
for i: 0..4 { inverse *= cast(u32)2-result.value.limbs[0]*inverse }
|
||||
result.inverse = (cast(u32)0-inverse)&cast(u32)65535
|
||||
result.squared.limbs[0] = 1
|
||||
for i: 0..4095 { result.squared = Double(*result.squared,*result.value) }
|
||||
result.valid = true; return result
|
||||
}
|
||||
Multiply :: (a: *Integer, b: *Integer, modulus: *Modulus) -> Integer {
|
||||
words: [258]u64
|
||||
for i: 0..127 { for j: 0..127 { words[i+j] += cast(u64)a.limbs[i]*cast(u64)b.limbs[j] } }
|
||||
for i: 0..255 { words[i+1] += words[i]>>cast(u64)16; words[i] &= cast(u64)65535 }
|
||||
for i: 0..127 {
|
||||
factor := (words[i]*cast(u64)modulus.inverse)&cast(u64)65535; carry: u64
|
||||
for j: 0..127 { at := i+j; next := words[at]+factor*cast(u64)modulus.value.limbs[j]+carry; words[at] = next&cast(u64)65535; carry = next>>cast(u64)16 }
|
||||
at := i+128; next := words[at]+carry; words[at] = next&cast(u64)65535; words[at+1] += next>>cast(u64)16
|
||||
}
|
||||
result: Integer; for i: 0..127 { result.limbs[i] = cast(u32)words[i+128] }
|
||||
return Reduce(*result,*modulus.value,cast(u32)words[256])
|
||||
}
|
||||
Power :: (base_bytes: []u8, exponent: []u8, modulus: *Modulus, output: []u8) -> bool {
|
||||
if !modulus.valid || base_bytes.count > 256 || exponent.count > 256 || output.count != 256 { return false }
|
||||
base := FromBytes(base_bytes); base = Reduce(*base,*modulus.value,0)
|
||||
one: Integer; one.limbs[0] = 1
|
||||
current := Multiply(*one,*modulus.squared,modulus); converted := Multiply(*base,*modulus.squared,modulus)
|
||||
for i: 0..cast(s32)exponent.count-1 {
|
||||
bit: s32 = 7
|
||||
while bit >= 0 {
|
||||
squared := Multiply(*current,*current,modulus); product := Multiply(*squared,*converted,modulus)
|
||||
mask := cast(u32)0-((cast(u32)exponent[i]>>cast(u32)bit)&cast(u32)1)
|
||||
for j: 0..127 { current.limbs[j] = (product.limbs[j]&mask)|(squared.limbs[j]&(~mask)) }; bit -= 1
|
||||
}
|
||||
}
|
||||
result := Multiply(*current,*one,modulus); return ToBytes(*result,output)
|
||||
}
|
||||
Product :: (left: []u8, right: []u8, modulus: *Modulus, output: []u8) -> bool {
|
||||
if !modulus.valid || left.count>256 || right.count>256 || output.count!=256 { return false }
|
||||
a := FromBytes(left); a = Reduce(*a,*modulus.value,0); b := FromBytes(right); b = Reduce(*b,*modulus.value,0)
|
||||
converted := Multiply(*a,*modulus.squared,modulus); result := Multiply(*converted,*b,modulus); return ToBytes(*result,output)
|
||||
}
|
||||
Difference :: (left: []u8, right: []u8, modulus: *Modulus, output: []u8) -> bool {
|
||||
if !modulus.valid || left.count>256 || right.count>256 || output.count!=256 { return false }
|
||||
a := FromBytes(left); b := FromBytes(right); result := Subtract(*a,*b)
|
||||
if Compare(*a,*b)<0 { carry: u32; for i: 0..127 { v := result.limbs[i]+modulus.value.limbs[i]+carry; result.limbs[i] = v&cast(u32)65535; carry = v>>cast(u32)16 } }
|
||||
return ToBytes(*result,output)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Hash :: #import "sha256";
|
||||
using LegacyHash :: #import "sha1";
|
||||
using Aes :: #import "aes";
|
||||
Equal :: (a: []u8, b: []u8) -> bool {
|
||||
if a.count != b.count { return false }; difference: u8
|
||||
for i: 0..cast(s32)a.count-1 { difference |= a[i] ^ b[i] }; return difference == cast(u8)0
|
||||
}
|
||||
Wipe :: (bytes: []u8) { for i: 0..cast(s32)bytes.count-1 { bytes[i] = 0 } }
|
||||
// Arbitrary salt length and output length; no silent truncation.
|
||||
Pbkdf2 :: (password: []u8, salt: []u8, iterations: s32, output: []u8) -> bool {
|
||||
if salt.count > 1024 || output.count <= 0 || output.count > 1024 || iterations <= 0 || iterations > 1000000 { return false }
|
||||
input: [1028]u8; u: [32]u8; t: [32]u8
|
||||
for i: 0..cast(s32)salt.count-1 { input[i] = salt[i] }
|
||||
block: s32 = 1; at: s32 = 0
|
||||
while at < output.count {
|
||||
for i: 0..3 { input[salt.count+i] = cast(u8)(cast(u32)block >> cast(u32)(24-i*8)) }
|
||||
if Hash.HmacSha256(*password[0],password.count,*input[0],salt.count+4,*u[0]) != 1 { return false }
|
||||
for i: 0..31 { t[i] = u[i] }
|
||||
for round: 1..iterations-1 { if Hash.HmacSha256(*password[0],password.count,*u[0],32,*u[0]) != 1 { return false }; for i: 0..31 { t[i] ^= u[i] } }
|
||||
for i: 0..31 { if at < output.count { output[at] = t[i]; at += 1 } }; block += 1
|
||||
}; Wipe(u[:]); Wipe(t[:]); return true
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
// SHA-1 is used only where Telegram's legacy key-exchange format requires it.
|
||||
Context :: struct { state: [5]u32; block: [64]u8; used: s32; length: u64; }
|
||||
Rotate :: (x: u32, n: u32) -> u32 { return (x << n) | (x >> (cast(u32)32 - n)) }
|
||||
Transform :: (c: *Context) {
|
||||
w: [80]u32
|
||||
for i: 0..15 { w[i] = (cast(u32)c.block[i*4] << cast(u32)24) | (cast(u32)c.block[i*4+1] << cast(u32)16) | (cast(u32)c.block[i*4+2] << cast(u32)8) | cast(u32)c.block[i*4+3] }
|
||||
for i: 16..79 { w[i] = Rotate(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1) }
|
||||
a := c.state[0]; b := c.state[1]; d := c.state[2]; e := c.state[3]; f := c.state[4]
|
||||
for i: 0..79 {
|
||||
v: u32; k: u32
|
||||
if i < 20 { v = (b & d) | ((~b) & e); k = 0x5a827999 }
|
||||
else if i < 40 { v = b ^ d ^ e; k = 0x6ed9eba1 }
|
||||
else if i < 60 { v = (b & d) | (b & e) | (d & e); k = 0x8f1bbcdc }
|
||||
else { v = b ^ d ^ e; k = 0xca62c1d6 }
|
||||
t := Rotate(a, 5) + v + f + k + w[i]
|
||||
f = e; e = d; d = Rotate(b, 30); b = a; a = t
|
||||
}
|
||||
c.state[0] += a; c.state[1] += b; c.state[2] += d; c.state[3] += e; c.state[4] += f
|
||||
}
|
||||
Start :: (c: *Context) { c.state = .{0x67452301,0xefcdab89,0x98badcfe,0x10325476,0xc3d2e1f0}; c.used = 0; c.length = 0 }
|
||||
Update :: (c: *Context, bytes: []u8) {
|
||||
for i: 0..cast(s32)bytes.count-1 { c.block[c.used] = bytes[i]; c.used += 1; c.length += cast(u64)1; if c.used == 64 { Transform(c); c.used = 0 } }
|
||||
}
|
||||
Finish :: (c: *Context, output: []u8) -> bool {
|
||||
if output.count != 20 { return false }
|
||||
bits := c.length * cast(u64)8; c.block[c.used] = 128; c.used += 1
|
||||
if c.used > 56 { while c.used < 64 { c.block[c.used] = 0; c.used += 1 }; Transform(c); c.used = 0 }
|
||||
while c.used < 56 { c.block[c.used] = 0; c.used += 1 }
|
||||
for i: 0..7 { c.block[63-i] = cast(u8)(bits >> cast(u64)(8*i)) }
|
||||
Transform(c)
|
||||
for i: 0..19 { output[i] = cast(u8)(c.state[i/4] >> cast(u32)(24-8*(i%4))) }; return true
|
||||
}
|
||||
Sha1 :: (bytes: []u8, output: []u8) -> bool { c: Context; Start(*c); Update(*c, bytes); return Finish(*c, output) }
|
||||
+347
@@ -0,0 +1,347 @@
|
||||
// SHA-256 and HMAC-SHA-256, extracted from Waozi’s Daochi implementation.
|
||||
// Portable Ziran; no foreign cryptographic implementation.
|
||||
Sha256Context :: struct {
|
||||
state: [8]u32
|
||||
bit_length: u64
|
||||
block: [64]u8
|
||||
block_length: s32
|
||||
}
|
||||
|
||||
Sha256RoundConstants: [64]u32 = .[
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
||||
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
||||
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
||||
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
];
|
||||
|
||||
Sha256RotateRight :: (value: u32, bits: u32) -> u32 {
|
||||
return (value >> bits) | (value << (cast(u32)32 - bits))
|
||||
}
|
||||
|
||||
Sha256Transform :: (context: *Sha256Context) {
|
||||
words: [64]u32
|
||||
index: s32 = 0
|
||||
while index < 16 {
|
||||
byte_index := index * 4
|
||||
words[index] =
|
||||
(cast(u32)context.block[byte_index] << cast(u32)24) |
|
||||
(cast(u32)context.block[byte_index + 1] << cast(u32)16) |
|
||||
(cast(u32)context.block[byte_index + 2] << cast(u32)8) |
|
||||
cast(u32)context.block[byte_index + 3]
|
||||
index += 1
|
||||
}
|
||||
while index < 64 {
|
||||
prior_15 := words[index - 15]
|
||||
prior_2 := words[index - 2]
|
||||
sigma_0 := (Sha256RotateRight(prior_15, 7) ^
|
||||
Sha256RotateRight(prior_15, 18) ^ (prior_15 >> cast(u32)3))
|
||||
sigma_1 := (Sha256RotateRight(prior_2, 17) ^
|
||||
Sha256RotateRight(prior_2, 19) ^ (prior_2 >> cast(u32)10))
|
||||
words[index] = (words[index - 16] + sigma_0 +
|
||||
words[index - 7] + sigma_1)
|
||||
index += 1
|
||||
}
|
||||
|
||||
a := context.state[0]
|
||||
b := context.state[1]
|
||||
c := context.state[2]
|
||||
d := context.state[3]
|
||||
e := context.state[4]
|
||||
f := context.state[5]
|
||||
g := context.state[6]
|
||||
h := context.state[7]
|
||||
index = 0
|
||||
while index < 64 {
|
||||
sigma_1 := (Sha256RotateRight(e, 6) ^
|
||||
Sha256RotateRight(e, 11) ^ Sha256RotateRight(e, 25))
|
||||
choose := (e & f) ^ ((~e) & g)
|
||||
temp_1 := (h + sigma_1 + choose + Sha256RoundConstants[index] + words[index])
|
||||
sigma_0 := (Sha256RotateRight(a, 2) ^
|
||||
Sha256RotateRight(a, 13) ^ Sha256RotateRight(a, 22))
|
||||
majority := (a & b) ^ (a & c) ^ (b & c)
|
||||
temp_2 := sigma_0 + majority
|
||||
h = g
|
||||
g = f
|
||||
f = e
|
||||
e = d + temp_1
|
||||
d = c
|
||||
c = b
|
||||
b = a
|
||||
a = temp_1 + temp_2
|
||||
index += 1
|
||||
}
|
||||
context.state[0] += a
|
||||
context.state[1] += b
|
||||
context.state[2] += c
|
||||
context.state[3] += d
|
||||
context.state[4] += e
|
||||
context.state[5] += f
|
||||
context.state[6] += g
|
||||
context.state[7] += h
|
||||
}
|
||||
|
||||
Sha256Initialize :: (context: *Sha256Context) {
|
||||
context.state[0] = 0x6a09e667
|
||||
context.state[1] = 0xbb67ae85
|
||||
context.state[2] = 0x3c6ef372
|
||||
context.state[3] = 0xa54ff53a
|
||||
context.state[4] = 0x510e527f
|
||||
context.state[5] = 0x9b05688c
|
||||
context.state[6] = 0x1f83d9ab
|
||||
context.state[7] = 0x5be0cd19
|
||||
context.bit_length = 0
|
||||
context.block_length = 0
|
||||
}
|
||||
|
||||
Sha256Update :: (context: *Sha256Context, data: *u8, length: s64) {
|
||||
index: s64 = 0
|
||||
while index < length {
|
||||
context.block[context.block_length] = data[index]
|
||||
context.block_length += 1
|
||||
if context.block_length == 64 {
|
||||
Sha256Transform(context)
|
||||
context.bit_length += 512
|
||||
context.block_length = 0
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
|
||||
Sha256Finish :: (context: *Sha256Context, output: *u8) {
|
||||
tail_length := context.block_length
|
||||
index := tail_length
|
||||
context.block[index] = cast(u8)0x80
|
||||
index += 1
|
||||
if index > 56 {
|
||||
while index < 64 {
|
||||
context.block[index] = 0
|
||||
index += 1
|
||||
}
|
||||
Sha256Transform(context)
|
||||
index = 0
|
||||
}
|
||||
while index < 56 {
|
||||
context.block[index] = 0
|
||||
index += 1
|
||||
}
|
||||
context.bit_length += cast(u64)tail_length * cast(u64)8
|
||||
index = 0
|
||||
while index < 8 {
|
||||
context.block[63 - index] = cast(u8)(context.bit_length >> cast(u64)(index * 8))
|
||||
index += 1
|
||||
}
|
||||
Sha256Transform(context)
|
||||
|
||||
byte_index: s32 = 0
|
||||
while byte_index < 4 {
|
||||
state_index: s32 = 0
|
||||
while state_index < 8 {
|
||||
output[state_index * 4 + byte_index] =
|
||||
cast(u8)(context.state[state_index] >>
|
||||
cast(u32)(24 - byte_index * 8))
|
||||
state_index += 1
|
||||
}
|
||||
byte_index += 1
|
||||
}
|
||||
}
|
||||
|
||||
#program_export
|
||||
Sha256 :: (data: *u8, length: s64, output: *u8) {
|
||||
if output == null || length < 0 || (data == null && length > 0) {
|
||||
return
|
||||
}
|
||||
context: Sha256Context
|
||||
Sha256Initialize(*context)
|
||||
if length > 0 {
|
||||
Sha256Update(*context, data, length)
|
||||
}
|
||||
Sha256Finish(*context, output)
|
||||
}
|
||||
|
||||
HexCharacter :: (value: u8) -> u8 {
|
||||
if value < 10 {
|
||||
return cast(u8)48 + value
|
||||
}
|
||||
return cast(u8)87 + value
|
||||
}
|
||||
|
||||
HexNibble :: (value: u8) -> s32 {
|
||||
if value >= cast(u8)48 && value <= cast(u8)57 {
|
||||
return cast(s32)(value - cast(u8)48)
|
||||
}
|
||||
if value >= cast(u8)97 && value <= cast(u8)102 {
|
||||
return cast(s32)(value - cast(u8)97) + 10
|
||||
}
|
||||
if value >= cast(u8)65 && value <= cast(u8)70 {
|
||||
return cast(s32)(value - cast(u8)65) + 10
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
#program_export
|
||||
BytesToHex :: (bytes: *u8, length: s64,
|
||||
output: *u8, output_size: s64) -> s32 {
|
||||
if output == null || output_size <= 0 {
|
||||
return 0
|
||||
}
|
||||
output[0] = 0
|
||||
if bytes == null || length < 0 || length > (output_size - 1) / 2 {
|
||||
return 0
|
||||
}
|
||||
index: s64 = 0
|
||||
while index < length {
|
||||
byte := bytes[index]
|
||||
output[index * 2] = HexCharacter(byte >> cast(u8)4)
|
||||
output[index * 2 + 1] = HexCharacter(byte & cast(u8)15)
|
||||
index += 1
|
||||
}
|
||||
output[length * 2] = 0
|
||||
return 1
|
||||
}
|
||||
|
||||
#program_export
|
||||
HexToBytes :: (hex: string, output: *u8, output_size: s64) -> s32 {
|
||||
if output == null || output_size < 0 ||
|
||||
output_size > 4611686018427387903 ||
|
||||
cast(s64)hex.count != output_size * 2 {
|
||||
return 0
|
||||
}
|
||||
index: s64 = 0
|
||||
while index < output_size {
|
||||
high := HexNibble(hex[index * 2])
|
||||
low := HexNibble(hex[index * 2 + 1])
|
||||
if high < 0 || low < 0 {
|
||||
return 0
|
||||
}
|
||||
output[index] = cast(u8)((high << 4) | low)
|
||||
index += 1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
#program_export
|
||||
Sha256Hex :: (data: *u8, length: s64, output: *u8) {
|
||||
if output == null {
|
||||
return
|
||||
}
|
||||
output[0] = 0
|
||||
if length < 0 || (data == null && length > 0) {
|
||||
return
|
||||
}
|
||||
digest: [32]u8
|
||||
Sha256(data, length, *digest[0])
|
||||
BytesToHex(*digest[0], 32, output, 65)
|
||||
}
|
||||
|
||||
// HMAC-SHA-256 and PBKDF2 keep the account key-file derivation format.
|
||||
// Historical PBKDF2 exports included at most 32 salt bytes.
|
||||
HmacBlockBytes :: 64
|
||||
Sha256DigestBytes :: 32
|
||||
|
||||
Sha256UpdateText :: (context: *Sha256Context, value: string) {
|
||||
index: s64 = 0
|
||||
while index < value.count {
|
||||
context.block[context.block_length] = value[index]
|
||||
context.block_length += 1
|
||||
if context.block_length == 64 {
|
||||
Sha256Transform(context)
|
||||
context.bit_length += 512
|
||||
context.block_length = 0
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
|
||||
HmacSha256Input :: (key: *u8, key_length: s64,
|
||||
key_text: string, use_key_text: bool,
|
||||
data: *u8, data_length: s64, text: string,
|
||||
use_text: bool, output: *u8) -> s32 {
|
||||
if output == null {
|
||||
return 0
|
||||
}
|
||||
if key_length < 0 || data_length < 0 ||
|
||||
(!use_key_text && key == null && key_length > 0) ||
|
||||
(!use_text && data == null && data_length > 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
block: [HmacBlockBytes]u8 = .[0]
|
||||
pad: [HmacBlockBytes]u8
|
||||
inner: [Sha256DigestBytes]u8
|
||||
context: Sha256Context
|
||||
|
||||
if key_length > HmacBlockBytes {
|
||||
if use_key_text {
|
||||
Sha256Initialize(*context)
|
||||
Sha256UpdateText(*context, key_text)
|
||||
Sha256Finish(*context, *block[0])
|
||||
} else {
|
||||
Sha256(key, key_length, *block[0])
|
||||
}
|
||||
} else {
|
||||
index: s64 = 0
|
||||
while index < key_length {
|
||||
if use_key_text {
|
||||
block[index] = key_text[index]
|
||||
} else {
|
||||
block[index] = key[index]
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
}
|
||||
|
||||
index: s32 = 0
|
||||
while index < HmacBlockBytes {
|
||||
pad[index] = block[index] ^ cast(u8)0x36
|
||||
index += 1
|
||||
}
|
||||
Sha256Initialize(*context)
|
||||
Sha256Update(*context, *pad[0], HmacBlockBytes)
|
||||
if use_text {
|
||||
Sha256UpdateText(*context, text)
|
||||
} else if data_length > 0 {
|
||||
Sha256Update(*context, data, data_length)
|
||||
}
|
||||
Sha256Finish(*context, *inner[0])
|
||||
|
||||
index = 0
|
||||
while index < HmacBlockBytes {
|
||||
pad[index] = block[index] ^ cast(u8)0x5c
|
||||
index += 1
|
||||
}
|
||||
Sha256Initialize(*context)
|
||||
Sha256Update(*context, *pad[0], HmacBlockBytes)
|
||||
Sha256Update(*context, *inner[0], Sha256DigestBytes)
|
||||
Sha256Finish(*context, output)
|
||||
return 1
|
||||
}
|
||||
|
||||
#program_export
|
||||
HmacSha256 :: (key: *u8, key_length: s64,
|
||||
data: *u8, data_length: s64, output: *u8) -> s32 {
|
||||
return HmacSha256Input(key, key_length, "", false,
|
||||
data, data_length,
|
||||
"", false, output)
|
||||
}
|
||||
|
||||
#program_export
|
||||
HmacSha256Text :: (key: *u8, key_length: s64,
|
||||
data: string, output: *u8) -> s32 {
|
||||
return HmacSha256Input(key, key_length, "", false,
|
||||
null, data.count,
|
||||
data, true, output)
|
||||
}
|
||||
|
||||
HmacSha256TextKey :: (key: string, data: *u8,
|
||||
data_length: s64, output: *u8) -> s32 {
|
||||
return HmacSha256Input(null, key.count, key, true,
|
||||
data, data_length, "", false, output)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
Crypto :: #import "crypto";
|
||||
Hash :: #import "sha256";
|
||||
Sha1 :: #import "sha1";
|
||||
Aes :: #import "aes";
|
||||
Big :: #import "bigint";
|
||||
#import "std/byte_text_linux"
|
||||
failed: s32;
|
||||
Check :: (ok: bool, label: string) { if !ok { failed += 1; print("FAIL %\n", label) } }
|
||||
Hex :: (text: string, bytes: []u8) { for i: 0..cast(s32)bytes.count-1 { bytes[i] = cast(u8)(Hash.HexNibble(text[i*2])*16+Hash.HexNibble(text[i*2+1])) } }
|
||||
Main :: () -> s32 {
|
||||
abc: [3]u8 = .{97,98,99}; expected: [64]u8; result: [64]u8
|
||||
Hash.Sha256(*abc[0],3,*result[0]); Hex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",expected[:32]); Check(Crypto.Equal(result[:32],expected[:32]),"FIPS SHA-256 abc")
|
||||
unused Sha1.Sha1(abc[:],result[:20]); Hex("a9993e364706816aba3e25717850c26c9cd0d89d",expected[:20]); Check(Crypto.Equal(result[:20],expected[:20]),"SHA-1 abc")
|
||||
key: [32]u8; plain: [16]u8; cipher: [16]u8; recovered: [16]u8
|
||||
Hex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",key[:]); Hex("00112233445566778899aabbccddeeff",plain[:])
|
||||
expanded := Aes.Expand(key[:]); Check(Aes.Block(*expanded,plain[:],cipher[:],false),"AES block accepts exact sizes")
|
||||
Hex("8ea2b7ca516745bfeafc49904b496089",expected[:16]); Check(Crypto.Equal(cipher[:],expected[:16]),"FIPS 197 AES-256 encryption")
|
||||
Check(Aes.Block(*expanded,cipher[:],recovered[:],true) && Crypto.Equal(plain[:],recovered[:]),"FIPS AES-256 decryption")
|
||||
password: [8]u8 = .{112,97,115,115,119,111,114,100}; salt: [4]u8 = .{115,97,108,116}
|
||||
Check(Crypto.Pbkdf2(password[:],salt[:],1,result[:64]),"PBKDF2 supports multiple output blocks")
|
||||
Hex("120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b4dbf3a2f3dad3377264bb7b8e8330d4efc7451418617dabef683735361cdc18c",expected[:]); Check(Crypto.Equal(result[:],expected[:]),"PBKDF2-HMAC-SHA256 known answer")
|
||||
iv: [32]u8; data: [32]u8; encrypted: [32]u8; restored: [32]u8
|
||||
for i: 0..31 { iv[i] = cast(u8)i; data[i] = cast(u8)(i*7) }
|
||||
Check(Aes.Ige(key[:],iv[:],data[:],encrypted[:],false) && Aes.Ige(key[:],iv[:],encrypted[:],restored[:],true) && Crypto.Equal(data[:],restored[:]),"AES-IGE round trip")
|
||||
Check(!Aes.Ige(key[:],iv[:31],data[:],encrypted[:],false),"IGE rejects malformed IV")
|
||||
prime: [256]u8; power: [256]u8; base: [1]u8 = .{2}; exponent: [1]u8 = .{13}
|
||||
for i: 0..255 { prime[i] = 255 }; prime[255] = 189
|
||||
modulus := Big.Prepare(prime[:]); Check(Big.Power(base[:],exponent[:],*modulus,power[:]) && power[254]==cast(u8)32 && power[255]==cast(u8)0,"2048-bit Montgomery exponentiation")
|
||||
if failed == 0 { print("Cryptographic known-answer checks passed\n") }; return failed
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"format": 1,
|
||||
"packages": [
|
||||
|
||||
],
|
||||
"root": {
|
||||
"dependencies": {},
|
||||
"name": "crypto"
|
||||
},
|
||||
"toolchain": {
|
||||
"commit": "04ee624720f4c0abd6c28b0eb6d0a3d2f8a166c2",
|
||||
"ref": "master",
|
||||
"url": "https://github.com/ziranlang/ziran.git"
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "crypto"
|
||||
module_roots = ["src", "tests"]
|
||||
|
||||
[toolchain]
|
||||
git = "https://github.com/ziranlang/ziran.git"
|
||||
ref = "master"
|
||||
|
||||
[exports]
|
||||
crypto = "src/crypto.zi"
|
||||
sha256 = "src/sha256.zi"
|
||||
sha1 = "src/sha1.zi"
|
||||
aes = "src/aes.zi"
|
||||
bigint = "src/bigint.zi"
|
||||
Reference in new issue
Block a user