EVP_F_EVP_KDF_CTX_NEW_ID:226:EVP_KDF_CTX_new_id
EVP_F_EVP_MAC_CTRL:209:EVP_MAC_ctrl
EVP_F_EVP_MAC_CTRL_STR:210:EVP_MAC_ctrl_str
-EVP_F_EVP_MAC_CTX_COPY:211:EVP_MAC_CTX_copy
+EVP_F_EVP_MAC_CTX_DUP:211:EVP_MAC_CTX_dup
EVP_F_EVP_MAC_CTX_NEW:213:EVP_MAC_CTX_new
EVP_F_EVP_MAC_INIT:212:EVP_MAC_init
EVP_F_EVP_MD_BLOCK_SIZE:232:EVP_MD_block_size
EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN:164:PKCS5_v2_PBKDF2_keyivgen
EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN:180:PKCS5_v2_scrypt_keyivgen
EVP_F_PKEY_KDF_CTRL:227:pkey_kdf_ctrl
+EVP_F_PKEY_MAC_COPY:241:pkey_mac_copy
EVP_F_PKEY_MAC_INIT:214:pkey_mac_init
EVP_F_PKEY_SET_TYPE:158:pkey_set_type
EVP_F_POLY1305_CTRL:216:poly1305_ctrl
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_KDF_CTX_NEW_ID, 0), "EVP_KDF_CTX_new_id"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MAC_CTRL, 0), "EVP_MAC_ctrl"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MAC_CTRL_STR, 0), "EVP_MAC_ctrl_str"},
- {ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MAC_CTX_COPY, 0), "EVP_MAC_CTX_copy"},
+ {ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MAC_CTX_DUP, 0), "EVP_MAC_CTX_dup"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MAC_CTX_NEW, 0), "EVP_MAC_CTX_new"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MAC_INIT, 0), "EVP_MAC_init"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MD_BLOCK_SIZE, 0), "EVP_MD_block_size"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, 0),
"PKCS5_v2_scrypt_keyivgen"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKEY_KDF_CTRL, 0), "pkey_kdf_ctrl"},
+ {ERR_PACK(ERR_LIB_EVP, EVP_F_PKEY_MAC_COPY, 0), "pkey_mac_copy"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKEY_MAC_INIT, 0), "pkey_mac_init"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKEY_SET_TYPE, 0), "pkey_set_type"},
{ERR_PACK(ERR_LIB_EVP, EVP_F_POLY1305_CTRL, 0), "poly1305_ctrl"},
OPENSSL_free(ctx);
}
-int EVP_MAC_CTX_copy(EVP_MAC_CTX *dst, const EVP_MAC_CTX *src)
+EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
{
- EVP_MAC_IMPL *macdata;
+ EVP_MAC_CTX *dst = EVP_MAC_CTX_new(src->meth);
- if (src->data != NULL && !dst->meth->copy(dst->data, src->data))
- return 0;
+ if (dst == NULL)
+ return NULL;
+
+ dst = OPENSSL_malloc(sizeof(*dst));
+ if (dst == NULL) {
+ EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
- macdata = dst->data;
*dst = *src;
- dst->data = macdata;
- return 1;
+ dst->data = src->meth->dup(src->data);
+ if (dst->data == NULL) {
+ EVP_MAC_CTX_free(dst);
+ return NULL;
+ }
+
+ return dst;
}
const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
#include <openssl/err.h>
#include <openssl/evp.h>
#include "internal/evp_int.h"
+#include "evp_locl.h"
/* MAC PKEY context structure */
{
MAC_PKEY_CTX *sctx, *dctx;
- if (!pkey_mac_init(dst))
+ sctx = EVP_PKEY_CTX_get_data(src);
+ if (sctx->ctx->data == NULL)
return 0;
- sctx = EVP_PKEY_CTX_get_data(src);
- dctx = EVP_PKEY_CTX_get_data(dst);
+ dctx = OPENSSL_zalloc(sizeof(*dctx));
+ if (dctx == NULL) {
+ EVPerr(EVP_F_PKEY_MAC_COPY, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
- if (!EVP_MAC_CTX_copy(dctx->ctx, sctx->ctx))
+ EVP_PKEY_CTX_set_data(dst, dctx);
+ dst->keygen_info_count = 0;
+
+ dctx->ctx = EVP_MAC_CTX_dup(sctx->ctx);
+ if (dctx->ctx == NULL)
goto err;
+ dctx->type = sctx->type;
+
switch (dctx->type) {
case MAC_TYPE_RAW:
dctx->raw_data.md = sctx->raw_data.md;
}
return 1;
err:
- pkey_mac_cleanup (dst);
+ pkey_mac_cleanup(dst);
return 0;
}
break;
case MAC_TYPE_MAC:
{
- EVP_MAC_CTX *cmkey = EVP_MAC_CTX_new_id(nid);
+ EVP_MAC_CTX *cmkey = EVP_MAC_CTX_dup(hctx->ctx);
if (cmkey == NULL)
return 0;
- if (!EVP_MAC_CTX_copy(cmkey, hctx->ctx)) {
- EVP_MAC_CTX_free(cmkey);
- return 0;
- }
EVP_PKEY_assign(pkey, nid, cmkey);
}
break;
case MAC_TYPE_RAW:
hctx->raw_data.md = p2;
break;
- case MAC_TYPE_MAC:
- if (ctx->pkey != NULL
- && !EVP_MAC_CTX_copy(hctx->ctx,
- (EVP_MAC_CTX *)ctx->pkey->pkey.ptr))
- return 0;
- if (!EVP_MAC_init(hctx->ctx))
- return 0;
+ case MAC_TYPE_MAC: {
+ EVP_MAC_CTX *new_mac_ctx;
+
+ if (ctx->pkey == NULL)
+ return 0;
+ new_mac_ctx = EVP_MAC_CTX_dup((EVP_MAC_CTX *)ctx->pkey
+ ->pkey.ptr);
+ if (new_mac_ctx == NULL)
+ return 0;
+ EVP_MAC_CTX_free(hctx->ctx);
+ hctx->ctx = new_mac_ctx;
+ }
break;
default:
/* This should be dead code */
|| derived_key_len == 0)
return 0;
- ctx = EVP_MAC_CTX_new(kdf_mac);
ctx_init = EVP_MAC_CTX_new(kdf_mac);
- if (ctx == NULL || ctx_init == NULL)
+ if (ctx_init == NULL)
goto end;
if (hmac_md != NULL &&
EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_MD, hmac_md) <= 0)
c[2] = (unsigned char)((counter >> 8) & 0xff);
c[3] = (unsigned char)(counter & 0xff);
- if (!(EVP_MAC_CTX_copy(ctx, ctx_init)
+ ctx = EVP_MAC_CTX_dup(ctx_init);
+ if (!(ctx != NULL
&& EVP_MAC_update(ctx, c, sizeof(c))
&& EVP_MAC_update(ctx, z, z_len)
&& EVP_MAC_update(ctx, info, info_len)))
memcpy(out, mac, len);
break;
}
+ EVP_MAC_CTX_free(ctx);
+ ctx = NULL;
}
ret = 1;
end:
size_t Ai_len;
int ret = 0;
- ctx = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
- ctx_Ai = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
ctx_init = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
- if (ctx == NULL || ctx_Ai == NULL || ctx_init == NULL)
+ if (ctx_init == NULL)
goto err;
if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_FLAGS, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW) != 1)
goto err;
if (chunk == 0)
goto err;
/* A(0) = seed */
- if (!EVP_MAC_CTX_copy(ctx_Ai, ctx_init))
+ ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
+ if (ctx_Ai == NULL)
goto err;
if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
goto err;
/* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len))
goto err;
+ EVP_MAC_CTX_free(ctx_Ai);
+ ctx_Ai = NULL;
/* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
- if (!EVP_MAC_CTX_copy(ctx, ctx_init))
+ ctx = EVP_MAC_CTX_dup(ctx_init);
+ if (ctx == NULL)
goto err;
if (!EVP_MAC_update(ctx, Ai, Ai_len))
goto err;
/* save state for calculating next A(i) value */
- if (olen > chunk && !EVP_MAC_CTX_copy(ctx_Ai, ctx))
- goto err;
+ if (olen > chunk) {
+ ctx_Ai = EVP_MAC_CTX_dup(ctx);
+ if (ctx_Ai == NULL)
+ goto err;
+ }
if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
goto err;
if (olen <= chunk) {
}
if (!EVP_MAC_final(ctx, out, NULL))
goto err;
+ EVP_MAC_CTX_free(ctx);
+ ctx = NULL;
out += chunk;
olen -= chunk;
}
SIV_BLOCK tag;
EVP_CIPHER_CTX *cipher_ctx;
EVP_MAC_CTX *mac_ctx_init;
- EVP_MAC_CTX *mac_ctx;
int final_ret;
int crypto_ok;
};
{
SIV_BLOCK t;
size_t out_len = sizeof(out->byte);
+ EVP_MAC_CTX *mac_ctx;
+ int ret = 0;
- if (!EVP_MAC_CTX_copy(ctx->mac_ctx, ctx->mac_ctx_init))
+ mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init);
+ if (mac_ctx == NULL)
return 0;
if (len >= SIV_LEN) {
- if (!EVP_MAC_update(ctx->mac_ctx, in, len - SIV_LEN))
- return 0;
+ if (!EVP_MAC_update(mac_ctx, in, len - SIV_LEN))
+ goto err;
memcpy(&t, in + (len-SIV_LEN), SIV_LEN);
siv128_xorblock(&t, &ctx->d);
- if (!EVP_MAC_update(ctx->mac_ctx, t.byte, SIV_LEN))
- return 0;
+ if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
+ goto err;
} else {
memset(&t, 0, sizeof(t));
memcpy(&t, in, len);
t.byte[len] = 0x80;
siv128_dbl(&ctx->d);
siv128_xorblock(&t, &ctx->d);
- if (!EVP_MAC_update(ctx->mac_ctx, t.byte, SIV_LEN))
- return 0;
+ if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
+ goto err;
}
- if (!EVP_MAC_final(ctx->mac_ctx, out->byte, &out_len)
+ if (!EVP_MAC_final(mac_ctx, out->byte, &out_len)
|| out_len != SIV_LEN)
- return 0;
- return 1;
+ goto err;
+
+ ret = 1;
+
+err:
+ EVP_MAC_CTX_free(mac_ctx);
+ return ret;
}
{
static const unsigned char zero[SIV_LEN] = { 0 };
size_t out_len = SIV_LEN;
+ EVP_MAC_CTX *mac_ctx = NULL;
memset(&ctx->d, 0, sizeof(ctx->d));
ctx->cipher_ctx = NULL;
- ctx->mac_ctx = NULL;
ctx->mac_ctx_init = NULL;
if (key == NULL || cbc == NULL || ctr == NULL
|| (ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
|| (ctx->mac_ctx_init = EVP_MAC_CTX_new_id(EVP_MAC_CMAC)) == NULL
- || (ctx->mac_ctx = EVP_MAC_CTX_new_id(EVP_MAC_CMAC)) == NULL
|| EVP_MAC_ctrl(ctx->mac_ctx_init, EVP_MAC_CTRL_SET_CIPHER, cbc) <= 0
|| EVP_MAC_ctrl(ctx->mac_ctx_init, EVP_MAC_CTRL_SET_KEY, key, klen) <= 0
|| !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL)
- || !EVP_MAC_CTX_copy(ctx->mac_ctx, ctx->mac_ctx_init)
- || !EVP_MAC_update(ctx->mac_ctx, zero, sizeof(zero))
- || !EVP_MAC_final(ctx->mac_ctx, ctx->d.byte, &out_len)) {
+ || (mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
+ || !EVP_MAC_update(mac_ctx, zero, sizeof(zero))
+ || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len)) {
EVP_CIPHER_CTX_free(ctx->cipher_ctx);
EVP_MAC_CTX_free(ctx->mac_ctx_init);
- EVP_MAC_CTX_free(ctx->mac_ctx);
+ EVP_MAC_CTX_free(mac_ctx);
return 0;
}
+ EVP_MAC_CTX_free(mac_ctx);
ctx->final_ret = -1;
ctx->crypto_ok = 1;
memcpy(&dest->d, &src->d, sizeof(src->d));
if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx))
return 0;
- if (!EVP_MAC_CTX_copy(dest->mac_ctx_init, src->mac_ctx_init))
+ EVP_MAC_CTX_free(dest->mac_ctx_init);
+ dest->mac_ctx_init = EVP_MAC_CTX_dup(src->mac_ctx_init);
+ if (dest->mac_ctx_init == NULL)
return 0;
- /* no need to copy mac_ctx since it's temp storage */
return 1;
}
{
SIV_BLOCK mac_out;
size_t out_len = SIV_LEN;
+ EVP_MAC_CTX *mac_ctx;
siv128_dbl(&ctx->d);
- if (!EVP_MAC_CTX_copy(ctx->mac_ctx, ctx->mac_ctx_init)
- || !EVP_MAC_update(ctx->mac_ctx, aad, len)
- || !EVP_MAC_final(ctx->mac_ctx, mac_out.byte, &out_len)
- || out_len != SIV_LEN)
+ mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init);
+ if (mac_ctx == NULL
+ || !EVP_MAC_update(mac_ctx, aad, len)
+ || !EVP_MAC_final(mac_ctx, mac_out.byte, &out_len)
+ || out_len != SIV_LEN) {
+ EVP_MAC_CTX_free(mac_ctx);
return 0;
+ }
+ EVP_MAC_CTX_free(mac_ctx);
siv128_xorblock(&ctx->d, &mac_out);
return 1;
-
}
/*
ctx->cipher_ctx = NULL;
EVP_MAC_CTX_free(ctx->mac_ctx_init);
ctx->mac_ctx_init = NULL;
- EVP_MAC_CTX_free(ctx->mac_ctx);
- ctx->mac_ctx = NULL;
OPENSSL_cleanse(&ctx->d, sizeof(ctx->d));
OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag));
ctx->final_ret = -1;
=head1 NAME
EVP_MAC, EVP_MAC_CTX, EVP_MAC_CTX_new, EVP_MAC_CTX_new_id, EVP_MAC_CTX_free,
-EVP_MAC_CTX_copy, EVP_MAC_CTX_mac, EVP_MAC_size, EVP_MAC_init, EVP_MAC_update,
+EVP_MAC_CTX_dup, EVP_MAC_CTX_mac, EVP_MAC_size, EVP_MAC_init, EVP_MAC_update,
EVP_MAC_final, EVP_MAC_ctrl, EVP_MAC_vctrl, EVP_MAC_ctrl_str,
EVP_MAC_str2ctrl, EVP_MAC_hex2ctrl, EVP_MAC_nid, EVP_MAC_name,
EVP_get_macbyname, EVP_get_macbynid, EVP_get_macbyobj - EVP MAC routines
EVP_MAC_CTX *EVP_MAC_CTX_new(const EVP_MAC *mac);
EVP_MAC_CTX *EVP_MAC_CTX_new_id(int nid);
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
- int EVP_MAC_CTX_copy(EVP_MAC_CTX *dest, EVP_MAC_CTX *src);
+ EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
int EVP_MAC_init(EVP_MAC_CTX *ctx);
underlying context if there is one, as well as the context itself.
B<NULL> is a valid parameter, for which this function is a no-op.
-EVP_MAC_CTX_copy() makes a deep copy of the C<src> context to the
-C<dest> context.
-The C<dest> context I<must> have been created before calling this
-function.
+EVP_MAC_CTX_dup() duplicates the C<src> context and returns a newly allocated
+context.
EVP_MAC_CTX_mac() returns the B<EVP_MAC> associated with the context
C<ctx>.
=head1 RETURN VALUES
-EVP_MAC_CTX_new() and EVP_MAC_CTX_new_id() return a pointer to a newly
-created EVP_MAC_CTX, or NULL if allocation failed.
+EVP_MAC_CTX_new(), EVP_MAC_CTX_new_id() and EVP_MAC_CTX_dup() return a pointer
+to a newly created EVP_MAC_CTX, or NULL if allocation failed.
EVP_MAC_CTX_free() returns nothing at all.
-EVP_MAC_CTX_copy(), EVP_MAC_init(), EVP_MAC_update(),
-and EVP_MAC_final() return 1 on success, 0 on error.
+EVP_MAC_init(), EVP_MAC_update(), and EVP_MAC_final() return 1 on success, 0 on error.
EVP_MAC_ctrl(), EVP_MAC_ctrl_str(), EVP_MAC_str2ctrl() and
EVP_MAC_hex2ctrl() return 1 on success and 0 or a negative value on
L<EVP_MAC_SIPHASH(7)>,
L<EVP_MAC_POLY1305(7)>
+=head1 HISTORY
+
+These functions were added in OpenSSL 3.0.0.
+
=head1 COPYRIGHT
Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
EVP_MAC_CTX *EVP_MAC_CTX_new(const EVP_MAC *mac);
EVP_MAC_CTX *EVP_MAC_CTX_new_id(int nid);
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx);
-int EVP_MAC_CTX_copy(EVP_MAC_CTX *dest, const EVP_MAC_CTX *src);
+EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src);
const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx);
size_t EVP_MAC_size(EVP_MAC_CTX *ctx);
int EVP_MAC_init(EVP_MAC_CTX *ctx);
# define EVP_F_EVP_KDF_CTX_NEW_ID 226
# define EVP_F_EVP_MAC_CTRL 209
# define EVP_F_EVP_MAC_CTRL_STR 210
-# define EVP_F_EVP_MAC_CTX_COPY 211
+# define EVP_F_EVP_MAC_CTX_DUP 211
# define EVP_F_EVP_MAC_CTX_NEW 213
# define EVP_F_EVP_MAC_INIT 212
# define EVP_F_EVP_MD_BLOCK_SIZE 232
# define EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN 164
# define EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN 180
# define EVP_F_PKEY_KDF_CTRL 227
+# define EVP_F_PKEY_MAC_COPY 241
# define EVP_F_PKEY_MAC_INIT 214
# define EVP_F_PKEY_SET_TYPE 158
# define EVP_F_POLY1305_CTRL 216
EVP_MAC_CTX_new 4534 3_0_0 EXIST::FUNCTION:
EVP_MAC_CTX_new_id 4535 3_0_0 EXIST::FUNCTION:
EVP_MAC_CTX_free 4536 3_0_0 EXIST::FUNCTION:
-EVP_MAC_CTX_copy 4537 3_0_0 EXIST::FUNCTION:
+EVP_MAC_CTX_dup 4537 3_0_0 EXIST::FUNCTION:
EVP_MAC_CTX_mac 4538 3_0_0 EXIST::FUNCTION:
EVP_MAC_size 4539 3_0_0 EXIST::FUNCTION:
EVP_MAC_init 4540 3_0_0 EXIST::FUNCTION: