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