11#include <botan/internal/sp_fors.h>
13#include <botan/assert.h>
14#include <botan/sp_parameters.h>
16#include <botan/internal/buffer_slicer.h>
17#include <botan/internal/buffer_stuffer.h>
18#include <botan/internal/sp_address.h>
19#include <botan/internal/sp_hash.h>
20#include <botan/internal/sp_treehash.h>
21#include <botan/internal/sp_types.h>
28std::vector<TreeNodeIndex> fors_message_to_indices(std::span<const uint8_t> message,
const Sphincs_Parameters& params) {
31 std::vector<TreeNodeIndex> indices(params.k());
36 const auto update_idx = [&]() -> std::function<
void(
TreeNodeIndex&, uint32_t)> {
37#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
38 if(params.is_slh_dsa()) {
40 idx ^= (((message[offset >> 3] >> (~offset & 0x7)) & 0x1) << (params.a() - 1 - i));
44#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
45 if(!params.is_slh_dsa()) {
46 return [&](
TreeNodeIndex& idx, uint32_t i) { idx ^= (((message[offset >> 3] >> (offset & 0x7)) & 0x1) << i); };
49 throw Internal_Error(
"Missing FORS index update logic for SPHINCS+ or SLH-DSA");
52 for(
auto& idx : indices) {
53 for(uint32_t i = 0; i < params.a(); ++i, ++offset) {
71 const auto indices = fors_message_to_indices(hashed_message, params);
75 const auto fors_pk_addr =
78 std::vector<uint8_t> roots_buffer(params.
k() * params.
n());
83 std::vector<Sphincs_Address> leaf_addrs;
85 std::vector<std::span<uint8_t>> secret_spans;
86 std::vector<std::span<const uint8_t>> csecret_spans;
87 std::vector<std::span<uint8_t>> leaf_spans;
91 leaf_addrs.assign(count, fors_tree_addr);
92 leaf_secrets.resize(count * params.
n());
93 secret_spans.resize(count);
94 csecret_spans.resize(count);
95 leaf_spans.resize(count);
97 for(uint32_t j = 0; j != count; ++j) {
101 .set_tree_index(first_idx + j);
102 secret_spans[j] = std::span(leaf_secrets).subspan(j * params.
n(), params.
n());
103 csecret_spans[j] = secret_spans[j];
104 leaf_spans[j] = out.subspan(j * params.
n(), params.
n());
107 hashes.
PRF_batch(secret_spans, secret_seed, leaf_addrs);
109 for(uint32_t j = 0; j != count; ++j) {
113 hashes.
T_batch(leaf_spans, leaf_addrs, csecret_spans);
119 for(uint32_t i = 0; i < params.
k(); ++i) {
120 const uint32_t idx_offset = i * (1 << params.
a());
125 .set_tree_index(indices[i] + idx_offset);
155 const auto indices = fors_message_to_indices(hashed_message, params);
159 const auto fors_pk_addr =
163 std::vector<uint8_t> roots_buffer(params.
k() * params.
n());
169 for(uint32_t i = 0; i < params.
k(); ++i) {
170 const uint32_t idx_offset = i * (1 << params.
a());
173 fors_tree_addr.set_tree_height(
TreeLayerIndex(0)).set_tree_index(indices[i] + idx_offset);
176 const auto leaf = hashes.
T<
SphincsTreeNode>(fors_tree_addr, fors_leaf_secret);
#define BOTAN_ASSERT_NOMSG(expr)
std::span< const uint8_t > take(const size_t count)
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr std::span< uint8_t > next(size_t bytes)
constexpr bool full() const
Sphincs_Address & set_type(Sphincs_Address_Type type)
static Sphincs_Address as_keypair_from(const Sphincs_Address &other)
void T_batch(std::span< std::span< uint8_t > > outputs, std::span< const Sphincs_Address > addresses, std::span< std::span< const uint8_t > > inputs)
void T(std::span< uint8_t > out, const Sphincs_Address &address, const BufferTs &... in)
void PRF_batch(std::span< std::span< uint8_t > > outputs, const SphincsSecretSeed &sk_seed, std::span< const Sphincs_Address > addresses)
void PRF(StrongSpan< ForsLeafSecret > out, const SphincsSecretSeed &sk_seed, const Sphincs_Address &address)
uint32_t fors_signature_bytes() const
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
Strong< std::vector< uint8_t >, struct SphincsTreeNode_ > SphincsTreeNode
Either an XMSS or FORS tree node or leaf.
SphincsTreeNode fors_sign_and_pkgen(StrongSpan< ForsSignature > sig_out, const SphincsHashedMessage &hashed_message, const SphincsSecretSeed &secret_seed, const Sphincs_Address &address, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 16: fors_sign (with simultaneous FORS pk generation).
Strong< secure_vector< uint8_t >, struct ForsLeafSecret_ > ForsLeafSecret
std::function< void(std::span< uint8_t >, TreeNodeIndex, uint32_t)> GenerateLeavesFunction
void treehash(StrongSpan< SphincsTreeNode > out_root, StrongSpan< SphincsAuthenticationPath > out_auth_path, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes, std::optional< TreeNodeIndex > leaf_idx, uint32_t idx_offset, uint32_t total_tree_height, const GenerateLeavesFunction &gen_leaves, Sphincs_Address &tree_address)
Strong< secure_vector< uint8_t >, struct SphincsSecretSeed_ > SphincsSecretSeed
Strong< std::vector< uint8_t >, struct SphincsHashedMessage_ > SphincsHashedMessage
Strong< uint32_t, struct TreeNodeIndex_, EnableArithmeticWithPlainNumber > TreeNodeIndex
Index of an individual node inside an XMSS or FORS tree.
SphincsTreeNode fors_public_key_from_signature(const SphincsHashedMessage &hashed_message, StrongSpan< const ForsSignature > signature, const Sphincs_Address &address, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 17: fors_pkFromSig.
Strong< std::vector< uint8_t >, struct SphincsAuthenticationPath_ > SphincsAuthenticationPath
std::vector< T, secure_allocator< T > > secure_vector
Strong< uint32_t, struct TreeLayerIndex_, EnableArithmeticWithPlainNumber > TreeLayerIndex
Index of the layer within a FORS/XMSS tree.
void compute_root(StrongSpan< SphincsTreeNode > out, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes, const SphincsTreeNode &leaf, TreeNodeIndex leaf_idx, uint32_t idx_offset, StrongSpan< const SphincsAuthenticationPath > authentication_path, uint32_t total_tree_height, Sphincs_Address &tree_address)