Botan 3.5.0
Crypto and TLS for C&
sp_xmss.cpp
Go to the documentation of this file.
1/*
2* Sphincs+ XMSS logic
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#include <botan/internal/sp_xmss.h>
10
11#include <botan/internal/sp_address.h>
12#include <botan/internal/sp_treehash.h>
13#include <botan/internal/sp_wots.h>
14#include <botan/internal/stl_util.h>
15#include <optional>
16
17namespace Botan {
18
20 const SphincsTreeNode& root,
21 const SphincsSecretSeed& secret_seed,
22 Sphincs_Address& wots_addr,
23 Sphincs_Address& tree_addr,
24 std::optional<TreeNodeIndex> idx_leaf,
25 const Sphincs_Parameters& params,
26 Sphincs_Hash_Functions& hashes) {
27 BufferStuffer sig(out_sig);
28 auto wots_bytes_s = sig.next<WotsSignature>(params.wots_bytes());
29 auto auth_path_s = sig.next<SphincsAuthenticationPath>(sig.remaining_capacity());
30
31 const auto steps = [&]() -> std::vector<WotsHashIndex> {
32 // if `idx_leaf` is not set, we don't want to calculate a signature and
33 // therefore won't need to bother preparing the chain lengths either.
34 if(idx_leaf.has_value()) {
35 return chain_lengths(root, params);
36 } else {
37 return {};
38 };
39 }();
40
43
45
46 GenerateLeafFunction xmss_gen_leaf = [&](StrongSpan<SphincsTreeNode> out_root, TreeNodeIndex address_index) {
48 wots_bytes_s, out_root, secret_seed, address_index, idx_leaf, steps, leaf_addr, pk_addr, params, hashes);
49 };
50
51 SphincsTreeNode next_root(params.n());
53 treehash(next_root, auth_path_s, params, hashes, idx_leaf, 0, params.xmss_tree_height(), xmss_gen_leaf, tree_addr);
54
55 return next_root;
56}
57
59 const SphincsSecretSeed& secret_seed,
60 Sphincs_Hash_Functions& hashes) {
61 // We do not need the a sig/auth path in key generation, but it simplifies the
62 // code to have just one treehash routine that computes both root and path
63 // in one function.
64 SphincsXmssSignature dummy_sig(params.xmss_tree_height() * params.n() + params.wots_bytes());
65 SphincsTreeNode dummy_root(params.n());
66
69
70 top_tree_addr.set_layer(HypertreeLayerIndex(params.d() - 1));
71 wots_addr.set_layer(HypertreeLayerIndex(params.d() - 1));
72
74 xmss_sign_and_pkgen(dummy_sig, dummy_root, secret_seed, wots_addr, top_tree_addr, std::nullopt, params, hashes);
75
76 return root;
77}
78
79} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
Helper class to ease in-place marshalling of concatenated fixed-length values.
Definition stl_util.h:142
constexpr size_t remaining_capacity() const
Definition stl_util.h:189
constexpr std::span< uint8_t > next(size_t bytes)
Definition stl_util.h:150
Sphincs_Address_Type get_type() const
Definition sp_address.h:132
Sphincs_Address & set_layer(HypertreeLayerIndex layer)
Definition sp_address.h:55
static Sphincs_Address as_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:110
Sphincs_Address & set_type(Sphincs_Address_Type type)
Definition sp_address.h:67
uint32_t wots_bytes() const
uint32_t xmss_tree_height() const
SphincsTreeNode xmss_sign_and_pkgen(StrongSpan< SphincsXmssSignature > out_sig, const SphincsTreeNode &root, const SphincsSecretSeed &secret_seed, Sphincs_Address &wots_addr, Sphincs_Address &tree_addr, std::optional< TreeNodeIndex > idx_leaf, const Sphincs_Parameters &params, Sphincs_Hash_Functions &hashes)
Definition sp_xmss.cpp:19
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
SphincsTreeNode xmss_gen_root(const Sphincs_Parameters &params, const SphincsSecretSeed &secret_seed, Sphincs_Hash_Functions &hashes)
Definition sp_xmss.cpp:58
void wots_sign_and_pkgen(StrongSpan< WotsSignature > sig_out, StrongSpan< SphincsTreeNode > leaf_out, const SphincsSecretSeed &secret_seed, TreeNodeIndex leaf_idx, std::optional< TreeNodeIndex > sign_leaf_idx, const std::vector< WotsHashIndex > &wots_steps, Sphincs_Address &leaf_addr, Sphincs_Address &pk_addr, const Sphincs_Parameters &params, Sphincs_Hash_Functions &hashes)
Definition sp_wots.cpp:126
std::function< void(StrongSpan< SphincsTreeNode >, TreeNodeIndex)> GenerateLeafFunction
Definition sp_treehash.h:25
Strong< uint32_t, struct HypertreeLayerIndex_ > HypertreeLayerIndex
Index of a layer in the XMSS hyper-tree.
Definition sp_types.h:72
std::vector< WotsHashIndex > chain_lengths(const SphincsTreeNode &msg, const Sphincs_Parameters &params)
Definition sp_wots.cpp:85
void treehash(StrongSpan< SphincsTreeNode > out_root, StrongSpan< SphincsAuthenticationPath > out_auth_path, const Sphincs_Parameters &params, Sphincs_Hash_Functions &hashes, std::optional< TreeNodeIndex > leaf_idx, uint32_t idx_offset, uint32_t total_tree_height, const GenerateLeafFunction &gen_leaf, Sphincs_Address &tree_address)