Botan 2.19.5 Crypto and TLS for C&

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/internal/xmss_index_registry.h>
11#include <botan/hash.h>
12#include <limits>
13
14 namespace Botan {
15
16 const std::string XMSS_Index_Registry::m_index_hash_function = "SHA-256";
17
18uint64_t XMSS_Index_Registry::make_key_id(
19 const secure_vector<uint8_t>& private_seed,
20 const secure_vector<uint8_t>& prf) const
21 {
22 std::unique_ptr<HashFunction> hash =
23 HashFunction::create(m_index_hash_function);
24 BOTAN_ASSERT(hash != nullptr, "XMSS_Index_Registry requires SHA-256");
25 hash->update(private_seed);
26 hash->update(prf);
27 secure_vector<uint8_t> result = hash->final();
28 uint64_t key_id = 0;
29 for(size_t i = 0; i < sizeof(key_id); i++)
30 {
31 key_id = ((key_id << 8) | result[i]);
32 }
33
34 return key_id;
35 }
36
37std::shared_ptr<Atomic<size_t>>
38 XMSS_Index_Registry::get(const secure_vector<uint8_t>& private_seed,
39 const secure_vector<uint8_t>& prf)
40 {
41 size_t pos = get(make_key_id(private_seed, prf));
42
43 if(pos < std::numeric_limits<size_t>::max())
44 {
45 return m_leaf_indices[pos];
46 }
47 else
48 {
49 return m_leaf_indices[add(make_key_id(private_seed, prf))];
50 }
51 }
52
53 size_t XMSS_Index_Registry::get(uint64_t id) const
54 {
55 for(size_t i = 0; i < m_key_ids.size(); i++)
56 {
57 if(m_key_ids[i] == id)
58 {
59 return i;
60 }
61 }
62
63 return std::numeric_limits<size_t>::max();
64 }
65
66 size_t XMSS_Index_Registry::add(uint64_t id, size_t last_unused)
67 {
68 lock_guard_type<mutex_type> lock(m_mutex);
69 size_t pos = get(id);
70 if(pos < m_key_ids.size())
71 {
72 if(last_unused > *(m_leaf_indices[pos]))
73 {
74 m_leaf_indices[pos] = std::make_shared<Atomic<size_t>>(last_unused);
75 }
76 return pos;
77 }
78
79 m_key_ids.push_back(id);
80 m_leaf_indices.push_back(std::make_shared<Atomic<size_t>>(last_unused));
81 return m_key_ids.size() - 1;
82 }
83
84}
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:102
Botan::XMSS_Index_Registry::get
std::shared_ptr< Atomic< size_t > > get(const secure_vector< uint8_t > &private_seed, const secure_vector< uint8_t > &prf)
Definition: xmss_index_registry.cpp:38
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
hash
MechanismType hash
Definition: p11_mechanism.cpp:64