8#include <botan/internal/stateful_key_index_registry.h>
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/hash.h>
27 std::span<const uint8_t> algo_params,
29 std::span<const uint8_t> key_material_1,
30 std::span<const uint8_t> key_material_2) :
34 hash->update(
"Botan Stateful_Key_Index_Registry KeyID");
35 hash->update_be(
static_cast<uint64_t
>(algo_name.size()));
36 hash->update(algo_name);
37 hash->update_be(
static_cast<uint64_t
>(algo_params.size()));
38 hash->update(algo_params);
39 hash->update_be(
static_cast<uint64_t
>(key_material_1.size()));
40 hash->update(key_material_1);
41 hash->update_be(
static_cast<uint64_t
>(key_material_2.size()));
42 hash->update(key_material_2);
50Stateful_Key_Index_Registry::RegistryMap::iterator Stateful_Key_Index_Registry::lookup(
const KeyId& key_id) {
51 if(this->fork_detected(key_id)) {
52 throw Invalid_State(
"Stateful key index registry cannot be used after fork");
55 auto [i, inserted] = m_registry.emplace(key_id, 0);
57 if(!inserted && i->first.max_operations() != key_id.
max_operations()) {
58 throw Internal_Error(
"Stateful key was already registered with a different maximum operation count");
94bool Stateful_Key_Index_Registry::fork_detected(
const KeyId& key_id) {
103 if(m_fork_detector.fork_detected() || key_id.m_fork_detector.fork_detected()) {
104 m_fork_detected =
true;
107 return m_fork_detected;
112 const auto idx = this->lookup(key_id);
118 const auto idx = this->lookup(key_id);
119 const uint64_t cur = idx->second;
123 idx->second = cur + 1;
130 const auto idx = this->lookup(key_id);
131 idx->second = std::max(idx->second, min);
136 const uint64_t idx = this->lookup(key_id)->second;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
uint64_t max_operations() const
uint64_t current_index(const KeyId &key_id)
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)
static Stateful_Key_Index_Registry & global()
Stateful_Key_Index_Registry(const Stateful_Key_Index_Registry &)=delete
~Stateful_Key_Index_Registry()
secure_vector< T > lock(const std::vector< T > &in)
lock_guard< T > lock_guard_type