Hashes run in portable bundles: SHA-256 hashes slices with Sha256UpdateBytes and Sha256FinishBytes beneath its pointer forms and string forms, SHA-512 keeps its round constants as 32-bit halves; a portable test bundle checks both
This commit is contained in:
1 parent
a4cd5bbe49
commit
d11e915b5e
4 files changed
+125
-29
No files matched your search
@@ -1,8 +1,17 @@
|
||||
ZIRAN ?= ziran
|
||||
LOCKED := $(if $(wildcard ziran.local.toml),,--locked)
|
||||
.PHONY: check test
|
||||
.PHONY: check test portable
|
||||
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
|
||||
$(MAKE) portable
|
||||
|
||||
# The hashes portable bundles use, built and run as a bundle.
|
||||
portable:
|
||||
mkdir -p build
|
||||
$(ZIRAN) bundle --root . --module-path src --module-path "$$($(ZIRAN) pkg path ziran $(LOCKED))/std" \
|
||||
--entry portable_test:Run -o build/portable.zx tests/portable_test.zi
|
||||
test "$$($(ZIRAN) run build/portable.zx)" = 0
|
||||
@echo 'Portable hashes passed'
|
||||
+56
-6
@@ -28,10 +28,11 @@ Sha256RotateRight :: (value: u32, bits: u32) -> u32 {
|
||||
}
|
||||
|
||||
Sha256Transform :: (context: *Sha256Context) {
|
||||
Sha256TransformBlock(context, *context.block[0])
|
||||
Sha256TransformBlock(context, context.block[:])
|
||||
}
|
||||
|
||||
Sha256TransformBlock :: (context: *Sha256Context, block: *u8) {
|
||||
// One 64-byte block. It takes a slice, so portable bundles can hash too.
|
||||
Sha256TransformBlock :: (context: *Sha256Context, block: []u8) {
|
||||
words: [64]u32
|
||||
index: s32 = 0
|
||||
while index < 16 {
|
||||
@@ -111,7 +112,30 @@ Sha256Update :: (context: *Sha256Context, data: *u8, length: s64) {
|
||||
while index < length {
|
||||
// Whole blocks are hashed where they lie instead of byte by byte.
|
||||
if context.block_length == 0 && length - index >= 64 {
|
||||
Sha256TransformBlock(context, *data[index])
|
||||
whole: [64]u8
|
||||
for at: 0..63 { whole[at] = data[index + at] }
|
||||
Sha256TransformBlock(context, whole[:])
|
||||
context.bit_length += 512
|
||||
index += 64
|
||||
continue
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes from a slice, for portable bundles; Sha256Update takes a pointer.
|
||||
Sha256UpdateBytes :: (context: *Sha256Context, data: []u8) {
|
||||
index: s64 = 0
|
||||
while index < data.count {
|
||||
if context.block_length == 0 && data.count - index >= 64 {
|
||||
Sha256TransformBlock(context, data[index:index + 64])
|
||||
context.bit_length += 512
|
||||
index += 64
|
||||
continue
|
||||
@@ -128,6 +152,13 @@ Sha256Update :: (context: *Sha256Context, data: *u8, length: s64) {
|
||||
}
|
||||
|
||||
Sha256Finish :: (context: *Sha256Context, output: *u8) {
|
||||
digest: [32]u8
|
||||
Sha256FinishBytes(context, digest[:])
|
||||
for index: 0..31 { output[index] = digest[index] }
|
||||
}
|
||||
|
||||
// The digest into a slice of 32 bytes, for portable bundles.
|
||||
Sha256FinishBytes :: (context: *Sha256Context, output: []u8) {
|
||||
tail_length := context.block_length
|
||||
index := tail_length
|
||||
context.block[index] = cast(u8)0x80
|
||||
@@ -354,13 +385,32 @@ Sha256Of :: (data: string) -> [32]u8 {
|
||||
context: Sha256Context
|
||||
Sha256Initialize(*context)
|
||||
Sha256UpdateText(*context, data)
|
||||
Sha256Finish(*context, *digest[0])
|
||||
Sha256FinishBytes(*context, digest[:])
|
||||
return digest
|
||||
}
|
||||
|
||||
HmacSha256Of :: (key: string, message: string) -> [32]u8 {
|
||||
digest: [32]u8
|
||||
unused HmacSha256Input(null, key.count, key, true, null, message.count, message, true, *digest[0])
|
||||
block: [HmacBlockBytes]u8
|
||||
if key.count > HmacBlockBytes {
|
||||
hashed := Sha256Of(key)
|
||||
for index: 0..31 { block[index] = hashed[index] }
|
||||
} else {
|
||||
for index: cast(s64)0..key.count - 1 { block[index] = key[index] }
|
||||
}
|
||||
pad: [HmacBlockBytes]u8
|
||||
for index: 0..HmacBlockBytes - 1 { pad[index] = block[index] ^ cast(u8)0x36 }
|
||||
context: Sha256Context
|
||||
Sha256Initialize(*context)
|
||||
Sha256UpdateBytes(*context, pad[:])
|
||||
Sha256UpdateText(*context, message)
|
||||
inner: [Sha256DigestBytes]u8
|
||||
Sha256FinishBytes(*context, inner[:])
|
||||
for index: 0..HmacBlockBytes - 1 { pad[index] = block[index] ^ cast(u8)0x5c }
|
||||
Sha256Initialize(*context)
|
||||
Sha256UpdateBytes(*context, pad[:])
|
||||
Sha256UpdateBytes(*context, inner[:])
|
||||
digest: [Sha256DigestBytes]u8
|
||||
Sha256FinishBytes(*context, digest[:])
|
||||
return digest
|
||||
}
|
||||
|
||||
|
||||
+25
-22
@@ -1,33 +1,36 @@
|
||||
// FIPS 180-4 SHA-512 and HMAC-SHA-512 in portable Ziran.
|
||||
Context :: struct { state: [8]u64; block: [128]u8; used: s32; length: u64; }
|
||||
Constants: [80]u64=.{
|
||||
0x428a2f98d728ae22,0x7137449123ef65cd,0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc,
|
||||
0x3956c25bf348b538,0x59f111f1b605d019,0x923f82a4af194f9b,0xab1c5ed5da6d8118,
|
||||
0xd807aa98a3030242,0x12835b0145706fbe,0x243185be4ee4b28c,0x550c7dc3d5ffb4e2,
|
||||
0x72be5d74f27b896f,0x80deb1fe3b1696b1,0x9bdc06a725c71235,0xc19bf174cf692694,
|
||||
0xe49b69c19ef14ad2,0xefbe4786384f25e3,0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65,
|
||||
0x2de92c6f592b0275,0x4a7484aa6ea6e483,0x5cb0a9dcbd41fbd4,0x76f988da831153b5,
|
||||
0x983e5152ee66dfab,0xa831c66d2db43210,0xb00327c898fb213f,0xbf597fc7beef0ee4,
|
||||
0xc6e00bf33da88fc2,0xd5a79147930aa725,0x06ca6351e003826f,0x142929670a0e6e70,
|
||||
0x27b70a8546d22ffc,0x2e1b21385c26c926,0x4d2c6dfc5ac42aed,0x53380d139d95b3df,
|
||||
0x650a73548baf63de,0x766a0abb3c77b2a8,0x81c2c92e47edaee6,0x92722c851482353b,
|
||||
0xa2bfe8a14cf10364,0xa81a664bbc423001,0xc24b8b70d0f89791,0xc76c51a30654be30,
|
||||
0xd192e819d6ef5218,0xd69906245565a910,0xf40e35855771202a,0x106aa07032bbd1b8,
|
||||
0x19a4c116b8d2d0c8,0x1e376c085141ab53,0x2748774cdf8eeb99,0x34b0bcb5e19b48a8,
|
||||
0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb,0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3,
|
||||
0x748f82ee5defb2fc,0x78a5636f43172f60,0x84c87814a1f0ab72,0x8cc702081a6439ec,
|
||||
0x90befffa23631e28,0xa4506cebde82bde9,0xbef9a3f7b2c67915,0xc67178f2e372532b,
|
||||
0xca273eceea26619c,0xd186b8c721c0c207,0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178,
|
||||
0x06f067aa72176fba,0x0a637dc5a2c898a6,0x113f9804bef90dae,0x1b710b35131c471b,
|
||||
0x28db77f523047d84,0x32caab7b40c72493,0x3c9ebe0a15c9bebc,0x431d67c49c100d4c,
|
||||
0x4cc5d4becb3e42b6,0x597f299cfc657e2a,0x5fcb6fab3ad6faec,0x6c44198c4a475817};
|
||||
// The 80 round constants as high and low 32-bit halves, as portable
|
||||
// bundles take no 64-bit literal above 2^63.
|
||||
ConstantHalves: [160]u32 = .[
|
||||
0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
|
||||
0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
|
||||
0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
|
||||
0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
|
||||
0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
|
||||
0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
|
||||
0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
|
||||
0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
|
||||
0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
|
||||
0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
|
||||
0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
|
||||
0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
|
||||
0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
|
||||
0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
|
||||
0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
|
||||
0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
|
||||
0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
|
||||
0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
|
||||
0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
|
||||
0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817];
|
||||
Constant :: (index: s32) -> u64 { return (cast(u64)ConstantHalves[index * 2] << cast(u64)32) | cast(u64)ConstantHalves[index * 2 + 1] }
|
||||
Rotate :: (x: u64,n: u64) -> u64 { return (x>>n)|(x<<(cast(u64)64-n)) }
|
||||
Transform :: (context: *Context) {
|
||||
w: [80]u64
|
||||
for i: 0..15 { for j: 0..7 { w[i]=(w[i]<<cast(u64)8)|cast(u64)context.block[i*8+j] } }
|
||||
for i: 16..79 { a:=w[i-15]; b:=w[i-2]; w[i]=w[i-16]+(Rotate(a,1)^Rotate(a,8)^(a>>cast(u64)7))+w[i-7]+(Rotate(b,19)^Rotate(b,61)^(b>>cast(u64)6)) }
|
||||
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]
|
||||
for i: 0..79 { t:=h+(Rotate(e,14)^Rotate(e,18)^Rotate(e,41))+((e&f)^((~e)&g))+Constants[i]+w[i]; u: u64=(Rotate(a,28)^Rotate(a,34)^Rotate(a,39))+((a&b)^(a&c)^(b&c));h=g;g=f;f=e;e=d+t;d=c;c=b;b=a;a=t+u }
|
||||
for i: 0..79 { t:=h+(Rotate(e,14)^Rotate(e,18)^Rotate(e,41))+((e&f)^((~e)&g))+Constant(cast(s32)i)+w[i]; u: u64=(Rotate(a,28)^Rotate(a,34)^Rotate(a,39))+((a&b)^(a&c)^(b&c));h=g;g=f;f=e;e=d+t;d=c;c=b;b=a;a=t+u }
|
||||
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
|
||||
}
|
||||
Start :: (context: *Context) { context.state=.{0x6a09e667f3bcc908,0xbb67ae8584caa73b,0x3c6ef372fe94f82b,0xa54ff53a5f1d36f1,0x510e527fade682d1,0x9b05688c2b3e6c1f,0x1f83d9abfb41bd6b,0x5be0cd19137e2179};context.used=0;context.length=0 }
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// The hashes portable bundles use, built and run as a bundle: SHA-256's
|
||||
// string forms, SHA-1 and SHA-512 over slices.
|
||||
Hash :: #import "sha256";
|
||||
Sha1 :: #import "sha1";
|
||||
Sha512 :: #import "sha512";
|
||||
|
||||
hex_text: [128]u8;
|
||||
|
||||
Hex :: (bytes: []u8) -> string {
|
||||
digits := "0123456789abcdef"
|
||||
text := hex_text[:]
|
||||
for index: 0..cast(s32)bytes.count - 1 {
|
||||
text[index * 2] = digits[cast(s64)(bytes[index] >> cast(u8)4)]
|
||||
text[index * 2 + 1] = digits[cast(s64)(bytes[index] & cast(u8)15)]
|
||||
}
|
||||
return TextView(hex_text[0:bytes.count * 2])
|
||||
}
|
||||
|
||||
#program_export
|
||||
Run :: () -> s32 {
|
||||
failed: s32 = 0
|
||||
digest := Hash.Sha256Of("abc")
|
||||
if Hex(digest[:]) != "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" { failed += 1 }
|
||||
mac := Hash.HmacSha256Of("Jefe", "what do ya want for nothing?")
|
||||
if Hex(mac[:]) != "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" { failed += 1 }
|
||||
derived := Hash.Pbkdf2Sha256("password", "salt", 2)
|
||||
if Hex(derived[:]) != "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43" { failed += 1 }
|
||||
abc: [3]u8 = .[97, 98, 99]
|
||||
short: [20]u8
|
||||
if !Sha1.Sha1(abc[:], short[:]) || Hex(short[:]) != "a9993e364706816aba3e25717850c26c9cd0d89d" { failed += 1 }
|
||||
long: [64]u8
|
||||
if !Sha512.Sha512(abc[:], long[:]) || Hex(long[:]) != "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" { failed += 1 }
|
||||
return failed
|
||||
}
|
||||
Reference in new issue
Block a user