Botan 3.14.1
Crypto and TLS for C&
xmss_signature_operation.cpp
Go to the documentation of this file.
1/*
2 * XMSS Signature Operation
3 * Signature generation operation for Extended Hash-Based Signatures (XMSS) as
4 * defined in:
5 *
6 * [1] XMSS: Extended Hash-Based Signatures,
7 * Request for Comments: 8391
8 * Release: May 2018.
9 * https://datatracker.ietf.org/doc/rfc8391/
10 *
11 * (C) 2016,2017,2018 Matthias Gierlings
12 * 2026 Jack Lloyd
13 *
14 * Botan is released under the Simplified BSD License (see license.txt)
15 **/
16
17#include <botan/internal/xmss_signature_operation.h>
18
19#include <botan/internal/xmss_tools.h>
20#include <utility>
21
22namespace Botan {
23
25 m_priv_key(private_key), m_hash(private_key.xmss_parameters()) {}
26
28 const auto& params = m_priv_key.xmss_parameters();
29 return sizeof(uint32_t) + // size of leaf index
30 params.element_size() + params.len() * params.element_size() + params.tree_height() * params.element_size();
31}
32
33void XMSS_Signature_Operation::update(std::span<const uint8_t> input) {
34 // The leaf index is part of the message hash prefix, so the message is
35 // buffered to avoid reserving a leaf before sign() is called
36 m_msg_buffer.insert(m_msg_buffer.end(), input.begin(), input.end());
37}
38
40 const std::vector<uint8_t> msg = std::exchange(m_msg_buffer, {});
41
42 const auto& params = m_priv_key.xmss_parameters();
43
44 // reserve leaf index so it can not be reused by another signature
45 // operation using the same private key.
46 const auto leaf_idx = static_cast<uint32_t>(m_priv_key.reserve_unused_leaf_index());
47
48 secure_vector<uint8_t> index_bytes;
49 secure_vector<uint8_t> randomness;
50
51 // write prefix for message hashing into buffer.
52 xmss_concat(index_bytes, leaf_idx, 32);
53 m_hash.prf(randomness, m_priv_key.prf_value(), index_bytes);
54 index_bytes.clear();
55 xmss_concat(index_bytes, leaf_idx, params.element_size());
56 m_hash.h_msg_init(randomness, m_priv_key.root(), index_bytes);
57 m_hash.h_msg_update(msg);
58
59 const auto msg_hash = m_hash.h_msg_final();
60
61 wots_keysig_t auth_path(params.tree_height());
62
63 XMSS_Address adrs;
65
66 for(size_t j = 0; j < params.tree_height(); j++) {
67 const size_t k = (leaf_idx / (static_cast<size_t>(1) << j)) ^ 0x01;
68 auth_path[j] = m_priv_key.tree_hash(k * (static_cast<size_t>(1) << j), j, adrs, m_hash);
69 }
70
72 adrs.set_ots_address(leaf_idx);
73
75 tree_sig.authentication_path = auth_path;
76 tree_sig.ots_signature =
77 m_priv_key.wots_private_key_for(adrs, m_hash).sign(msg_hash, m_priv_key.public_seed(), adrs, m_hash);
78
79 const XMSS_Signature sig(leaf_idx, randomness, tree_sig);
80 return sig.bytes();
81}
82
86
87} // namespace Botan
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
void set_ots_address(uint32_t value)
void set_type(Type type)
XMSS_Signature_Operation(const XMSS_PrivateKey &private_key)
AlgorithmIdentifier algorithm_identifier() const override
std::vector< uint8_t > sign(RandomNumberGenerator &rng) override
void update(std::span< const uint8_t > input) override
std::vector< uint8_t > bytes() const
XMSS_TreeSignature TreeSignature
std::vector< secure_vector< uint8_t > > wots_keysig_t
void xmss_concat(secure_vector< uint8_t > &target, const T &src)
Definition xmss_tools.h:28
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
wots_keysig_t authentication_path