Commit fa817bca9c for openssl.org

commit fa817bca9c9f45f295df23771e7e276dd9bb9052
Author: Nikola Pajkovsky <nikolap@openssl.org>
Date:   Wed Nov 25 19:59:13 2026 +0100

    evp: use fast path parameter decoding

    Generate a parameter decoder for OSSL_CIPHER_PARAM_KEYLEN and
    OSSL_CIPHER_PARAM_IVLEN with OpenSSL::paramnames and use it in
    evp_cipher_init_internal() and EVP_CIPHER_CTX_set_params() in place of
    separate OSSL_PARAM_locate_const() calls.

    OSSL_CIPHER_PARAM_AEAD_IVLEN is a synonym for OSSL_CIPHER_PARAM_IVLEN,
    so the single ivlen entry covers both. Duplicates resolve to the first
    occurrence, matching the behaviour of OSSL_PARAM_locate_const().

    Assisted-by: Pi:deepseek/deepseek-v4.1-flash-1017
    Reviewed-by: Paul Dale <paul.dale@oracle.com>
    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Merge-date: Tue Dec  1 16:51:35 2026
    Merged-from: https://github.com/openssl/openssl/pull/34005

diff --git a/.gitignore b/.gitignore
index fc43052862..6eaed90fdc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -99,6 +99,7 @@ providers/common/include/prov/der_sm2.h
 providers/common/include/prov/der_ml_dsa.h
 providers/common/include/prov/der_hkdf.h
 providers/fips/fipsparams.inc
+crypto/evp/evp_enc.inc
 ssl/t1_lib.inc
 ssl/record/methods/tls_common.inc
 providers/implementations/asymciphers/rsa_enc.inc
diff --git a/build.info b/build.info
index f441f69de4..bf2cc46dec 100644
--- a/build.info
+++ b/build.info
@@ -81,6 +81,7 @@ DEPEND[]=crypto/core_names.c \
          include/crypto/dso_conf.h \
          include/crypto/ec_params.h \
          include/crypto/rsa_params.h \
+         crypto/evp/evp_enc.inc \
          ssl/t1_lib.inc \
          ssl/record/methods/tls_common.inc \
          providers/implementations/asymciphers/rsa_enc.inc \
@@ -227,6 +228,7 @@ GENERATE[include/openssl/x509_vfy.h]=include/openssl/x509_vfy.h.in
 GENERATE[include/crypto/dso_conf.h]=include/crypto/dso_conf.h.in

 DEPEND[crypto/core_names.c \
+       crypto/evp/evp_enc.inc \
        ssl/t1_lib.inc \
        ssl/record/methods/tls_common.inc \
        providers/implementations/asymciphers/rsa_enc.inc \
@@ -340,6 +342,7 @@ GENERATE[include/crypto/ec_params.h]=\
     include/crypto/ec_params.h.in
 GENERATE[include/crypto/rsa_params.h]=\
     include/crypto/rsa_params.h.in
+GENERATE[crypto/evp/evp_enc.inc]=crypto/evp/evp_enc.inc.in
 GENERATE[ssl/t1_lib.inc]=ssl/t1_lib.inc.in
 GENERATE[ssl/record/methods/tls_common.inc]=\
     ssl/record/methods/tls_common.inc.in
diff --git a/crypto/evp/evp_enc.c b/crypto/evp/evp_enc.c
index b512653e8f..b73f039c85 100644
--- a/crypto/evp/evp_enc.c
+++ b/crypto/evp/evp_enc.c
@@ -23,6 +23,8 @@
 #include "crypto/evp.h"
 #include "evp_local.h"

+#include "crypto/evp/evp_enc.inc"
+
 OSSL_SAFE_MATH_SIGNED(int, int)

 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
@@ -175,19 +177,18 @@ static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
         OSSL_PARAM param_lens[3] = { OSSL_PARAM_END, OSSL_PARAM_END,
             OSSL_PARAM_END };
         OSSL_PARAM *q = param_lens;
-        const OSSL_PARAM *p;
-
-        p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
-        if (p != NULL)
-            memcpy(q++, p, sizeof(*q));
+        struct evp_cipher_len_params_st p;

         /*
          * Note that OSSL_CIPHER_PARAM_AEAD_IVLEN is a synonym for
          * OSSL_CIPHER_PARAM_IVLEN so both are covered here.
          */
-        p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
-        if (p != NULL)
-            memcpy(q++, p, sizeof(*q));
+        if (!evp_cipher_len_params_decoder(params, &p))
+            return 0;
+        if (p.keylen != NULL)
+            *q++ = *p.keylen;
+        if (p.ivlen != NULL)
+            *q++ = *p.ivlen;

         if (q != param_lens) {
             if (!EVP_CIPHER_CTX_set_params(ctx, param_lens)) {
@@ -1144,23 +1145,21 @@ int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
 {
     int r = 0;
-    const OSSL_PARAM *p;
+    struct evp_cipher_len_params_st p;

     if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
         r = ctx->cipher->set_ctx_params(ctx->algctx, params);
-        if (r > 0) {
-            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
-            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
-                r = 0;
-                ctx->key_len = -1;
-            }
+        if (r > 0 && !evp_cipher_len_params_decoder(params, &p))
+            r = 0;
+        if (r > 0 && p.keylen != NULL
+            && !OSSL_PARAM_get_int(p.keylen, &ctx->key_len)) {
+            r = 0;
+            ctx->key_len = -1;
         }
-        if (r > 0) {
-            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
-            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
-                r = 0;
-                ctx->iv_len = -1;
-            }
+        if (r > 0 && p.ivlen != NULL
+            && !OSSL_PARAM_get_int(p.ivlen, &ctx->iv_len)) {
+            r = 0;
+            ctx->iv_len = -1;
         }
     }
     return r;
diff --git a/crypto/evp/evp_enc.inc.in b/crypto/evp/evp_enc.inc.in
new file mode 100644
index 0000000000..f9da95c5e6
--- /dev/null
+++ b/crypto/evp/evp_enc.inc.in
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+{-
+use OpenSSL::paramnames qw(produce_param_decoder);
+
+sub produce_evp_param_decoder {
+    my $decoder = produce_param_decoder(@_);
+
+    $decoder =~ s|#include "prov/proverr.h"|#include <openssl/err.h>|;
+    $decoder =~ s/ERR_LIB_PROV/ERR_LIB_EVP/g;
+    $decoder =~ s/PROV_R_REPEATED_PARAMETER/ERR_R_PASSED_INVALID_ARGUMENT/g;
+    return $decoder;
+}
+-}
+
+#define evp_cipher_len_params_list
+{- produce_evp_param_decoder('evp_cipher_len_params',
+                         (['OSSL_CIPHER_PARAM_KEYLEN', 'keylen', 'size_t'],
+                          ['OSSL_CIPHER_PARAM_IVLEN',  'ivlen',  'size_t'],
+                         )); -}