From cda19b080eee1c8057ebcb4369455582949ed20e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 4 Sep 2024 19:41:56 +0200 Subject: Fix and test BLAKE2 support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 2 ++ libhashsum_init_blake2b_hasher.c | 17 +++++++++-------- libhashsum_init_blake2s_hasher.c | 10 ++++++---- t/blake2b.c | 30 ++++++++++++++++++++++++++++++ t/blake2s.c | 22 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 t/blake2b.c create mode 100644 t/blake2s.c diff --git a/Makefile b/Makefile index 6a88b66..ecb36af 100644 --- a/Makefile +++ b/Makefile @@ -119,6 +119,8 @@ TEST =\ t/blake256.t\ t/blake384.t\ t/blake512.t\ + t/blake2s.t\ + t/blake2b.t\ t/misc-tests.t LOBJ = $(OBJ:.o=.lo) diff --git a/libhashsum_init_blake2b_hasher.c b/libhashsum_init_blake2b_hasher.c index 57be6fe..06504d3 100644 --- a/libhashsum_init_blake2b_hasher.c +++ b/libhashsum_init_blake2b_hasher.c @@ -69,7 +69,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (!this->hash_output) - this->hash_output = this->state.blake224.buf; + this->hash_output = this->state.blake2b.buf; if (this->state.blake2b.keybytes) { bytes = BLAKE2B_BLOCK_SIZE; @@ -92,16 +92,15 @@ __attribute__((__pure__)) static int allzeroes(const uint8_t *data, size_t n) { - if (data) - while (n--) - if (data[n]) - return 0; + while (n--) + if (data[n]) + return 0; return 1; } static char * -hex(char *buf, const uint8_t *data, size_t n) +hex(char *restrict buf, const uint8_t *restrict data, size_t n) { size_t i; for (i = 0; i < n; i++) { @@ -151,12 +150,14 @@ libhashsum_init_blake2b_hasher(struct libhashsum_hasher *this, size_t hashbits, .node_depth = 0U, .inner_len = 0U }; - if (((hashbits | keybits) & 7U) || ((hashbits | keybits) > 256U)) { + if (((hashbits | keybits) & 7U) || hashbits > 512U || keybits > 512U) { errno = EINVAL; return -1; } - if (!hashbits) + if (!hashbits) { hashbits = 512U; + params.digest_len = (unsigned char)(hashbits / 8U); + } libblake_init(); this->algorithm = LIBHASHSUM_BLAKE2B; this->algorithm_string = mkalgostr(this->state.blake2b.algostr, "BLAKE2b", diff --git a/libhashsum_init_blake2s_hasher.c b/libhashsum_init_blake2s_hasher.c index 0957b22..689dc64 100644 --- a/libhashsum_init_blake2s_hasher.c +++ b/libhashsum_init_blake2s_hasher.c @@ -69,7 +69,7 @@ finalise(struct libhashsum_hasher *this, void *data, size_t bytes, unsigned extr size -= r; if (!this->hash_output) - this->hash_output = this->state.blake224.buf; + this->hash_output = this->state.blake2s.buf; if (this->state.blake2s.keybytes) { bytes = BLAKE2S_BLOCK_SIZE; @@ -100,7 +100,7 @@ allzeroes(const uint8_t *data, size_t n) static char * -hex(char *buf, const uint8_t *data, size_t n) +hex(char *restrict buf, const uint8_t *restrict data, size_t n) { size_t i; for (i = 0; i < n; i++) { @@ -150,12 +150,14 @@ libhashsum_init_blake2s_hasher(struct libhashsum_hasher *this, size_t hashbits, .node_depth = 0U, .inner_len = 0U }; - if (((hashbits | keybits) & 7U) || ((hashbits | keybits) > 256U)) { + if (((hashbits | keybits) & 7U) || hashbits > 256U || keybits > 256U) { errno = EINVAL; return -1; } - if (!hashbits) + if (!hashbits) { hashbits = 256U; + params.digest_len = (unsigned char)(hashbits / 8U); + } libblake_init(); this->algorithm = LIBHASHSUM_BLAKE2S; this->algorithm_string = mkalgostr(this->state.blake2s.algostr, "BLAKE2s", diff --git a/t/blake2b.c b/t/blake2b.c new file mode 100644 index 0000000..8f0c877 --- /dev/null +++ b/t/blake2b.c @@ -0,0 +1,30 @@ +/* See LICENSE file for copyright and license details. */ +#ifdef SUPPORT_BLAKE2B +# define TEST +# include "../common.h" + + +static struct testcase testcases[] = { + {1, 0, "", + "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419" + "d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"}, + {1, 0, "The quick brown fox jumps over the lazy dog", + "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673" + "f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918"}, + {1, 0, "The quick brown fox jumps over the lazy dof", + "ab6b007747d8068c02e25a6008db8a77c218d94f3b40d2291a7dc8a62090a744" + "c082ea27af01521a102e42f480a31e9844053f456b4b41e8aa78bbe5c12957bb"} +}; + + +#else +# define TEST_UNSUPPORTED +# include "../common.h" +#endif + + +int +main(void) +{ + TEST_MAIN("BLAKE2b", BLAKE2B, 0); +} diff --git a/t/blake2s.c b/t/blake2s.c new file mode 100644 index 0000000..58408df --- /dev/null +++ b/t/blake2s.c @@ -0,0 +1,22 @@ +/* See LICENSE file for copyright and license details. */ +#ifdef SUPPORT_BLAKE2S +# define TEST +# include "../common.h" + + +static struct testcase testcases[] = { + {1, 0, "", "69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9"} +}; + + +#else +# define TEST_UNSUPPORTED +# include "../common.h" +#endif + + +int +main(void) +{ + TEST_MAIN("BLAKE2s", BLAKE2S, 0); +} -- cgit v1.2.3-70-g09d2