Botan 3.13.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 // The pre-hash variants (HashSLH-DSA) are not currently implemented
119 if(name.starts_with("Hash-SLH-DSA-")) {
120 throw Not_Implemented(fmt("Pre-hash SLH-DSA ({}) is not supported", name));
121 }
122
123 throw Lookup_Error(fmt("No SLH-DSA (or SPHINCS+) parameter supported for: {}", name));
124}
125
126std::string name_from_set_and_hash(Sphincs_Parameter_Set set, Sphincs_Hash_Type hash) {
127 if(hash == Sphincs_Hash_Type::Sha256) {
128 switch(set) {
130 return "SphincsPlus-sha2-128s-r3.1";
132 return "SphincsPlus-sha2-128f-r3.1";
134 return "SphincsPlus-sha2-192s-r3.1";
136 return "SphincsPlus-sha2-192f-r3.1";
138 return "SphincsPlus-sha2-256s-r3.1";
140 return "SphincsPlus-sha2-256f-r3.1";
141
143 return "SLH-DSA-SHA2-128s";
145 return "SLH-DSA-SHA2-128f";
147 return "SLH-DSA-SHA2-192s";
149 return "SLH-DSA-SHA2-192f";
151 return "SLH-DSA-SHA2-256s";
153 return "SLH-DSA-SHA2-256f";
154 }
155 }
156
157 if(hash == Sphincs_Hash_Type::Shake256) {
158 switch(set) {
160 return "SphincsPlus-shake-128s-r3.1";
162 return "SphincsPlus-shake-128f-r3.1";
164 return "SphincsPlus-shake-192s-r3.1";
166 return "SphincsPlus-shake-192f-r3.1";
168 return "SphincsPlus-shake-256s-r3.1";
170 return "SphincsPlus-shake-256f-r3.1";
171
173 return "SLH-DSA-SHAKE-128s";
175 return "SLH-DSA-SHAKE-128f";
177 return "SLH-DSA-SHAKE-192s";
179 return "SLH-DSA-SHAKE-192f";
181 return "SLH-DSA-SHAKE-256s";
183 return "SLH-DSA-SHAKE-256f";
184 }
185 }
186
187 if(hash == Sphincs_Hash_Type::Haraka) {
188 switch(set) {
190 return "SphincsPlus-haraka-128s-r3.1";
192 return "SphincsPlus-haraka-128f-r3.1";
194 return "SphincsPlus-haraka-192s-r3.1";
196 return "SphincsPlus-haraka-192f-r3.1";
198 return "SphincsPlus-haraka-256s-r3.1";
200 return "SphincsPlus-haraka-256f-r3.1";
201
208 throw Invalid_Argument("SLH-DSA does not support Haraka");
209 }
210 }
211 throw Invalid_Argument("Cannot serialize invalid parameter combination");
212}
213
214constexpr bool is_slh_dsa_set(Sphincs_Parameter_Set set) {
215 switch(set) {
222 return true;
229 return false;
230 }
232}
233
234} // namespace
235
236Sphincs_Parameters::Sphincs_Parameters(Sphincs_Parameter_Set set,
237 Sphincs_Hash_Type hash_type,
238 uint32_t n,
239 uint32_t h,
240 uint32_t d,
241 uint32_t a,
242 uint32_t k,
243 uint32_t w,
244 uint32_t bitsec) :
245 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) {
246 BOTAN_ARG_CHECK(!(hash_type == Sphincs_Hash_Type::Haraka && is_slh_dsa_set(set)),
247 "Haraka is not available for SLH-DSA");
248 BOTAN_ARG_CHECK(w == 4 || w == 16 || w == 256, "Winternitz parameter must be one of 4, 16, 256");
249 BOTAN_ARG_CHECK(n == 16 || n == 24 || n == 32, "n must be one of 16, 24, 32");
250 BOTAN_ARG_CHECK(m_d > 0, "d must be greater than zero");
251
252 m_xmss_tree_height = m_h / m_d;
253 m_lg_w = ceil_log2(m_w);
254
255 // base_2^b algorithm (Fips 205, Algorithm 4) only works
256 // when m_log_w is a divisor of 8.
257 BOTAN_ASSERT_NOMSG(m_lg_w <= 8 && 8 % m_lg_w == 0);
258
259 // # Winternitz blocks of the message (len_1 of FIPS 205, Algorithm 1)
260 m_wots_len1 = (m_n * 8) / m_lg_w;
261
262 // # Winternitz blocks of the checksum (output of FIPS 205 Algorithm 1)
263 m_wots_len2 = ceil_log2(m_wots_len1 * (m_w - 1)) / m_lg_w + 1;
264
265 // # Winternitz blocks in the signature (len of FIPS 205, Equation 5.4)
266 m_wots_len = m_wots_len1 + m_wots_len2;
267
268 // byte length of WOTS+ signature as well as public key
269 m_wots_bytes = m_wots_len * m_n;
270
271 // # of bytes the WOTS+ checksum consists of
272 m_wots_checksum_bytes = ceil_tobytes(m_wots_len2 * m_lg_w);
273
274 m_fors_sig_bytes = (m_a + 1) * m_k * m_n;
275
276 // byte length of the FORS input message
277 m_fors_message_bytes = ceil_tobytes(m_k * m_a);
278
279 m_xmss_sig_bytes = m_wots_bytes + m_xmss_tree_height * m_n;
280 m_ht_sig_bytes = m_d * m_xmss_sig_bytes;
281 m_sp_sig_bytes = m_n /* random */ + m_fors_sig_bytes + m_ht_sig_bytes;
282
283 m_tree_digest_bytes = ceil_tobytes(m_h - m_xmss_tree_height);
284 m_leaf_digest_bytes = ceil_tobytes(m_xmss_tree_height);
285 m_h_msg_digest_bytes = m_fors_message_bytes + m_tree_digest_bytes + m_leaf_digest_bytes;
286}
287
289 [[maybe_unused]] const bool is_slh_dsa = is_slh_dsa_set(m_set);
290#ifdef BOTAN_HAS_SLH_DSA_WITH_SHA2
291 if(is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Sha256) {
292 return true;
293 }
294#endif
295#ifdef BOTAN_HAS_SLH_DSA_WITH_SHAKE
296 if(is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Shake256) {
297 return true;
298 }
299#endif
300#ifdef BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2
301 if(!is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Sha256) {
302 return true;
303 }
304#endif
305#ifdef BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE
306 if(!is_slh_dsa && m_hash_type == Sphincs_Hash_Type::Shake256) {
307 return true;
308 }
309#endif
310 return false;
311}
312
313Sphincs_Parameters Sphincs_Parameters::create(
314 Sphincs_Parameter_Set set, Sphincs_Hash_Type hash /*, SlhDsaInputMode input_mode [TODO: prehash mode]*/) {
315 // See FIPS 205, Table 2
316 switch(set) {
319 return Sphincs_Parameters(set, hash, 16, 63, 7, 12, 14, 16, 133);
322 return Sphincs_Parameters(set, hash, 16, 66, 22, 6, 33, 16, 128);
323
326 return Sphincs_Parameters(set, hash, 24, 63, 7, 14, 17, 16, 193);
329 return Sphincs_Parameters(set, hash, 24, 66, 22, 8, 33, 16, 194);
330
333 return Sphincs_Parameters(set, hash, 32, 64, 8, 14, 22, 16, 255);
336 return Sphincs_Parameters(set, hash, 32, 68, 17, 9, 35, 16, 255);
337 }
339}
340
341Sphincs_Parameters Sphincs_Parameters::create(std::string_view name) {
342 auto [param_set, hash_type] = set_and_hash_from_name(name);
343 return Sphincs_Parameters::create(param_set, hash_type);
344}
345
347 return is_slh_dsa_set(m_set);
348}
349
350std::string Sphincs_Parameters::hash_name() const {
351 switch(m_hash_type) {
353 return "SHA-256";
355 return fmt("SHAKE-256({})", 8 * n());
357 return "Haraka";
358 }
360}
361
362std::string Sphincs_Parameters::to_string() const {
363 return name_from_set_and_hash(parameter_set(), hash_type());
364}
365
366Sphincs_Parameters Sphincs_Parameters::create(const OID& oid) {
367 if(const auto name = oid.registered_name()) {
368 return Sphincs_Parameters::create(*name);
369 }
370
371 throw Lookup_Error(fmt("No SLH-DSA (or SPHINCS+) parameter registered for OID {}", oid));
372}
373
377
381
382} // 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:166
std::optional< std::string > registered_name() const
Definition asn1_oid.cpp:149
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
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:140
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:175