Coverage Report

Created: 2026-05-24 07:14

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