Coverage Report

Created: 2025-08-28 07:07

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