Coverage Report

Created: 2026-02-22 06:11

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