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