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