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