Coverage Report

Created: 2026-10-28 06:28

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