Botan 3.14.1
Crypto and TLS for C&
Botan::Stateful_Key_Index_Registry Class Referencefinal

#include <stateful_key_index_registry.h>

Classes

class  KeyId

Public Member Functions

uint64_t current_index (const KeyId &key_id)
Stateful_Key_Index_Registry & operator= (const Stateful_Key_Index_Registry &)=delete
Stateful_Key_Index_Registry & operator= (Stateful_Key_Index_Registry &&)=delete
uint64_t remaining_operations (const KeyId &key_id)
std::optional< uint64_t > reserve_next_index (const KeyId &key_id)
void set_index_lower_bound (const KeyId &key_id, uint64_t min)
 Stateful_Key_Index_Registry (const Stateful_Key_Index_Registry &)=delete
 Stateful_Key_Index_Registry (Stateful_Key_Index_Registry &&)=delete
 ~Stateful_Key_Index_Registry ()

Static Public Member Functions

static Stateful_Key_Index_Registry & global ()

Detailed Description

A process-wide registry mapping stateful key identity to a shared monotonic counter. Ensures that independent copies of the same key material (e.g. deserialized separately) share a single leaf index, preventing catastrophic one-time signature reuse.

The same key material used with different algorithm parameters is tracked independently, since the parameters are part of the key identity. The maximum operation count is a function of the identity.

Used by XMSS and HSS-LMS.

If this registry or a key identity is inherited across fork(), it fails closed and refuses to issue further indices in the child process.

Definition at line 37 of file stateful_key_index_registry.h.

Constructor & Destructor Documentation

◆ Stateful_Key_Index_Registry() [1/2]

Botan::Stateful_Key_Index_Registry::Stateful_Key_Index_Registry ( const Stateful_Key_Index_Registry & )
delete

◆ Stateful_Key_Index_Registry() [2/2]

Botan::Stateful_Key_Index_Registry::Stateful_Key_Index_Registry ( Stateful_Key_Index_Registry && )
delete

◆ ~Stateful_Key_Index_Registry()

Botan::Stateful_Key_Index_Registry::~Stateful_Key_Index_Registry ( )
default

Member Function Documentation

◆ current_index()

uint64_t Botan::Stateful_Key_Index_Registry::current_index ( const KeyId & key_id)

Return the current counter

Definition at line 110 of file stateful_key_index_registry.cpp.

110 {
111 const lock_guard_type<mutex_type> lock(m_mutex);
112 const auto idx = this->lookup(key_id);
113 return idx->second;
114}
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:145
lock_guard< T > lock_guard_type
Definition mutex.h:58

References Botan::lock().

Referenced by Botan::HSS_LMS_PrivateKeyInternal::to_bytes().

◆ global()

Stateful_Key_Index_Registry & Botan::Stateful_Key_Index_Registry::global ( )
static

Retrieve the process-wide instance

Definition at line 17 of file stateful_key_index_registry.cpp.

17 {
18 static Stateful_Key_Index_Registry g_registry;
19 return g_registry;
20}
Stateful_Key_Index_Registry(const Stateful_Key_Index_Registry &)=delete

References Stateful_Key_Index_Registry().

Referenced by Botan::HSS_LMS_PrivateKeyInternal::remaining_operations(), Botan::HSS_LMS_PrivateKeyInternal::set_idx(), and Botan::HSS_LMS_PrivateKeyInternal::to_bytes().

◆ operator=() [1/2]

Stateful_Key_Index_Registry & Botan::Stateful_Key_Index_Registry::operator= ( const Stateful_Key_Index_Registry & )
delete

◆ operator=() [2/2]

Stateful_Key_Index_Registry & Botan::Stateful_Key_Index_Registry::operator= ( Stateful_Key_Index_Registry && )
delete

◆ remaining_operations()

uint64_t Botan::Stateful_Key_Index_Registry::remaining_operations ( const KeyId & key_id)

If the current counter is >= the key's maximum returns 0, otherwise maximum - counter

Definition at line 134 of file stateful_key_index_registry.cpp.

134 {
135 const lock_guard_type<mutex_type> lock(m_mutex);
136 const uint64_t idx = this->lookup(key_id)->second;
137 const uint64_t max = key_id.max_operations();
138
139 if(idx >= max) {
140 return 0;
141 } else {
142 return max - idx;
143 }
144}

References Botan::lock(), and Botan::Stateful_Key_Index_Registry::KeyId::max_operations().

◆ reserve_next_index()

std::optional< uint64_t > Botan::Stateful_Key_Index_Registry::reserve_next_index ( const KeyId & key_id)

Reserve and return the next counter value, or nullopt if the counter has already reached the key's maximum. The counter never increments past the maximum, so it cannot wrap, and an exhausted key remains exhausted.

Definition at line 116 of file stateful_key_index_registry.cpp.

116 {
117 const lock_guard_type<mutex_type> lock(m_mutex);
118 const auto idx = this->lookup(key_id);
119 const uint64_t cur = idx->second;
120 if(cur >= key_id.max_operations()) {
121 return std::nullopt;
122 }
123 idx->second = cur + 1;
124 return cur;
125}

References Botan::lock(), and Botan::Stateful_Key_Index_Registry::KeyId::max_operations().

◆ set_index_lower_bound()

void Botan::Stateful_Key_Index_Registry::set_index_lower_bound ( const KeyId & key_id,
uint64_t min )

Set the counter to at least min (but if already higher it will retain its current value)

Definition at line 127 of file stateful_key_index_registry.cpp.

127 {
128 BOTAN_ARG_CHECK(min <= key_id.max_operations(), "Index lower bound exceeds maximum operation count");
129 const lock_guard_type<mutex_type> lock(m_mutex);
130 const auto idx = this->lookup(key_id);
131 idx->second = std::max(idx->second, min);
132}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, Botan::lock(), and Botan::Stateful_Key_Index_Registry::KeyId::max_operations().

Referenced by Botan::HSS_LMS_PrivateKeyInternal::set_idx().


The documentation for this class was generated from the following files: