Coverage Report

Created: 2025-04-11 06:34

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