Botan 3.11.0
Crypto and TLS for C&
stateful_key_index_registry.cpp
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#include <botan/internal/stateful_key_index_registry.h>
9
10#include <botan/assert.h>
11#include <botan/hash.h>
12
13namespace Botan {
14
19
21
23
25 uint32_t algo_params,
26 std::span<const uint8_t> key_material_1,
27 std::span<const uint8_t> key_material_2) :
28 m_val() {
29 auto hash = HashFunction::create_or_throw("SHA-256");
30
31 hash->update("Botan Stateful_Key_Index_Registry KeyID");
32 hash->update_be(static_cast<uint64_t>(algo_name.size()));
33 hash->update(algo_name);
34 hash->update_be(algo_params);
35 hash->update_be(static_cast<uint64_t>(key_material_1.size()));
36 hash->update(key_material_1);
37 hash->update_be(static_cast<uint64_t>(key_material_2.size()));
38 hash->update(key_material_2);
39
40 BOTAN_ASSERT_NOMSG(hash->output_length() == m_val.size());
41
42 hash->final(m_val);
43}
44
45// Lock must be held while this function is called
46Stateful_Key_Index_Registry::RegistryMap::iterator Stateful_Key_Index_Registry::lookup(const KeyId& key_id) {
47 auto [i, _inserted] = m_registry.emplace(key_id, 0);
48 return i;
49}
50
52 const lock_guard_type<mutex_type> lock(m_mutex);
53 auto idx = this->lookup(key_id);
54 return idx->second;
55}
56
58 const lock_guard_type<mutex_type> lock(m_mutex);
59 auto idx = this->lookup(key_id);
60 const uint64_t cur = idx->second;
61 idx->second += 1;
62 return cur;
63}
64
66 const lock_guard_type<mutex_type> lock(m_mutex);
67 auto idx = this->lookup(key_id);
68 idx->second = std::max(idx->second, min);
69}
70
71uint64_t Stateful_Key_Index_Registry::remaining_operations(const KeyId& key_id, uint64_t max) {
72 const lock_guard_type<mutex_type> lock(m_mutex);
73 const uint64_t idx = this->lookup(key_id)->second;
74
75 if(idx >= max) {
76 return 0;
77 } else {
78 return max - idx;
79 }
80}
81
82} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
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)
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:80
lock_guard< T > lock_guard_type
Definition mutex.h:55