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