From c29f4739ceb69343c92c3f24d2a16b609747e5a9 Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 17 Feb 2018 14:53:58 +0000 Subject: [PATCH] Provide HMAC_CTX_new(), HMAC_CTX_free(), HMAC_CTX_reset() and HMAC_CTX_get_md(). --- lib/libcrypto/Symbols.list | 4 +++ lib/libcrypto/hmac/hmac.c | 56 ++++++++++++++++++++++++++++++++------ lib/libcrypto/hmac/hmac.h | 7 +++-- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/lib/libcrypto/Symbols.list b/lib/libcrypto/Symbols.list index bee42ac1641..1da0493c73e 100644 --- a/lib/libcrypto/Symbols.list +++ b/lib/libcrypto/Symbols.list @@ -1604,7 +1604,11 @@ HKDF_extract HMAC HMAC_CTX_cleanup HMAC_CTX_copy +HMAC_CTX_free +HMAC_CTX_get_md HMAC_CTX_init +HMAC_CTX_new +HMAC_CTX_reset HMAC_CTX_set_flags HMAC_Final HMAC_Init diff --git a/lib/libcrypto/hmac/hmac.c b/lib/libcrypto/hmac/hmac.c index 84917662ca8..7bf17eed965 100644 --- a/lib/libcrypto/hmac/hmac.c +++ b/lib/libcrypto/hmac/hmac.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hmac.c,v 1.24 2017/03/03 10:39:07 inoguchi Exp $ */ +/* $OpenBSD: hmac.c,v 1.25 2018/02/17 14:53:58 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -171,6 +171,38 @@ err: return 0; } +HMAC_CTX * +HMAC_CTX_new(void) +{ + HMAC_CTX *ctx; + + if ((ctx = calloc(1, sizeof(*ctx))) == NULL) + return NULL; + + HMAC_CTX_init(ctx); + + return ctx; +} + +void +HMAC_CTX_free(HMAC_CTX *ctx) +{ + if (ctx == NULL) + return; + + HMAC_CTX_cleanup(ctx); + + free(ctx); +} + +int +HMAC_CTX_reset(HMAC_CTX *ctx) +{ + HMAC_CTX_cleanup(ctx); + HMAC_CTX_init(ctx); + return 1; +} + void HMAC_CTX_init(HMAC_CTX *ctx) { @@ -206,6 +238,20 @@ HMAC_CTX_cleanup(HMAC_CTX *ctx) explicit_bzero(ctx, sizeof(*ctx)); } +void +HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) +{ + EVP_MD_CTX_set_flags(&ctx->i_ctx, flags); + EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); + EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); +} + +const EVP_MD * +HMAC_CTX_get_md(const HMAC_CTX *ctx) +{ + return ctx->md; +} + 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) @@ -228,11 +274,3 @@ err: HMAC_CTX_cleanup(&c); return NULL; } - -void -HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) -{ - EVP_MD_CTX_set_flags(&ctx->i_ctx, flags); - EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); - EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); -} diff --git a/lib/libcrypto/hmac/hmac.h b/lib/libcrypto/hmac/hmac.h index f3418b3cb75..e787c62ac89 100644 --- a/lib/libcrypto/hmac/hmac.h +++ b/lib/libcrypto/hmac/hmac.h @@ -1,4 +1,4 @@ -/* $OpenBSD: hmac.h,v 1.12 2014/06/21 13:39:46 jsing Exp $ */ +/* $OpenBSD: hmac.h,v 1.13 2018/02/17 14:53:59 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -83,8 +83,10 @@ typedef struct hmac_ctx_st { #define HMAC_size(e) (EVP_MD_size((e)->md)) - +HMAC_CTX *HMAC_CTX_new(void); +void HMAC_CTX_free(HMAC_CTX *ctx); void HMAC_CTX_init(HMAC_CTX *ctx); +int HMAC_CTX_reset(HMAC_CTX *ctx); void HMAC_CTX_cleanup(HMAC_CTX *ctx); #define HMAC_cleanup(ctx) HMAC_CTX_cleanup(ctx) /* deprecated */ @@ -100,6 +102,7 @@ unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx); void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags); +const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx); #ifdef __cplusplus } -- 2.20.1