Botan 3.12.0
Crypto and TLS for C&
xmss_privatekey.cpp
Go to the documentation of this file.
1/*
2 * XMSS Private Key
3 * An XMSS: Extended Hash-Based Signature private key.
4 * The XMSS private key does not support the X509 and PKCS7 standard. Instead
5 * the raw format described in [1] is used.
6 *
7 * [1] XMSS: Extended Hash-Based Signatures,
8 * Request for Comments: 8391
9 * Release: May 2018.
10 * https://datatracker.ietf.org/doc/rfc8391/
11 *
12 * (C) 2016,2017,2018 Matthias Gierlings
13 * (C) 2019,2026 Jack Lloyd
14 * (C) 2023 René Meusel - Rohde & Schwarz Cybersecurity
15 *
16 * Botan is released under the Simplified BSD License (see license.txt)
17 **/
18
19#include <botan/xmss.h>
20
21#include <botan/ber_dec.h>
22#include <botan/der_enc.h>
23#include <botan/rng.h>
24#include <botan/internal/buffer_slicer.h>
25#include <botan/internal/concat_util.h>
26#include <botan/internal/int_utils.h>
27#include <botan/internal/loadstor.h>
28#include <botan/internal/stateful_key_index_registry.h>
29#include <botan/internal/xmss_common_ops.h>
30#include <botan/internal/xmss_hash.h>
31#include <botan/internal/xmss_signature_operation.h>
32
33#if defined(BOTAN_HAS_THREAD_UTILS)
34 #include <botan/internal/thread_pool.h>
35#endif
36
37namespace Botan {
38
39namespace {
40
41// fall back to raw decoding for previous versions, which did not encode an OCTET STRING
42secure_vector<uint8_t> extract_raw_private_key(std::span<const uint8_t> key_bits, const XMSS_Parameters& xmss_params) {
44
45 // The public part of the input key bits was already parsed, so we can
46 // decide depending on the buffer length whether this must be BER decoded.
47 if(key_bits.size() == xmss_params.raw_private_key_size() ||
48 key_bits.size() == xmss_params.raw_legacy_private_key_size()) {
49 raw_key.assign(key_bits.begin(), key_bits.end());
50 } else {
52 }
53
54 return raw_key;
55}
56
57} // namespace
58
59class XMSS_PrivateKey_Internal final {
60 public:
61 XMSS_PrivateKey_Internal(XMSS_Parameters::xmss_algorithm_t xmss_algo_id,
62 WOTS_Derivation_Method wots_derivation_method,
63 RandomNumberGenerator& rng) :
64 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
65 m_wots_params(m_xmss_params.wots_parameters()),
66 m_wots_derivation_method(wots_derivation_method),
67 m_prf(rng.random_vec(m_xmss_params.element_size())),
68 m_private_seed(rng.random_vec(m_xmss_params.element_size())),
69 m_keyid(Stateful_Key_Index_Registry::KeyId("XMSS", m_xmss_params.oid(), m_private_seed, m_prf)) {}
70
71 XMSS_PrivateKey_Internal(XMSS_Parameters::xmss_algorithm_t xmss_algo_id,
72 WOTS_Derivation_Method wots_derivation_method,
73 secure_vector<uint8_t> private_seed,
75 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
76 m_wots_params(m_xmss_params.wots_parameters()),
77 m_wots_derivation_method(wots_derivation_method),
78 m_prf(std::move(prf)),
79 m_private_seed(std::move(private_seed)),
80 m_keyid(Stateful_Key_Index_Registry::KeyId("XMSS", m_xmss_params.oid(), m_private_seed, m_prf)) {}
81
82 XMSS_PrivateKey_Internal(XMSS_Parameters::xmss_algorithm_t xmss_algo_id, std::span<const uint8_t> key_bits) :
83 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
84 m_wots_params(m_xmss_params.wots_parameters()),
85 m_keyid(/* initialized later*/) {
86 /*
87 The code requires sizeof(size_t) >= ceil(tree_height / 8)
88
89 Maximum supported tree height is 20, ceil(20/8) == 3, so 4 byte
90 size_t is sufficient for all defined parameters, or even a
91 (hypothetical) tree height 32, which would be extremely slow to
92 compute.
93 */
94 static_assert(sizeof(size_t) >= 4, "size_t is big enough to support leaf index");
95
96 const secure_vector<uint8_t> raw_key = extract_raw_private_key(key_bits, m_xmss_params);
97
98 if(raw_key.size() != m_xmss_params.raw_private_key_size() &&
99 raw_key.size() != m_xmss_params.raw_legacy_private_key_size()) {
100 throw Decoding_Error("Invalid XMSS private key size");
101 }
102
103 BufferSlicer s(raw_key);
104
105 // We're not interested in the public key here
106 s.skip(m_xmss_params.raw_public_key_size());
107
108 auto unused_leaf_bytes = s.take(sizeof(uint32_t));
109 const size_t unused_leaf = load_be<uint32_t>(unused_leaf_bytes.data(), 0);
110 if(unused_leaf >= (1ULL << m_xmss_params.tree_height())) {
111 throw Decoding_Error("XMSS private key leaf index out of bounds");
112 }
113
114 m_prf = s.copy_as_secure_vector(m_xmss_params.element_size());
115 m_private_seed = s.copy_as_secure_vector(m_xmss_params.element_size());
116
117 m_keyid = Stateful_Key_Index_Registry::KeyId("XMSS", m_xmss_params.oid(), m_private_seed, m_prf);
118
119 // Note m_keyid must be initialized before set_unused_leaf_index is called!
120 set_unused_leaf_index(unused_leaf);
121
122 // Legacy keys generated prior to Botan 3.x don't feature a
123 // WOTS+ key derivation method encoded in their private key.
124 m_wots_derivation_method =
125 (s.empty()) ? WOTS_Derivation_Method::Botan2x : static_cast<WOTS_Derivation_Method>(s.take(1).front());
126
127 BOTAN_ASSERT_NOMSG(s.empty());
128 }
129
130 secure_vector<uint8_t> serialize(std::vector<uint8_t> raw_public_key) const {
131 std::vector<uint8_t> unused_index(4);
132 store_be(static_cast<uint32_t>(unused_leaf_index()), unused_index.data());
133
134 std::vector<uint8_t> wots_derivation_method;
135 wots_derivation_method.push_back(static_cast<uint8_t>(m_wots_derivation_method));
136
138 raw_public_key, unused_index, m_prf, m_private_seed, wots_derivation_method);
139 }
140
141 const secure_vector<uint8_t>& prf_value() const { return m_prf; }
142
143 const secure_vector<uint8_t>& private_seed() { return m_private_seed; }
144
145 const XMSS_WOTS_Parameters& wots_parameters() { return m_wots_params; }
146
147 WOTS_Derivation_Method wots_derivation_method() const { return m_wots_derivation_method; }
148
149 void set_unused_leaf_index(size_t idx) {
150 if(idx >= (1ULL << m_xmss_params.tree_height())) {
151 throw Decoding_Error("XMSS private key leaf index out of bounds");
152 } else {
154 }
155 }
156
157 size_t reserve_unused_leaf_index() {
158 const uint64_t idx = Stateful_Key_Index_Registry::global().reserve_next_index(m_keyid);
159 if(idx >= m_xmss_params.total_number_of_signatures()) {
160 throw Decoding_Error("XMSS private key, one time signatures exhausted");
161 }
162 // Cast is safe even on 32 bit since total_number_of_signatures will be less
163 return static_cast<size_t>(idx);
164 }
165
166 size_t unused_leaf_index() const {
167 const uint64_t idx = Stateful_Key_Index_Registry::global().current_index(m_keyid);
168 return checked_cast_to<size_t>(idx);
169 }
170
171 uint64_t remaining_signatures() const {
172 const size_t max = m_xmss_params.total_number_of_signatures();
174 }
175
176 private:
177 XMSS_Parameters m_xmss_params;
178 XMSS_WOTS_Parameters m_wots_params;
179 WOTS_Derivation_Method m_wots_derivation_method;
180
182 secure_vector<uint8_t> m_private_seed;
183 Stateful_Key_Index_Registry::KeyId m_keyid;
184};
185
186XMSS_PrivateKey::XMSS_PrivateKey(std::span<const uint8_t> key_bits) :
187 XMSS_PublicKey(key_bits),
188 m_private(std::make_shared<XMSS_PrivateKey_Internal>(xmss_parameters().oid(), key_bits)) {}
189
193 XMSS_PublicKey(xmss_algo_id, rng),
194 m_private(std::make_shared<XMSS_PrivateKey_Internal>(xmss_algo_id, wots_derivation_method, rng)) {
195 const XMSS_Address adrs;
197 set_root(tree_hash(0, xmss_parameters().tree_height(), adrs, hash));
198}
199
201 size_t idx_leaf,
202 secure_vector<uint8_t> wots_priv_seed,
207 XMSS_PublicKey(xmss_algo_id, std::move(root), std::move(public_seed)),
208 m_private(std::make_shared<XMSS_PrivateKey_Internal>(
209 xmss_algo_id, wots_derivation_method, std::move(wots_priv_seed), std::move(prf))) {
210 m_private->set_unused_leaf_index(idx_leaf);
211 BOTAN_ARG_CHECK(m_private->prf_value().size() == xmss_parameters().element_size(),
212 "XMSS: unexpected byte length of PRF value");
213 BOTAN_ARG_CHECK(m_private->private_seed().size() == xmss_parameters().element_size(),
214 "XMSS: unexpected byte length of private seed");
215}
216
217secure_vector<uint8_t> XMSS_PrivateKey::tree_hash(size_t start_idx,
218 size_t target_node_height,
219 const XMSS_Address& adrs,
220 XMSS_Hash& hash) const {
221 BOTAN_ASSERT_NOMSG(target_node_height <= 30);
222 BOTAN_ASSERT((start_idx % (static_cast<size_t>(1) << target_node_height)) == 0,
223 "Start index must be divisible by 2^{target node height}.");
224
225#if defined(BOTAN_HAS_THREAD_UTILS)
226 // determine number of parallel tasks to split the tree_hashing into.
227
229
230 const size_t split_level = std::min(target_node_height, thread_pool.worker_count());
231
232 // skip parallelization overhead for leaf nodes.
233 if(split_level == 0) {
235 XMSS_Address subtree_addr(adrs);
236 tree_hash_subtree(result, start_idx, target_node_height, subtree_addr, hash);
237 return result;
238 }
239
240 const size_t subtrees = static_cast<size_t>(1) << split_level;
241 const size_t last_idx = (static_cast<size_t>(1) << (target_node_height)) + start_idx;
242 const size_t offs = (last_idx - start_idx) / subtrees;
243 // this cast cannot overflow because target_node_height is limited
244 uint8_t level = static_cast<uint8_t>(split_level); // current level in the tree
245
246 BOTAN_ASSERT((last_idx - start_idx) % subtrees == 0,
247 "Number of worker threads in tree_hash need to divide range "
248 "of calculated nodes.");
249
250 std::vector<secure_vector<uint8_t>> nodes(subtrees, secure_vector<uint8_t>(xmss_parameters().element_size()));
251 std::vector<XMSS_Address> node_addresses(subtrees, adrs);
252 std::vector<XMSS_Hash> xmss_hash(subtrees, hash);
253 std::vector<std::future<void>> work;
254
255 // Calculate multiple subtrees in parallel.
256 for(size_t i = 0; i < subtrees; i++) {
257 using tree_hash_subtree_fn_t =
258 void (XMSS_PrivateKey::*)(secure_vector<uint8_t>&, size_t, size_t, XMSS_Address&, XMSS_Hash&) const;
259
260 const tree_hash_subtree_fn_t work_fn = &XMSS_PrivateKey::tree_hash_subtree;
261
262 work.push_back(thread_pool.run(work_fn,
263 this,
264 std::ref(nodes[i]),
265 start_idx + i * offs,
266 target_node_height - split_level,
267 std::ref(node_addresses[i]),
268 std::ref(xmss_hash[i])));
269 }
270
271 for(auto& w : work) {
272 w.get();
273 }
274 work.clear();
275
276 // Parallelize the top tree levels horizontally
277 while(level-- > 1) {
278 std::vector<secure_vector<uint8_t>> ro_nodes(nodes.begin(),
279 nodes.begin() + (static_cast<size_t>(1) << (level + 1)));
280
281 for(size_t i = 0; i < (static_cast<size_t>(1) << level); i++) {
282 BOTAN_ASSERT_NOMSG(xmss_hash.size() > i);
283
284 node_addresses[i].set_tree_height(static_cast<uint32_t>(target_node_height - (level + 1)));
285 node_addresses[i].set_tree_index((node_addresses[2 * i + 1].get_tree_index() - 1) >> 1);
286
287 work.push_back(thread_pool.run(&XMSS_Common_Ops::randomize_tree_hash,
288 std::ref(nodes[i]),
289 std::cref(ro_nodes[2 * i]),
290 std::cref(ro_nodes[2 * i + 1]),
291 node_addresses[i],
292 std::cref(this->public_seed()),
293 std::ref(xmss_hash[i]),
294 std::cref(xmss_parameters())));
295 }
296
297 for(auto& w : work) {
298 w.get();
299 }
300 work.clear();
301 }
302
303 // Avoid creation an extra thread to calculate root node.
304 node_addresses[0].set_tree_height(static_cast<uint32_t>(target_node_height - 1));
305 node_addresses[0].set_tree_index((node_addresses[1].get_tree_index() - 1) >> 1);
307 nodes[0], nodes[0], nodes[1], node_addresses[0], this->public_seed(), hash, xmss_parameters());
308 return nodes[0];
309#else
311 XMSS_Address subtree_addr(adrs);
312 tree_hash_subtree(result, start_idx, target_node_height, subtree_addr, hash);
313 return result;
314#endif
315}
316
317void XMSS_PrivateKey::tree_hash_subtree(secure_vector<uint8_t>& result,
318 size_t start_idx,
319 size_t target_node_height,
320 XMSS_Address& adrs,
321 XMSS_Hash& hash) const {
322 const secure_vector<uint8_t>& seed = this->public_seed();
323
324 std::vector<secure_vector<uint8_t>> nodes(target_node_height + 1,
325 secure_vector<uint8_t>(xmss_parameters().element_size()));
326
327 // node stack, holds all nodes on stack and one extra "pending" node. This
328 // temporary node referred to as "node" in the XMSS standard document stays
329 // a pending element, meaning it is not regarded as element on the stack
330 // until level is increased.
331 std::vector<uint8_t> node_levels(target_node_height + 1);
332
333 uint8_t level = 0; // current level on the node stack.
334 const size_t last_idx = (static_cast<size_t>(1) << target_node_height) + start_idx;
335
336 for(size_t i = start_idx; i < last_idx; i++) {
338 adrs.set_ots_address(static_cast<uint32_t>(i));
339
340 const XMSS_WOTS_PublicKey pk = this->wots_public_key_for(adrs, hash);
341
343 adrs.set_ltree_address(static_cast<uint32_t>(i));
344 XMSS_Common_Ops::create_l_tree(nodes[level], pk.key_data(), adrs, seed, hash, xmss_parameters());
345 node_levels[level] = 0;
346
348 adrs.set_tree_height(0);
349 adrs.set_tree_index(static_cast<uint32_t>(i));
350
351 while(level > 0 && node_levels[level] == node_levels[level - 1]) {
352 adrs.set_tree_index(((adrs.get_tree_index() - 1) >> 1));
354 nodes[level - 1], nodes[level - 1], nodes[level], adrs, seed, hash, xmss_parameters());
355 node_levels[level - 1]++;
356 level--; //Pop stack top element
357 adrs.set_tree_height(adrs.get_tree_height() + 1);
358 }
359 level++; //push temporary node to stack
360 }
361 result = nodes[level - 1];
362}
363
364XMSS_WOTS_PublicKey XMSS_PrivateKey::wots_public_key_for(const XMSS_Address& adrs, XMSS_Hash& hash) const {
365 const auto private_key = wots_private_key_for(adrs, hash);
366 return XMSS_WOTS_PublicKey(m_private->wots_parameters(), public_seed(), private_key, adrs, hash);
367}
368
369XMSS_WOTS_PrivateKey XMSS_PrivateKey::wots_private_key_for(const XMSS_Address& adrs, XMSS_Hash& hash) const {
370 switch(wots_derivation_method()) {
372 return XMSS_WOTS_PrivateKey(
373 m_private->wots_parameters(), public_seed(), m_private->private_seed(), adrs, hash);
375 return XMSS_WOTS_PrivateKey(m_private->wots_parameters(), m_private->private_seed(), adrs, hash);
376 }
377
378 throw Invalid_State("WOTS derivation method is out of the enum's range");
379}
380
384
385size_t XMSS_PrivateKey::reserve_unused_leaf_index() {
386 return m_private->reserve_unused_leaf_index();
387}
388
390 return m_private->unused_leaf_index();
391}
392
394 return checked_cast_to<size_t>(m_private->remaining_signatures());
395}
396
397std::optional<uint64_t> XMSS_PrivateKey::remaining_operations() const {
398 return m_private->remaining_signatures();
399}
400
401const secure_vector<uint8_t>& XMSS_PrivateKey::prf_value() const {
402 return m_private->prf_value();
403}
404
406 return m_private->serialize(raw_public_key());
407}
408
410 return m_private->wots_derivation_method();
411}
412
413std::unique_ptr<Public_Key> XMSS_PrivateKey::public_key() const {
414 return std::make_unique<XMSS_PublicKey>(xmss_parameters().oid(), root(), public_seed());
415}
416
417std::unique_ptr<PK_Ops::Signature> XMSS_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
418 std::string_view /*params*/,
419 std::string_view provider) const {
420 if(provider == "base" || provider.empty()) {
421 return std::make_unique<XMSS_Signature_Operation>(*this);
422 }
423
424 throw Provider_Not_Found(algo_name(), provider);
425}
426
427} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
static Limits DER()
Definition ber_dec.h:35
BER_Decoder & decode(bool &out)
Definition ber_dec.h:220
BER_Decoder & verify_end()
Definition ber_dec.cpp:381
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:134
DER_Encoder & encode(bool b)
Definition der_enc.cpp:245
void set_index_lower_bound(const KeyId &key_id, uint64_t min)
static Stateful_Key_Index_Registry & global()
uint64_t reserve_next_index(const KeyId &key_id)
uint64_t remaining_operations(const KeyId &key_id, uint64_t max)
size_t worker_count() const
Definition thread_pool.h:52
auto run(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
Definition thread_pool.h:66
static Thread_Pool & global_instance()
static void create_l_tree(secure_vector< uint8_t > &result, wots_keysig_t pk, XMSS_Address adrs, const secure_vector< uint8_t > &seed, XMSS_Hash &hash, const XMSS_Parameters &params)
static void randomize_tree_hash(secure_vector< uint8_t > &result, const secure_vector< uint8_t > &left, const secure_vector< uint8_t > &right, XMSS_Address adrs, const secure_vector< uint8_t > &seed, XMSS_Hash &hash, const XMSS_Parameters &params)
std::unique_ptr< Public_Key > public_key() const override
size_t remaining_signatures() const
size_t unused_leaf_index() const
std::optional< uint64_t > remaining_operations() const override
Retrieves the number of remaining operations if this is a stateful private key.
WOTS_Derivation_Method wots_derivation_method() const
secure_vector< uint8_t > raw_private_key() const
secure_vector< uint8_t > private_key_bits() const override
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
XMSS_PrivateKey(XMSS_Parameters::xmss_algorithm_t xmss_algo_id, RandomNumberGenerator &rng, WOTS_Derivation_Method wots_derivation_method=WOTS_Derivation_Method::NIST_SP800_208)
const secure_vector< uint8_t > & root() const
const secure_vector< uint8_t > & public_seed() const
const XMSS_Parameters & xmss_parameters() const
std::vector< uint8_t > raw_public_key() const
void set_root(secure_vector< uint8_t > root)
std::string algo_name() const override
Definition xmss.h:71
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, RandomNumberGenerator &rng)
constexpr RT checked_cast_to(AT i)
Definition int_utils.h:74
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:90
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
WOTS_Derivation_Method
Definition xmss.h:135
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504