hcrypto: Make more buffers persist across HMAC operations
If we don't change MD algorithm across two calls to HMAC_Init_ex using the same context, don't bother reallocating all of our buffers.
This commit is contained in:

committed by
Jeffrey Altman

parent
b9b8e76376
commit
2d84fc65bb
@@ -93,49 +93,39 @@ HMAC_Init_ex(HMAC_CTX *ctx,
|
|||||||
ENGINE *engine)
|
ENGINE *engine)
|
||||||
{
|
{
|
||||||
unsigned char *p;
|
unsigned char *p;
|
||||||
size_t i;
|
size_t i, blockSize;
|
||||||
|
|
||||||
|
blockSize = EVP_MD_block_size(md);
|
||||||
|
|
||||||
if (ctx->md != md) {
|
if (ctx->md != md) {
|
||||||
|
if (ctx->md != NULL)
|
||||||
|
HMAC_CTX_cleanup(ctx);
|
||||||
|
|
||||||
ctx->md = md;
|
ctx->md = md;
|
||||||
if (ctx->buf) {
|
|
||||||
memset(ctx->buf, 0, ctx->key_length);
|
|
||||||
free (ctx->buf);
|
|
||||||
}
|
|
||||||
ctx->key_length = EVP_MD_size(ctx->md);
|
ctx->key_length = EVP_MD_size(ctx->md);
|
||||||
ctx->buf = malloc(ctx->key_length);
|
ctx->buf = malloc(ctx->key_length);
|
||||||
|
ctx->opad = malloc(blockSize);
|
||||||
|
ctx->ipad = malloc(blockSize);
|
||||||
|
ctx->ctx = EVP_MD_CTX_create();
|
||||||
}
|
}
|
||||||
#if 0
|
#if 0
|
||||||
ctx->engine = engine;
|
ctx->engine = engine;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (keylen > EVP_MD_block_size(ctx->md)) {
|
if (keylen > blockSize) {
|
||||||
EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine);
|
EVP_Digest(key, keylen, ctx->buf, NULL, ctx->md, engine);
|
||||||
key = ctx->buf;
|
key = ctx->buf;
|
||||||
keylen = EVP_MD_size(ctx->md);
|
keylen = EVP_MD_size(ctx->md);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->opad) {
|
memset(ctx->ipad, 0x36, blockSize);
|
||||||
memset(ctx->opad, 0, ctx->key_length);
|
memset(ctx->opad, 0x5c, blockSize);
|
||||||
free(ctx->opad);
|
|
||||||
}
|
|
||||||
if (ctx->ipad) {
|
|
||||||
memset(ctx->ipad, 0, ctx->key_length);
|
|
||||||
free(ctx->ipad);
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->opad = malloc(EVP_MD_block_size(ctx->md));
|
|
||||||
ctx->ipad = malloc(EVP_MD_block_size(ctx->md));
|
|
||||||
memset(ctx->ipad, 0x36, EVP_MD_block_size(ctx->md));
|
|
||||||
memset(ctx->opad, 0x5c, EVP_MD_block_size(ctx->md));
|
|
||||||
|
|
||||||
for (i = 0, p = ctx->ipad; i < keylen; i++)
|
for (i = 0, p = ctx->ipad; i < keylen; i++)
|
||||||
p[i] ^= ((const unsigned char *)key)[i];
|
p[i] ^= ((const unsigned char *)key)[i];
|
||||||
for (i = 0, p = ctx->opad; i < keylen; i++)
|
for (i = 0, p = ctx->opad; i < keylen; i++)
|
||||||
p[i] ^= ((const unsigned char *)key)[i];
|
p[i] ^= ((const unsigned char *)key)[i];
|
||||||
|
|
||||||
if (ctx->ctx == NULL)
|
|
||||||
ctx->ctx = EVP_MD_CTX_create();
|
|
||||||
|
|
||||||
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
|
EVP_DigestInit_ex(ctx->ctx, ctx->md, ctx->engine);
|
||||||
EVP_DigestUpdate(ctx->ctx, ctx->ipad, EVP_MD_block_size(ctx->md));
|
EVP_DigestUpdate(ctx->ctx, ctx->ipad, EVP_MD_block_size(ctx->md));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user