Skip to content

Commit

Permalink
Implement dupctx for chacha20 cipher
Browse files Browse the repository at this point in the history
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 #21878)

(cherry picked from commit 61cfc22)
  • Loading branch information
nhorman authored and paulidale committed Sep 4, 2023
1 parent 6754de4 commit 2fb934d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions providers/implementations/ciphers/cipher_chacha20.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit 2fb934d

Please sign in to comment.