Botan 3.14.1
Crypto and TLS for C&
Botan::Sphincs_Hash_Functions_Sha2 Class Reference

#include <sp_hash_sha2.h>

Inheritance diagram for Botan::Sphincs_Hash_Functions_Sha2:
Botan::Sphincs_Hash_Functions

Public Member Functions

std::tuple< SphincsHashedMessage, XmssTreeIndexInLayer, TreeNodeIndex > H_msg (StrongSpan< const SphincsMessageRandomness > r, const SphincsTreeNode &root, const SphincsMessageInternal &message)
std::string msg_hash_function_name () const override
void PRF (StrongSpan< ForsLeafSecret > out, const SphincsSecretSeed &sk_seed, const Sphincs_Address &address)
void PRF (StrongSpan< WotsNode > out, const SphincsSecretSeed &sk_seed, const Sphincs_Address &address)
void PRF_batch (std::span< std::span< uint8_t > > outputs, const SphincsSecretSeed &sk_seed, std::span< const Sphincs_Address > addresses)
void PRF_msg (StrongSpan< SphincsMessageRandomness > out, StrongSpan< const SphincsSecretPRF > sk_prf, StrongSpan< const SphincsOptionalRandomness > opt_rand, const SphincsMessageInternal &msg) override
 Sphincs_Hash_Functions_Sha2 (const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
template<typename OutT = std::vector<uint8_t>, typename... BufferTs>
OutT T (const Sphincs_Address &address, const BufferTs &... in)
template<typename... BufferTs>
void T (std::span< uint8_t > out, const Sphincs_Address &address, const BufferTs &... in)
void T_batch (std::span< std::span< uint8_t > > outputs, std::span< const Sphincs_Address > addresses, std::span< std::span< const uint8_t > > inputs)

Static Public Member Functions

static std::unique_ptr< Sphincs_Hash_Functions > create (const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)

Protected Attributes

const SphincsPublicSeed & m_pub_seed

Detailed Description

Implementation of SLH-DSA hash function abstraction for SHA2

Definition at line 28 of file sp_hash_sha2.h.

Constructor & Destructor Documentation

◆ Sphincs_Hash_Functions_Sha2()

Botan::Sphincs_Hash_Functions_Sha2::Sphincs_Hash_Functions_Sha2 ( const Sphincs_Parameters & sphincs_params,
const SphincsPublicSeed & pub_seed )
inline

Definition at line 78 of file sp_hash_sha2.h.

78 :
79 Sphincs_Hash_Functions(sphincs_params, pub_seed), m_sphincs_params(sphincs_params) {
80 m_padded_pub_seed_256 = std::vector<uint8_t>(64, '\0');
81 BOTAN_ASSERT_NOMSG(pub_seed.size() <= m_padded_pub_seed_256.size());
82 std::copy(pub_seed.begin(), pub_seed.end(), m_padded_pub_seed_256.begin());
83
84 if(sphincs_params.n() == 16) {
85 m_sha_x = std::make_unique<Truncated_Hash>(std::make_unique<SHA_256>(), sphincs_params.n() * 8);
86 m_sha_x_full = std::make_unique<SHA_256>();
87 m_padded_pub_seed_x = m_padded_pub_seed_256;
88 } else {
89 BOTAN_ASSERT_NOMSG(sphincs_params.n() <= 128);
90 m_sha_x = std::make_unique<Truncated_Hash>(std::make_unique<SHA_512>(), sphincs_params.n() * 8);
91 m_sha_x_full = std::make_unique<SHA_512>();
92
93 m_padded_pub_seed_x = std::vector<uint8_t>(128, '\0');
94 BOTAN_ASSERT_NOMSG(pub_seed.size() <= m_padded_pub_seed_x.size());
95 std::copy(pub_seed.begin(), pub_seed.end(), m_padded_pub_seed_x.begin());
96 }
97
98 if(m_sphincs_params.n() < 32) {
99 m_sha_256 = std::make_unique<Truncated_Hash>(std::make_unique<SHA_256>(), m_sphincs_params.n() * 8);
100 } else {
101 m_sha_256 = std::make_unique<SHA_256>();
102 }
103
104 // Batch engines for the same tweaked hashes. The padded pub seed
105 // prefix is exactly one hash block, so the engines can precompute
106 // its midstate once.
107 const size_t n_bits = m_sphincs_params.n() * 8;
108
109 const std::string hash_256 = (m_sphincs_params.n() < 32) ? fmt("Truncated(SHA-256,{})", n_bits) : "SHA-256";
110 m_engine_256 = Hash_Engine::create_or_throw(hash_256, m_padded_pub_seed_256);
111
112 const std::string hash_x =
113 (m_sphincs_params.n() == 16) ? fmt("Truncated(SHA-256,{})", n_bits) : fmt("Truncated(SHA-512,{})", n_bits);
114 m_engine_x = Hash_Engine::create_or_throw(hash_x, m_padded_pub_seed_x);
115 }
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static std::unique_ptr< Hash_Engine > create_or_throw(std::string_view hash_fn, std::span< const uint8_t > common_prefix={}, std::string_view provider="")
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
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::detail::Container_Strong_Adapter_Base< T >::begin(), BOTAN_ASSERT_NOMSG, Botan::Hash_Engine::create_or_throw(), Botan::detail::Container_Strong_Adapter_Base< T >::end(), Botan::fmt(), Botan::Sphincs_Parameters::n(), Botan::detail::Container_Strong_Adapter_Base< T >::size(), and Botan::Sphincs_Hash_Functions::Sphincs_Hash_Functions().

Member Function Documentation

◆ create()

std::unique_ptr< Sphincs_Hash_Functions > Botan::Sphincs_Hash_Functions::create ( const Sphincs_Parameters & sphincs_params,
const SphincsPublicSeed & pub_seed )
staticinherited

Creates a Sphincs_Hash_Functions object instantiating the hash functions used for the specified sphincs_params. The pub_seed is used to seed the hash functions (possibly padded). This is pre-computed and the respective state is copied on the further calls on H(seed) with tweak_hash, i.e., T and PRF.

Definition at line 63 of file sp_hash.cpp.

64 {
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}
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173
@ Haraka
Haraka is currently not supported.

References BOTAN_ASSERT_UNREACHABLE, Botan::Haraka, Botan::Sphincs_Parameters::hash_type(), Botan::Sha256, and Botan::Shake256.

Referenced by Botan::SphincsPlus_PrivateKey::check_key(), Botan::SphincsPlus_PrivateKey::SphincsPlus_PrivateKey(), and ~Sphincs_Hash_Functions().

◆ H_msg()

std::tuple< SphincsHashedMessage, XmssTreeIndexInLayer, TreeNodeIndex > Botan::Sphincs_Hash_Functions::H_msg ( StrongSpan< const SphincsMessageRandomness > r,
const SphincsTreeNode & root,
const SphincsMessageInternal & message )
inherited

Definition at line 107 of file sp_hash.cpp.

108 {
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());
118 BOTAN_ASSERT_NOMSG(s.empty());
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}
virtual std::vector< uint8_t > H_msg_digest(StrongSpan< const SphincsMessageRandomness > r, const SphincsTreeNode &root, const SphincsMessageInternal &message)=0
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
Strong< std::vector< uint8_t >, struct SphincsHashedMessage_ > SphincsHashedMessage
Definition sp_types.h:59

References BOTAN_ASSERT_NOMSG, Botan::BufferSlicer::copy(), Botan::BufferSlicer::empty(), H_msg_digest(), m_sphincs_params, Botan::root(), and Botan::BufferSlicer::take().

Referenced by ~Sphincs_Hash_Functions().

◆ msg_hash_function_name()

std::string Botan::Sphincs_Hash_Functions_Sha2::msg_hash_function_name ( ) const
inlineoverridevirtual

Implements Botan::Sphincs_Hash_Functions.

Definition at line 131 of file sp_hash_sha2.h.

131{ return m_sha_x_full->name(); }

◆ PRF() [1/2]

void Botan::Sphincs_Hash_Functions::PRF ( StrongSpan< ForsLeafSecret > out,
const SphincsSecretSeed & sk_seed,
const Sphincs_Address & address )
inlineinherited

Definition at line 72 of file sp_hash.h.

72 {
73 T(out, address, sk_seed);
74 }
void T(std::span< uint8_t > out, const Sphincs_Address &address, const BufferTs &... in)
Definition sp_hash.h:59

References T().

Referenced by Botan::fors_sign_and_pkgen().

◆ PRF() [2/2]

void Botan::Sphincs_Hash_Functions::PRF ( StrongSpan< WotsNode > out,
const SphincsSecretSeed & sk_seed,
const Sphincs_Address & address )
inlineinherited

Definition at line 76 of file sp_hash.h.

76 {
77 T(out, address, sk_seed);
78 }

References T().

◆ PRF_batch()

void Botan::Sphincs_Hash_Functions::PRF_batch ( std::span< std::span< uint8_t > > outputs,
const SphincsSecretSeed & sk_seed,
std::span< const Sphincs_Address > addresses )
inherited

Batched variant of PRF: outputs[i] = PRF(sk_seed, addresses[i])

Definition at line 56 of file sp_hash.cpp.

58 {
59 m_prf_inputs.assign(addresses.size(), std::span<const uint8_t>(sk_seed.get()));
60 T_batch(outputs, addresses, m_prf_inputs);
61}
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

References Botan::detail::Strong_Base< T >::get(), and T_batch().

Referenced by Botan::fors_sign_and_pkgen(), and Botan::wots_sign_and_pkgen().

◆ PRF_msg()

void Botan::Sphincs_Hash_Functions_Sha2::PRF_msg ( StrongSpan< SphincsMessageRandomness > out,
StrongSpan< const SphincsSecretPRF > sk_prf,
StrongSpan< const SphincsOptionalRandomness > opt_rand,
const SphincsMessageInternal & msg )
inlineoverridevirtual

Using SK.PRF, the optional randomness, and a message, computes the message random R, and the tree and leaf indices.

Parameters
outoutput location for the message hash
sk_prfSK.PRF
opt_randoptional randomness
msgmessage

Implements Botan::Sphincs_Hash_Functions.

Definition at line 117 of file sp_hash_sha2.h.

120 {
121 HMAC hmac_sha_x(m_sha_x_full->new_object());
122 hmac_sha_x.set_key(sk_prf);
123 hmac_sha_x.update(opt_rand);
124 hmac_sha_x.update(msg.prefix);
125 hmac_sha_x.update(msg.message);
126
127 const auto prf = hmac_sha_x.final();
128 std::copy(prf.begin(), prf.begin() + out.size(), out.begin());
129 }

References Botan::StrongSpan< T >::begin(), Botan::Buffered_Computation::final(), Botan::SphincsMessageInternal::message, Botan::SphincsMessageInternal::prefix, Botan::SymmetricAlgorithm::set_key(), Botan::StrongSpan< T >::size(), and Botan::Buffered_Computation::update().

◆ T() [1/2]

template<typename OutT = std::vector<uint8_t>, typename... BufferTs>
OutT Botan::Sphincs_Hash_Functions::T ( const Sphincs_Address & address,
const BufferTs &... in )
inlineinherited

Definition at line 66 of file sp_hash.h.

66 {
67 OutT t(m_sphincs_params.n());
68 T(t, address, in...);
69 return t;
70 }

References m_sphincs_params, and T().

◆ T() [2/2]

template<typename... BufferTs>
void Botan::Sphincs_Hash_Functions::T ( std::span< uint8_t > out,
const Sphincs_Address & address,
const BufferTs &... in )
inlineinherited

Definition at line 59 of file sp_hash.h.

59 {
60 auto& hash = tweak_hash(address, (in.size() + ...));
61 (hash.update(in), ...);
62 hash.final(out);
63 }
virtual HashFunction & tweak_hash(const Sphincs_Address &address, size_t input_length)=0

References tweak_hash().

Referenced by Botan::compute_root(), Botan::fors_public_key_from_signature(), Botan::fors_sign_and_pkgen(), Botan::ht_verify(), PRF(), PRF(), T(), and Botan::treehash().

◆ T_batch()

void Botan::Sphincs_Hash_Functions::T_batch ( std::span< std::span< uint8_t > > outputs,
std::span< const Sphincs_Address > addresses,
std::span< std::span< const uint8_t > > inputs )
inherited

Batched variant of T with a single input buffer per lane

Computes outputs[i] = T(addresses[i], inputs[i]). All inputs must have identical length. outputs[i] may alias inputs[i], as used for stepping WOTS+ chains in place.

Definition at line 32 of file sp_hash.cpp.

34 {
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}
virtual size_t address_encoding_len() const =0
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

References address_encoding_len(), BOTAN_ASSERT_NOMSG, encode_address(), and tweak_hash_engine().

Referenced by Botan::fors_sign_and_pkgen(), PRF_batch(), Botan::wots_public_key_from_signature(), and Botan::wots_sign_and_pkgen().

Member Data Documentation

◆ m_pub_seed

const SphincsPublicSeed& Botan::Sphincs_Hash_Functions::m_pub_seed
protectedinherited

Definition at line 138 of file sp_hash.h.

Referenced by Sphincs_Hash_Functions().


The documentation for this class was generated from the following file: