Botan 3.14.1
Crypto and TLS for C&
sp_hash_shake.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_SHAKE_H_
10#define BOTAN_SP_HASH_SHAKE_H_
11
12#include <botan/internal/sp_hash.h>
13
14#include <botan/internal/fmt.h>
15#include <botan/internal/hash_engine.h>
16#include <botan/internal/shake.h>
17
18namespace Botan {
19
20/**
21 * Implementation of SLH-DSA hash function abstraction for SHAKE256
22 */
24 private:
25 HashFunction& tweak_hash(const Sphincs_Address& address, size_t input_length) override {
26 BOTAN_UNUSED(input_length);
27 m_hash.update(m_pub_seed);
28 m_hash.update(address.to_bytes());
29 return m_hash;
30 }
31
32 Hash_Engine& tweak_hash_engine(size_t input_length) override {
33 BOTAN_UNUSED(input_length);
34 return *m_engine;
35 }
36
37 size_t address_encoding_len() const override { return 32; }
38
39 void encode_address(std::span<uint8_t> out, const Sphincs_Address& address) const override {
40 const auto adrs = address.to_bytes();
41 copy_mem(out.data(), adrs.data(), adrs.size());
42 }
43
44 std::vector<uint8_t> H_msg_digest(StrongSpan<const SphincsMessageRandomness> r,
45 const SphincsTreeNode& root,
46 const SphincsMessageInternal& message) override {
47 m_h_msg_hash.update(r);
48 m_h_msg_hash.update(m_pub_seed);
49 m_h_msg_hash.update(root);
50 m_h_msg_hash.update(message.prefix);
51 m_h_msg_hash.update(message.message);
52
53 return m_h_msg_hash.final_stdvec();
54 }
55
56 public:
57 Sphincs_Hash_Functions_Shake(const Sphincs_Parameters& sphincs_params, const SphincsPublicSeed& pub_seed) :
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()))) {}
63
67 const SphincsMessageInternal& msg) override {
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 }
74
75 std::string msg_hash_function_name() const override { return m_h_msg_hash.name(); }
76
77 private:
78 SHAKE_256 m_hash;
79 SHAKE_256 m_h_msg_hash;
80 std::unique_ptr<Hash_Engine> m_engine;
81};
82
83} // namespace Botan
84
85#endif
#define BOTAN_UNUSED
Definition assert.h:151
std::array< uint8_t, 32 > to_bytes() const
Definition sp_address.h:136
Sphincs_Hash_Functions_Shake(const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
std::string msg_hash_function_name() const override
void PRF_msg(StrongSpan< SphincsMessageRandomness > out, StrongSpan< const SphincsSecretPRF > sk_prf, StrongSpan< const SphincsOptionalRandomness > opt_rand, const SphincsMessageInternal &msg) override
const SphincsPublicSeed & m_pub_seed
Definition sp_hash.h:138
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
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
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
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