Coverage Report

Created: 2025-12-10 06:24

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