/src/botan/src/lib/pubkey/xmss/xmss_index_registry.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #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 | | |
18 | | uint64_t XMSS_Index_Registry::make_key_id( |
19 | | const secure_vector<uint8_t>& private_seed, |
20 | | const secure_vector<uint8_t>& prf) const |
21 | 0 | { |
22 | 0 | std::unique_ptr<HashFunction> hash = |
23 | 0 | HashFunction::create(m_index_hash_function); |
24 | 0 | BOTAN_ASSERT(hash != nullptr, "XMSS_Index_Registry requires SHA-256"); |
25 | 0 | hash->update(private_seed); |
26 | 0 | hash->update(prf); |
27 | 0 | secure_vector<uint8_t> result = hash->final(); |
28 | 0 | uint64_t key_id = 0; |
29 | 0 | for(size_t i = 0; i < sizeof(key_id); i++) |
30 | 0 | { |
31 | 0 | key_id = ((key_id << 8) | result[i]); |
32 | 0 | } |
33 | 0 |
|
34 | 0 | return key_id; |
35 | 0 | } |
36 | | |
37 | | std::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 | 0 | { |
41 | 0 | size_t pos = get(make_key_id(private_seed, prf)); |
42 | 0 |
|
43 | 0 | if(pos < std::numeric_limits<size_t>::max()) |
44 | 0 | { |
45 | 0 | return m_leaf_indices[pos]; |
46 | 0 | } |
47 | 0 | else |
48 | 0 | { |
49 | 0 | return m_leaf_indices[add(make_key_id(private_seed, prf))]; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | size_t XMSS_Index_Registry::get(uint64_t id) const |
54 | 0 | { |
55 | 0 | for(size_t i = 0; i < m_key_ids.size(); i++) |
56 | 0 | { |
57 | 0 | if(m_key_ids[i] == id) |
58 | 0 | { |
59 | 0 | return i; |
60 | 0 | } |
61 | 0 | } |
62 | 0 |
|
63 | 0 | return std::numeric_limits<size_t>::max(); |
64 | 0 | } |
65 | | |
66 | | size_t XMSS_Index_Registry::add(uint64_t id, size_t last_unused) |
67 | 0 | { |
68 | 0 | lock_guard_type<mutex_type> lock(m_mutex); |
69 | 0 | size_t pos = get(id); |
70 | 0 | if(pos < m_key_ids.size()) |
71 | 0 | { |
72 | 0 | if(last_unused > *(m_leaf_indices[pos])) |
73 | 0 | { |
74 | 0 | m_leaf_indices[pos] = std::make_shared<Atomic<size_t>>(last_unused); |
75 | 0 | } |
76 | 0 | return pos; |
77 | 0 | } |
78 | 0 |
|
79 | 0 | m_key_ids.push_back(id); |
80 | 0 | m_leaf_indices.push_back(std::make_shared<Atomic<size_t>>(last_unused)); |
81 | 0 | return m_key_ids.size() - 1; |
82 | 0 | } |
83 | | |
84 | | } |