8#include <botan/internal/stateful_key_index_registry.h>
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/hash.h>
13#include <botan/internal/os_utils.h>
28 std::span<const uint8_t> algo_params,
30 std::span<const uint8_t> key_material_1,
31 std::span<const uint8_t> key_material_2) :
35 hash->update(
"Botan Stateful_Key_Index_Registry KeyID");
36 hash->update_be(
static_cast<uint64_t
>(algo_name.size()));
37 hash->update(algo_name);
38 hash->update_be(
static_cast<uint64_t
>(algo_params.size()));
39 hash->update(algo_params);
40 hash->update_be(
static_cast<uint64_t
>(key_material_1.size()));
41 hash->update(key_material_1);
42 hash->update_be(
static_cast<uint64_t
>(key_material_2.size()));
43 hash->update(key_material_2);
51Stateful_Key_Index_Registry::RegistryMap::iterator Stateful_Key_Index_Registry::lookup(
const KeyId& key_id) {
52 if(this->fork_detected(key_id)) {
53 throw Invalid_State(
"Stateful key index registry cannot be used after fork");
56 auto [i, inserted] = m_registry.emplace(key_id, 0);
58 if(!inserted && i->first.max_operations() != key_id.
max_operations()) {
59 throw Internal_Error(
"Stateful key was already registered with a different maximum operation count");
95bool Stateful_Key_Index_Registry::fork_detected(
const KeyId& key_id) {
106 if(current_process_id == 0) {
114 if((m_process_id != 0 && m_process_id != current_process_id) ||
115 (key_id.m_process_id != 0 && key_id.m_process_id != current_process_id)) {
116 m_fork_detected =
true;
119 return m_fork_detected;
124 auto idx = this->lookup(key_id);
130 auto idx = this->lookup(key_id);
131 const uint64_t cur = idx->second;
135 idx->second = cur + 1;
142 auto idx = this->lookup(key_id);
143 idx->second = std::max(idx->second, min);
148 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()
uint32_t BOTAN_TEST_API get_process_id()
secure_vector< T > lock(const std::vector< T > &in)
lock_guard< T > lock_guard_type