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