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