Coverage Report

Created: 2026-10-28 06:28

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