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