Implement dupctx for chacha20 cipher

Implement the dupctx method for the chacha20 cipher, so that
EVP_PKEY_CTX_copy works

Its pretty straightforward, its basically just a memdup.  Checking the
pointers that might need fixing up:

in PROV_CHACHA20_CTX all members are statically declared, so memduping
should be fine

in PROV_CHACHA20_CTX->base (PROV_CIPHER_CTX):
        Non statically declared members:
                *tlsmac needs to get memduped to avoid double free
                 conditions, but only if base.alloced is set
                *hw pointer is always assigned to the chacha20_hw global
                 variable, so can be left alone
                *libctx can be left alone as provctx is always NULL in
                 chacha20_newctx
                *ks appears unused by chacha20, so can be ignored
Fixes #20978

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21878)
This commit is contained in:
Neil Horman 2023-08-28 12:07:16 -04:00 committed by Pauli
parent 12d08fe3a5
commit 61cfc22b60

View File

@ -21,6 +21,7 @@
static OSSL_FUNC_cipher_newctx_fn chacha20_newctx;
static OSSL_FUNC_cipher_freectx_fn chacha20_freectx;
static OSSL_FUNC_cipher_dupctx_fn chacha20_dupctx;
static OSSL_FUNC_cipher_get_params_fn chacha20_get_params;
static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params;
static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params;
@ -64,6 +65,25 @@ static void chacha20_freectx(void *vctx)
}
}
static void *chacha20_dupctx(void *vctx)
{
PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
PROV_CHACHA20_CTX *dupctx = NULL;
if (ctx != NULL) {
dupctx = OPENSSL_memdup(ctx, sizeof(*dupctx));
if (dupctx != NULL && dupctx->base.tlsmac != NULL && dupctx->base.alloced) {
dupctx->base.tlsmac = OPENSSL_memdup(dupctx->base.tlsmac,
dupctx->base.tlsmacsize);
if (dupctx->base.tlsmac == NULL) {
OPENSSL_free(dupctx);
dupctx = NULL;
}
}
}
return dupctx;
}
static int chacha20_get_params(OSSL_PARAM params[])
{
return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
@ -187,6 +207,7 @@ int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
const OSSL_DISPATCH ossl_chacha20_functions[] = {
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_dupctx },
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit },
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit },
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },