9#ifndef BOTAN_SP_HASH_SHA2_H_
10#define BOTAN_SP_HASH_SHA2_H_
12#include <botan/internal/sp_hash.h>
14#include <botan/internal/hmac.h>
15#include <botan/internal/mgf1.h>
16#include <botan/internal/sha2_32.h>
17#include <botan/internal/sha2_64.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/trunc_hash.h>
37 auto& hash = (input_length > m_sphincs_params.n()) ? *m_sha_x : *m_sha_256;
38 const auto& padded_pub_seed =
39 (input_length > m_sphincs_params.n()) ? m_padded_pub_seed_x : m_padded_pub_seed_256;
40 hash.
update(padded_pub_seed);
48 m_sha_x_full->update(r);
50 m_sha_x_full->update(
root);
51 m_sha_x_full->update(message.
prefix);
52 m_sha_x_full->update(message.
message);
54 auto r_pk_buffer = m_sha_x_full->final();
57 std::vector<uint8_t> digest(m_sphincs_params.h_msg_digest_bytes());
58 mgf1_mask(*m_sha_x_full, mgf1_input.data(), mgf1_input.size(), digest.data(), digest.size());
66 m_padded_pub_seed_256 = std::vector<uint8_t>(64,
'\0');
68 std::copy(pub_seed.
begin(), pub_seed.
end(), m_padded_pub_seed_256.begin());
70 if(sphincs_params.
n() == 16) {
71 m_sha_x = std::make_unique<Truncated_Hash>(std::make_unique<SHA_256>(), sphincs_params.
n() * 8);
72 m_sha_x_full = std::make_unique<SHA_256>();
73 m_padded_pub_seed_x = m_padded_pub_seed_256;
76 m_sha_x = std::make_unique<Truncated_Hash>(std::make_unique<SHA_512>(), sphincs_params.
n() * 8);
77 m_sha_x_full = std::make_unique<SHA_512>();
79 m_padded_pub_seed_x = std::vector<uint8_t>(128,
'\0');
81 std::copy(pub_seed.
begin(), pub_seed.
end(), m_padded_pub_seed_x.begin());
84 if(m_sphincs_params.n() < 32) {
85 m_sha_256 = std::make_unique<Truncated_Hash>(std::make_unique<SHA_256>(), m_sphincs_params.n() * 8);
87 m_sha_256 = std::make_unique<SHA_256>();
95 HMAC hmac_sha_x(m_sha_x_full->new_object());
97 hmac_sha_x.
update(opt_rand);
101 const auto prf = hmac_sha_x.
final();
102 std::copy(prf.begin(), prf.begin() + out.
size(), out.
begin());
109 std::unique_ptr<HashFunction> m_sha_256;
110 std::unique_ptr<HashFunction> m_sha_x;
112 std::unique_ptr<HashFunction> m_sha_x_full;
114 std::vector<uint8_t> m_padded_pub_seed_256;
115 std::vector<uint8_t> m_padded_pub_seed_x;
#define BOTAN_ASSERT_NOMSG(expr)
void update(const uint8_t in[], size_t length)
void final(uint8_t out[])
std::array< uint8_t, 22 > to_bytes_compressed() const
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)
std::string msg_hash_function_name() const override
const SphincsPublicSeed & m_pub_seed
const Sphincs_Parameters & m_sphincs_params
Sphincs_Hash_Functions(const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
decltype(auto) begin() noexcept(noexcept(this->m_span.begin()))
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
void set_key(const OctetString &key)
decltype(auto) begin() noexcept(noexcept(this->get().begin()))
size_type size() const noexcept(noexcept(this->get().size()))
decltype(auto) end() noexcept(noexcept(this->get().end()))
void mgf1_mask(HashFunction &hash, const uint8_t in[], size_t in_len, uint8_t out[], size_t out_len)
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.
Strong< std::vector< uint8_t >, struct SphincsPublicSeed_ > SphincsPublicSeed
constexpr auto concat(Rs &&... ranges)
M' representation of FIPS 205 (the input to slh_sign_internal and slh_verify_internal)
SphincsInputMessage message
SphincsMessagePrefix prefix