Botan 3.14.1
Crypto and TLS for C&
sp_hash.cpp
Go to the documentation of this file.
1/*
2 * SLH-DSA Hash Function Interface
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/internal/sp_hash.h>
10
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <botan/sp_parameters.h>
14#include <botan/internal/buffer_slicer.h>
15#include <botan/internal/hash_engine.h>
16#include <memory>
17
18#if defined(BOTAN_HAS_SPHINCS_PLUS_SHAKE_BASE)
19 #include <botan/internal/sp_hash_shake.h>
20#endif
21
22#if defined(BOTAN_HAS_SPHINCS_PLUS_SHA2_BASE)
23 #include <botan/internal/sp_hash_sha2.h>
24#endif
25
26namespace Botan {
27
29 const SphincsPublicSeed& pub_seed) :
30 m_sphincs_params(sphincs_params), m_pub_seed(pub_seed) {}
31
32void Sphincs_Hash_Functions::T_batch(std::span<std::span<uint8_t>> outputs,
33 std::span<const Sphincs_Address> addresses,
34 std::span<std::span<const uint8_t>> inputs) {
35 BOTAN_ASSERT_NOMSG(outputs.size() == addresses.size() && addresses.size() == inputs.size());
36
37 if(addresses.empty()) {
38 return;
39 }
40
41 auto& engine = tweak_hash_engine(inputs[0].size());
42
43 const size_t adrs_len = address_encoding_len();
44 m_adrs_buf.resize(addresses.size() * adrs_len);
45 m_adrs_spans.resize(addresses.size());
46
47 for(size_t i = 0; i != addresses.size(); ++i) {
48 const auto adrs = std::span(m_adrs_buf).subspan(i * adrs_len, adrs_len);
49 encode_address(adrs, addresses[i]);
50 m_adrs_spans[i] = adrs;
51 }
52
53 engine.batch_hash(outputs, m_adrs_spans, inputs);
54}
55
56void Sphincs_Hash_Functions::PRF_batch(std::span<std::span<uint8_t>> outputs,
57 const SphincsSecretSeed& sk_seed,
58 std::span<const Sphincs_Address> addresses) {
59 m_prf_inputs.assign(addresses.size(), std::span<const uint8_t>(sk_seed.get()));
60 T_batch(outputs, addresses, m_prf_inputs);
61}
62
63std::unique_ptr<Sphincs_Hash_Functions> Sphincs_Hash_Functions::create(const Sphincs_Parameters& sphincs_params,
64 const SphincsPublicSeed& pub_seed) {
65 switch(sphincs_params.hash_type()) {
67#if defined(BOTAN_HAS_SPHINCS_PLUS_SHA2_BASE)
68 return std::make_unique<Sphincs_Hash_Functions_Sha2>(sphincs_params, pub_seed);
69#else
70 throw Not_Implemented("SLH-DSA (or SPHINCS+) with SHA-256 is not available in this build");
71#endif
72
74#if defined(BOTAN_HAS_SPHINCS_PLUS_SHAKE_BASE)
75 return std::make_unique<Sphincs_Hash_Functions_Shake>(sphincs_params, pub_seed);
76#else
77 throw Not_Implemented("SLH-DSA (or SPHINCS+) with SHAKE is not available in this build");
78#endif
79
81 throw Not_Implemented("Haraka is not implemented");
82 }
84}
85
86namespace {
87
88template <typename T>
89T from_first_n_bits(const uint32_t nbits, std::span<const uint8_t> bytes) {
90 using wrapped_type = typename T::wrapped_type;
91
92 constexpr const auto outsize = sizeof(wrapped_type);
93 BOTAN_ASSERT_NOMSG(nbits <= bytes.size() * 8);
94 BOTAN_ASSERT_NOMSG(bytes.size() <= outsize);
95
96 // The input buffer might shorter than the byte-length of the desired
97 // integer type. This prepends \0-bytes accordingly.
98 std::array<uint8_t, outsize> normalized_bytes = {};
99 std::copy(bytes.rbegin(), bytes.rend(), normalized_bytes.rbegin());
100 const auto bits = load_be<wrapped_type>(normalized_bytes.data(), 0);
101
102 return T(bits & (~wrapped_type(0) >> (8 * outsize - nbits)));
103}
104
105} // namespace
106
107std::tuple<SphincsHashedMessage, XmssTreeIndexInLayer, TreeNodeIndex> Sphincs_Hash_Functions::H_msg(
109 const auto digest = H_msg_digest(r, root, message);
110
111 // The following calculates the message digest and indices from the
112 // raw message digest. See FIPS 205, Algorithm 19, Line 5-10.
113 const auto& p = m_sphincs_params;
114 BufferSlicer s(digest);
115 auto msg_hash = s.copy<SphincsHashedMessage>(p.fors_message_bytes());
116 const auto tree_index_bytes = s.take(p.tree_digest_bytes());
117 const auto leaf_index_bytes = s.take(p.leaf_digest_bytes());
119
120 const auto tree_index = from_first_n_bits<XmssTreeIndexInLayer>(p.h() - p.xmss_tree_height(), tree_index_bytes);
121 const auto leaf_index = from_first_n_bits<TreeNodeIndex>(p.xmss_tree_height(), leaf_index_bytes);
122 return {std::move(msg_hash), tree_index, leaf_index};
123}
124
125} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173
auto copy(const size_t count)
std::span< const uint8_t > take(const size_t count)
const SphincsPublicSeed & m_pub_seed
Definition sp_hash.h:138
const Sphincs_Parameters & m_sphincs_params
Definition sp_hash.h:137
Sphincs_Hash_Functions(const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
Definition sp_hash.cpp:28
virtual size_t address_encoding_len() const =0
void T_batch(std::span< std::span< uint8_t > > outputs, std::span< const Sphincs_Address > addresses, std::span< std::span< const uint8_t > > inputs)
Definition sp_hash.cpp:32
virtual void encode_address(std::span< uint8_t > out, const Sphincs_Address &address) const =0
virtual Hash_Engine & tweak_hash_engine(size_t input_length)=0
std::tuple< SphincsHashedMessage, XmssTreeIndexInLayer, TreeNodeIndex > H_msg(StrongSpan< const SphincsMessageRandomness > r, const SphincsTreeNode &root, const SphincsMessageInternal &message)
Definition sp_hash.cpp:107
void PRF_batch(std::span< std::span< uint8_t > > outputs, const SphincsSecretSeed &sk_seed, std::span< const Sphincs_Address > addresses)
Definition sp_hash.cpp:56
static std::unique_ptr< Sphincs_Hash_Functions > create(const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
Definition sp_hash.cpp:63
virtual std::vector< uint8_t > H_msg_digest(StrongSpan< const SphincsMessageRandomness > r, const SphincsTreeNode &root, const SphincsMessageInternal &message)=0
Sphincs_Hash_Type hash_type() const
constexpr T & get() &
Definition strong_type.h:81
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
Strong< std::vector< uint8_t >, struct SphincsTreeNode_ > SphincsTreeNode
Either an XMSS or FORS tree node or leaf.
Definition sp_types.h:70
@ Haraka
Haraka is currently not supported.
Strong< std::vector< uint8_t >, struct SphincsPublicSeed_ > SphincsPublicSeed
Definition sp_types.h:60
Strong< secure_vector< uint8_t >, struct SphincsSecretSeed_ > SphincsSecretSeed
Definition sp_types.h:61
Strong< std::vector< uint8_t >, struct SphincsHashedMessage_ > SphincsHashedMessage
Definition sp_types.h:59
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504
M' representation of FIPS 205 (the input to slh_sign_internal and slh_verify_internal).
Definition sp_types.h:52