hcrypto: HMAC_Init_Ex(): return int like OpenSSL

This commit is contained in:
Nicolas Williams
2022-01-12 17:46:39 -06:00
parent 77392d5d9c
commit 52f3dc6aa4
2 changed files with 18 additions and 7 deletions

View File

@@ -85,7 +85,7 @@ HMAC_size(const HMAC_CTX *ctx)
return EVP_MD_size(ctx->md);
}
void
int
HMAC_Init_ex(HMAC_CTX *ctx,
const void *key,
size_t keylen,
@@ -103,17 +103,26 @@ HMAC_Init_ex(HMAC_CTX *ctx,
ctx->md = md;
ctx->key_length = EVP_MD_size(ctx->md);
ctx->opad = NULL;
ctx->ipad = NULL;
ctx->ctx = NULL;
ctx->buf = malloc(ctx->key_length);
ctx->opad = malloc(blockSize);
ctx->ipad = malloc(blockSize);
ctx->ctx = EVP_MD_CTX_create();
if (ctx->buf)
ctx->opad = malloc(blockSize);
if (ctx->opad)
ctx->ipad = malloc(blockSize);
if (ctx->ipad)
ctx->ctx = EVP_MD_CTX_create();
if (!ctx->buf || !ctx->opad || !ctx->ipad || !ctx->ctx)
return 0;
}
#if 0
ctx->engine = engine;
#endif
if (keylen > blockSize) {
EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine);
if (EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine) == 0)
return 0;
key = ctx->buf;
keylen = EVP_MD_size(ctx->md);
}
@@ -126,8 +135,10 @@ HMAC_Init_ex(HMAC_CTX *ctx,
for (i = 0, p = ctx->opad; i < keylen; i++)
p[i] ^= ((const unsigned char *)key)[i];
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
if (EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine) == 0)
return 0;
EVP_DigestUpdate(ctx->ctx, ctx->ipad, EVP_MD_block_size(ctx->md));
return 1;
}
void

View File

@@ -75,7 +75,7 @@ void HMAC_CTX_free(HMAC_CTX *ctx);
size_t HMAC_size(const HMAC_CTX *ctx);
void HMAC_Init_ex(HMAC_CTX *, const void *, size_t,
int HMAC_Init_ex(HMAC_CTX *, const void *, size_t,
const EVP_MD *, ENGINE *);
void HMAC_Update(HMAC_CTX *ctx, const void *data, size_t len);
void HMAC_Final(HMAC_CTX *ctx, void *md, unsigned int *len);