From 4ee9d767bb3807ed728a016d1a1527a1d1d7a53b Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Mon, 10 Apr 2017 18:29:51 -0400 Subject: [PATCH] add key/keylen parameters to blake2x_init --- src/hashes/blake2b.c | 35 ++++++++++++++++++++++++++++------- src/hashes/blake2s.c | 36 +++++++++++++++++++++++++++--------- src/headers/tomcrypt_hash.h | 4 ++-- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/hashes/blake2b.c b/src/hashes/blake2b.c index 3765b23..20a36e8 100644 --- a/src/hashes/blake2b.c +++ b/src/hashes/blake2b.c @@ -199,30 +199,51 @@ static int blake2b_init_param(hash_state *md, const unsigned char *P) return CRYPT_OK; } -int blake2b_init(hash_state *md, unsigned long outlen) +int blake2b_init(hash_state *md, unsigned long outlen, const unsigned char *key, unsigned long keylen) { unsigned char P[BLAKE2B_PARAM_SIZE]; + int err; LTC_ARGCHK(md != NULL); if ((!outlen) || (outlen > BLAKE2B_OUTBYTES)) return CRYPT_INVALID_ARG; + if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2B_KEYBYTES)) + return CRYPT_INVALID_ARG; + XMEMSET(P, 0, sizeof(P)); P[O_DIGEST_LENGTH] = (unsigned char)outlen; + P[O_KEY_LENGTH] = (unsigned char)keylen; P[O_FANOUT] = 1; P[O_DEPTH] = 1; - return blake2b_init_param(md, P); + + err = blake2b_init_param(md, P); + if (err != CRYPT_OK) return err; + + if (key) { + unsigned char block[BLAKE2B_BLOCKBYTES]; + + XMEMSET(block, 0, BLAKE2B_BLOCKBYTES); + XMEMCPY(block, key, keylen); + blake2b_process(md, block, BLAKE2B_BLOCKBYTES); + +#ifdef LTC_CLEAN_STACK + zeromem(block, sizeof(block)); +#endif + } + + return CRYPT_OK; } -int blake2b_160_init(hash_state *md) { return blake2b_init(md, 20); } +int blake2b_160_init(hash_state *md) { return blake2b_init(md, 20, NULL, 0); } -int blake2b_256_init(hash_state *md) { return blake2b_init(md, 32); } +int blake2b_256_init(hash_state *md) { return blake2b_init(md, 32, NULL, 0); } -int blake2b_384_init(hash_state *md) { return blake2b_init(md, 48); } +int blake2b_384_init(hash_state *md) { return blake2b_init(md, 48, NULL, 0); } -int blake2b_512_init(hash_state *md) { return blake2b_init(md, 64); } +int blake2b_512_init(hash_state *md) { return blake2b_init(md, 64, NULL, 0); } #define G(r, i, a, b, c, d) \ do { \ @@ -302,7 +323,7 @@ static int blake2b_compress(hash_state *md, const unsigned char *buf) { int err; err = _blake2b_compress(md, buf); - burn_stack(sizeof(ulong64) * (32 + 1)); + burn_stack(sizeof(ulong64) * 32 + sizeof(unsigned long)); return err; } #endif diff --git a/src/hashes/blake2s.c b/src/hashes/blake2s.c index d39d45b..c385c7a 100644 --- a/src/hashes/blake2s.c +++ b/src/hashes/blake2s.c @@ -193,32 +193,50 @@ static int blake2s_init_param(hash_state *md, const unsigned char *P) return CRYPT_OK; } -/* Sequential blake2s initialization */ -int blake2s_init(hash_state *md, unsigned long outlen) +int blake2s_init(hash_state *md, unsigned long outlen, const unsigned char *key, unsigned long keylen) { unsigned char P[BLAKE2S_PARAM_SIZE]; + int err; + LTC_ARGCHK(md != NULL); if ((!outlen) || (outlen > BLAKE2S_OUTBYTES)) return CRYPT_INVALID_ARG; + if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2S_KEYBYTES)) + return CRYPT_INVALID_ARG; + XMEMSET(P, 0, sizeof(P)); P[O_DIGEST_LENGTH] = (unsigned char)outlen; - + P[O_KEY_LENGTH] = (unsigned char)keylen; P[O_FANOUT] = 1; P[O_DEPTH] = 1; - return blake2s_init_param(md, P); + err = blake2s_init_param(md, P); + if (err != CRYPT_OK) return err; + + if (key) { + unsigned char block[BLAKE2S_BLOCKBYTES]; + + XMEMSET(block, 0, BLAKE2S_BLOCKBYTES); + XMEMCPY(block, key, keylen); + blake2s_process(md, block, BLAKE2S_BLOCKBYTES); + +#ifdef LTC_CLEAN_STACK + zeromem(block, sizeof(block)); +#endif + } + return CRYPT_OK; } -int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16); } +int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16, NULL, 0); } -int blake2s_160_init(hash_state *md) { return blake2s_init(md, 20); } +int blake2s_160_init(hash_state *md) { return blake2s_init(md, 20, NULL, 0); } -int blake2s_224_init(hash_state *md) { return blake2s_init(md, 28); } +int blake2s_224_init(hash_state *md) { return blake2s_init(md, 28, NULL, 0); } -int blake2s_256_init(hash_state *md) { return blake2s_init(md, 32); } +int blake2s_256_init(hash_state *md) { return blake2s_init(md, 32, NULL, 0); } #define G(r, i, a, b, c, d) \ do { \ @@ -293,7 +311,7 @@ static int blake2s_compress(hash_state *md, const unsigned char *buf) { int err; err = _blake2s_compress(md, buf); - burn_stack(sizeof(ulong32) * (32 + 2)); + burn_stack(sizeof(ulong32) * (32) + sizeof(unsigned long)); return err; } #endif diff --git a/src/headers/tomcrypt_hash.h b/src/headers/tomcrypt_hash.h index 0f09861..a07cbc8 100644 --- a/src/headers/tomcrypt_hash.h +++ b/src/headers/tomcrypt_hash.h @@ -362,7 +362,7 @@ extern const struct ltc_hash_descriptor blake2s_128_desc; int blake2s_128_init(hash_state * md); int blake2s_128_test(void); -int blake2s_init(hash_state * md, unsigned long outlen); +int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen); int blake2s_done(hash_state * md, unsigned char *hash); #endif @@ -384,7 +384,7 @@ extern const struct ltc_hash_descriptor blake2b_160_desc; int blake2b_160_init(hash_state * md); int blake2b_160_test(void); -int blake2b_init(hash_state * md, unsigned long outlen); +int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen); int blake2b_done(hash_state * md, unsigned char *hash); #endif