Coverage Report

Created: 2026-09-12 06:55

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