diff options
author | Matt Caswell <matt@openssl.org> | 2015-02-10 11:39:52 +0000 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2015-03-25 12:54:11 +0000 |
commit | e2095c6531da1207a936b65730db0222831a93ed (patch) | |
tree | 1a5ef888b2b8e5511fd495c1ef428833abd9e6c7 /crypto/hmac | |
parent | 2cfbdd71dde0c3ddf4597eb20cc3e3fb8485fc15 (diff) |
Ensure that both the MD and key have been initialised before attempting to
create an HMAC
Inspired by BoringSSL commit 2fe7f2d0d9a6fcc75b4e594eeec306cc55acd594
Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'crypto/hmac')
-rw-r--r-- | crypto/hmac/hmac.c | 23 | ||||
-rw-r--r-- | crypto/hmac/hmac.h | 1 |
2 files changed, 21 insertions, 3 deletions
diff --git a/crypto/hmac/hmac.c b/crypto/hmac/hmac.c index 3a4dfa4f40..f1fdba42d8 100644 --- a/crypto/hmac/hmac.c +++ b/crypto/hmac/hmac.c @@ -71,8 +71,14 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, if (md != NULL) { reset = 1; ctx->md = md; - } else + } else if(ctx->md) { md = ctx->md; + } else { + return 0; + } + + if(!ctx->key_init && key == NULL) + return 0; if (key != NULL) { reset = 1; @@ -94,6 +100,7 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, if (ctx->key_length != HMAC_MAX_MD_CBLOCK) memset(&ctx->key[ctx->key_length], 0, HMAC_MAX_MD_CBLOCK - ctx->key_length); + ctx->key_init = 1; } if (reset) { @@ -129,6 +136,8 @@ int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) { + if(!ctx->key_init) + return 0; return EVP_DigestUpdate(&ctx->md_ctx, data, len); } @@ -137,6 +146,9 @@ int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) unsigned int i; unsigned char buf[EVP_MAX_MD_SIZE]; + if(!ctx->key_init) + goto err; + if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i)) goto err; if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx)) @@ -155,6 +167,8 @@ void HMAC_CTX_init(HMAC_CTX *ctx) EVP_MD_CTX_init(&ctx->i_ctx); EVP_MD_CTX_init(&ctx->o_ctx); EVP_MD_CTX_init(&ctx->md_ctx); + ctx->key_init = 0; + ctx->md = NULL; } int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) @@ -166,8 +180,11 @@ int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) goto err; if (!EVP_MD_CTX_copy_ex(&dctx->md_ctx, &sctx->md_ctx)) goto err; - memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK); - dctx->key_length = sctx->key_length; + dctx->key_init = sctx->key_init; + if(sctx->key_init) { + memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK); + dctx->key_length = sctx->key_length; + } dctx->md = sctx->md; return 1; err: diff --git a/crypto/hmac/hmac.h b/crypto/hmac/hmac.h index 81aa49da49..61946fc456 100644 --- a/crypto/hmac/hmac.h +++ b/crypto/hmac/hmac.h @@ -75,6 +75,7 @@ typedef struct hmac_ctx_st { EVP_MD_CTX o_ctx; unsigned int key_length; unsigned char key[HMAC_MAX_MD_CBLOCK]; + int key_init; } HMAC_CTX; # define HMAC_size(e) (EVP_MD_size((e)->md)) |