From 2edbc04bc7b5a5b3d5100c0f27bbbe4423d3ceda Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Mon, 21 Dec 2015 10:49:09 -0600 Subject: [PATCH] properly handle longer HMAC keys for hmac_sha384 and hmac_sha512 --- lib/hmac.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/hmac.c b/lib/hmac.c index 1dd5d60c..52a36b0e 100644 --- a/lib/hmac.c +++ b/lib/hmac.c @@ -320,10 +320,16 @@ hmac_sha384_init(hmac_sha384_ctx *ctx, const char *key, const int key_len) if(key_len > MAX_DIGEST_BLOCK_LEN) final_len = MAX_DIGEST_BLOCK_LEN; - /* When we eventually support arbitrary key sizes, take the digest - * of the key with: sha384(final_key, init_key, final_len); - */ - memcpy(final_key, key, final_len); + if(SHA384_BLOCK_LEN < key_len) + { + /* Calculate the digest of the key + */ + sha384(final_key, (unsigned char *)key, final_len); + } + else + { + memcpy(final_key, key, key_len); + } pad_init(ctx->block_inner_pad, ctx->block_outer_pad, final_key, final_len); @@ -382,10 +388,17 @@ hmac_sha512_init(hmac_sha512_ctx *ctx, const char *key, const int key_len) if(key_len > MAX_DIGEST_BLOCK_LEN) final_len = MAX_DIGEST_BLOCK_LEN; - /* When we eventually support arbitrary key sizes, take the digest - * of the key with: sha512(final_key, init_key, final_len); - */ - memcpy(final_key, key, final_len); + if(SHA512_BLOCK_LEN < key_len) + { + /* Calculate the digest of the key + */ + sha512(final_key, (unsigned char *)key, final_len); + final_len = SHA512_DIGEST_LEN; + } + else + { + memcpy(final_key, key, key_len); + } pad_init(ctx->block_inner_pad, ctx->block_outer_pad, final_key, final_len);