Botan  2.11.0
Crypto and TLS for C++11
xmss_privatekey.cpp
Go to the documentation of this file.
1 /*
2  * XMSS Private Key
3  * An XMSS: Extended Hash-Based Siganture 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 Jack Lloyd
14  *
15  * Botan is released under the Simplified BSD License (see license.txt)
16  **/
17 
18 #include <botan/xmss_privatekey.h>
19 #include <botan/internal/xmss_signature_operation.h>
20 
21 #if defined(BOTAN_HAS_THREAD_UTILS)
22  #include <botan/internal/thread_pool.h>
23 #endif
24 
25 namespace Botan {
26 
28  : XMSS_PublicKey(unlock(raw_key)),
29  XMSS_Common_Ops(XMSS_PublicKey::m_xmss_params.oid()),
30  m_wots_priv_key(m_wots_params.oid(), m_public_seed),
31  m_index_reg(XMSS_Index_Registry::get_instance())
32  {
33  /*
34  The code requires sizeof(size_t) >= ceil(tree_height / 8)
35 
36  Maximum supported tree height is 20, ceil(20/8) == 3, so 4 byte
37  size_t is sufficient for all defined parameters, or even a
38  (hypothetical) tree height 32, which would be extremely slow to
39  compute.
40  */
41  static_assert(sizeof(size_t) >= 4, "size_t is big enough to support leaf index");
42 
43  if(raw_key.size() != XMSS_PrivateKey::size())
44  {
45  throw Decoding_Error("Invalid XMSS private key size detected.");
46  }
47 
48  // extract & copy unused leaf index from raw_key.
49  uint64_t unused_leaf = 0;
50  auto begin = (raw_key.begin() + XMSS_PublicKey::size());
51  auto end = raw_key.begin() + XMSS_PublicKey::size() + sizeof(uint32_t);
52 
53  for(auto& i = begin; i != end; i++)
54  {
55  unused_leaf = ((unused_leaf << 8) | *i);
56  }
57 
58  if(unused_leaf >= (1ull << XMSS_PublicKey::m_xmss_params.tree_height()))
59  {
60  throw Decoding_Error("XMSS private key leaf index out of bounds");
61  }
62 
63  begin = end;
65  m_prf.clear();
66  m_prf.reserve(XMSS_PublicKey::m_xmss_params.element_size());
67  std::copy(begin, end, std::back_inserter(m_prf));
68 
69  begin = end;
70  end = begin + m_wots_params.element_size();
71  m_wots_priv_key.set_private_seed(secure_vector<uint8_t>(begin, end));
72  set_unused_leaf_index(static_cast<size_t>(unused_leaf));
73  }
74 
78  : XMSS_PublicKey(xmss_algo_id, rng),
79  XMSS_Common_Ops(xmss_algo_id),
80  m_wots_priv_key(XMSS_PublicKey::m_xmss_params.ots_oid(),
81  public_seed(),
82  rng),
83  m_prf(rng.random_vec(XMSS_PublicKey::m_xmss_params.element_size())),
84  m_index_reg(XMSS_Index_Registry::get_instance())
85  {
86  XMSS_Address adrs;
88  XMSS_PublicKey::m_xmss_params.tree_height(),
89  adrs));
90  }
91 
93 XMSS_PrivateKey::tree_hash(size_t start_idx,
94  size_t target_node_height,
95  XMSS_Address& adrs)
96  {
97  BOTAN_ASSERT((start_idx % (1 << target_node_height)) == 0,
98  "Start index must be divisible by 2^{target node height}.");
99 
100 #if defined(BOTAN_HAS_THREAD_UTILS)
101  // dertermine number of parallel tasks to split the tree_hashing into.
102 
103  Thread_Pool& thread_pool = Thread_Pool::global_instance();
104 
105  const size_t split_level = std::min(target_node_height, thread_pool.worker_count());
106 
107  // skip parallelization overhead for leaf nodes.
108  if(split_level == 0)
109  {
110  secure_vector<uint8_t> result;
111  tree_hash_subtree(result, start_idx, target_node_height, adrs);
112  return result;
113  }
114 
115  const size_t subtrees = static_cast<size_t>(1) << split_level;
116  const size_t last_idx = (static_cast<size_t>(1) << (target_node_height)) + start_idx;
117  const size_t offs = (last_idx - start_idx) / subtrees;
118  uint8_t level = split_level; // current level in the tree
119 
120  BOTAN_ASSERT((last_idx - start_idx) % subtrees == 0,
121  "Number of worker threads in tree_hash need to divide range "
122  "of calculated nodes.");
123 
124  std::vector<secure_vector<uint8_t>> nodes(
125  subtrees,
127  std::vector<XMSS_Address> node_addresses(subtrees, adrs);
128  std::vector<XMSS_Hash> xmss_hash(subtrees, m_hash);
129  std::vector<std::future<void>> work;
130 
131  // Calculate multiple subtrees in parallel.
132  for(size_t i = 0; i < subtrees; i++)
133  {
134  using tree_hash_subtree_fn_t =
136  size_t,
137  size_t,
138  XMSS_Address&,
139  XMSS_Hash&);
140 
141  auto work_fn = static_cast<tree_hash_subtree_fn_t>(&XMSS_PrivateKey::tree_hash_subtree);
142 
143  work.push_back(thread_pool.run(
144  work_fn,
145  this,
146  std::ref(nodes[i]),
147  start_idx + i * offs,
148  target_node_height - split_level,
149  std::ref(node_addresses[i]),
150  std::ref(xmss_hash[i])));
151  }
152 
153  for(auto& w : work)
154  {
155  w.get();
156  }
157  work.clear();
158 
159  // Parallelize the top tree levels horizontally
160  while(level-- > 1)
161  {
162  std::vector<secure_vector<uint8_t>> ro_nodes(
163  nodes.begin(), nodes.begin() + (1 << (level+1)));
164 
165  for(size_t i = 0; i < (1U << level); i++)
166  {
167  BOTAN_ASSERT_NOMSG(xmss_hash.size() > i);
168 
169  node_addresses[i].set_tree_height(target_node_height - (level + 1));
170  node_addresses[i].set_tree_index(
171  (node_addresses[2 * i + 1].get_tree_index() - 1) >> 1);
172  using rnd_tree_hash_fn_t =
174  const secure_vector<uint8_t>&,
175  const secure_vector<uint8_t>&,
176  XMSS_Address& adrs,
177  const secure_vector<uint8_t>&,
178  XMSS_Hash&);
179 
180  auto work_fn = static_cast<rnd_tree_hash_fn_t>(&XMSS_PrivateKey::randomize_tree_hash);
181 
182  work.push_back(thread_pool.run(
183  work_fn,
184  this,
185  std::ref(nodes[i]),
186  std::ref(ro_nodes[2 * i]),
187  std::ref(ro_nodes[2 * i + 1]),
188  std::ref(node_addresses[i]),
189  std::ref(this->public_seed()),
190  std::ref(xmss_hash[i])));
191  }
192 
193  for(auto &w : work)
194  {
195  w.get();
196  }
197  work.clear();
198  }
199 
200  // Avoid creation an extra thread to calculate root node.
201  node_addresses[0].set_tree_height(target_node_height - 1);
202  node_addresses[0].set_tree_index(
203  (node_addresses[1].get_tree_index() - 1) >> 1);
204  randomize_tree_hash(nodes[0],
205  nodes[0],
206  nodes[1],
207  node_addresses[0],
208  this->public_seed());
209  return nodes[0];
210 #else
211  secure_vector<uint8_t> result;
212  tree_hash_subtree(result, start_idx, target_node_height, adrs);
213  return result;
214 #endif
215  }
216 
217 void
218 XMSS_PrivateKey::tree_hash_subtree(secure_vector<uint8_t>& result,
219  size_t start_idx,
220  size_t target_node_height,
221  XMSS_Address& adrs,
222  XMSS_Hash& hash)
223  {
224  const secure_vector<uint8_t>& seed = this->public_seed();
225 
226  std::vector<secure_vector<uint8_t>> nodes(
227  target_node_height + 1,
229 
230  // node stack, holds all nodes on stack and one extra "pending" node. This
231  // temporary node referred to as "node" in the XMSS standard document stays
232  // a pending element, meaning it is not regarded as element on the stack
233  // until level is increased.
234  std::vector<uint8_t> node_levels(target_node_height + 1);
235 
236  uint8_t level = 0; // current level on the node stack.
237  XMSS_WOTS_PublicKey pk(m_wots_priv_key.wots_parameters().oid(), seed);
238  const size_t last_idx = (static_cast<size_t>(1) << target_node_height) + start_idx;
239 
240  for(size_t i = start_idx; i < last_idx; i++)
241  {
243  adrs.set_ots_address(i);
245  pk,
246  // getWOTS_SK(SK, s + i), reference implementation uses adrs
247  // instead of zero padded index s + i.
248  this->wots_private_key().at(adrs, hash),
249  adrs,
250  hash);
252  adrs.set_ltree_address(i);
253  create_l_tree(nodes[level], pk, adrs, seed, hash);
254  node_levels[level] = 0;
255 
257  adrs.set_tree_height(0);
258  adrs.set_tree_index(i);
259 
260  while(level > 0 && node_levels[level] ==
261  node_levels[level - 1])
262  {
263  adrs.set_tree_index(((adrs.get_tree_index() - 1) >> 1));
264  randomize_tree_hash(nodes[level - 1],
265  nodes[level - 1],
266  nodes[level],
267  adrs,
268  seed,
269  hash);
270  node_levels[level - 1]++;
271  level--; //Pop stack top element
272  adrs.set_tree_height(adrs.get_tree_height() + 1);
273  }
274  level++; //push temporary node to stack
275  }
276  result = nodes[level - 1];
277  }
278 
279 std::shared_ptr<Atomic<size_t>>
280 XMSS_PrivateKey::recover_global_leaf_index() const
281  {
282  BOTAN_ASSERT(m_wots_priv_key.private_seed().size() ==
285  "Trying to retrieve index for partially initialized "
286  "key.");
287  return m_index_reg.get(m_wots_priv_key.private_seed(),
288  m_prf);
289  }
290 
292  {
293  std::vector<uint8_t> pk { raw_public_key() };
294  secure_vector<uint8_t> result(pk.begin(), pk.end());
295  result.reserve(size());
296 
297  for(int i = 3; i >= 0; i--)
298  {
299  result.push_back(
300  static_cast<uint8_t>(
301  static_cast<uint64_t>(unused_leaf_index()) >> 8 * i));
302  }
303 
304  std::copy(m_prf.begin(), m_prf.end(), std::back_inserter(result));
305  std::copy(m_wots_priv_key.private_seed().begin(),
306  m_wots_priv_key.private_seed().end(),
307  std::back_inserter(result));
308 
309  return result;
310  }
311 
312 std::unique_ptr<PK_Ops::Signature>
314  const std::string&,
315  const std::string& provider) const
316  {
317  if(provider == "base" || provider.empty())
318  return std::unique_ptr<PK_Ops::Signature>(
319  new XMSS_Signature_Operation(*this));
320 
321  throw Provider_Not_Found(algo_name(), provider);
322  }
323 
324 }
const secure_vector< uint8_t > & public_seed() const override
size_t worker_count() const
Definition: thread_pool.h:43
const XMSS_WOTS_PrivateKey & wots_private_key() const
secure_vector< uint8_t > tree_hash(size_t start_idx, size_t target_node_height, XMSS_Address &adrs)
uint32_t get_tree_index() const
Definition: xmss_address.h:297
void set_ots_address(uint32_t value)
Definition: xmss_address.h:164
void set_tree_height(uint32_t value)
Definition: xmss_address.h:251
void set_ltree_address(uint32_t value)
Definition: xmss_address.h:194
void set_root(const secure_vector< uint8_t > &root)
void set_unused_leaf_index(size_t idx)
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68
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)
virtual std::vector< uint8_t > raw_public_key() const
virtual size_t size() const
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
auto run(F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
Definition: thread_pool.h:57
size_t unused_leaf_index() const
XMSS_PrivateKey(XMSS_Parameters::xmss_algorithm_t xmss_algo_id, RandomNumberGenerator &rng)
size_t size() const override
uint32_t get_tree_height() const
Definition: xmss_address.h:235
void set_type(Type type)
Definition: xmss_address.h:111
std::shared_ptr< Atomic< size_t > > get(const secure_vector< uint8_t > &private_seed, const secure_vector< uint8_t > &prf)
XMSS_WOTS_PublicKey generate_public_key(XMSS_Address &adrs)
Definition: alg_id.cpp:13
const XMSS_WOTS_Parameters & wots_parameters() const
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:72
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &, const std::string &, const std::string &provider) const override
const secure_vector< uint8_t > & private_seed() const
std::string algo_name() const override
secure_vector< uint8_t > raw_private_key() const
size_t element_size() const
XMSS_WOTS_Parameters m_wots_params
static Thread_Pool & global_instance()
Definition: thread_pool.cpp:15
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
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)
void set_tree_index(uint32_t value)
Definition: xmss_address.h:313
void set_private_seed(const secure_vector< uint8_t > &private_seed)
XMSS_Parameters m_xmss_params
MechanismType hash
ots_algorithm_t oid() const