Coverage Report

Created: 2025-07-11 06:57

/src/openssl/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
11
#include <openssl/core_dispatch.h>
12
#include <openssl/core_names.h>
13
#include <openssl/err.h>
14
#include <openssl/param_build.h>
15
#include <openssl/params.h>
16
#include <openssl/proverr.h>
17
#include <openssl/rand.h>
18
#include <openssl/self_test.h>
19
#include "internal/nelem.h"
20
#include "internal/param_build_set.h"
21
#include "prov/implementations.h"
22
#include "prov/mlx_kem.h"
23
#include "prov/provider_ctx.h"
24
#include "prov/providercommon.h"
25
#include "prov/securitycheck.h"
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
102
static int mlx_kem_has(const void *vkey, int selection)
103
0
{
104
0
    const MLX_KEY *key = vkey;
105
106
    /* A NULL key MUST fail to have anything */
107
0
    if (!ossl_prov_is_running() || key == NULL)
108
0
        return 0;
109
110
0
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
111
0
    case 0:
112
0
        return 1;
113
0
    case OSSL_KEYMGMT_SELECT_PUBLIC_KEY:
114
0
        return mlx_kem_have_pubkey(key);
115
0
    default:
116
0
        return mlx_kem_have_prvkey(key);
117
0
    }
118
0
}
119
120
static int mlx_kem_match(const void *vkey1, const void *vkey2, int selection)
121
0
{
122
0
    const MLX_KEY *key1 = vkey1;
123
0
    const MLX_KEY *key2 = vkey2;
124
0
    int have_pub1 = mlx_kem_have_pubkey(key1);
125
0
    int have_pub2 = mlx_kem_have_pubkey(key2);
126
127
0
    if (!ossl_prov_is_running())
128
0
        return 0;
129
130
    /* Compare domain parameters */
131
0
    if (key1->xinfo != key2->xinfo)
132
0
        return 0;
133
134
0
    if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR))
135
0
        return 1;
136
137
0
    if (have_pub1 ^ have_pub2)
138
0
        return 0;
139
140
    /* As in other providers, equal when both have no key material. */
141
0
    if (!have_pub1)
142
0
        return 1;
143
144
0
    return EVP_PKEY_eq(key1->mkey, key2->mkey)
145
0
        && EVP_PKEY_eq(key1->xkey, key2->xkey);
146
0
}
147
148
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
149
#ifndef ml_kem_import_export_list
150
static const OSSL_PARAM ml_kem_import_export_list[] = {
151
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
152
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
153
    OSSL_PARAM_END
154
};
155
#endif
156
157
#ifndef ml_kem_import_export_st
158
struct ml_kem_import_export_st {
159
    OSSL_PARAM *privkey;
160
    OSSL_PARAM *pubkey;
161
};
162
#endif
163
164
#ifndef ml_kem_import_export_decoder
165
static struct ml_kem_import_export_st
166
0
ml_kem_import_export_decoder(const OSSL_PARAM params[]) {
167
0
    struct ml_kem_import_export_st r;
168
0
    const OSSL_PARAM *p;
169
0
    const char *s;
170
171
0
    memset(&r, 0, sizeof(r));
172
0
    for (p = params; (s = p->key) != NULL; p++)
173
0
        switch(s[0]) {
174
0
        default:
175
0
            break;
176
0
        case 'p':
177
0
            switch(s[1]) {
178
0
            default:
179
0
                break;
180
0
            case 'r':
181
0
                if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0))
182
0
                    r.privkey = (OSSL_PARAM *)p;
183
0
                break;
184
0
            case 'u':
185
0
                if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0))
186
0
                    r.pubkey = (OSSL_PARAM *)p;
187
0
            }
188
0
        }
189
0
    return r;
190
0
}
191
#endif
192
/* End of machine generated */
193
194
typedef struct export_cb_arg_st {
195
    const char *algorithm_name;
196
    uint8_t *pubenc;
197
    uint8_t *prvenc;
198
    int      pubcount;
199
    int      prvcount;
200
    size_t   puboff;
201
    size_t   prvoff;
202
    size_t   publen;
203
    size_t   prvlen;
204
} EXPORT_CB_ARG;
205
206
/* Copy any exported key material into its storage slot */
207
static int export_sub_cb(const OSSL_PARAM *params, void *varg)
208
0
{
209
0
    EXPORT_CB_ARG *sub_arg = varg;
210
0
    struct ml_kem_import_export_st p;
211
0
    size_t len;
212
213
    /*
214
     * The caller will decide whether anything essential is missing, but, if
215
     * some key material was returned, it should have the right (parameter)
216
     * data type and length.
217
     */
218
0
    if (ossl_param_is_empty(params))
219
0
        return 1;
220
0
    p = ml_kem_import_export_decoder(params);
221
0
    if (sub_arg->pubenc != NULL && p.pubkey != NULL) {
222
0
        void *pub = sub_arg->pubenc + sub_arg->puboff;
223
224
0
        if (OSSL_PARAM_get_octet_string(p.pubkey, &pub, sub_arg->publen, &len) != 1)
225
0
            return 0;
226
0
        if (len != sub_arg->publen) {
227
0
            ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
228
0
                           "Unexpected %s public key length %lu != %lu",
229
0
                           sub_arg->algorithm_name, (unsigned long) len,
230
0
                           sub_arg->publen);
231
0
            return 0;
232
0
        }
233
0
        ++sub_arg->pubcount;
234
0
    }
235
0
    if (sub_arg->prvenc != NULL && p.privkey != NULL) {
236
0
        void *prv = sub_arg->prvenc + sub_arg->prvoff;
237
238
0
        if (OSSL_PARAM_get_octet_string(p.privkey, &prv, sub_arg->prvlen, &len) != 1)
239
0
            return 0;
240
0
        if (len != sub_arg->prvlen) {
241
0
            ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
242
0
                           "Unexpected %s private key length %lu != %lu",
243
0
                           sub_arg->algorithm_name, (unsigned long) len,
244
0
                           (unsigned long) sub_arg->publen);
245
0
            return 0;
246
0
        }
247
0
        ++sub_arg->prvcount;
248
0
    }
249
0
    return 1;
250
0
}
251
252
static int
253
export_sub(EXPORT_CB_ARG *sub_arg, int selection, MLX_KEY *key)
254
0
{
255
0
    int slot;
256
257
    /*
258
     * The caller is responsible for initialising only the pubenc and prvenc
259
     * pointer fields, the rest are set here or in the callback.
260
     */
261
0
    sub_arg->pubcount = 0;
262
0
    sub_arg->prvcount = 0;
263
264
0
    for (slot = 0; slot < 2; ++slot) {
265
0
        int ml_kem_slot = key->xinfo->ml_kem_slot;
266
0
        EVP_PKEY *pkey;
267
268
        /* Export the parts of each component into its storage slot */
269
0
        if (slot == ml_kem_slot) {
270
0
            pkey = key->mkey;
271
0
            sub_arg->algorithm_name = key->minfo->algorithm_name;
272
0
            sub_arg->puboff = slot * key->xinfo->pubkey_bytes;
273
0
            sub_arg->prvoff = slot * key->xinfo->prvkey_bytes;
274
0
            sub_arg->publen = key->minfo->pubkey_bytes;
275
0
            sub_arg->prvlen = key->minfo->prvkey_bytes;
276
0
        } else {
277
0
            pkey = key->xkey;
278
0
            sub_arg->algorithm_name = key->xinfo->algorithm_name;
279
0
            sub_arg->puboff = (1 - ml_kem_slot) * key->minfo->pubkey_bytes;
280
0
            sub_arg->prvoff = (1 - ml_kem_slot) * key->minfo->prvkey_bytes;
281
0
            sub_arg->publen = key->xinfo->pubkey_bytes;
282
0
            sub_arg->prvlen = key->xinfo->prvkey_bytes;
283
0
        }
284
0
        if (!EVP_PKEY_export(pkey, selection, export_sub_cb, (void *)sub_arg))
285
0
            return 0;
286
0
    }
287
0
    return 1;
288
0
}
289
290
static int mlx_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb,
291
                          void *cbarg)
292
0
{
293
0
    MLX_KEY *key = vkey;
294
0
    OSSL_PARAM_BLD *tmpl = NULL;
295
0
    OSSL_PARAM *params = NULL;
296
0
    size_t publen;
297
0
    size_t prvlen;
298
0
    int ret = 0;
299
0
    EXPORT_CB_ARG sub_arg;
300
301
0
    if (!ossl_prov_is_running() || key == NULL)
302
0
        return 0;
303
304
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
305
0
        return 0;
306
307
    /* Fail when no key material has yet been provided */
308
0
    if (!mlx_kem_have_pubkey(key)) {
309
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
310
0
        return 0;
311
0
    }
312
0
    publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
313
0
    prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
314
0
    memset(&sub_arg, 0, sizeof(sub_arg));
315
316
0
    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
317
0
        sub_arg.pubenc = OPENSSL_malloc(publen);
318
0
        if (sub_arg.pubenc == NULL)
319
0
            goto err;
320
0
    }
321
322
0
    if (mlx_kem_have_prvkey(key)
323
0
        && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
324
        /*
325
         * Allocated on the secure heap if configured, this is detected in
326
         * ossl_param_build_set_octet_string(), which will then also use the
327
         * secure heap.
328
         */
329
0
        sub_arg.prvenc = OPENSSL_secure_zalloc(prvlen);
330
0
        if (sub_arg.prvenc == NULL)
331
0
            goto err;
332
0
    }
333
334
0
    tmpl = OSSL_PARAM_BLD_new();
335
0
    if (tmpl == NULL)
336
0
        goto err;
337
338
    /* Extract sub-component key material */
339
0
    if (!export_sub(&sub_arg, selection, key))
340
0
        goto err;
341
342
0
    if (sub_arg.pubenc != NULL && sub_arg.pubcount == 2
343
0
        && !ossl_param_build_set_octet_string(
344
0
                tmpl, NULL, OSSL_PKEY_PARAM_PUB_KEY, sub_arg.pubenc, publen))
345
0
        goto err;
346
347
0
    if (sub_arg.prvenc != NULL && sub_arg.prvcount == 2
348
0
        && !ossl_param_build_set_octet_string(
349
0
                tmpl, NULL, OSSL_PKEY_PARAM_PRIV_KEY, sub_arg.prvenc, prvlen))
350
0
        goto err;
351
352
0
    params = OSSL_PARAM_BLD_to_param(tmpl);
353
0
    if (params == NULL)
354
0
        goto err;
355
356
0
    ret = param_cb(params, cbarg);
357
0
    OSSL_PARAM_free(params);
358
359
0
 err:
360
0
    OSSL_PARAM_BLD_free(tmpl);
361
0
    OPENSSL_secure_clear_free(sub_arg.prvenc, prvlen);
362
0
    OPENSSL_free(sub_arg.pubenc);
363
0
    return ret;
364
0
}
365
366
static const OSSL_PARAM *mlx_kem_imexport_types(int selection)
367
0
{
368
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
369
0
        return ml_kem_import_export_list;
370
0
    return NULL;
371
0
}
372
373
static int
374
load_slot(OSSL_LIB_CTX *libctx, const char *propq, const char *pname,
375
          int selection, MLX_KEY *key, int slot, const uint8_t *in,
376
          int mbytes, int xbytes)
377
0
{
378
0
    EVP_PKEY_CTX *ctx;
379
0
    EVP_PKEY **ppkey;
380
0
    OSSL_PARAM parr[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
381
0
    const char *alg;
382
0
    char *group = NULL;
383
0
    size_t off, len;
384
0
    void *val;
385
0
    int ml_kem_slot = key->xinfo->ml_kem_slot;
386
0
    int ret = 0;
387
388
0
    if (slot == ml_kem_slot) {
389
0
        alg = key->minfo->algorithm_name;
390
0
        ppkey = &key->mkey;
391
0
        off = slot * xbytes;
392
0
        len = mbytes;
393
0
    } else {
394
0
        alg = key->xinfo->algorithm_name;
395
0
        group = (char *) key->xinfo->group_name;
396
0
        ppkey = &key->xkey;
397
0
        off = (1 - ml_kem_slot) * mbytes;
398
0
        len = xbytes;
399
0
    }
400
0
    val = (void *)(in + off);
401
402
0
    if ((ctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propq)) == NULL
403
0
        || EVP_PKEY_fromdata_init(ctx) <= 0)
404
0
        goto err;
405
0
    parr[0] = OSSL_PARAM_construct_octet_string(pname, val, len);
406
0
    if (group != NULL)
407
0
        parr[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
408
0
                                                   group, 0);
409
0
    if (EVP_PKEY_fromdata(ctx, ppkey, selection, parr) > 0)
410
0
        ret = 1;
411
412
0
 err:
413
0
    EVP_PKEY_CTX_free(ctx);
414
0
    return ret;
415
0
}
416
417
static int
418
load_keys(MLX_KEY *key,
419
          const uint8_t *pubenc, size_t publen,
420
          const uint8_t *prvenc, size_t prvlen)
421
0
{
422
0
    int slot;
423
424
0
    for (slot = 0; slot < 2; ++slot) {
425
0
        if (prvlen) {
426
            /* Ignore public keys when private provided */
427
0
            if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PRIV_KEY,
428
0
                           minimal_selection, key, slot, prvenc,
429
0
                           (int)key->minfo->prvkey_bytes,
430
0
                           (int)key->xinfo->prvkey_bytes))
431
0
                goto err;
432
0
        } else if (publen) {
433
            /* Absent private key data, import public keys */
434
0
            if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PUB_KEY,
435
0
                           minimal_selection, key, slot, pubenc,
436
0
                           (int)key->minfo->pubkey_bytes,
437
0
                           (int)key->xinfo->pubkey_bytes))
438
0
                goto err;
439
0
        }
440
0
    }
441
0
    key->state = prvlen ? MLX_HAVE_PRVKEY : MLX_HAVE_PUBKEY;
442
0
    return 1;
443
444
0
 err:
445
0
    EVP_PKEY_free(key->mkey);
446
0
    EVP_PKEY_free(key->xkey);
447
0
    key->xkey = key->mkey = NULL;
448
0
    key->state = MLX_HAVE_NOKEYS;
449
0
    return 0;
450
0
}
451
452
static int mlx_kem_key_fromdata(MLX_KEY *key,
453
                               const OSSL_PARAM params[],
454
                               int include_private)
455
0
{
456
0
    struct ml_kem_import_export_st p;
457
0
    const void *pubenc = NULL, *prvenc = NULL;
458
0
    size_t pubkey_bytes, prvkey_bytes;
459
0
    size_t publen = 0, prvlen = 0;
460
461
    /* Invalid attempt to mutate a key, what is the right error to report? */
462
0
    if (key == NULL || mlx_kem_have_pubkey(key))
463
0
        return 0;
464
0
    pubkey_bytes = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
465
0
    prvkey_bytes = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
466
467
0
    p = ml_kem_import_export_decoder(params);
468
469
    /* What does the caller want to set? */
470
0
    if (p.pubkey != NULL &&
471
0
        OSSL_PARAM_get_octet_string_ptr(p.pubkey, &pubenc, &publen) != 1)
472
0
        return 0;
473
0
    if (include_private
474
0
            && p.privkey != NULL
475
0
            && OSSL_PARAM_get_octet_string_ptr(p.privkey, &prvenc, &prvlen) != 1)
476
0
        return 0;
477
478
    /* The caller MUST specify at least one of the public or private keys. */
479
0
    if (publen == 0 && prvlen == 0) {
480
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
481
0
        return 0;
482
0
    }
483
484
    /*
485
     * When a pubkey is provided, its length MUST be correct, if a private key
486
     * is also provided, the public key will be otherwise ignored.  We could
487
     * look for a matching encoded block, but unclear this is useful.
488
     */
489
0
    if (publen != 0 && publen != pubkey_bytes) {
490
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
491
0
        return 0;
492
0
    }
493
0
    if (prvlen != 0 && prvlen != prvkey_bytes) {
494
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
495
0
        return 0;
496
0
    }
497
498
0
    return load_keys(key, pubenc, publen, prvenc, prvlen);
499
0
}
500
501
static int mlx_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
502
0
{
503
0
    MLX_KEY *key = vkey;
504
0
    int include_private;
505
506
0
    if (!ossl_prov_is_running() || key == NULL)
507
0
        return 0;
508
509
0
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
510
0
        return 0;
511
512
0
    include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
513
0
    return mlx_kem_key_fromdata(key, params, include_private);
514
0
}
515
516
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
517
#ifndef mlx_get_params_list
518
static const OSSL_PARAM mlx_get_params_list[] = {
519
    OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
520
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
521
    OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
522
    OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL),
523
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
524
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
525
    OSSL_PARAM_END
526
};
527
#endif
528
529
#ifndef mlx_get_params_st
530
struct mlx_get_params_st {
531
    OSSL_PARAM *bits;
532
    OSSL_PARAM *maxsize;
533
    OSSL_PARAM *priv;
534
    OSSL_PARAM *pub;
535
    OSSL_PARAM *secbits;
536
    OSSL_PARAM *seccat;
537
};
538
#endif
539
540
#ifndef mlx_get_params_decoder
541
static struct mlx_get_params_st
542
0
mlx_get_params_decoder(const OSSL_PARAM params[]) {
543
0
    struct mlx_get_params_st r;
544
0
    const OSSL_PARAM *p;
545
0
    const char *s;
546
547
0
    memset(&r, 0, sizeof(r));
548
0
    for (p = params; (s = p->key) != NULL; p++)
549
0
        switch(s[0]) {
550
0
        default:
551
0
            break;
552
0
        case 'b':
553
0
            if (ossl_likely(r.bits == NULL && strcmp("its", s + 1) == 0))
554
0
                r.bits = (OSSL_PARAM *)p;
555
0
            break;
556
0
        case 'e':
557
0
            if (ossl_likely(r.pub == NULL && strcmp("ncoded-pub-key", s + 1) == 0))
558
0
                r.pub = (OSSL_PARAM *)p;
559
0
            break;
560
0
        case 'm':
561
0
            if (ossl_likely(r.maxsize == NULL && strcmp("ax-size", s + 1) == 0))
562
0
                r.maxsize = (OSSL_PARAM *)p;
563
0
            break;
564
0
        case 'p':
565
0
            if (ossl_likely(r.priv == NULL && strcmp("riv", s + 1) == 0))
566
0
                r.priv = (OSSL_PARAM *)p;
567
0
            break;
568
0
        case 's':
569
0
            switch(s[1]) {
570
0
            default:
571
0
                break;
572
0
            case 'e':
573
0
                switch(s[2]) {
574
0
                default:
575
0
                    break;
576
0
                case 'c':
577
0
                    switch(s[3]) {
578
0
                    default:
579
0
                        break;
580
0
                    case 'u':
581
0
                        switch(s[4]) {
582
0
                        default:
583
0
                            break;
584
0
                        case 'r':
585
0
                            switch(s[5]) {
586
0
                            default:
587
0
                                break;
588
0
                            case 'i':
589
0
                                switch(s[6]) {
590
0
                                default:
591
0
                                    break;
592
0
                                case 't':
593
0
                                    switch(s[7]) {
594
0
                                    default:
595
0
                                        break;
596
0
                                    case 'y':
597
0
                                        switch(s[8]) {
598
0
                                        default:
599
0
                                            break;
600
0
                                        case '-':
601
0
                                            switch(s[9]) {
602
0
                                            default:
603
0
                                                break;
604
0
                                            case 'b':
605
0
                                                if (ossl_likely(r.secbits == NULL && strcmp("its", s + 10) == 0))
606
0
                                                    r.secbits = (OSSL_PARAM *)p;
607
0
                                                break;
608
0
                                            case 'c':
609
0
                                                if (ossl_likely(r.seccat == NULL && strcmp("ategory", s + 10) == 0))
610
0
                                                    r.seccat = (OSSL_PARAM *)p;
611
0
                                            }
612
0
                                        }
613
0
                                    }
614
0
                                }
615
0
                            }
616
0
                        }
617
0
                    }
618
0
                }
619
0
            }
620
0
        }
621
0
    return r;
622
0
}
623
#endif
624
/* End of machine generated */
625
626
static const OSSL_PARAM *mlx_kem_gettable_params(void *provctx)
627
0
{
628
0
    return mlx_get_params_list;
629
0
}
630
631
/*
632
 * It is assumed the key is guaranteed non-NULL here, and is from this provider
633
 */
634
static int mlx_kem_get_params(void *vkey, OSSL_PARAM params[])
635
0
{
636
0
    MLX_KEY *key = vkey;
637
0
    OSSL_PARAM *pub, *prv = NULL;
638
0
    EXPORT_CB_ARG sub_arg;
639
0
    int selection;
640
0
    struct mlx_get_params_st p;
641
0
    size_t publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
642
0
    size_t prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
643
644
0
    p = mlx_get_params_decoder(params);
645
646
    /* The reported "bit" count is those of the ML-KEM key */
647
0
    if (p.bits != NULL)
648
0
        if (!OSSL_PARAM_set_int(p.bits, key->minfo->bits))
649
0
            return 0;
650
651
    /* The reported security bits are those of the ML-KEM key */
652
0
    if (p.secbits != NULL)
653
0
        if (!OSSL_PARAM_set_int(p.secbits, key->minfo->secbits))
654
0
            return 0;
655
656
    /* The reported security category are those of the ML-KEM key */
657
0
    if (p.seccat != NULL)
658
0
        if (!OSSL_PARAM_set_int(p.seccat, key->minfo->security_category))
659
0
            return 0;
660
661
    /* The ciphertext sizes are additive */
662
0
    if (p.maxsize != NULL)
663
0
        if (!OSSL_PARAM_set_size_t(p.maxsize, key->minfo->ctext_bytes + key->xinfo->pubkey_bytes))
664
0
            return 0;
665
666
0
    if (!mlx_kem_have_pubkey(key))
667
0
        return 1;
668
669
0
    memset(&sub_arg, 0, sizeof(sub_arg));
670
0
    if ((pub = p.pub) != NULL) {
671
0
        if (pub->data_type != OSSL_PARAM_OCTET_STRING)
672
0
            return 0;
673
0
        pub->return_size = publen;
674
0
        if (pub->data == NULL) {
675
0
            pub = NULL;
676
0
        } else if (pub->data_size < publen) {
677
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
678
0
                           "public key output buffer too short: %lu < %lu",
679
0
                           (unsigned long) pub->data_size,
680
0
                           (unsigned long) publen);
681
0
            return 0;
682
0
        } else {
683
0
            sub_arg.pubenc = pub->data;
684
0
        }
685
0
    }
686
0
    if (mlx_kem_have_prvkey(key)) {
687
0
        if ((prv = p.priv) != NULL) {
688
0
            if (prv->data_type != OSSL_PARAM_OCTET_STRING)
689
0
                return 0;
690
0
            prv->return_size = prvlen;
691
0
            if (prv->data == NULL) {
692
0
                prv = NULL;
693
0
            } else if (prv->data_size < prvlen) {
694
0
                ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
695
0
                               "private key output buffer too short: %lu < %lu",
696
0
                               (unsigned long) prv->data_size,
697
0
                               (unsigned long) prvlen);
698
0
                return 0;
699
0
            } else {
700
0
                sub_arg.prvenc = prv->data;
701
0
            }
702
0
        }
703
0
    }
704
0
    if (pub == NULL && prv == NULL)
705
0
        return 1;
706
707
0
    selection = prv == NULL ? 0 : OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
708
0
    selection |= pub == NULL ? 0 : OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
709
0
    if (key->xinfo->group_name != NULL)
710
0
        selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
711
712
    /* Extract sub-component key material */
713
0
    if (!export_sub(&sub_arg, selection, key))
714
0
        return 0;
715
716
0
    if ((pub != NULL && sub_arg.pubcount != 2)
717
0
        || (prv != NULL && sub_arg.prvcount != 2))
718
0
        return 0;
719
720
0
    return 1;
721
0
}
722
723
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
724
#ifndef mlx_set_params_list
725
static const OSSL_PARAM mlx_set_params_list[] = {
726
    OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
727
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
728
    OSSL_PARAM_END
729
};
730
#endif
731
732
#ifndef mlx_set_params_st
733
struct mlx_set_params_st {
734
    OSSL_PARAM *propq;
735
    OSSL_PARAM *pub;
736
};
737
#endif
738
739
#ifndef mlx_set_params_decoder
740
static struct mlx_set_params_st
741
0
mlx_set_params_decoder(const OSSL_PARAM params[]) {
742
0
    struct mlx_set_params_st r;
743
0
    const OSSL_PARAM *p;
744
0
    const char *s;
745
746
0
    memset(&r, 0, sizeof(r));
747
0
    for (p = params; (s = p->key) != NULL; p++)
748
0
        switch(s[0]) {
749
0
        default:
750
0
            break;
751
0
        case 'e':
752
0
            if (ossl_likely(r.pub == NULL && strcmp("ncoded-pub-key", s + 1) == 0))
753
0
                r.pub = (OSSL_PARAM *)p;
754
0
            break;
755
0
        case 'p':
756
0
            if (ossl_likely(r.propq == NULL && strcmp("roperties", s + 1) == 0))
757
0
                r.propq = (OSSL_PARAM *)p;
758
0
        }
759
0
    return r;
760
0
}
761
#endif
762
/* End of machine generated */
763
764
static const OSSL_PARAM *mlx_kem_settable_params(void *provctx)
765
0
{
766
0
    return mlx_set_params_list;
767
0
}
768
769
static int mlx_kem_set_params(void *vkey, const OSSL_PARAM params[])
770
0
{
771
0
    MLX_KEY *key = vkey;
772
0
    struct mlx_set_params_st p;
773
0
    const void *pubenc = NULL;
774
0
    size_t publen = 0;
775
776
0
    if (ossl_param_is_empty(params))
777
0
        return 1;
778
779
0
    p = mlx_set_params_decoder(params);
780
781
0
    if (p.propq != NULL) {
782
0
        OPENSSL_free(key->propq);
783
0
        key->propq = NULL;
784
0
        if (!OSSL_PARAM_get_utf8_string(p.propq, &key->propq, 0))
785
0
            return 0;
786
0
    }
787
788
0
    if (p.pub == NULL)
789
0
        return 1;
790
791
    /* Key mutation is reportedly generally not allowed */
792
0
    if (mlx_kem_have_pubkey(key)) {
793
0
        ERR_raise_data(ERR_LIB_PROV,
794
0
                       PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
795
0
                       "keys cannot be mutated");
796
0
        return 0;
797
0
    }
798
    /* An unlikely failure mode is the parameter having some unexpected type */
799
0
    if (!OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen))
800
0
        return 0;
801
802
0
    if (publen != key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes) {
803
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
804
0
        return 0;
805
0
    }
806
807
0
    return load_keys(key, pubenc, publen, NULL, 0);
808
0
}
809
810
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
811
#ifndef mlx_gen_set_params_list
812
static const OSSL_PARAM mlx_gen_set_params_list[] = {
813
    OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
814
    OSSL_PARAM_END
815
};
816
#endif
817
818
#ifndef mlx_gen_set_params_st
819
struct mlx_gen_set_params_st {
820
    OSSL_PARAM *propq;
821
};
822
#endif
823
824
#ifndef mlx_gen_set_params_decoder
825
static struct mlx_gen_set_params_st
826
0
mlx_gen_set_params_decoder(const OSSL_PARAM params[]) {
827
0
    struct mlx_gen_set_params_st r;
828
0
    const OSSL_PARAM *p;
829
0
    const char *s;
830
831
0
    memset(&r, 0, sizeof(r));
832
0
    for (p = params; (s = p->key) != NULL; p++)
833
0
        if (ossl_likely(r.propq == NULL && strcmp("properties", s + 0) == 0))
834
0
            r.propq = (OSSL_PARAM *)p;
835
0
    return r;
836
0
}
837
#endif
838
/* End of machine generated */
839
840
static int mlx_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
841
0
{
842
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
843
0
    struct mlx_gen_set_params_st p;
844
845
0
    if (gctx == NULL)
846
0
        return 0;
847
0
    if (ossl_param_is_empty(params))
848
0
        return 1;
849
850
0
    p = mlx_gen_set_params_decoder(params);
851
852
0
    if (p.propq != NULL) {
853
0
        if (p.propq->data_type != OSSL_PARAM_UTF8_STRING)
854
0
            return 0;
855
0
        OPENSSL_free(gctx->propq);
856
0
        if ((gctx->propq = OPENSSL_strdup(p.propq->data)) == NULL)
857
0
            return 0;
858
0
    }
859
0
    return 1;
860
0
}
861
862
static void *mlx_kem_gen_init(int evp_type, OSSL_LIB_CTX *libctx,
863
                              int selection, const OSSL_PARAM params[])
864
0
{
865
0
    PROV_ML_KEM_GEN_CTX *gctx = NULL;
866
867
    /*
868
     * We can only generate private keys, check that the selection is
869
     * appropriate.
870
     */
871
0
    if (!ossl_prov_is_running()
872
0
        || (selection & minimal_selection) == 0
873
0
        || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
874
0
        return NULL;
875
876
0
    gctx->evp_type = evp_type;
877
0
    gctx->libctx = libctx;
878
0
    gctx->selection = selection;
879
0
    if (mlx_kem_gen_set_params(gctx, params))
880
0
        return gctx;
881
882
0
    mlx_kem_gen_cleanup(gctx);
883
0
    return NULL;
884
0
}
885
886
static const OSSL_PARAM *mlx_kem_gen_settable_params(ossl_unused void *vgctx,
887
                                                     ossl_unused void *provctx)
888
0
{
889
0
    return mlx_gen_set_params_list;
890
0
}
891
892
static void *mlx_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
893
0
{
894
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
895
0
    MLX_KEY *key;
896
0
    char *propq;
897
898
0
    if (gctx == NULL
899
0
        || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
900
0
            OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
901
0
        return NULL;
902
903
    /* Lose ownership of propq */
904
0
    propq = gctx->propq;
905
0
    gctx->propq = NULL;
906
0
    if ((key = mlx_kem_key_new(gctx->evp_type, gctx->libctx, propq)) == NULL)
907
0
        return NULL;
908
909
0
    if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
910
0
        return key;
911
912
    /* For now, using the same "propq" for all components */
913
0
    key->mkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
914
0
                                  key->minfo->algorithm_name);
915
0
    key->xkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
916
0
                                  key->xinfo->algorithm_name,
917
0
                                  key->xinfo->group_name);
918
0
    if (key->mkey != NULL && key->xkey != NULL) {
919
0
        key->state = MLX_HAVE_PRVKEY;
920
0
        return key;
921
0
    }
922
923
0
    mlx_kem_key_free(key);
924
0
    return NULL;
925
0
}
926
927
static void mlx_kem_gen_cleanup(void *vgctx)
928
0
{
929
0
    PROV_ML_KEM_GEN_CTX *gctx = vgctx;
930
931
0
    if (gctx == NULL)
932
0
        return;
933
0
    OPENSSL_free(gctx->propq);
934
0
    OPENSSL_free(gctx);
935
0
}
936
937
static void *mlx_kem_dup(const void *vkey, int selection)
938
0
{
939
0
    const MLX_KEY *key = vkey;
940
0
    MLX_KEY *ret;
941
942
0
    if (!ossl_prov_is_running()
943
0
        || (ret = OPENSSL_memdup(key, sizeof(*ret))) == NULL)
944
0
        return NULL;
945
946
0
    if (ret->propq != NULL
947
0
        && (ret->propq = OPENSSL_strdup(ret->propq)) == NULL) {
948
0
        OPENSSL_free(ret);
949
0
        return NULL;
950
0
    }
951
952
    /* Absent key material, nothing left to do */
953
0
    if (ret->mkey == NULL) {
954
0
        if (ret->xkey == NULL)
955
0
            return ret;
956
        /* Fail if the source key is an inconsistent state */
957
0
        OPENSSL_free(ret);
958
0
        return NULL;
959
0
    }
960
961
0
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
962
0
    case 0:
963
0
        ret->xkey = ret->mkey = NULL;
964
0
        return ret;
965
0
    case OSSL_KEYMGMT_SELECT_KEYPAIR:
966
0
        ret->mkey = EVP_PKEY_dup(key->mkey);
967
0
        ret->xkey = EVP_PKEY_dup(key->xkey);
968
0
        if (ret->xkey != NULL && ret->mkey != NULL)
969
0
            return ret;
970
0
        break;
971
0
    default:
972
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_UNSUPPORTED_SELECTION,
973
0
                       "duplication of partial key material not supported");
974
0
        break;
975
0
    }
976
977
0
    mlx_kem_key_free(ret);
978
0
    return NULL;
979
0
}
980
981
#define DECLARE_DISPATCH(name, variant) \
982
    static OSSL_FUNC_keymgmt_new_fn mlx_##name##_kem_new; \
983
    static void *mlx_##name##_kem_new(void *provctx) \
984
0
    { \
985
0
        OSSL_LIB_CTX *libctx; \
986
0
                              \
987
0
        libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \
988
0
        return mlx_kem_key_new(variant, libctx, NULL); \
989
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
990
    static OSSL_FUNC_keymgmt_gen_init_fn mlx_##name##_kem_gen_init; \
991
    static void *mlx_##name##_kem_gen_init(void *provctx, int selection, \
992
                                           const OSSL_PARAM params[]) \
993
0
    { \
994
0
        OSSL_LIB_CTX *libctx; \
995
0
                              \
996
0
        libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \
997
0
        return mlx_kem_gen_init(variant, libctx, selection, params); \
998
0
    } \
Unexecuted instantiation: mlx_kmgmt.c:mlx_p256_kem_gen_init
Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_gen_init
Unexecuted instantiation: mlx_kmgmt.c:mlx_x25519_kem_gen_init
Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_gen_init
999
    const OSSL_DISPATCH ossl_mlx_##name##_kem_kmgmt_functions[] = { \
1000
        { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) mlx_##name##_kem_new }, \
1001
        { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) mlx_kem_key_free }, \
1002
        { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) mlx_kem_get_params }, \
1003
        { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_gettable_params }, \
1004
        { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) mlx_kem_set_params }, \
1005
        { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_settable_params }, \
1006
        { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) mlx_kem_has }, \
1007
        { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) mlx_kem_match }, \
1008
        { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) mlx_##name##_kem_gen_init }, \
1009
        { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) mlx_kem_gen_set_params }, \
1010
        { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_gen_settable_params }, \
1011
        { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) mlx_kem_gen }, \
1012
        { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) mlx_kem_gen_cleanup }, \
1013
        { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) mlx_kem_dup }, \
1014
        { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) mlx_kem_import }, \
1015
        { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) mlx_kem_imexport_types }, \
1016
        { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) mlx_kem_export }, \
1017
        { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) mlx_kem_imexport_types }, \
1018
        OSSL_DISPATCH_END \
1019
    }
1020
/* See |hybrid_vtable| above */
1021
DECLARE_DISPATCH(p256, 0);
1022
DECLARE_DISPATCH(p384, 1);
1023
#if !defined(OPENSSL_NO_ECX)
1024
DECLARE_DISPATCH(x25519, 2);
1025
DECLARE_DISPATCH(x448, 3);
1026
#endif