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

#include <sp_hash_shake.h>

Inheritance diagram for Botan::Sphincs_Hash_Functions_Shake:
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_Shake (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
const Sphincs_Parameters & m_sphincs_params

Detailed Description

Implementation of SLH-DSA hash function abstraction for SHAKE256

Definition at line 23 of file sp_hash_shake.h.

Constructor & Destructor Documentation

◆ Sphincs_Hash_Functions_Shake()

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

Definition at line 57 of file sp_hash_shake.h.

57 :
58 Sphincs_Hash_Functions(sphincs_params, pub_seed),
59 m_hash(sphincs_params.n() * 8),
60 m_h_msg_hash(8 * sphincs_params.h_msg_digest_bytes()),
61 m_engine(Hash_Engine::create_or_throw(fmt("SHAKE-256({})", sphincs_params.n() * 8),
62 std::span<const uint8_t>(pub_seed.get()))) {}
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="")
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::fmt(), 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}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
const Sphincs_Parameters & m_sphincs_params
Definition sp_hash.h:137
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_Shake::msg_hash_function_name ( ) const
inlineoverridevirtual

Implements Botan::Sphincs_Hash_Functions.

Definition at line 75 of file sp_hash_shake.h.

75{ return m_h_msg_hash.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_Shake::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 64 of file sp_hash_shake.h.

67 {
68 m_hash.update(sk_prf);
69 m_hash.update(opt_rand);
70 m_hash.update(msg.prefix);
71 m_hash.update(msg.message);
72 m_hash.final(out);
73 }

References Botan::SphincsMessageInternal::message, and Botan::SphincsMessageInternal::prefix.

◆ 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().

◆ m_sphincs_params

const Sphincs_Parameters& Botan::Sphincs_Hash_Functions::m_sphincs_params
protectedinherited

Definition at line 137 of file sp_hash.h.

Referenced by H_msg(), Sphincs_Hash_Functions(), and T().


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