19#include <botan/xmss.h>
21#include <botan/ber_dec.h>
22#include <botan/der_enc.h>
24#include <botan/internal/buffer_slicer.h>
25#include <botan/internal/concat_util.h>
26#include <botan/internal/int_utils.h>
27#include <botan/internal/loadstor.h>
28#include <botan/internal/stateful_key_index_registry.h>
29#include <botan/internal/xmss_common_ops.h>
30#include <botan/internal/xmss_hash.h>
31#include <botan/internal/xmss_signature_operation.h>
33#if defined(BOTAN_HAS_THREAD_UTILS)
34 #include <botan/internal/thread_pool.h>
47 if(key_bits.size() == xmss_params.raw_private_key_size() ||
48 key_bits.size() == xmss_params.raw_legacy_private_key_size()) {
49 raw_key.assign(key_bits.begin(), key_bits.end());
59class XMSS_PrivateKey_Internal final {
63 RandomNumberGenerator& rng) :
64 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
65 m_wots_params(m_xmss_params.wots_parameters()),
66 m_wots_derivation_method(wots_derivation_method),
67 m_prf(rng.random_vec(m_xmss_params.element_size())),
68 m_private_seed(rng.random_vec(m_xmss_params.element_size())),
69 m_keyid(Stateful_Key_Index_Registry::KeyId(
"XMSS", m_xmss_params.oid(), m_private_seed, m_prf)) {}
75 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
76 m_wots_params(m_xmss_params.wots_parameters()),
77 m_wots_derivation_method(wots_derivation_method),
78 m_prf(std::move(prf)),
79 m_private_seed(std::move(private_seed)),
80 m_keyid(Stateful_Key_Index_Registry::KeyId(
"XMSS", m_xmss_params.oid(), m_private_seed, m_prf)) {}
83 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
84 m_wots_params(m_xmss_params.wots_parameters()),
94 static_assert(
sizeof(size_t) >= 4,
"size_t is big enough to support leaf index");
98 if(raw_key.size() != m_xmss_params.raw_private_key_size() &&
99 raw_key.size() != m_xmss_params.raw_legacy_private_key_size()) {
100 throw Decoding_Error(
"Invalid XMSS private key size");
103 BufferSlicer s(raw_key);
106 s.skip(m_xmss_params.raw_public_key_size());
108 auto unused_leaf_bytes = s.take(
sizeof(uint32_t));
110 if(unused_leaf >= (1ULL << m_xmss_params.tree_height())) {
111 throw Decoding_Error(
"XMSS private key leaf index out of bounds");
114 m_prf = s.copy_as_secure_vector(m_xmss_params.element_size());
115 m_private_seed = s.copy_as_secure_vector(m_xmss_params.element_size());
117 m_keyid = Stateful_Key_Index_Registry::KeyId(
"XMSS", m_xmss_params.oid(), m_private_seed, m_prf);
120 set_unused_leaf_index(unused_leaf);
124 m_wots_derivation_method =
131 std::vector<uint8_t> unused_index(4);
132 store_be(
static_cast<uint32_t
>(unused_leaf_index()), unused_index.data());
134 std::vector<uint8_t> wots_derivation_method;
135 wots_derivation_method.push_back(
static_cast<uint8_t
>(m_wots_derivation_method));
138 raw_public_key, unused_index, m_prf, m_private_seed, wots_derivation_method);
145 const XMSS_WOTS_Parameters& wots_parameters() {
return m_wots_params; }
149 void set_unused_leaf_index(
size_t idx) {
150 if(idx >= (1ULL << m_xmss_params.tree_height())) {
151 throw Decoding_Error(
"XMSS private key leaf index out of bounds");
157 size_t reserve_unused_leaf_index() {
159 if(idx >= m_xmss_params.total_number_of_signatures()) {
160 throw Decoding_Error(
"XMSS private key, one time signatures exhausted");
163 return static_cast<size_t>(idx);
166 size_t unused_leaf_index()
const {
171 uint64_t remaining_signatures()
const {
172 const size_t max = m_xmss_params.total_number_of_signatures();
177 XMSS_Parameters m_xmss_params;
178 XMSS_WOTS_Parameters m_wots_params;
183 Stateful_Key_Index_Registry::KeyId m_keyid;
188 m_private(std::make_shared<XMSS_PrivateKey_Internal>(
xmss_parameters().oid(), key_bits)) {}
208 m_private(std::make_shared<XMSS_PrivateKey_Internal>(
210 m_private->set_unused_leaf_index(idx_leaf);
212 "XMSS: unexpected byte length of PRF value");
214 "XMSS: unexpected byte length of private seed");
218 size_t target_node_height,
222 BOTAN_ASSERT((start_idx % (
static_cast<size_t>(1) << target_node_height)) == 0,
223 "Start index must be divisible by 2^{target node height}.");
225#if defined(BOTAN_HAS_THREAD_UTILS)
230 const size_t split_level = std::min(target_node_height, thread_pool.
worker_count());
233 if(split_level == 0) {
236 tree_hash_subtree(result, start_idx, target_node_height, subtree_addr, hash);
240 const size_t subtrees =
static_cast<size_t>(1) << split_level;
241 const size_t last_idx = (
static_cast<size_t>(1) << (target_node_height)) + start_idx;
242 const size_t offs = (last_idx - start_idx) / subtrees;
244 uint8_t level =
static_cast<uint8_t
>(split_level);
247 "Number of worker threads in tree_hash need to divide range "
248 "of calculated nodes.");
251 std::vector<XMSS_Address> node_addresses(subtrees, adrs);
252 std::vector<XMSS_Hash> xmss_hash(subtrees, hash);
253 std::vector<std::future<void>> work;
256 for(
size_t i = 0; i < subtrees; i++) {
257 using tree_hash_subtree_fn_t =
260 const tree_hash_subtree_fn_t work_fn = &XMSS_PrivateKey::tree_hash_subtree;
262 work.push_back(thread_pool.
run(work_fn,
265 start_idx + i * offs,
266 target_node_height - split_level,
267 std::ref(node_addresses[i]),
268 std::ref(xmss_hash[i])));
271 for(
auto& w : work) {
278 std::vector<secure_vector<uint8_t>> ro_nodes(nodes.begin(),
279 nodes.begin() + (
static_cast<size_t>(1) << (level + 1)));
281 for(
size_t i = 0; i < (static_cast<size_t>(1) << level); i++) {
284 node_addresses[i].set_tree_height(
static_cast<uint32_t
>(target_node_height - (level + 1)));
285 node_addresses[i].set_tree_index((node_addresses[2 * i + 1].get_tree_index() - 1) >> 1);
289 std::cref(ro_nodes[2 * i]),
290 std::cref(ro_nodes[2 * i + 1]),
292 std::cref(this->public_seed()),
293 std::ref(xmss_hash[i]),
297 for(
auto& w : work) {
304 node_addresses[0].set_tree_height(
static_cast<uint32_t
>(target_node_height - 1));
305 node_addresses[0].set_tree_index((node_addresses[1].get_tree_index() - 1) >> 1);
311 XMSS_Address subtree_addr(adrs);
312 tree_hash_subtree(result, start_idx, target_node_height, subtree_addr, hash);
319 size_t target_node_height,
324 std::vector<secure_vector<uint8_t>> nodes(target_node_height + 1,
331 std::vector<uint8_t> node_levels(target_node_height + 1);
334 const size_t last_idx = (
static_cast<size_t>(1) << target_node_height) + start_idx;
336 for(
size_t i = start_idx; i < last_idx; i++) {
338 adrs.set_ots_address(
static_cast<uint32_t
>(i));
340 const XMSS_WOTS_PublicKey pk = this->wots_public_key_for(adrs, hash);
343 adrs.set_ltree_address(
static_cast<uint32_t
>(i));
345 node_levels[level] = 0;
348 adrs.set_tree_height(0);
349 adrs.set_tree_index(
static_cast<uint32_t
>(i));
351 while(level > 0 && node_levels[level] == node_levels[level - 1]) {
352 adrs.set_tree_index(((adrs.get_tree_index() - 1) >> 1));
354 nodes[level - 1], nodes[level - 1], nodes[level], adrs, seed, hash,
xmss_parameters());
355 node_levels[level - 1]++;
357 adrs.set_tree_height(adrs.get_tree_height() + 1);
361 result = nodes[level - 1];
365 const auto private_key = wots_private_key_for(adrs, hash);
366 return XMSS_WOTS_PublicKey(m_private->wots_parameters(),
public_seed(), private_key, adrs, hash);
372 return XMSS_WOTS_PrivateKey(
373 m_private->wots_parameters(),
public_seed(), m_private->private_seed(), adrs, hash);
375 return XMSS_WOTS_PrivateKey(m_private->wots_parameters(), m_private->private_seed(), adrs, hash);
378 throw Invalid_State(
"WOTS derivation method is out of the enum's range");
385size_t XMSS_PrivateKey::reserve_unused_leaf_index() {
386 return m_private->reserve_unused_leaf_index();
390 return m_private->unused_leaf_index();
398 return m_private->remaining_signatures();
402 return m_private->prf_value();
410 return m_private->wots_derivation_method();
419 std::string_view provider)
const {
420 if(provider ==
"base" || provider.empty()) {
421 return std::make_unique<XMSS_Signature_Operation>(*
this);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
BER_Decoder & decode(bool &out)
BER_Decoder & verify_end()
secure_vector< uint8_t > get_contents()
DER_Encoder & encode(bool b)
uint64_t current_index(const KeyId &key_id)
void set_index_lower_bound(const KeyId &key_id, uint64_t min)
static Stateful_Key_Index_Registry & global()
uint64_t reserve_next_index(const KeyId &key_id)
uint64_t remaining_operations(const KeyId &key_id, uint64_t max)
size_t worker_count() const
auto run(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
static Thread_Pool & global_instance()
static void create_l_tree(secure_vector< uint8_t > &result, wots_keysig_t pk, XMSS_Address adrs, const secure_vector< uint8_t > &seed, XMSS_Hash &hash, const XMSS_Parameters ¶ms)
static void randomize_tree_hash(secure_vector< uint8_t > &result, const secure_vector< uint8_t > &left, const secure_vector< uint8_t > &right, XMSS_Address adrs, const secure_vector< uint8_t > &seed, XMSS_Hash &hash, const XMSS_Parameters ¶ms)
std::unique_ptr< Public_Key > public_key() const override
size_t remaining_signatures() const
size_t unused_leaf_index() const
std::optional< uint64_t > remaining_operations() const override
Retrieves the number of remaining operations if this is a stateful private key.
WOTS_Derivation_Method wots_derivation_method() const
secure_vector< uint8_t > raw_private_key() const
secure_vector< uint8_t > private_key_bits() const override
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
XMSS_PrivateKey(XMSS_Parameters::xmss_algorithm_t xmss_algo_id, RandomNumberGenerator &rng, WOTS_Derivation_Method wots_derivation_method=WOTS_Derivation_Method::NIST_SP800_208)
const secure_vector< uint8_t > & root() const
const secure_vector< uint8_t > & public_seed() const
const XMSS_Parameters & xmss_parameters() const
std::vector< uint8_t > raw_public_key() const
void set_root(secure_vector< uint8_t > root)
std::string algo_name() const override
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, RandomNumberGenerator &rng)
constexpr RT checked_cast_to(AT i)
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)