return ctx;
}
+static void hmac_ctx_cleanup(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->md = NULL;
+ ctx->key_length = 0;
+ memset(ctx->key, 0, sizeof(HMAC_MAX_MD_CBLOCK));
+}
+
void HMAC_CTX_free(HMAC_CTX *ctx)
{
if (ctx != NULL) {
- HMAC_CTX_cleanup(ctx);
+ hmac_ctx_cleanup(ctx);
+ EVP_MD_CTX_destroy(ctx->i_ctx);
+ EVP_MD_CTX_destroy(ctx->o_ctx);
+ EVP_MD_CTX_destroy(ctx->md_ctx);
OPENSSL_free(ctx);
}
}
int HMAC_CTX_init(HMAC_CTX *ctx)
{
+ hmac_ctx_cleanup(ctx);
if (ctx->i_ctx == NULL)
ctx->i_ctx = EVP_MD_CTX_create();
- else
- EVP_MD_CTX_init(ctx->i_ctx);
if (ctx->i_ctx == NULL)
goto err;
if (ctx->o_ctx == NULL)
ctx->o_ctx = EVP_MD_CTX_create();
- else
- EVP_MD_CTX_init(ctx->o_ctx);
if (ctx->o_ctx == NULL)
goto err;
if (ctx->md_ctx == NULL)
ctx->md_ctx = EVP_MD_CTX_create();
- else
- EVP_MD_CTX_init(ctx->md_ctx);
if (ctx->md_ctx == NULL)
goto err;
ctx->md = NULL;
return 1;
err:
- HMAC_CTX_cleanup(ctx);
+ hmac_ctx_cleanup(ctx);
return 0;
}
dctx->md = sctx->md;
return 1;
err:
- HMAC_CTX_cleanup(dctx);
+ hmac_ctx_cleanup(dctx);
return 0;
}
-void HMAC_CTX_cleanup(HMAC_CTX *ctx)
-{
- EVP_MD_CTX_destroy(ctx->i_ctx);
- EVP_MD_CTX_destroy(ctx->o_ctx);
- EVP_MD_CTX_destroy(ctx->md_ctx);
- memset(ctx, 0, sizeof(*ctx));
-}
-
unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
const unsigned char *d, size_t n, unsigned char *md,
unsigned int *md_len)