Botan 3.11.0
Crypto and TLS for C&
stateful_key_index_registry.h
Go to the documentation of this file.
1/*
2 * (C) 2016 Matthias Gierlings
3 * 2026 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8#ifndef BOTAN_STATEFUL_KEY_INDEX_REGISTRY_H_
9#define BOTAN_STATEFUL_KEY_INDEX_REGISTRY_H_
10
11#include <botan/mutex.h>
12#include <botan/types.h>
13#include <array>
14#include <map>
15#include <span>
16#include <string_view>
17
18namespace Botan {
19
20/**
21 * A process-wide registry mapping stateful key identity to a shared
22 * atomic counter. Ensures that independent copies of the same key
23 * material (e.g. deserialized separately) share a single leaf index,
24 * preventing catastrophic one-time signature reuse.
25 *
26 * Used by XMSS and HSS-LMS.
27 */
29 public:
30 class KeyId {
31 public:
32 /**
33 * Create a KeyId for some kind of key material
34 *
35 * @param algo_name Algorithm name (ex "XMSS", "HSS-LMS")
36 * @param algo_params Algorithm specific parameters
37 * @param key_material_1 First part of key identifying material
38 * @param key_material_2 Second part of key identifying material (can be omitted)
39 */
40 KeyId(std::string_view algo_name,
41 uint32_t algo_params,
42 std::span<const uint8_t> key_material_1,
43 std::span<const uint8_t> key_material_2);
44
45 KeyId() = default;
46
47 auto operator<=>(const KeyId& other) const = default;
48
49 private:
50 std::array<uint8_t, 32> m_val;
51 };
52
58
59 /**
60 * Retrieve the process-wide instance
61 */
63
64 /**
65 * Return the current counter
66 */
67 uint64_t current_index(const KeyId& key_id);
68
69 /**
70 * Return a new counter
71 */
72 uint64_t reserve_next_index(const KeyId& key_id);
73
74 /**
75 * Set the counter to at least min (but if already higher it will retain its current value)
76 */
77 void set_index_lower_bound(const KeyId& key_id, uint64_t min);
78
79 /**
80 * If the current counter is >= max returns 0, otherwise max - counter
81 */
82 uint64_t remaining_operations(const KeyId& key_id, uint64_t max);
83
84 private:
85 typedef std::map<KeyId, uint64_t> RegistryMap;
86
87 RegistryMap::iterator lookup(const KeyId& key_id);
88
90
91 mutex_type m_mutex;
92 RegistryMap m_registry;
93};
94
95} // namespace Botan
96
97#endif
auto operator<=>(const KeyId &other) const =default
KeyId(std::string_view algo_name, uint32_t algo_params, std::span< const uint8_t > key_material_1, std::span< const uint8_t > key_material_2)
Stateful_Key_Index_Registry & operator=(Stateful_Key_Index_Registry &&)=delete
Stateful_Key_Index_Registry & operator=(const Stateful_Key_Index_Registry &)=delete
Stateful_Key_Index_Registry(Stateful_Key_Index_Registry &&)=delete
void set_index_lower_bound(const KeyId &key_id, uint64_t min)
static Stateful_Key_Index_Registry & global()
Stateful_Key_Index_Registry(const Stateful_Key_Index_Registry &)=delete
uint64_t reserve_next_index(const KeyId &key_id)
uint64_t remaining_operations(const KeyId &key_id, uint64_t max)
noop_mutex mutex_type
Definition mutex.h:37