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