Botan 3.14.1
Crypto and TLS for C&
sp_hash_sha2.h
Go to the documentation of this file.
1/*
2 * SLH-DSA Hash Implementation for SHA-256
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#ifndef BOTAN_SP_HASH_SHA2_H_
10#define BOTAN_SP_HASH_SHA2_H_
11
12#include <botan/internal/sp_hash.h>
13
14#include <botan/internal/concat_util.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/hash_engine.h>
17#include <botan/internal/hmac.h>
18#include <botan/internal/mgf1.h>
19#include <botan/internal/sha2_32.h>
20#include <botan/internal/sha2_64.h>
21#include <botan/internal/trunc_hash.h>
22
23namespace Botan {
24
25/**
26 * Implementation of SLH-DSA hash function abstraction for SHA2
27 */
29 private:
30 HashFunction& tweak_hash(const Sphincs_Address& address, size_t input_length) override {
31 // Depending on the input length we decide to use SHA-256 (Function F
32 // in the spec) or SHA-X (Function T_l and H in the spec).
33 //
34 // All hashed values share a common prefix. Hence we could reuse the
35 // hash state after this prefix was consumed as an optimization.
36 //
37 // TODO: Currently, the `copy_state()` method would force us to perform
38 // a re-allocation whenever we want to do that.
39 auto& hash = (input_length > m_sphincs_params.n()) ? *m_sha_x : *m_sha_256;
40 const auto& padded_pub_seed =
41 (input_length > m_sphincs_params.n()) ? m_padded_pub_seed_x : m_padded_pub_seed_256;
42 hash.update(padded_pub_seed);
43 hash.update(address.to_bytes_compressed());
44 return hash;
45 }
46
47 Hash_Engine& tweak_hash_engine(size_t input_length) override {
48 auto& engine = (input_length > m_sphincs_params.n()) ? m_engine_x : m_engine_256;
49 return *engine;
50 }
51
52 size_t address_encoding_len() const override { return 22; }
53
54 void encode_address(std::span<uint8_t> out, const Sphincs_Address& address) const override {
55 const auto adrs = address.to_bytes_compressed();
56 copy_mem(out.data(), adrs.data(), adrs.size());
57 }
58
59 std::vector<uint8_t> H_msg_digest(StrongSpan<const SphincsMessageRandomness> r,
60 const SphincsTreeNode& root,
61 const SphincsMessageInternal& message) override {
62 m_sha_x_full->update(r);
63 m_sha_x_full->update(m_pub_seed);
64 m_sha_x_full->update(root);
65 m_sha_x_full->update(message.prefix);
66 m_sha_x_full->update(message.message);
67
68 auto r_pk_buffer = m_sha_x_full->final();
69 std::vector<uint8_t> mgf1_input = concat<std::vector<uint8_t>>(r, m_pub_seed, r_pk_buffer);
70
71 std::vector<uint8_t> digest(m_sphincs_params.h_msg_digest_bytes());
72 mgf1_mask(*m_sha_x_full, mgf1_input, digest);
73
74 return digest;
75 }
76
77 public:
78 Sphincs_Hash_Functions_Sha2(const Sphincs_Parameters& sphincs_params, const SphincsPublicSeed& pub_seed) :
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 }
116
120 const SphincsMessageInternal& msg) override {
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 }
130
131 std::string msg_hash_function_name() const override { return m_sha_x_full->name(); }
132
133 private:
135 std::unique_ptr<HashFunction> m_sha_256;
136 std::unique_ptr<HashFunction> m_sha_x;
137 /// Non truncated SHA-X hash
138 std::unique_ptr<HashFunction> m_sha_x_full;
139
140 std::unique_ptr<Hash_Engine> m_engine_256;
141 std::unique_ptr<Hash_Engine> m_engine_x;
142
143 std::vector<uint8_t> m_padded_pub_seed_256;
144 std::vector<uint8_t> m_padded_pub_seed_x;
145};
146
147} // namespace Botan
148
149#endif
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:35
void final(uint8_t out[])
Definition buf_comp.h:97
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="")
std::array< uint8_t, 22 > to_bytes_compressed() const
Definition sp_address.h:144
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
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
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)
Definition sym_algo.cpp:14
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()))
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
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void mgf1_mask(HashFunction &hash, std::span< const uint8_t > input, std::span< uint8_t > output)
Definition mgf1.cpp:15
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
Strong< std::vector< uint8_t >, struct SphincsPublicSeed_ > SphincsPublicSeed
Definition sp_types.h:60
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95
M' representation of FIPS 205 (the input to slh_sign_internal and slh_verify_internal).
Definition sp_types.h:52
SphincsInputMessage message
Definition sp_types.h:54
SphincsMessagePrefix prefix
Definition sp_types.h:53