Botan  2.7.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  * draft-itrf-cfrg-xmss-hash-based-signatures-06
9  * Release: July 2016.
10  * https://datatracker.ietf.org/doc/
11  * draft-irtf-cfrg-xmss-hash-based-signatures/?include_text=1
12  *
13  * (C) 2016,2017 Matthias Gierlings
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 #include <cmath>
21 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
22  #include <thread>
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  BOTAN_ASSERT(sizeof(size_t) >= std::ceil(
34  static_cast<float>(XMSS_PublicKey::m_xmss_params.tree_height()) / 8.f),
35  "System type \"size_t\" not big enough to support"
36  " leaf index.");
37 
38  if(raw_key.size() != size())
39  {
40  throw Integrity_Failure("Invalid XMSS private key size detected.");
41  }
42 
43  // extract & copy unused leaf index from raw_key.
44  uint64_t unused_leaf = 0;
45  auto begin = (raw_key.begin() + XMSS_PublicKey::size());
46  auto end = raw_key.begin() + XMSS_PublicKey::size() + sizeof(uint64_t);
47 
48  for(auto& i = begin; i != end; i++)
49  {
50  unused_leaf = ((unused_leaf << 8) | *i);
51  }
52 
53  if(unused_leaf >= (1ull << XMSS_PublicKey::m_xmss_params.tree_height()))
54  {
55  throw Integrity_Failure("XMSS private key leaf index out of "
56  "bounds.");
57  }
58 
59  begin = end;
61  m_prf.clear();
62  m_prf.reserve(XMSS_PublicKey::m_xmss_params.element_size());
63  std::copy(begin, end, std::back_inserter(m_prf));
64 
65  begin = end;
66  end = begin + m_wots_params.element_size();
67  m_wots_priv_key.set_private_seed(secure_vector<uint8_t>(begin, end));
68  set_unused_leaf_index(static_cast<size_t>(unused_leaf));
69  }
70 
74  : XMSS_PublicKey(xmss_algo_id, rng),
75  XMSS_Common_Ops(xmss_algo_id),
76  m_wots_priv_key(XMSS_PublicKey::m_xmss_params.ots_oid(),
77  public_seed(),
78  rng),
79  m_prf(rng.random_vec(XMSS_PublicKey::m_xmss_params.element_size())),
80  m_index_reg(XMSS_Index_Registry::get_instance())
81  {
82  XMSS_Address adrs;
84  XMSS_PublicKey::m_xmss_params.tree_height(),
85  adrs));
86  }
87 
89 XMSS_PrivateKey::tree_hash(size_t start_idx,
90  size_t target_node_height,
91  XMSS_Address& adrs)
92  {
93  BOTAN_ASSERT((start_idx % (1 << target_node_height)) == 0,
94  "Start index must be divisible by 2^{target node height}.");
95 
96 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
97  // dertermine number of parallel tasks to split the tree_hashing into.
98  size_t split_level = std::min(
99  {
100  target_node_height,
101  static_cast<size_t>(
102  std::ceil(std::log2(XMSS_Tools::max_threads())))
103  });
104 
105  // skip parallelization overhead for leaf nodes.
106  if(split_level == 0)
107  {
108 #endif
109  secure_vector<uint8_t> result;
110  tree_hash_subtree(result, start_idx, target_node_height, adrs);
111  return result;
112 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
113  }
114 
115  size_t subtrees = 1 << split_level;
116  size_t last_idx = static_cast<size_t>(1 << (target_node_height)) + start_idx;
117  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::thread> threads;
130  threads.reserve(subtrees);
131 
132  // Calculate multiple subtrees in parallel.
133  for(size_t i = 0; i < subtrees; i++)
134  {
135  using tree_hash_subtree_fn_t =
137  size_t,
138  size_t,
139  XMSS_Address&,
140  XMSS_Hash&);
141 
142  threads.emplace_back(
143  std::thread(
144  static_cast<tree_hash_subtree_fn_t>(
145  &XMSS_PrivateKey::tree_hash_subtree),
146  this,
147  std::ref(nodes[i]),
148  start_idx + i * offs,
149  target_node_height - split_level,
150  std::ref(node_addresses[i]),
151  std::ref(xmss_hash[i])));
152  }
153 
154  for(auto& t : threads)
155  {
156  t.join();
157  }
158 
159  threads.clear();
160 
161  // Parallelize the top tree levels horizontally
162  while(level-- > 1)
163  {
164  std::vector<secure_vector<uint8_t>> ro_nodes(
165  nodes.begin(), nodes.begin() + (1 << (level+1)));
166 
167  for(size_t i = 0; i < (1U << level); 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  threads.emplace_back(
181  std::thread(
182  static_cast<rnd_tree_hash_fn_t>(
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  for(auto &t : threads)
193  {
194  t.join();
195  }
196  threads.clear();
197  }
198 
199  // Avoid creation an extra thread to calculate root node.
200  node_addresses[0].set_tree_height(target_node_height - 1);
201  node_addresses[0].set_tree_index(
202  (node_addresses[1].get_tree_index() - 1) >> 1);
203  randomize_tree_hash(nodes[0],
204  nodes[0],
205  nodes[1],
206  node_addresses[0],
207  this->public_seed());
208  return nodes[0];
209 #endif
210  }
211 
212 void
213 XMSS_PrivateKey::tree_hash_subtree(secure_vector<uint8_t>& result,
214  size_t start_idx,
215  size_t target_node_height,
216  XMSS_Address& adrs,
217  XMSS_Hash& hash)
218  {
219  const secure_vector<uint8_t>& seed = this->public_seed();
220 
221  std::vector<secure_vector<uint8_t>> nodes(
222  target_node_height + 1,
224 
225  // node stack, holds all nodes on stack and one extra "pending" node. This
226  // temporary node referred to as "node" in the XMSS standard document stays
227  // a pending element, meaning it is not regarded as element on the stack
228  // until level is increased.
229  std::vector<uint8_t> node_levels(target_node_height + 1);
230 
231  uint8_t level = 0; // current level on the node stack.
232  XMSS_WOTS_PublicKey pk(m_wots_priv_key.wots_parameters().oid(), seed);
233  size_t last_idx = static_cast<size_t>(1 << target_node_height) + start_idx;
234 
235  for(size_t i = start_idx; i < last_idx; i++)
236  {
238  adrs.set_ots_address(i);
240  pk,
241  // getWOTS_SK(SK, s + i), reference implementation uses adrs
242  // instead of zero padded index s + i.
243  this->wots_private_key().at(adrs, hash),
244  adrs,
245  hash);
247  adrs.set_ltree_address(i);
248  create_l_tree(nodes[level], pk, adrs, seed, hash);
249  node_levels[level] = 0;
250 
252  adrs.set_tree_height(0);
253  adrs.set_tree_index(i);
254 
255  while(level > 0 && node_levels[level] ==
256  node_levels[level - 1])
257  {
258  adrs.set_tree_index(((adrs.get_tree_index() - 1) >> 1));
259  randomize_tree_hash(nodes[level - 1],
260  nodes[level - 1],
261  nodes[level],
262  adrs,
263  seed,
264  hash);
265  node_levels[level - 1]++;
266  level--; //Pop stack top element
267  adrs.set_tree_height(adrs.get_tree_height() + 1);
268  }
269  level++; //push temporary node to stack
270  }
271  result = nodes[level - 1];
272  }
273 
274 std::shared_ptr<Atomic<size_t>>
275 XMSS_PrivateKey::recover_global_leaf_index() const
276  {
277  BOTAN_ASSERT(m_wots_priv_key.private_seed().size() ==
280  "Trying to retrieve index for partially initialized "
281  "key.");
282  return m_index_reg.get(m_wots_priv_key.private_seed(),
283  m_prf);
284  }
285 
287  {
288  std::vector<uint8_t> pk { raw_public_key() };
289  secure_vector<uint8_t> result(pk.begin(), pk.end());
290  result.reserve(size());
291 
292  for(int i = 7; i >= 0; i--)
293  {
294  result.push_back(
295  static_cast<uint8_t>(
296  static_cast<uint64_t>(unused_leaf_index()) >> 8 * i));
297  }
298 
299  std::copy(m_prf.begin(), m_prf.end(), std::back_inserter(result));
300  std::copy(m_wots_priv_key.private_seed().begin(),
301  m_wots_priv_key.private_seed().end(),
302  std::back_inserter(result));
303 
304  return result;
305  }
306 
307 std::unique_ptr<PK_Ops::Signature>
309  const std::string&,
310  const std::string& provider) const
311  {
312  if(provider == "base" || provider.empty())
313  return std::unique_ptr<PK_Ops::Signature>(
314  new XMSS_Signature_Operation(*this));
315 
316  throw Provider_Not_Found(algo_name(), provider);
317  }
318 
319 }
const secure_vector< uint8_t > & public_seed() const override
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
static size_t max_threads()
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)
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:43
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:95
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
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
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