From af841adf9fb0aedbc047ba8b59a5437ed21a89b8 Mon Sep 17 00:00:00 2001 From: Pauli Date: Fri, 25 Jul 2025 13:15:26 +1000 Subject: [PATCH] ml_kem kem: convert to using generated param decoders Reviewed-by: Paul Yang Reviewed-by: Shane Lontis Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/28149) --- providers/implementations/kem/ml_kem_kem.c.in | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/providers/implementations/kem/ml_kem_kem.c.in b/providers/implementations/kem/ml_kem_kem.c.in index 27aa3b81983..5c361ae8893 100644 --- a/providers/implementations/kem/ml_kem_kem.c.in +++ b/providers/implementations/kem/ml_kem_kem.c.in @@ -6,6 +6,9 @@ * in the file LICENSE in the source distribution or at * https://www.openssl.org/source/license.html */ +{- +use OpenSSL::paramnames qw(produce_param_decoder); +-} #include #include @@ -16,6 +19,7 @@ #include #include #include "crypto/ml_kem.h" +#include "internal/cryptlib.h" #include "prov/provider_ctx.h" #include "prov/implementations.h" #include "prov/securitycheck.h" @@ -95,12 +99,16 @@ static int ml_kem_decapsulate_init(void *vctx, void *vkey, return ml_kem_init(vctx, EVP_PKEY_OP_DECAPSULATE, key, params); } +{- produce_param_decoder('ml_kem_set_ctx_params', + (['KEM_PARAM_IKME', 'ikme', 'octet_string'], + )); -} + static int ml_kem_set_ctx_params(void *vctx, const OSSL_PARAM params[]) { PROV_ML_KEM_CTX *ctx = vctx; - const OSSL_PARAM *p; + struct ml_kem_set_ctx_params_st p; - if (ctx == NULL) + if (ctx == NULL || !ml_kem_set_ctx_params_decoder(params, &p)) return 0; if (ctx->op == EVP_PKEY_OP_DECAPSULATE && ctx->entropy != NULL) { @@ -109,16 +117,12 @@ static int ml_kem_set_ctx_params(void *vctx, const OSSL_PARAM params[]) ctx->entropy = NULL; } - if (ossl_param_is_empty(params)) - return 1; - /* Encapsulation ephemeral input key material "ikmE" */ - if (ctx->op == EVP_PKEY_OP_ENCAPSULATE - && (p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_IKME)) != NULL) { + if (ctx->op == EVP_PKEY_OP_ENCAPSULATE && p.ikme != NULL) { size_t len = ML_KEM_RANDOM_BYTES; ctx->entropy = ctx->entropy_buf; - if (OSSL_PARAM_get_octet_string(p, (void **)&ctx->entropy, + if (OSSL_PARAM_get_octet_string(p.ikme, (void **)&ctx->entropy, len, &len) && len == ML_KEM_RANDOM_BYTES) return 1; @@ -135,12 +139,7 @@ static int ml_kem_set_ctx_params(void *vctx, const OSSL_PARAM params[]) static const OSSL_PARAM *ml_kem_settable_ctx_params(ossl_unused void *vctx, ossl_unused void *provctx) { - static const OSSL_PARAM params[] = { - OSSL_PARAM_octet_string(OSSL_KEM_PARAM_IKME, NULL, 0), - OSSL_PARAM_END - }; - - return params; + return ml_kem_set_ctx_params_list; } static int ml_kem_encapsulate(void *vctx, unsigned char *ctext, size_t *clen, -- 2.47.3