/src/openssl40/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 | | |
10 | | #include <openssl/core_dispatch.h> |
11 | | #include <openssl/core_names.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/param_build.h> |
14 | | #include <openssl/params.h> |
15 | | #include <openssl/proverr.h> |
16 | | #include <openssl/rand.h> |
17 | | #include <openssl/self_test.h> |
18 | | #include "internal/nelem.h" |
19 | | #include "internal/param_build_set.h" |
20 | | #include "prov/implementations.h" |
21 | | #include "prov/mlx_kem.h" |
22 | | #include "prov/provider_ctx.h" |
23 | | #include "prov/providercommon.h" |
24 | | #include "prov/securitycheck.h" |
25 | | #include "providers/implementations/keymgmt/mlx_kmgmt.inc" |
26 | | |
27 | | static OSSL_FUNC_keymgmt_gen_fn mlx_kem_gen; |
28 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn mlx_kem_gen_cleanup; |
29 | | static OSSL_FUNC_keymgmt_gen_set_params_fn mlx_kem_gen_set_params; |
30 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn mlx_kem_gen_settable_params; |
31 | | static OSSL_FUNC_keymgmt_get_params_fn mlx_kem_get_params; |
32 | | static OSSL_FUNC_keymgmt_gettable_params_fn mlx_kem_gettable_params; |
33 | | static OSSL_FUNC_keymgmt_set_params_fn mlx_kem_set_params; |
34 | | static OSSL_FUNC_keymgmt_settable_params_fn mlx_kem_settable_params; |
35 | | static OSSL_FUNC_keymgmt_has_fn mlx_kem_has; |
36 | | static OSSL_FUNC_keymgmt_match_fn mlx_kem_match; |
37 | | static OSSL_FUNC_keymgmt_import_fn mlx_kem_import; |
38 | | static OSSL_FUNC_keymgmt_export_fn mlx_kem_export; |
39 | | static OSSL_FUNC_keymgmt_import_types_fn mlx_kem_imexport_types; |
40 | | static OSSL_FUNC_keymgmt_export_types_fn mlx_kem_imexport_types; |
41 | | static OSSL_FUNC_keymgmt_dup_fn mlx_kem_dup; |
42 | | |
43 | | static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS |
44 | | | OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
45 | | |
46 | | /* Must match DECLARE_DISPATCH invocations at the end of the file */ |
47 | | static const ECDH_VINFO hybrid_vtable[] = { |
48 | | { "EC", "P-256", 65, 32, 32, 1, EVP_PKEY_ML_KEM_768 }, |
49 | | { "EC", "P-384", 97, 48, 48, 1, EVP_PKEY_ML_KEM_1024 }, |
50 | | #if !defined(OPENSSL_NO_ECX) |
51 | | { "X25519", NULL, 32, 32, 32, 0, EVP_PKEY_ML_KEM_768 }, |
52 | | { "X448", NULL, 56, 56, 56, 0, EVP_PKEY_ML_KEM_1024 }, |
53 | | #else |
54 | | { NULL, NULL, 0, 0, 0, 0, NID_undef }, |
55 | | { NULL, NULL, 0, 0, 0, 0, NID_undef }, |
56 | | #endif |
57 | | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_SM2) |
58 | | { "curveSM2", "SM2", 65, 32, 32, 1, EVP_PKEY_ML_KEM_768 }, |
59 | | #else |
60 | | { NULL, NULL, 0, 0, 0, 0, NID_undef }, |
61 | | #endif |
62 | | }; |
63 | | |
64 | | typedef struct mlx_kem_gen_ctx_st { |
65 | | OSSL_LIB_CTX *libctx; |
66 | | char *propq; |
67 | | int selection; |
68 | | unsigned int evp_type; |
69 | | } PROV_ML_KEM_GEN_CTX; |
70 | | |
71 | | static void mlx_kem_key_free(void *vkey) |
72 | 62.9k | { |
73 | 62.9k | MLX_KEY *key = vkey; |
74 | | |
75 | 62.9k | if (key == NULL) |
76 | 0 | return; |
77 | 62.9k | OPENSSL_free(key->propq); |
78 | 62.9k | EVP_PKEY_free(key->mkey); |
79 | 62.9k | EVP_PKEY_free(key->xkey); |
80 | 62.9k | OPENSSL_free(key); |
81 | 62.9k | } |
82 | | |
83 | | /* Takes ownership of propq */ |
84 | | static void * |
85 | | mlx_kem_key_new(unsigned int v, OSSL_LIB_CTX *libctx, char *propq) |
86 | 62.9k | { |
87 | 62.9k | MLX_KEY *key = NULL; |
88 | 62.9k | unsigned int ml_kem_variant; |
89 | | |
90 | 62.9k | if (!ossl_prov_is_running() |
91 | 62.9k | || v >= OSSL_NELEM(hybrid_vtable) |
92 | 62.9k | || (key = OPENSSL_malloc(sizeof(*key))) == NULL) |
93 | 0 | goto err; |
94 | | |
95 | 62.9k | ml_kem_variant = hybrid_vtable[v].ml_kem_variant; |
96 | 62.9k | key->libctx = libctx; |
97 | 62.9k | key->minfo = ossl_ml_kem_get_vinfo(ml_kem_variant); |
98 | 62.9k | key->xinfo = &hybrid_vtable[v]; |
99 | 62.9k | key->xkey = key->mkey = NULL; |
100 | 62.9k | key->state = MLX_HAVE_NOKEYS; |
101 | 62.9k | key->propq = propq; |
102 | 62.9k | return key; |
103 | | |
104 | 0 | err: |
105 | 0 | OPENSSL_free(propq); |
106 | 0 | return NULL; |
107 | 62.9k | } |
108 | | |
109 | | static int mlx_kem_has(const void *vkey, int selection) |
110 | 0 | { |
111 | 0 | const MLX_KEY *key = vkey; |
112 | | |
113 | | /* A NULL key MUST fail to have anything */ |
114 | 0 | if (!ossl_prov_is_running() || key == NULL) |
115 | 0 | return 0; |
116 | | |
117 | 0 | switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) { |
118 | 0 | case 0: |
119 | 0 | return 1; |
120 | 0 | case OSSL_KEYMGMT_SELECT_PUBLIC_KEY: |
121 | 0 | return mlx_kem_have_pubkey(key); |
122 | 0 | default: |
123 | 0 | return mlx_kem_have_prvkey(key); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | static int mlx_kem_match(const void *vkey1, const void *vkey2, int selection) |
128 | 0 | { |
129 | 0 | const MLX_KEY *key1 = vkey1; |
130 | 0 | const MLX_KEY *key2 = vkey2; |
131 | 0 | int have_pub1 = mlx_kem_have_pubkey(key1); |
132 | 0 | int have_pub2 = mlx_kem_have_pubkey(key2); |
133 | |
|
134 | 0 | if (!ossl_prov_is_running()) |
135 | 0 | return 0; |
136 | | |
137 | | /* Compare domain parameters */ |
138 | 0 | if (key1->xinfo != key2->xinfo) |
139 | 0 | return 0; |
140 | | |
141 | 0 | if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR)) |
142 | 0 | return 1; |
143 | | |
144 | 0 | if (have_pub1 ^ have_pub2) |
145 | 0 | return 0; |
146 | | |
147 | | /* As in other providers, equal when both have no key material. */ |
148 | 0 | if (!have_pub1) |
149 | 0 | return 1; |
150 | | |
151 | 0 | return EVP_PKEY_eq(key1->mkey, key2->mkey) |
152 | 0 | && EVP_PKEY_eq(key1->xkey, key2->xkey); |
153 | 0 | } |
154 | | |
155 | | typedef struct export_cb_arg_st { |
156 | | const char *algorithm_name; |
157 | | uint8_t *pubenc; |
158 | | uint8_t *prvenc; |
159 | | int pubcount; |
160 | | int prvcount; |
161 | | size_t puboff; |
162 | | size_t prvoff; |
163 | | size_t publen; |
164 | | size_t prvlen; |
165 | | } EXPORT_CB_ARG; |
166 | | |
167 | | /* Copy any exported key material into its storage slot */ |
168 | | static int export_sub_cb(const OSSL_PARAM *params, void *varg) |
169 | 97.2k | { |
170 | 97.2k | EXPORT_CB_ARG *sub_arg = varg; |
171 | 97.2k | struct ml_kem_import_export_st p; |
172 | 97.2k | size_t len; |
173 | | |
174 | 97.2k | if (!ml_kem_import_export_decoder(params, &p)) |
175 | 0 | return 0; |
176 | | |
177 | | /* |
178 | | * The caller will decide whether anything essential is missing, but, if |
179 | | * some key material was returned, it should have the right (parameter) |
180 | | * data type and length. |
181 | | */ |
182 | 97.2k | if (sub_arg->pubenc != NULL && p.pubkey != NULL) { |
183 | 97.2k | void *pub = sub_arg->pubenc + sub_arg->puboff; |
184 | | |
185 | 97.2k | if (OSSL_PARAM_get_octet_string(p.pubkey, &pub, sub_arg->publen, &len) != 1) |
186 | 0 | return 0; |
187 | 97.2k | if (len != sub_arg->publen) { |
188 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
189 | 0 | "Unexpected %s public key length %lu != %lu", |
190 | 0 | sub_arg->algorithm_name, (unsigned long)len, |
191 | 0 | sub_arg->publen); |
192 | 0 | return 0; |
193 | 0 | } |
194 | 97.2k | ++sub_arg->pubcount; |
195 | 97.2k | } |
196 | 97.2k | if (sub_arg->prvenc != NULL && p.privkey != NULL) { |
197 | 0 | void *prv = sub_arg->prvenc + sub_arg->prvoff; |
198 | |
|
199 | 0 | if (OSSL_PARAM_get_octet_string(p.privkey, &prv, sub_arg->prvlen, &len) != 1) |
200 | 0 | return 0; |
201 | 0 | if (len != sub_arg->prvlen) { |
202 | 0 | ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR, |
203 | 0 | "Unexpected %s private key length %zu != %zu", |
204 | 0 | sub_arg->algorithm_name, len, sub_arg->prvlen); |
205 | 0 | return 0; |
206 | 0 | } |
207 | 0 | ++sub_arg->prvcount; |
208 | 0 | } |
209 | 97.2k | return 1; |
210 | 97.2k | } |
211 | | |
212 | | static int |
213 | | export_sub(EXPORT_CB_ARG *sub_arg, int selection, MLX_KEY *key) |
214 | 63.4k | { |
215 | 63.4k | int slot; |
216 | | |
217 | | /* |
218 | | * The caller is responsible for initialising only the pubenc and prvenc |
219 | | * pointer fields, the rest are set here or in the callback. |
220 | | */ |
221 | 63.4k | sub_arg->pubcount = 0; |
222 | 63.4k | sub_arg->prvcount = 0; |
223 | | |
224 | 190k | for (slot = 0; slot < 2; ++slot) { |
225 | 126k | int ml_kem_slot = key->xinfo->ml_kem_slot; |
226 | 126k | EVP_PKEY *pkey; |
227 | | |
228 | | /* Export the parts of each component into its storage slot */ |
229 | 126k | if (slot == ml_kem_slot) { |
230 | 63.4k | pkey = key->mkey; |
231 | 63.4k | sub_arg->algorithm_name = key->minfo->algorithm_name; |
232 | 63.4k | sub_arg->puboff = slot * key->xinfo->pubkey_bytes; |
233 | 63.4k | sub_arg->prvoff = slot * key->xinfo->prvkey_bytes; |
234 | 63.4k | sub_arg->publen = key->minfo->pubkey_bytes; |
235 | 63.4k | sub_arg->prvlen = key->minfo->prvkey_bytes; |
236 | 63.4k | } else { |
237 | 63.4k | pkey = key->xkey; |
238 | 63.4k | sub_arg->algorithm_name = key->xinfo->algorithm_name; |
239 | 63.4k | sub_arg->puboff = (1 - ml_kem_slot) * key->minfo->pubkey_bytes; |
240 | 63.4k | sub_arg->prvoff = (1 - ml_kem_slot) * key->minfo->prvkey_bytes; |
241 | 63.4k | sub_arg->publen = key->xinfo->pubkey_bytes; |
242 | 63.4k | sub_arg->prvlen = key->xinfo->prvkey_bytes; |
243 | 63.4k | } |
244 | 126k | if (!EVP_PKEY_export(pkey, selection, export_sub_cb, (void *)sub_arg)) |
245 | 0 | return 0; |
246 | 126k | } |
247 | 63.4k | return 1; |
248 | 63.4k | } |
249 | | |
250 | | static int mlx_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb, |
251 | | void *cbarg) |
252 | 0 | { |
253 | 0 | MLX_KEY *key = vkey; |
254 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
255 | 0 | OSSL_PARAM *params = NULL; |
256 | 0 | size_t publen; |
257 | 0 | size_t prvlen; |
258 | 0 | int ret = 0; |
259 | 0 | EXPORT_CB_ARG sub_arg; |
260 | |
|
261 | 0 | if (!ossl_prov_is_running() || key == NULL) |
262 | 0 | return 0; |
263 | | |
264 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
265 | 0 | return 0; |
266 | | |
267 | | /* Fail when no key material has yet been provided */ |
268 | 0 | if (!mlx_kem_have_pubkey(key)) { |
269 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
270 | 0 | return 0; |
271 | 0 | } |
272 | 0 | publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes; |
273 | 0 | prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes; |
274 | 0 | memset(&sub_arg, 0, sizeof(sub_arg)); |
275 | |
|
276 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
277 | 0 | sub_arg.pubenc = OPENSSL_malloc(publen); |
278 | 0 | if (sub_arg.pubenc == NULL) |
279 | 0 | goto err; |
280 | 0 | } |
281 | | |
282 | 0 | if (mlx_kem_have_prvkey(key) |
283 | 0 | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
284 | | /* |
285 | | * Allocated on the secure heap if configured, this is detected in |
286 | | * ossl_param_build_set_octet_string(), which will then also use the |
287 | | * secure heap. |
288 | | */ |
289 | 0 | sub_arg.prvenc = OPENSSL_secure_zalloc(prvlen); |
290 | 0 | if (sub_arg.prvenc == NULL) |
291 | 0 | goto err; |
292 | 0 | } |
293 | | |
294 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
295 | 0 | if (tmpl == NULL) |
296 | 0 | goto err; |
297 | | |
298 | | /* Extract sub-component key material */ |
299 | 0 | if (!export_sub(&sub_arg, selection, key)) |
300 | 0 | goto err; |
301 | | |
302 | 0 | if (sub_arg.pubenc != NULL && sub_arg.pubcount == 2 |
303 | 0 | && !ossl_param_build_set_octet_string( |
304 | 0 | tmpl, NULL, OSSL_PKEY_PARAM_PUB_KEY, sub_arg.pubenc, publen)) |
305 | 0 | goto err; |
306 | | |
307 | 0 | if (sub_arg.prvenc != NULL && sub_arg.prvcount == 2 |
308 | 0 | && !ossl_param_build_set_octet_string( |
309 | 0 | tmpl, NULL, OSSL_PKEY_PARAM_PRIV_KEY, sub_arg.prvenc, prvlen)) |
310 | 0 | goto err; |
311 | | |
312 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
313 | 0 | if (params == NULL) |
314 | 0 | goto err; |
315 | | |
316 | 0 | ret = param_cb(params, cbarg); |
317 | 0 | OSSL_PARAM_clear_free(params); |
318 | |
|
319 | 0 | err: |
320 | 0 | OSSL_PARAM_BLD_free(tmpl); |
321 | 0 | OPENSSL_secure_clear_free(sub_arg.prvenc, prvlen); |
322 | 0 | OPENSSL_clear_free(sub_arg.pubenc, publen); |
323 | 0 | return ret; |
324 | 0 | } |
325 | | |
326 | | static const OSSL_PARAM *mlx_kem_imexport_types(int selection) |
327 | 0 | { |
328 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
329 | 0 | return ml_kem_import_export_list; |
330 | 0 | return NULL; |
331 | 0 | } |
332 | | |
333 | | static int |
334 | | load_slot(OSSL_LIB_CTX *libctx, const char *propq, const char *pname, |
335 | | int selection, MLX_KEY *key, int slot, const uint8_t *in, |
336 | | int mbytes, int xbytes) |
337 | 57 | { |
338 | 57 | EVP_PKEY_CTX *ctx; |
339 | 57 | EVP_PKEY **ppkey; |
340 | 57 | OSSL_PARAM parr[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; |
341 | 57 | const char *alg; |
342 | 57 | char *group = NULL; |
343 | 57 | size_t off, len; |
344 | 57 | void *val; |
345 | 57 | int ml_kem_slot = key->xinfo->ml_kem_slot; |
346 | 57 | int ret = 0; |
347 | | |
348 | 57 | if (slot == ml_kem_slot) { |
349 | 32 | alg = key->minfo->algorithm_name; |
350 | 32 | ppkey = &key->mkey; |
351 | 32 | off = slot * xbytes; |
352 | 32 | len = mbytes; |
353 | 32 | } else { |
354 | 25 | alg = key->xinfo->algorithm_name; |
355 | 25 | group = (char *)key->xinfo->group_name; |
356 | 25 | ppkey = &key->xkey; |
357 | 25 | off = (1 - ml_kem_slot) * mbytes; |
358 | 25 | len = xbytes; |
359 | 25 | } |
360 | 57 | val = (void *)(in + off); |
361 | | |
362 | 57 | if ((ctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propq)) == NULL |
363 | 57 | || EVP_PKEY_fromdata_init(ctx) <= 0) |
364 | 0 | goto err; |
365 | 57 | parr[0] = OSSL_PARAM_construct_octet_string(pname, val, len); |
366 | 57 | if (group != NULL) |
367 | 0 | parr[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
368 | 0 | group, 0); |
369 | 57 | if (EVP_PKEY_fromdata(ctx, ppkey, selection, parr) > 0) |
370 | 50 | ret = 1; |
371 | | |
372 | 57 | err: |
373 | 57 | EVP_PKEY_CTX_free(ctx); |
374 | 57 | return ret; |
375 | 57 | } |
376 | | |
377 | | static int |
378 | | load_keys(MLX_KEY *key, |
379 | | const uint8_t *pubenc, size_t publen, |
380 | | const uint8_t *prvenc, size_t prvlen) |
381 | 32 | { |
382 | 32 | int slot; |
383 | | |
384 | 82 | for (slot = 0; slot < 2; ++slot) { |
385 | 57 | if (prvlen) { |
386 | | /* Ignore public keys when private provided */ |
387 | 0 | if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PRIV_KEY, |
388 | 0 | minimal_selection, key, slot, prvenc, |
389 | 0 | (int)key->minfo->prvkey_bytes, |
390 | 0 | (int)key->xinfo->prvkey_bytes)) |
391 | 0 | goto err; |
392 | 57 | } else if (publen) { |
393 | | /* Absent private key data, import public keys */ |
394 | 57 | if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PUB_KEY, |
395 | 57 | minimal_selection, key, slot, pubenc, |
396 | 57 | (int)key->minfo->pubkey_bytes, |
397 | 57 | (int)key->xinfo->pubkey_bytes)) |
398 | 7 | goto err; |
399 | 57 | } |
400 | 57 | } |
401 | 25 | key->state = prvlen ? MLX_HAVE_PRVKEY : MLX_HAVE_PUBKEY; |
402 | 25 | return 1; |
403 | | |
404 | 7 | err: |
405 | 7 | EVP_PKEY_free(key->mkey); |
406 | 7 | EVP_PKEY_free(key->xkey); |
407 | 7 | key->xkey = key->mkey = NULL; |
408 | 7 | key->state = MLX_HAVE_NOKEYS; |
409 | 7 | return 0; |
410 | 32 | } |
411 | | |
412 | | static int mlx_kem_key_fromdata(MLX_KEY *key, |
413 | | const OSSL_PARAM params[], |
414 | | int include_private) |
415 | 0 | { |
416 | 0 | struct ml_kem_import_export_st p; |
417 | 0 | const void *pubenc = NULL, *prvenc = NULL; |
418 | 0 | size_t pubkey_bytes, prvkey_bytes; |
419 | 0 | size_t publen = 0, prvlen = 0; |
420 | | |
421 | | /* Invalid attempt to mutate a key, what is the right error to report? */ |
422 | 0 | if (key == NULL || mlx_kem_have_pubkey(key)) |
423 | 0 | return 0; |
424 | 0 | pubkey_bytes = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes; |
425 | 0 | prvkey_bytes = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes; |
426 | |
|
427 | 0 | if (!ml_kem_import_export_decoder(params, &p)) |
428 | 0 | return 0; |
429 | | |
430 | | /* What does the caller want to set? */ |
431 | 0 | if (p.pubkey != NULL && OSSL_PARAM_get_octet_string_ptr(p.pubkey, &pubenc, &publen) != 1) |
432 | 0 | return 0; |
433 | 0 | if (include_private |
434 | 0 | && p.privkey != NULL |
435 | 0 | && OSSL_PARAM_get_octet_string_ptr(p.privkey, &prvenc, &prvlen) != 1) |
436 | 0 | return 0; |
437 | | |
438 | | /* The caller MUST specify at least one of the public or private keys. */ |
439 | 0 | if (publen == 0 && prvlen == 0) { |
440 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
441 | 0 | return 0; |
442 | 0 | } |
443 | | |
444 | | /* |
445 | | * When a pubkey is provided, its length MUST be correct, if a private key |
446 | | * is also provided, the public key will be otherwise ignored. We could |
447 | | * look for a matching encoded block, but unclear this is useful. |
448 | | */ |
449 | 0 | if (publen != 0 && publen != pubkey_bytes) { |
450 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
451 | 0 | return 0; |
452 | 0 | } |
453 | 0 | if (prvlen != 0 && prvlen != prvkey_bytes) { |
454 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
455 | 0 | return 0; |
456 | 0 | } |
457 | | |
458 | 0 | return load_keys(key, pubenc, publen, prvenc, prvlen); |
459 | 0 | } |
460 | | |
461 | | static int mlx_kem_import(void *vkey, int selection, const OSSL_PARAM params[]) |
462 | 0 | { |
463 | 0 | MLX_KEY *key = vkey; |
464 | 0 | int include_private; |
465 | |
|
466 | 0 | if (!ossl_prov_is_running() || key == NULL) |
467 | 0 | return 0; |
468 | | |
469 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
470 | 0 | return 0; |
471 | | |
472 | 0 | include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
473 | 0 | return mlx_kem_key_fromdata(key, params, include_private); |
474 | 0 | } |
475 | | |
476 | | static const OSSL_PARAM *mlx_kem_gettable_params(void *provctx) |
477 | 0 | { |
478 | 0 | return mlx_get_params_list; |
479 | 0 | } |
480 | | |
481 | | /* |
482 | | * It is assumed the key is guaranteed non-NULL here, and is from this provider |
483 | | */ |
484 | | static int mlx_kem_get_params(void *vkey, OSSL_PARAM params[]) |
485 | 145k | { |
486 | 145k | MLX_KEY *key = vkey; |
487 | 145k | OSSL_PARAM *pub, *prv = NULL; |
488 | 145k | EXPORT_CB_ARG sub_arg; |
489 | 145k | int selection; |
490 | 145k | struct mlx_get_params_st p; |
491 | | |
492 | 145k | if (key == NULL || !mlx_get_params_decoder(params, &p)) |
493 | 0 | return 0; |
494 | | |
495 | | /* The reported "bit" count is those of the ML-KEM key */ |
496 | 145k | if (p.bits != NULL) |
497 | 48.0k | if (!OSSL_PARAM_set_int(p.bits, key->minfo->bits)) |
498 | 0 | return 0; |
499 | | |
500 | | /* The reported security bits are those of the ML-KEM key */ |
501 | 145k | if (p.secbits != NULL) |
502 | 48.0k | if (!OSSL_PARAM_set_int(p.secbits, key->minfo->secbits)) |
503 | 0 | return 0; |
504 | | |
505 | | /* The reported security category are those of the ML-KEM key */ |
506 | 145k | if (p.seccat != NULL) |
507 | 48.0k | if (!OSSL_PARAM_set_int(p.seccat, key->minfo->security_category)) |
508 | 0 | return 0; |
509 | | |
510 | | /* The ciphertext sizes are additive */ |
511 | 145k | if (p.maxsize != NULL) |
512 | 48.0k | if (!OSSL_PARAM_set_size_t(p.maxsize, key->minfo->ctext_bytes + key->xinfo->pubkey_bytes)) |
513 | 0 | return 0; |
514 | | |
515 | 145k | if (!mlx_kem_have_pubkey(key)) |
516 | 39 | return 1; |
517 | | |
518 | 145k | memset(&sub_arg, 0, sizeof(sub_arg)); |
519 | 145k | if ((pub = p.pub) != NULL) { |
520 | 97.2k | size_t publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes; |
521 | | |
522 | 97.2k | if (pub->data_type != OSSL_PARAM_OCTET_STRING) |
523 | 0 | return 0; |
524 | 97.2k | pub->return_size = publen; |
525 | 97.2k | if (pub->data == NULL) { |
526 | 48.6k | pub = NULL; |
527 | 48.6k | } else if (pub->data_size < publen) { |
528 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
529 | 0 | "public key output buffer too short: %lu < %lu", |
530 | 0 | (unsigned long)pub->data_size, |
531 | 0 | (unsigned long)publen); |
532 | 0 | return 0; |
533 | 48.6k | } else { |
534 | 48.6k | sub_arg.pubenc = pub->data; |
535 | 48.6k | } |
536 | 97.2k | } |
537 | 145k | if (mlx_kem_have_prvkey(key)) { |
538 | 145k | if ((prv = p.priv) != NULL) { |
539 | 0 | size_t prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes; |
540 | |
|
541 | 0 | if (prv->data_type != OSSL_PARAM_OCTET_STRING) |
542 | 0 | return 0; |
543 | 0 | prv->return_size = prvlen; |
544 | 0 | if (prv->data == NULL) { |
545 | 0 | prv = NULL; |
546 | 0 | } else if (prv->data_size < prvlen) { |
547 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL, |
548 | 0 | "private key output buffer too short: %lu < %lu", |
549 | 0 | (unsigned long)prv->data_size, |
550 | 0 | (unsigned long)prvlen); |
551 | 0 | return 0; |
552 | 0 | } else { |
553 | 0 | sub_arg.prvenc = prv->data; |
554 | 0 | } |
555 | 0 | } |
556 | 145k | } |
557 | 145k | if (pub == NULL && prv == NULL) |
558 | 96.6k | return 1; |
559 | | |
560 | 48.6k | selection = prv == NULL ? 0 : OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
561 | 48.6k | selection |= pub == NULL ? 0 : OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
562 | 48.6k | if (key->xinfo->group_name != NULL) |
563 | 3 | selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; |
564 | | |
565 | | /* Extract sub-component key material */ |
566 | 48.6k | if (!export_sub(&sub_arg, selection, key) |
567 | 48.6k | || (pub != NULL && sub_arg.pubcount != 2) |
568 | 48.6k | || (prv != NULL && sub_arg.prvcount != 2)) { |
569 | | /* Erase any partial key material on failure */ |
570 | 0 | if (sub_arg.pubenc != NULL) |
571 | 0 | OPENSSL_cleanse(sub_arg.pubenc, |
572 | 0 | key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes); |
573 | 0 | if (sub_arg.prvenc != NULL) |
574 | 0 | OPENSSL_cleanse(sub_arg.prvenc, |
575 | 0 | key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes); |
576 | 0 | return 0; |
577 | 0 | } |
578 | | |
579 | 48.6k | return 1; |
580 | 48.6k | } |
581 | | |
582 | | static const OSSL_PARAM *mlx_kem_settable_params(void *provctx) |
583 | 0 | { |
584 | 0 | return mlx_set_params_list; |
585 | 0 | } |
586 | | |
587 | | static int mlx_kem_set_params(void *vkey, const OSSL_PARAM params[]) |
588 | 39 | { |
589 | 39 | MLX_KEY *key = vkey; |
590 | 39 | struct mlx_set_params_st p; |
591 | 39 | const void *pubenc = NULL; |
592 | 39 | size_t publen = 0; |
593 | | |
594 | 39 | if (key == NULL || !mlx_set_params_decoder(params, &p)) |
595 | 0 | return 0; |
596 | | |
597 | 39 | if (p.propq != NULL) { |
598 | 0 | OPENSSL_free(key->propq); |
599 | 0 | key->propq = NULL; |
600 | 0 | if (!OSSL_PARAM_get_utf8_string(p.propq, &key->propq, 0)) |
601 | 0 | return 0; |
602 | 0 | } |
603 | | |
604 | 39 | if (p.pub == NULL) |
605 | 0 | return 1; |
606 | | |
607 | | /* Key mutation is reportedly generally not allowed */ |
608 | 39 | if (mlx_kem_have_pubkey(key)) { |
609 | 0 | ERR_raise_data(ERR_LIB_PROV, |
610 | 0 | PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE, |
611 | 0 | "keys cannot be mutated"); |
612 | 0 | return 0; |
613 | 0 | } |
614 | | /* An unlikely failure mode is the parameter having some unexpected type */ |
615 | 39 | if (!OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen)) |
616 | 0 | return 0; |
617 | | |
618 | 39 | if (publen != key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes) { |
619 | 7 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
620 | 7 | return 0; |
621 | 7 | } |
622 | | |
623 | 32 | return load_keys(key, pubenc, publen, NULL, 0); |
624 | 39 | } |
625 | | |
626 | | static int mlx_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[]) |
627 | 96.0k | { |
628 | 96.0k | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
629 | 96.0k | struct mlx_gen_set_params_st p; |
630 | | |
631 | 96.0k | if (gctx == NULL || !mlx_gen_set_params_decoder(params, &p)) |
632 | 0 | return 0; |
633 | | |
634 | 96.0k | if (p.propq != NULL) { |
635 | 0 | if (p.propq->data_type != OSSL_PARAM_UTF8_STRING) |
636 | 0 | return 0; |
637 | 0 | OPENSSL_free(gctx->propq); |
638 | 0 | if ((gctx->propq = OPENSSL_strdup(p.propq->data)) == NULL) |
639 | 0 | return 0; |
640 | 0 | } |
641 | 96.0k | return 1; |
642 | 96.0k | } |
643 | | |
644 | | static void *mlx_kem_gen_init(int evp_type, OSSL_LIB_CTX *libctx, |
645 | | int selection, const OSSL_PARAM params[]) |
646 | 62.9k | { |
647 | 62.9k | PROV_ML_KEM_GEN_CTX *gctx = NULL; |
648 | | |
649 | | /* |
650 | | * We can only generate private keys, check that the selection is |
651 | | * appropriate. |
652 | | */ |
653 | 62.9k | if (!ossl_prov_is_running() |
654 | 62.9k | || (selection & minimal_selection) == 0 |
655 | 62.9k | || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL) |
656 | 0 | return NULL; |
657 | | |
658 | 62.9k | gctx->evp_type = evp_type; |
659 | 62.9k | gctx->libctx = libctx; |
660 | 62.9k | gctx->selection = selection; |
661 | 62.9k | if (mlx_kem_gen_set_params(gctx, params)) |
662 | 62.9k | return gctx; |
663 | | |
664 | 0 | mlx_kem_gen_cleanup(gctx); |
665 | 0 | return NULL; |
666 | 62.9k | } |
667 | | |
668 | | static const OSSL_PARAM *mlx_kem_gen_settable_params(ossl_unused void *vgctx, |
669 | | ossl_unused void *provctx) |
670 | 0 | { |
671 | 0 | return mlx_gen_set_params_list; |
672 | 0 | } |
673 | | |
674 | | static void *mlx_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg) |
675 | 62.9k | { |
676 | 62.9k | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
677 | 62.9k | MLX_KEY *key; |
678 | 62.9k | char *propq; |
679 | | |
680 | 62.9k | if (gctx == NULL |
681 | 62.9k | || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) |
682 | 0 | return NULL; |
683 | | |
684 | | /* Lose ownership of propq */ |
685 | 62.9k | propq = gctx->propq; |
686 | 62.9k | gctx->propq = NULL; |
687 | 62.9k | if ((key = mlx_kem_key_new(gctx->evp_type, gctx->libctx, propq)) == NULL) |
688 | 0 | return NULL; |
689 | | |
690 | 62.9k | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
691 | 40 | return key; |
692 | | |
693 | | /* For now, using the same "propq" for all components */ |
694 | 62.8k | key->mkey = EVP_PKEY_Q_keygen(key->libctx, key->propq, |
695 | 62.8k | key->minfo->algorithm_name); |
696 | 62.8k | key->xkey = EVP_PKEY_Q_keygen(key->libctx, key->propq, |
697 | 62.8k | key->xinfo->algorithm_name, |
698 | 62.8k | key->xinfo->group_name); |
699 | 62.8k | if (key->mkey != NULL && key->xkey != NULL) { |
700 | 62.8k | key->state = MLX_HAVE_PRVKEY; |
701 | 62.8k | return key; |
702 | 62.8k | } |
703 | | |
704 | 0 | mlx_kem_key_free(key); |
705 | 0 | return NULL; |
706 | 62.8k | } |
707 | | |
708 | | static void mlx_kem_gen_cleanup(void *vgctx) |
709 | 62.9k | { |
710 | 62.9k | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
711 | | |
712 | 62.9k | if (gctx == NULL) |
713 | 0 | return; |
714 | 62.9k | OPENSSL_free(gctx->propq); |
715 | 62.9k | OPENSSL_free(gctx); |
716 | 62.9k | } |
717 | | |
718 | | static void *mlx_kem_dup(const void *vkey, int selection) |
719 | 0 | { |
720 | 0 | const MLX_KEY *key = vkey; |
721 | 0 | MLX_KEY *ret; |
722 | |
|
723 | 0 | if (!ossl_prov_is_running() |
724 | 0 | || (ret = OPENSSL_memdup(key, sizeof(*ret))) == NULL) |
725 | 0 | return NULL; |
726 | | |
727 | 0 | ret->mkey = ret->xkey = NULL; |
728 | |
|
729 | 0 | if (key->propq != NULL |
730 | 0 | && (ret->propq = OPENSSL_strdup(key->propq)) == NULL) { |
731 | 0 | OPENSSL_free(ret); |
732 | 0 | return NULL; |
733 | 0 | } |
734 | | |
735 | | /* Absent key material, nothing left to do */ |
736 | 0 | if (key->mkey == NULL) { |
737 | 0 | if (key->xkey == NULL) |
738 | 0 | return ret; |
739 | | /* Fail if the source key is an inconsistent state */ |
740 | 0 | OPENSSL_free(ret->propq); |
741 | 0 | OPENSSL_free(ret); |
742 | 0 | return NULL; |
743 | 0 | } |
744 | | |
745 | 0 | switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) { |
746 | 0 | case 0: |
747 | 0 | ret->state = MLX_HAVE_NOKEYS; |
748 | 0 | return ret; |
749 | 0 | case OSSL_KEYMGMT_SELECT_KEYPAIR: |
750 | 0 | ret->mkey = EVP_PKEY_dup(key->mkey); |
751 | 0 | ret->xkey = EVP_PKEY_dup(key->xkey); |
752 | 0 | if (ret->xkey != NULL && ret->mkey != NULL) |
753 | 0 | return ret; |
754 | 0 | break; |
755 | 0 | default: |
756 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_UNSUPPORTED_SELECTION, |
757 | 0 | "duplication of partial key material not supported"); |
758 | 0 | break; |
759 | 0 | } |
760 | | |
761 | 0 | mlx_kem_key_free(ret); |
762 | 0 | return NULL; |
763 | 0 | } |
764 | | |
765 | | #define DECLARE_DISPATCH(name, variant) \ |
766 | | static OSSL_FUNC_keymgmt_new_fn mlx_##name##_kem_new; \ |
767 | | static void *mlx_##name##_kem_new(void *provctx) \ |
768 | 0 | { \ |
769 | 0 | OSSL_LIB_CTX *libctx; \ |
770 | 0 | \ |
771 | 0 | libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \ |
772 | 0 | return mlx_kem_key_new(variant, libctx, NULL); \ |
773 | 0 | } \ |
774 | | static OSSL_FUNC_keymgmt_gen_init_fn mlx_##name##_kem_gen_init; \ |
775 | | static void *mlx_##name##_kem_gen_init(void *provctx, int selection, \ |
776 | | const OSSL_PARAM params[]) \ |
777 | 62.9k | { \ |
778 | 62.9k | OSSL_LIB_CTX *libctx; \ |
779 | 62.9k | \ |
780 | 62.9k | libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \ |
781 | 62.9k | return mlx_kem_gen_init(variant, libctx, selection, params); \ |
782 | 62.9k | } \ |
783 | | const OSSL_DISPATCH ossl_mlx_##name##_kem_kmgmt_functions[] = { \ |
784 | | { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC)mlx_##name##_kem_new }, \ |
785 | | { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC)mlx_kem_key_free }, \ |
786 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC)mlx_kem_get_params }, \ |
787 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC)mlx_kem_gettable_params }, \ |
788 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC)mlx_kem_set_params }, \ |
789 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC)mlx_kem_settable_params }, \ |
790 | | { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC)mlx_kem_has }, \ |
791 | | { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC)mlx_kem_match }, \ |
792 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC)mlx_##name##_kem_gen_init }, \ |
793 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC)mlx_kem_gen_set_params }, \ |
794 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC)mlx_kem_gen_settable_params }, \ |
795 | | { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC)mlx_kem_gen }, \ |
796 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC)mlx_kem_gen_cleanup }, \ |
797 | | { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC)mlx_kem_dup }, \ |
798 | | { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC)mlx_kem_import }, \ |
799 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC)mlx_kem_imexport_types }, \ |
800 | | { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC)mlx_kem_export }, \ |
801 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC)mlx_kem_imexport_types }, \ |
802 | | OSSL_DISPATCH_END \ |
803 | | } |
804 | | /* See |hybrid_vtable| above */ |
805 | 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 | 805 | | DECLARE_DISPATCH(p256, 0); |
|
806 | 0 | DECLARE_DISPATCH(p384, 1); Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_new Unexecuted instantiation: mlx_kmgmt.c:mlx_p384_kem_gen_init |
807 | | #if !defined(OPENSSL_NO_ECX) |
808 | 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 | 808 | | DECLARE_DISPATCH(x25519, 2); |
|
809 | 0 | DECLARE_DISPATCH(x448, 3); Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_new Unexecuted instantiation: mlx_kmgmt.c:mlx_x448_kem_gen_init |
810 | | #endif |
811 | | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_SM2) |
812 | | DECLARE_DISPATCH(curve_sm2, 4); Unexecuted instantiation: mlx_kmgmt.c:mlx_curve_sm2_kem_new mlx_kmgmt.c:mlx_curve_sm2_kem_gen_init Line | Count | Source | 812 | | DECLARE_DISPATCH(curve_sm2, 4); |
|
813 | | #endif |