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/mlx_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/err.h>
13
#include <openssl/obj_mac.h>
14
#include <openssl/param_build.h>
15
#include <openssl/params.h>
16
#include <openssl/proverr.h>
17
#include <openssl/rand.h>
18
#include <openssl/self_test.h>
19
#include "internal/nelem.h"
20
#include "internal/param_build_set.h"
21
#include "prov/implementations.h"
22
#include "prov/mlx_kem.h"
23
#include "prov/provider_ctx.h"
24
#include "prov/providercommon.h"
25
#include "prov/securitycheck.h"
26
#include "fips/fipsindicator.h"
27
28
static OSSL_FUNC_keymgmt_gen_fn mlx_kem_gen;
29
static OSSL_FUNC_keymgmt_gen_cleanup_fn mlx_kem_gen_cleanup;
30
static OSSL_FUNC_keymgmt_gen_set_params_fn mlx_kem_gen_set_params;
31
static OSSL_FUNC_keymgmt_gen_settable_params_fn mlx_kem_gen_settable_params;
32
static OSSL_FUNC_keymgmt_get_params_fn mlx_kem_get_params;
33
static OSSL_FUNC_keymgmt_gettable_params_fn mlx_kem_gettable_params;
34
static OSSL_FUNC_keymgmt_set_params_fn mlx_kem_set_params;
35
static OSSL_FUNC_keymgmt_settable_params_fn mlx_kem_settable_params;
36
static OSSL_FUNC_keymgmt_has_fn mlx_kem_has;
37
static OSSL_FUNC_keymgmt_match_fn mlx_kem_match;
38
static OSSL_FUNC_keymgmt_import_fn mlx_kem_import;
39
static OSSL_FUNC_keymgmt_export_fn mlx_kem_export;
40
static OSSL_FUNC_keymgmt_import_types_fn mlx_kem_imexport_types;
41
static OSSL_FUNC_keymgmt_export_types_fn mlx_kem_imexport_types;
42
static OSSL_FUNC_keymgmt_dup_fn mlx_kem_dup;
43
44
static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
45
    | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
46
47
/* Must match DECLARE_DISPATCH invocations at the end of the file */
48
static const ECDH_VINFO hybrid_vtable[] = {
49
    { "EC", "P-256", 65, 32, 32, 1, EVP_PKEY_ML_KEM_768 },
50
    { "EC", "P-384", 97, 48, 48, 1, EVP_PKEY_ML_KEM_1024 },
51
#if !defined(OPENSSL_NO_ECX)
52
    { "X25519", NULL, 32, 32, 32, 0, EVP_PKEY_ML_KEM_768 },
53
    { "X448", NULL, 56, 56, 56, 0, EVP_PKEY_ML_KEM_1024 },
54
#endif
55
};
56
57
typedef struct mlx_kem_gen_ctx_st {
58
    OSSL_LIB_CTX *libctx;
59
    char *propq;
60
    int selection;
61
    unsigned int evp_type;
62
} PROV_ML_KEM_GEN_CTX;
63
64
static void mlx_kem_key_free(void *vkey)
65
86.0k
{
66
86.0k
    MLX_KEY *key = vkey;
67
68
86.0k
    if (key == NULL)
69
0
        return;
70
86.0k
    OPENSSL_free(key->propq);
71
86.0k
    EVP_PKEY_free(key->mkey);
72
86.0k
    EVP_PKEY_free(key->xkey);
73
86.0k
    OPENSSL_free(key);
74
86.0k
}
75
76
/* Takes ownership of propq */
77
static void *
78
mlx_kem_key_new(unsigned int v, OSSL_LIB_CTX *libctx, char *propq)
79
86.0k
{
80
86.0k
    MLX_KEY *key = NULL;
81
86.0k
    unsigned int ml_kem_variant;
82
83
86.0k
    if (!ossl_prov_is_running()
84
86.0k
        || v >= OSSL_NELEM(hybrid_vtable)
85
86.0k
        || (key = OPENSSL_malloc(sizeof(*key))) == NULL)
86
0
        goto err;
87
88
86.0k
    ml_kem_variant = hybrid_vtable[v].ml_kem_variant;
89
86.0k
    key->libctx = libctx;
90
86.0k
    key->minfo = ossl_ml_kem_get_vinfo(ml_kem_variant);
91
86.0k
    key->xinfo = &hybrid_vtable[v];
92
86.0k
    key->xkey = key->mkey = NULL;
93
86.0k
    key->state = MLX_HAVE_NOKEYS;
94
86.0k
    key->propq = propq;
95
86.0k
    return key;
96
97
0
err:
98
0
    OPENSSL_free(propq);
99
0
    return NULL;
100
86.0k
}
101
102
static int mlx_kem_has(const void *vkey, int selection)
103
0
{
104
0
    const MLX_KEY *key = vkey;
105
106
    /* A NULL key MUST fail to have anything */
107
0
    if (!ossl_prov_is_running() || key == NULL)
108
0
        return 0;
109
110
0
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
111
0
    case 0:
112
0
        return 1;
113
0
    case OSSL_KEYMGMT_SELECT_PUBLIC_KEY:
114
0
        return mlx_kem_have_pubkey(key);
115
0
    default:
116
0
        return mlx_kem_have_prvkey(key);
117
0
    }
118
0
}
119
120
static int mlx_kem_match(const void *vkey1, const void *vkey2, int selection)
121
0
{
122
0
    const MLX_KEY *key1 = vkey1;
123
0
    const MLX_KEY *key2 = vkey2;
124
0
    int have_pub1 = mlx_kem_have_pubkey(key1);
125
0
    int have_pub2 = mlx_kem_have_pubkey(key2);
126
127
0
    if (!ossl_prov_is_running())
128
0
        return 0;
129
130
    /* Compare domain parameters */
131
0
    if (key1->xinfo != key2->xinfo)
132
0
        return 0;
133
134
0
    if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR))
135
0
        return 1;
136
137
0
    if (have_pub1 ^ have_pub2)
138
0
        return 0;
139
140
    /* As in other providers, equal when both have no key material. */
141
0
    if (!have_pub1)
142
0
        return 1;
143
144
0
    return EVP_PKEY_eq(key1->mkey, key2->mkey)
145
0
        && EVP_PKEY_eq(key1->xkey, key2->xkey);
146
0
}
147
148
typedef struct export_cb_arg_st {
149
    const char *algorithm_name;
150
    uint8_t *pubenc;
151
    uint8_t *prvenc;
152
    int pubcount;
153
    int prvcount;
154
    size_t puboff;
155
    size_t prvoff;
156
    size_t publen;
157
    size_t prvlen;
158
} EXPORT_CB_ARG;
159
160
/* Copy any exported key material into its storage slot */
161
static int export_sub_cb(const OSSL_PARAM *params, void *varg)
162
30.3k
{
163
30.3k
    EXPORT_CB_ARG *sub_arg = varg;
164
30.3k
    const OSSL_PARAM *p = NULL;
165
30.3k
    size_t len;
166
167
    /*
168
     * The caller will decide whether anything essential is missing, but, if
169
     * some key material was returned, it should have the right (parameter)
170
     * data type and length.
171
     */
172
30.3k
    if (ossl_param_is_empty(params))
173
0
        return 1;
174
30.3k
    if (sub_arg->pubenc != NULL
175
30.3k
        && (p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY)) != NULL) {
176
30.3k
        void *pub = sub_arg->pubenc + sub_arg->puboff;
177
178
30.3k
        if (OSSL_PARAM_get_octet_string(p, &pub, sub_arg->publen, &len) != 1)
179
0
            return 0;
180
30.3k
        if (len != sub_arg->publen) {
181
0
            ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
182
0
                "Unexpected %s public key length %lu != %lu",
183
0
                sub_arg->algorithm_name, (unsigned long)len,
184
0
                sub_arg->publen);
185
0
            return 0;
186
0
        }
187
30.3k
        ++sub_arg->pubcount;
188
30.3k
    }
189
30.3k
    if (sub_arg->prvenc != NULL
190
0
        && (p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY)) != NULL) {
191
0
        void *prv = sub_arg->prvenc + sub_arg->prvoff;
192
193
0
        if (OSSL_PARAM_get_octet_string(p, &prv, sub_arg->prvlen, &len) != 1)
194
0
            return 0;
195
0
        if (len != sub_arg->prvlen) {
196
0
            ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
197
0
                "Unexpected %s private key length %zu != %zu",
198
0
                sub_arg->algorithm_name, len, sub_arg->prvlen);
199
0
            return 0;
200
0
        }
201
0
        ++sub_arg->prvcount;
202
0
    }
203
30.3k
    return 1;
204
30.3k
}
205
206
static int
207
export_sub(EXPORT_CB_ARG *sub_arg, int selection, MLX_KEY *key)
208
87.5k
{
209
87.5k
    int slot;
210
211
    /*
212
     * The caller is responsible for initialising only the pubenc and prvenc
213
     * pointer fields, the rest are set here or in the callback.
214
     */
215
87.5k
    sub_arg->pubcount = 0;
216
87.5k
    sub_arg->prvcount = 0;
217
218
262k
    for (slot = 0; slot < 2; ++slot) {
219
175k
        int ml_kem_slot = key->xinfo->ml_kem_slot;
220
175k
        EVP_PKEY *pkey;
221
222
        /* Export the parts of each component into its storage slot */
223
175k
        if (slot == ml_kem_slot) {
224
87.5k
            pkey = key->mkey;
225
87.5k
            sub_arg->algorithm_name = key->minfo->algorithm_name;
226
87.5k
            sub_arg->puboff = slot * key->xinfo->pubkey_bytes;
227
87.5k
            sub_arg->prvoff = slot * key->xinfo->prvkey_bytes;
228
87.5k
            sub_arg->publen = key->minfo->pubkey_bytes;
229
87.5k
            sub_arg->prvlen = key->minfo->prvkey_bytes;
230
87.5k
        } else {
231
87.5k
            pkey = key->xkey;
232
87.5k
            sub_arg->algorithm_name = key->xinfo->algorithm_name;
233
87.5k
            sub_arg->puboff = (1 - ml_kem_slot) * key->minfo->pubkey_bytes;
234
87.5k
            sub_arg->prvoff = (1 - ml_kem_slot) * key->minfo->prvkey_bytes;
235
87.5k
            sub_arg->publen = key->xinfo->pubkey_bytes;
236
87.5k
            sub_arg->prvlen = key->xinfo->prvkey_bytes;
237
87.5k
        }
238
175k
        if (!EVP_PKEY_export(pkey, selection, export_sub_cb, (void *)sub_arg))
239
0
            return 0;
240
175k
    }
241
87.5k
    return 1;
242
87.5k
}
243
244
static int mlx_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb,
245
    void *cbarg)
246
0
{
247
0
    MLX_KEY *key = vkey;
248
0
    OSSL_PARAM_BLD *tmpl = NULL;
249
0
    OSSL_PARAM *params = NULL, *p;
250
0
    size_t publen;
251
0
    size_t prvlen;
252
0
    int ret = 0;
253
0
    EXPORT_CB_ARG sub_arg;
254
255
0
    if (!ossl_prov_is_running() || key == NULL)
256
0
        return 0;
257
258
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
259
0
        return 0;
260
261
    /* Fail when no key material has yet been provided */
262
0
    if (!mlx_kem_have_pubkey(key)) {
263
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
264
0
        return 0;
265
0
    }
266
0
    publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
267
0
    prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
268
0
    memset(&sub_arg, 0, sizeof(sub_arg));
269
270
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
271
0
        sub_arg.pubenc = OPENSSL_malloc(publen);
272
0
        if (sub_arg.pubenc == NULL)
273
0
            goto err;
274
0
    }
275
276
0
    if (mlx_kem_have_prvkey(key)
277
0
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
278
        /*
279
         * Allocated on the secure heap if configured, this is detected in
280
         * ossl_param_build_set_octet_string(), which will then also use the
281
         * secure heap.
282
         */
283
0
        sub_arg.prvenc = OPENSSL_secure_zalloc(prvlen);
284
0
        if (sub_arg.prvenc == NULL)
285
0
            goto err;
286
0
    }
287
288
0
    tmpl = OSSL_PARAM_BLD_new();
289
0
    if (tmpl == NULL)
290
0
        goto err;
291
292
    /* Extract sub-component key material */
293
0
    if (!export_sub(&sub_arg, selection, key))
294
0
        goto err;
295
296
0
    if (sub_arg.pubenc != NULL && sub_arg.pubcount == 2
297
0
        && !ossl_param_build_set_octet_string(
298
0
            tmpl, NULL, OSSL_PKEY_PARAM_PUB_KEY, sub_arg.pubenc, publen))
299
0
        goto err;
300
301
0
    if (sub_arg.prvenc != NULL && sub_arg.prvcount == 2
302
0
        && !ossl_param_build_set_octet_string(
303
0
            tmpl, NULL, OSSL_PKEY_PARAM_PRIV_KEY, sub_arg.prvenc, prvlen))
304
0
        goto err;
305
306
0
    params = OSSL_PARAM_BLD_to_param(tmpl);
307
0
    if (params == NULL)
308
0
        goto err;
309
310
0
    ret = param_cb(params, cbarg);
311
    /*
312
     * OSSL_PARAM_free() only wipes the secure-heap data block,
313
     * so wipe the key material copies held in the params first.
314
     */
315
0
    for (p = params; p->key != NULL; p++)
316
0
        OPENSSL_cleanse(p->data, p->data_size);
317
0
    OSSL_PARAM_free(params);
318
319
0
err:
320
0
    OSSL_PARAM_BLD_free(tmpl);
321
0
    OPENSSL_secure_clear_free(sub_arg.prvenc, prvlen);
322
0
    OPENSSL_clear_free(sub_arg.pubenc, publen);
323
0
    return ret;
324
0
}
325
326
static const OSSL_PARAM *mlx_kem_imexport_types(int selection)
327
0
{
328
0
    static const OSSL_PARAM key_types[] = {
329
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
330
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
331
0
        OSSL_PARAM_END
332
0
    };
333
334
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
335
0
        return key_types;
336
0
    return NULL;
337
0
}
338
339
static int
340
load_slot(OSSL_LIB_CTX *libctx, const char *propq, const char *pname,
341
    int selection, MLX_KEY *key, int slot, const uint8_t *in,
342
    int mbytes, int xbytes)
343
45
{
344
45
    EVP_PKEY_CTX *ctx;
345
45
    EVP_PKEY **ppkey;
346
45
    OSSL_PARAM parr[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
347
45
    const char *alg;
348
45
    char *group = NULL;
349
45
    size_t off, len;
350
45
    void *val;
351
45
    int ml_kem_slot = key->xinfo->ml_kem_slot;
352
45
    int ret = 0;
353
354
45
    if (slot == ml_kem_slot) {
355
26
        alg = key->minfo->algorithm_name;
356
26
        ppkey = &key->mkey;
357
26
        off = slot * xbytes;
358
26
        len = mbytes;
359
26
    } else {
360
19
        alg = key->xinfo->algorithm_name;
361
19
        group = (char *)key->xinfo->group_name;
362
19
        ppkey = &key->xkey;
363
19
        off = (1 - ml_kem_slot) * mbytes;
364
19
        len = xbytes;
365
19
    }
366
45
    val = (void *)(in + off);
367
368
45
    if ((ctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propq)) == NULL
369
45
        || EVP_PKEY_fromdata_init(ctx) <= 0)
370
0
        goto err;
371
45
    parr[0] = OSSL_PARAM_construct_octet_string(pname, val, len);
372
45
    if (group != NULL)
373
0
        parr[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
374
0
            group, 0);
375
45
    if (EVP_PKEY_fromdata(ctx, ppkey, selection, parr) > 0)
376
38
        ret = 1;
377
378
45
err:
379
45
    EVP_PKEY_CTX_free(ctx);
380
45
    return ret;
381
45
}
382
383
static int
384
load_keys(MLX_KEY *key,
385
    const uint8_t *pubenc, size_t publen,
386
    const uint8_t *prvenc, size_t prvlen)
387
26
{
388
26
    int slot;
389
390
64
    for (slot = 0; slot < 2; ++slot) {
391
45
        if (prvlen) {
392
            /* Ignore public keys when private provided */
393
0
            if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PRIV_KEY,
394
0
                    minimal_selection, key, slot, prvenc,
395
0
                    key->minfo->prvkey_bytes, key->xinfo->prvkey_bytes))
396
0
                goto err;
397
45
        } else if (publen) {
398
            /* Absent private key data, import public keys */
399
45
            if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PUB_KEY,
400
45
                    minimal_selection, key, slot, pubenc,
401
45
                    key->minfo->pubkey_bytes, key->xinfo->pubkey_bytes))
402
7
                goto err;
403
45
        }
404
45
    }
405
19
    key->state = prvlen ? MLX_HAVE_PRVKEY : MLX_HAVE_PUBKEY;
406
19
    return 1;
407
408
7
err:
409
7
    EVP_PKEY_free(key->mkey);
410
7
    EVP_PKEY_free(key->xkey);
411
7
    key->xkey = key->mkey = NULL;
412
7
    key->state = MLX_HAVE_NOKEYS;
413
7
    return 0;
414
26
}
415
416
static int mlx_kem_key_fromdata(MLX_KEY *key,
417
    const OSSL_PARAM params[],
418
    int include_private)
419
0
{
420
0
    const OSSL_PARAM *param_prv_key = NULL, *param_pub_key;
421
0
    const void *pubenc = NULL, *prvenc = NULL;
422
0
    size_t pubkey_bytes, prvkey_bytes;
423
0
    size_t publen = 0, prvlen = 0;
424
425
    /* Invalid attempt to mutate a key, what is the right error to report? */
426
0
    if (key == NULL || mlx_kem_have_pubkey(key))
427
0
        return 0;
428
0
    pubkey_bytes = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
429
0
    prvkey_bytes = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
430
431
    /* What does the caller want to set? */
432
0
    param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
433
0
    if (param_pub_key != NULL && OSSL_PARAM_get_octet_string_ptr(param_pub_key, &pubenc, &publen) != 1)
434
0
        return 0;
435
0
    if (include_private)
436
0
        param_prv_key = OSSL_PARAM_locate_const(params,
437
0
            OSSL_PKEY_PARAM_PRIV_KEY);
438
0
    if (param_prv_key != NULL && OSSL_PARAM_get_octet_string_ptr(param_prv_key, &prvenc, &prvlen) != 1)
439
0
        return 0;
440
441
    /* The caller MUST specify at least one of the public or private keys. */
442
0
    if (publen == 0 && prvlen == 0) {
443
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
444
0
        return 0;
445
0
    }
446
447
    /*
448
     * When a pubkey is provided, its length MUST be correct, if a private key
449
     * is also provided, the public key will be otherwise ignored.  We could
450
     * look for a matching encoded block, but unclear this is useful.
451
     */
452
0
    if (publen != 0 && publen != pubkey_bytes) {
453
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
454
0
        return 0;
455
0
    }
456
0
    if (prvlen != 0 && prvlen != prvkey_bytes) {
457
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
458
0
        return 0;
459
0
    }
460
461
0
    return load_keys(key, pubenc, publen, prvenc, prvlen);
462
0
}
463
464
static int mlx_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
465
0
{
466
0
    MLX_KEY *key = vkey;
467
0
    int include_private;
468
469
0
    if (!ossl_prov_is_running() || key == NULL)
470
0
        return 0;
471
472
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
473
0
        return 0;
474
475
0
    include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
476
0
    return mlx_kem_key_fromdata(key, params, include_private);
477
0
}
478
479
static const OSSL_PARAM *mlx_kem_gettable_params(void *provctx)
480
0
{
481
0
    static const OSSL_PARAM arr[] = {
482
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
483
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
484
0
        OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
485
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
486
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
487
0
        OSSL_PARAM_END
488
0
    };
489
490
0
    return arr;
491
0
}
492
493
/*
494
 * It is assumed the key is guaranteed non-NULL here, and is from this provider
495
 */
496
static int mlx_kem_get_params(void *vkey, OSSL_PARAM params[])
497
45.4k
{
498
45.4k
    MLX_KEY *key = vkey;
499
45.4k
    OSSL_PARAM *p, *pub, *prv = NULL;
500
45.4k
    EXPORT_CB_ARG sub_arg;
501
45.4k
    int selection;
502
45.4k
    size_t publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
503
45.4k
    size_t prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
504
505
    /* The reported "bit" count is those of the ML-KEM key */
506
45.4k
    p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS);
507
45.4k
    if (p != NULL)
508
15.1k
        if (!OSSL_PARAM_set_int(p, key->minfo->bits))
509
0
            return 0;
510
511
    /* The reported security bits are those of the ML-KEM key */
512
45.4k
    p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS);
513
45.4k
    if (p != NULL)
514
15.1k
        if (!OSSL_PARAM_set_int(p, key->minfo->secbits))
515
0
            return 0;
516
517
    /* The ciphertext sizes are additive */
518
45.4k
    p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE);
519
45.4k
    if (p != NULL)
520
15.1k
        if (!OSSL_PARAM_set_int(p, key->minfo->ctext_bytes + key->xinfo->pubkey_bytes))
521
0
            return 0;
522
523
45.4k
    if (!mlx_kem_have_pubkey(key))
524
1
        return 1;
525
526
45.4k
    memset(&sub_arg, 0, sizeof(sub_arg));
527
45.4k
    pub = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
528
45.4k
    if (pub != NULL) {
529
30.3k
        if (pub->data_type != OSSL_PARAM_OCTET_STRING)
530
0
            return 0;
531
30.3k
        pub->return_size = publen;
532
30.3k
        if (pub->data == NULL) {
533
15.1k
            pub = NULL;
534
15.1k
        } else if (pub->data_size < publen) {
535
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
536
0
                "public key output buffer too short: %lu < %lu",
537
0
                (unsigned long)pub->data_size,
538
0
                (unsigned long)publen);
539
0
            return 0;
540
15.1k
        } else {
541
15.1k
            sub_arg.pubenc = pub->data;
542
15.1k
        }
543
30.3k
    }
544
45.4k
    if (mlx_kem_have_prvkey(key)) {
545
45.4k
        prv = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
546
45.4k
        if (prv != NULL) {
547
0
            if (prv->data_type != OSSL_PARAM_OCTET_STRING)
548
0
                return 0;
549
0
            prv->return_size = prvlen;
550
0
            if (prv->data == NULL) {
551
0
                prv = NULL;
552
0
            } else if (prv->data_size < prvlen) {
553
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
554
0
                    "private key output buffer too short: %lu < %lu",
555
0
                    (unsigned long)prv->data_size,
556
0
                    (unsigned long)prvlen);
557
0
                return 0;
558
0
            } else {
559
0
                sub_arg.prvenc = prv->data;
560
0
            }
561
0
        }
562
45.4k
    }
563
45.4k
    if (pub == NULL && prv == NULL)
564
30.3k
        return 1;
565
566
15.1k
    selection = prv == NULL ? 0 : OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
567
15.1k
    selection |= pub == NULL ? 0 : OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
568
15.1k
    if (key->xinfo->group_name != NULL)
569
0
        selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
570
571
    /* Extract sub-component key material */
572
15.1k
    if (!export_sub(&sub_arg, selection, key)
573
15.1k
        || (pub != NULL && sub_arg.pubcount != 2)
574
15.1k
        || (prv != NULL && sub_arg.prvcount != 2)) {
575
        /* Erase any partial key material on failure */
576
0
        if (sub_arg.pubenc != NULL)
577
0
            OPENSSL_cleanse(sub_arg.pubenc,
578
0
                key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes);
579
0
        if (sub_arg.prvenc != NULL)
580
0
            OPENSSL_cleanse(sub_arg.prvenc,
581
0
                key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes);
582
0
        return 0;
583
0
    }
584
585
15.1k
    return 1;
586
15.1k
}
587
588
static const OSSL_PARAM *mlx_kem_settable_params(void *provctx)
589
0
{
590
0
    static const OSSL_PARAM arr[] = {
591
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
592
0
        OSSL_PARAM_END
593
0
    };
594
595
0
    return arr;
596
0
}
597
598
static int mlx_kem_set_params(void *vkey, const OSSL_PARAM params[])
599
1
{
600
1
    MLX_KEY *key = vkey;
601
1
    const OSSL_PARAM *p;
602
1
    const void *pubenc = NULL;
603
1
    size_t publen = 0;
604
605
1
    if (ossl_param_is_empty(params))
606
0
        return 1;
607
608
    /* Only one settable parameter is supported */
609
1
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
610
1
    if (p == NULL)
611
0
        return 1;
612
613
    /* Key mutation is reportedly generally not allowed */
614
1
    if (mlx_kem_have_pubkey(key)) {
615
0
        ERR_raise_data(ERR_LIB_PROV,
616
0
            PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
617
0
            "keys cannot be mutated");
618
0
        return 0;
619
0
    }
620
    /* An unlikely failure mode is the parameter having some unexpected type */
621
1
    if (!OSSL_PARAM_get_octet_string_ptr(p, &pubenc, &publen))
622
0
        return 0;
623
624
1
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
625
1
    if (p != NULL) {
626
0
        OPENSSL_free(key->propq);
627
0
        key->propq = NULL;
628
0
        if (!OSSL_PARAM_get_utf8_string(p, &key->propq, 0))
629
0
            return 0;
630
0
    }
631
632
1
    if (publen != key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes) {
633
1
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
634
1
        return 0;
635
1
    }
636
637
0
    return load_keys(key, pubenc, publen, NULL, 0);
638
1
}
639
640
static int mlx_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
641
30.3k
{
642
30.3k
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
643
30.3k
    const OSSL_PARAM *p;
644
645
30.3k
    if (gctx == NULL)
646
0
        return 0;
647
30.3k
    if (ossl_param_is_empty(params))
648
15.1k
        return 1;
649
650
15.1k
    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
651
15.1k
    if (p != NULL) {
652
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
653
0
            return 0;
654
0
        OPENSSL_free(gctx->propq);
655
0
        if ((gctx->propq = OPENSSL_strdup(p->data)) == NULL)
656
0
            return 0;
657
0
    }
658
15.1k
    return 1;
659
15.1k
}
660
661
static void *mlx_kem_gen_init(int evp_type, OSSL_LIB_CTX *libctx,
662
    int selection, const OSSL_PARAM params[])
663
86.0k
{
664
86.0k
    PROV_ML_KEM_GEN_CTX *gctx = NULL;
665
666
    /*
667
     * We can only generate private keys, check that the selection is
668
     * appropriate.
669
     */
670
86.0k
    if (!ossl_prov_is_running()
671
86.0k
        || (selection & minimal_selection) == 0
672
86.0k
        || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
673
0
        return NULL;
674
675
86.0k
    gctx->evp_type = evp_type;
676
86.0k
    gctx->libctx = libctx;
677
86.0k
    gctx->selection = selection;
678
86.0k
    if (mlx_kem_gen_set_params(gctx, params))
679
86.0k
        return gctx;
680
681
0
    mlx_kem_gen_cleanup(gctx);
682
0
    return NULL;
683
86.0k
}
684
685
static const OSSL_PARAM *mlx_kem_gen_settable_params(ossl_unused void *vgctx,
686
    ossl_unused void *provctx)
687
0
{
688
0
    static OSSL_PARAM settable[] = {
689
0
        OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
690
0
        OSSL_PARAM_END
691
0
    };
692
693
0
    return settable;
694
0
}
695
696
#ifdef FIPS_MODULE
697
static int mlx_kem_gen_get_params(void *vgctx, OSSL_PARAM params[])
698
{
699
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
700
    OSSL_PARAM *p;
701
    int approved;
702
703
    if (gctx == NULL || gctx->evp_type >= OSSL_NELEM(hybrid_vtable))
704
        return 0;
705
    p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_FIPS_APPROVED_INDICATOR);
706
    if (p != NULL) {
707
        approved = strcmp(hybrid_vtable[gctx->evp_type].algorithm_name,
708
                       SN_X448)
709
            != 0;
710
        if (!OSSL_PARAM_set_int(p, approved))
711
            return 0;
712
    }
713
    return 1;
714
}
715
#endif
716
717
static void *mlx_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
718
86.0k
{
719
86.0k
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
720
86.0k
    MLX_KEY *key;
721
86.0k
    char *propq;
722
723
86.0k
    if (gctx == NULL
724
86.0k
        || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
725
0
        return NULL;
726
727
    /* Lose ownership of propq */
728
86.0k
    propq = gctx->propq;
729
86.0k
    gctx->propq = NULL;
730
86.0k
    if ((key = mlx_kem_key_new(gctx->evp_type, gctx->libctx, propq)) == NULL)
731
0
        return NULL;
732
733
86.0k
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
734
37
        return key;
735
736
    /* For now, using the same "propq" for all components */
737
86.0k
    key->mkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
738
86.0k
        key->minfo->algorithm_name);
739
86.0k
    key->xkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
740
86.0k
        key->xinfo->algorithm_name,
741
86.0k
        key->xinfo->group_name);
742
86.0k
    if (key->mkey != NULL && key->xkey != NULL) {
743
86.0k
        key->state = MLX_HAVE_PRVKEY;
744
86.0k
        return key;
745
86.0k
    }
746
747
0
    mlx_kem_key_free(key);
748
0
    return NULL;
749
86.0k
}
750
751
static void mlx_kem_gen_cleanup(void *vgctx)
752
86.0k
{
753
86.0k
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
754
755
86.0k
    if (gctx == NULL)
756
0
        return;
757
86.0k
    OPENSSL_free(gctx->propq);
758
86.0k
    OPENSSL_free(gctx);
759
86.0k
}
760
761
static void *mlx_kem_dup(const void *vkey, int selection)
762
0
{
763
0
    const MLX_KEY *key = vkey;
764
0
    MLX_KEY *ret;
765
766
0
    if (!ossl_prov_is_running()
767
0
        || (ret = OPENSSL_memdup(key, sizeof(*ret))) == NULL)
768
0
        return NULL;
769
770
0
    ret->mkey = ret->xkey = NULL;
771
772
0
    if (key->propq != NULL
773
0
        && (ret->propq = OPENSSL_strdup(key->propq)) == NULL) {
774
0
        OPENSSL_free(ret);
775
0
        return NULL;
776
0
    }
777
778
    /* Absent key material, nothing left to do */
779
0
    if (key->mkey == NULL) {
780
0
        if (key->xkey == NULL)
781
0
            return ret;
782
        /* Fail if the source key is an inconsistent state */
783
0
        OPENSSL_free(ret->propq);
784
0
        OPENSSL_free(ret);
785
0
        return NULL;
786
0
    }
787
788
0
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
789
0
    case 0:
790
0
        ret->state = MLX_HAVE_NOKEYS;
791
0
        return ret;
792
0
    case OSSL_KEYMGMT_SELECT_KEYPAIR:
793
0
        ret->mkey = EVP_PKEY_dup(key->mkey);
794
0
        ret->xkey = EVP_PKEY_dup(key->xkey);
795
0
        if (ret->xkey != NULL && ret->mkey != NULL)
796
0
            return ret;
797
0
        break;
798
0
    default:
799
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_UNSUPPORTED_SELECTION,
800
0
            "duplication of partial key material not supported");
801
0
        break;
802
0
    }
803
804
0
    mlx_kem_key_free(ret);
805
0
    return NULL;
806
0
}
807
808
#define DECLARE_DISPATCH(name, variant)                                                    \
809
    static OSSL_FUNC_keymgmt_new_fn mlx_##name##_kem_new;                                  \
810
    static void *mlx_##name##_kem_new(void *provctx)                                       \
811
0
    {                                                                                      \
812
0
        OSSL_LIB_CTX *libctx;                                                              \
813
0
                                                                                           \
814
0
        libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx);                         \
815
0
        return mlx_kem_key_new(variant, libctx, NULL);                                     \
816
0
    }                                                                                      \
817
    static OSSL_FUNC_keymgmt_gen_init_fn mlx_##name##_kem_gen_init;                        \
818
    static void *mlx_##name##_kem_gen_init(void *provctx, int selection,                   \
819
        const OSSL_PARAM params[])                                                         \
820
86.0k
    {                                                                                      \
821
86.0k
        OSSL_LIB_CTX *libctx;                                                              \
822
86.0k
                                                                                           \
823
86.0k
        libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx);                         \
824
86.0k
        return mlx_kem_gen_init(variant, libctx, selection, params);                       \
825
86.0k
    }                                                                                      \
826
    const OSSL_DISPATCH ossl_mlx_##name##_kem_kmgmt_functions[] = {                        \
827
        { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC)mlx_##name##_kem_new },                        \
828
        { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC)mlx_kem_key_free },                           \
829
        { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC)mlx_kem_get_params },                   \
830
        { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC)mlx_kem_gettable_params },         \
831
        { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC)mlx_kem_set_params },                   \
832
        { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC)mlx_kem_settable_params },         \
833
        { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC)mlx_kem_has },                                 \
834
        { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC)mlx_kem_match },                             \
835
        { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC)mlx_##name##_kem_gen_init },              \
836
        { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC)mlx_kem_gen_set_params },           \
837
        { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC)mlx_kem_gen_settable_params }, \
838
        { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC)mlx_kem_gen },                                 \
839
        { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC)mlx_kem_gen_cleanup },                 \
840
        { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC)mlx_kem_dup },                                 \
841
        { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC)mlx_kem_import },                           \
842
        { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC)mlx_kem_imexport_types },             \
843
        { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC)mlx_kem_export },                           \
844
        { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC)mlx_kem_imexport_types },             \
845
        OSSL_FIPS_IND_DISPATCH(OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS,                           \
846
            OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS, mlx_kem_gen_get_params)                 \
847
            OSSL_DISPATCH_END                                                              \
848
    }
849
/* See |hybrid_vtable| above */
850
5
DECLARE_DISPATCH(p256, 0);
Unexecuted instantiation: mlx_kmgmt.c:mlx_p256_kem_new
mlx_kmgmt.c:mlx_p256_kem_gen_init
Line
Count
Source
850
DECLARE_DISPATCH(p256, 0);
851
0
DECLARE_DISPATCH(p384, 1);
Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_new
Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_gen_init
852
#if !defined(OPENSSL_NO_ECX)
853
86.0k
DECLARE_DISPATCH(x25519, 2);
Unexecuted instantiation: mlx_kmgmt.c:mlx_x25519_kem_new
mlx_kmgmt.c:mlx_x25519_kem_gen_init
Line
Count
Source
853
DECLARE_DISPATCH(x25519, 2);
854
DECLARE_DISPATCH(x448, 3);
Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_new
Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_gen_init
855
#endif