Botan  2.4.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 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 #include <cmath>
10 
11 namespace Botan {
12 
14  const secure_vector<uint8_t>& raw_sig)
15  : m_leaf_idx(0), m_randomness(0, 0x00), m_tree_sig()
16  {
17  BOTAN_ASSERT(sizeof(size_t) >= std::ceil(static_cast<float>(
18  (XMSS_Parameters(oid)).tree_height()) / 8.f),
19  "System type \"size_t\" not big enough to support"
20  " leaf index.");
21 
22  XMSS_Parameters xmss_params(oid);
23  uint64_t leaf_idx = 0;
24  for(size_t i = 0; i < 8; i++)
25  { leaf_idx = ((leaf_idx << 8) | raw_sig[i]); }
26 
27  if(leaf_idx >= (1ull << (xmss_params.tree_height() - 1)))
28  {
29  throw Integrity_Failure("XMSS signature leaf index out of "
30  "bounds.");
31  }
32  m_leaf_idx = static_cast<size_t>(leaf_idx);
33 
34  auto begin = raw_sig.begin() + sizeof(uint64_t);
35  auto end = begin + xmss_params.element_size();
36  std::copy(begin, end, std::back_inserter(m_randomness));
37 
38  for(size_t i = 0; i < xmss_params.len(); i++)
39  {
40  begin = end;
41  end = begin + xmss_params.element_size();
42  m_tree_sig.ots_signature().push_back(secure_vector<uint8_t>(0));
43  m_tree_sig.ots_signature().back().reserve(
44  xmss_params.element_size());
45  std::copy(begin,
46  end,
47  std::back_inserter(m_tree_sig.ots_signature().back()));
48  }
49 
50  for(size_t i = 0; i < xmss_params.tree_height(); i++)
51  {
52  begin = end;
53  end = begin + xmss_params.element_size();
54  m_tree_sig.authentication_path().push_back(secure_vector<uint8_t>(0));
55  m_tree_sig.authentication_path().back().reserve(
56  xmss_params.element_size());
57  std::copy(begin,
58  end,
59  std::back_inserter(m_tree_sig.authentication_path().back()));
60  }
61  }
62 
64  {
66  {
67  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 56U),
68  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 48U),
69  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 40U),
70  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 32U),
71  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 24U),
72  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 16U),
73  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx) >> 8U),
74  static_cast<uint8_t>(static_cast<uint64_t>(m_leaf_idx))
75  };
76 
77  std::copy(m_randomness.begin(),
78  m_randomness.end(),
79  std::back_inserter(result));
80 
81  for(const auto& sig : tree().ots_signature())
82  {
83  std::copy(sig.begin(),
84  sig.end(),
85  std::back_inserter(result));
86  }
87 
88  for(const auto& auth : tree().authentication_path())
89  {
90  std::copy(auth.begin(),
91  auth.end(),
92  std::back_inserter(result));
93  }
94  return result;
95  }
96 
97 }
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
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
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:88