xref: /freebsd/crypto/openssl/providers/implementations/keymgmt/ml_dsa_kmgmt.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <openssl/core_dispatch.h>
11 #include <openssl/core_names.h>
12 #include <openssl/evp.h>
13 #include <openssl/param_build.h>
14 #include <openssl/proverr.h>
15 #include <openssl/self_test.h>
16 #include "crypto/ml_dsa.h"
17 #include "internal/fips.h"
18 #include "internal/param_build_set.h"
19 #include "prov/implementations.h"
20 #include "prov/providercommon.h"
21 #include "prov/provider_ctx.h"
22 #include "prov/ml_dsa.h"
23 
24 static OSSL_FUNC_keymgmt_free_fn ml_dsa_free_key;
25 static OSSL_FUNC_keymgmt_has_fn ml_dsa_has;
26 static OSSL_FUNC_keymgmt_match_fn ml_dsa_match;
27 static OSSL_FUNC_keymgmt_import_fn ml_dsa_import;
28 static OSSL_FUNC_keymgmt_export_fn ml_dsa_export;
29 static OSSL_FUNC_keymgmt_import_types_fn ml_dsa_imexport_types;
30 static OSSL_FUNC_keymgmt_export_types_fn ml_dsa_imexport_types;
31 static OSSL_FUNC_keymgmt_dup_fn ml_dsa_dup_key;
32 static OSSL_FUNC_keymgmt_get_params_fn ml_dsa_get_params;
33 static OSSL_FUNC_keymgmt_gettable_params_fn ml_dsa_gettable_params;
34 static OSSL_FUNC_keymgmt_validate_fn ml_dsa_validate;
35 static OSSL_FUNC_keymgmt_gen_init_fn ml_dsa_gen_init;
36 static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_dsa_gen_cleanup;
37 static OSSL_FUNC_keymgmt_gen_set_params_fn ml_dsa_gen_set_params;
38 static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_dsa_gen_settable_params;
39 #ifndef FIPS_MODULE
40 static OSSL_FUNC_keymgmt_load_fn ml_dsa_load;
41 #endif
42 
43 struct ml_dsa_gen_ctx {
44     PROV_CTX *provctx;
45     char *propq;
46     uint8_t entropy[32];
47     size_t entropy_len;
48 };
49 
50 #ifdef FIPS_MODULE
ml_dsa_pairwise_test(const ML_DSA_KEY * key)51 static int ml_dsa_pairwise_test(const ML_DSA_KEY *key)
52 {
53     OSSL_SELF_TEST *st = NULL;
54     OSSL_CALLBACK *cb = NULL;
55     OSSL_LIB_CTX *ctx;
56     void *cbarg = NULL;
57     static const uint8_t msg[] = { 80, 108, 117, 103, 104 };
58     uint8_t rnd[ML_DSA_ENTROPY_LEN];
59     uint8_t sig[ML_DSA_87_SIG_LEN];
60     size_t sig_len = 0;
61     int ret = 0;
62 
63     if (!ml_dsa_has(key, OSSL_KEYMGMT_SELECT_KEYPAIR)
64             || ossl_fips_self_testing())
65         return 1;
66 
67     /*
68      * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st`
69      * is NULL.
70      */
71     ctx = ossl_ml_dsa_key_get0_libctx(key);
72     OSSL_SELF_TEST_get_callback(ctx, &cb, &cbarg);
73 
74     if ((st = OSSL_SELF_TEST_new(cb, cbarg)) == NULL)
75         return 0;
76 
77     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
78                            OSSL_SELF_TEST_DESC_PCT_ML_DSA);
79 
80     memset(rnd, 0, sizeof(rnd));
81     memset(sig, 0, sizeof(sig));
82 
83     if (ossl_ml_dsa_sign(key, 0, msg, sizeof(msg), NULL, 0, rnd, sizeof(rnd), 0,
84                          sig, &sig_len, sizeof(sig)) <= 0)
85         goto err;
86 
87     OSSL_SELF_TEST_oncorrupt_byte(st, sig);
88 
89     if (ossl_ml_dsa_verify(key, 0, msg, sizeof(msg), NULL, 0, 0,
90                            sig, sig_len) <= 0)
91         goto err;
92 
93     ret = 1;
94  err:
95     OSSL_SELF_TEST_onend(st, ret);
96     OSSL_SELF_TEST_free(st);
97     return ret;
98 }
99 #endif
100 
ossl_prov_ml_dsa_new(PROV_CTX * ctx,const char * propq,int evp_type)101 ML_DSA_KEY *ossl_prov_ml_dsa_new(PROV_CTX *ctx, const char *propq, int evp_type)
102 {
103     ML_DSA_KEY *key;
104 
105     if (!ossl_prov_is_running())
106         return 0;
107 
108     key = ossl_ml_dsa_key_new(PROV_LIBCTX_OF(ctx), propq, evp_type);
109     /*
110      * When decoding, if the key ends up "loaded" into the same provider, these
111      * are the correct config settings, otherwise, new values will be assigned
112      * on import into a different provider.  The "load" API does not pass along
113      * the provider context.
114      */
115     if (key != NULL) {
116         int flags_set = 0, flags_clr = 0;
117 
118         if (ossl_prov_ctx_get_bool_param(
119                 ctx, OSSL_PKEY_PARAM_ML_DSA_RETAIN_SEED, 1))
120             flags_set |= ML_DSA_KEY_RETAIN_SEED;
121         else
122             flags_clr = ML_DSA_KEY_RETAIN_SEED;
123 
124         if (ossl_prov_ctx_get_bool_param(
125                 ctx, OSSL_PKEY_PARAM_ML_DSA_PREFER_SEED, 1))
126             flags_set |= ML_DSA_KEY_PREFER_SEED;
127         else
128             flags_clr |= ML_DSA_KEY_PREFER_SEED;
129 
130         ossl_ml_dsa_set_prekey(key, flags_set, flags_clr, NULL, 0, NULL, 0);
131     }
132     return key;
133 }
134 
ml_dsa_free_key(void * keydata)135 static void ml_dsa_free_key(void *keydata)
136 {
137     ossl_ml_dsa_key_free((ML_DSA_KEY *)keydata);
138 }
139 
ml_dsa_dup_key(const void * keydata_from,int selection)140 static void *ml_dsa_dup_key(const void *keydata_from, int selection)
141 {
142     if (ossl_prov_is_running())
143         return ossl_ml_dsa_key_dup(keydata_from, selection);
144     return NULL;
145 }
146 
ml_dsa_has(const void * keydata,int selection)147 static int ml_dsa_has(const void *keydata, int selection)
148 {
149     const ML_DSA_KEY *key = keydata;
150 
151     if (!ossl_prov_is_running() || key == NULL)
152         return 0;
153     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
154         return 1; /* the selection is not missing */
155 
156     return ossl_ml_dsa_key_has(key, selection);
157 }
158 
ml_dsa_match(const void * keydata1,const void * keydata2,int selection)159 static int ml_dsa_match(const void *keydata1, const void *keydata2, int selection)
160 {
161     const ML_DSA_KEY *key1 = keydata1;
162     const ML_DSA_KEY *key2 = keydata2;
163 
164     if (!ossl_prov_is_running())
165         return 0;
166     if (key1 == NULL || key2 == NULL)
167         return 0;
168     return ossl_ml_dsa_key_equal(key1, key2, selection);
169 }
170 
ml_dsa_validate(const void * key_data,int selection,int check_type)171 static int ml_dsa_validate(const void *key_data, int selection, int check_type)
172 {
173     const ML_DSA_KEY *key = key_data;
174 
175     if (!ml_dsa_has(key, selection))
176         return 0;
177 
178     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
179         return ossl_ml_dsa_key_pairwise_check(key);
180     return 1;
181 }
182 
183 /**
184  * @brief Load a ML_DSA key from raw data.
185  *
186  * @param key An ML_DSA key to load into
187  * @param params An array of parameters containing key data.
188  * @param include_private Set to 1 to optionally include the private key data
189  *                        if it exists.
190  * @returns 1 on success, or 0 on failure.
191  */
ml_dsa_key_fromdata(ML_DSA_KEY * key,const OSSL_PARAM params[],int include_private)192 static int ml_dsa_key_fromdata(ML_DSA_KEY *key, const OSSL_PARAM params[],
193                                int include_private)
194 {
195     const OSSL_PARAM *p = NULL;
196     const ML_DSA_PARAMS *key_params = ossl_ml_dsa_key_params(key);
197     const uint8_t *pk = NULL, *sk = NULL, *seed = NULL;
198     size_t pk_len = 0, sk_len = 0, seed_len = 0;
199 
200     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
201     if (p != NULL
202         && !OSSL_PARAM_get_octet_string_ptr(p, (const void **)&pk, &pk_len))
203         return 0;
204     if (pk != NULL && pk_len != key_params->pk_len) {
205         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH,
206                        "Invalid %s public key length", key_params->alg);
207         return 0;
208     }
209 
210     /* Private key is optional */
211     if (include_private) {
212         p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ML_DSA_SEED);
213         if (p != NULL
214             && !OSSL_PARAM_get_octet_string_ptr(p, (const void **)&seed,
215                                                 &seed_len))
216             return 0;
217         if (seed != NULL && seed_len != ML_DSA_SEED_BYTES) {
218             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
219             return 0;
220         }
221         p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
222         if (p != NULL
223             && !OSSL_PARAM_get_octet_string_ptr(p, (const void **)&sk, &sk_len))
224             return 0;
225         if (sk != NULL && sk_len != key_params->sk_len) {
226             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH,
227                            "Invalid %s private key length", key_params->alg);
228             return 0;
229         }
230     }
231 
232     /* The caller MUST specify at least one of seed, private or public keys. */
233     if (seed_len == 0 && pk_len == 0 && sk_len == 0) {
234         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
235         return 0;
236     }
237 
238     if (seed_len != 0
239         && (sk_len == 0
240             || (ossl_ml_dsa_key_get_prov_flags(key) & ML_DSA_KEY_PREFER_SEED))) {
241         if (!ossl_ml_dsa_set_prekey(key, 0, 0, seed, seed_len, sk, sk_len))
242             return 0;
243         if (!ossl_ml_dsa_generate_key(key)) {
244             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
245             return 0;
246         }
247     } else if (sk_len > 0) {
248         if (!ossl_ml_dsa_sk_decode(key, sk, sk_len))
249             return 0;
250     } else if (pk_len > 0) {
251         if (!ossl_ml_dsa_pk_decode(key, pk, pk_len))
252             return 0;
253     }
254 
255     /* Error if the supplied public key does not match the generated key */
256     if (pk_len == 0
257         || seed_len + sk_len == 0
258         || memcmp(ossl_ml_dsa_key_get_pub(key), pk, pk_len) == 0)
259         return 1;
260     ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
261                    "explicit %s public key does not match private",
262                    key_params->alg);
263     ossl_ml_dsa_key_reset(key);
264     return 0;
265 }
266 
ml_dsa_import(void * keydata,int selection,const OSSL_PARAM params[])267 static int ml_dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
268 {
269     ML_DSA_KEY *key = keydata;
270     int include_priv;
271 
272     if (!ossl_prov_is_running() || key == NULL)
273         return 0;
274 
275     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
276         return 0;
277 
278     include_priv = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0);
279     return ml_dsa_key_fromdata(key, params, include_priv);
280 }
281 
282 #define ML_DSA_IMEXPORTABLE_PARAMETERS \
283     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0), \
284     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), \
285     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
286 
287 static const OSSL_PARAM ml_dsa_key_types[] = {
288     ML_DSA_IMEXPORTABLE_PARAMETERS,
289     OSSL_PARAM_END
290 };
ml_dsa_imexport_types(int selection)291 static const OSSL_PARAM *ml_dsa_imexport_types(int selection)
292 {
293     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
294         return NULL;
295     return ml_dsa_key_types;
296 }
297 
298 static const OSSL_PARAM ml_dsa_params[] = {
299     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
300     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
301     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
302     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST, NULL, 0),
303     ML_DSA_IMEXPORTABLE_PARAMETERS,
304     OSSL_PARAM_END
305 };
ml_dsa_gettable_params(void * provctx)306 static const OSSL_PARAM *ml_dsa_gettable_params(void *provctx)
307 {
308     return ml_dsa_params;
309 }
310 
ml_dsa_get_params(void * keydata,OSSL_PARAM params[])311 static int ml_dsa_get_params(void *keydata, OSSL_PARAM params[])
312 {
313     ML_DSA_KEY *key = keydata;
314     OSSL_PARAM *p;
315     const uint8_t *pub, *priv, *seed;
316 
317     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
318             && !OSSL_PARAM_set_int(p, 8 * ossl_ml_dsa_key_get_pub_len(key)))
319         return 0;
320     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
321             && !OSSL_PARAM_set_int(p, ossl_ml_dsa_key_get_collision_strength_bits(key)))
322         return 0;
323     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
324             && !OSSL_PARAM_set_int(p, ossl_ml_dsa_key_get_sig_len(key)))
325         return 0;
326 
327     pub = ossl_ml_dsa_key_get_pub(key);
328     priv = ossl_ml_dsa_key_get_priv(key);
329     seed = ossl_ml_dsa_key_get_seed(key);
330 
331     if (seed != NULL
332         && (p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ML_DSA_SEED)) != NULL
333         && !OSSL_PARAM_set_octet_string(p, seed, ML_DSA_SEED_BYTES))
334         return 0;
335     if (priv != NULL
336         && (p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY)) != NULL
337         && !OSSL_PARAM_set_octet_string(p, priv,
338                                         ossl_ml_dsa_key_get_priv_len(key)))
339         return 0;
340     if (pub != NULL
341         && (p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PUB_KEY)) != NULL
342         && !OSSL_PARAM_set_octet_string(p, pub,
343                                         ossl_ml_dsa_key_get_pub_len(key)))
344         return 0;
345     /*
346      * This allows apps to use an empty digest, so that the old API
347      * for digest signing can be used.
348      */
349     p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MANDATORY_DIGEST);
350     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ""))
351         return 0;
352 
353     return 1;
354 }
355 
ml_dsa_export(void * keydata,int selection,OSSL_CALLBACK * param_cb,void * cbarg)356 static int ml_dsa_export(void *keydata, int selection,
357                          OSSL_CALLBACK *param_cb, void *cbarg)
358 {
359     ML_DSA_KEY *key = keydata;
360     OSSL_PARAM params[4];
361     const uint8_t *buf;
362     int include_private, pnum = 0;
363 
364     if (!ossl_prov_is_running() || key == NULL)
365         return 0;
366 
367     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
368         return 0;
369 
370     include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0);
371 
372     /*
373      * Note that if the seed is present, both the seed and the private key are
374      * exported.  The recipient will have a choice.
375      */
376     if (include_private) {
377         if ((buf = ossl_ml_dsa_key_get_seed(key)) != NULL) {
378             params[pnum++] = OSSL_PARAM_construct_octet_string
379                 (OSSL_PKEY_PARAM_ML_DSA_SEED, (void *)buf, ML_DSA_SEED_BYTES);
380         }
381         if ((buf = ossl_ml_dsa_key_get_priv(key)) != NULL) {
382             params[pnum++] = OSSL_PARAM_construct_octet_string
383                 (OSSL_PKEY_PARAM_PRIV_KEY, (void *)buf,
384                  ossl_ml_dsa_key_get_priv_len(key));
385         }
386     }
387     if (((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
388         && ((buf = ossl_ml_dsa_key_get_pub(key)) != NULL)) {
389         params[pnum++] = OSSL_PARAM_construct_octet_string
390             (OSSL_PKEY_PARAM_PUB_KEY, (void *)buf,
391              ossl_ml_dsa_key_get_pub_len(key));
392     }
393     if (pnum == 0)
394         return 0;
395     params[pnum] = OSSL_PARAM_construct_end();
396     return param_cb(params, cbarg);
397 }
398 
399 #ifndef FIPS_MODULE
ml_dsa_load(const void * reference,size_t reference_sz)400 static void *ml_dsa_load(const void *reference, size_t reference_sz)
401 {
402     ML_DSA_KEY *key = NULL;
403     const ML_DSA_PARAMS *key_params;
404     const uint8_t *sk, *seed;
405 
406     if (ossl_prov_is_running() && reference_sz == sizeof(key)) {
407         /* The contents of the reference is the address to our object */
408         key = *(ML_DSA_KEY **)reference;
409         /* We grabbed, so we detach it */
410         *(ML_DSA_KEY **)reference = NULL;
411         /* All done, if the pubkey is present. */
412         if (key == NULL || ossl_ml_dsa_key_get_pub(key) != NULL)
413             return key;
414         /* Handle private prekey inputs. */
415         sk = ossl_ml_dsa_key_get_priv(key);
416         seed = ossl_ml_dsa_key_get_seed(key);
417         if (seed != NULL
418             && (sk == NULL || (ossl_ml_dsa_key_get_prov_flags(key)
419                                & ML_DSA_KEY_PREFER_SEED))) {
420             if (ossl_ml_dsa_generate_key(key))
421                 return key;
422         } else if (sk != NULL) {
423             if (ossl_ml_dsa_sk_decode(key, sk,
424                                       ossl_ml_dsa_key_get_priv_len(key)))
425                 return key;
426             key_params = ossl_ml_dsa_key_params(key);
427             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
428                            "error parsing %s private key",
429                            key_params->alg);
430         } else {
431             return key;
432         }
433     }
434 
435     ossl_ml_dsa_key_free(key);
436     return NULL;
437 }
438 #endif
439 
ml_dsa_gen_init(void * provctx,int selection,const OSSL_PARAM params[])440 static void *ml_dsa_gen_init(void *provctx, int selection,
441                              const OSSL_PARAM params[])
442 {
443     struct ml_dsa_gen_ctx *gctx = NULL;
444 
445     if (!ossl_prov_is_running())
446         return NULL;
447 
448     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
449         gctx->provctx = provctx;
450         if (!ml_dsa_gen_set_params(gctx, params)) {
451             OPENSSL_free(gctx);
452             gctx = NULL;
453         }
454     }
455     return gctx;
456 }
457 
ml_dsa_gen(void * genctx,int evp_type)458 static void *ml_dsa_gen(void *genctx, int evp_type)
459 {
460     struct ml_dsa_gen_ctx *gctx = genctx;
461     ML_DSA_KEY *key = NULL;
462 
463     if (!ossl_prov_is_running())
464         return NULL;
465     key = ossl_prov_ml_dsa_new(gctx->provctx, gctx->propq, evp_type);
466     if (key == NULL)
467         return NULL;
468     if (gctx->entropy_len != 0
469         && !ossl_ml_dsa_set_prekey(key, 0, 0,
470                                    gctx->entropy, gctx->entropy_len, NULL, 0))
471         goto err;
472     if (!ossl_ml_dsa_generate_key(key)) {
473         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
474         goto err;
475     }
476 #ifdef FIPS_MODULE
477     if (!ml_dsa_pairwise_test(key)) {
478         ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
479         goto err;
480     }
481 #endif
482     return key;
483  err:
484     ossl_ml_dsa_key_free(key);
485     return NULL;
486 }
487 
ml_dsa_gen_set_params(void * genctx,const OSSL_PARAM params[])488 static int ml_dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
489 {
490     struct ml_dsa_gen_ctx *gctx = genctx;
491     const OSSL_PARAM *p;
492 
493     if (gctx == NULL)
494         return 0;
495 
496     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ML_DSA_SEED);
497     if (p != NULL) {
498         void *vp = gctx->entropy;
499         size_t len = sizeof(gctx->entropy);
500 
501         if (!OSSL_PARAM_get_octet_string(p, &vp, len, &(gctx->entropy_len))) {
502             gctx->entropy_len = 0;
503             return 0;
504         }
505     }
506 
507     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
508     if (p != NULL) {
509         OPENSSL_free(gctx->propq);
510         gctx->propq = NULL;
511         if (!OSSL_PARAM_get_utf8_string(p, &gctx->propq, 0))
512             return 0;
513     }
514     return 1;
515 }
516 
ml_dsa_gen_settable_params(ossl_unused void * genctx,ossl_unused void * provctx)517 static const OSSL_PARAM *ml_dsa_gen_settable_params(ossl_unused void *genctx,
518                                                     ossl_unused void *provctx)
519 {
520     static OSSL_PARAM settable[] = {
521         OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
522         OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0),
523         OSSL_PARAM_END
524     };
525     return settable;
526 }
527 
ml_dsa_gen_cleanup(void * genctx)528 static void ml_dsa_gen_cleanup(void *genctx)
529 {
530     struct ml_dsa_gen_ctx *gctx = genctx;
531 
532     if (gctx == NULL)
533         return;
534 
535     OPENSSL_cleanse(gctx->entropy, gctx->entropy_len);
536     OPENSSL_free(gctx->propq);
537     OPENSSL_free(gctx);
538 }
539 
540 #ifndef FIPS_MODULE
541 # define DISPATCH_LOAD_FN \
542         { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_dsa_load },
543 #else
544 # define DISPATCH_LOAD_FN   /* Non-FIPS only */
545 #endif
546 
547 #define MAKE_KEYMGMT_FUNCTIONS(alg)                                            \
548     static OSSL_FUNC_keymgmt_new_fn ml_dsa_##alg##_new_key;                    \
549     static OSSL_FUNC_keymgmt_gen_fn ml_dsa_##alg##_gen;                        \
550     static void *ml_dsa_##alg##_new_key(void *provctx)                         \
551     {                                                                          \
552         return ossl_prov_ml_dsa_new(provctx, NULL, EVP_PKEY_ML_DSA_##alg);     \
553     }                                                                          \
554     static void *ml_dsa_##alg##_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)\
555     {                                                                          \
556         return ml_dsa_gen(genctx, EVP_PKEY_ML_DSA_##alg);                      \
557     }                                                                          \
558     const OSSL_DISPATCH ossl_ml_dsa_##alg##_keymgmt_functions[] = {            \
559         { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ml_dsa_##alg##_new_key },     \
560         { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ml_dsa_free_key },           \
561         { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ml_dsa_has },                 \
562         { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ml_dsa_match },             \
563         { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ml_dsa_import },           \
564         { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ml_dsa_imexport_types },\
565         { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ml_dsa_export },           \
566         { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ml_dsa_imexport_types },\
567         DISPATCH_LOAD_FN                                                       \
568         { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ml_dsa_get_params },  \
569         { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ml_dsa_gettable_params },\
570         { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ml_dsa_validate },       \
571         { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ml_dsa_gen_init },       \
572         { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ml_dsa_##alg##_gen },         \
573         { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ml_dsa_gen_cleanup }, \
574         { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,                                    \
575           (void (*)(void))ml_dsa_gen_set_params },                             \
576         { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,                               \
577           (void (*)(void))ml_dsa_gen_settable_params },                        \
578         { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ml_dsa_dup_key },             \
579         OSSL_DISPATCH_END                                                      \
580     }
581 
582 MAKE_KEYMGMT_FUNCTIONS(44);
583 MAKE_KEYMGMT_FUNCTIONS(65);
584 MAKE_KEYMGMT_FUNCTIONS(87);
585