/src/openssl/crypto/slh_dsa/slh_dsa.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | #include <stddef.h> |
10 | | #include <string.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/proverr.h> |
14 | | #include "slh_dsa_local.h" |
15 | | #include "slh_dsa_key.h" |
16 | | |
17 | | #define SLH_MAX_M 49 /* See slh_params.c */ |
18 | | /* The size of md is (21..40 bytes) - since a is in bits round up to nearest byte */ |
19 | 0 | #define MD_LEN(params) (((params)->k * (params)->a + 7) >> 3) |
20 | | |
21 | | static int get_tree_ids(PACKET *pkt, const SLH_DSA_PARAMS *params, |
22 | | uint64_t *tree_id, uint32_t *leaf_id); |
23 | | |
24 | | /** |
25 | | * @brief SLH-DSA Signature generation |
26 | | * See FIPS 205 Section 9.2 Algorithm 19 |
27 | | * |
28 | | * A signature consists of |
29 | | * r[n] random bytes |
30 | | * [k]*[1+a][n] FORS signature bytes |
31 | | * [h + d*len][n] Hyper tree signature bytes |
32 | | * |
33 | | * @param ctx Contains SLH_DSA algorithm functions and constants, and the |
34 | | * private SLH_DSA key to use for signing. |
35 | | * @param msg The message to sign. This may be encoded beforehand. |
36 | | * @param msg_len The size of |msg| |
37 | | * @param sig The returned signature |
38 | | * @param sig_len The size of the returned |sig| |
39 | | * @param sig_size The maximum size of |sig| |
40 | | * @param opt_rand An optional random value to use of size |n|. It can be NULL. |
41 | | * @returns 1 if the signature generation succeeded or 0 otherwise. |
42 | | */ |
43 | | static int slh_sign_internal(SLH_DSA_HASH_CTX *hctx, |
44 | | const uint8_t *msg, size_t msg_len, |
45 | | uint8_t *sig, size_t *sig_len, size_t sig_size, |
46 | | const uint8_t *opt_rand) |
47 | 0 | { |
48 | 0 | int ret = 0; |
49 | 0 | const SLH_DSA_KEY *priv = hctx->key; |
50 | 0 | const SLH_DSA_PARAMS *params = priv->params; |
51 | 0 | size_t sig_len_expected = params->sig_len; |
52 | 0 | uint8_t m_digest[SLH_MAX_M]; |
53 | 0 | const uint8_t *md; /* The first md_len bytes of m_digest */ |
54 | 0 | size_t md_len = MD_LEN(params); /* The size of the digest |md| */ |
55 | | /* Points to |m_digest| buffer, it is also reused to point to |sig_fors| */ |
56 | 0 | PACKET r_packet, *rpkt = &r_packet; |
57 | 0 | uint8_t *r, *sig_fors; /* Pointers into buffer inside |wpkt| */ |
58 | 0 | WPACKET w_packet, *wpkt = &w_packet; /* Points to output |sig| buffer */ |
59 | 0 | const uint8_t *pk_seed, *sk_seed; /* pointers to elements within |priv| */ |
60 | 0 | uint8_t pk_fors[SLH_MAX_N]; |
61 | 0 | uint64_t tree_id; |
62 | 0 | uint32_t leaf_id; |
63 | |
|
64 | 0 | SLH_ADRS_DECLARE(adrs); |
65 | 0 | SLH_HASH_FUNC_DECLARE(priv, hashf); |
66 | 0 | SLH_ADRS_FUNC_DECLARE(priv, adrsf); |
67 | |
|
68 | 0 | if (sig == NULL) { |
69 | 0 | *sig_len = sig_len_expected; |
70 | 0 | return 1; |
71 | 0 | } |
72 | | |
73 | 0 | if (sig_size < sig_len_expected) { |
74 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE, |
75 | 0 | "is %zu, should be at least %zu", sig_size, sig_len_expected); |
76 | 0 | return 0; |
77 | 0 | } |
78 | | /* Exit if private key is not set */ |
79 | 0 | if (priv->has_priv == 0) { |
80 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
81 | 0 | return 0; |
82 | 0 | } |
83 | | |
84 | 0 | if (!WPACKET_init_static_len(wpkt, sig, sig_len_expected, 0)) |
85 | 0 | return 0; |
86 | 0 | if (!PACKET_buf_init(rpkt, m_digest, params->m)) |
87 | 0 | goto err; |
88 | | |
89 | 0 | pk_seed = SLH_DSA_PK_SEED(priv); |
90 | 0 | sk_seed = SLH_DSA_SK_SEED(priv); |
91 | |
|
92 | 0 | if (opt_rand == NULL) |
93 | 0 | opt_rand = pk_seed; |
94 | |
|
95 | 0 | adrsf->zero(adrs); |
96 | | /* calculate Randomness value r, and output to the SLH-DSA signature */ |
97 | 0 | r = WPACKET_get_curr(wpkt); |
98 | 0 | if (!hashf->PRF_MSG(hctx, SLH_DSA_SK_PRF(priv), opt_rand, msg, msg_len, wpkt) |
99 | | /* generate a digest of size |params->m| bytes where m is (30..49) */ |
100 | 0 | || !hashf->H_MSG(hctx, r, pk_seed, SLH_DSA_PK_ROOT(priv), msg, msg_len, |
101 | 0 | m_digest, sizeof(m_digest)) |
102 | | /* Grab the first md_len bytes of m_digest to use in fors_sign() */ |
103 | 0 | || !PACKET_get_bytes(rpkt, &md, md_len) |
104 | | /* Grab remaining bytes from m_digest to select tree and leaf id's */ |
105 | 0 | || !get_tree_ids(rpkt, params, &tree_id, &leaf_id)) |
106 | 0 | goto err; |
107 | | |
108 | 0 | adrsf->set_tree_address(adrs, tree_id); |
109 | 0 | adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_FORS_TREE); |
110 | 0 | adrsf->set_keypair_address(adrs, leaf_id); |
111 | |
|
112 | 0 | sig_fors = WPACKET_get_curr(wpkt); |
113 | | /* generate the FORS signature and append it to the SLH-DSA signature */ |
114 | 0 | ret = ossl_slh_fors_sign(hctx, md, sk_seed, pk_seed, adrs, wpkt) |
115 | | /* Reuse rpkt to point to the FORS signature that was just generated */ |
116 | 0 | && PACKET_buf_init(rpkt, sig_fors, WPACKET_get_curr(wpkt) - sig_fors) |
117 | | /* Calculate the FORS public key using the generated FORS signature */ |
118 | 0 | && ossl_slh_fors_pk_from_sig(hctx, rpkt, md, pk_seed, adrs, |
119 | 0 | pk_fors, sizeof(pk_fors)) |
120 | | /* Generate ht signature and append to the SLH-DSA signature */ |
121 | 0 | && ossl_slh_ht_sign(hctx, pk_fors, sk_seed, pk_seed, tree_id, leaf_id, |
122 | 0 | wpkt); |
123 | 0 | err: |
124 | 0 | if (!WPACKET_finish(wpkt)) |
125 | 0 | ret = 0; |
126 | 0 | OPENSSL_cleanse(m_digest, sizeof(m_digest)); |
127 | 0 | OPENSSL_cleanse(pk_fors, sizeof(pk_fors)); |
128 | 0 | if (ret) |
129 | 0 | *sig_len = sig_len_expected; |
130 | 0 | else |
131 | | /* Erase any partial signature output */ |
132 | 0 | OPENSSL_cleanse(sig, sig_len_expected); |
133 | 0 | return ret; |
134 | 0 | } |
135 | | |
136 | | /** |
137 | | * @brief SLH-DSA Signature verification |
138 | | * See FIPS 205 Section 9.3 Algorithm 20 |
139 | | * |
140 | | * A signature consists of |
141 | | * r[n] random bytes |
142 | | * [k]*[1+a][n] FORS signature bytes |
143 | | * [h + d*len][n] Hyper tree signature bytes |
144 | | * |
145 | | * @param hctx Contains SLH_DSA algorithm functions and constants and the |
146 | | * public SLH_DSA key to use for verification. |
147 | | * @param msg The message to verify. This may be encoded beforehand. |
148 | | * @param msg_len The size of |msg| |
149 | | * @param sig A signature to verify |
150 | | * @param sig_len The size of |sig| |
151 | | * @returns 1 if the signature verification succeeded or 0 otherwise. |
152 | | */ |
153 | | static int slh_verify_internal(SLH_DSA_HASH_CTX *hctx, |
154 | | const uint8_t *msg, size_t msg_len, |
155 | | const uint8_t *sig, size_t sig_len) |
156 | 0 | { |
157 | 0 | int ret = 0; |
158 | 0 | const SLH_DSA_KEY *pub = hctx->key; |
159 | 0 | SLH_HASH_FUNC_DECLARE(pub, hashf); |
160 | 0 | SLH_ADRS_FUNC_DECLARE(pub, adrsf); |
161 | 0 | SLH_ADRS_DECLARE(adrs); |
162 | 0 | const SLH_DSA_PARAMS *params = pub->params; |
163 | 0 | uint32_t n = params->n; |
164 | 0 | const uint8_t *pk_seed, *pk_root; /* Pointers to elements in |pub| */ |
165 | 0 | PACKET pkt, *sig_rpkt = &pkt; /* Points to the |sig| buffer */ |
166 | 0 | uint8_t m_digest[SLH_MAX_M]; |
167 | 0 | const uint8_t *md; /* This is a pointer into the buffer in m_digest_rpkt */ |
168 | 0 | size_t md_len = MD_LEN(params); /* 21..40 bytes */ |
169 | 0 | PACKET pkt2, *m_digest_rpkt = &pkt2; /* Points to m_digest buffer */ |
170 | 0 | const uint8_t *r; /* Pointer to |sig_rpkt| buffer */ |
171 | 0 | uint8_t pk_fors[SLH_MAX_N]; |
172 | 0 | uint64_t tree_id; |
173 | 0 | uint32_t leaf_id; |
174 | | |
175 | | /* Exit if public key is not set */ |
176 | 0 | if (pub->pub == NULL) { |
177 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
178 | 0 | return 0; |
179 | 0 | } |
180 | | |
181 | | /* Exit if signature is invalid size */ |
182 | 0 | if (sig_len != params->sig_len |
183 | 0 | || !PACKET_buf_init(sig_rpkt, sig, sig_len)) |
184 | 0 | return 0; |
185 | 0 | if (!PACKET_get_bytes(sig_rpkt, &r, n)) |
186 | 0 | return 0; |
187 | | |
188 | 0 | adrsf->zero(adrs); |
189 | |
|
190 | 0 | pk_seed = SLH_DSA_PK_SEED(pub); |
191 | 0 | pk_root = SLH_DSA_PK_ROOT(pub); |
192 | |
|
193 | 0 | if (!hashf->H_MSG(hctx, r, pk_seed, pk_root, msg, msg_len, |
194 | 0 | m_digest, sizeof(m_digest))) |
195 | 0 | goto err; |
196 | | |
197 | | /* |
198 | | * Get md (the first md_len bytes of m_digest to use in |
199 | | * ossl_slh_fors_pk_from_sig(), and then retrieve the tree id and leaf id |
200 | | * from the remaining bytes in m_digest. |
201 | | */ |
202 | 0 | if (!PACKET_buf_init(m_digest_rpkt, m_digest, sizeof(m_digest)) |
203 | 0 | || !PACKET_get_bytes(m_digest_rpkt, &md, md_len) |
204 | 0 | || !get_tree_ids(m_digest_rpkt, params, &tree_id, &leaf_id)) |
205 | 0 | goto err; |
206 | | |
207 | 0 | adrsf->set_tree_address(adrs, tree_id); |
208 | 0 | adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_FORS_TREE); |
209 | 0 | adrsf->set_keypair_address(adrs, leaf_id); |
210 | 0 | ret = ossl_slh_fors_pk_from_sig(hctx, sig_rpkt, md, pk_seed, adrs, |
211 | 0 | pk_fors, sizeof(pk_fors)) |
212 | 0 | && ossl_slh_ht_verify(hctx, pk_fors, sig_rpkt, pk_seed, |
213 | 0 | tree_id, leaf_id, pk_root) |
214 | 0 | && PACKET_remaining(sig_rpkt) == 0; |
215 | 0 | err: |
216 | 0 | OPENSSL_cleanse(m_digest, sizeof(m_digest)); |
217 | 0 | OPENSSL_cleanse(pk_fors, sizeof(pk_fors)); |
218 | 0 | return ret; |
219 | 0 | } |
220 | | |
221 | | /** |
222 | | * @brief Encode a message |
223 | | * See FIPS 205 Algorithm 22 Step 8 (and algorithm 24 Step 4). |
224 | | * |
225 | | * SLH_DSA pure signatures are encoded as M' = 00 || ctx_len || ctx || msg |
226 | | * Where ctx is the empty string by default and ctx_len <= 255. |
227 | | * |
228 | | * @param msg A message to encode |
229 | | * @param msg_len The size of |msg| |
230 | | * @param ctx An optional context to add to the message encoding. |
231 | | * @param ctx_len The size of |ctx|. It must be in the range 0..255 |
232 | | * @param encode Use the Pure signature encoding if this is 1, and dont encode |
233 | | * if this value is 0. |
234 | | * @param tmp A small buffer that may be used if the message is small. |
235 | | * @param tmp_len The size of |tmp| |
236 | | * @param out_len The size of the returned encoded buffer. |
237 | | * @returns A buffer containing the encoded message. If the passed in |
238 | | * |tmp| buffer is big enough to hold the encoded message then it returns |tmp| |
239 | | * otherwise it allocates memory which must be freed by the caller. If |encode| |
240 | | * is 0 then it returns |msg|. NULL is returned if there is a failure. |
241 | | */ |
242 | | static uint8_t *msg_encode(const uint8_t *msg, size_t msg_len, |
243 | | const uint8_t *ctx, size_t ctx_len, int encode, |
244 | | uint8_t *tmp, size_t tmp_len, size_t *out_len) |
245 | 0 | { |
246 | 0 | WPACKET pkt; |
247 | 0 | uint8_t *encoded = NULL; |
248 | 0 | size_t encoded_len; |
249 | |
|
250 | 0 | if (encode == 0) { |
251 | | /* Raw message */ |
252 | 0 | *out_len = msg_len; |
253 | 0 | return (uint8_t *)msg; |
254 | 0 | } |
255 | | |
256 | 0 | if (ctx_len > SLH_DSA_MAX_CONTEXT_STRING_LEN) |
257 | 0 | return NULL; |
258 | | |
259 | | /* Pure encoding */ |
260 | 0 | encoded_len = 1 + 1 + ctx_len + msg_len; |
261 | 0 | if (encoded_len < msg_len) /* Check for overflow */ |
262 | 0 | return NULL; |
263 | 0 | *out_len = encoded_len; |
264 | 0 | if (encoded_len <= tmp_len) { |
265 | 0 | encoded = tmp; |
266 | 0 | } else { |
267 | 0 | encoded = OPENSSL_zalloc(encoded_len); |
268 | 0 | if (encoded == NULL) |
269 | 0 | return NULL; |
270 | 0 | } |
271 | 0 | if (!WPACKET_init_static_len(&pkt, encoded, encoded_len, 0) |
272 | 0 | || !WPACKET_put_bytes_u8(&pkt, 0) |
273 | 0 | || !WPACKET_put_bytes_u8(&pkt, (uint8_t)ctx_len) |
274 | 0 | || !WPACKET_memcpy(&pkt, ctx, ctx_len) |
275 | 0 | || !WPACKET_memcpy(&pkt, msg, msg_len) |
276 | 0 | || !WPACKET_finish(&pkt)) { |
277 | 0 | if (encoded != tmp) |
278 | 0 | OPENSSL_free(encoded); |
279 | 0 | encoded = NULL; |
280 | 0 | WPACKET_cleanup(&pkt); |
281 | 0 | } |
282 | 0 | return encoded; |
283 | 0 | } |
284 | | |
285 | | /** |
286 | | * See FIPS 205 Section 10.2.1 Algorithm 22 |
287 | | * @returns 1 on success, or 0 on error. |
288 | | */ |
289 | | int ossl_slh_dsa_sign(SLH_DSA_HASH_CTX *slh_ctx, |
290 | | const uint8_t *msg, size_t msg_len, |
291 | | const uint8_t *ctx, size_t ctx_len, |
292 | | const uint8_t *add_rand, int encode, |
293 | | unsigned char *sig, size_t *siglen, size_t sigsize) |
294 | 0 | { |
295 | 0 | uint8_t m_tmp[1024], *m = m_tmp; |
296 | 0 | size_t m_len = 0; |
297 | 0 | int ret = 0; |
298 | |
|
299 | 0 | if (sig != NULL) { |
300 | 0 | m = msg_encode(msg, msg_len, ctx, ctx_len, encode, m_tmp, sizeof(m_tmp), |
301 | 0 | &m_len); |
302 | 0 | if (m == NULL) |
303 | 0 | return 0; |
304 | 0 | } |
305 | 0 | ret = slh_sign_internal(slh_ctx, m, m_len, sig, siglen, sigsize, add_rand); |
306 | | /* The encoded message may contain confidential message content */ |
307 | 0 | if (m != msg) { |
308 | 0 | if (m != m_tmp) |
309 | 0 | OPENSSL_clear_free(m, m_len); |
310 | 0 | else |
311 | 0 | OPENSSL_cleanse(m_tmp, sizeof(m_tmp)); |
312 | 0 | } |
313 | 0 | return ret; |
314 | 0 | } |
315 | | |
316 | | /** |
317 | | * See FIPS 205 Section 10.3 Algorithm 24 |
318 | | * @returns 1 on success, or 0 on error. |
319 | | */ |
320 | | int ossl_slh_dsa_verify(SLH_DSA_HASH_CTX *slh_ctx, |
321 | | const uint8_t *msg, size_t msg_len, |
322 | | const uint8_t *ctx, size_t ctx_len, int encode, |
323 | | const uint8_t *sig, size_t sig_len) |
324 | 0 | { |
325 | 0 | uint8_t *m; |
326 | 0 | size_t m_len; |
327 | 0 | uint8_t m_tmp[1024]; |
328 | 0 | int ret = 0; |
329 | |
|
330 | 0 | m = msg_encode(msg, msg_len, ctx, ctx_len, encode, m_tmp, sizeof(m_tmp), |
331 | 0 | &m_len); |
332 | 0 | if (m == NULL) |
333 | 0 | return 0; |
334 | | |
335 | 0 | ret = slh_verify_internal(slh_ctx, m, m_len, sig, sig_len); |
336 | | /* The encoded message may contain confidential message content */ |
337 | 0 | if (m != msg) { |
338 | 0 | if (m != m_tmp) |
339 | 0 | OPENSSL_clear_free(m, m_len); |
340 | 0 | else |
341 | 0 | OPENSSL_cleanse(m_tmp, sizeof(m_tmp)); |
342 | 0 | } |
343 | 0 | return ret; |
344 | 0 | } |
345 | | |
346 | | /* |
347 | | * See FIPS 205 Algorithm 2 toInt(X, n) |
348 | | * OPENSSL_load_u64_be() cant be used here as the |in_len| may be < 8 |
349 | | */ |
350 | | static uint64_t bytes_to_u64_be(const uint8_t *in, size_t in_len) |
351 | 0 | { |
352 | |
|
353 | 0 | size_t i; |
354 | 0 | uint64_t total = 0; |
355 | |
|
356 | 0 | for (i = 0; i < in_len; i++) |
357 | 0 | total = (total << 8) + *in++; |
358 | 0 | return total; |
359 | 0 | } |
360 | | |
361 | | /* |
362 | | * See Algorithm 19 Steps 7..10 (also Algorithm 20 Step 10..13). |
363 | | * Converts digested bytes into a tree index, and leaf index within the tree. |
364 | | * The sizes are determined by the |params| parameter set. |
365 | | */ |
366 | | static int get_tree_ids(PACKET *rpkt, const SLH_DSA_PARAMS *params, |
367 | | uint64_t *tree_id, uint32_t *leaf_id) |
368 | 0 | { |
369 | 0 | const uint8_t *tree_id_bytes, *leaf_id_bytes; |
370 | 0 | uint32_t tree_id_len, leaf_id_len; |
371 | 0 | uint64_t tree_id_mask, leaf_id_mask; |
372 | |
|
373 | 0 | tree_id_len = ((params->h - params->hm + 7) >> 3); /* 7 or 8 bytes */ |
374 | 0 | leaf_id_len = ((params->hm + 7) >> 3); /* 1 or 2 bytes */ |
375 | |
|
376 | 0 | if (!PACKET_get_bytes(rpkt, &tree_id_bytes, tree_id_len) |
377 | 0 | || !PACKET_get_bytes(rpkt, &leaf_id_bytes, leaf_id_len)) |
378 | 0 | return 0; |
379 | | |
380 | | /* |
381 | | * In order to calculate A mod (2^X) where X is in the range of (54..64) |
382 | | * This is equivalent to A & (2^x - 1) which is just a sequence of X ones |
383 | | * that must fit into a 64 bit value. |
384 | | * e.g when X = 64 it would be A & (0xFFFF_FFFF_FFFF_FFFF) |
385 | | * when X = 54 it would be A & (0x3F_FFFF_FFFF_FFFF) |
386 | | * i.e. A & (0xFFFF_FFFF_FFFF_FFFF >> (64 - X)) |
387 | | */ |
388 | 0 | tree_id_mask = (~(uint64_t)0) >> (64 - (params->h - params->hm)); |
389 | 0 | leaf_id_mask = ((uint64_t)1 << params->hm) - 1; /* max value is 0x1FF when hm = 9 */ |
390 | 0 | *tree_id = bytes_to_u64_be(tree_id_bytes, tree_id_len) & tree_id_mask; |
391 | 0 | *leaf_id = (uint32_t)(bytes_to_u64_be(leaf_id_bytes, leaf_id_len) & leaf_id_mask); |
392 | 0 | return 1; |
393 | 0 | } |