Coverage Report

Created: 2025-10-28 06:56

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 &&
425
0
        OSSL_PARAM_get_octet_string_ptr(p.pubkey, &pubenc, &publen) != 1)
426
0
        return 0;
427
0
    if (include_private
428
0
            && p.privkey != NULL
429
0
            && OSSL_PARAM_get_octet_string_ptr(p.privkey, &prvenc, &prvlen) != 1)
430
0
        return 0;
431
432
    /* The caller MUST specify at least one of the public or private keys. */
433
0
    if (publen == 0 && prvlen == 0) {
434
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
435
0
        return 0;
436
0
    }
437
438
    /*
439
     * When a pubkey is provided, its length MUST be correct, if a private key
440
     * is also provided, the public key will be otherwise ignored.  We could
441
     * look for a matching encoded block, but unclear this is useful.
442
     */
443
0
    if (publen != 0 && publen != pubkey_bytes) {
444
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
445
0
        return 0;
446
0
    }
447
0
    if (prvlen != 0 && prvlen != prvkey_bytes) {
448
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
449
0
        return 0;
450
0
    }
451
452
0
    return load_keys(key, pubenc, publen, prvenc, prvlen);
453
0
}
454
455
static int mlx_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
456
0
{
457
0
    MLX_KEY *key = vkey;
458
0
    int include_private;
459
460
0
    if (!ossl_prov_is_running() || key == NULL)
461
0
        return 0;
462
463
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
464
0
        return 0;
465
466
0
    include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
467
0
    return mlx_kem_key_fromdata(key, params, include_private);
468
0
}
469
470
static const OSSL_PARAM *mlx_kem_gettable_params(void *provctx)
471
0
{
472
0
    return mlx_get_params_list;
473
0
}
474
475
/*
476
 * It is assumed the key is guaranteed non-NULL here, and is from this provider
477
 */
478
static int mlx_kem_get_params(void *vkey, OSSL_PARAM params[])
479
0
{
480
0
    MLX_KEY *key = vkey;
481
0
    OSSL_PARAM *pub, *prv = NULL;
482
0
    EXPORT_CB_ARG sub_arg;
483
0
    int selection;
484
0
    struct mlx_get_params_st p;
485
486
0
    if (key == NULL || !mlx_get_params_decoder(params, &p))
487
0
        return 0;
488
489
    /* The reported "bit" count is those of the ML-KEM key */
490
0
    if (p.bits != NULL)
491
0
        if (!OSSL_PARAM_set_int(p.bits, key->minfo->bits))
492
0
            return 0;
493
494
    /* The reported security bits are those of the ML-KEM key */
495
0
    if (p.secbits != NULL)
496
0
        if (!OSSL_PARAM_set_int(p.secbits, key->minfo->secbits))
497
0
            return 0;
498
499
    /* The reported security category are those of the ML-KEM key */
500
0
    if (p.seccat != NULL)
501
0
        if (!OSSL_PARAM_set_int(p.seccat, key->minfo->security_category))
502
0
            return 0;
503
504
    /* The ciphertext sizes are additive */
505
0
    if (p.maxsize != NULL)
506
0
        if (!OSSL_PARAM_set_size_t(p.maxsize, key->minfo->ctext_bytes + key->xinfo->pubkey_bytes))
507
0
            return 0;
508
509
0
    if (!mlx_kem_have_pubkey(key))
510
0
        return 1;
511
512
0
    memset(&sub_arg, 0, sizeof(sub_arg));
513
0
    if ((pub = p.pub) != NULL) {
514
0
        size_t publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
515
516
0
        if (pub->data_type != OSSL_PARAM_OCTET_STRING)
517
0
            return 0;
518
0
        pub->return_size = publen;
519
0
        if (pub->data == NULL) {
520
0
            pub = NULL;
521
0
        } else if (pub->data_size < publen) {
522
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
523
0
                           "public key output buffer too short: %lu < %lu",
524
0
                           (unsigned long) pub->data_size,
525
0
                           (unsigned long) publen);
526
0
            return 0;
527
0
        } else {
528
0
            sub_arg.pubenc = pub->data;
529
0
        }
530
0
    }
531
0
    if (mlx_kem_have_prvkey(key)) {
532
0
        if ((prv = p.priv) != NULL) {
533
0
            size_t prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
534
535
0
            if (prv->data_type != OSSL_PARAM_OCTET_STRING)
536
0
                return 0;
537
0
            prv->return_size = prvlen;
538
0
            if (prv->data == NULL) {
539
0
                prv = NULL;
540
0
            } else if (prv->data_size < prvlen) {
541
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
542
0
                               "private key output buffer too short: %lu < %lu",
543
0
                               (unsigned long) prv->data_size,
544
0
                               (unsigned long) prvlen);
545
0
                return 0;
546
0
            } else {
547
0
                sub_arg.prvenc = prv->data;
548
0
            }
549
0
        }
550
0
    }
551
0
    if (pub == NULL && prv == NULL)
552
0
        return 1;
553
554
0
    selection = prv == NULL ? 0 : OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
555
0
    selection |= pub == NULL ? 0 : OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
556
0
    if (key->xinfo->group_name != NULL)
557
0
        selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
558
559
    /* Extract sub-component key material */
560
0
    if (!export_sub(&sub_arg, selection, key))
561
0
        return 0;
562
563
0
    if ((pub != NULL && sub_arg.pubcount != 2)
564
0
        || (prv != NULL && sub_arg.prvcount != 2))
565
0
        return 0;
566
567
0
    return 1;
568
0
}
569
570
static const OSSL_PARAM *mlx_kem_settable_params(void *provctx)
571
0
{
572
0
    return mlx_set_params_list;
573
0
}
574
575
static int mlx_kem_set_params(void *vkey, const OSSL_PARAM params[])
576
0
{
577
0
    MLX_KEY *key = vkey;
578
0
    struct mlx_set_params_st p;
579
0
    const void *pubenc = NULL;
580
0
    size_t publen = 0;
581
582
0
    if (key == NULL || !mlx_set_params_decoder(params, &p))
583
0
        return 0;
584
585
0
    if (p.propq != NULL) {
586
0
        OPENSSL_free(key->propq);
587
0
        key->propq = NULL;
588
0
        if (!OSSL_PARAM_get_utf8_string(p.propq, &key->propq, 0))
589
0
            return 0;
590
0
    }
591
592
0
    if (p.pub == NULL)
593
0
        return 1;
594
595
    /* Key mutation is reportedly generally not allowed */
596
0
    if (mlx_kem_have_pubkey(key)) {
597
0
        ERR_raise_data(ERR_LIB_PROV,
598
0
                       PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
599
0
                       "keys cannot be mutated");
600
0
        return 0;
601
0
    }
602
    /* An unlikely failure mode is the parameter having some unexpected type */
603
0
    if (!OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen))
604
0
        return 0;
605
606
0
    if (publen != key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes) {
607
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
608
0
        return 0;
609
0
    }
610
611
0
    return load_keys(key, pubenc, publen, NULL, 0);
612
0
}
613
614
static int mlx_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
615
0
{
616
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
617
0
    struct mlx_gen_set_params_st p;
618
619
0
    if (gctx == NULL || !mlx_gen_set_params_decoder(params, &p))
620
0
        return 0;
621
622
0
    if (p.propq != NULL) {
623
0
        if (p.propq->data_type != OSSL_PARAM_UTF8_STRING)
624
0
            return 0;
625
0
        OPENSSL_free(gctx->propq);
626
0
        if ((gctx->propq = OPENSSL_strdup(p.propq->data)) == NULL)
627
0
            return 0;
628
0
    }
629
0
    return 1;
630
0
}
631
632
static void *mlx_kem_gen_init(int evp_type, OSSL_LIB_CTX *libctx,
633
                              int selection, const OSSL_PARAM params[])
634
0
{
635
0
    PROV_ML_KEM_GEN_CTX *gctx = NULL;
636
637
    /*
638
     * We can only generate private keys, check that the selection is
639
     * appropriate.
640
     */
641
0
    if (!ossl_prov_is_running()
642
0
        || (selection & minimal_selection) == 0
643
0
        || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
644
0
        return NULL;
645
646
0
    gctx->evp_type = evp_type;
647
0
    gctx->libctx = libctx;
648
0
    gctx->selection = selection;
649
0
    if (mlx_kem_gen_set_params(gctx, params))
650
0
        return gctx;
651
652
0
    mlx_kem_gen_cleanup(gctx);
653
0
    return NULL;
654
0
}
655
656
static const OSSL_PARAM *mlx_kem_gen_settable_params(ossl_unused void *vgctx,
657
                                                     ossl_unused void *provctx)
658
0
{
659
0
    return mlx_gen_set_params_list;
660
0
}
661
662
static void *mlx_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
663
0
{
664
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
665
0
    MLX_KEY *key;
666
0
    char *propq;
667
668
0
    if (gctx == NULL
669
0
        || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
670
0
            OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
671
0
        return NULL;
672
673
    /* Lose ownership of propq */
674
0
    propq = gctx->propq;
675
0
    gctx->propq = NULL;
676
0
    if ((key = mlx_kem_key_new(gctx->evp_type, gctx->libctx, propq)) == NULL)
677
0
        return NULL;
678
679
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
680
0
        return key;
681
682
    /* For now, using the same "propq" for all components */
683
0
    key->mkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
684
0
                                  key->minfo->algorithm_name);
685
0
    key->xkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
686
0
                                  key->xinfo->algorithm_name,
687
0
                                  key->xinfo->group_name);
688
0
    if (key->mkey != NULL && key->xkey != NULL) {
689
0
        key->state = MLX_HAVE_PRVKEY;
690
0
        return key;
691
0
    }
692
693
0
    mlx_kem_key_free(key);
694
0
    return NULL;
695
0
}
696
697
static void mlx_kem_gen_cleanup(void *vgctx)
698
0
{
699
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
700
701
0
    if (gctx == NULL)
702
0
        return;
703
0
    OPENSSL_free(gctx->propq);
704
0
    OPENSSL_free(gctx);
705
0
}
706
707
static void *mlx_kem_dup(const void *vkey, int selection)
708
0
{
709
0
    const MLX_KEY *key = vkey;
710
0
    MLX_KEY *ret;
711
712
0
    if (!ossl_prov_is_running()
713
0
        || (ret = OPENSSL_memdup(key, sizeof(*ret))) == NULL)
714
0
        return NULL;
715
716
0
    if (ret->propq != NULL
717
0
        && (ret->propq = OPENSSL_strdup(ret->propq)) == NULL) {
718
0
        OPENSSL_free(ret);
719
0
        return NULL;
720
0
    }
721
722
    /* Absent key material, nothing left to do */
723
0
    if (ret->mkey == NULL) {
724
0
        if (ret->xkey == NULL)
725
0
            return ret;
726
        /* Fail if the source key is an inconsistent state */
727
0
        OPENSSL_free(ret);
728
0
        return NULL;
729
0
    }
730
731
0
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
732
0
    case 0:
733
0
        ret->xkey = ret->mkey = NULL;
734
0
        return ret;
735
0
    case OSSL_KEYMGMT_SELECT_KEYPAIR:
736
0
        ret->mkey = EVP_PKEY_dup(key->mkey);
737
0
        ret->xkey = EVP_PKEY_dup(key->xkey);
738
0
        if (ret->xkey != NULL && ret->mkey != NULL)
739
0
            return ret;
740
0
        break;
741
0
    default:
742
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_UNSUPPORTED_SELECTION,
743
0
                       "duplication of partial key material not supported");
744
0
        break;
745
0
    }
746
747
0
    mlx_kem_key_free(ret);
748
0
    return NULL;
749
0
}
750
751
#define DECLARE_DISPATCH(name, variant) \
752
    static OSSL_FUNC_keymgmt_new_fn mlx_##name##_kem_new; \
753
    static void *mlx_##name##_kem_new(void *provctx) \
754
0
    { \
755
0
        OSSL_LIB_CTX *libctx; \
756
0
                              \
757
0
        libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \
758
0
        return mlx_kem_key_new(variant, libctx, NULL); \
759
0
    } \
760
    static OSSL_FUNC_keymgmt_gen_init_fn mlx_##name##_kem_gen_init; \
761
    static void *mlx_##name##_kem_gen_init(void *provctx, int selection, \
762
                                           const OSSL_PARAM params[]) \
763
0
    { \
764
0
        OSSL_LIB_CTX *libctx; \
765
0
                              \
766
0
        libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \
767
0
        return mlx_kem_gen_init(variant, libctx, selection, params); \
768
0
    } \
769
    const OSSL_DISPATCH ossl_mlx_##name##_kem_kmgmt_functions[] = { \
770
        { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) mlx_##name##_kem_new }, \
771
        { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) mlx_kem_key_free }, \
772
        { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) mlx_kem_get_params }, \
773
        { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_gettable_params }, \
774
        { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) mlx_kem_set_params }, \
775
        { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_settable_params }, \
776
        { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) mlx_kem_has }, \
777
        { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) mlx_kem_match }, \
778
        { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) mlx_##name##_kem_gen_init }, \
779
        { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) mlx_kem_gen_set_params }, \
780
        { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_gen_settable_params }, \
781
        { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) mlx_kem_gen }, \
782
        { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) mlx_kem_gen_cleanup }, \
783
        { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) mlx_kem_dup }, \
784
        { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) mlx_kem_import }, \
785
        { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) mlx_kem_imexport_types }, \
786
        { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) mlx_kem_export }, \
787
        { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) mlx_kem_imexport_types }, \
788
        OSSL_DISPATCH_END \
789
    }
790
/* See |hybrid_vtable| above */
791
0
DECLARE_DISPATCH(p256, 0);
Unexecuted instantiation: mlx_kmgmt.c:mlx_p256_kem_new
Unexecuted instantiation: mlx_kmgmt.c:mlx_p256_kem_gen_init
792
0
DECLARE_DISPATCH(p384, 1);
Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_new
Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_gen_init
793
#if !defined(OPENSSL_NO_ECX)
794
0
DECLARE_DISPATCH(x25519, 2);
Unexecuted instantiation: mlx_kmgmt.c:mlx_x25519_kem_new
Unexecuted instantiation: mlx_kmgmt.c:mlx_x25519_kem_gen_init
795
DECLARE_DISPATCH(x448, 3);
Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_new
Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_gen_init
796
#endif