Coverage Report

Created: 2026-08-12 06:14

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