Coverage Report

Created: 2025-12-31 06:58

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