Botan  2.1.0
Crypto and TLS for C++11
xmss_index_registry.cpp
Go to the documentation of this file.
1 /*
2  * XMSS Index Registry
3  * A registry for XMSS private keys, keeps track of the leaf index for
4  * independend copies of the same key.
5  * (C) 2016 Matthias Gierlings
6  *
7  * Botan is released under the Simplified BSD License (see license.txt)
8  **/
9 
10 #include <botan/xmss_index_registry.h>
11 
12 namespace Botan {
13 
14 const std::string XMSS_Index_Registry::m_index_hash_function = "SHA-256";
15 
16 uint64_t XMSS_Index_Registry::make_key_id(
17  const secure_vector<uint8_t>& private_seed,
18  const secure_vector<uint8_t>& prf) const
19  {
20  std::unique_ptr<HashFunction> hash =
21  HashFunction::create(m_index_hash_function);
22  BOTAN_ASSERT(hash != nullptr, "XMSS_Index_Registry requires SHA-256");
23  hash->update(private_seed);
24  hash->update(prf);
25  secure_vector<uint8_t> result = hash->final();
26  uint64_t key_id = 0;
27  for(size_t i = 0; i < sizeof(key_id); i++)
28  {
29  key_id = ((key_id << 8) | result[i]);
30  }
31 
32  return key_id;
33  }
34 
35 std::shared_ptr<Atomic<size_t>>
37  const secure_vector<uint8_t>& prf)
38  {
39  size_t pos = get(make_key_id(private_seed, prf));
40 
42  {
43  return m_leaf_indices[pos];
44  }
45  else
46  {
47  return m_leaf_indices[add(make_key_id(private_seed, prf))];
48  }
49  }
50 
51 size_t XMSS_Index_Registry::get(uint64_t id) const
52  {
53  for(size_t i = 0; i < m_key_ids.size(); i++)
54  {
55  if(m_key_ids[i] == id)
56  {
57  return i;
58  }
59  }
60 
62  }
63 
64 size_t XMSS_Index_Registry::add(uint64_t id, size_t last_unused)
65  {
66  lock_guard_type<mutex_type> lock(m_mutex);
67  size_t pos = get(id);
68  if(pos < m_key_ids.size())
69  {
70  if(last_unused > *(m_leaf_indices[pos]))
71  {
72  m_leaf_indices[pos] = std::make_shared<Atomic<size_t>>(last_unused);
73  }
74  return pos;
75  }
76 
77  m_key_ids.push_back(id);
78  m_leaf_indices.push_back(std::make_shared<Atomic<size_t>>(last_unused));
79  return m_key_ids.size() - 1;
80  }
81 
82 }
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
std::shared_ptr< Atomic< size_t > > get(const secure_vector< uint8_t > &private_seed, const secure_vector< uint8_t > &prf)
Definition: alg_id.cpp:13
T max(T a, T b)
Definition: ct_utils.h:173
MechanismType hash