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