Implement dupctx for chacha20 cipher
authorNeil Horman <nhorman@openssl.org>
Mon, 28 Aug 2023 16:07:16 +0000 (12:07 -0400)
committerPauli <pauli@openssl.org>
Mon, 4 Sep 2023 00:15:58 +0000 (10:15 +1000)
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)

providers/implementations/ciphers/cipher_chacha20.c

index 41aa056c2dc035bd8cebe532c64044d2c551acea..49e36ba1a9777896859f9fb843a9101a78ff268a 100644 (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 },