Coverage Report

Created: 2026-10-28 06:28

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