commit 7c8be881cf6e4cc541d9ff89799671afc820cb80
parent 368aafe3b7f7d2793c926ef44a79a48f88a5741f
Author: Armin Preiml <apreiml@strohwolke.at>
Date: Fri, 10 Dec 2021 19:28:26 +0100
fix blake2b when writing multiple times
Make sure to not skip updating tlow on multiple smaller writes.
Signed-off-by: Armin Preiml <apreiml@strohwolke.at>
Diffstat:
4 files changed, 42 insertions(+), 8 deletions(-)
diff --git a/crypto/blake2b/+test.ha b/crypto/blake2b/+test.ha
@@ -1,3 +1,4 @@
+use bytes;
use encoding::hex;
use fmt;
use hash;
@@ -61,3 +62,37 @@ use strio;
};
};
};
+
+@test fn blake2b_multiple_writes() void = {
+ let in: [_]u8 = [
+ 0x20, 0x00, 0x00, 0x00, 0x75, 0x96, 0xf8, 0xa3, 0x2f, 0xb7,
+ 0xcf, 0x12, 0x83, 0x05, 0x0f, 0xbd, 0x4b, 0x48, 0x97, 0x70,
+ 0xe1, 0x67, 0x90, 0x1d, 0xc2, 0x02, 0x63, 0x31, 0x48, 0x2c,
+ 0xda, 0xdc, 0xf4, 0x37, 0x3b, 0xa1, 0x33, 0x10, 0xb8, 0xb9,
+ 0x91, 0x1e, 0xc5, 0xc8, 0xb7, 0x45, 0xcc, 0x3c, 0x45, 0x26,
+ 0xf4, 0x95, 0xf1, 0x79, 0x1b, 0x0b, 0xe4, 0x5f, 0xed, 0xdf,
+ 0x5e, 0xbf, 0x61, 0xef, 0xa6, 0x21, 0x12, 0x4b, 0x8a, 0x81,
+ 0x65, 0xe8, 0x92, 0x3d, 0xe4, 0x99, 0x66, 0x76, 0x4e, 0x68,
+ 0x46, 0xfe, 0x22, 0x5b, 0xce, 0xce, 0x80, 0x86, 0x72, 0xa5,
+ 0x0d, 0x23, 0x45, 0xd3, 0x27, 0x42, 0x4b, 0xf7, 0x34, 0x31,
+ 0xd5, 0x17, 0x8d, 0x48, 0x87, 0x6a, 0x1b, 0x52, 0x32, 0xc8,
+ 0x86, 0x7b, 0x42, 0x57, 0xc7, 0xd0, 0xe1, 0x27, 0x79, 0x53,
+ 0xd6, 0xf6, 0xb1, 0xcb, 0x3f, 0x9b, 0xed, 0x28, 0xb4,
+ ];
+
+ let expected: [_]u8 = [
+ 0xf8, 0x9a, 0x3a, 0x42, 0x54, 0x89, 0x3a, 0xe7, 0x48, 0xa7,
+ 0x76, 0xb8, 0x45, 0x1e, 0x15, 0x5c, 0x13, 0x56, 0x33, 0xac,
+ 0x23, 0x30, 0xb6, 0xb7, 0x74, 0xe7, 0x93, 0x7e, 0x29, 0xfa,
+ 0xcd, 0x3e,
+ ];
+
+ let result: [32]u8 = [0...];
+
+ let h = blake2b([], len(result));
+ hash::write(&h, in[..4]);
+ hash::write(&h, in[4..]);
+ hash::finish(&h, result[..]);
+
+ assert(bytes::equal(expected, result));
+};
diff --git a/crypto/blake2b/blake2b.ha b/crypto/blake2b/blake2b.ha
@@ -76,16 +76,13 @@ fn write(st: *io::stream, buf: const []u8) (size | io::error) = {
let h = st: *digest;
let length = len(buf);
let buf = buf[..];
- if (h.blocklen == BSIZE) {
- assert(h.tlow == 0 && h.thigh == 0 && h.keylen != 0);
- h.tlow += BSIZE;
- };
+
for (h.blocklen + len(buf) > BSIZE) {
const n = BSIZE - h.blocklen;
h.block[h.blocklen..] = buf[..n];
buf = buf[n..];
- h.tlow += n;
+ h.tlow += BSIZE;
if (h.tlow < n: u64) h.thigh += 1;
compress(&h.h, &h.block, h.tlow, h.thigh, false);
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -212,11 +212,13 @@ crypto_blake2b() {
if [ $testing -eq 0 ]
then
gensrcs_crypto_blake2b
+ gen_ssa crypto::blake2b encoding::hex fmt hash io strings \
+ strio crypto::math endian
else
gensrcs_crypto_blake2b +test.ha vectors+test.ha
+ gen_ssa crypto::blake2b encoding::hex fmt hash io strings \
+ strio crypto::math endian bytes
fi
- gen_ssa crypto::blake2b encoding::hex fmt hash io strings strio \
- crypto::math endian
}
crypto_cipher() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -2387,7 +2387,7 @@ testlib_crypto_blake2b_any_srcs= \
$(STDLIB)/crypto/blake2b/+test.ha \
$(STDLIB)/crypto/blake2b/vectors+test.ha
-$(TESTCACHE)/crypto/blake2b/crypto_blake2b-any.ssa: $(testlib_crypto_blake2b_any_srcs) $(testlib_rt) $(testlib_encoding_hex_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_crypto_math_$(PLATFORM)) $(testlib_endian_$(PLATFORM))
+$(TESTCACHE)/crypto/blake2b/crypto_blake2b-any.ssa: $(testlib_crypto_blake2b_any_srcs) $(testlib_rt) $(testlib_encoding_hex_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_crypto_math_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_bytes_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/crypto/blake2b
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::blake2b \