Coverage Report

Created: 2026-09-12 06:55

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