Coverage Report

Created: 2026-05-30 06:06

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