/src/openssl/crypto/slh_dsa/slh_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 <string.h>  | 
11  |  | #include <openssl/err.h>  | 
12  |  | #include <openssl/core_dispatch.h>  | 
13  |  | #include <openssl/core_names.h>  | 
14  |  | #include <openssl/params.h>  | 
15  |  | #include <openssl/rand.h>  | 
16  |  | #include <openssl/proverr.h>  | 
17  |  | #include "slh_dsa_local.h"  | 
18  |  | #include "slh_dsa_key.h"  | 
19  |  | #include "internal/encoder.h"  | 
20  |  |  | 
21  |  | static int slh_dsa_compute_pk_root(SLH_DSA_HASH_CTX *ctx, SLH_DSA_KEY *out, int verify);  | 
22  |  |  | 
23  |  | static void slh_dsa_key_hash_cleanup(SLH_DSA_KEY *key)  | 
24  | 0  | { | 
25  | 0  |     OPENSSL_free(key->propq);  | 
26  | 0  |     if (key->md_big != key->md)  | 
27  | 0  |         EVP_MD_free(key->md_big);  | 
28  | 0  |     key->md_big = NULL;  | 
29  | 0  |     EVP_MD_free(key->md);  | 
30  | 0  |     EVP_MAC_free(key->hmac);  | 
31  | 0  |     key->md = NULL;  | 
32  | 0  | }  | 
33  |  |  | 
34  |  | static int slh_dsa_key_hash_init(SLH_DSA_KEY *key)  | 
35  | 0  | { | 
36  | 0  |     int is_shake = key->params->is_shake;  | 
37  | 0  |     int security_category = key->params->security_category;  | 
38  | 0  |     const char *digest_alg = is_shake ? "SHAKE-256" : "SHA2-256";  | 
39  |  | 
  | 
40  | 0  |     key->md = EVP_MD_fetch(key->libctx, digest_alg, key->propq);  | 
41  | 0  |     if (key->md == NULL)  | 
42  | 0  |         return 0;  | 
43  |  |     /*  | 
44  |  |      * SHA2 algorithm(s) require SHA256 + HMAC_SHA(X) & MGF1(SHAX)  | 
45  |  |      * SHAKE algorithm(s) use SHAKE for all functions.  | 
46  |  |      */  | 
47  | 0  |     if (is_shake == 0) { | 
48  | 0  |         if (security_category == 1) { | 
49  |  |             /* For category 1 SHA2-256 is used for all hash operations */  | 
50  | 0  |             key->md_big = key->md;  | 
51  | 0  |         } else { | 
52  |  |             /* Security categories 3 & 5 also need SHA-512 */  | 
53  | 0  |             key->md_big = EVP_MD_fetch(key->libctx, "SHA2-512", key->propq);  | 
54  | 0  |             if (key->md_big == NULL)  | 
55  | 0  |                 goto err;  | 
56  | 0  |         }  | 
57  | 0  |         key->hmac = EVP_MAC_fetch(key->libctx, "HMAC", key->propq);  | 
58  | 0  |         if (key->hmac == NULL)  | 
59  | 0  |             goto err;  | 
60  | 0  |     }  | 
61  | 0  |     key->adrs_func = ossl_slh_get_adrs_fn(is_shake == 0);  | 
62  | 0  |     key->hash_func = ossl_slh_get_hash_fn(is_shake);  | 
63  | 0  |     return 1;  | 
64  | 0  |  err:  | 
65  | 0  |     slh_dsa_key_hash_cleanup(key);  | 
66  | 0  |     return 0;  | 
67  | 0  | }  | 
68  |  |  | 
69  |  | static void slh_dsa_key_hash_dup(SLH_DSA_KEY *dst, const SLH_DSA_KEY *src)  | 
70  | 0  | { | 
71  | 0  |     if (src->md_big != NULL && src->md_big != src->md)  | 
72  | 0  |         EVP_MD_up_ref(src->md_big);  | 
73  | 0  |     if (src->md != NULL)  | 
74  | 0  |         EVP_MD_up_ref(src->md);  | 
75  | 0  |     if (src->hmac != NULL)  | 
76  | 0  |         EVP_MAC_up_ref(src->hmac);  | 
77  | 0  | }  | 
78  |  |  | 
79  |  | /**  | 
80  |  |  * @brief Create a new SLH_DSA_KEY object  | 
81  |  |  *  | 
82  |  |  * @param libctx A OSSL_LIB_CTX object used for fetching algorithms.  | 
83  |  |  * @param propq The property query used for fetching algorithms  | 
84  |  |  * @param alg The algorithm name associated with the key type  | 
85  |  |  * @returns The new SLH_DSA_KEY object on success, or NULL on malloc failure  | 
86  |  |  */  | 
87  |  | SLH_DSA_KEY *ossl_slh_dsa_key_new(OSSL_LIB_CTX *libctx, const char *propq,  | 
88  |  |                                   const char *alg)  | 
89  | 0  | { | 
90  | 0  |     SLH_DSA_KEY *ret;  | 
91  | 0  |     const SLH_DSA_PARAMS *params = ossl_slh_dsa_params_get(alg);  | 
92  |  | 
  | 
93  | 0  |     if (params == NULL)  | 
94  | 0  |         return NULL;  | 
95  |  |  | 
96  | 0  |     ret = OPENSSL_zalloc(sizeof(*ret));  | 
97  | 0  |     if (ret != NULL) { | 
98  | 0  |         ret->libctx = libctx;  | 
99  | 0  |         ret->params = params;  | 
100  | 0  |         if (propq != NULL) { | 
101  | 0  |             ret->propq = OPENSSL_strdup(propq);  | 
102  | 0  |             if (ret->propq == NULL)  | 
103  | 0  |                 goto err;  | 
104  | 0  |         }  | 
105  | 0  |         if (!slh_dsa_key_hash_init(ret))  | 
106  | 0  |             goto err;  | 
107  | 0  |     }  | 
108  | 0  |     return ret;  | 
109  | 0  |  err:  | 
110  | 0  |     ossl_slh_dsa_key_free(ret);  | 
111  | 0  |     return NULL;  | 
112  | 0  | }  | 
113  |  |  | 
114  |  | /**  | 
115  |  |  * @brief Destroy a SLH_DSA_KEY object  | 
116  |  |  */  | 
117  |  | void ossl_slh_dsa_key_free(SLH_DSA_KEY *key)  | 
118  | 0  | { | 
119  | 0  |     if (key == NULL)  | 
120  | 0  |         return;  | 
121  |  |  | 
122  | 0  |     slh_dsa_key_hash_cleanup(key);  | 
123  | 0  |     OPENSSL_cleanse(&key->priv, sizeof(key->priv) >> 1);  | 
124  | 0  |     OPENSSL_free(key);  | 
125  | 0  | }  | 
126  |  |  | 
127  |  | /**  | 
128  |  |  * @brief Duplicate a key  | 
129  |  |  *  | 
130  |  |  * @param src A SLH_DSA_KEY object to copy  | 
131  |  |  * @param selection to select public and/or private components. Selecting the  | 
132  |  |  *                  private key will also select the public key  | 
133  |  |  * @returns The duplicated key, or NULL on failure.  | 
134  |  |  */  | 
135  |  | SLH_DSA_KEY *ossl_slh_dsa_key_dup(const SLH_DSA_KEY *src, int selection)  | 
136  | 0  | { | 
137  | 0  |     SLH_DSA_KEY *ret = NULL;  | 
138  |  | 
  | 
139  | 0  |     if (src == NULL)  | 
140  | 0  |         return NULL;  | 
141  |  |  | 
142  | 0  |     ret = OPENSSL_zalloc(sizeof(*ret));  | 
143  | 0  |     if (ret != NULL) { | 
144  | 0  |         *ret = *src; /* this copies everything including the keydata in priv[] */  | 
145  | 0  |         ret->propq = NULL;  | 
146  | 0  |         ret->pub = NULL;  | 
147  | 0  |         ret->has_priv = 0;  | 
148  | 0  |         slh_dsa_key_hash_dup(ret, src);  | 
149  | 0  |         if (src->propq != NULL) { | 
150  | 0  |             ret->propq = OPENSSL_strdup(src->propq);  | 
151  | 0  |             if (ret->propq == NULL)  | 
152  | 0  |                 goto err;  | 
153  | 0  |         }  | 
154  | 0  |         if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { | 
155  |  |             /* The public components are present if the private key is present */  | 
156  | 0  |             if (src->pub != NULL)  | 
157  | 0  |                 ret->pub = SLH_DSA_PUB(ret);  | 
158  | 0  |             if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)  | 
159  | 0  |                 ret->has_priv = src->has_priv;  | 
160  | 0  |         }  | 
161  | 0  |     }  | 
162  | 0  |     return ret;  | 
163  | 0  |  err:  | 
164  | 0  |     ossl_slh_dsa_key_free(ret);  | 
165  | 0  |     return NULL;  | 
166  | 0  | }  | 
167  |  |  | 
168  |  | /**  | 
169  |  |  * @brief Are 2 keys equal?  | 
170  |  |  *  | 
171  |  |  * To be equal the keys must have the same key data and algorithm name.  | 
172  |  |  *  | 
173  |  |  * @param key1 A SLH_DSA_KEY object  | 
174  |  |  * @param key2 A SLH_DSA_KEY object  | 
175  |  |  * @param selection to select public and/or private component comparison.  | 
176  |  |  * @returns 1 if the keys are equal otherwise it returns 0.  | 
177  |  |  */  | 
178  |  | int ossl_slh_dsa_key_equal(const SLH_DSA_KEY *key1, const SLH_DSA_KEY *key2,  | 
179  |  |                            int selection)  | 
180  | 0  | { | 
181  | 0  |     int key_checked = 0;  | 
182  |  |  | 
183  |  |     /* The parameter sets must match - i.e. The same algorithm name */  | 
184  | 0  |     if (key1->params != key2->params)  | 
185  | 0  |         return 0;  | 
186  |  |  | 
187  | 0  |     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { | 
188  | 0  |         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { | 
189  | 0  |             if (key1->pub != NULL && key2->pub != NULL) { | 
190  | 0  |                 if (memcmp(key1->pub, key2->pub, key1->params->pk_len) != 0)  | 
191  | 0  |                     return 0;  | 
192  | 0  |                 key_checked = 1;  | 
193  | 0  |             }  | 
194  | 0  |         }  | 
195  | 0  |         if (!key_checked  | 
196  | 0  |                 && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { | 
197  | 0  |             if (key1->has_priv && key2->has_priv) { | 
198  | 0  |                 if (memcmp(key1->priv, key2->priv,  | 
199  | 0  |                            key1->params->pk_len) != 0)  | 
200  | 0  |                     return 0;  | 
201  | 0  |                 key_checked = 1;  | 
202  | 0  |             }  | 
203  | 0  |         }  | 
204  | 0  |         return key_checked;  | 
205  | 0  |     }  | 
206  | 0  |     return 1;  | 
207  | 0  | }  | 
208  |  |  | 
209  |  | int ossl_slh_dsa_key_has(const SLH_DSA_KEY *key, int selection)  | 
210  | 0  | { | 
211  | 0  |     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { | 
212  | 0  |         if (key->pub == NULL)  | 
213  | 0  |             return 0; /* No public key */  | 
214  | 0  |         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0  | 
215  | 0  |                 && key->has_priv == 0)  | 
216  | 0  |             return 0; /* No private key */  | 
217  | 0  |         return 1;  | 
218  | 0  |     }  | 
219  | 0  |     return 0;  | 
220  | 0  | }  | 
221  |  |  | 
222  |  | int ossl_slh_dsa_key_pairwise_check(const SLH_DSA_KEY *key)  | 
223  | 0  | { | 
224  | 0  |     int ret;  | 
225  | 0  |     SLH_DSA_HASH_CTX *ctx = NULL;  | 
226  |  | 
  | 
227  | 0  |     if (key->pub == NULL || key->has_priv == 0)  | 
228  | 0  |         return 0;  | 
229  |  |  | 
230  | 0  |     ctx = ossl_slh_dsa_hash_ctx_new(key);  | 
231  | 0  |     if (ctx == NULL)  | 
232  | 0  |         return 0;  | 
233  | 0  |     ret = slh_dsa_compute_pk_root(ctx, (SLH_DSA_KEY *)key, 1);  | 
234  | 0  |     ossl_slh_dsa_hash_ctx_free(ctx);  | 
235  | 0  |     return ret;  | 
236  | 0  | }  | 
237  |  |  | 
238  |  | /**  | 
239  |  |  * @brief Load a SLH_DSA key from raw data.  | 
240  |  |  *  | 
241  |  |  * @param key An SLH_DSA key to load into  | 
242  |  |  * @param params An array of parameters containing key data.  | 
243  |  |  * @param include_private Set to 1 to optionally include the private key data  | 
244  |  |  *                        if it exists.  | 
245  |  |  * @returns 1 on success, or 0 on failure.  | 
246  |  |  */  | 
247  |  | int ossl_slh_dsa_key_fromdata(SLH_DSA_KEY *key, const OSSL_PARAM params[],  | 
248  |  |                               int include_private)  | 
249  | 0  | { | 
250  | 0  |     size_t priv_len, key_len, data_len = 0;  | 
251  | 0  |     const OSSL_PARAM *param_priv = NULL, *param_pub = NULL;  | 
252  | 0  |     void *p;  | 
253  |  | 
  | 
254  | 0  |     if (key == NULL)  | 
255  | 0  |         return 0;  | 
256  |  |  | 
257  |  |     /* The private key consists of 4 elements SK_SEED, SK_PRF, PK_SEED and PK_ROOT */  | 
258  | 0  |     priv_len = ossl_slh_dsa_key_get_priv_len(key);  | 
259  |  |     /* The size of either SK_SEED + SK_PRF OR PK_SEED + PK_ROOT */  | 
260  | 0  |     key_len = priv_len >> 1;  | 
261  |  |  | 
262  |  |     /* Private key is optional */  | 
263  | 0  |     if (include_private) { | 
264  | 0  |         param_priv = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);  | 
265  | 0  |         if (param_priv != NULL) { | 
266  | 0  |             p = key->priv;  | 
267  | 0  |             if (!OSSL_PARAM_get_octet_string(param_priv, &p, priv_len, &data_len))  | 
268  | 0  |                 return 0;  | 
269  |  |             /* If the data read includes all 4 elements then we are finished */  | 
270  | 0  |             if (data_len == priv_len) { | 
271  | 0  |                 key->has_priv = 1;  | 
272  | 0  |                 key->pub = SLH_DSA_PUB(key);  | 
273  | 0  |                 return 1;  | 
274  | 0  |             }  | 
275  |  |             /* Otherwise it must be just SK_SEED + SK_PRF */  | 
276  | 0  |             if (data_len != key_len)  | 
277  | 0  |                 goto err;  | 
278  | 0  |             key->has_priv = 1;  | 
279  | 0  |         }  | 
280  | 0  |     }  | 
281  |  |     /*  | 
282  |  |      * In the case where the passed in private key does not contain the public key  | 
283  |  |      * there MUST be a separate public key, since the private key cannot exist  | 
284  |  |      * without the public key elements. NOTE that this does not accept half of  | 
285  |  |      * the public key, (Keygen must be used for this case currently).  | 
286  |  |      */  | 
287  | 0  |     p = SLH_DSA_PUB(key);  | 
288  | 0  |     param_pub = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);  | 
289  | 0  |     if (param_pub == NULL  | 
290  | 0  |             || !OSSL_PARAM_get_octet_string(param_pub, &p, key_len, &data_len)  | 
291  | 0  |             || data_len != key_len)  | 
292  | 0  |         goto err;  | 
293  | 0  |     key->pub = p;  | 
294  | 0  |     return 1;  | 
295  | 0  |  err:  | 
296  | 0  |     key->pub = NULL;  | 
297  | 0  |     key->has_priv = 0;  | 
298  | 0  |     OPENSSL_cleanse(key->priv, priv_len);  | 
299  | 0  |     return 0;  | 
300  | 0  | }  | 
301  |  |  | 
302  |  | /**  | 
303  |  |  * Generate the public key root from private key (seed and prf) and public key seed.  | 
304  |  |  * See FIPS 205 Section 9.1 Algorithm 18  | 
305  |  |  *  | 
306  |  |  * @param ctx Contains SLH_DSA algorithm functions and constants.  | 
307  |  |  * @param out An SLH_DSA key containing the private key (seed and prf) and public key seed.  | 
308  |  |  *            The public root key is written to this key.  | 
309  |  |  * @param validate If set to 1 the computed public key is not written to the key,  | 
310  |  |  *                 but will be compared to the existing value.  | 
311  |  |  * @returns 1 if the root key is generated or compared successfully, or 0 on error.  | 
312  |  |  */  | 
313  |  | static int slh_dsa_compute_pk_root(SLH_DSA_HASH_CTX *ctx, SLH_DSA_KEY *out,  | 
314  |  |                                    int validate)  | 
315  | 0  | { | 
316  | 0  |     const SLH_DSA_KEY *key = ctx->key;  | 
317  | 0  |     SLH_ADRS_FUNC_DECLARE(key, adrsf);  | 
318  | 0  |     SLH_ADRS_DECLARE(adrs);  | 
319  | 0  |     const SLH_DSA_PARAMS *params = key->params;  | 
320  | 0  |     size_t n = params->n;  | 
321  | 0  |     uint8_t pk_root[SLH_DSA_MAX_N], *dst;  | 
322  |  | 
  | 
323  | 0  |     adrsf->zero(adrs);  | 
324  | 0  |     adrsf->set_layer_address(adrs, params->d - 1);  | 
325  |  | 
  | 
326  | 0  |     dst = validate ? pk_root : SLH_DSA_PK_ROOT(out);  | 
327  |  |  | 
328  |  |     /* Generate the ROOT public key */  | 
329  | 0  |     return ossl_slh_xmss_node(ctx, SLH_DSA_SK_SEED(key), 0, params->hm,  | 
330  | 0  |                               SLH_DSA_PK_SEED(key), adrs, dst, n)  | 
331  | 0  |         && (validate == 0 || memcmp(dst, SLH_DSA_PK_ROOT(out), n) == 0);  | 
332  | 0  | }  | 
333  |  |  | 
334  |  | /**  | 
335  |  |  * @brief Generate a SLH_DSA keypair. The private key seed and prf as well as the  | 
336  |  |  * public key seed are generated using an approved DRBG's. The public key root is  | 
337  |  |  * calculated using these generated values.  | 
338  |  |  * See FIPS 205 Section 10.1 Algorithm 21  | 
339  |  |  *  | 
340  |  |  * @param ctx Contains SLH_DSA algorithm functions and constants  | 
341  |  |  * @param out An SLH_DSA key to write key pair data to.  | 
342  |  |  * @param lib_ctx A library context for fetching RAND algorithms  | 
343  |  |  * @param entropy Optional entropy to use instead of using a DRBG.  | 
344  |  |  *        Required for ACVP testing. It may be NULL.  | 
345  |  |  * @param entropy_len the size of |entropy|. If set it must be at least 3 * |n|.  | 
346  |  |  * @returns 1 if the key is generated or 0 otherwise.  | 
347  |  |  */  | 
348  |  | int ossl_slh_dsa_generate_key(SLH_DSA_HASH_CTX *ctx, SLH_DSA_KEY *out,  | 
349  |  |                               OSSL_LIB_CTX *lib_ctx,  | 
350  |  |                               const uint8_t *entropy, size_t entropy_len)  | 
351  | 0  | { | 
352  | 0  |     size_t n = out->params->n;  | 
353  | 0  |     size_t secret_key_len = 2 * n; /* The length of SK_SEED + SK_PRF */  | 
354  | 0  |     size_t pk_seed_len = n;        /* The length of PK_SEED */  | 
355  | 0  |     size_t entropy_len_expected = secret_key_len + pk_seed_len;  | 
356  | 0  |     uint8_t *priv = SLH_DSA_PRIV(out);  | 
357  | 0  |     uint8_t *pub = SLH_DSA_PUB(out);  | 
358  |  | 
  | 
359  | 0  |     if (entropy != NULL && entropy_len != 0) { | 
360  | 0  |         if (entropy_len != entropy_len_expected)  | 
361  | 0  |             goto err;  | 
362  | 0  |         memcpy(priv, entropy, entropy_len_expected);  | 
363  | 0  |     } else { | 
364  | 0  |         if (RAND_priv_bytes_ex(lib_ctx, priv, secret_key_len, 0) <= 0  | 
365  | 0  |                 || RAND_bytes_ex(lib_ctx, pub, pk_seed_len, 0) <= 0)  | 
366  | 0  |             goto err;  | 
367  | 0  |     }  | 
368  | 0  |     if (!slh_dsa_compute_pk_root(ctx, out, 0))  | 
369  | 0  |         goto err;  | 
370  | 0  |     out->pub = pub;  | 
371  | 0  |     out->has_priv = 1;  | 
372  | 0  |     return 1;  | 
373  | 0  | err:  | 
374  | 0  |     out->pub = NULL;  | 
375  | 0  |     out->has_priv = 0;  | 
376  | 0  |     OPENSSL_cleanse(priv, secret_key_len);  | 
377  | 0  |     return 0;  | 
378  | 0  | }  | 
379  |  |  | 
380  |  | /**  | 
381  |  |  * @brief This is used when a SLH key is used for an operation.  | 
382  |  |  * This checks that the algorithm is the same (i.e. uses the same parameters)  | 
383  |  |  *  | 
384  |  |  * @param ctx Contains SLH_DSA algorithm functions and constants to be used for  | 
385  |  |  *            an operation.  | 
386  |  |  * @param key A SLH_DSA key to use for an operation.  | 
387  |  |  *  | 
388  |  |  * @returns 1 if the algorithm matches, or 0 otherwise.  | 
389  |  |  */  | 
390  |  | int ossl_slh_dsa_key_type_matches(const SLH_DSA_KEY *key, const char *alg)  | 
391  | 0  | { | 
392  | 0  |     return (OPENSSL_strcasecmp(key->params->alg, alg) == 0);  | 
393  | 0  | }  | 
394  |  |  | 
395  |  | /* Returns the public key data or NULL if there is no public key */  | 
396  |  | const uint8_t *ossl_slh_dsa_key_get_pub(const SLH_DSA_KEY *key)  | 
397  | 0  | { | 
398  | 0  |     return key->pub;  | 
399  | 0  | }  | 
400  |  |  | 
401  |  | /* Returns the constant 2 * |n| which is the size of PK_SEED + PK_ROOT */  | 
402  |  | size_t ossl_slh_dsa_key_get_pub_len(const SLH_DSA_KEY *key)  | 
403  | 0  | { | 
404  | 0  |     return 2 * key->params->n;  | 
405  | 0  | }  | 
406  |  |  | 
407  |  | /* Returns the private key data or NULL if there is no private key */  | 
408  |  | const uint8_t *ossl_slh_dsa_key_get_priv(const SLH_DSA_KEY *key)  | 
409  | 0  | { | 
410  | 0  |     return key->has_priv ? key->priv : NULL;  | 
411  | 0  | }  | 
412  |  |  | 
413  |  | /*  | 
414  |  |  * Returns the constant 4 * |n| which is the size of both  | 
415  |  |  * the private and public key components.  | 
416  |  |  * SK_SEED + SK_ROOT + PK_SEED + PK_ROOT  | 
417  |  |  */  | 
418  |  | size_t ossl_slh_dsa_key_get_priv_len(const SLH_DSA_KEY *key)  | 
419  | 0  | { | 
420  | 0  |     return 4 * key->params->n;  | 
421  | 0  | }  | 
422  |  |  | 
423  |  | size_t ossl_slh_dsa_key_get_n(const SLH_DSA_KEY *key)  | 
424  | 0  | { | 
425  | 0  |     return key->params->n;  | 
426  | 0  | }  | 
427  |  |  | 
428  |  | int ossl_slh_dsa_key_get_security_category(const SLH_DSA_KEY *key)  | 
429  | 0  | { | 
430  | 0  |     return key->params->security_category;  | 
431  | 0  | }  | 
432  |  |  | 
433  |  | size_t ossl_slh_dsa_key_get_sig_len(const SLH_DSA_KEY *key)  | 
434  | 0  | { | 
435  | 0  |     return key->params->sig_len;  | 
436  | 0  | }  | 
437  |  | const char *ossl_slh_dsa_key_get_name(const SLH_DSA_KEY *key)  | 
438  | 0  | { | 
439  | 0  |     return key->params->alg;  | 
440  | 0  | }  | 
441  |  | int ossl_slh_dsa_key_get_type(const SLH_DSA_KEY *key)  | 
442  | 0  | { | 
443  | 0  |     return key->params->type;  | 
444  | 0  | }  | 
445  |  |  | 
446  |  | int ossl_slh_dsa_set_priv(SLH_DSA_KEY *key, const uint8_t *priv, size_t priv_len)  | 
447  | 0  | { | 
448  | 0  |     if (ossl_slh_dsa_key_get_priv_len(key) != priv_len)  | 
449  | 0  |         return 0;  | 
450  | 0  |     memcpy(key->priv, priv, priv_len);  | 
451  | 0  |     key->has_priv = 1;  | 
452  | 0  |     key->pub = SLH_DSA_PUB(key);  | 
453  | 0  |     return 1;  | 
454  | 0  | }  | 
455  |  |  | 
456  |  | int ossl_slh_dsa_set_pub(SLH_DSA_KEY *key, const uint8_t *pub, size_t pub_len)  | 
457  | 0  | { | 
458  | 0  |     if (ossl_slh_dsa_key_get_pub_len(key) != pub_len)  | 
459  | 0  |         return 0;  | 
460  | 0  |     key->pub = SLH_DSA_PUB(key);  | 
461  | 0  |     memcpy(key->pub, pub, pub_len);  | 
462  | 0  |     key->has_priv = 0;  | 
463  | 0  |     return 1;  | 
464  | 0  | }  | 
465  |  |  | 
466  |  | #ifndef FIPS_MODULE  | 
467  |  | int ossl_slh_dsa_key_to_text(BIO *out, const SLH_DSA_KEY *key, int selection)  | 
468  | 0  | { | 
469  | 0  |     const char *name;  | 
470  |  | 
  | 
471  | 0  |     if (out == NULL || key == NULL) { | 
472  | 0  |         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);  | 
473  | 0  |         return 0;  | 
474  | 0  |     }  | 
475  | 0  |     name = ossl_slh_dsa_key_get_name(key);  | 
476  | 0  |     if (ossl_slh_dsa_key_get_pub(key) == NULL) { | 
477  |  |         /* Regardless of the |selection|, there must be a public key */  | 
478  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,  | 
479  | 0  |                        "no %s key material available", name);  | 
480  | 0  |         return 0;  | 
481  | 0  |     }  | 
482  |  |  | 
483  | 0  |     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { | 
484  | 0  |         if (ossl_slh_dsa_key_get_priv(key) == NULL) { | 
485  | 0  |             ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,  | 
486  | 0  |                            "no %s key material available", name);  | 
487  | 0  |             return 0;  | 
488  | 0  |         }  | 
489  | 0  |         if (BIO_printf(out, "%s Private-Key:\n", name) <= 0)  | 
490  | 0  |             return 0;  | 
491  | 0  |         if (!ossl_bio_print_labeled_buf(out, "priv:", ossl_slh_dsa_key_get_priv(key),  | 
492  | 0  |                                         ossl_slh_dsa_key_get_priv_len(key)))  | 
493  | 0  |             return 0;  | 
494  | 0  |     } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { | 
495  | 0  |         if (BIO_printf(out, "%s Public-Key:\n", name) <= 0)  | 
496  | 0  |             return 0;  | 
497  | 0  |     }  | 
498  |  |  | 
499  | 0  |     if (!ossl_bio_print_labeled_buf(out, "pub:", ossl_slh_dsa_key_get_pub(key),  | 
500  | 0  |                                     ossl_slh_dsa_key_get_pub_len(key)))  | 
501  | 0  |         return 0;  | 
502  |  |  | 
503  | 0  |     return 1;  | 
504  | 0  | }  | 
505  |  | #endif /* FIPS_MODULE */  |