From 958abe25e6882f772ff4bebfe72cca89b4b0ff8c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 10 Feb 2019 18:37:36 +0100 Subject: HMAC: fix support for key lengths that are not multiples of 8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- hmac_init.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hmac_init.c b/hmac_init.c index 17b31ce..b60ebef 100644 --- a/hmac_init.c +++ b/hmac_init.c @@ -29,10 +29,14 @@ libsha2_hmac_init(struct libsha2_hmac_state *restrict state, enum libsha2_algori if (keylen <= state->sha2_state.chunk_size * 8) { memset(state->ipad, 0x36, sizeof(state->ipad)); memset(state->opad, 0x5C, sizeof(state->opad)); - for (i = 0, keylen /= 8; i < keylen; i++) { + for (i = 0; i < keylen / 8; i++) { state->ipad[i] ^= key[i]; state->opad[i] ^= key[i]; } + if (keylen & 7) { + state->ipad[i] ^= (unsigned char)(key[i] << (8 - (keylen & 7))); + state->opad[i] ^= (unsigned char)(key[i] << (8 - (keylen & 7))); + } } else { memset(state->ipad, 0, sizeof(state->ipad)); if (libsha2_init(&state->sha2_state, algorithm)) -- cgit v1.2.3-70-g09d2