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