Botan  2.1.0
Crypto and TLS for C++11
xmss_index_registry.h
Go to the documentation of this file.
1 /*
2  * XMSS Index Registry
3  * (C) 2016 Matthias Gierlings
4  *
5  * Botan is released under the Simplified BSD License (see license.txt)
6  **/
7 
8 #ifndef BOTAN_XMSS_INDEX_REGISTRY_H__
9 #define BOTAN_XMSS_INDEX_REGISTRY_H__
10 
11 #include <stdint.h>
12 #include <cstddef>
13 #include <limits>
14 #include <memory>
15 #include <string>
16 #include <botan/hash.h>
17 #include <botan/secmem.h>
18 #include <botan/types.h>
19 #include <botan/atomic.h>
20 #include <botan/mutex.h>
21 
22 namespace Botan {
23 
24 /**
25  * A registry for XMSS private keys, keeps track of the leaf index for
26  * independend copies of the same key.
27  **/
29  {
30  public:
33 
34  /**
35  * Retrieves a handle to the process-wide unique XMSS index registry.
36  *
37  * @return Reference to unique XMSS index registry.
38  **/
40  {
41  static XMSS_Index_Registry self;
42  return self;
43  }
44 
45  /**
46  * Retrieves the last unused leaf index for the private key identified
47  * by private_seed and prf. The leaf index will be updated properly
48  * across independent copies of private_key.
49  *
50  * @param private_seed Part of the unique identifier for an
51  * XMSS_PrivateKey.
52  * @param prf Part of the unique identifier for an XMSS_PrivateKey.
53  *
54  * @return last unused leaf index for private_key.
55  **/
56  std::shared_ptr<Atomic<size_t>>
57  get(const secure_vector<uint8_t>& private_seed,
58  const secure_vector<uint8_t>& prf);
59 
60  private:
61  XMSS_Index_Registry() = default;
62 
63  static const std::string m_index_hash_function;
64 
65  /**
66  * Creates a unique 64-bit id for an XMSS_Private key, by interpreting
67  * the first 64-bit of HASH(PRIVATE_SEED || PRF) as 64 bit integer
68  * value.
69  *
70  * @return unique integral identifier for an XMSS private key.
71  **/
72  uint64_t make_key_id(const secure_vector<uint8_t>& private_seed,
73  const secure_vector<uint8_t>& prf) const;
74 
75  /**
76  * Retrieves the index position of a key within the registry or
77  * max(size_t) if key has not been found.
78  *
79  * @param unique id of the XMSS private key (see make_key_id()).
80  *
81  * @return index position of key or max(size_t) if key not found.
82  **/
83  size_t get(uint64_t id) const;
84 
85  /**
86  * If XMSS_PrivateKey identified by id is already registered, the
87  * position of the according registry entry is returned. If last_unused
88  * is bigger than the last unused index stored for the key identified by
89  * id the unused leaf index for this key is set to last_unused. If no key
90  * matching id is registed yet, an entry of id is added, with the last
91  * unused leaf index initialized to the value of last_unused.
92  *
93  * @last_unused Initial value for the last unused leaf index of the
94  * registered key.
95  *
96  * @return positon of leaf index registry entry for key identified
97  * by id.
98  **/
99  size_t add(uint64_t id, size_t last_unused = 0);
100 
101  std::vector<uint64_t> m_key_ids;
102  std::vector<std::shared_ptr<Atomic<size_t>>> m_leaf_indices;
103  mutex_type m_mutex;
104  };
105 
106 }
107 
108 #endif
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
XMSS_Index_Registry & operator=(const XMSS_Index_Registry &)=delete
Definition: alg_id.cpp:13
static XMSS_Index_Registry & get_instance()