/src/openssl/crypto/ml_dsa/ml_dsa_key.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/params.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include <openssl/rand.h> |
16 | | #include "ml_dsa_key.h" |
17 | | #include "ml_dsa_matrix.h" |
18 | | #include "ml_dsa_hash.h" |
19 | | #include "internal/encoder.h" |
20 | | |
21 | | const ML_DSA_PARAMS *ossl_ml_dsa_key_params(const ML_DSA_KEY *key) |
22 | 0 | { |
23 | 0 | return key->params; |
24 | 0 | } |
25 | | |
26 | | /* Returns the seed data or NULL if there is no seed */ |
27 | | const uint8_t *ossl_ml_dsa_key_get_seed(const ML_DSA_KEY *key) |
28 | 0 | { |
29 | 0 | return key->seed; |
30 | 0 | } |
31 | | |
32 | | int ossl_ml_dsa_key_get_prov_flags(const ML_DSA_KEY *key) |
33 | 0 | { |
34 | 0 | return key->prov_flags; |
35 | 0 | } |
36 | | |
37 | | int ossl_ml_dsa_set_prekey(ML_DSA_KEY *key, int flags_set, int flags_clr, |
38 | | const uint8_t *seed, size_t seed_len, |
39 | | const uint8_t *sk, size_t sk_len) |
40 | 0 | { |
41 | 0 | int ret = 0; |
42 | |
|
43 | 0 | if (key == NULL |
44 | 0 | || key->pub_encoding != NULL |
45 | 0 | || key->priv_encoding != NULL |
46 | 0 | || (sk != NULL && sk_len != key->params->sk_len) |
47 | 0 | || (seed != NULL && seed_len != ML_DSA_SEED_BYTES) |
48 | 0 | || key->seed != NULL) |
49 | 0 | return 0; |
50 | | |
51 | 0 | if (sk != NULL) { |
52 | 0 | key->priv_encoding = OPENSSL_secure_malloc(sk_len); |
53 | 0 | if (key->priv_encoding == NULL) |
54 | 0 | goto end; |
55 | 0 | memcpy(key->priv_encoding, sk, sk_len); |
56 | 0 | } |
57 | | |
58 | 0 | if (seed != NULL) { |
59 | 0 | if ((key->seed = OPENSSL_secure_malloc(seed_len)) == NULL) |
60 | 0 | goto end; |
61 | 0 | memcpy(key->seed, seed, seed_len); |
62 | 0 | } |
63 | 0 | key->prov_flags |= flags_set; |
64 | 0 | key->prov_flags &= ~flags_clr; |
65 | 0 | ret = 1; |
66 | |
|
67 | 0 | end: |
68 | 0 | if (!ret) { |
69 | 0 | OPENSSL_secure_clear_free(key->priv_encoding, sk_len); |
70 | 0 | OPENSSL_secure_clear_free(key->seed, seed_len); |
71 | 0 | key->priv_encoding = key->seed = NULL; |
72 | 0 | } |
73 | 0 | return ret; |
74 | 0 | } |
75 | | |
76 | | /** |
77 | | * @brief Create a new ML_DSA_KEY object |
78 | | * |
79 | | * @param libctx A OSSL_LIB_CTX object used for fetching algorithms. |
80 | | * @param propq The property query used for fetching algorithms |
81 | | * @param alg The algorithm name associated with the key type |
82 | | * @returns The new ML_DSA_KEY object on success, or NULL on malloc failure |
83 | | */ |
84 | | ML_DSA_KEY *ossl_ml_dsa_key_new(OSSL_LIB_CTX *libctx, const char *propq, |
85 | | int evp_type) |
86 | 0 | { |
87 | 0 | ML_DSA_KEY *ret; |
88 | 0 | const ML_DSA_PARAMS *params = ossl_ml_dsa_params_get(evp_type); |
89 | |
|
90 | 0 | if (params == NULL) |
91 | 0 | return NULL; |
92 | | |
93 | 0 | ret = OPENSSL_zalloc(sizeof(*ret)); |
94 | 0 | if (ret != NULL) { |
95 | 0 | ret->libctx = libctx; |
96 | 0 | ret->params = params; |
97 | 0 | ret->prov_flags = ML_DSA_KEY_PROV_FLAGS_DEFAULT; |
98 | 0 | ret->shake128_md = EVP_MD_fetch(libctx, "SHAKE-128", propq); |
99 | 0 | ret->shake256_md = EVP_MD_fetch(libctx, "SHAKE-256", propq); |
100 | 0 | if (ret->shake128_md == NULL || ret->shake256_md == NULL) |
101 | 0 | goto err; |
102 | 0 | } |
103 | 0 | return ret; |
104 | 0 | err: |
105 | 0 | ossl_ml_dsa_key_free(ret); |
106 | 0 | return NULL; |
107 | 0 | } |
108 | | |
109 | | int ossl_ml_dsa_key_pub_alloc(ML_DSA_KEY *key) |
110 | 0 | { |
111 | 0 | if (key->t1.poly != NULL) |
112 | 0 | return 0; |
113 | 0 | return vector_alloc(&key->t1, key->params->k); |
114 | 0 | } |
115 | | |
116 | | int ossl_ml_dsa_key_priv_alloc(ML_DSA_KEY *key) |
117 | 0 | { |
118 | 0 | size_t k = key->params->k, l = key->params->l; |
119 | 0 | POLY *poly; |
120 | |
|
121 | 0 | if (key->s1.poly != NULL) |
122 | 0 | return 0; |
123 | 0 | if (!vector_secure_alloc(&key->s1, l + 2 * k)) |
124 | 0 | return 0; |
125 | | |
126 | 0 | poly = key->s1.poly; |
127 | 0 | key->s1.num_poly = l; |
128 | 0 | vector_init(&key->s2, poly + l, k); |
129 | 0 | vector_init(&key->t0, poly + l + k, k); |
130 | 0 | return 1; |
131 | 0 | } |
132 | | |
133 | | /** |
134 | | * @brief Destroy an ML_DSA_KEY object |
135 | | */ |
136 | | void ossl_ml_dsa_key_free(ML_DSA_KEY *key) |
137 | 0 | { |
138 | 0 | if (key == NULL) |
139 | 0 | return; |
140 | | |
141 | 0 | EVP_MD_free(key->shake128_md); |
142 | 0 | EVP_MD_free(key->shake256_md); |
143 | 0 | ossl_ml_dsa_key_reset(key); |
144 | 0 | OPENSSL_free(key); |
145 | 0 | } |
146 | | |
147 | | /** |
148 | | * @brief Factory reset an ML_DSA_KEY object |
149 | | */ |
150 | | void ossl_ml_dsa_key_reset(ML_DSA_KEY *key) |
151 | 0 | { |
152 | | /* |
153 | | * The allocation for |s1.poly| subsumes those for |s2| and |t0|, which we |
154 | | * must not access after |s1|'s poly is freed. |
155 | | */ |
156 | 0 | if (key->s1.poly != NULL) { |
157 | 0 | const ML_DSA_PARAMS *params = key->params; |
158 | 0 | size_t k = params->k, l = params->l; |
159 | |
|
160 | 0 | vector_secure_free(&key->s1, l + 2 * k); |
161 | 0 | vector_init(&key->s2, NULL, 0); |
162 | 0 | vector_init(&key->t0, NULL, 0); |
163 | 0 | } |
164 | | /* The |t1| vector is public and allocated separately */ |
165 | 0 | vector_free(&key->t1); |
166 | 0 | OPENSSL_cleanse(key->K, sizeof(key->K)); |
167 | 0 | OPENSSL_free(key->pub_encoding); |
168 | 0 | key->pub_encoding = NULL; |
169 | 0 | if (key->priv_encoding != NULL) |
170 | 0 | OPENSSL_secure_clear_free(key->priv_encoding, key->params->sk_len); |
171 | 0 | key->priv_encoding = NULL; |
172 | 0 | if (key->seed != NULL) |
173 | 0 | OPENSSL_secure_clear_free(key->seed, ML_DSA_SEED_BYTES); |
174 | 0 | key->seed = NULL; |
175 | 0 | } |
176 | | |
177 | | /** |
178 | | * @brief Duplicate a key |
179 | | * |
180 | | * @param src A ML_DSA_KEY object to copy |
181 | | * @param selection to select public and/or private components. Selecting the |
182 | | * private key will also select the public key |
183 | | * @returns The duplicated key, or NULL on failure. |
184 | | */ |
185 | | ML_DSA_KEY *ossl_ml_dsa_key_dup(const ML_DSA_KEY *src, int selection) |
186 | 0 | { |
187 | 0 | ML_DSA_KEY *ret = NULL; |
188 | |
|
189 | 0 | if (src == NULL) |
190 | 0 | return NULL; |
191 | | |
192 | | /* Prekeys with just a seed or private key are not dupable */ |
193 | 0 | if (src->pub_encoding == NULL |
194 | 0 | && (src->priv_encoding != NULL || src->seed != NULL)) |
195 | 0 | return NULL; |
196 | | |
197 | 0 | ret = OPENSSL_zalloc(sizeof(*ret)); |
198 | 0 | if (ret != NULL) { |
199 | 0 | ret->libctx = src->libctx; |
200 | 0 | ret->params = src->params; |
201 | 0 | ret->prov_flags = src->prov_flags; |
202 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
203 | 0 | if (src->pub_encoding != NULL) { |
204 | | /* The public components are present if the private key is present */ |
205 | 0 | memcpy(ret->rho, src->rho, sizeof(src->rho)); |
206 | 0 | memcpy(ret->tr, src->tr, sizeof(src->tr)); |
207 | 0 | if (src->t1.poly != NULL) { |
208 | 0 | if (!ossl_ml_dsa_key_pub_alloc(ret)) |
209 | 0 | goto err; |
210 | 0 | vector_copy(&ret->t1, &src->t1); |
211 | 0 | } |
212 | 0 | if ((ret->pub_encoding = OPENSSL_memdup(src->pub_encoding, |
213 | 0 | src->params->pk_len)) == NULL) |
214 | 0 | goto err; |
215 | 0 | } |
216 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
217 | 0 | if (src->priv_encoding != NULL) { |
218 | 0 | memcpy(ret->K, src->K, sizeof(src->K)); |
219 | 0 | if (src->s1.poly != NULL) { |
220 | 0 | if (!ossl_ml_dsa_key_priv_alloc(ret)) |
221 | 0 | goto err; |
222 | 0 | vector_copy(&ret->s1, &src->s1); |
223 | 0 | vector_copy(&ret->s2, &src->s2); |
224 | 0 | vector_copy(&ret->t0, &src->t0); |
225 | 0 | } |
226 | 0 | ret->priv_encoding = OPENSSL_secure_malloc(src->params->sk_len); |
227 | 0 | if (ret->priv_encoding == NULL) |
228 | 0 | goto err; |
229 | 0 | memcpy(ret->priv_encoding, src->priv_encoding, src->params->sk_len); |
230 | 0 | } |
231 | 0 | if (src->seed != NULL) { |
232 | 0 | ret->seed = OPENSSL_secure_malloc(ML_DSA_SEED_BYTES); |
233 | 0 | if (ret->seed == NULL) |
234 | 0 | goto err; |
235 | 0 | memcpy(ret->seed, src->seed, ML_DSA_SEED_BYTES); |
236 | 0 | } |
237 | 0 | } |
238 | 0 | } |
239 | 0 | EVP_MD_up_ref(src->shake128_md); |
240 | 0 | EVP_MD_up_ref(src->shake256_md); |
241 | 0 | ret->shake128_md = src->shake128_md; |
242 | 0 | ret->shake256_md = src->shake256_md; |
243 | 0 | } |
244 | 0 | return ret; |
245 | 0 | err: |
246 | 0 | ossl_ml_dsa_key_free(ret); |
247 | 0 | return NULL; |
248 | 0 | } |
249 | | |
250 | | /** |
251 | | * @brief Are 2 keys equal? |
252 | | * |
253 | | * To be equal the keys must have matching public or private key data and |
254 | | * contain the same parameters. |
255 | | * (Note that in OpenSSL that the private key always has a public key component). |
256 | | * |
257 | | * @param key1 A ML_DSA_KEY object |
258 | | * @param key2 A ML_DSA_KEY object |
259 | | * @param selection to select public and/or private component comparison. |
260 | | * @returns 1 if the keys are equal otherwise it returns 0. |
261 | | */ |
262 | | int ossl_ml_dsa_key_equal(const ML_DSA_KEY *key1, const ML_DSA_KEY *key2, |
263 | | int selection) |
264 | 0 | { |
265 | 0 | int key_checked = 0; |
266 | |
|
267 | 0 | if (key1->params != key2->params) |
268 | 0 | return 0; |
269 | | |
270 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
271 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
272 | 0 | if (key1->pub_encoding != NULL && key2->pub_encoding != NULL) { |
273 | 0 | if (memcmp(key1->pub_encoding, key2->pub_encoding, |
274 | 0 | key1->params->pk_len) != 0) |
275 | 0 | return 0; |
276 | 0 | key_checked = 1; |
277 | 0 | } |
278 | 0 | } |
279 | 0 | if (!key_checked |
280 | 0 | && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
281 | 0 | if (key1->priv_encoding != NULL && key2->priv_encoding != NULL) { |
282 | 0 | if (memcmp(key1->priv_encoding, key2->priv_encoding, |
283 | 0 | key1->params->sk_len) != 0) |
284 | 0 | return 0; |
285 | 0 | key_checked = 1; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | return key_checked; |
289 | 0 | } |
290 | 0 | return 1; |
291 | 0 | } |
292 | | |
293 | | int ossl_ml_dsa_key_has(const ML_DSA_KEY *key, int selection) |
294 | 0 | { |
295 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
296 | | /* Note that the public key always exists if there is a private key */ |
297 | 0 | if (ossl_ml_dsa_key_get_pub(key) == NULL) |
298 | 0 | return 0; /* No public key */ |
299 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 |
300 | 0 | && ossl_ml_dsa_key_get_priv(key) == NULL) |
301 | 0 | return 0; /* No private key */ |
302 | 0 | return 1; |
303 | 0 | } |
304 | 0 | return 0; |
305 | 0 | } |
306 | | |
307 | | /* |
308 | | * @brief Given a key containing private key values for rho, s1 & s2 |
309 | | * generate the public value t and return the compressed values t1, t0. |
310 | | * |
311 | | * @param key A private key containing params, rh0, s1 & s2. |
312 | | * @param md_ctx A EVP_MD_CTX used for sampling. |
313 | | * @param t1 The returned polynomial encoding of the 10 MSB of each coefficient |
314 | | * of the uncompressed public key polynomial t. |
315 | | * @param t0 The returned polynomial encoding of the 13 LSB of each coefficient |
316 | | * of the uncompressed public key polynomial t. |
317 | | * @returns 1 on success, or 0 on failure. |
318 | | */ |
319 | | static int public_from_private(const ML_DSA_KEY *key, EVP_MD_CTX *md_ctx, |
320 | | VECTOR *t1, VECTOR *t0) |
321 | 0 | { |
322 | 0 | const ML_DSA_PARAMS *params = key->params; |
323 | 0 | uint32_t k = (uint32_t)params->k, l = (uint32_t)params->l; |
324 | 0 | POLY *polys; |
325 | 0 | MATRIX a_ntt; |
326 | 0 | VECTOR s1_ntt; |
327 | 0 | VECTOR t; |
328 | |
|
329 | 0 | polys = OPENSSL_malloc(sizeof(*polys) * (k + l + k * l)); |
330 | 0 | if (polys == NULL) |
331 | 0 | return 0; |
332 | | |
333 | 0 | vector_init(&t, polys, k); |
334 | 0 | vector_init(&s1_ntt, t.poly + k, l); |
335 | 0 | matrix_init(&a_ntt, s1_ntt.poly + l, k, l); |
336 | | |
337 | | /* Using rho generate A' = A in NTT form */ |
338 | 0 | if (!matrix_expand_A(md_ctx, key->shake128_md, key->rho, &a_ntt)) |
339 | 0 | goto err; |
340 | | |
341 | | /* t = NTT_inv(A' * NTT(s1)) + s2 */ |
342 | 0 | vector_copy(&s1_ntt, &key->s1); |
343 | 0 | vector_ntt(&s1_ntt); |
344 | |
|
345 | 0 | matrix_mult_vector(&a_ntt, &s1_ntt, &t); |
346 | 0 | vector_ntt_inverse(&t); |
347 | 0 | vector_add(&t, &key->s2, &t); |
348 | | |
349 | | /* Compress t */ |
350 | 0 | vector_power2_round(&t, t1, t0); |
351 | | |
352 | | /* Zeroize secret */ |
353 | 0 | vector_zero(&s1_ntt); |
354 | 0 | err: |
355 | 0 | OPENSSL_free(polys); |
356 | 0 | return 1; |
357 | 0 | } |
358 | | |
359 | | int ossl_ml_dsa_key_public_from_private(ML_DSA_KEY *key) |
360 | 0 | { |
361 | 0 | int ret = 0; |
362 | 0 | VECTOR t0; |
363 | 0 | EVP_MD_CTX *md_ctx = NULL; |
364 | |
|
365 | 0 | if (!vector_alloc(&t0, key->params->k)) /* t0 is already in the private key */ |
366 | 0 | return 0; |
367 | 0 | ret = ((md_ctx = EVP_MD_CTX_new())!= NULL) |
368 | 0 | && ossl_ml_dsa_key_pub_alloc(key) /* allocate space for t1 */ |
369 | 0 | && public_from_private(key, md_ctx, &key->t1, &t0) |
370 | 0 | && vector_equal(&t0, &key->t0) /* compare the generated t0 to the expected */ |
371 | 0 | && ossl_ml_dsa_pk_encode(key) |
372 | 0 | && shake_xof(md_ctx, key->shake256_md, |
373 | 0 | key->pub_encoding, key->params->pk_len, |
374 | 0 | key->tr, sizeof(key->tr)); |
375 | 0 | vector_free(&t0); |
376 | 0 | EVP_MD_CTX_free(md_ctx); |
377 | 0 | return ret; |
378 | 0 | } |
379 | | |
380 | | int ossl_ml_dsa_key_pairwise_check(const ML_DSA_KEY *key) |
381 | 0 | { |
382 | 0 | int ret = 0; |
383 | 0 | VECTOR t1, t0; |
384 | 0 | POLY *polys = NULL; |
385 | 0 | uint32_t k = (uint32_t)key->params->k; |
386 | 0 | EVP_MD_CTX *md_ctx = NULL; |
387 | |
|
388 | 0 | if (key->pub_encoding == NULL || key->priv_encoding == 0) |
389 | 0 | return 0; |
390 | | |
391 | 0 | polys = OPENSSL_malloc(sizeof(*polys) * (2 * k)); |
392 | 0 | if (polys == NULL) |
393 | 0 | return 0; |
394 | 0 | md_ctx = EVP_MD_CTX_new(); |
395 | 0 | if (md_ctx == NULL) |
396 | 0 | goto err; |
397 | | |
398 | 0 | vector_init(&t1, polys, k); |
399 | 0 | vector_init(&t0, polys + k, k); |
400 | 0 | if (!public_from_private(key, md_ctx, &t1, &t0)) |
401 | 0 | goto err; |
402 | | |
403 | 0 | ret = vector_equal(&t1, &key->t1) && vector_equal(&t0, &key->t0); |
404 | 0 | err: |
405 | 0 | EVP_MD_CTX_free(md_ctx); |
406 | 0 | OPENSSL_free(polys); |
407 | 0 | return ret; |
408 | 0 | } |
409 | | |
410 | | /* |
411 | | * @brief Generate a public-private key pair from a seed. |
412 | | * See FIPS 204, Algorithm 6 ML-DSA.KeyGen_internal(). |
413 | | * |
414 | | * @param out The generated key (which contains params on input) |
415 | | * |
416 | | * @returns 1 on success or 0 on failure. |
417 | | */ |
418 | | static int keygen_internal(ML_DSA_KEY *out) |
419 | 0 | { |
420 | 0 | int ret = 0; |
421 | 0 | uint8_t augmented_seed[ML_DSA_SEED_BYTES + 2]; |
422 | 0 | uint8_t expanded_seed[ML_DSA_RHO_BYTES + ML_DSA_PRIV_SEED_BYTES + ML_DSA_K_BYTES]; |
423 | 0 | const uint8_t *const rho = expanded_seed; /* p = Public Random Seed */ |
424 | 0 | const uint8_t *const priv_seed = expanded_seed + ML_DSA_RHO_BYTES; |
425 | 0 | const uint8_t *const K = priv_seed + ML_DSA_PRIV_SEED_BYTES; |
426 | 0 | const ML_DSA_PARAMS *params = out->params; |
427 | 0 | EVP_MD_CTX *md_ctx = NULL; |
428 | |
|
429 | 0 | if (out->seed == NULL |
430 | 0 | || (md_ctx = EVP_MD_CTX_new()) == NULL |
431 | 0 | || !ossl_ml_dsa_key_pub_alloc(out) |
432 | 0 | || !ossl_ml_dsa_key_priv_alloc(out)) |
433 | 0 | goto err; |
434 | | |
435 | | /* augmented_seed = seed || k || l */ |
436 | 0 | memcpy(augmented_seed, out->seed, ML_DSA_SEED_BYTES); |
437 | 0 | augmented_seed[ML_DSA_SEED_BYTES] = (uint8_t)params->k; |
438 | 0 | augmented_seed[ML_DSA_SEED_BYTES + 1] = (uint8_t)params->l; |
439 | | /* Expand the seed into p[32], p'[64], K[32] */ |
440 | 0 | if (!shake_xof(md_ctx, out->shake256_md, augmented_seed, sizeof(augmented_seed), |
441 | 0 | expanded_seed, sizeof(expanded_seed))) |
442 | 0 | goto err; |
443 | | |
444 | 0 | memcpy(out->rho, rho, sizeof(out->rho)); |
445 | 0 | memcpy(out->K, K, sizeof(out->K)); |
446 | |
|
447 | 0 | ret = vector_expand_S(md_ctx, out->shake256_md, params->eta, priv_seed, &out->s1, &out->s2) |
448 | 0 | && public_from_private(out, md_ctx, &out->t1, &out->t0) |
449 | 0 | && ossl_ml_dsa_pk_encode(out) |
450 | 0 | && shake_xof(md_ctx, out->shake256_md, out->pub_encoding, out->params->pk_len, |
451 | 0 | out->tr, sizeof(out->tr)) |
452 | 0 | && ossl_ml_dsa_sk_encode(out); |
453 | |
|
454 | 0 | err: |
455 | 0 | if (out->seed != NULL && (out->prov_flags & ML_DSA_KEY_RETAIN_SEED) == 0) { |
456 | 0 | OPENSSL_clear_free(out->seed, ML_DSA_SEED_BYTES); |
457 | 0 | out->seed = NULL; |
458 | 0 | } |
459 | 0 | EVP_MD_CTX_free(md_ctx); |
460 | 0 | OPENSSL_cleanse(augmented_seed, sizeof(augmented_seed)); |
461 | 0 | OPENSSL_cleanse(expanded_seed, sizeof(expanded_seed)); |
462 | 0 | return ret; |
463 | 0 | } |
464 | | |
465 | | int ossl_ml_dsa_generate_key(ML_DSA_KEY *out) |
466 | 0 | { |
467 | 0 | size_t seed_len = ML_DSA_SEED_BYTES; |
468 | 0 | uint8_t *sk; |
469 | 0 | int ret; |
470 | |
|
471 | 0 | if (out->seed == NULL) { |
472 | 0 | if ((out->seed = OPENSSL_secure_malloc(seed_len)) == NULL) |
473 | 0 | return 0; |
474 | 0 | if (RAND_priv_bytes_ex(out->libctx, out->seed, seed_len, 0) <= 0) { |
475 | 0 | OPENSSL_secure_free(out->seed); |
476 | 0 | out->seed = NULL; |
477 | 0 | return 0; |
478 | 0 | } |
479 | 0 | } |
480 | | /* We're generating from a seed, drop private prekey encoding */ |
481 | 0 | sk = out->priv_encoding; |
482 | 0 | out->priv_encoding = NULL; |
483 | 0 | if (sk == NULL) { |
484 | 0 | ret = keygen_internal(out); |
485 | 0 | } else { |
486 | 0 | if ((ret = keygen_internal(out)) != 0 |
487 | 0 | && memcmp(out->priv_encoding, sk, out->params->sk_len) != 0) { |
488 | 0 | ret = 0; |
489 | 0 | ossl_ml_dsa_key_reset(out); |
490 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
491 | 0 | "explicit %s private key does not match seed", |
492 | 0 | out->params->alg); |
493 | 0 | } |
494 | 0 | OPENSSL_free(sk); |
495 | 0 | } |
496 | 0 | return ret; |
497 | 0 | } |
498 | | |
499 | | /** |
500 | | * @brief This is used when a ML DSA key is used for an operation. |
501 | | * This checks that the algorithm is the same (i.e. uses the same parameters) |
502 | | * |
503 | | * @param key A ML_DSA key to use for an operation. |
504 | | * @param evp_type The algorithm nid associated with an operation |
505 | | * |
506 | | * @returns 1 if the algorithm matches, or 0 otherwise. |
507 | | */ |
508 | | |
509 | | int ossl_ml_dsa_key_matches(const ML_DSA_KEY *key, int evp_type) |
510 | 0 | { |
511 | 0 | return (key->params->evp_type == evp_type); |
512 | 0 | } |
513 | | |
514 | | /* Returns the public key data or NULL if there is no public key */ |
515 | | const uint8_t *ossl_ml_dsa_key_get_pub(const ML_DSA_KEY *key) |
516 | 0 | { |
517 | 0 | return key->pub_encoding; |
518 | 0 | } |
519 | | |
520 | | /* Returns the encoded public key size */ |
521 | | size_t ossl_ml_dsa_key_get_pub_len(const ML_DSA_KEY *key) |
522 | 0 | { |
523 | 0 | return key->params->pk_len; |
524 | 0 | } |
525 | | |
526 | | size_t ossl_ml_dsa_key_get_collision_strength_bits(const ML_DSA_KEY *key) |
527 | 0 | { |
528 | 0 | return key->params->bit_strength; |
529 | 0 | } |
530 | | |
531 | | int ossl_ml_dsa_key_get_security_category(const ML_DSA_KEY *key) |
532 | 0 | { |
533 | 0 | return key->params->security_category; |
534 | 0 | } |
535 | | |
536 | | /* Returns the private key data or NULL if there is no private key */ |
537 | | const uint8_t *ossl_ml_dsa_key_get_priv(const ML_DSA_KEY *key) |
538 | 0 | { |
539 | 0 | return key->priv_encoding; |
540 | 0 | } |
541 | | |
542 | | size_t ossl_ml_dsa_key_get_priv_len(const ML_DSA_KEY *key) |
543 | 0 | { |
544 | 0 | return key->params->sk_len; |
545 | 0 | } |
546 | | |
547 | | size_t ossl_ml_dsa_key_get_sig_len(const ML_DSA_KEY *key) |
548 | 0 | { |
549 | 0 | return key->params->sig_len; |
550 | 0 | } |
551 | | |
552 | | OSSL_LIB_CTX *ossl_ml_dsa_key_get0_libctx(const ML_DSA_KEY *key) |
553 | 0 | { |
554 | 0 | return key != NULL ? key->libctx : NULL; |
555 | 0 | } |
556 | | |
557 | | const char *ossl_ml_dsa_key_get_name(const ML_DSA_KEY *key) |
558 | 0 | { |
559 | 0 | return key->params->alg; |
560 | 0 | } |