Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
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, TreeNodeIndexH_msg (StrongSpan< const SphincsMessageRandomness > r, const SphincsTreeNode &root, std::span< const uint8_t > 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_msg (StrongSpan< SphincsMessageRandomness > out, const SphincsSecretPRF &sk_prf, const SphincsOptionalRandomness &opt_rand, std::span< const uint8_t > in) 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, BufferTs &&... in)
 
template<typename... BufferTs>
void T (std::span< uint8_t > out, const Sphincs_Address &address, BufferTs &&... in)
 

Static Public Member Functions

static std::unique_ptr< Sphincs_Hash_Functionscreate (const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
 

Protected Attributes

const SphincsPublicSeedm_pub_seed
 

Detailed Description

Implementation of SPHINCS+ hash function abstraction for SHA2

Definition at line 26 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 63 of file sp_hash_sha2.h.

63 :
64 Sphincs_Hash_Functions(sphincs_params, pub_seed), m_sphincs_params(sphincs_params) {
65 m_padded_pub_seed_256 = std::vector<uint8_t>(64, '\0');
66 BOTAN_ASSERT_NOMSG(pub_seed.size() <= m_padded_pub_seed_256.size());
67 std::copy(pub_seed.begin(), pub_seed.end(), m_padded_pub_seed_256.begin());
68
69 if(sphincs_params.n() == 16) {
70 m_sha_x = std::make_unique<Truncated_Hash>(std::make_unique<SHA_256>(), sphincs_params.n() * 8);
71 m_sha_x_full = std::make_unique<SHA_256>();
72 m_padded_pub_seed_x = m_padded_pub_seed_256;
73 } else {
74 BOTAN_ASSERT_NOMSG(sphincs_params.n() <= 128);
75 m_sha_x = std::make_unique<Truncated_Hash>(std::make_unique<SHA_512>(), sphincs_params.n() * 8);
76 m_sha_x_full = std::make_unique<SHA_512>();
77
78 m_padded_pub_seed_x = std::vector<uint8_t>(128, '\0');
79 BOTAN_ASSERT_NOMSG(pub_seed.size() <= m_padded_pub_seed_x.size());
80 std::copy(pub_seed.begin(), pub_seed.end(), m_padded_pub_seed_x.begin());
81 }
82
83 if(m_sphincs_params.n() < 32) {
84 m_sha_256 = std::make_unique<Truncated_Hash>(std::make_unique<SHA_256>(), m_sphincs_params.n() * 8);
85 } else {
86 m_sha_256 = std::make_unique<SHA_256>();
87 }
88 }
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
Sphincs_Hash_Functions(const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
Definition sp_hash.cpp:30

References BOTAN_ASSERT_NOMSG, and Botan::Sphincs_Parameters::n().

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 34 of file sp_hash.cpp.

35 {
36 switch(sphincs_params.hash_type()) {
38#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2)
39 return std::make_unique<Sphincs_Hash_Functions_Sha2>(sphincs_params, pub_seed);
40#else
41 throw Not_Implemented("SPHINCS+ with SHA-256 is not available in this build");
42#endif
43
45#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
46 return std::make_unique<Sphincs_Hash_Functions_Shake>(sphincs_params, pub_seed);
47#else
48 throw Not_Implemented("SPHINCS+ with SHAKE is not available in this build");
49#endif
50
52 throw Not_Implemented("Haraka is not yet implemented");
53 }
55}
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:137
@ 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::SphincsPlus_PrivateKey().

◆ H_msg()

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

Definition at line 78 of file sp_hash.cpp.

79 {
80 const auto digest = H_msg_digest(r, root, message);
81
82 // The following calculates the message digest and indices from the
83 // raw message digest. See Algorithm 20 (spx_sign) in SPHINCS+ 3.1
84 const auto& p = m_sphincs_params;
85 BufferSlicer s(digest);
86 auto msg_hash = s.copy<SphincsHashedMessage>(p.fors_message_bytes());
87 auto tree_index_bytes = s.take(p.tree_digest_bytes());
88 auto leaf_index_bytes = s.take(p.leaf_digest_bytes());
89 BOTAN_ASSERT_NOMSG(s.empty());
90
91 auto tree_index = from_first_n_bits<XmssTreeIndexInLayer>(p.h() - p.xmss_tree_height(), tree_index_bytes);
92 auto leaf_index = from_first_n_bits<TreeNodeIndex>(p.xmss_tree_height(), leaf_index_bytes);
93 return {std::move(msg_hash), tree_index, leaf_index};
94}
virtual std::vector< uint8_t > H_msg_digest(StrongSpan< const SphincsMessageRandomness > r, const SphincsTreeNode &root, std::span< const uint8_t > message)=0
const Sphincs_Parameters & m_sphincs_params
Definition sp_hash.h:101
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:45

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

◆ 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 103 of file sp_hash_sha2.h.

103{ 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 69 of file sp_hash.h.

69 {
70 T(out, address, sk_seed);
71 }
FE_25519 T
Definition ge.cpp:34

References T.

Referenced by Botan::fors_sign_and_pkgen(), and Botan::wots_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 73 of file sp_hash.h.

73 {
74 T(out, address, sk_seed);
75 }

References T.

◆ PRF_msg()

void Botan::Sphincs_Hash_Functions_Sha2::PRF_msg ( StrongSpan< SphincsMessageRandomness > out,
const SphincsSecretPRF & sk_prf,
const SphincsOptionalRandomness & opt_rand,
std::span< const uint8_t > 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 90 of file sp_hash_sha2.h.

93 {
94 HMAC hmac_sha_x(m_sha_x_full->new_object());
95 hmac_sha_x.set_key(sk_prf);
96 hmac_sha_x.update(opt_rand);
97 hmac_sha_x.update(in);
98
99 const auto prf = hmac_sha_x.final();
100 std::copy(prf.begin(), prf.begin() + out.size(), out.begin());
101 }

References Botan::StrongSpan< T >::begin(), Botan::Buffered_Computation::final(), 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,
BufferTs &&... in )
inlineinherited

Definition at line 63 of file sp_hash.h.

63 {
64 OutT t(m_sphincs_params.n());
65 T(t, address, std::forward<BufferTs>(in)...);
66 return t;
67 }

References T.

◆ T() [2/2]

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

Definition at line 56 of file sp_hash.h.

56 {
57 auto& hash = tweak_hash(address, (std::forward<BufferTs>(in).size() + ...));
58 (hash.update(std::forward<BufferTs>(in)), ...);
59 hash.final(out);
60 }
virtual HashFunction & tweak_hash(const Sphincs_Address &address, size_t input_length)=0

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

Member Data Documentation

◆ m_pub_seed

const SphincsPublicSeed& Botan::Sphincs_Hash_Functions::m_pub_seed
protectedinherited

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