Coverage Report

Created: 2025-06-22 06:56

/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
0
static struct ml_kem_key_type_params_st ml_kem_key_type_params_decoder(const OSSL_PARAM params[]) {
349
0
    struct ml_kem_key_type_params_st r;
350
0
    const OSSL_PARAM *p;
351
0
    const char *s;
352
353
0
    memset(&r, 0, sizeof(r));
354
0
    for (p = params; (s = p->key) != NULL; p++)
355
0
        switch(s[0]) {
356
0
        default:
357
0
            break;
358
0
        case 'p':
359
0
            switch(s[1]) {
360
0
            default:
361
0
                break;
362
0
            case 'r':
363
0
                if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0))
364
0
                    r.privkey = (OSSL_PARAM *)p;
365
0
                break;
366
0
            case 'u':
367
0
                if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0))
368
0
                    r.pubkey = (OSSL_PARAM *)p;
369
0
            }
370
0
            break;
371
0
        case 's':
372
0
            if (ossl_likely(r.seed == NULL && strcmp("eed", s + 1) == 0))
373
0
                r.seed = (OSSL_PARAM *)p;
374
0
        }
375
0
    return r;
376
0
}
377
#endif
378
/* End of machine generated */
379
380
static const OSSL_PARAM *ml_kem_imexport_types(int selection)
381
0
{
382
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
383
0
        return ml_kem_key_type_params_list;
384
0
    return NULL;
385
0
}
386
387
static int check_seed(const uint8_t *seed, const uint8_t *prvenc,
388
                      ML_KEM_KEY *key)
389
0
{
390
0
    size_t zlen = ML_KEM_RANDOM_BYTES;
391
392
0
    if (memcmp(seed + ML_KEM_SEED_BYTES - zlen,
393
0
               prvenc + key->vinfo->prvkey_bytes - zlen, zlen) == 0)
394
0
        return 1;
395
0
    ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
396
0
                   "private %s key implicit rejection secret does"
397
0
                   " not match seed", key->vinfo->algorithm_name);
398
0
    return 0;
399
0
}
400
401
static int check_prvenc(const uint8_t *prvenc, ML_KEM_KEY *key)
402
0
{
403
0
    size_t len = key->vinfo->prvkey_bytes;
404
0
    uint8_t *buf = OPENSSL_malloc(len);
405
0
    int ret = 0;
406
407
0
    if (buf != NULL
408
0
        && ossl_ml_kem_encode_private_key(buf, len, key))
409
0
        ret = memcmp(buf, prvenc, len) == 0;
410
0
    OPENSSL_clear_free(buf, len);
411
0
    if (ret)
412
0
        return 1;
413
414
0
    if (buf != NULL)
415
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
416
0
                       "explicit %s private key does not match seed",
417
0
                       key->vinfo->algorithm_name);
418
0
    ossl_ml_kem_key_reset(key);
419
0
    return 0;
420
0
}
421
422
static int ml_kem_key_fromdata(ML_KEM_KEY *key,
423
                               const OSSL_PARAM params[],
424
                               int include_private)
425
0
{
426
0
    const void *pubenc = NULL, *prvenc = NULL, *seedenc = NULL;
427
0
    size_t publen = 0, prvlen = 0, seedlen = 0, puboff;
428
0
    const ML_KEM_VINFO *v;
429
0
    struct ml_kem_key_type_params_st p;
430
431
    /* Invalid attempt to mutate a key, what is the right error to report? */
432
0
    if (key == NULL || ossl_ml_kem_have_pubkey(key))
433
0
        return 0;
434
0
    v = ossl_ml_kem_key_vinfo(key);
435
0
    p = ml_kem_key_type_params_decoder(params);
436
437
    /*
438
     * When a private key is provided, without a seed, any public key also
439
     * provided will be ignored (apart from length), just as with the seed.
440
     */
441
0
    if (p.seed != NULL && include_private) {
442
        /*
443
         * When a seed is provided, the private and public keys may be ignored,
444
         * after validating just their lengths.  Comparing encodings or hashes
445
         * when applicable is possible, but not currently implemented.
446
         */
447
0
        if (OSSL_PARAM_get_octet_string_ptr(p.seed, &seedenc, &seedlen) != 1)
448
0
            return 0;
449
0
        if (seedlen != 0 && seedlen != ML_KEM_SEED_BYTES) {
450
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
451
0
            return 0;
452
0
        }
453
0
    }
454
455
0
    if (p.privkey != NULL && include_private) {
456
0
        if (OSSL_PARAM_get_octet_string_ptr(p.privkey, &prvenc, &prvlen) != 1)
457
0
            return 0;
458
0
        if (prvlen != 0 && prvlen != v->prvkey_bytes) {
459
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
460
0
            return 0;
461
0
        }
462
0
    }
463
464
    /* Used only when no seed or private key is provided. */
465
0
    if (p.pubkey != NULL) {
466
0
        if (OSSL_PARAM_get_octet_string_ptr(p.pubkey, &pubenc, &publen) != 1)
467
0
            return 0;
468
0
        if (publen != 0 && publen != v->pubkey_bytes) {
469
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
470
0
            return 0;
471
0
        }
472
0
    }
473
474
    /* The caller MUST specify at least one of seed, private or public keys. */
475
0
    if (seedlen == 0 && publen == 0 && prvlen == 0) {
476
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
477
0
        return 0;
478
0
    }
479
480
    /* Check any explicit public key against embedded value in private key */
481
0
    if (publen > 0 && prvlen > 0) {
482
        /* point to the ek offset in dk = DKpke||ek||H(ek)||z */
483
0
        puboff = prvlen - ML_KEM_RANDOM_BYTES - ML_KEM_PKHASH_BYTES - publen;
484
0
        if (memcmp(pubenc, (unsigned char *)prvenc + puboff, publen) != 0) {
485
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
486
0
                           "explicit %s public key does not match private",
487
0
                           v->algorithm_name);
488
0
            return 0;
489
0
        }
490
0
    }
491
492
0
    if (seedlen != 0
493
0
        && (prvlen == 0 || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) {
494
0
        if (prvlen != 0 && !check_seed(seedenc, prvenc, key))
495
0
            return 0;
496
0
        if (!ossl_ml_kem_set_seed(seedenc, seedlen, key)
497
0
            || !ossl_ml_kem_genkey(NULL, 0, key))
498
0
            return 0;
499
0
        return prvlen == 0 || check_prvenc(prvenc, key);
500
0
    } else if (prvlen != 0) {
501
0
        return ossl_ml_kem_parse_private_key(prvenc, prvlen, key);
502
0
    }
503
0
    return ossl_ml_kem_parse_public_key(pubenc, publen, key);
504
0
}
505
506
static int ml_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
507
0
{
508
0
    ML_KEM_KEY *key = vkey;
509
0
    int include_private;
510
0
    int res;
511
512
0
    if (!ossl_prov_is_running() || key == NULL)
513
0
        return 0;
514
515
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
516
0
        return 0;
517
518
0
    include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
519
0
    res = ml_kem_key_fromdata(key, params, include_private);
520
0
    if (res > 0 && include_private
521
0
        && !ml_kem_pairwise_test(key, key->prov_flags)) {
522
#ifdef FIPS_MODULE
523
        ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
524
#endif
525
0
        ossl_ml_kem_key_reset(key);
526
0
        res = 0;
527
0
    }
528
0
    return res;
529
0
}
530
531
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
532
#ifndef ml_kem_get_params_list
533
static const OSSL_PARAM ml_kem_get_params_list[] = {
534
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
535
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
536
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
537
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL),
538
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0),
539
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
540
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
541
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
542
    OSSL_PARAM_END
543
};
544
#endif
545
546
#ifndef ml_kem_get_params_st
547
struct ml_kem_get_params_st {
548
    OSSL_PARAM *bits;
549
    OSSL_PARAM *encpubkey;
550
    OSSL_PARAM *maxsize;
551
    OSSL_PARAM *privkey;
552
    OSSL_PARAM *pubkey;
553
    OSSL_PARAM *secbits;
554
    OSSL_PARAM *seccat;
555
    OSSL_PARAM *seed;
556
};
557
#endif
558
559
#ifndef ml_kem_get_params_decoder
560
0
static struct ml_kem_get_params_st ml_kem_get_params_decoder(const OSSL_PARAM params[]) {
561
0
    struct ml_kem_get_params_st r;
562
0
    const OSSL_PARAM *p;
563
0
    const char *s;
564
565
0
    memset(&r, 0, sizeof(r));
566
0
    for (p = params; (s = p->key) != NULL; p++)
567
0
        switch(s[0]) {
568
0
        default:
569
0
            break;
570
0
        case 'b':
571
0
            if (ossl_likely(r.bits == NULL && strcmp("its", s + 1) == 0))
572
0
                r.bits = (OSSL_PARAM *)p;
573
0
            break;
574
0
        case 'e':
575
0
            if (ossl_likely(r.encpubkey == NULL && strcmp("ncoded-pub-key", s + 1) == 0))
576
0
                r.encpubkey = (OSSL_PARAM *)p;
577
0
            break;
578
0
        case 'm':
579
0
            if (ossl_likely(r.maxsize == NULL && strcmp("ax-size", s + 1) == 0))
580
0
                r.maxsize = (OSSL_PARAM *)p;
581
0
            break;
582
0
        case 'p':
583
0
            switch(s[1]) {
584
0
            default:
585
0
                break;
586
0
            case 'r':
587
0
                if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0))
588
0
                    r.privkey = (OSSL_PARAM *)p;
589
0
                break;
590
0
            case 'u':
591
0
                if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0))
592
0
                    r.pubkey = (OSSL_PARAM *)p;
593
0
            }
594
0
            break;
595
0
        case 's':
596
0
            switch(s[1]) {
597
0
            default:
598
0
                break;
599
0
            case 'e':
600
0
                switch(s[2]) {
601
0
                default:
602
0
                    break;
603
0
                case 'c':
604
0
                    switch(s[3]) {
605
0
                    default:
606
0
                        break;
607
0
                    case 'u':
608
0
                        switch(s[4]) {
609
0
                        default:
610
0
                            break;
611
0
                        case 'r':
612
0
                            switch(s[5]) {
613
0
                            default:
614
0
                                break;
615
0
                            case 'i':
616
0
                                switch(s[6]) {
617
0
                                default:
618
0
                                    break;
619
0
                                case 't':
620
0
                                    switch(s[7]) {
621
0
                                    default:
622
0
                                        break;
623
0
                                    case 'y':
624
0
                                        switch(s[8]) {
625
0
                                        default:
626
0
                                            break;
627
0
                                        case '-':
628
0
                                            switch(s[9]) {
629
0
                                            default:
630
0
                                                break;
631
0
                                            case 'b':
632
0
                                                if (ossl_likely(r.secbits == NULL && strcmp("its", s + 10) == 0))
633
0
                                                    r.secbits = (OSSL_PARAM *)p;
634
0
                                                break;
635
0
                                            case 'c':
636
0
                                                if (ossl_likely(r.seccat == NULL && strcmp("ategory", s + 10) == 0))
637
0
                                                    r.seccat = (OSSL_PARAM *)p;
638
0
                                            }
639
0
                                        }
640
0
                                    }
641
0
                                }
642
0
                            }
643
0
                        }
644
0
                    }
645
0
                    break;
646
0
                case 'e':
647
0
                    if (ossl_likely(r.seed == NULL && strcmp("d", s + 3) == 0))
648
0
                        r.seed = (OSSL_PARAM *)p;
649
0
                }
650
0
            }
651
0
        }
652
0
    return r;
653
0
}
654
#endif
655
/* End of machine generated */
656
657
static const OSSL_PARAM *ml_kem_gettable_params(void *provctx)
658
0
{
659
0
    return ml_kem_get_params_list;
660
0
}
661
662
#ifndef FIPS_MODULE
663
void *ml_kem_load(const void *reference, size_t reference_sz)
664
0
{
665
0
    ML_KEM_KEY *key = NULL;
666
0
    uint8_t *encoded_dk = NULL;
667
0
    uint8_t seed[ML_KEM_SEED_BYTES];
668
669
0
    if (ossl_prov_is_running() && reference_sz == sizeof(key)) {
670
        /* The contents of the reference is the address to our object */
671
0
        key = *(ML_KEM_KEY **)reference;
672
0
        encoded_dk = key->encoded_dk;
673
0
        key->encoded_dk = NULL;
674
        /* We grabbed, so we detach it */
675
0
        *(ML_KEM_KEY **)reference = NULL;
676
0
        if (encoded_dk != NULL
677
0
            && ossl_ml_kem_encode_seed(seed, sizeof(seed), key)
678
0
            && !check_seed(seed, encoded_dk, key))
679
0
            goto err;
680
        /* Generate the key now, if it holds only a stashed seed. */
681
0
        if (ossl_ml_kem_have_seed(key)
682
0
            && (encoded_dk == NULL
683
0
                || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) {
684
0
            if (!ossl_ml_kem_genkey(NULL, 0, key)
685
0
                || (encoded_dk != NULL && !check_prvenc(encoded_dk, key)))
686
0
                goto err;
687
0
        } else if (encoded_dk != NULL) {
688
0
            if (!ossl_ml_kem_parse_private_key(encoded_dk,
689
0
                                               key->vinfo->prvkey_bytes, key)) {
690
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
691
0
                               "error parsing %s private key",
692
0
                               key->vinfo->algorithm_name);
693
0
                goto err;
694
0
            }
695
0
            if (!ml_kem_pairwise_test(key, key->prov_flags))
696
0
                goto err;
697
0
        }
698
0
        OPENSSL_free(encoded_dk);
699
0
        return key;
700
0
    }
701
702
0
 err:
703
0
    OPENSSL_free(encoded_dk);
704
0
    ossl_ml_kem_key_free(key);
705
0
    return NULL;
706
0
}
707
#endif
708
709
static int ml_kem_get_key_param(const ML_KEM_KEY *key, OSSL_PARAM *p,
710
                                size_t bytes,
711
                                int (*get_f)(uint8_t *out, size_t len,
712
                                             const ML_KEM_KEY *key))
713
0
{
714
0
    if (p->data_type != OSSL_PARAM_OCTET_STRING)
715
0
            return 0;
716
0
    p->return_size = bytes;
717
0
    if (p->data != NULL)
718
0
        if (p->data_size < p->return_size
719
0
                || !(*get_f)(p->data, p->return_size, key))
720
0
            return 0;
721
0
    return 1;
722
0
}
723
724
/*
725
 * It is assumed the key is guaranteed non-NULL here, and is from this provider
726
 */
727
static int ml_kem_get_params(void *vkey, OSSL_PARAM params[])
728
0
{
729
0
    ML_KEM_KEY *key = vkey;
730
0
    const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key);
731
0
    struct ml_kem_get_params_st p = ml_kem_get_params_decoder(params);
732
733
0
    if (p.bits != NULL && !OSSL_PARAM_set_int(p.bits, v->bits))
734
0
        return 0;
735
736
0
    if (p.secbits != NULL && !OSSL_PARAM_set_int(p.secbits, v->secbits))
737
0
        return 0;
738
739
0
    if (p.maxsize != NULL && !OSSL_PARAM_set_int(p.maxsize, v->ctext_bytes))
740
0
        return 0;
741
742
0
    if (p.seccat != NULL && !OSSL_PARAM_set_int(p.seccat, v->security_category))
743
0
        return 0;
744
745
0
    if (p.pubkey != NULL && ossl_ml_kem_have_pubkey(key)) {
746
        /* Exported to EVP_PKEY_get_raw_public_key() */
747
0
        if (!ml_kem_get_key_param(key, p.pubkey, v->pubkey_bytes,
748
0
                                  &ossl_ml_kem_encode_public_key))
749
0
            return 0;
750
0
    }
751
752
0
    if (p.encpubkey != NULL && ossl_ml_kem_have_pubkey(key)) {
753
        /* Needed by EVP_PKEY_get1_encoded_public_key() */
754
0
        if (!ml_kem_get_key_param(key, p.encpubkey, v->pubkey_bytes,
755
0
                                  &ossl_ml_kem_encode_public_key))
756
0
            return 0;
757
0
    }
758
759
0
    if (p.privkey != NULL && ossl_ml_kem_have_prvkey(key)) {
760
        /* Exported to EVP_PKEY_get_raw_private_key() */
761
0
        if (!ml_kem_get_key_param(key, p.privkey, v->prvkey_bytes,
762
0
                                  &ossl_ml_kem_encode_private_key))
763
0
            return 0;
764
0
    }
765
766
0
    if (p.seed != NULL && ossl_ml_kem_have_seed(key)) {
767
        /* Exported for import */
768
0
        if (!ml_kem_get_key_param(key, p.seed, ML_KEM_SEED_BYTES,
769
0
                                  &ossl_ml_kem_encode_seed))
770
0
            return 0;
771
0
    }
772
0
    return 1;
773
0
}
774
775
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
776
#ifndef ml_kem_set_params_list
777
static const OSSL_PARAM ml_kem_set_params_list[] = {
778
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
779
    OSSL_PARAM_END
780
};
781
#endif
782
783
#ifndef ml_kem_set_params_st
784
struct ml_kem_set_params_st {
785
    OSSL_PARAM *pub;
786
};
787
#endif
788
789
#ifndef ml_kem_set_params_decoder
790
0
static struct ml_kem_set_params_st ml_kem_set_params_decoder(const OSSL_PARAM params[]) {
791
0
    struct ml_kem_set_params_st r;
792
0
    const OSSL_PARAM *p;
793
0
    const char *s;
794
795
0
    memset(&r, 0, sizeof(r));
796
0
    for (p = params; (s = p->key) != NULL; p++)
797
0
        if (ossl_likely(r.pub == NULL && strcmp("encoded-pub-key", s + 0) == 0))
798
0
            r.pub = (OSSL_PARAM *)p;
799
0
    return r;
800
0
}
801
#endif
802
/* End of machine generated */
803
804
static const OSSL_PARAM *ml_kem_settable_params(void *provctx)
805
0
{
806
0
    return ml_kem_set_params_list;
807
0
}
808
809
static int ml_kem_set_params(void *vkey, const OSSL_PARAM params[])
810
0
{
811
0
    ML_KEM_KEY *key = vkey;
812
0
    const void *pubenc = NULL;
813
0
    size_t publen = 0;
814
0
    struct ml_kem_set_params_st p;
815
816
0
    if (ossl_param_is_empty(params))
817
0
        return 1;
818
819
0
    p = ml_kem_set_params_decoder(params);
820
821
    /* Used in TLS via EVP_PKEY_set1_encoded_public_key(). */
822
0
    if (p.pub != NULL
823
0
        && (OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen) != 1
824
0
            || publen != key->vinfo->pubkey_bytes)) {
825
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
826
0
        return 0;
827
0
    }
828
829
0
    if (publen == 0)
830
0
        return 1;
831
832
    /* Key mutation is reportedly generally not allowed */
833
0
    if (ossl_ml_kem_have_pubkey(key)) {
834
0
        ERR_raise_data(ERR_LIB_PROV,
835
0
                       PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
836
0
                       "ML-KEM keys cannot be mutated");
837
0
        return 0;
838
0
    }
839
840
0
    return ossl_ml_kem_parse_public_key(pubenc, publen, key);
841
0
}
842
843
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
844
#ifndef ml_kem_gen_set_params_list
845
static const OSSL_PARAM ml_kem_gen_set_params_list[] = {
846
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0),
847
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
848
    OSSL_PARAM_END
849
};
850
#endif
851
852
#ifndef ml_kem_gen_set_params_st
853
struct ml_kem_gen_set_params_st {
854
    OSSL_PARAM *propq;
855
    OSSL_PARAM *seed;
856
};
857
#endif
858
859
#ifndef ml_kem_gen_set_params_decoder
860
0
static struct ml_kem_gen_set_params_st ml_kem_gen_set_params_decoder(const OSSL_PARAM params[]) {
861
0
    struct ml_kem_gen_set_params_st r;
862
0
    const OSSL_PARAM *p;
863
0
    const char *s;
864
865
0
    memset(&r, 0, sizeof(r));
866
0
    for (p = params; (s = p->key) != NULL; p++)
867
0
        switch(s[0]) {
868
0
        default:
869
0
            break;
870
0
        case 'p':
871
0
            if (ossl_likely(r.propq == NULL && strcmp("roperties", s + 1) == 0))
872
0
                r.propq = (OSSL_PARAM *)p;
873
0
            break;
874
0
        case 's':
875
0
            if (ossl_likely(r.seed == NULL && strcmp("eed", s + 1) == 0))
876
0
                r.seed = (OSSL_PARAM *)p;
877
0
        }
878
0
    return r;
879
0
}
880
#endif
881
/* End of machine generated */
882
883
static int ml_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
884
0
{
885
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
886
0
    struct ml_kem_gen_set_params_st p;
887
888
0
    if (gctx == NULL)
889
0
        return 0;
890
0
    if (ossl_param_is_empty(params))
891
0
        return 1;
892
893
0
    p = ml_kem_gen_set_params_decoder(params);
894
895
0
    if (p.propq != NULL) {
896
0
        if (p.propq->data_type != OSSL_PARAM_UTF8_STRING)
897
0
            return 0;
898
0
        OPENSSL_free(gctx->propq);
899
0
        if ((gctx->propq = OPENSSL_strdup(p.propq->data)) == NULL)
900
0
            return 0;
901
0
    }
902
903
0
    if (p.seed != NULL) {
904
0
        size_t len = ML_KEM_SEED_BYTES;
905
906
0
        gctx->seed = gctx->seedbuf;
907
0
        if (OSSL_PARAM_get_octet_string(p.seed, (void **)&gctx->seed, len, &len)
908
0
            && len == ML_KEM_SEED_BYTES)
909
0
            return 1;
910
911
        /* Possibly, but less likely wrong data type */
912
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
913
0
        gctx->seed = NULL;
914
0
        return 0;
915
0
    }
916
917
0
    return 1;
918
0
}
919
920
static void *ml_kem_gen_init(void *provctx, int selection,
921
                             const OSSL_PARAM params[], int evp_type)
922
0
{
923
0
    PROV_ML_KEM_GEN_CTX *gctx = NULL;
924
925
    /*
926
     * We can only generate private keys, check that the selection is
927
     * appropriate.
928
     */
929
0
    if (!ossl_prov_is_running()
930
0
        || (selection & minimal_selection) == 0
931
0
        || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
932
0
        return NULL;
933
934
0
    gctx->selection = selection;
935
0
    gctx->evp_type = evp_type;
936
0
    gctx->provctx = provctx;
937
0
    if (ml_kem_gen_set_params(gctx, params))
938
0
        return gctx;
939
940
0
    ml_kem_gen_cleanup(gctx);
941
0
    return NULL;
942
0
}
943
944
static const OSSL_PARAM *ml_kem_gen_settable_params(ossl_unused void *vgctx,
945
                                                    ossl_unused void *provctx)
946
0
{
947
0
    return ml_kem_gen_set_params_list;
948
0
}
949
950
static void *ml_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
951
0
{
952
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
953
0
    ML_KEM_KEY *key;
954
0
    uint8_t *nopub = NULL;
955
0
    uint8_t *seed;
956
0
    int genok = 0;
957
958
0
    if (gctx == NULL
959
0
        || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
960
0
            OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
961
0
        return NULL;
962
0
    seed = gctx->seed;
963
0
    key = ossl_prov_ml_kem_new(gctx->provctx, gctx->propq, gctx->evp_type);
964
0
    if (key == NULL)
965
0
        return NULL;
966
967
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
968
0
        return key;
969
970
0
    if (seed != NULL && !ossl_ml_kem_set_seed(seed, ML_KEM_SEED_BYTES, key))
971
0
        return NULL;
972
0
    genok = ossl_ml_kem_genkey(nopub, 0, key);
973
974
    /* Erase the single-use seed */
975
0
    if (seed != NULL)
976
0
        OPENSSL_cleanse(seed, ML_KEM_SEED_BYTES);
977
0
    gctx->seed = NULL;
978
979
0
    if (genok) {
980
#ifdef FIPS_MODULE
981
        if (!ml_kem_pairwise_test(key, ML_KEM_KEY_FIXED_PCT)) {
982
            ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
983
            ossl_ml_kem_key_free(key);
984
            return NULL;
985
        }
986
#endif  /* FIPS_MODULE */
987
0
        return key;
988
0
    }
989
990
0
    ossl_ml_kem_key_free(key);
991
0
    return NULL;
992
0
}
993
994
static void ml_kem_gen_cleanup(void *vgctx)
995
0
{
996
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
997
998
0
    if (gctx == NULL)
999
0
        return;
1000
1001
0
    if (gctx->seed != NULL)
1002
0
        OPENSSL_cleanse(gctx->seed, ML_KEM_RANDOM_BYTES);
1003
0
    OPENSSL_free(gctx->propq);
1004
0
    OPENSSL_free(gctx);
1005
0
}
1006
1007
static void *ml_kem_dup(const void *vkey, int selection)
1008
0
{
1009
0
    const ML_KEM_KEY *key = vkey;
1010
1011
0
    if (!ossl_prov_is_running())
1012
0
        return NULL;
1013
1014
0
    return ossl_ml_kem_key_dup(key, selection);
1015
0
}
1016
1017
#ifndef FIPS_MODULE
1018
# define DISPATCH_LOAD_FN \
1019
        { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_kem_load },
1020
#else
1021
# define DISPATCH_LOAD_FN   /* Non-FIPS only */
1022
#endif
1023
1024
#define DECLARE_VARIANT(bits) \
1025
    static OSSL_FUNC_keymgmt_new_fn ml_kem_##bits##_new;                    \
1026
    static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_##bits##_gen_init;          \
1027
    static void *ml_kem_##bits##_new(void *provctx) \
1028
0
    { \
1029
0
        return ossl_prov_ml_kem_new(provctx, NULL, EVP_PKEY_ML_KEM_##bits); \
1030
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
1031
    static void *ml_kem_##bits##_gen_init(void *provctx, int selection, \
1032
                                          const OSSL_PARAM params[]) \
1033
0
    { \
1034
0
        return ml_kem_gen_init(provctx, selection, params, \
1035
0
                               EVP_PKEY_ML_KEM_##bits); \
1036
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
1037
    const OSSL_DISPATCH ossl_ml_kem_##bits##_keymgmt_functions[] = { \
1038
        { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) ml_kem_##bits##_new }, \
1039
        { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) ossl_ml_kem_key_free }, \
1040
        { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) ml_kem_get_params }, \
1041
        { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gettable_params }, \
1042
        { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) ml_kem_set_params }, \
1043
        { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_settable_params }, \
1044
        { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) ml_kem_has }, \
1045
        { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) ml_kem_match }, \
1046
        { OSSL_FUNC_KEYMGMT_VALIDATE, (OSSL_FUNC) ml_kem_validate }, \
1047
        { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) ml_kem_##bits##_gen_init }, \
1048
        { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) ml_kem_gen_set_params }, \
1049
        { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gen_settable_params }, \
1050
        { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) ml_kem_gen }, \
1051
        { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) ml_kem_gen_cleanup }, \
1052
        DISPATCH_LOAD_FN \
1053
        { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) ml_kem_dup }, \
1054
        { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) ml_kem_import }, \
1055
        { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \
1056
        { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) ml_kem_export }, \
1057
        { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \
1058
        OSSL_DISPATCH_END \
1059
    }
1060
DECLARE_VARIANT(512);
1061
DECLARE_VARIANT(768);
1062
DECLARE_VARIANT(1024);