/src/openssl/providers/implementations/keymgmt/ml_kem_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 | | |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/params.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/proverr.h> |
16 | | #include <openssl/provider.h> |
17 | | #include <openssl/rand.h> |
18 | | #include <openssl/self_test.h> |
19 | | #include <openssl/param_build.h> |
20 | | #include "crypto/ml_kem.h" |
21 | | #include "internal/fips.h" |
22 | | #include "internal/param_build_set.h" |
23 | | #include "internal/param_names.h" |
24 | | #include "prov/implementations.h" |
25 | | #include "prov/providercommon.h" |
26 | | #include "prov/provider_ctx.h" |
27 | | #include "prov/securitycheck.h" |
28 | | #include "prov/ml_kem.h" |
29 | | |
30 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_512_new; |
31 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_768_new; |
32 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_1024_new; |
33 | | static OSSL_FUNC_keymgmt_gen_fn ml_kem_gen; |
34 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_512_gen_init; |
35 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_768_gen_init; |
36 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_1024_gen_init; |
37 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_kem_gen_cleanup; |
38 | | static OSSL_FUNC_keymgmt_gen_set_params_fn ml_kem_gen_set_params; |
39 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_kem_gen_settable_params; |
40 | | #ifndef FIPS_MODULE |
41 | | static OSSL_FUNC_keymgmt_load_fn ml_kem_load; |
42 | | #endif |
43 | | static OSSL_FUNC_keymgmt_get_params_fn ml_kem_get_params; |
44 | | static OSSL_FUNC_keymgmt_gettable_params_fn ml_kem_gettable_params; |
45 | | static OSSL_FUNC_keymgmt_set_params_fn ml_kem_set_params; |
46 | | static OSSL_FUNC_keymgmt_settable_params_fn ml_kem_settable_params; |
47 | | static OSSL_FUNC_keymgmt_has_fn ml_kem_has; |
48 | | static OSSL_FUNC_keymgmt_match_fn ml_kem_match; |
49 | | static OSSL_FUNC_keymgmt_validate_fn ml_kem_validate; |
50 | | static OSSL_FUNC_keymgmt_import_fn ml_kem_import; |
51 | | static OSSL_FUNC_keymgmt_export_fn ml_kem_export; |
52 | | static OSSL_FUNC_keymgmt_import_types_fn ml_kem_imexport_types; |
53 | | static OSSL_FUNC_keymgmt_export_types_fn ml_kem_imexport_types; |
54 | | static OSSL_FUNC_keymgmt_dup_fn ml_kem_dup; |
55 | | |
56 | | static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS |
57 | | | OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
58 | | |
59 | | typedef struct ml_kem_gen_ctx_st { |
60 | | PROV_CTX *provctx; |
61 | | char *propq; |
62 | | int selection; |
63 | | int evp_type; |
64 | | uint8_t seedbuf[ML_KEM_SEED_BYTES]; |
65 | | uint8_t *seed; |
66 | | } PROV_ML_KEM_GEN_CTX; |
67 | | |
68 | | static int ml_kem_pairwise_test(const ML_KEM_KEY *key, int key_flags) |
69 | 0 | { |
70 | | #ifdef FIPS_MODULE |
71 | | OSSL_SELF_TEST *st = NULL; |
72 | | OSSL_CALLBACK *cb = NULL; |
73 | | void *cbarg = NULL; |
74 | | #endif |
75 | 0 | unsigned char entropy[ML_KEM_RANDOM_BYTES]; |
76 | 0 | unsigned char secret[ML_KEM_SHARED_SECRET_BYTES]; |
77 | 0 | unsigned char out[ML_KEM_SHARED_SECRET_BYTES]; |
78 | 0 | unsigned char *ctext = NULL; |
79 | 0 | const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key); |
80 | 0 | int operation_result = 0; |
81 | 0 | int ret = 0; |
82 | | |
83 | | /* Unless we have both a public and private key, we can't do the test */ |
84 | 0 | if (!ossl_ml_kem_have_prvkey(key) |
85 | 0 | || !ossl_ml_kem_have_pubkey(key) |
86 | 0 | || (key_flags & ML_KEM_KEY_PCT_TYPE) == 0) |
87 | 0 | return 1; |
88 | | #ifdef FIPS_MODULE |
89 | | /* During self test, it is a waste to do this test */ |
90 | | if (ossl_fips_self_testing()) |
91 | | return 1; |
92 | | |
93 | | /* |
94 | | * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st` |
95 | | * is NULL. |
96 | | */ |
97 | | OSSL_SELF_TEST_get_callback(key->libctx, &cb, &cbarg); |
98 | | |
99 | | st = OSSL_SELF_TEST_new(cb, cbarg); |
100 | | if (st == NULL) |
101 | | return 0; |
102 | | |
103 | | OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT, |
104 | | OSSL_SELF_TEST_DESC_PCT_ML_KEM); |
105 | | #endif /* FIPS_MODULE */ |
106 | | |
107 | 0 | ctext = OPENSSL_malloc(v->ctext_bytes); |
108 | 0 | if (ctext == NULL) |
109 | 0 | goto err; |
110 | | |
111 | 0 | memset(out, 0, sizeof(out)); |
112 | | |
113 | | /* |
114 | | * The pairwise test is skipped unless either RANDOM or FIXED entropy PCTs |
115 | | * are enabled. |
116 | | */ |
117 | 0 | if (key_flags & ML_KEM_KEY_RANDOM_PCT) { |
118 | 0 | operation_result = ossl_ml_kem_encap_rand(ctext, v->ctext_bytes, |
119 | 0 | secret, sizeof(secret), key); |
120 | 0 | } else { |
121 | 0 | memset(entropy, 0125, sizeof(entropy)); |
122 | 0 | operation_result = ossl_ml_kem_encap_seed(ctext, v->ctext_bytes, |
123 | 0 | secret, sizeof(secret), |
124 | 0 | entropy, sizeof(entropy), |
125 | 0 | key); |
126 | 0 | } |
127 | 0 | if (operation_result != 1) |
128 | 0 | goto err; |
129 | | |
130 | | #ifdef FIPS_MODULE |
131 | | OSSL_SELF_TEST_oncorrupt_byte(st, ctext); |
132 | | #endif |
133 | | |
134 | 0 | operation_result = ossl_ml_kem_decap(out, sizeof(out), ctext, v->ctext_bytes, |
135 | 0 | key); |
136 | 0 | if (operation_result != 1 || memcmp(out, secret, sizeof(out)) != 0) |
137 | 0 | goto err; |
138 | | |
139 | 0 | ret = 1; |
140 | 0 | err: |
141 | | #ifdef FIPS_MODULE |
142 | | OSSL_SELF_TEST_onend(st, ret); |
143 | | OSSL_SELF_TEST_free(st); |
144 | | #else |
145 | 0 | if (ret == 0) { |
146 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
147 | 0 | "public part of %s private key fails to match private", |
148 | 0 | v->algorithm_name); |
149 | 0 | } |
150 | 0 | #endif |
151 | 0 | OPENSSL_free(ctext); |
152 | 0 | return ret; |
153 | 0 | } |
154 | | |
155 | | ML_KEM_KEY *ossl_prov_ml_kem_new(PROV_CTX *ctx, const char *propq, int evp_type) |
156 | 0 | { |
157 | 0 | ML_KEM_KEY *key; |
158 | |
|
159 | 0 | if (!ossl_prov_is_running()) |
160 | 0 | return NULL; |
161 | | /* |
162 | | * When decoding, if the key ends up "loaded" into the same provider, these |
163 | | * are the correct config settings, otherwise, new values will be assigned |
164 | | * on import into a different provider. The "load" API does not pass along |
165 | | * the provider context. |
166 | | */ |
167 | 0 | if ((key = ossl_ml_kem_key_new(PROV_LIBCTX_OF(ctx), propq, evp_type)) != NULL) { |
168 | 0 | const char *pct_type = ossl_prov_ctx_get_param( |
169 | 0 | ctx, OSSL_PKEY_PARAM_ML_KEM_IMPORT_PCT_TYPE, "random"); |
170 | |
|
171 | 0 | if (ossl_prov_ctx_get_bool_param( |
172 | 0 | ctx, OSSL_PKEY_PARAM_ML_KEM_RETAIN_SEED, 1)) |
173 | 0 | key->prov_flags |= ML_KEM_KEY_RETAIN_SEED; |
174 | 0 | else |
175 | 0 | key->prov_flags &= ~ML_KEM_KEY_RETAIN_SEED; |
176 | 0 | if (ossl_prov_ctx_get_bool_param( |
177 | 0 | ctx, OSSL_PKEY_PARAM_ML_KEM_PREFER_SEED, 1)) |
178 | 0 | key->prov_flags |= ML_KEM_KEY_PREFER_SEED; |
179 | 0 | else |
180 | 0 | key->prov_flags &= ~ML_KEM_KEY_PREFER_SEED; |
181 | 0 | if (OPENSSL_strcasecmp(pct_type, "random") == 0) |
182 | 0 | key->prov_flags |= ML_KEM_KEY_RANDOM_PCT; |
183 | 0 | else if (OPENSSL_strcasecmp(pct_type, "fixed") == 0) |
184 | 0 | key->prov_flags |= ML_KEM_KEY_FIXED_PCT; |
185 | 0 | else |
186 | 0 | key->prov_flags &= ~ML_KEM_KEY_PCT_TYPE; |
187 | 0 | } |
188 | 0 | return key; |
189 | 0 | } |
190 | | |
191 | | static int ml_kem_has(const void *vkey, int selection) |
192 | 0 | { |
193 | 0 | const ML_KEM_KEY *key = vkey; |
194 | | |
195 | | /* A NULL key MUST fail to have anything */ |
196 | 0 | if (!ossl_prov_is_running() || key == NULL) |
197 | 0 | return 0; |
198 | | |
199 | 0 | switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) { |
200 | 0 | case 0: |
201 | 0 | return 1; |
202 | 0 | case OSSL_KEYMGMT_SELECT_PUBLIC_KEY: |
203 | 0 | return ossl_ml_kem_have_pubkey(key); |
204 | 0 | default: |
205 | 0 | return ossl_ml_kem_have_prvkey(key); |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | | static int ml_kem_match(const void *vkey1, const void *vkey2, int selection) |
210 | 0 | { |
211 | 0 | const ML_KEM_KEY *key1 = vkey1; |
212 | 0 | const ML_KEM_KEY *key2 = vkey2; |
213 | |
|
214 | 0 | if (!ossl_prov_is_running()) |
215 | 0 | return 0; |
216 | | |
217 | | /* All we have that can be compared is key material */ |
218 | 0 | if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR)) |
219 | 0 | return 1; |
220 | | |
221 | 0 | return ossl_ml_kem_pubkey_cmp(key1, key2); |
222 | 0 | } |
223 | | |
224 | | static int ml_kem_validate(const void *vkey, int selection, int check_type) |
225 | 0 | { |
226 | 0 | const ML_KEM_KEY *key = vkey; |
227 | |
|
228 | 0 | if (!ml_kem_has(key, selection)) |
229 | 0 | return 0; |
230 | | |
231 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) |
232 | 0 | return ml_kem_pairwise_test(key, ML_KEM_KEY_RANDOM_PCT); |
233 | 0 | return 1; |
234 | 0 | } |
235 | | |
236 | | static int ml_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb, |
237 | | void *cbarg) |
238 | 0 | { |
239 | 0 | ML_KEM_KEY *key = vkey; |
240 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
241 | 0 | OSSL_PARAM *params = NULL; |
242 | 0 | const ML_KEM_VINFO *v; |
243 | 0 | uint8_t *pubenc = NULL, *prvenc = NULL, *seedenc = NULL; |
244 | 0 | size_t prvlen = 0, seedlen = 0; |
245 | 0 | int ret = 0; |
246 | |
|
247 | 0 | if (!ossl_prov_is_running() || key == NULL) |
248 | 0 | return 0; |
249 | | |
250 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
251 | 0 | return 0; |
252 | | |
253 | 0 | v = ossl_ml_kem_key_vinfo(key); |
254 | 0 | if (!ossl_ml_kem_have_pubkey(key)) { |
255 | | /* Fail when no key material can be returned */ |
256 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0 |
257 | 0 | || !ossl_ml_kem_decoded_key(key)) { |
258 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
259 | 0 | return 0; |
260 | 0 | } |
261 | 0 | } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
262 | 0 | pubenc = OPENSSL_malloc(v->pubkey_bytes); |
263 | 0 | if (pubenc == NULL |
264 | 0 | || !ossl_ml_kem_encode_public_key(pubenc, v->pubkey_bytes, key)) |
265 | 0 | goto err; |
266 | 0 | } |
267 | | |
268 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
269 | | /* |
270 | | * The seed and/or private key material are allocated on the secure |
271 | | * heap if configured, ossl_param_build_set_octet_string(), will then |
272 | | * also use the secure heap. |
273 | | */ |
274 | 0 | if (ossl_ml_kem_have_seed(key)) { |
275 | 0 | seedlen = ML_KEM_SEED_BYTES; |
276 | 0 | if ((seedenc = OPENSSL_secure_zalloc(seedlen)) == NULL |
277 | 0 | || !ossl_ml_kem_encode_seed(seedenc, seedlen, key)) |
278 | 0 | goto err; |
279 | 0 | } |
280 | 0 | if (ossl_ml_kem_have_prvkey(key)) { |
281 | 0 | prvlen = v->prvkey_bytes; |
282 | 0 | if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL |
283 | 0 | || !ossl_ml_kem_encode_private_key(prvenc, prvlen, key)) |
284 | 0 | goto err; |
285 | 0 | } else if (ossl_ml_kem_have_dkenc(key)) { |
286 | 0 | prvlen = v->prvkey_bytes; |
287 | 0 | if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL) |
288 | 0 | goto err; |
289 | 0 | memcpy(prvenc, key->encoded_dk, prvlen); |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
294 | 0 | if (tmpl == NULL) |
295 | 0 | goto err; |
296 | | |
297 | | /* The (d, z) seed, when available and private keys are requested. */ |
298 | 0 | if (seedenc != NULL |
299 | 0 | && !ossl_param_build_set_octet_string( |
300 | 0 | tmpl, params, OSSL_PKEY_PARAM_ML_KEM_SEED, seedenc, seedlen)) |
301 | 0 | goto err; |
302 | | |
303 | | /* The private key in the FIPS 203 |dk| format, when requested. */ |
304 | 0 | if (prvenc != NULL |
305 | 0 | && !ossl_param_build_set_octet_string( |
306 | 0 | tmpl, params, OSSL_PKEY_PARAM_PRIV_KEY, prvenc, prvlen)) |
307 | 0 | goto err; |
308 | | |
309 | | /* The public key on request; it is always available when either is */ |
310 | 0 | if (pubenc != NULL |
311 | 0 | && !ossl_param_build_set_octet_string( |
312 | 0 | tmpl, params, OSSL_PKEY_PARAM_PUB_KEY, pubenc, v->pubkey_bytes)) |
313 | 0 | goto err; |
314 | | |
315 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
316 | 0 | if (params == NULL) |
317 | 0 | goto err; |
318 | | |
319 | 0 | ret = param_cb(params, cbarg); |
320 | 0 | OSSL_PARAM_free(params); |
321 | |
|
322 | 0 | err: |
323 | 0 | OSSL_PARAM_BLD_free(tmpl); |
324 | 0 | OPENSSL_secure_clear_free(seedenc, seedlen); |
325 | 0 | OPENSSL_secure_clear_free(prvenc, prvlen); |
326 | 0 | OPENSSL_free(pubenc); |
327 | 0 | return ret; |
328 | 0 | } |
329 | | |
330 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
331 | | static const OSSL_PARAM ml_kem_key_types[] = { |
332 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0), |
333 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), |
334 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), |
335 | | OSSL_PARAM_END |
336 | | }; |
337 | | |
338 | | static int ml_kem_key_types_find_pidx(const char *s) |
339 | 0 | { |
340 | 0 | switch(s[0]) { |
341 | 0 | default: |
342 | 0 | break; |
343 | 0 | case 'p': |
344 | 0 | switch(s[1]) { |
345 | 0 | default: |
346 | 0 | break; |
347 | 0 | case 'r': |
348 | 0 | if (strcmp("iv", s + 2) == 0) |
349 | 0 | return PIDX_PKEY_PARAM_PRIV_KEY; |
350 | 0 | break; |
351 | 0 | case 'u': |
352 | 0 | if (strcmp("b", s + 2) == 0) |
353 | 0 | return PIDX_PKEY_PARAM_PUB_KEY; |
354 | 0 | } |
355 | 0 | break; |
356 | 0 | case 's': |
357 | 0 | if (strcmp("eed", s + 1) == 0) |
358 | 0 | return PIDX_PKEY_PARAM_ML_KEM_SEED; |
359 | 0 | } |
360 | 0 | return -1; |
361 | 0 | } |
362 | | |
363 | | /* End of machine generated */ |
364 | | |
365 | | static const OSSL_PARAM *ml_kem_imexport_types(int selection) |
366 | 0 | { |
367 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
368 | 0 | return ml_kem_key_types; |
369 | 0 | return NULL; |
370 | 0 | } |
371 | | |
372 | | static int check_seed(const uint8_t *seed, const uint8_t *prvenc, |
373 | | ML_KEM_KEY *key) |
374 | 0 | { |
375 | 0 | size_t zlen = ML_KEM_RANDOM_BYTES; |
376 | |
|
377 | 0 | if (memcmp(seed + ML_KEM_SEED_BYTES - zlen, |
378 | 0 | prvenc + key->vinfo->prvkey_bytes - zlen, zlen) == 0) |
379 | 0 | return 1; |
380 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
381 | 0 | "private %s key implicit rejection secret does" |
382 | 0 | " not match seed", key->vinfo->algorithm_name); |
383 | 0 | return 0; |
384 | 0 | } |
385 | | |
386 | | static int check_prvenc(const uint8_t *prvenc, ML_KEM_KEY *key) |
387 | 0 | { |
388 | 0 | size_t len = key->vinfo->prvkey_bytes; |
389 | 0 | uint8_t *buf = OPENSSL_malloc(len); |
390 | 0 | int ret = 0; |
391 | |
|
392 | 0 | if (buf != NULL |
393 | 0 | && ossl_ml_kem_encode_private_key(buf, len, key)) |
394 | 0 | ret = memcmp(buf, prvenc, len) == 0; |
395 | 0 | OPENSSL_clear_free(buf, len); |
396 | 0 | if (ret) |
397 | 0 | return 1; |
398 | | |
399 | 0 | if (buf != NULL) |
400 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
401 | 0 | "explicit %s private key does not match seed", |
402 | 0 | key->vinfo->algorithm_name); |
403 | 0 | ossl_ml_kem_key_reset(key); |
404 | 0 | return 0; |
405 | 0 | } |
406 | | |
407 | | static int ml_kem_key_fromdata(ML_KEM_KEY *key, |
408 | | const OSSL_PARAM params[], |
409 | | int include_private) |
410 | 0 | { |
411 | 0 | const OSSL_PARAM *p = NULL; |
412 | 0 | const void *pubenc = NULL, *prvenc = NULL, *seedenc = NULL; |
413 | 0 | size_t publen = 0, prvlen = 0, seedlen = 0, puboff; |
414 | 0 | const ML_KEM_VINFO *v; |
415 | | |
416 | | /* Invalid attempt to mutate a key, what is the right error to report? */ |
417 | 0 | if (key == NULL || ossl_ml_kem_have_pubkey(key)) |
418 | 0 | return 0; |
419 | 0 | v = ossl_ml_kem_key_vinfo(key); |
420 | | |
421 | | /* |
422 | | * When a private key is provided, without a seed, any public key also |
423 | | * provided will be ignored (apart from length), just as with the seed. |
424 | | */ |
425 | 0 | for (p = params; p->key != NULL; p++) |
426 | 0 | switch (ml_kem_key_types_find_pidx(p->key)) { |
427 | 0 | default: |
428 | 0 | break; |
429 | | |
430 | 0 | case PIDX_PKEY_PARAM_ML_KEM_SEED: |
431 | 0 | if (include_private) { |
432 | | /* |
433 | | * When a seed is provided, the private and public keys may be ignored, |
434 | | * after validating just their lengths. Comparing encodings or hashes |
435 | | * when applicable is possible, but not currently implemented. |
436 | | */ |
437 | 0 | if (OSSL_PARAM_get_octet_string_ptr(p, &seedenc, &seedlen) != 1) |
438 | 0 | return 0; |
439 | 0 | if (seedlen != 0 && seedlen != ML_KEM_SEED_BYTES) { |
440 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
441 | 0 | return 0; |
442 | 0 | } |
443 | 0 | } |
444 | 0 | break; |
445 | | |
446 | 0 | case PIDX_PKEY_PARAM_PRIV_KEY: |
447 | 0 | if (include_private) { |
448 | 0 | if (OSSL_PARAM_get_octet_string_ptr(p, &prvenc, &prvlen) != 1) |
449 | 0 | return 0; |
450 | 0 | if (prvlen != 0 && prvlen != v->prvkey_bytes) { |
451 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
452 | 0 | return 0; |
453 | 0 | } |
454 | 0 | } |
455 | 0 | break; |
456 | | |
457 | | /* Used only when no seed or private key is provided. */ |
458 | 0 | case PIDX_PKEY_PARAM_PUB_KEY: |
459 | 0 | if (OSSL_PARAM_get_octet_string_ptr(p, &pubenc, &publen) != 1) |
460 | 0 | return 0; |
461 | 0 | if (publen != 0 && publen != v->pubkey_bytes) { |
462 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
463 | 0 | return 0; |
464 | 0 | } |
465 | 0 | break; |
466 | 0 | } |
467 | | |
468 | | /* The caller MUST specify at least one of seed, private or public keys. */ |
469 | 0 | if (seedlen == 0 && publen == 0 && prvlen == 0) { |
470 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
471 | 0 | return 0; |
472 | 0 | } |
473 | | |
474 | | /* Check any explicit public key against embedded value in private key */ |
475 | 0 | if (publen > 0 && prvlen > 0) { |
476 | | /* point to the ek offset in dk = DKpke||ek||H(ek)||z */ |
477 | 0 | puboff = prvlen - ML_KEM_RANDOM_BYTES - ML_KEM_PKHASH_BYTES - publen; |
478 | 0 | if (memcmp(pubenc, (unsigned char *)prvenc + puboff, publen) != 0) { |
479 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
480 | 0 | "explicit %s public key does not match private", |
481 | 0 | v->algorithm_name); |
482 | 0 | return 0; |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | 0 | if (seedlen != 0 |
487 | 0 | && (prvlen == 0 || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) { |
488 | 0 | if (prvlen != 0 && !check_seed(seedenc, prvenc, key)) |
489 | 0 | return 0; |
490 | 0 | if (!ossl_ml_kem_set_seed(seedenc, seedlen, key) |
491 | 0 | || !ossl_ml_kem_genkey(NULL, 0, key)) |
492 | 0 | return 0; |
493 | 0 | return prvlen == 0 || check_prvenc(prvenc, key); |
494 | 0 | } else if (prvlen != 0) { |
495 | 0 | return ossl_ml_kem_parse_private_key(prvenc, prvlen, key); |
496 | 0 | } |
497 | 0 | return ossl_ml_kem_parse_public_key(pubenc, publen, key); |
498 | 0 | } |
499 | | |
500 | | static int ml_kem_import(void *vkey, int selection, const OSSL_PARAM params[]) |
501 | 0 | { |
502 | 0 | ML_KEM_KEY *key = vkey; |
503 | 0 | int include_private; |
504 | 0 | int res; |
505 | |
|
506 | 0 | if (!ossl_prov_is_running() || key == NULL) |
507 | 0 | return 0; |
508 | | |
509 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
510 | 0 | return 0; |
511 | | |
512 | 0 | include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
513 | 0 | res = ml_kem_key_fromdata(key, params, include_private); |
514 | 0 | if (res > 0 && include_private |
515 | 0 | && !ml_kem_pairwise_test(key, key->prov_flags)) { |
516 | | #ifdef FIPS_MODULE |
517 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
518 | | #endif |
519 | 0 | ossl_ml_kem_key_reset(key); |
520 | 0 | res = 0; |
521 | 0 | } |
522 | 0 | return res; |
523 | 0 | } |
524 | | |
525 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
526 | | static const OSSL_PARAM ml_kem_known_gettable_params[] = { |
527 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), |
528 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), |
529 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), |
530 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL), |
531 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0), |
532 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), |
533 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), |
534 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
535 | | OSSL_PARAM_END |
536 | | }; |
537 | | |
538 | | static int ml_kem_get_params_find_pidx(const char *s) |
539 | 0 | { |
540 | 0 | switch(s[0]) { |
541 | 0 | default: |
542 | 0 | break; |
543 | 0 | case 'b': |
544 | 0 | if (strcmp("its", s + 1) == 0) |
545 | 0 | return PIDX_PKEY_PARAM_BITS; |
546 | 0 | break; |
547 | 0 | case 'e': |
548 | 0 | if (strcmp("ncoded-pub-key", s + 1) == 0) |
549 | 0 | return PIDX_PKEY_PARAM_ENCODED_PUBLIC_KEY; |
550 | 0 | break; |
551 | 0 | case 'm': |
552 | 0 | if (strcmp("ax-size", s + 1) == 0) |
553 | 0 | return PIDX_PKEY_PARAM_MAX_SIZE; |
554 | 0 | break; |
555 | 0 | case 'p': |
556 | 0 | switch(s[1]) { |
557 | 0 | default: |
558 | 0 | break; |
559 | 0 | case 'r': |
560 | 0 | if (strcmp("iv", s + 2) == 0) |
561 | 0 | return PIDX_PKEY_PARAM_PRIV_KEY; |
562 | 0 | break; |
563 | 0 | case 'u': |
564 | 0 | if (strcmp("b", s + 2) == 0) |
565 | 0 | return PIDX_PKEY_PARAM_PUB_KEY; |
566 | 0 | } |
567 | 0 | break; |
568 | 0 | case 's': |
569 | 0 | switch(s[1]) { |
570 | 0 | default: |
571 | 0 | break; |
572 | 0 | case 'e': |
573 | 0 | switch(s[2]) { |
574 | 0 | default: |
575 | 0 | break; |
576 | 0 | case 'c': |
577 | 0 | switch(s[3]) { |
578 | 0 | default: |
579 | 0 | break; |
580 | 0 | case 'u': |
581 | 0 | switch(s[4]) { |
582 | 0 | default: |
583 | 0 | break; |
584 | 0 | case 'r': |
585 | 0 | switch(s[5]) { |
586 | 0 | default: |
587 | 0 | break; |
588 | 0 | case 'i': |
589 | 0 | switch(s[6]) { |
590 | 0 | default: |
591 | 0 | break; |
592 | 0 | case 't': |
593 | 0 | switch(s[7]) { |
594 | 0 | default: |
595 | 0 | break; |
596 | 0 | case 'y': |
597 | 0 | switch(s[8]) { |
598 | 0 | default: |
599 | 0 | break; |
600 | 0 | case '-': |
601 | 0 | switch(s[9]) { |
602 | 0 | default: |
603 | 0 | break; |
604 | 0 | case 'b': |
605 | 0 | if (strcmp("its", s + 10) == 0) |
606 | 0 | return PIDX_PKEY_PARAM_SECURITY_BITS; |
607 | 0 | break; |
608 | 0 | case 'c': |
609 | 0 | if (strcmp("ategory", s + 10) == 0) |
610 | 0 | return PIDX_PKEY_PARAM_SECURITY_CATEGORY; |
611 | 0 | } |
612 | 0 | } |
613 | 0 | } |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | 0 | } |
618 | 0 | break; |
619 | 0 | case 'e': |
620 | 0 | if (strcmp("d", s + 3) == 0) |
621 | 0 | return PIDX_PKEY_PARAM_ML_KEM_SEED; |
622 | 0 | } |
623 | 0 | } |
624 | 0 | } |
625 | 0 | return -1; |
626 | 0 | } |
627 | | |
628 | | /* End of machine generated */ |
629 | | |
630 | | static const OSSL_PARAM *ml_kem_gettable_params(void *provctx) |
631 | 0 | { |
632 | 0 | return ml_kem_known_gettable_params; |
633 | 0 | } |
634 | | |
635 | | #ifndef FIPS_MODULE |
636 | | void *ml_kem_load(const void *reference, size_t reference_sz) |
637 | 0 | { |
638 | 0 | ML_KEM_KEY *key = NULL; |
639 | 0 | uint8_t *encoded_dk = NULL; |
640 | 0 | uint8_t seed[ML_KEM_SEED_BYTES]; |
641 | |
|
642 | 0 | if (ossl_prov_is_running() && reference_sz == sizeof(key)) { |
643 | | /* The contents of the reference is the address to our object */ |
644 | 0 | key = *(ML_KEM_KEY **)reference; |
645 | 0 | encoded_dk = key->encoded_dk; |
646 | 0 | key->encoded_dk = NULL; |
647 | | /* We grabbed, so we detach it */ |
648 | 0 | *(ML_KEM_KEY **)reference = NULL; |
649 | 0 | if (encoded_dk != NULL |
650 | 0 | && ossl_ml_kem_encode_seed(seed, sizeof(seed), key) |
651 | 0 | && !check_seed(seed, encoded_dk, key)) |
652 | 0 | goto err; |
653 | | /* Generate the key now, if it holds only a stashed seed. */ |
654 | 0 | if (ossl_ml_kem_have_seed(key) |
655 | 0 | && (encoded_dk == NULL |
656 | 0 | || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) { |
657 | 0 | if (!ossl_ml_kem_genkey(NULL, 0, key) |
658 | 0 | || (encoded_dk != NULL && !check_prvenc(encoded_dk, key))) |
659 | 0 | goto err; |
660 | 0 | } else if (encoded_dk != NULL) { |
661 | 0 | if (!ossl_ml_kem_parse_private_key(encoded_dk, |
662 | 0 | key->vinfo->prvkey_bytes, key)) { |
663 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
664 | 0 | "error parsing %s private key", |
665 | 0 | key->vinfo->algorithm_name); |
666 | 0 | goto err; |
667 | 0 | } |
668 | 0 | if (!ml_kem_pairwise_test(key, key->prov_flags)) |
669 | 0 | goto err; |
670 | 0 | } |
671 | 0 | OPENSSL_free(encoded_dk); |
672 | 0 | return key; |
673 | 0 | } |
674 | | |
675 | 0 | err: |
676 | 0 | OPENSSL_free(encoded_dk); |
677 | 0 | ossl_ml_kem_key_free(key); |
678 | 0 | return NULL; |
679 | 0 | } |
680 | | #endif |
681 | | |
682 | | /* |
683 | | * It is assumed the key is guaranteed non-NULL here, and is from this provider |
684 | | */ |
685 | | static int ml_kem_get_params(void *vkey, OSSL_PARAM params[]) |
686 | 0 | { |
687 | 0 | ML_KEM_KEY *key = vkey; |
688 | 0 | const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key); |
689 | 0 | OSSL_PARAM *p; |
690 | 0 | uint8_t *pubenc = NULL; |
691 | |
|
692 | 0 | for (p = params; p->key != NULL; p++) |
693 | 0 | switch (ml_kem_get_params_find_pidx(p->key)) { |
694 | 0 | default: |
695 | 0 | break; |
696 | | |
697 | 0 | case PIDX_PKEY_PARAM_BITS: |
698 | 0 | if (!OSSL_PARAM_set_int(p, v->bits)) |
699 | 0 | return 0; |
700 | 0 | break; |
701 | | |
702 | 0 | case PIDX_PKEY_PARAM_SECURITY_BITS: |
703 | 0 | if (!OSSL_PARAM_set_int(p, v->secbits)) |
704 | 0 | return 0; |
705 | 0 | break; |
706 | | |
707 | 0 | case PIDX_PKEY_PARAM_MAX_SIZE: |
708 | 0 | if (!OSSL_PARAM_set_int(p, v->ctext_bytes)) |
709 | 0 | return 0; |
710 | 0 | break; |
711 | | |
712 | 0 | case PIDX_PKEY_PARAM_SECURITY_CATEGORY: |
713 | 0 | if (!OSSL_PARAM_set_int(p, v->security_category)) |
714 | 0 | return 0; |
715 | 0 | break; |
716 | | |
717 | | /* Exported to EVP_PKEY_get_raw_public_key() */ |
718 | 0 | case PIDX_PKEY_PARAM_PUB_KEY: |
719 | | /* Needed by EVP_PKEY_get1_encoded_public_key() */ |
720 | 0 | case PIDX_PKEY_PARAM_ENCODED_PUBLIC_KEY: |
721 | 0 | if (ossl_ml_kem_have_pubkey(key)) { |
722 | 0 | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
723 | 0 | return 0; |
724 | 0 | p->return_size = v->pubkey_bytes; |
725 | 0 | if (p->data == NULL) |
726 | 0 | break; |
727 | 0 | if (p->data_size < p->return_size) |
728 | 0 | return 0; |
729 | 0 | if (pubenc != NULL) { |
730 | 0 | memcpy(p->data, pubenc, p->return_size); |
731 | 0 | break; |
732 | 0 | } |
733 | 0 | if (!ossl_ml_kem_encode_public_key(p->data, p->return_size, key)) |
734 | 0 | return 0; |
735 | 0 | pubenc = p->data; |
736 | 0 | } |
737 | 0 | break; |
738 | | |
739 | | /* Exported to EVP_PKEY_get_raw_private_key() */ |
740 | 0 | case PIDX_PKEY_PARAM_PRIV_KEY: |
741 | 0 | if (ossl_ml_kem_have_prvkey(key)) { |
742 | 0 | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
743 | 0 | return 0; |
744 | 0 | p->return_size = v->prvkey_bytes; |
745 | 0 | if (p->data != NULL) { |
746 | 0 | if (p->data_size < p->return_size) |
747 | 0 | return 0; |
748 | 0 | if (!ossl_ml_kem_encode_private_key(p->data, p->return_size, key)) |
749 | 0 | return 0; |
750 | 0 | } |
751 | 0 | } |
752 | 0 | break; |
753 | | |
754 | | /* Exported for import */ |
755 | 0 | case PIDX_PKEY_PARAM_ML_KEM_SEED: |
756 | 0 | if (ossl_ml_kem_have_seed(key)) { |
757 | 0 | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
758 | 0 | return 0; |
759 | 0 | p->return_size = ML_KEM_SEED_BYTES; |
760 | 0 | if (p->data != NULL) { |
761 | 0 | if (p->data_size < p->return_size) |
762 | 0 | return 0; |
763 | 0 | if (!ossl_ml_kem_encode_seed(p->data, p->return_size, key)) |
764 | 0 | return 0; |
765 | 0 | } |
766 | 0 | } |
767 | 0 | break; |
768 | 0 | } |
769 | 0 | return 1; |
770 | 0 | } |
771 | | |
772 | | static const OSSL_PARAM *ml_kem_settable_params(void *provctx) |
773 | 0 | { |
774 | 0 | static const OSSL_PARAM arr[] = { |
775 | | /* Used in TLS via EVP_PKEY_set1_encoded_public_key(). */ |
776 | 0 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
777 | 0 | OSSL_PARAM_END |
778 | 0 | }; |
779 | |
|
780 | 0 | return arr; |
781 | 0 | } |
782 | | |
783 | | static int ml_kem_set_params(void *vkey, const OSSL_PARAM params[]) |
784 | 0 | { |
785 | 0 | ML_KEM_KEY *key = vkey; |
786 | 0 | const OSSL_PARAM *p; |
787 | 0 | const void *pubenc = NULL; |
788 | 0 | size_t publen = 0; |
789 | |
|
790 | 0 | if (ossl_param_is_empty(params)) |
791 | 0 | return 1; |
792 | | |
793 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY); |
794 | 0 | if (p != NULL |
795 | 0 | && (OSSL_PARAM_get_octet_string_ptr(p, &pubenc, &publen) != 1 |
796 | 0 | || publen != key->vinfo->pubkey_bytes)) { |
797 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
798 | 0 | return 0; |
799 | 0 | } |
800 | | |
801 | 0 | if (publen == 0) |
802 | 0 | return 1; |
803 | | |
804 | | /* Key mutation is reportedly generally not allowed */ |
805 | 0 | if (ossl_ml_kem_have_pubkey(key)) { |
806 | 0 | ERR_raise_data(ERR_LIB_PROV, |
807 | 0 | PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE, |
808 | 0 | "ML-KEM keys cannot be mutated"); |
809 | 0 | return 0; |
810 | 0 | } |
811 | | |
812 | 0 | return ossl_ml_kem_parse_public_key(pubenc, publen, key); |
813 | 0 | } |
814 | | |
815 | | static int ml_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[]) |
816 | 0 | { |
817 | 0 | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
818 | 0 | const OSSL_PARAM *p; |
819 | |
|
820 | 0 | if (gctx == NULL) |
821 | 0 | return 0; |
822 | 0 | if (ossl_param_is_empty(params)) |
823 | 0 | return 1; |
824 | | |
825 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES); |
826 | 0 | if (p != NULL) { |
827 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING) |
828 | 0 | return 0; |
829 | 0 | OPENSSL_free(gctx->propq); |
830 | 0 | if ((gctx->propq = OPENSSL_strdup(p->data)) == NULL) |
831 | 0 | return 0; |
832 | 0 | } |
833 | | |
834 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ML_KEM_SEED); |
835 | 0 | if (p != NULL) { |
836 | 0 | size_t len = ML_KEM_SEED_BYTES; |
837 | |
|
838 | 0 | gctx->seed = gctx->seedbuf; |
839 | 0 | if (OSSL_PARAM_get_octet_string(p, (void **)&gctx->seed, len, &len) |
840 | 0 | && len == ML_KEM_SEED_BYTES) |
841 | 0 | return 1; |
842 | | |
843 | | /* Possibly, but less likely wrong data type */ |
844 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
845 | 0 | gctx->seed = NULL; |
846 | 0 | return 0; |
847 | 0 | } |
848 | | |
849 | 0 | return 1; |
850 | 0 | } |
851 | | |
852 | | static void *ml_kem_gen_init(void *provctx, int selection, |
853 | | const OSSL_PARAM params[], int evp_type) |
854 | 0 | { |
855 | 0 | PROV_ML_KEM_GEN_CTX *gctx = NULL; |
856 | | |
857 | | /* |
858 | | * We can only generate private keys, check that the selection is |
859 | | * appropriate. |
860 | | */ |
861 | 0 | if (!ossl_prov_is_running() |
862 | 0 | || (selection & minimal_selection) == 0 |
863 | 0 | || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL) |
864 | 0 | return NULL; |
865 | | |
866 | 0 | gctx->selection = selection; |
867 | 0 | gctx->evp_type = evp_type; |
868 | 0 | gctx->provctx = provctx; |
869 | 0 | if (ml_kem_gen_set_params(gctx, params)) |
870 | 0 | return gctx; |
871 | | |
872 | 0 | ml_kem_gen_cleanup(gctx); |
873 | 0 | return NULL; |
874 | 0 | } |
875 | | |
876 | | static const OSSL_PARAM *ml_kem_gen_settable_params(ossl_unused void *vgctx, |
877 | | ossl_unused void *provctx) |
878 | 0 | { |
879 | 0 | static OSSL_PARAM settable[] = { |
880 | 0 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0), |
881 | 0 | OSSL_PARAM_END |
882 | 0 | }; |
883 | 0 | return settable; |
884 | 0 | } |
885 | | |
886 | | static void *ml_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg) |
887 | 0 | { |
888 | 0 | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
889 | 0 | ML_KEM_KEY *key; |
890 | 0 | uint8_t *nopub = NULL; |
891 | 0 | uint8_t *seed; |
892 | 0 | int genok = 0; |
893 | |
|
894 | 0 | if (gctx == NULL |
895 | 0 | || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == |
896 | 0 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY) |
897 | 0 | return NULL; |
898 | 0 | seed = gctx->seed; |
899 | 0 | key = ossl_prov_ml_kem_new(gctx->provctx, gctx->propq, gctx->evp_type); |
900 | 0 | if (key == NULL) |
901 | 0 | return NULL; |
902 | | |
903 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
904 | 0 | return key; |
905 | | |
906 | 0 | if (seed != NULL && !ossl_ml_kem_set_seed(seed, ML_KEM_SEED_BYTES, key)) |
907 | 0 | return NULL; |
908 | 0 | genok = ossl_ml_kem_genkey(nopub, 0, key); |
909 | | |
910 | | /* Erase the single-use seed */ |
911 | 0 | if (seed != NULL) |
912 | 0 | OPENSSL_cleanse(seed, ML_KEM_SEED_BYTES); |
913 | 0 | gctx->seed = NULL; |
914 | |
|
915 | 0 | if (genok) { |
916 | | #ifdef FIPS_MODULE |
917 | | if (!ml_kem_pairwise_test(key, ML_KEM_KEY_FIXED_PCT)) { |
918 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
919 | | ossl_ml_kem_key_free(key); |
920 | | return NULL; |
921 | | } |
922 | | #endif /* FIPS_MODULE */ |
923 | 0 | return key; |
924 | 0 | } |
925 | | |
926 | 0 | ossl_ml_kem_key_free(key); |
927 | 0 | return NULL; |
928 | 0 | } |
929 | | |
930 | | static void ml_kem_gen_cleanup(void *vgctx) |
931 | 0 | { |
932 | 0 | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
933 | |
|
934 | 0 | if (gctx == NULL) |
935 | 0 | return; |
936 | | |
937 | 0 | if (gctx->seed != NULL) |
938 | 0 | OPENSSL_cleanse(gctx->seed, ML_KEM_RANDOM_BYTES); |
939 | 0 | OPENSSL_free(gctx->propq); |
940 | 0 | OPENSSL_free(gctx); |
941 | 0 | } |
942 | | |
943 | | static void *ml_kem_dup(const void *vkey, int selection) |
944 | 0 | { |
945 | 0 | const ML_KEM_KEY *key = vkey; |
946 | |
|
947 | 0 | if (!ossl_prov_is_running()) |
948 | 0 | return NULL; |
949 | | |
950 | 0 | return ossl_ml_kem_key_dup(key, selection); |
951 | 0 | } |
952 | | |
953 | | #ifndef FIPS_MODULE |
954 | | # define DISPATCH_LOAD_FN \ |
955 | | { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_kem_load }, |
956 | | #else |
957 | | # define DISPATCH_LOAD_FN /* Non-FIPS only */ |
958 | | #endif |
959 | | |
960 | | #define DECLARE_VARIANT(bits) \ |
961 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_##bits##_new; \ |
962 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_##bits##_gen_init; \ |
963 | | static void *ml_kem_##bits##_new(void *provctx) \ |
964 | 0 | { \ |
965 | 0 | return ossl_prov_ml_kem_new(provctx, NULL, EVP_PKEY_ML_KEM_##bits); \ |
966 | 0 | } \ Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_512_new Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_768_new Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_1024_new |
967 | | static void *ml_kem_##bits##_gen_init(void *provctx, int selection, \ |
968 | | const OSSL_PARAM params[]) \ |
969 | 0 | { \ |
970 | 0 | return ml_kem_gen_init(provctx, selection, params, \ |
971 | 0 | EVP_PKEY_ML_KEM_##bits); \ |
972 | 0 | } \ Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_512_gen_init Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_768_gen_init Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_1024_gen_init |
973 | | const OSSL_DISPATCH ossl_ml_kem_##bits##_keymgmt_functions[] = { \ |
974 | | { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) ml_kem_##bits##_new }, \ |
975 | | { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) ossl_ml_kem_key_free }, \ |
976 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) ml_kem_get_params }, \ |
977 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gettable_params }, \ |
978 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) ml_kem_set_params }, \ |
979 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_settable_params }, \ |
980 | | { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) ml_kem_has }, \ |
981 | | { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) ml_kem_match }, \ |
982 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (OSSL_FUNC) ml_kem_validate }, \ |
983 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) ml_kem_##bits##_gen_init }, \ |
984 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) ml_kem_gen_set_params }, \ |
985 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gen_settable_params }, \ |
986 | | { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) ml_kem_gen }, \ |
987 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) ml_kem_gen_cleanup }, \ |
988 | | DISPATCH_LOAD_FN \ |
989 | | { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) ml_kem_dup }, \ |
990 | | { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) ml_kem_import }, \ |
991 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \ |
992 | | { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) ml_kem_export }, \ |
993 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \ |
994 | | OSSL_DISPATCH_END \ |
995 | | } |
996 | | DECLARE_VARIANT(512); |
997 | | DECLARE_VARIANT(768); |
998 | | DECLARE_VARIANT(1024); |