| 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/shake.h> |
| 15 |
|
| 16 |
namespace Botan { |
| 17 |
|
| 18 |
/** |
| 19 |
* Implementation of SLH-DSA hash function abstraction for SHAKE256 |
| 20 |
*/ |
| 21 |
class Sphincs_Hash_Functions_Shake : public Sphincs_Hash_Functions { |
| 22 |
private: |
| 23 |
HashFunction& tweak_hash(const Sphincs_Address& address, size_t input_length) override { |
| 24 |
BOTAN_UNUSED(input_length); |
| 25 |
m_hash.update(m_pub_seed); |
| 26 |
m_hash.update(address.to_bytes()); |
| 27 |
return m_hash; |
| 28 |
} |
| 29 |
|
| 30 |
std::vector<uint8_t> H_msg_digest(StrongSpan<const SphincsMessageRandomness> r, |
| 31 |
const SphincsTreeNode& root, |
| 32 |
const SphincsMessageInternal& message) override { |
| 33 |
m_h_msg_hash.update(r); |
| 34 |
m_h_msg_hash.update(m_pub_seed); |
| 35 |
m_h_msg_hash.update(root); |
| 36 |
m_h_msg_hash.update(message.prefix); |
| 37 |
m_h_msg_hash.update(message.message); |
| 38 |
|
| 39 |
return m_h_msg_hash.final_stdvec(); |
| 40 |
} |
| 41 |
|
| 42 |
public: |
| 43 |
Sphincs_Hash_Functions_Shake(const Sphincs_Parameters& sphincs_params, const SphincsPublicSeed& pub_seed) : |
| 44 |
Sphincs_Hash_Functions(sphincs_params, pub_seed), |
| 45 |
m_hash(sphincs_params.n() * 8), |
| 46 |
m_h_msg_hash(8 * sphincs_params.h_msg_digest_bytes()) {} |
| 47 |
|
| 48 |
void PRF_msg(StrongSpan<SphincsMessageRandomness> out, |
| 49 |
StrongSpan<const SphincsSecretPRF> sk_prf, |
| 50 |
StrongSpan<const SphincsOptionalRandomness> opt_rand, |
| 51 |
const SphincsMessageInternal& msg) override { |
| 52 |
m_hash.update(sk_prf); |
| 53 |
m_hash.update(opt_rand); |
| 54 |
m_hash.update(msg.prefix); |
| 55 |
m_hash.update(msg.message); |
| 56 |
m_hash.final(out); |
| 57 |
} |
| 58 |
|
| 59 |
std::string msg_hash_function_name() const override { return m_h_msg_hash.name(); } |
| 60 |
|
| 61 |
private: |
| 62 |
SHAKE_256 m_hash; |
| 63 |
SHAKE_256 m_h_msg_hash; |
| 64 |
}; |
| 65 |
|
| 66 |
} // namespace Botan |
| 67 |
|
| 68 |
#endif |