diff options
author | Mattias Andrée <maandree@kth.se> | 2019-02-10 18:37:36 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-02-10 18:38:25 +0100 |
commit | 958abe25e6882f772ff4bebfe72cca89b4b0ff8c (patch) | |
tree | fc57339324c67166d4a94803e136f82b8ff7a0f6 /hmac_init.c | |
parent | Improve makefile (diff) | |
download | libsha2-958abe25e6882f772ff4bebfe72cca89b4b0ff8c.tar.gz libsha2-958abe25e6882f772ff4bebfe72cca89b4b0ff8c.tar.bz2 libsha2-958abe25e6882f772ff4bebfe72cca89b4b0ff8c.tar.xz |
HMAC: fix support for key lengths that are not multiples of 8
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'hmac_init.c')
-rw-r--r-- | hmac_init.c | 6 |
1 files changed, 5 insertions, 1 deletions
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)) |