Botan 3.10.0
Crypto and TLS for C&
sp_parameters.cpp
Go to the documentation of this file.
1/*
2 * SLH-DSA Parameters
3 * (C) 2023 Jack Lloyd
4 * 2023 Fabian Albert, René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#include <botan/sp_parameters.h>
10
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
18namespace {
19/// @returns pair: (parameter set, hash type)
20std::pair<Sphincs_Parameter_Set, Sphincs_Hash_Type> set_and_hash_from_name(std::string_view name) {
21 // SPHINCS+ Round 3.1 instances
22 if(name == "SphincsPlus-sha2-128s-r3.1") {
24 }
25 if(name == "SphincsPlus-sha2-128f-r3.1") {
27 }
28 if(name == "SphincsPlus-sha2-192s-r3.1") {
30 }
31 if(name == "SphincsPlus-sha2-192f-r3.1") {
33 }
34 if(name == "SphincsPlus-sha2-256s-r3.1") {
36 }
37 if(name == "SphincsPlus-sha2-256f-r3.1") {
39 }
40
41 if(name == "SphincsPlus-shake-128s-r3.1") {
43 }
44 if(name == "SphincsPlus-shake-128f-r3.1") {
46 }
47 if(name == "SphincsPlus-shake-192s-r3.1") {
49 }
50 if(name == "SphincsPlus-shake-192f-r3.1") {
52 }
53 if(name == "SphincsPlus-shake-256s-r3.1") {
55 }
56 if(name == "SphincsPlus-shake-256f-r3.1") {
58 }
59
60 if(name == "SphincsPlus-haraka-128s-r3.1") {
62 }
63 if(name == "SphincsPlus-haraka-128f-r3.1") {
65 }
66 if(name == "SphincsPlus-haraka-192s-r3.1") {
68 }
69 if(name == "SphincsPlus-haraka-192f-r3.1") {
71 }
72 if(name == "SphincsPlus-haraka-256s-r3.1") {
74 }
75 if(name == "SphincsPlus-haraka-256f-r3.1") {
77 }
78
79 // SLH-DSA instances WITHOUT prehash mode
80 if(name == "SLH-DSA-SHA2-128s") {
82 }
83 if(name == "SLH-DSA-SHA2-128f") {
85 }
86 if(name == "SLH-DSA-SHA2-192s") {
88 }
89 if(name == "SLH-DSA-SHA2-192f") {
91 }
92 if(name == "SLH-DSA-SHA2-256s") {
94 }
95 if(name == "SLH-DSA-SHA2-256f") {
97 }
98
99 if(name == "SLH-DSA-SHAKE-128s") {
101 }
102 if(name == "SLH-DSA-SHAKE-128f") {
104 }
105 if(name == "SLH-DSA-SHAKE-192s") {
107 }
108 if(name == "SLH-DSA-SHAKE-192f") {
110 }
111 if(name == "SLH-DSA-SHAKE-256s") {
113 }
114 if(name == "SLH-DSA-SHAKE-256f") {
116 }
117
118 // SLH-DSA instances WITH prehash mode
119 if(name == "Hash-SLH-DSA-SHA2-128s-with-SHA256") {
121 }
122 if(name == "Hash-SLH-DSA-SHA2-128f-with-SHA256") {
124 }
125 if(name == "Hash-SLH-DSA-SHA2-192s-with-SHA512") {
127 }
128 if(name == "Hash-SLH-DSA-SHA2-192f-with-SHA512") {
130 }
131 if(name == "Hash-SLH-DSA-SHA2-256s-with-SHA512") {
133 }
134 if(name == "Hash-SLH-DSA-SHA2-256f-with-SHA512") {
136 }
137
138 if(name == "Hash-SLH-DSA-SHAKE-128s-with-SHAKE128") {
140 }
141 if(name == "Hash-SLH-DSA-SHAKE-128f-with-SHAKE128") {
143 }
144 if(name == "Hash-SLH-DSA-SHAKE-192s-with-SHAKE256") {
146 }
147 if(name == "Hash-SLH-DSA-SHAKE-192f-with-SHAKE256") {
149 }
150 if(name == "Hash-SLH-DSA-SHAKE-256s-with-SHAKE256") {
152 }
153 if(name == "Hash-SLH-DSA-SHAKE-256f-with-SHAKE256") {
155 }
156
157 throw Lookup_Error(fmt("No SLH-DSA (or SPHINCS+) parameter supported for: {}", name));
158}
159
160std::string name_from_set_and_hash(Sphincs_Parameter_Set set, Sphincs_Hash_Type hash) {
161 if(hash == Sphincs_Hash_Type::Sha256) {
162 switch(set) {
164 return "SphincsPlus-sha2-128s-r3.1";
166 return "SphincsPlus-sha2-128f-r3.1";
168 return "SphincsPlus-sha2-192s-r3.1";
170 return "SphincsPlus-sha2-192f-r3.1";
172 return "SphincsPlus-sha2-256s-r3.1";
174 return "SphincsPlus-sha2-256f-r3.1";
175
177 return "SLH-DSA-SHA2-128s";
179 return "SLH-DSA-SHA2-128f";
181 return "SLH-DSA-SHA2-192s";
183 return "SLH-DSA-SHA2-192f";
185 return "SLH-DSA-SHA2-256s";
187 return "SLH-DSA-SHA2-256f";
188 }
189 }
190
191 if(hash == Sphincs_Hash_Type::Shake256) {
192 switch(set) {
194 return "SphincsPlus-shake-128s-r3.1";
196 return "SphincsPlus-shake-128f-r3.1";
198 return "SphincsPlus-shake-192s-r3.1";
200 return "SphincsPlus-shake-192f-r3.1";
202 return "SphincsPlus-shake-256s-r3.1";
204 return "SphincsPlus-shake-256f-r3.1";
205
207 return "SLH-DSA-SHAKE-128s";
209 return "SLH-DSA-SHAKE-128f";
211 return "SLH-DSA-SHAKE-192s";
213 return "SLH-DSA-SHAKE-192f";
215 return "SLH-DSA-SHAKE-256s";
217 return "SLH-DSA-SHAKE-256f";
218 }
219 }
220
221 if(hash == Sphincs_Hash_Type::Haraka) {
222 switch(set) {
224 return "SphincsPlus-haraka-128s-r3.1";
226 return "SphincsPlus-haraka-128f-r3.1";
228 return "SphincsPlus-haraka-192s-r3.1";
230 return "SphincsPlus-haraka-192f-r3.1";
232 return "SphincsPlus-haraka-256s-r3.1";
234 return "SphincsPlus-haraka-256f-r3.1";
235
242 throw Invalid_Argument("SLH-DSA does not support Haraka");
243 }
244 }
245 throw Invalid_Argument("Cannot serialize invalid parameter combination");
246}
247
248constexpr bool is_slh_dsa_set(Sphincs_Parameter_Set set) {
249 switch(set) {
256 return true;
263 return false;
264 }
266}
267
268} // namespace
269
270Sphincs_Parameters::Sphincs_Parameters(Sphincs_Parameter_Set set,
271 Sphincs_Hash_Type hash_type,
272 uint32_t n,
273 uint32_t h,
274 uint32_t d,
275 uint32_t a,
276 uint32_t k,
277 uint32_t w,
278 uint32_t bitsec) :
279 m_set(set), m_hash_type(hash_type), m_n(n), m_h(h), m_d(d), m_a(a), m_k(k), m_w(w), m_bitsec(bitsec) {
280 BOTAN_ARG_CHECK(!(hash_type == Sphincs_Hash_Type::Haraka && is_slh_dsa_set(set)),
281 "Haraka is not available for SLH-DSA");
282 BOTAN_ARG_CHECK(w == 4 || w == 16 || w == 256, "Winternitz parameter must be one of 4, 16, 256");
283 BOTAN_ARG_CHECK(n == 16 || n == 24 || n == 32, "n must be one of 16, 24, 32");
284 BOTAN_ARG_CHECK(m_d > 0, "d must be greater than zero");
285
286 m_xmss_tree_height = m_h / m_d;
287 m_lg_w = ceil_log2(m_w);
288
289 // base_2^b algorithm (Fips 205, Algorithm 4) only works
290 // when m_log_w is a divisor of 8.
291 BOTAN_ASSERT_NOMSG(m_lg_w <= 8 && 8 % m_lg_w == 0);
292
293 // # Winternitz blocks of the message (len_1 of FIPS 205, Algorithm 1)
294 m_wots_len1 = (m_n * 8) / m_lg_w;
295
296 // # Winternitz blocks of the checksum (output of FIPS 205 Algorithm 1)
297 m_wots_len2 = ceil_log2(m_wots_len1 * (m_w - 1)) / m_lg_w + 1;
298
299 // # Winternitz blocks in the signature (len of FIPS 205, Equation 5.4)
300 m_wots_len = m_wots_len1 + m_wots_len2;
301
302 // byte length of WOTS+ signature as well as public key
303 m_wots_bytes = m_wots_len * m_n;
304
305 // # of bytes the WOTS+ checksum consists of
306 m_wots_checksum_bytes = ceil_tobytes(m_wots_len2 * m_lg_w);
307
308 m_fors_sig_bytes = (m_a + 1) * m_k * m_n;
309
310 // byte length of the FORS input message
311 m_fors_message_bytes = ceil_tobytes(m_k * m_a);
312
313 m_xmss_sig_bytes = m_wots_bytes + m_xmss_tree_height * m_n;
314 m_ht_sig_bytes = m_d * m_xmss_sig_bytes;
315 m_sp_sig_bytes = m_n /* random */ + m_fors_sig_bytes + m_ht_sig_bytes;
316
317 m_tree_digest_bytes = ceil_tobytes(m_h - m_xmss_tree_height);
318 m_leaf_digest_bytes = ceil_tobytes(m_xmss_tree_height);
319 m_h_msg_digest_bytes = m_fors_message_bytes + m_tree_digest_bytes + m_leaf_digest_bytes;
320}
321
323 [[maybe_unused]] const bool is_slh_dsa = is_slh_dsa_set(m_set);
324#ifdef BOTAN_HAS_SLH_DSA_WITH_SHA2
325 if(is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Sha256) {
326 return true;
327 }
328#endif
329#ifdef BOTAN_HAS_SLH_DSA_WITH_SHAKE
330 if(is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Shake256) {
331 return true;
332 }
333#endif
334#ifdef BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2
335 if(!is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Sha256) {
336 return true;
337 }
338#endif
339#ifdef BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE
340 if(!is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Shake256) {
341 return true;
342 }
343#endif
344 return false;
345}
346
347Sphincs_Parameters Sphincs_Parameters::create(
348 Sphincs_Parameter_Set set, Sphincs_Hash_Type hash /*, SlhDsaInputMode input_mode [TODO: prehash mode]*/) {
349 // See FIPS 205, Table 2
350 switch(set) {
353 return Sphincs_Parameters(set, hash, 16, 63, 7, 12, 14, 16, 133);
356 return Sphincs_Parameters(set, hash, 16, 66, 22, 6, 33, 16, 128);
357
360 return Sphincs_Parameters(set, hash, 24, 63, 7, 14, 17, 16, 193);
363 return Sphincs_Parameters(set, hash, 24, 66, 22, 8, 33, 16, 194);
364
367 return Sphincs_Parameters(set, hash, 32, 64, 8, 14, 22, 16, 255);
370 return Sphincs_Parameters(set, hash, 32, 68, 17, 9, 35, 16, 255);
371 }
373}
374
375Sphincs_Parameters Sphincs_Parameters::create(std::string_view name) {
376 auto [param_set, hash_type] = set_and_hash_from_name(name);
377 return Sphincs_Parameters::create(param_set, hash_type);
378}
379
381 return is_slh_dsa_set(m_set);
382}
383
384std::string Sphincs_Parameters::hash_name() const {
385 switch(m_hash_type) {
387 return "SHA-256";
389 return fmt("SHAKE-256({})", 8 * n());
391 return "Haraka";
392 }
394}
395
396std::string Sphincs_Parameters::to_string() const {
397 return name_from_set_and_hash(parameter_set(), hash_type());
398}
399
400Sphincs_Parameters Sphincs_Parameters::create(const OID& oid) {
402}
403
407
411
412} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:163
std::string to_formatted_string() const
Definition asn1_oid.cpp:139
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:86
Sphincs_Parameter_Set parameter_set() const
std::string to_string() const
AlgorithmIdentifier algorithm_identifier() const
std::string hash_name() const
Sphincs_Hash_Type hash_type() const
static Sphincs_Parameters create(Sphincs_Parameter_Set set, Sphincs_Hash_Type hash)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
Sphincs_Parameter_Set
Sphincs_Hash_Type
@ Haraka
Haraka is currently not supported.
constexpr uint8_t ceil_log2(T x)
Definition bit_ops.h:133
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:168