xref: /freebsd/crypto/openssl/providers/implementations/signature/slh_dsa_sig.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery  *
4*e7be843bSPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e7be843bSPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*e7be843bSPierre Pronchery  * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery  * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery  */
9*e7be843bSPierre Pronchery 
10*e7be843bSPierre Pronchery #include <openssl/core_names.h>
11*e7be843bSPierre Pronchery #include <openssl/err.h>
12*e7be843bSPierre Pronchery #include <openssl/rand.h>
13*e7be843bSPierre Pronchery #include <openssl/proverr.h>
14*e7be843bSPierre Pronchery #include "prov/implementations.h"
15*e7be843bSPierre Pronchery #include "prov/providercommon.h"
16*e7be843bSPierre Pronchery #include "prov/provider_ctx.h"
17*e7be843bSPierre Pronchery #include "prov/der_slh_dsa.h"
18*e7be843bSPierre Pronchery #include "crypto/slh_dsa.h"
19*e7be843bSPierre Pronchery #include "internal/sizes.h"
20*e7be843bSPierre Pronchery 
21*e7be843bSPierre Pronchery #define SLH_DSA_MAX_ADD_RANDOM_LEN 32
22*e7be843bSPierre Pronchery 
23*e7be843bSPierre Pronchery #define SLH_DSA_MESSAGE_ENCODE_RAW  0
24*e7be843bSPierre Pronchery #define SLH_DSA_MESSAGE_ENCODE_PURE 1
25*e7be843bSPierre Pronchery 
26*e7be843bSPierre Pronchery static OSSL_FUNC_signature_sign_message_init_fn slh_dsa_sign_msg_init;
27*e7be843bSPierre Pronchery static OSSL_FUNC_signature_sign_fn slh_dsa_sign;
28*e7be843bSPierre Pronchery static OSSL_FUNC_signature_verify_message_init_fn slh_dsa_verify_msg_init;
29*e7be843bSPierre Pronchery static OSSL_FUNC_signature_verify_fn slh_dsa_verify;
30*e7be843bSPierre Pronchery static OSSL_FUNC_signature_digest_sign_init_fn slh_dsa_digest_signverify_init;
31*e7be843bSPierre Pronchery static OSSL_FUNC_signature_digest_sign_fn slh_dsa_digest_sign;
32*e7be843bSPierre Pronchery static OSSL_FUNC_signature_digest_verify_fn slh_dsa_digest_verify;
33*e7be843bSPierre Pronchery static OSSL_FUNC_signature_freectx_fn slh_dsa_freectx;
34*e7be843bSPierre Pronchery static OSSL_FUNC_signature_dupctx_fn slh_dsa_dupctx;
35*e7be843bSPierre Pronchery static OSSL_FUNC_signature_set_ctx_params_fn slh_dsa_set_ctx_params;
36*e7be843bSPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn slh_dsa_settable_ctx_params;
37*e7be843bSPierre Pronchery 
38*e7be843bSPierre Pronchery /*
39*e7be843bSPierre Pronchery  * NOTE: Any changes to this structure may require updating slh_dsa_dupctx().
40*e7be843bSPierre Pronchery  */
41*e7be843bSPierre Pronchery typedef struct {
42*e7be843bSPierre Pronchery     SLH_DSA_KEY *key; /* Note that the key is not owned by this object */
43*e7be843bSPierre Pronchery     SLH_DSA_HASH_CTX *hash_ctx;
44*e7be843bSPierre Pronchery     uint8_t context_string[SLH_DSA_MAX_CONTEXT_STRING_LEN];
45*e7be843bSPierre Pronchery     size_t context_string_len;
46*e7be843bSPierre Pronchery     uint8_t add_random[SLH_DSA_MAX_ADD_RANDOM_LEN];
47*e7be843bSPierre Pronchery     size_t add_random_len;
48*e7be843bSPierre Pronchery     int msg_encode;
49*e7be843bSPierre Pronchery     int deterministic;
50*e7be843bSPierre Pronchery     OSSL_LIB_CTX *libctx;
51*e7be843bSPierre Pronchery     char *propq;
52*e7be843bSPierre Pronchery     const char *alg;
53*e7be843bSPierre Pronchery     /* The Algorithm Identifier of the signature algorithm */
54*e7be843bSPierre Pronchery     uint8_t aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
55*e7be843bSPierre Pronchery     size_t  aid_len;
56*e7be843bSPierre Pronchery } PROV_SLH_DSA_CTX;
57*e7be843bSPierre Pronchery 
slh_dsa_freectx(void * vctx)58*e7be843bSPierre Pronchery static void slh_dsa_freectx(void *vctx)
59*e7be843bSPierre Pronchery {
60*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx = (PROV_SLH_DSA_CTX *)vctx;
61*e7be843bSPierre Pronchery 
62*e7be843bSPierre Pronchery     ossl_slh_dsa_hash_ctx_free(ctx->hash_ctx);
63*e7be843bSPierre Pronchery     OPENSSL_free(ctx->propq);
64*e7be843bSPierre Pronchery     OPENSSL_cleanse(ctx->add_random, ctx->add_random_len);
65*e7be843bSPierre Pronchery     OPENSSL_free(ctx);
66*e7be843bSPierre Pronchery }
67*e7be843bSPierre Pronchery 
slh_dsa_newctx(void * provctx,const char * alg,const char * propq)68*e7be843bSPierre Pronchery static void *slh_dsa_newctx(void *provctx, const char *alg, const char *propq)
69*e7be843bSPierre Pronchery {
70*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx;
71*e7be843bSPierre Pronchery 
72*e7be843bSPierre Pronchery     if (!ossl_prov_is_running())
73*e7be843bSPierre Pronchery         return NULL;
74*e7be843bSPierre Pronchery 
75*e7be843bSPierre Pronchery     ctx = OPENSSL_zalloc(sizeof(PROV_SLH_DSA_CTX));
76*e7be843bSPierre Pronchery     if (ctx == NULL)
77*e7be843bSPierre Pronchery         return NULL;
78*e7be843bSPierre Pronchery 
79*e7be843bSPierre Pronchery     ctx->libctx = PROV_LIBCTX_OF(provctx);
80*e7be843bSPierre Pronchery     if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL)
81*e7be843bSPierre Pronchery         goto err;
82*e7be843bSPierre Pronchery     ctx->alg = alg;
83*e7be843bSPierre Pronchery     ctx->msg_encode = SLH_DSA_MESSAGE_ENCODE_PURE;
84*e7be843bSPierre Pronchery     return ctx;
85*e7be843bSPierre Pronchery  err:
86*e7be843bSPierre Pronchery     slh_dsa_freectx(ctx);
87*e7be843bSPierre Pronchery     return NULL;
88*e7be843bSPierre Pronchery }
89*e7be843bSPierre Pronchery 
slh_dsa_dupctx(void * vctx)90*e7be843bSPierre Pronchery static void *slh_dsa_dupctx(void *vctx)
91*e7be843bSPierre Pronchery {
92*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *src = (PROV_SLH_DSA_CTX *)vctx;
93*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ret;
94*e7be843bSPierre Pronchery 
95*e7be843bSPierre Pronchery     if (!ossl_prov_is_running())
96*e7be843bSPierre Pronchery         return NULL;
97*e7be843bSPierre Pronchery 
98*e7be843bSPierre Pronchery     /*
99*e7be843bSPierre Pronchery      * Note that the SLH_DSA_KEY is ref counted via EVP_PKEY so we can just copy
100*e7be843bSPierre Pronchery      * the key here.
101*e7be843bSPierre Pronchery      */
102*e7be843bSPierre Pronchery     ret = OPENSSL_memdup(src, sizeof(*src));
103*e7be843bSPierre Pronchery     if (ret == NULL)
104*e7be843bSPierre Pronchery         return NULL;
105*e7be843bSPierre Pronchery     ret->propq = NULL;
106*e7be843bSPierre Pronchery     ret->hash_ctx = NULL;
107*e7be843bSPierre Pronchery     if (src->propq != NULL && (ret->propq = OPENSSL_strdup(src->propq)) == NULL)
108*e7be843bSPierre Pronchery         goto err;
109*e7be843bSPierre Pronchery     ret->hash_ctx = ossl_slh_dsa_hash_ctx_dup(src->hash_ctx);
110*e7be843bSPierre Pronchery     if (ret->hash_ctx == NULL)
111*e7be843bSPierre Pronchery         goto err;
112*e7be843bSPierre Pronchery 
113*e7be843bSPierre Pronchery     return ret;
114*e7be843bSPierre Pronchery  err:
115*e7be843bSPierre Pronchery     slh_dsa_freectx(ret);
116*e7be843bSPierre Pronchery     return NULL;
117*e7be843bSPierre Pronchery }
118*e7be843bSPierre Pronchery 
slh_dsa_set_alg_id_buffer(PROV_SLH_DSA_CTX * ctx)119*e7be843bSPierre Pronchery static int slh_dsa_set_alg_id_buffer(PROV_SLH_DSA_CTX *ctx)
120*e7be843bSPierre Pronchery {
121*e7be843bSPierre Pronchery     int ret;
122*e7be843bSPierre Pronchery     WPACKET pkt;
123*e7be843bSPierre Pronchery     uint8_t *aid = NULL;
124*e7be843bSPierre Pronchery 
125*e7be843bSPierre Pronchery     /*
126*e7be843bSPierre Pronchery      * We do not care about DER writing errors.
127*e7be843bSPierre Pronchery      * All it really means is that for some reason, there's no
128*e7be843bSPierre Pronchery      * AlgorithmIdentifier to be had, but the operation itself is
129*e7be843bSPierre Pronchery      * still valid, just as long as it's not used to construct
130*e7be843bSPierre Pronchery      * anything that needs an AlgorithmIdentifier.
131*e7be843bSPierre Pronchery      */
132*e7be843bSPierre Pronchery     ctx->aid_len = 0;
133*e7be843bSPierre Pronchery     ret = WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf));
134*e7be843bSPierre Pronchery     ret = ret && ossl_DER_w_algorithmIdentifier_SLH_DSA(&pkt, -1, ctx->key);
135*e7be843bSPierre Pronchery     if (ret && WPACKET_finish(&pkt)) {
136*e7be843bSPierre Pronchery         WPACKET_get_total_written(&pkt, &ctx->aid_len);
137*e7be843bSPierre Pronchery         aid = WPACKET_get_curr(&pkt);
138*e7be843bSPierre Pronchery     }
139*e7be843bSPierre Pronchery     WPACKET_cleanup(&pkt);
140*e7be843bSPierre Pronchery     if (aid != NULL && ctx->aid_len != 0)
141*e7be843bSPierre Pronchery         memmove(ctx->aid_buf, aid, ctx->aid_len);
142*e7be843bSPierre Pronchery     return 1;
143*e7be843bSPierre Pronchery }
144*e7be843bSPierre Pronchery 
slh_dsa_signverify_msg_init(void * vctx,void * vkey,const OSSL_PARAM params[],int operation,const char * desc)145*e7be843bSPierre Pronchery static int slh_dsa_signverify_msg_init(void *vctx, void *vkey,
146*e7be843bSPierre Pronchery                                        const OSSL_PARAM params[], int operation,
147*e7be843bSPierre Pronchery                                        const char *desc)
148*e7be843bSPierre Pronchery {
149*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx = (PROV_SLH_DSA_CTX *)vctx;
150*e7be843bSPierre Pronchery     SLH_DSA_KEY *key = vkey;
151*e7be843bSPierre Pronchery 
152*e7be843bSPierre Pronchery     if (!ossl_prov_is_running()
153*e7be843bSPierre Pronchery             || ctx == NULL)
154*e7be843bSPierre Pronchery         return 0;
155*e7be843bSPierre Pronchery 
156*e7be843bSPierre Pronchery     if (vkey == NULL && ctx->key == NULL) {
157*e7be843bSPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
158*e7be843bSPierre Pronchery         return 0;
159*e7be843bSPierre Pronchery     }
160*e7be843bSPierre Pronchery 
161*e7be843bSPierre Pronchery     if (key != NULL) {
162*e7be843bSPierre Pronchery         if (!ossl_slh_dsa_key_type_matches(key, ctx->alg))
163*e7be843bSPierre Pronchery             return 0;
164*e7be843bSPierre Pronchery         ctx->hash_ctx = ossl_slh_dsa_hash_ctx_new(key);
165*e7be843bSPierre Pronchery         if (ctx->hash_ctx == NULL)
166*e7be843bSPierre Pronchery             return 0;
167*e7be843bSPierre Pronchery         ctx->key = vkey;
168*e7be843bSPierre Pronchery     }
169*e7be843bSPierre Pronchery 
170*e7be843bSPierre Pronchery     slh_dsa_set_alg_id_buffer(ctx);
171*e7be843bSPierre Pronchery     if (!slh_dsa_set_ctx_params(ctx, params))
172*e7be843bSPierre Pronchery         return 0;
173*e7be843bSPierre Pronchery     return 1;
174*e7be843bSPierre Pronchery }
175*e7be843bSPierre Pronchery 
slh_dsa_sign_msg_init(void * vctx,void * vkey,const OSSL_PARAM params[])176*e7be843bSPierre Pronchery static int slh_dsa_sign_msg_init(void *vctx, void *vkey, const OSSL_PARAM params[])
177*e7be843bSPierre Pronchery {
178*e7be843bSPierre Pronchery     return slh_dsa_signverify_msg_init(vctx, vkey, params,
179*e7be843bSPierre Pronchery                                        EVP_PKEY_OP_SIGN, "SLH_DSA Sign Init");
180*e7be843bSPierre Pronchery }
181*e7be843bSPierre Pronchery 
slh_dsa_digest_signverify_init(void * vctx,const char * mdname,void * vkey,const OSSL_PARAM params[])182*e7be843bSPierre Pronchery static int slh_dsa_digest_signverify_init(void *vctx, const char *mdname,
183*e7be843bSPierre Pronchery                                           void *vkey, const OSSL_PARAM params[])
184*e7be843bSPierre Pronchery {
185*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx = (PROV_SLH_DSA_CTX *)vctx;
186*e7be843bSPierre Pronchery 
187*e7be843bSPierre Pronchery     if (mdname != NULL && mdname[0] != '\0') {
188*e7be843bSPierre Pronchery         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
189*e7be843bSPierre Pronchery                        "Explicit digest not supported for SLH-DSA operations");
190*e7be843bSPierre Pronchery         return 0;
191*e7be843bSPierre Pronchery     }
192*e7be843bSPierre Pronchery 
193*e7be843bSPierre Pronchery     if (vkey == NULL && ctx->key != NULL)
194*e7be843bSPierre Pronchery         return slh_dsa_set_ctx_params(ctx, params);
195*e7be843bSPierre Pronchery 
196*e7be843bSPierre Pronchery     return slh_dsa_signverify_msg_init(vctx, vkey, params,
197*e7be843bSPierre Pronchery                                        EVP_PKEY_OP_SIGN, "SLH_DSA Sign Init");
198*e7be843bSPierre Pronchery }
199*e7be843bSPierre Pronchery 
slh_dsa_sign(void * vctx,unsigned char * sig,size_t * siglen,size_t sigsize,const unsigned char * msg,size_t msg_len)200*e7be843bSPierre Pronchery static int slh_dsa_sign(void *vctx, unsigned char *sig, size_t *siglen,
201*e7be843bSPierre Pronchery                         size_t sigsize, const unsigned char *msg, size_t msg_len)
202*e7be843bSPierre Pronchery {
203*e7be843bSPierre Pronchery     int ret = 0;
204*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx = (PROV_SLH_DSA_CTX *)vctx;
205*e7be843bSPierre Pronchery     uint8_t add_rand[SLH_DSA_MAX_ADD_RANDOM_LEN], *opt_rand = NULL;
206*e7be843bSPierre Pronchery     size_t n = 0;
207*e7be843bSPierre Pronchery 
208*e7be843bSPierre Pronchery     if (!ossl_prov_is_running())
209*e7be843bSPierre Pronchery         return 0;
210*e7be843bSPierre Pronchery 
211*e7be843bSPierre Pronchery     if (sig != NULL) {
212*e7be843bSPierre Pronchery         if (ctx->add_random_len != 0) {
213*e7be843bSPierre Pronchery             opt_rand = ctx->add_random;
214*e7be843bSPierre Pronchery         } else if (ctx->deterministic == 0) {
215*e7be843bSPierre Pronchery             n = ossl_slh_dsa_key_get_n(ctx->key);
216*e7be843bSPierre Pronchery             if (RAND_priv_bytes_ex(ctx->libctx, add_rand, n, 0) <= 0)
217*e7be843bSPierre Pronchery                 return 0;
218*e7be843bSPierre Pronchery             opt_rand = add_rand;
219*e7be843bSPierre Pronchery         }
220*e7be843bSPierre Pronchery     }
221*e7be843bSPierre Pronchery     ret = ossl_slh_dsa_sign(ctx->hash_ctx, msg, msg_len,
222*e7be843bSPierre Pronchery                             ctx->context_string, ctx->context_string_len,
223*e7be843bSPierre Pronchery                             opt_rand, ctx->msg_encode,
224*e7be843bSPierre Pronchery                             sig, siglen, sigsize);
225*e7be843bSPierre Pronchery     if (opt_rand != add_rand)
226*e7be843bSPierre Pronchery         OPENSSL_cleanse(opt_rand, n);
227*e7be843bSPierre Pronchery     return ret;
228*e7be843bSPierre Pronchery }
229*e7be843bSPierre Pronchery 
slh_dsa_digest_sign(void * vctx,uint8_t * sig,size_t * siglen,size_t sigsize,const uint8_t * tbs,size_t tbslen)230*e7be843bSPierre Pronchery static int slh_dsa_digest_sign(void *vctx, uint8_t *sig, size_t *siglen, size_t sigsize,
231*e7be843bSPierre Pronchery                                const uint8_t *tbs, size_t tbslen)
232*e7be843bSPierre Pronchery {
233*e7be843bSPierre Pronchery     return slh_dsa_sign(vctx, sig, siglen, sigsize, tbs, tbslen);
234*e7be843bSPierre Pronchery }
235*e7be843bSPierre Pronchery 
slh_dsa_verify_msg_init(void * vctx,void * vkey,const OSSL_PARAM params[])236*e7be843bSPierre Pronchery static int slh_dsa_verify_msg_init(void *vctx, void *vkey, const OSSL_PARAM params[])
237*e7be843bSPierre Pronchery {
238*e7be843bSPierre Pronchery     return slh_dsa_signverify_msg_init(vctx, vkey, params, EVP_PKEY_OP_VERIFY,
239*e7be843bSPierre Pronchery                                        "SLH_DSA Verify Init");
240*e7be843bSPierre Pronchery }
241*e7be843bSPierre Pronchery 
slh_dsa_verify(void * vctx,const uint8_t * sig,size_t siglen,const uint8_t * msg,size_t msg_len)242*e7be843bSPierre Pronchery static int slh_dsa_verify(void *vctx, const uint8_t *sig, size_t siglen,
243*e7be843bSPierre Pronchery                           const uint8_t *msg, size_t msg_len)
244*e7be843bSPierre Pronchery {
245*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx = (PROV_SLH_DSA_CTX *)vctx;
246*e7be843bSPierre Pronchery 
247*e7be843bSPierre Pronchery     if (!ossl_prov_is_running())
248*e7be843bSPierre Pronchery         return 0;
249*e7be843bSPierre Pronchery     return ossl_slh_dsa_verify(ctx->hash_ctx, msg, msg_len,
250*e7be843bSPierre Pronchery                                ctx->context_string, ctx->context_string_len,
251*e7be843bSPierre Pronchery                                ctx->msg_encode, sig, siglen);
252*e7be843bSPierre Pronchery }
slh_dsa_digest_verify(void * vctx,const uint8_t * sig,size_t siglen,const uint8_t * tbs,size_t tbslen)253*e7be843bSPierre Pronchery static int slh_dsa_digest_verify(void *vctx, const uint8_t *sig, size_t siglen,
254*e7be843bSPierre Pronchery                                  const uint8_t *tbs, size_t tbslen)
255*e7be843bSPierre Pronchery {
256*e7be843bSPierre Pronchery     return slh_dsa_verify(vctx, sig, siglen, tbs, tbslen);
257*e7be843bSPierre Pronchery }
258*e7be843bSPierre Pronchery 
slh_dsa_set_ctx_params(void * vctx,const OSSL_PARAM params[])259*e7be843bSPierre Pronchery static int slh_dsa_set_ctx_params(void *vctx, const OSSL_PARAM params[])
260*e7be843bSPierre Pronchery {
261*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *pctx = (PROV_SLH_DSA_CTX *)vctx;
262*e7be843bSPierre Pronchery     const OSSL_PARAM *p;
263*e7be843bSPierre Pronchery 
264*e7be843bSPierre Pronchery     if (pctx == NULL)
265*e7be843bSPierre Pronchery         return 0;
266*e7be843bSPierre Pronchery     if (ossl_param_is_empty(params))
267*e7be843bSPierre Pronchery         return 1;
268*e7be843bSPierre Pronchery 
269*e7be843bSPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_CONTEXT_STRING);
270*e7be843bSPierre Pronchery     if (p != NULL) {
271*e7be843bSPierre Pronchery         void *vp = pctx->context_string;
272*e7be843bSPierre Pronchery 
273*e7be843bSPierre Pronchery         if (!OSSL_PARAM_get_octet_string(p, &vp, sizeof(pctx->context_string),
274*e7be843bSPierre Pronchery                                          &(pctx->context_string_len))) {
275*e7be843bSPierre Pronchery             pctx->context_string_len = 0;
276*e7be843bSPierre Pronchery             return 0;
277*e7be843bSPierre Pronchery         }
278*e7be843bSPierre Pronchery     }
279*e7be843bSPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_TEST_ENTROPY);
280*e7be843bSPierre Pronchery     if (p != NULL) {
281*e7be843bSPierre Pronchery         void *vp = pctx->add_random;
282*e7be843bSPierre Pronchery         size_t n = ossl_slh_dsa_key_get_n(pctx->key);
283*e7be843bSPierre Pronchery 
284*e7be843bSPierre Pronchery         if (!OSSL_PARAM_get_octet_string(p, &vp, n, &(pctx->add_random_len))
285*e7be843bSPierre Pronchery                 || pctx->add_random_len != n) {
286*e7be843bSPierre Pronchery             pctx->add_random_len = 0;
287*e7be843bSPierre Pronchery             return 0;
288*e7be843bSPierre Pronchery         }
289*e7be843bSPierre Pronchery     }
290*e7be843bSPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DETERMINISTIC);
291*e7be843bSPierre Pronchery     if (p != NULL && !OSSL_PARAM_get_int(p, &pctx->deterministic))
292*e7be843bSPierre Pronchery         return 0;
293*e7be843bSPierre Pronchery 
294*e7be843bSPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MESSAGE_ENCODING);
295*e7be843bSPierre Pronchery     if (p != NULL && !OSSL_PARAM_get_int(p, &pctx->msg_encode))
296*e7be843bSPierre Pronchery         return 0;
297*e7be843bSPierre Pronchery     return 1;
298*e7be843bSPierre Pronchery }
299*e7be843bSPierre Pronchery 
slh_dsa_settable_ctx_params(void * vctx,ossl_unused void * provctx)300*e7be843bSPierre Pronchery static const OSSL_PARAM *slh_dsa_settable_ctx_params(void *vctx,
301*e7be843bSPierre Pronchery                                                      ossl_unused void *provctx)
302*e7be843bSPierre Pronchery {
303*e7be843bSPierre Pronchery     static const OSSL_PARAM settable_ctx_params[] = {
304*e7be843bSPierre Pronchery         OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, NULL, 0),
305*e7be843bSPierre Pronchery         OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_TEST_ENTROPY, NULL, 0),
306*e7be843bSPierre Pronchery         OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_DETERMINISTIC, 0),
307*e7be843bSPierre Pronchery         OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MESSAGE_ENCODING, 0),
308*e7be843bSPierre Pronchery         OSSL_PARAM_END
309*e7be843bSPierre Pronchery     };
310*e7be843bSPierre Pronchery 
311*e7be843bSPierre Pronchery     return settable_ctx_params;
312*e7be843bSPierre Pronchery }
313*e7be843bSPierre Pronchery 
314*e7be843bSPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
315*e7be843bSPierre Pronchery     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
316*e7be843bSPierre Pronchery     OSSL_PARAM_END
317*e7be843bSPierre Pronchery };
318*e7be843bSPierre Pronchery 
slh_dsa_gettable_ctx_params(ossl_unused void * vctx,ossl_unused void * provctx)319*e7be843bSPierre Pronchery static const OSSL_PARAM *slh_dsa_gettable_ctx_params(ossl_unused void *vctx,
320*e7be843bSPierre Pronchery                                                      ossl_unused void *provctx)
321*e7be843bSPierre Pronchery {
322*e7be843bSPierre Pronchery     return known_gettable_ctx_params;
323*e7be843bSPierre Pronchery }
324*e7be843bSPierre Pronchery 
slh_dsa_get_ctx_params(void * vctx,OSSL_PARAM * params)325*e7be843bSPierre Pronchery static int slh_dsa_get_ctx_params(void *vctx, OSSL_PARAM *params)
326*e7be843bSPierre Pronchery {
327*e7be843bSPierre Pronchery     PROV_SLH_DSA_CTX *ctx = (PROV_SLH_DSA_CTX *)vctx;
328*e7be843bSPierre Pronchery     OSSL_PARAM *p;
329*e7be843bSPierre Pronchery 
330*e7be843bSPierre Pronchery     if (ctx == NULL)
331*e7be843bSPierre Pronchery         return 0;
332*e7be843bSPierre Pronchery 
333*e7be843bSPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
334*e7be843bSPierre Pronchery     if (p != NULL
335*e7be843bSPierre Pronchery         && !OSSL_PARAM_set_octet_string(p,
336*e7be843bSPierre Pronchery                                         ctx->aid_len == 0 ? NULL : ctx->aid_buf,
337*e7be843bSPierre Pronchery                                         ctx->aid_len))
338*e7be843bSPierre Pronchery         return 0;
339*e7be843bSPierre Pronchery 
340*e7be843bSPierre Pronchery     return 1;
341*e7be843bSPierre Pronchery }
342*e7be843bSPierre Pronchery 
343*e7be843bSPierre Pronchery #define MAKE_SIGNATURE_FUNCTIONS(alg, fn)                                      \
344*e7be843bSPierre Pronchery     static OSSL_FUNC_signature_newctx_fn slh_dsa_##fn##_newctx;                \
345*e7be843bSPierre Pronchery     static void *slh_dsa_##fn##_newctx(void *provctx, const char *propq)       \
346*e7be843bSPierre Pronchery     {                                                                          \
347*e7be843bSPierre Pronchery         return slh_dsa_newctx(provctx, alg, propq);                            \
348*e7be843bSPierre Pronchery     }                                                                          \
349*e7be843bSPierre Pronchery     const OSSL_DISPATCH ossl_slh_dsa_##fn##_signature_functions[] = {          \
350*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))slh_dsa_##fn##_newctx }, \
351*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT,                               \
352*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_sign_msg_init },                             \
353*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))slh_dsa_sign },            \
354*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT,                             \
355*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_verify_msg_init },                           \
356*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))slh_dsa_verify },        \
357*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,                                \
358*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_digest_signverify_init },                    \
359*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_SIGN,                                     \
360*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_digest_sign },                               \
361*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,                              \
362*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_digest_signverify_init },                    \
363*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,                                   \
364*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_digest_verify },                             \
365*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))slh_dsa_freectx },      \
366*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))slh_dsa_dupctx },        \
367*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))slh_dsa_set_ctx_params },\
368*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                             \
369*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_settable_ctx_params },                       \
370*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                                  \
371*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_get_ctx_params },                            \
372*e7be843bSPierre Pronchery         { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                             \
373*e7be843bSPierre Pronchery           (void (*)(void))slh_dsa_gettable_ctx_params },                       \
374*e7be843bSPierre Pronchery         OSSL_DISPATCH_END                                                      \
375*e7be843bSPierre Pronchery     }
376*e7be843bSPierre Pronchery 
377*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHA2-128s", sha2_128s);
378*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHA2-128f", sha2_128f);
379*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHA2-192s", sha2_192s);
380*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHA2-192f", sha2_192f);
381*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHA2-256s", sha2_256s);
382*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHA2-256f", sha2_256f);
383*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHAKE-128s", shake_128s);
384*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHAKE-128f", shake_128f);
385*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHAKE-192s", shake_192s);
386*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHAKE-192f", shake_192f);
387*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHAKE-256s", shake_256s);
388*e7be843bSPierre Pronchery MAKE_SIGNATURE_FUNCTIONS("SLH-DSA-SHAKE-256f", shake_256f);
389