Coverage Report

Created: 2026-10-28 06:28

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