Coverage Report

Created: 2026-09-12 06:55

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