Botan  2.15.0
Crypto and TLS for C++11
xmss_signature.cpp
Go to the documentation of this file.
1 /*
2  * XMSS Signature
3  * (C) 2016,2017,2018 Matthias Gierlings
4  *
5  * Botan is released under the Simplified BSD License (see license.txt)
6  **/
7 
8 #include <botan/internal/xmss_signature.h>
9 
10 namespace Botan {
11 
13  const secure_vector<uint8_t>& raw_sig)
14  : m_leaf_idx(0), m_randomness(0, 0x00), m_tree_sig()
15  {
16  XMSS_Parameters xmss_params(oid);
17 
18  if(raw_sig.size() != (xmss_params.len() + xmss_params.tree_height() + 1)
19  * xmss_params.element_size() + sizeof(uint32_t))
20  {
21  throw Decoding_Error("XMSS signature size invalid.");
22  }
23 
24  for(size_t i = 0; i < 4; i++)
25  { m_leaf_idx = ((m_leaf_idx << 8) | raw_sig[i]); }
26 
27  if(m_leaf_idx >= (1ull << xmss_params.tree_height()))
28  {
29  throw Decoding_Error("XMSS signature leaf index out of bounds.");
30  }
31 
32  auto begin = raw_sig.begin() + sizeof(uint32_t);
33  auto end = begin + xmss_params.element_size();
34  std::copy(begin, end, std::back_inserter(m_randomness));
35 
36  for(size_t i = 0; i < xmss_params.len(); i++)
37  {
38  begin = end;
39  end = begin + xmss_params.element_size();
40  m_tree_sig.ots_signature().push_back(secure_vector<uint8_t>(0));
41  m_tree_sig.ots_signature().back().reserve(
42  xmss_params.element_size());
43  std::copy(begin,
44  end,
45  std::back_inserter(m_tree_sig.ots_signature().back()));
46  }
47 
48  for(size_t i = 0; i < xmss_params.tree_height(); i++)
49  {
50  begin = end;
51  end = begin + xmss_params.element_size();
52  m_tree_sig.authentication_path().push_back(secure_vector<uint8_t>(0));
53  m_tree_sig.authentication_path().back().reserve(
54  xmss_params.element_size());
55  std::copy(begin,
56  end,
57  std::back_inserter(m_tree_sig.authentication_path().back()));
58  }
59  }
60 
62  {
64  {
65  static_cast<uint8_t>(m_leaf_idx >> 24U),
66  static_cast<uint8_t>(m_leaf_idx >> 16U),
67  static_cast<uint8_t>(m_leaf_idx >> 8U),
68  static_cast<uint8_t>(m_leaf_idx)
69  };
70 
71  std::copy(m_randomness.begin(),
72  m_randomness.end(),
73  std::back_inserter(result));
74 
75  for(const auto& sig : tree().ots_signature())
76  {
77  std::copy(sig.begin(),
78  sig.end(),
79  std::back_inserter(result));
80  }
81 
82  for(const auto& auth : tree().authentication_path())
83  {
84  std::copy(auth.begin(),
85  auth.end(),
86  std::back_inserter(result));
87  }
88  return result;
89  }
90 
91 }
XMSS_Signature(XMSS_Parameters::xmss_algorithm_t oid, const secure_vector< uint8_t > &raw_sig)
secure_vector< uint8_t > bytes() const
const XMSS_WOTS_PublicKey::TreeSignature & tree() const
size_t tree_height() const
const wots_keysig_t & authentication_path() const
Definition: alg_id.cpp:13
const wots_keysig_t & ots_signature() const
size_t element_size() const
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65