Coverage Report

Created: 2026-04-11 06:29

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