From: Kurt Roeckx Date: Tue, 18 Dec 2018 23:36:40 +0000 (+0100) Subject: Change EVP_MAC method from copy to dup X-Git-Tag: openssl-3.0.0-alpha1~1986 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=7ed66e2634e6cfbb16a1ef975572e79a479217a8;p=oweals%2Fopenssl.git Change EVP_MAC method from copy to dup Reviewed-by: Tomas Mraz GH: #7651 --- diff --git a/crypto/blake2/blake2b_mac.c b/crypto/blake2/blake2b_mac.c index b38e9b8d27..f6025b1f70 100644 --- a/crypto/blake2/blake2b_mac.c +++ b/crypto/blake2/blake2b_mac.c @@ -39,10 +39,16 @@ static void blake2b_mac_free(EVP_MAC_IMPL *macctx) } } -static int blake2b_mac_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src) +static EVP_MAC_IMPL *blake2b_mac_dup(const EVP_MAC_IMPL *src) { + EVP_MAC_IMPL *dst; + + dst = OPENSSL_zalloc(sizeof(*dst)); + if (dst == NULL) + return NULL; + *dst = *src; - return 1; + return dst; } static int blake2b_mac_init(EVP_MAC_IMPL *macctx) @@ -177,7 +183,7 @@ static size_t blake2b_mac_size(EVP_MAC_IMPL *macctx) const EVP_MAC blake2b_mac_meth = { EVP_MAC_BLAKE2B, blake2b_mac_new, - blake2b_mac_copy, + blake2b_mac_dup, blake2b_mac_free, blake2b_mac_size, blake2b_mac_init, diff --git a/crypto/blake2/blake2s_mac.c b/crypto/blake2/blake2s_mac.c index 04dbf4e027..9ce8db1360 100644 --- a/crypto/blake2/blake2s_mac.c +++ b/crypto/blake2/blake2s_mac.c @@ -39,10 +39,16 @@ static void blake2s_mac_free(EVP_MAC_IMPL *macctx) } } -static int blake2s_mac_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src) +static EVP_MAC_IMPL *blake2s_mac_dup(const EVP_MAC_IMPL *src) { + EVP_MAC_IMPL *dst; + + dst = OPENSSL_malloc(sizeof(*dst)); + if (dst == NULL) + return NULL; + *dst = *src; - return 1; + return dst; } static int blake2s_mac_init(EVP_MAC_IMPL *macctx) @@ -177,7 +183,7 @@ static size_t blake2s_mac_size(EVP_MAC_IMPL *macctx) const EVP_MAC blake2s_mac_meth = { EVP_MAC_BLAKE2S, blake2s_mac_new, - blake2s_mac_copy, + blake2s_mac_dup, blake2s_mac_free, blake2s_mac_size, blake2s_mac_init, diff --git a/crypto/cmac/cm_meth.c b/crypto/cmac/cm_meth.c index 3f20e6ce06..07acf050b3 100644 --- a/crypto/cmac/cm_meth.c +++ b/crypto/cmac/cm_meth.c @@ -46,14 +46,22 @@ static void cmac_free(EVP_MAC_IMPL *cctx) } } -static int cmac_copy(EVP_MAC_IMPL *cdst, EVP_MAC_IMPL *csrc) +static EVP_MAC_IMPL *cmac_dup(const EVP_MAC_IMPL *csrc) { - if (!CMAC_CTX_copy(cdst->ctx, csrc->ctx)) - return 0; + EVP_MAC_IMPL *cdst = cmac_new(); + + if (cdst == NULL) + return NULL; + + if (!CMAC_CTX_copy(cdst->ctx, csrc->ctx)) { + cmac_free(cdst); + return NULL; + } cdst->tmpengine = csrc->tmpengine; cdst->tmpcipher = csrc->tmpcipher; - return 1; + + return cdst; } static size_t cmac_size(EVP_MAC_IMPL *cctx) @@ -153,7 +161,7 @@ static int cmac_ctrl_str(EVP_MAC_IMPL *cctx, const char *type, const EVP_MAC cmac_meth = { EVP_MAC_CMAC, cmac_new, - cmac_copy, + cmac_dup, cmac_free, cmac_size, cmac_init, diff --git a/crypto/evp/mac_lib.c b/crypto/evp/mac_lib.c index d11fae099c..ee4a68f459 100644 --- a/crypto/evp/mac_lib.c +++ b/crypto/evp/mac_lib.c @@ -50,9 +50,9 @@ void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx) EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src) { - EVP_MAC_CTX *dst = EVP_MAC_CTX_new(src->meth); + EVP_MAC_CTX *dst; - if (dst == NULL) + if (src->data == NULL) return NULL; dst = OPENSSL_malloc(sizeof(*dst)); diff --git a/crypto/gmac/gmac.c b/crypto/gmac/gmac.c index 5e3891fbaa..0e2eda38e9 100644 --- a/crypto/gmac/gmac.c +++ b/crypto/gmac/gmac.c @@ -39,11 +39,23 @@ static EVP_MAC_IMPL *gmac_new(void) return gctx; } -static int gmac_copy(EVP_MAC_IMPL *gdst, EVP_MAC_IMPL *gsrc) +static EVP_MAC_IMPL *gmac_dup(const EVP_MAC_IMPL *gsrc) { + EVP_MAC_IMPL *gdst; + + gdst = gmac_new(); + if (gdst == NULL) + return NULL; + + if (!EVP_CIPHER_CTX_copy(gdst->ctx, gsrc->ctx)) { + gmac_free(gdst); + return NULL; + } + gdst->cipher = gsrc->cipher; gdst->engine = gsrc->engine; - return EVP_CIPHER_CTX_copy(gdst->ctx, gsrc->ctx); + + return gdst; } static size_t gmac_size(EVP_MAC_IMPL *gctx) @@ -172,7 +184,7 @@ static int gmac_ctrl_str(EVP_MAC_IMPL *gctx, const char *type, const EVP_MAC gmac_meth = { EVP_MAC_GMAC, gmac_new, - gmac_copy, + gmac_dup, gmac_free, gmac_size, gmac_init, diff --git a/crypto/hmac/hm_meth.c b/crypto/hmac/hm_meth.c index 705bf7fd2a..db9af95cb7 100644 --- a/crypto/hmac/hm_meth.c +++ b/crypto/hmac/hm_meth.c @@ -45,14 +45,23 @@ static void hmac_free(EVP_MAC_IMPL *hctx) } } -static int hmac_copy(EVP_MAC_IMPL *hdst, EVP_MAC_IMPL *hsrc) +static EVP_MAC_IMPL *hmac_dup(const EVP_MAC_IMPL *hsrc) { - if (!HMAC_CTX_copy(hdst->ctx, hsrc->ctx)) - return 0; + EVP_MAC_IMPL *hdst; + + hdst = hmac_new(); + if (hdst == NULL) + return NULL; + + if (!HMAC_CTX_copy(hdst->ctx, hsrc->ctx)) { + hmac_free(hdst); + return NULL; + } hdst->tmpengine = hsrc->tmpengine; hdst->tmpmd = hsrc->tmpmd; - return 1; + + return hdst; } static size_t hmac_size(EVP_MAC_IMPL *hctx) @@ -162,7 +171,7 @@ static int hmac_ctrl_str(EVP_MAC_IMPL *hctx, const char *type, const EVP_MAC hmac_meth = { EVP_MAC_HMAC, hmac_new, - hmac_copy, + hmac_dup, hmac_free, hmac_size, hmac_init, diff --git a/crypto/include/internal/evp_int.h b/crypto/include/internal/evp_int.h index 77684b2f86..183fc42932 100644 --- a/crypto/include/internal/evp_int.h +++ b/crypto/include/internal/evp_int.h @@ -118,7 +118,7 @@ typedef struct evp_mac_impl_st EVP_MAC_IMPL; struct evp_mac_st { int type; EVP_MAC_IMPL *(*new) (void); - int (*copy) (EVP_MAC_IMPL *macdst, EVP_MAC_IMPL *macsrc); + EVP_MAC_IMPL *(*dup) (const EVP_MAC_IMPL *macsrc); void (*free) (EVP_MAC_IMPL *macctx); size_t (*size) (EVP_MAC_IMPL *macctx); int (*init) (EVP_MAC_IMPL *macctx); diff --git a/crypto/kmac/kmac.c b/crypto/kmac/kmac.c index 76e75c1e3f..69c334c379 100644 --- a/crypto/kmac/kmac.c +++ b/crypto/kmac/kmac.c @@ -147,8 +147,19 @@ static EVP_MAC_IMPL *kmac256_new(void) return kmac_new(evp_keccak_kmac256()); } -static int kmac_copy(EVP_MAC_IMPL *gdst, EVP_MAC_IMPL *gsrc) +static EVP_MAC_IMPL *kmac_dup(const EVP_MAC_IMPL *gsrc) { + EVP_MAC_IMPL *gdst; + + gdst = kmac_new(gsrc->md); + if (gdst == NULL) + return NULL; + + if (!EVP_MD_CTX_copy(gdst->ctx, gsrc->ctx)) { + kmac_free(gdst); + return NULL; + } + gdst->md = gsrc->md; gdst->out_len = gsrc->out_len; gdst->key_len = gsrc->key_len; @@ -157,7 +168,7 @@ static int kmac_copy(EVP_MAC_IMPL *gdst, EVP_MAC_IMPL *gsrc) memcpy(gdst->key, gsrc->key, gsrc->key_len); memcpy(gdst->custom, gsrc->custom, gdst->custom_len); - return EVP_MD_CTX_copy(gdst->ctx, gsrc->ctx); + return gdst; } /* @@ -444,7 +455,7 @@ static int kmac_bytepad_encode_key(unsigned char *out, int *out_len, const EVP_MAC kmac128_meth = { EVP_MAC_KMAC128, kmac128_new, - kmac_copy, + kmac_dup, kmac_free, kmac_size, kmac_init, @@ -457,7 +468,7 @@ const EVP_MAC kmac128_meth = { const EVP_MAC kmac256_meth = { EVP_MAC_KMAC256, kmac256_new, - kmac_copy, + kmac_dup, kmac_free, kmac_size, kmac_init, diff --git a/crypto/poly1305/poly1305_meth.c b/crypto/poly1305/poly1305_meth.c index 9248d46099..f1ade58b40 100644 --- a/crypto/poly1305/poly1305_meth.c +++ b/crypto/poly1305/poly1305_meth.c @@ -37,11 +37,17 @@ static void poly1305_free(EVP_MAC_IMPL *ctx) } } -static int poly1305_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src) +static EVP_MAC_IMPL *poly1305_dup(const EVP_MAC_IMPL *src) { + EVP_MAC_IMPL *dst; + + dst = poly1305_new(); + if (dst == NULL) + return NULL; + *dst->ctx = *src->ctx; - return 1; + return dst; } static size_t poly1305_size(EVP_MAC_IMPL *ctx) @@ -130,7 +136,7 @@ static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx, const EVP_MAC poly1305_meth = { EVP_MAC_POLY1305, poly1305_new, - poly1305_copy, + poly1305_dup, poly1305_free, poly1305_size, poly1305_init, diff --git a/crypto/siphash/siphash_meth.c b/crypto/siphash/siphash_meth.c index 37cb286ebf..5fcff2d1ef 100644 --- a/crypto/siphash/siphash_meth.c +++ b/crypto/siphash/siphash_meth.c @@ -31,10 +31,17 @@ static void siphash_free(EVP_MAC_IMPL *sctx) OPENSSL_free(sctx); } -static int siphash_copy(EVP_MAC_IMPL *sdst, EVP_MAC_IMPL *ssrc) +static EVP_MAC_IMPL *siphash_dup(const EVP_MAC_IMPL *ssrc) { + EVP_MAC_IMPL *sdst; + + sdst = siphash_new(); + if (sdst == NULL) + return NULL; + *sdst = *ssrc; - return 1; + + return sdst; } static size_t siphash_size(EVP_MAC_IMPL *sctx) @@ -128,7 +135,7 @@ static int siphash_ctrl_str(EVP_MAC_IMPL *ctx, const EVP_MAC siphash_meth = { EVP_MAC_SIPHASH, siphash_new, - siphash_copy, + siphash_dup, siphash_free, siphash_size, siphash_init,