From: Pauli Date: Tue, 2 Mar 2021 12:44:53 +0000 (+1000) Subject: prov: support params argument to CHACHA20 ciphers X-Git-Tag: openssl-3.0.0-alpha14~313 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f336f98dbf80af632ea142ea3d43fe1e9d727e14;p=thirdparty%2Fopenssl.git prov: support params argument to CHACHA20 ciphers Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/14383) --- diff --git a/providers/implementations/ciphers/cipher_chacha20.c b/providers/implementations/ciphers/cipher_chacha20.c index 9bce5b09146..386c865d832 100644 --- a/providers/implementations/ciphers/cipher_chacha20.c +++ b/providers/implementations/ciphers/cipher_chacha20.c @@ -106,6 +106,9 @@ static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[]) const OSSL_PARAM *p; size_t len; + if (params == NULL) + return 1; + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); if (p != NULL) { if (!OSSL_PARAM_get_size_t(p, &len)) { @@ -143,34 +146,40 @@ const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx, } int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen, - const unsigned char *iv, size_t ivlen) + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) { int ret; /* The generic function checks for ossl_prov_is_running() */ - ret= ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen); + ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); if (ret && iv != NULL) { PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw; hw->initiv(ctx); } + if (ret && !chacha20_set_ctx_params(vctx, params)) + ret = 0; return ret; } int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen, - const unsigned char *iv, size_t ivlen) + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) { int ret; /* The generic function checks for ossl_prov_is_running() */ - ret= ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen); + ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); if (ret && iv != NULL) { PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw; hw->initiv(ctx); } + if (ret && !chacha20_set_ctx_params(vctx, params)) + ret = 0; return ret; } diff --git a/providers/implementations/ciphers/cipher_chacha20_poly1305.c b/providers/implementations/ciphers/cipher_chacha20_poly1305.c index 78ede20b44d..0ba7483780f 100644 --- a/providers/implementations/ciphers/cipher_chacha20_poly1305.c +++ b/providers/implementations/ciphers/cipher_chacha20_poly1305.c @@ -149,6 +149,9 @@ static int chacha20_poly1305_set_ctx_params(void *vctx, PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw; + if (params == NULL) + return 1; + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); if (p != NULL) { if (!OSSL_PARAM_get_size_t(p, &len)) { @@ -224,12 +227,12 @@ static int chacha20_poly1305_set_ctx_params(void *vctx, static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, size_t keylen, const unsigned char *iv, - size_t ivlen) + size_t ivlen, const OSSL_PARAM params[]) { int ret; /* The generic function checks for ossl_prov_is_running() */ - ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen); + ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL); if (ret && iv != NULL) { PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = @@ -237,17 +240,19 @@ static int chacha20_poly1305_einit(void *vctx, const unsigned char *key, hw->initiv(ctx); } + if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) + ret = 0; return ret; } static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, size_t keylen, const unsigned char *iv, - size_t ivlen) + size_t ivlen, const OSSL_PARAM params[]) { int ret; /* The generic function checks for ossl_prov_is_running() */ - ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen); + ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL); if (ret && iv != NULL) { PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; PROV_CIPHER_HW_CHACHA20_POLY1305 *hw = @@ -255,6 +260,8 @@ static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key, hw->initiv(ctx); } + if (ret && !chacha20_poly1305_set_ctx_params(vctx, params)) + ret = 0; return ret; } diff --git a/providers/implementations/ciphers/cipher_chacha20_poly1305_hw.c b/providers/implementations/ciphers/cipher_chacha20_poly1305_hw.c index 4e4165868e2..1533a3869b8 100644 --- a/providers/implementations/ciphers/cipher_chacha20_poly1305_hw.c +++ b/providers/implementations/ciphers/cipher_chacha20_poly1305_hw.c @@ -68,9 +68,9 @@ static int chacha20_poly1305_initkey(PROV_CIPHER_CTX *bctx, ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH; if (bctx->enc) - return ossl_chacha20_einit(&ctx->chacha, key, keylen, NULL, 0); + return ossl_chacha20_einit(&ctx->chacha, key, keylen, NULL, 0, NULL); else - return ossl_chacha20_dinit(&ctx->chacha, key, keylen, NULL, 0); + return ossl_chacha20_dinit(&ctx->chacha, key, keylen, NULL, 0, NULL); } static int chacha20_poly1305_initiv(PROV_CIPHER_CTX *bctx) @@ -92,10 +92,10 @@ static int chacha20_poly1305_initiv(PROV_CIPHER_CTX *bctx) if (bctx->enc) ret = ossl_chacha20_einit(&ctx->chacha, NULL, 0, - tempiv, sizeof(tempiv)); + tempiv, sizeof(tempiv), NULL); else ret = ossl_chacha20_dinit(&ctx->chacha, NULL, 0, - tempiv, sizeof(tempiv)); + tempiv, sizeof(tempiv), NULL); ctx->nonce[0] = ctx->chacha.counter[1]; ctx->nonce[1] = ctx->chacha.counter[2]; ctx->nonce[2] = ctx->chacha.counter[3];