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> Conflicts: crypto/hmac/hmac.c
This commit is contained in:
parent
b7279ed55c
commit
929b0d70c1
@ -101,8 +101,14 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
|
|||||||
if (md != NULL) {
|
if (md != NULL) {
|
||||||
reset = 1;
|
reset = 1;
|
||||||
ctx->md = md;
|
ctx->md = md;
|
||||||
} else
|
} else if(ctx->md) {
|
||||||
md = ctx->md;
|
md = ctx->md;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!ctx->key_init && key == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (key != NULL) {
|
if (key != NULL) {
|
||||||
reset = 1;
|
reset = 1;
|
||||||
@ -124,6 +130,7 @@ int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len,
|
|||||||
if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
|
if (ctx->key_length != HMAC_MAX_MD_CBLOCK)
|
||||||
memset(&ctx->key[ctx->key_length], 0,
|
memset(&ctx->key[ctx->key_length], 0,
|
||||||
HMAC_MAX_MD_CBLOCK - ctx->key_length);
|
HMAC_MAX_MD_CBLOCK - ctx->key_length);
|
||||||
|
ctx->key_init = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reset) {
|
if (reset) {
|
||||||
@ -161,6 +168,9 @@ int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
|
|||||||
if (FIPS_mode() && !ctx->i_ctx.engine)
|
if (FIPS_mode() && !ctx->i_ctx.engine)
|
||||||
return FIPS_hmac_update(ctx, data, len);
|
return FIPS_hmac_update(ctx, data, len);
|
||||||
#endif
|
#endif
|
||||||
|
if(!ctx->key_init)
|
||||||
|
return 0;
|
||||||
|
|
||||||
return EVP_DigestUpdate(&ctx->md_ctx, data, len);
|
return EVP_DigestUpdate(&ctx->md_ctx, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,6 +183,9 @@ int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
|
|||||||
return FIPS_hmac_final(ctx, md, len);
|
return FIPS_hmac_final(ctx, md, len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if(!ctx->key_init)
|
||||||
|
goto err;
|
||||||
|
|
||||||
if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i))
|
if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i))
|
||||||
goto err;
|
goto err;
|
||||||
if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx))
|
if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx))
|
||||||
@ -191,6 +204,8 @@ void HMAC_CTX_init(HMAC_CTX *ctx)
|
|||||||
EVP_MD_CTX_init(&ctx->i_ctx);
|
EVP_MD_CTX_init(&ctx->i_ctx);
|
||||||
EVP_MD_CTX_init(&ctx->o_ctx);
|
EVP_MD_CTX_init(&ctx->o_ctx);
|
||||||
EVP_MD_CTX_init(&ctx->md_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)
|
int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
|
||||||
@ -201,8 +216,11 @@ int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
|
|||||||
goto err;
|
goto err;
|
||||||
if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
|
if (!EVP_MD_CTX_copy(&dctx->md_ctx, &sctx->md_ctx))
|
||||||
goto err;
|
goto err;
|
||||||
memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
|
dctx->key_init = sctx->key_init;
|
||||||
dctx->key_length = sctx->key_length;
|
if(sctx->key_init) {
|
||||||
|
memcpy(dctx->key, sctx->key, HMAC_MAX_MD_CBLOCK);
|
||||||
|
dctx->key_length = sctx->key_length;
|
||||||
|
}
|
||||||
dctx->md = sctx->md;
|
dctx->md = sctx->md;
|
||||||
return 1;
|
return 1;
|
||||||
err:
|
err:
|
||||||
|
@ -79,6 +79,7 @@ typedef struct hmac_ctx_st {
|
|||||||
EVP_MD_CTX o_ctx;
|
EVP_MD_CTX o_ctx;
|
||||||
unsigned int key_length;
|
unsigned int key_length;
|
||||||
unsigned char key[HMAC_MAX_MD_CBLOCK];
|
unsigned char key[HMAC_MAX_MD_CBLOCK];
|
||||||
|
int key_init;
|
||||||
} HMAC_CTX;
|
} HMAC_CTX;
|
||||||
|
|
||||||
# define HMAC_size(e) (EVP_MD_size((e)->md))
|
# define HMAC_size(e) (EVP_MD_size((e)->md))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user