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