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