Coverage Report

Created: 2025-07-23 06:08

/src/openssl/providers/implementations/keymgmt/ml_kem_kmgmt.c
Line
Count
Source (jump to first uncovered line)
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
11
#include <openssl/core_dispatch.h>
12
#include <openssl/core_names.h>
13
#include <openssl/params.h>
14
#include <openssl/err.h>
15
#include <openssl/proverr.h>
16
#include <openssl/provider.h>
17
#include <openssl/rand.h>
18
#include <openssl/self_test.h>
19
#include <openssl/param_build.h>
20
#include "crypto/ml_kem.h"
21
#include "internal/fips.h"
22
#include "internal/param_build_set.h"
23
#include "prov/implementations.h"
24
#include "prov/providercommon.h"
25
#include "prov/provider_ctx.h"
26
#include "prov/securitycheck.h"
27
#include "prov/ml_kem.h"
28
29
static OSSL_FUNC_keymgmt_new_fn ml_kem_512_new;
30
static OSSL_FUNC_keymgmt_new_fn ml_kem_768_new;
31
static OSSL_FUNC_keymgmt_new_fn ml_kem_1024_new;
32
static OSSL_FUNC_keymgmt_gen_fn ml_kem_gen;
33
static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_512_gen_init;
34
static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_768_gen_init;
35
static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_1024_gen_init;
36
static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_kem_gen_cleanup;
37
static OSSL_FUNC_keymgmt_gen_set_params_fn ml_kem_gen_set_params;
38
static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_kem_gen_settable_params;
39
#ifndef FIPS_MODULE
40
static OSSL_FUNC_keymgmt_load_fn ml_kem_load;
41
#endif
42
static OSSL_FUNC_keymgmt_get_params_fn ml_kem_get_params;
43
static OSSL_FUNC_keymgmt_gettable_params_fn ml_kem_gettable_params;
44
static OSSL_FUNC_keymgmt_set_params_fn ml_kem_set_params;
45
static OSSL_FUNC_keymgmt_settable_params_fn ml_kem_settable_params;
46
static OSSL_FUNC_keymgmt_has_fn ml_kem_has;
47
static OSSL_FUNC_keymgmt_match_fn ml_kem_match;
48
static OSSL_FUNC_keymgmt_validate_fn ml_kem_validate;
49
static OSSL_FUNC_keymgmt_import_fn ml_kem_import;
50
static OSSL_FUNC_keymgmt_export_fn ml_kem_export;
51
static OSSL_FUNC_keymgmt_import_types_fn ml_kem_imexport_types;
52
static OSSL_FUNC_keymgmt_export_types_fn ml_kem_imexport_types;
53
static OSSL_FUNC_keymgmt_dup_fn ml_kem_dup;
54
55
static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
56
    | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
57
58
typedef struct ml_kem_gen_ctx_st {
59
    PROV_CTX *provctx;
60
    char *propq;
61
    int selection;
62
    int evp_type;
63
    uint8_t seedbuf[ML_KEM_SEED_BYTES];
64
    uint8_t *seed;
65
} PROV_ML_KEM_GEN_CTX;
66
67
static int ml_kem_pairwise_test(const ML_KEM_KEY *key, int key_flags)
68
0
{
69
#ifdef FIPS_MODULE
70
    OSSL_SELF_TEST *st = NULL;
71
    OSSL_CALLBACK *cb = NULL;
72
    void *cbarg = NULL;
73
#endif
74
0
    unsigned char entropy[ML_KEM_RANDOM_BYTES];
75
0
    unsigned char secret[ML_KEM_SHARED_SECRET_BYTES];
76
0
    unsigned char out[ML_KEM_SHARED_SECRET_BYTES];
77
0
    unsigned char *ctext = NULL;
78
0
    const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key);
79
0
    int operation_result = 0;
80
0
    int ret = 0;
81
82
    /* Unless we have both a public and private key, we can't do the test */
83
0
    if (!ossl_ml_kem_have_prvkey(key)
84
0
        || !ossl_ml_kem_have_pubkey(key)
85
0
        || (key_flags & ML_KEM_KEY_PCT_TYPE) == 0)
86
0
        return 1;
87
#ifdef FIPS_MODULE
88
    /* During self test, it is a waste to do this test */
89
    if (ossl_fips_self_testing())
90
        return 1;
91
92
    /*
93
     * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st`
94
     * is NULL.
95
     */
96
    OSSL_SELF_TEST_get_callback(key->libctx, &cb, &cbarg);
97
98
    st = OSSL_SELF_TEST_new(cb, cbarg);
99
    if (st == NULL)
100
        return 0;
101
102
    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
103
                           OSSL_SELF_TEST_DESC_PCT_ML_KEM);
104
#endif  /* FIPS_MODULE */
105
106
0
    ctext = OPENSSL_malloc(v->ctext_bytes);
107
0
    if (ctext == NULL)
108
0
        goto err;
109
110
0
    memset(out, 0, sizeof(out));
111
112
    /*
113
     * The pairwise test is skipped unless either RANDOM or FIXED entropy PCTs
114
     * are enabled.
115
     */
116
0
    if (key_flags & ML_KEM_KEY_RANDOM_PCT) {
117
0
        operation_result = ossl_ml_kem_encap_rand(ctext, v->ctext_bytes,
118
0
                                                  secret, sizeof(secret), key);
119
0
    } else {
120
0
        memset(entropy, 0125, sizeof(entropy));
121
0
        operation_result = ossl_ml_kem_encap_seed(ctext, v->ctext_bytes,
122
0
                                                  secret, sizeof(secret),
123
0
                                                  entropy, sizeof(entropy),
124
0
                                                  key);
125
0
    }
126
0
    if (operation_result != 1)
127
0
        goto err;
128
129
#ifdef FIPS_MODULE
130
    OSSL_SELF_TEST_oncorrupt_byte(st, ctext);
131
#endif
132
133
0
    operation_result = ossl_ml_kem_decap(out, sizeof(out), ctext, v->ctext_bytes,
134
0
                                         key);
135
0
    if (operation_result != 1 || memcmp(out, secret, sizeof(out)) != 0)
136
0
        goto err;
137
138
0
    ret = 1;
139
0
err:
140
#ifdef FIPS_MODULE
141
    OSSL_SELF_TEST_onend(st, ret);
142
    OSSL_SELF_TEST_free(st);
143
#else
144
0
    if (ret == 0) {
145
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
146
0
                       "public part of %s private key fails to match private",
147
0
                       v->algorithm_name);
148
0
    }
149
0
#endif
150
0
    OPENSSL_free(ctext);
151
0
    return ret;
152
0
}
153
154
ML_KEM_KEY *ossl_prov_ml_kem_new(PROV_CTX *ctx, const char *propq, int evp_type)
155
0
{
156
0
    ML_KEM_KEY *key;
157
158
0
    if (!ossl_prov_is_running())
159
0
        return NULL;
160
    /*
161
     * When decoding, if the key ends up "loaded" into the same provider, these
162
     * are the correct config settings, otherwise, new values will be assigned
163
     * on import into a different provider.  The "load" API does not pass along
164
     * the provider context.
165
     */
166
0
    if ((key = ossl_ml_kem_key_new(PROV_LIBCTX_OF(ctx), propq, evp_type)) != NULL) {
167
0
        const char *pct_type = ossl_prov_ctx_get_param(
168
0
            ctx, OSSL_PKEY_PARAM_ML_KEM_IMPORT_PCT_TYPE, "random");
169
170
0
        if (ossl_prov_ctx_get_bool_param(
171
0
            ctx, OSSL_PKEY_PARAM_ML_KEM_RETAIN_SEED, 1))
172
0
            key->prov_flags |= ML_KEM_KEY_RETAIN_SEED;
173
0
        else
174
0
            key->prov_flags &= ~ML_KEM_KEY_RETAIN_SEED;
175
0
        if (ossl_prov_ctx_get_bool_param(
176
0
            ctx, OSSL_PKEY_PARAM_ML_KEM_PREFER_SEED, 1))
177
0
            key->prov_flags |= ML_KEM_KEY_PREFER_SEED;
178
0
        else
179
0
            key->prov_flags &= ~ML_KEM_KEY_PREFER_SEED;
180
0
        if (OPENSSL_strcasecmp(pct_type, "random") == 0)
181
0
            key->prov_flags |= ML_KEM_KEY_RANDOM_PCT;
182
0
        else if (OPENSSL_strcasecmp(pct_type, "fixed") == 0)
183
0
            key->prov_flags |= ML_KEM_KEY_FIXED_PCT;
184
0
        else
185
0
            key->prov_flags &= ~ML_KEM_KEY_PCT_TYPE;
186
0
    }
187
0
    return key;
188
0
}
189
190
static int ml_kem_has(const void *vkey, int selection)
191
0
{
192
0
    const ML_KEM_KEY *key = vkey;
193
194
    /* A NULL key MUST fail to have anything */
195
0
    if (!ossl_prov_is_running() || key == NULL)
196
0
        return 0;
197
198
0
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
199
0
    case 0:
200
0
        return 1;
201
0
    case OSSL_KEYMGMT_SELECT_PUBLIC_KEY:
202
0
        return ossl_ml_kem_have_pubkey(key);
203
0
    default:
204
0
        return ossl_ml_kem_have_prvkey(key);
205
0
    }
206
0
}
207
208
static int ml_kem_match(const void *vkey1, const void *vkey2, int selection)
209
0
{
210
0
    const ML_KEM_KEY *key1 = vkey1;
211
0
    const ML_KEM_KEY *key2 = vkey2;
212
213
0
    if (!ossl_prov_is_running())
214
0
        return 0;
215
216
    /* All we have that can be compared is key material */
217
0
    if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR))
218
0
        return 1;
219
220
0
    return ossl_ml_kem_pubkey_cmp(key1, key2);
221
0
}
222
223
static int ml_kem_validate(const void *vkey, int selection, int check_type)
224
0
{
225
0
    const ML_KEM_KEY *key = vkey;
226
227
0
    if (!ml_kem_has(key, selection))
228
0
        return 0;
229
230
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
231
0
        return ml_kem_pairwise_test(key, ML_KEM_KEY_RANDOM_PCT);
232
0
    return 1;
233
0
}
234
235
static int ml_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb,
236
                         void *cbarg)
237
0
{
238
0
    ML_KEM_KEY *key = vkey;
239
0
    OSSL_PARAM_BLD *tmpl = NULL;
240
0
    OSSL_PARAM *params = NULL;
241
0
    const ML_KEM_VINFO *v;
242
0
    uint8_t *pubenc = NULL, *prvenc = NULL, *seedenc = NULL;
243
0
    size_t prvlen = 0, seedlen = 0;
244
0
    int ret = 0;
245
246
0
    if (!ossl_prov_is_running() || key == NULL)
247
0
        return 0;
248
249
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
250
0
        return 0;
251
252
0
    v = ossl_ml_kem_key_vinfo(key);
253
0
    if (!ossl_ml_kem_have_pubkey(key)) {
254
        /* Fail when no key material can be returned */
255
0
        if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0
256
0
            || !ossl_ml_kem_decoded_key(key)) {
257
0
            ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
258
0
            return 0;
259
0
        }
260
0
    } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
261
0
        pubenc = OPENSSL_malloc(v->pubkey_bytes);
262
0
        if (pubenc == NULL
263
0
            || !ossl_ml_kem_encode_public_key(pubenc, v->pubkey_bytes, key))
264
0
            goto err;
265
0
    }
266
267
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
268
        /*
269
         * The seed and/or private key material are allocated on the secure
270
         * heap if configured, ossl_param_build_set_octet_string(), will then
271
         * also use the secure heap.
272
         */
273
0
        if (ossl_ml_kem_have_seed(key)) {
274
0
            seedlen = ML_KEM_SEED_BYTES;
275
0
            if ((seedenc = OPENSSL_secure_zalloc(seedlen)) == NULL
276
0
                || !ossl_ml_kem_encode_seed(seedenc, seedlen, key))
277
0
                goto err;
278
0
        }
279
0
        if (ossl_ml_kem_have_prvkey(key)) {
280
0
            prvlen = v->prvkey_bytes;
281
0
            if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL
282
0
                || !ossl_ml_kem_encode_private_key(prvenc, prvlen, key))
283
0
                goto err;
284
0
        } else if (ossl_ml_kem_have_dkenc(key)) {
285
0
            prvlen = v->prvkey_bytes;
286
0
            if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL)
287
0
                goto err;
288
0
            memcpy(prvenc, key->encoded_dk, prvlen);
289
0
        }
290
0
    }
291
292
0
    tmpl = OSSL_PARAM_BLD_new();
293
0
    if (tmpl == NULL)
294
0
        goto err;
295
296
    /* The (d, z) seed, when available and private keys are requested. */
297
0
    if (seedenc != NULL
298
0
        && !ossl_param_build_set_octet_string(
299
0
                tmpl, params, OSSL_PKEY_PARAM_ML_KEM_SEED, seedenc, seedlen))
300
0
        goto err;
301
302
    /* The private key in the FIPS 203 |dk| format, when requested. */
303
0
    if (prvenc != NULL
304
0
        && !ossl_param_build_set_octet_string(
305
0
                tmpl, params, OSSL_PKEY_PARAM_PRIV_KEY, prvenc, prvlen))
306
0
            goto err;
307
308
    /* The public key on request; it is always available when either is */
309
0
    if (pubenc != NULL
310
0
        && !ossl_param_build_set_octet_string(
311
0
               tmpl, params, OSSL_PKEY_PARAM_PUB_KEY, pubenc, v->pubkey_bytes))
312
0
            goto err;
313
314
0
    params = OSSL_PARAM_BLD_to_param(tmpl);
315
0
    if (params == NULL)
316
0
        goto err;
317
318
0
    ret = param_cb(params, cbarg);
319
0
    OSSL_PARAM_free(params);
320
321
0
err:
322
0
    OSSL_PARAM_BLD_free(tmpl);
323
0
    OPENSSL_secure_clear_free(seedenc, seedlen);
324
0
    OPENSSL_secure_clear_free(prvenc, prvlen);
325
0
    OPENSSL_free(pubenc);
326
0
    return ret;
327
0
}
328
329
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
330
#ifndef ml_kem_key_type_params_list
331
static const OSSL_PARAM ml_kem_key_type_params_list[] = {
332
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0),
333
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
334
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
335
    OSSL_PARAM_END
336
};
337
#endif
338
339
#ifndef ml_kem_key_type_params_st
340
struct ml_kem_key_type_params_st {
341
    OSSL_PARAM *privkey;
342
    OSSL_PARAM *pubkey;
343
    OSSL_PARAM *seed;
344
};
345
#endif
346
347
#ifndef ml_kem_key_type_params_decoder
348
static struct ml_kem_key_type_params_st
349
0
ml_kem_key_type_params_decoder(const OSSL_PARAM params[]) {
350
0
    struct ml_kem_key_type_params_st r;
351
0
    const OSSL_PARAM *p;
352
0
    const char *s;
353
354
0
    memset(&r, 0, sizeof(r));
355
0
    for (p = params; (s = p->key) != NULL; p++)
356
0
        switch(s[0]) {
357
0
        default:
358
0
            break;
359
0
        case 'p':
360
0
            switch(s[1]) {
361
0
            default:
362
0
                break;
363
0
            case 'r':
364
0
                if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0))
365
0
                    r.privkey = (OSSL_PARAM *)p;
366
0
                break;
367
0
            case 'u':
368
0
                if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0))
369
0
                    r.pubkey = (OSSL_PARAM *)p;
370
0
            }
371
0
            break;
372
0
        case 's':
373
0
            if (ossl_likely(r.seed == NULL && strcmp("eed", s + 1) == 0))
374
0
                r.seed = (OSSL_PARAM *)p;
375
0
        }
376
0
    return r;
377
0
}
378
#endif
379
/* End of machine generated */
380
381
static const OSSL_PARAM *ml_kem_imexport_types(int selection)
382
0
{
383
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
384
0
        return ml_kem_key_type_params_list;
385
0
    return NULL;
386
0
}
387
388
static int check_seed(const uint8_t *seed, const uint8_t *prvenc,
389
                      ML_KEM_KEY *key)
390
0
{
391
0
    size_t zlen = ML_KEM_RANDOM_BYTES;
392
393
0
    if (memcmp(seed + ML_KEM_SEED_BYTES - zlen,
394
0
               prvenc + key->vinfo->prvkey_bytes - zlen, zlen) == 0)
395
0
        return 1;
396
0
    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
397
0
                   "private %s key implicit rejection secret does"
398
0
                   " not match seed", key->vinfo->algorithm_name);
399
0
    return 0;
400
0
}
401
402
static int check_prvenc(const uint8_t *prvenc, ML_KEM_KEY *key)
403
0
{
404
0
    size_t len = key->vinfo->prvkey_bytes;
405
0
    uint8_t *buf = OPENSSL_malloc(len);
406
0
    int ret = 0;
407
408
0
    if (buf != NULL
409
0
        && ossl_ml_kem_encode_private_key(buf, len, key))
410
0
        ret = memcmp(buf, prvenc, len) == 0;
411
0
    OPENSSL_clear_free(buf, len);
412
0
    if (ret)
413
0
        return 1;
414
415
0
    if (buf != NULL)
416
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
417
0
                       "explicit %s private key does not match seed",
418
0
                       key->vinfo->algorithm_name);
419
0
    ossl_ml_kem_key_reset(key);
420
0
    return 0;
421
0
}
422
423
static int ml_kem_key_fromdata(ML_KEM_KEY *key,
424
                               const OSSL_PARAM params[],
425
                               int include_private)
426
0
{
427
0
    const void *pubenc = NULL, *prvenc = NULL, *seedenc = NULL;
428
0
    size_t publen = 0, prvlen = 0, seedlen = 0, puboff;
429
0
    const ML_KEM_VINFO *v;
430
0
    struct ml_kem_key_type_params_st p;
431
432
    /* Invalid attempt to mutate a key, what is the right error to report? */
433
0
    if (key == NULL || ossl_ml_kem_have_pubkey(key))
434
0
        return 0;
435
0
    v = ossl_ml_kem_key_vinfo(key);
436
0
    p = ml_kem_key_type_params_decoder(params);
437
438
    /*
439
     * When a private key is provided, without a seed, any public key also
440
     * provided will be ignored (apart from length), just as with the seed.
441
     */
442
0
    if (p.seed != NULL && include_private) {
443
        /*
444
         * When a seed is provided, the private and public keys may be ignored,
445
         * after validating just their lengths.  Comparing encodings or hashes
446
         * when applicable is possible, but not currently implemented.
447
         */
448
0
        if (OSSL_PARAM_get_octet_string_ptr(p.seed, &seedenc, &seedlen) != 1)
449
0
            return 0;
450
0
        if (seedlen != 0 && seedlen != ML_KEM_SEED_BYTES) {
451
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
452
0
            return 0;
453
0
        }
454
0
    }
455
456
0
    if (p.privkey != NULL && include_private) {
457
0
        if (OSSL_PARAM_get_octet_string_ptr(p.privkey, &prvenc, &prvlen) != 1)
458
0
            return 0;
459
0
        if (prvlen != 0 && prvlen != v->prvkey_bytes) {
460
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
461
0
            return 0;
462
0
        }
463
0
    }
464
465
    /* Used only when no seed or private key is provided. */
466
0
    if (p.pubkey != NULL) {
467
0
        if (OSSL_PARAM_get_octet_string_ptr(p.pubkey, &pubenc, &publen) != 1)
468
0
            return 0;
469
0
        if (publen != 0 && publen != v->pubkey_bytes) {
470
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
471
0
            return 0;
472
0
        }
473
0
    }
474
475
    /* The caller MUST specify at least one of seed, private or public keys. */
476
0
    if (seedlen == 0 && publen == 0 && prvlen == 0) {
477
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
478
0
        return 0;
479
0
    }
480
481
    /* Check any explicit public key against embedded value in private key */
482
0
    if (publen > 0 && prvlen > 0) {
483
        /* point to the ek offset in dk = DKpke||ek||H(ek)||z */
484
0
        puboff = prvlen - ML_KEM_RANDOM_BYTES - ML_KEM_PKHASH_BYTES - publen;
485
0
        if (memcmp(pubenc, (unsigned char *)prvenc + puboff, publen) != 0) {
486
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
487
0
                           "explicit %s public key does not match private",
488
0
                           v->algorithm_name);
489
0
            return 0;
490
0
        }
491
0
    }
492
493
0
    if (seedlen != 0
494
0
        && (prvlen == 0 || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) {
495
0
        if (prvlen != 0 && !check_seed(seedenc, prvenc, key))
496
0
            return 0;
497
0
        if (!ossl_ml_kem_set_seed(seedenc, seedlen, key)
498
0
            || !ossl_ml_kem_genkey(NULL, 0, key))
499
0
            return 0;
500
0
        return prvlen == 0 || check_prvenc(prvenc, key);
501
0
    } else if (prvlen != 0) {
502
0
        return ossl_ml_kem_parse_private_key(prvenc, prvlen, key);
503
0
    }
504
0
    return ossl_ml_kem_parse_public_key(pubenc, publen, key);
505
0
}
506
507
static int ml_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
508
0
{
509
0
    ML_KEM_KEY *key = vkey;
510
0
    int include_private;
511
0
    int res;
512
513
0
    if (!ossl_prov_is_running() || key == NULL)
514
0
        return 0;
515
516
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
517
0
        return 0;
518
519
0
    include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
520
0
    res = ml_kem_key_fromdata(key, params, include_private);
521
0
    if (res > 0 && include_private
522
0
        && !ml_kem_pairwise_test(key, key->prov_flags)) {
523
#ifdef FIPS_MODULE
524
        ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
525
#endif
526
0
        ossl_ml_kem_key_reset(key);
527
0
        res = 0;
528
0
    }
529
0
    return res;
530
0
}
531
532
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
533
#ifndef ml_kem_get_params_list
534
static const OSSL_PARAM ml_kem_get_params_list[] = {
535
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
536
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
537
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
538
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL),
539
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0),
540
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
541
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
542
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
543
    OSSL_PARAM_END
544
};
545
#endif
546
547
#ifndef ml_kem_get_params_st
548
struct ml_kem_get_params_st {
549
    OSSL_PARAM *bits;
550
    OSSL_PARAM *encpubkey;
551
    OSSL_PARAM *maxsize;
552
    OSSL_PARAM *privkey;
553
    OSSL_PARAM *pubkey;
554
    OSSL_PARAM *secbits;
555
    OSSL_PARAM *seccat;
556
    OSSL_PARAM *seed;
557
};
558
#endif
559
560
#ifndef ml_kem_get_params_decoder
561
static struct ml_kem_get_params_st
562
0
ml_kem_get_params_decoder(const OSSL_PARAM params[]) {
563
0
    struct ml_kem_get_params_st r;
564
0
    const OSSL_PARAM *p;
565
0
    const char *s;
566
567
0
    memset(&r, 0, sizeof(r));
568
0
    for (p = params; (s = p->key) != NULL; p++)
569
0
        switch(s[0]) {
570
0
        default:
571
0
            break;
572
0
        case 'b':
573
0
            if (ossl_likely(r.bits == NULL && strcmp("its", s + 1) == 0))
574
0
                r.bits = (OSSL_PARAM *)p;
575
0
            break;
576
0
        case 'e':
577
0
            if (ossl_likely(r.encpubkey == NULL && strcmp("ncoded-pub-key", s + 1) == 0))
578
0
                r.encpubkey = (OSSL_PARAM *)p;
579
0
            break;
580
0
        case 'm':
581
0
            if (ossl_likely(r.maxsize == NULL && strcmp("ax-size", s + 1) == 0))
582
0
                r.maxsize = (OSSL_PARAM *)p;
583
0
            break;
584
0
        case 'p':
585
0
            switch(s[1]) {
586
0
            default:
587
0
                break;
588
0
            case 'r':
589
0
                if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0))
590
0
                    r.privkey = (OSSL_PARAM *)p;
591
0
                break;
592
0
            case 'u':
593
0
                if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0))
594
0
                    r.pubkey = (OSSL_PARAM *)p;
595
0
            }
596
0
            break;
597
0
        case 's':
598
0
            switch(s[1]) {
599
0
            default:
600
0
                break;
601
0
            case 'e':
602
0
                switch(s[2]) {
603
0
                default:
604
0
                    break;
605
0
                case 'c':
606
0
                    switch(s[3]) {
607
0
                    default:
608
0
                        break;
609
0
                    case 'u':
610
0
                        switch(s[4]) {
611
0
                        default:
612
0
                            break;
613
0
                        case 'r':
614
0
                            switch(s[5]) {
615
0
                            default:
616
0
                                break;
617
0
                            case 'i':
618
0
                                switch(s[6]) {
619
0
                                default:
620
0
                                    break;
621
0
                                case 't':
622
0
                                    switch(s[7]) {
623
0
                                    default:
624
0
                                        break;
625
0
                                    case 'y':
626
0
                                        switch(s[8]) {
627
0
                                        default:
628
0
                                            break;
629
0
                                        case '-':
630
0
                                            switch(s[9]) {
631
0
                                            default:
632
0
                                                break;
633
0
                                            case 'b':
634
0
                                                if (ossl_likely(r.secbits == NULL && strcmp("its", s + 10) == 0))
635
0
                                                    r.secbits = (OSSL_PARAM *)p;
636
0
                                                break;
637
0
                                            case 'c':
638
0
                                                if (ossl_likely(r.seccat == NULL && strcmp("ategory", s + 10) == 0))
639
0
                                                    r.seccat = (OSSL_PARAM *)p;
640
0
                                            }
641
0
                                        }
642
0
                                    }
643
0
                                }
644
0
                            }
645
0
                        }
646
0
                    }
647
0
                    break;
648
0
                case 'e':
649
0
                    if (ossl_likely(r.seed == NULL && strcmp("d", s + 3) == 0))
650
0
                        r.seed = (OSSL_PARAM *)p;
651
0
                }
652
0
            }
653
0
        }
654
0
    return r;
655
0
}
656
#endif
657
/* End of machine generated */
658
659
static const OSSL_PARAM *ml_kem_gettable_params(void *provctx)
660
0
{
661
0
    return ml_kem_get_params_list;
662
0
}
663
664
#ifndef FIPS_MODULE
665
void *ml_kem_load(const void *reference, size_t reference_sz)
666
0
{
667
0
    ML_KEM_KEY *key = NULL;
668
0
    uint8_t *encoded_dk = NULL;
669
0
    uint8_t seed[ML_KEM_SEED_BYTES];
670
671
0
    if (ossl_prov_is_running() && reference_sz == sizeof(key)) {
672
        /* The contents of the reference is the address to our object */
673
0
        key = *(ML_KEM_KEY **)reference;
674
0
        encoded_dk = key->encoded_dk;
675
0
        key->encoded_dk = NULL;
676
        /* We grabbed, so we detach it */
677
0
        *(ML_KEM_KEY **)reference = NULL;
678
0
        if (encoded_dk != NULL
679
0
            && ossl_ml_kem_encode_seed(seed, sizeof(seed), key)
680
0
            && !check_seed(seed, encoded_dk, key))
681
0
            goto err;
682
        /* Generate the key now, if it holds only a stashed seed. */
683
0
        if (ossl_ml_kem_have_seed(key)
684
0
            && (encoded_dk == NULL
685
0
                || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) {
686
0
            if (!ossl_ml_kem_genkey(NULL, 0, key)
687
0
                || (encoded_dk != NULL && !check_prvenc(encoded_dk, key)))
688
0
                goto err;
689
0
        } else if (encoded_dk != NULL) {
690
0
            if (!ossl_ml_kem_parse_private_key(encoded_dk,
691
0
                                               key->vinfo->prvkey_bytes, key)) {
692
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
693
0
                               "error parsing %s private key",
694
0
                               key->vinfo->algorithm_name);
695
0
                goto err;
696
0
            }
697
0
            if (!ml_kem_pairwise_test(key, key->prov_flags))
698
0
                goto err;
699
0
        }
700
0
        OPENSSL_secure_clear_free(encoded_dk, key->vinfo->prvkey_bytes);
701
0
        return key;
702
0
    }
703
704
0
 err:
705
0
    if (key != NULL && key->vinfo != NULL)
706
0
        OPENSSL_secure_clear_free(encoded_dk, key->vinfo->prvkey_bytes);
707
0
    ossl_ml_kem_key_free(key);
708
0
    return NULL;
709
0
}
710
#endif
711
712
static int ml_kem_get_key_param(const ML_KEM_KEY *key, OSSL_PARAM *p,
713
                                size_t bytes,
714
                                int (*get_f)(uint8_t *out, size_t len,
715
                                             const ML_KEM_KEY *key))
716
0
{
717
0
    if (p->data_type != OSSL_PARAM_OCTET_STRING)
718
0
            return 0;
719
0
    p->return_size = bytes;
720
0
    if (p->data != NULL)
721
0
        if (p->data_size < p->return_size
722
0
                || !(*get_f)(p->data, p->return_size, key))
723
0
            return 0;
724
0
    return 1;
725
0
}
726
727
/*
728
 * It is assumed the key is guaranteed non-NULL here, and is from this provider
729
 */
730
static int ml_kem_get_params(void *vkey, OSSL_PARAM params[])
731
0
{
732
0
    ML_KEM_KEY *key = vkey;
733
0
    const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key);
734
0
    struct ml_kem_get_params_st p = ml_kem_get_params_decoder(params);
735
736
0
    if (p.bits != NULL && !OSSL_PARAM_set_size_t(p.bits, v->bits))
737
0
        return 0;
738
739
0
    if (p.secbits != NULL && !OSSL_PARAM_set_size_t(p.secbits, v->secbits))
740
0
        return 0;
741
742
0
    if (p.maxsize != NULL && !OSSL_PARAM_set_size_t(p.maxsize, v->ctext_bytes))
743
0
        return 0;
744
745
0
    if (p.seccat != NULL && !OSSL_PARAM_set_int(p.seccat, v->security_category))
746
0
        return 0;
747
748
0
    if (p.pubkey != NULL && ossl_ml_kem_have_pubkey(key)) {
749
        /* Exported to EVP_PKEY_get_raw_public_key() */
750
0
        if (!ml_kem_get_key_param(key, p.pubkey, v->pubkey_bytes,
751
0
                                  &ossl_ml_kem_encode_public_key))
752
0
            return 0;
753
0
    }
754
755
0
    if (p.encpubkey != NULL && ossl_ml_kem_have_pubkey(key)) {
756
        /* Needed by EVP_PKEY_get1_encoded_public_key() */
757
0
        if (!ml_kem_get_key_param(key, p.encpubkey, v->pubkey_bytes,
758
0
                                  &ossl_ml_kem_encode_public_key))
759
0
            return 0;
760
0
    }
761
762
0
    if (p.privkey != NULL && ossl_ml_kem_have_prvkey(key)) {
763
        /* Exported to EVP_PKEY_get_raw_private_key() */
764
0
        if (!ml_kem_get_key_param(key, p.privkey, v->prvkey_bytes,
765
0
                                  &ossl_ml_kem_encode_private_key))
766
0
            return 0;
767
0
    }
768
769
0
    if (p.seed != NULL && ossl_ml_kem_have_seed(key)) {
770
        /* Exported for import */
771
0
        if (!ml_kem_get_key_param(key, p.seed, ML_KEM_SEED_BYTES,
772
0
                                  &ossl_ml_kem_encode_seed))
773
0
            return 0;
774
0
    }
775
0
    return 1;
776
0
}
777
778
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
779
#ifndef ml_kem_set_params_list
780
static const OSSL_PARAM ml_kem_set_params_list[] = {
781
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
782
    OSSL_PARAM_END
783
};
784
#endif
785
786
#ifndef ml_kem_set_params_st
787
struct ml_kem_set_params_st {
788
    OSSL_PARAM *pub;
789
};
790
#endif
791
792
#ifndef ml_kem_set_params_decoder
793
static struct ml_kem_set_params_st
794
0
ml_kem_set_params_decoder(const OSSL_PARAM params[]) {
795
0
    struct ml_kem_set_params_st r;
796
0
    const OSSL_PARAM *p;
797
0
    const char *s;
798
799
0
    memset(&r, 0, sizeof(r));
800
0
    for (p = params; (s = p->key) != NULL; p++)
801
0
        if (ossl_likely(r.pub == NULL && strcmp("encoded-pub-key", s + 0) == 0))
802
0
            r.pub = (OSSL_PARAM *)p;
803
0
    return r;
804
0
}
805
#endif
806
/* End of machine generated */
807
808
static const OSSL_PARAM *ml_kem_settable_params(void *provctx)
809
0
{
810
0
    return ml_kem_set_params_list;
811
0
}
812
813
static int ml_kem_set_params(void *vkey, const OSSL_PARAM params[])
814
0
{
815
0
    ML_KEM_KEY *key = vkey;
816
0
    const void *pubenc = NULL;
817
0
    size_t publen = 0;
818
0
    struct ml_kem_set_params_st p;
819
820
0
    if (ossl_param_is_empty(params))
821
0
        return 1;
822
823
0
    p = ml_kem_set_params_decoder(params);
824
825
    /* Used in TLS via EVP_PKEY_set1_encoded_public_key(). */
826
0
    if (p.pub != NULL
827
0
        && (OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen) != 1
828
0
            || publen != key->vinfo->pubkey_bytes)) {
829
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
830
0
        return 0;
831
0
    }
832
833
0
    if (publen == 0)
834
0
        return 1;
835
836
    /* Key mutation is reportedly generally not allowed */
837
0
    if (ossl_ml_kem_have_pubkey(key)) {
838
0
        ERR_raise_data(ERR_LIB_PROV,
839
0
                       PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
840
0
                       "ML-KEM keys cannot be mutated");
841
0
        return 0;
842
0
    }
843
844
0
    return ossl_ml_kem_parse_public_key(pubenc, publen, key);
845
0
}
846
847
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
848
#ifndef ml_kem_gen_set_params_list
849
static const OSSL_PARAM ml_kem_gen_set_params_list[] = {
850
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0),
851
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
852
    OSSL_PARAM_END
853
};
854
#endif
855
856
#ifndef ml_kem_gen_set_params_st
857
struct ml_kem_gen_set_params_st {
858
    OSSL_PARAM *propq;
859
    OSSL_PARAM *seed;
860
};
861
#endif
862
863
#ifndef ml_kem_gen_set_params_decoder
864
static struct ml_kem_gen_set_params_st
865
0
ml_kem_gen_set_params_decoder(const OSSL_PARAM params[]) {
866
0
    struct ml_kem_gen_set_params_st r;
867
0
    const OSSL_PARAM *p;
868
0
    const char *s;
869
870
0
    memset(&r, 0, sizeof(r));
871
0
    for (p = params; (s = p->key) != NULL; p++)
872
0
        switch(s[0]) {
873
0
        default:
874
0
            break;
875
0
        case 'p':
876
0
            if (ossl_likely(r.propq == NULL && strcmp("roperties", s + 1) == 0))
877
0
                r.propq = (OSSL_PARAM *)p;
878
0
            break;
879
0
        case 's':
880
0
            if (ossl_likely(r.seed == NULL && strcmp("eed", s + 1) == 0))
881
0
                r.seed = (OSSL_PARAM *)p;
882
0
        }
883
0
    return r;
884
0
}
885
#endif
886
/* End of machine generated */
887
888
static int ml_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
889
0
{
890
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
891
0
    struct ml_kem_gen_set_params_st p;
892
893
0
    if (gctx == NULL)
894
0
        return 0;
895
0
    if (ossl_param_is_empty(params))
896
0
        return 1;
897
898
0
    p = ml_kem_gen_set_params_decoder(params);
899
900
0
    if (p.propq != NULL) {
901
0
        if (p.propq->data_type != OSSL_PARAM_UTF8_STRING)
902
0
            return 0;
903
0
        OPENSSL_free(gctx->propq);
904
0
        if ((gctx->propq = OPENSSL_strdup(p.propq->data)) == NULL)
905
0
            return 0;
906
0
    }
907
908
0
    if (p.seed != NULL) {
909
0
        size_t len = ML_KEM_SEED_BYTES;
910
911
0
        gctx->seed = gctx->seedbuf;
912
0
        if (OSSL_PARAM_get_octet_string(p.seed, (void **)&gctx->seed, len, &len)
913
0
            && len == ML_KEM_SEED_BYTES)
914
0
            return 1;
915
916
        /* Possibly, but less likely wrong data type */
917
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
918
0
        gctx->seed = NULL;
919
0
        return 0;
920
0
    }
921
922
0
    return 1;
923
0
}
924
925
static void *ml_kem_gen_init(void *provctx, int selection,
926
                             const OSSL_PARAM params[], int evp_type)
927
0
{
928
0
    PROV_ML_KEM_GEN_CTX *gctx = NULL;
929
930
    /*
931
     * We can only generate private keys, check that the selection is
932
     * appropriate.
933
     */
934
0
    if (!ossl_prov_is_running()
935
0
        || (selection & minimal_selection) == 0
936
0
        || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
937
0
        return NULL;
938
939
0
    gctx->selection = selection;
940
0
    gctx->evp_type = evp_type;
941
0
    gctx->provctx = provctx;
942
0
    if (ml_kem_gen_set_params(gctx, params))
943
0
        return gctx;
944
945
0
    ml_kem_gen_cleanup(gctx);
946
0
    return NULL;
947
0
}
948
949
static const OSSL_PARAM *ml_kem_gen_settable_params(ossl_unused void *vgctx,
950
                                                    ossl_unused void *provctx)
951
0
{
952
0
    return ml_kem_gen_set_params_list;
953
0
}
954
955
static void *ml_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
956
0
{
957
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
958
0
    ML_KEM_KEY *key;
959
0
    uint8_t *nopub = NULL;
960
0
    uint8_t *seed;
961
0
    int genok = 0;
962
963
0
    if (gctx == NULL
964
0
        || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
965
0
            OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
966
0
        return NULL;
967
0
    seed = gctx->seed;
968
0
    key = ossl_prov_ml_kem_new(gctx->provctx, gctx->propq, gctx->evp_type);
969
0
    if (key == NULL)
970
0
        return NULL;
971
972
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
973
0
        return key;
974
975
0
    if (seed != NULL && !ossl_ml_kem_set_seed(seed, ML_KEM_SEED_BYTES, key))
976
0
        return NULL;
977
0
    genok = ossl_ml_kem_genkey(nopub, 0, key);
978
979
    /* Erase the single-use seed */
980
0
    if (seed != NULL)
981
0
        OPENSSL_cleanse(seed, ML_KEM_SEED_BYTES);
982
0
    gctx->seed = NULL;
983
984
0
    if (genok) {
985
#ifdef FIPS_MODULE
986
        if (!ml_kem_pairwise_test(key, ML_KEM_KEY_FIXED_PCT)) {
987
            ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
988
            ossl_ml_kem_key_free(key);
989
            return NULL;
990
        }
991
#endif  /* FIPS_MODULE */
992
0
        return key;
993
0
    }
994
995
0
    ossl_ml_kem_key_free(key);
996
0
    return NULL;
997
0
}
998
999
static void ml_kem_gen_cleanup(void *vgctx)
1000
0
{
1001
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
1002
1003
0
    if (gctx == NULL)
1004
0
        return;
1005
1006
0
    if (gctx->seed != NULL)
1007
0
        OPENSSL_cleanse(gctx->seed, ML_KEM_RANDOM_BYTES);
1008
0
    OPENSSL_free(gctx->propq);
1009
0
    OPENSSL_free(gctx);
1010
0
}
1011
1012
static void *ml_kem_dup(const void *vkey, int selection)
1013
0
{
1014
0
    const ML_KEM_KEY *key = vkey;
1015
1016
0
    if (!ossl_prov_is_running())
1017
0
        return NULL;
1018
1019
0
    return ossl_ml_kem_key_dup(key, selection);
1020
0
}
1021
1022
#ifndef FIPS_MODULE
1023
# define DISPATCH_LOAD_FN \
1024
        { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_kem_load },
1025
#else
1026
# define DISPATCH_LOAD_FN   /* Non-FIPS only */
1027
#endif
1028
1029
#define DECLARE_VARIANT(bits) \
1030
    static OSSL_FUNC_keymgmt_new_fn ml_kem_##bits##_new;                    \
1031
    static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_##bits##_gen_init;          \
1032
    static void *ml_kem_##bits##_new(void *provctx) \
1033
0
    { \
1034
0
        return ossl_prov_ml_kem_new(provctx, NULL, EVP_PKEY_ML_KEM_##bits); \
1035
0
    } \
Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_512_new
Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_768_new
Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_1024_new
1036
    static void *ml_kem_##bits##_gen_init(void *provctx, int selection, \
1037
                                          const OSSL_PARAM params[]) \
1038
0
    { \
1039
0
        return ml_kem_gen_init(provctx, selection, params, \
1040
0
                               EVP_PKEY_ML_KEM_##bits); \
1041
0
    } \
Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_512_gen_init
Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_768_gen_init
Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_1024_gen_init
1042
    const OSSL_DISPATCH ossl_ml_kem_##bits##_keymgmt_functions[] = { \
1043
        { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) ml_kem_##bits##_new }, \
1044
        { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) ossl_ml_kem_key_free }, \
1045
        { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) ml_kem_get_params }, \
1046
        { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gettable_params }, \
1047
        { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) ml_kem_set_params }, \
1048
        { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_settable_params }, \
1049
        { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) ml_kem_has }, \
1050
        { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) ml_kem_match }, \
1051
        { OSSL_FUNC_KEYMGMT_VALIDATE, (OSSL_FUNC) ml_kem_validate }, \
1052
        { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) ml_kem_##bits##_gen_init }, \
1053
        { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) ml_kem_gen_set_params }, \
1054
        { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gen_settable_params }, \
1055
        { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) ml_kem_gen }, \
1056
        { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) ml_kem_gen_cleanup }, \
1057
        DISPATCH_LOAD_FN \
1058
        { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) ml_kem_dup }, \
1059
        { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) ml_kem_import }, \
1060
        { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \
1061
        { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) ml_kem_export }, \
1062
        { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \
1063
        OSSL_DISPATCH_END \
1064
    }
1065
DECLARE_VARIANT(512);
1066
DECLARE_VARIANT(768);
1067
DECLARE_VARIANT(1024);