19#include <botan/xmss.h>
21#include <botan/ber_dec.h>
22#include <botan/der_enc.h>
23#include <botan/internal/loadstor.h>
24#include <botan/internal/stl_util.h>
25#include <botan/internal/xmss_common_ops.h>
26#include <botan/internal/xmss_index_registry.h>
27#include <botan/internal/xmss_signature_operation.h>
30#if defined(BOTAN_HAS_THREAD_UTILS)
31 #include <botan/internal/thread_pool.h>
39secure_vector<uint8_t> extract_raw_private_key(std::span<const uint8_t> key_bits,
const XMSS_Parameters& xmss_params) {
44 if(key_bits.size() == xmss_params.raw_private_key_size() ||
45 key_bits.size() == xmss_params.raw_legacy_private_key_size()) {
46 raw_key.assign(key_bits.begin(), key_bits.end());
48 DataSource_Memory src(key_bits);
57class XMSS_PrivateKey_Internal {
59 XMSS_PrivateKey_Internal(
const XMSS_Parameters& xmss_params,
60 const XMSS_WOTS_Parameters& wots_params,
62 RandomNumberGenerator& rng) :
63 m_xmss_params(xmss_params),
64 m_wots_params(wots_params),
65 m_wots_derivation_method(wots_derivation_method),
67 m_prf(rng.random_vec(xmss_params.element_size())),
68 m_private_seed(rng.random_vec(xmss_params.element_size())),
69 m_index_reg(XMSS_Index_Registry::get_instance()) {}
71 XMSS_PrivateKey_Internal(
const XMSS_Parameters& xmss_params,
72 const XMSS_WOTS_Parameters& wots_params,
76 m_xmss_params(xmss_params),
77 m_wots_params(wots_params),
78 m_wots_derivation_method(wots_derivation_method),
79 m_hash(m_xmss_params),
80 m_prf(std::move(prf)),
81 m_private_seed(std::move(private_seed)),
82 m_index_reg(XMSS_Index_Registry::get_instance()) {}
84 XMSS_PrivateKey_Internal(
const XMSS_Parameters& xmss_params,
85 const XMSS_WOTS_Parameters& wots_params,
86 std::span<const uint8_t> key_bits) :
87 m_xmss_params(xmss_params),
88 m_wots_params(wots_params),
89 m_hash(m_xmss_params),
90 m_index_reg(XMSS_Index_Registry::get_instance()) {
99 static_assert(
sizeof(size_t) >= 4,
"size_t is big enough to support leaf index");
105 throw Decoding_Error(
"Invalid XMSS private key size");
108 BufferSlicer s(raw_key);
113 auto unused_leaf_bytes = s.take(
sizeof(uint32_t));
115 if(unused_leaf >= (1ULL << m_xmss_params.
tree_height())) {
116 throw Decoding_Error(
"XMSS private key leaf index out of bounds");
119 m_prf = s.copy_as_secure_vector(m_xmss_params.
element_size());
120 m_private_seed = s.copy_as_secure_vector(m_xmss_params.
element_size());
121 set_unused_leaf_index(unused_leaf);
125 m_wots_derivation_method =
132 std::vector<uint8_t> unused_index(4);
133 store_be(
static_cast<uint32_t
>(unused_leaf_index()), unused_index.data());
135 std::vector<uint8_t> wots_derivation_method;
136 wots_derivation_method.push_back(
static_cast<uint8_t
>(m_wots_derivation_method));
139 raw_public_key, unused_index, m_prf, m_private_seed, wots_derivation_method);
142 XMSS_Hash& hash() {
return m_hash; }
148 const XMSS_WOTS_Parameters& wots_parameters() {
return m_wots_params; }
152 XMSS_Index_Registry& index_registry() {
return m_index_reg; }
154 std::shared_ptr<Atomic<size_t>> recover_global_leaf_index()
const {
157 "Trying to retrieve index for partially initialized key");
158 return m_index_reg.
get(m_private_seed, m_prf);
161 void set_unused_leaf_index(
size_t idx) {
163 throw Decoding_Error(
"XMSS private key leaf index out of bounds");
165 std::atomic<size_t>& index =
static_cast<std::atomic<size_t>&
>(*recover_global_leaf_index());
169 current = index.load();
173 }
while(!index.compare_exchange_strong(current, idx));
177 size_t reserve_unused_leaf_index() {
178 size_t idx = (
static_cast<std::atomic<size_t>&
>(*recover_global_leaf_index())).fetch_add(1);
180 throw Decoding_Error(
"XMSS private key, one time signatures exhaused");
185 size_t unused_leaf_index()
const {
return *recover_global_leaf_index(); }
187 size_t remaining_signatures()
const {
192 XMSS_Parameters m_xmss_params;
193 XMSS_WOTS_Parameters m_wots_params;
199 XMSS_Index_Registry& m_index_reg;
204 m_private(std::make_shared<XMSS_PrivateKey_Internal>(m_xmss_params, m_wots_params, key_bits)) {}
210 m_private(std::make_shared<XMSS_PrivateKey_Internal>(m_xmss_params, m_wots_params, wots_derivation_method, rng)) {
223 m_private(std::make_shared<XMSS_PrivateKey_Internal>(
224 m_xmss_params, m_wots_params, wots_derivation_method, std::move(wots_priv_seed), std::move(prf))) {
225 m_private->set_unused_leaf_index(idx_leaf);
227 "XMSS: unexpected byte length of PRF value");
229 "XMSS: unexpected byte length of private seed");
234 BOTAN_ASSERT((start_idx % (
static_cast<size_t>(1) << target_node_height)) == 0,
235 "Start index must be divisible by 2^{target node height}.");
237#if defined(BOTAN_HAS_THREAD_UTILS)
242 const size_t split_level = std::min(target_node_height, thread_pool.
worker_count());
245 if(split_level == 0) {
247 tree_hash_subtree(result, start_idx, target_node_height, adrs);
251 const size_t subtrees =
static_cast<size_t>(1) << split_level;
252 const size_t last_idx = (
static_cast<size_t>(1) << (target_node_height)) + start_idx;
253 const size_t offs = (last_idx - start_idx) / subtrees;
255 uint8_t level =
static_cast<uint8_t
>(split_level);
258 "Number of worker threads in tree_hash need to divide range "
259 "of calculated nodes.");
261 std::vector<secure_vector<uint8_t>> nodes(subtrees,
263 std::vector<XMSS_Address> node_addresses(subtrees, adrs);
264 std::vector<XMSS_Hash> xmss_hash(subtrees, m_private->hash());
265 std::vector<std::future<void>> work;
268 for(
size_t i = 0; i < subtrees; i++) {
269 using tree_hash_subtree_fn_t =
272 tree_hash_subtree_fn_t work_fn = &XMSS_PrivateKey::tree_hash_subtree;
274 work.push_back(thread_pool.
run(work_fn,
277 start_idx + i * offs,
278 target_node_height - split_level,
279 std::ref(node_addresses[i]),
280 std::ref(xmss_hash[i])));
283 for(
auto& w : work) {
290 std::vector<secure_vector<uint8_t>> ro_nodes(nodes.begin(),
291 nodes.begin() + (
static_cast<size_t>(1) << (level + 1)));
293 for(
size_t i = 0; i < (static_cast<size_t>(1) << level); i++) {
296 node_addresses[i].set_tree_height(
static_cast<uint32_t
>(target_node_height - (level + 1)));
297 node_addresses[i].set_tree_index((node_addresses[2 * i + 1].get_tree_index() - 1) >> 1);
301 std::cref(ro_nodes[2 * i]),
302 std::cref(ro_nodes[2 * i + 1]),
303 std::ref(node_addresses[i]),
304 std::cref(this->public_seed()),
305 std::ref(xmss_hash[i]),
309 for(
auto& w : work) {
316 node_addresses[0].set_tree_height(
static_cast<uint32_t
>(target_node_height - 1));
317 node_addresses[0].set_tree_index((node_addresses[1].get_tree_index() - 1) >> 1);
323 tree_hash_subtree(result, start_idx, target_node_height, adrs, m_private->hash());
330 size_t target_node_height,
331 XMSS_Address& adrs) {
332 return tree_hash_subtree(result, start_idx, target_node_height, adrs, m_private->hash());
335void XMSS_PrivateKey::tree_hash_subtree(
336 secure_vector<uint8_t>& result,
size_t start_idx,
size_t target_node_height, XMSS_Address& adrs, XMSS_Hash& hash) {
339 std::vector<secure_vector<uint8_t>> nodes(target_node_height + 1,
346 std::vector<uint8_t> node_levels(target_node_height + 1);
349 const size_t last_idx = (
static_cast<size_t>(1) << target_node_height) + start_idx;
351 for(
size_t i = start_idx; i < last_idx; i++) {
353 adrs.set_ots_address(
static_cast<uint32_t
>(i));
355 XMSS_WOTS_PublicKey pk = this->wots_public_key_for(adrs, hash);
358 adrs.set_ltree_address(
static_cast<uint32_t
>(i));
360 node_levels[level] = 0;
363 adrs.set_tree_height(0);
364 adrs.set_tree_index(
static_cast<uint32_t
>(i));
366 while(level > 0 && node_levels[level] == node_levels[level - 1]) {
367 adrs.set_tree_index(((adrs.get_tree_index() - 1) >> 1));
369 nodes[level - 1], nodes[level - 1], nodes[level], adrs, seed, hash,
m_xmss_params);
370 node_levels[level - 1]++;
372 adrs.set_tree_height(adrs.get_tree_height() + 1);
376 result = nodes[level - 1];
379XMSS_WOTS_PublicKey XMSS_PrivateKey::wots_public_key_for(XMSS_Address& adrs, XMSS_Hash& hash)
const {
380 const auto private_key = wots_private_key_for(adrs, hash);
381 return XMSS_WOTS_PublicKey(m_private->wots_parameters(),
m_public_seed, private_key, adrs, hash);
384XMSS_WOTS_PrivateKey XMSS_PrivateKey::wots_private_key_for(XMSS_Address& adrs, XMSS_Hash& hash)
const {
387 return XMSS_WOTS_PrivateKey(
388 m_private->wots_parameters(),
m_public_seed, m_private->private_seed(), adrs, hash);
390 return XMSS_WOTS_PrivateKey(m_private->wots_parameters(), m_private->private_seed(), adrs, hash);
393 throw Invalid_State(
"WOTS derivation method is out of the enum's range");
400size_t XMSS_PrivateKey::reserve_unused_leaf_index() {
401 return m_private->reserve_unused_leaf_index();
405 return m_private->unused_leaf_index();
409 return m_private->remaining_signatures();
413 return m_private->remaining_signatures();
417 return m_private->prf_value();
425 return m_private->wots_derivation_method();
434 std::string_view provider)
const {
435 if(provider ==
"base" || provider.empty()) {
436 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)
secure_vector< uint8_t > get_contents()
DER_Encoder & encode(bool b)
size_t worker_count() const
auto run(F &&f, Args &&... args) -> std::future< typename std::invoke_result< F, Args... >::type >
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::shared_ptr< Atomic< size_t > > get(const secure_vector< uint8_t > &private_seed, const secure_vector< uint8_t > &prf)
size_t total_number_of_signatures() const
size_t raw_private_key_size() const
size_t raw_public_key_size() const
size_t tree_height() const
size_t raw_legacy_private_key_size() const
size_t element_size() const
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
XMSS_PrivateKey(XMSS_Parameters::xmss_algorithm_t xmss_algo_id, RandomNumberGenerator &rng, WOTS_Derivation_Method wots_derivation_method=WOTS_Derivation_Method::NIST_SP800_208)
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &, std::string_view, std::string_view provider) const override
const secure_vector< uint8_t > & root() const
secure_vector< uint8_t > m_root
const secure_vector< uint8_t > & public_seed() const
secure_vector< uint8_t > m_public_seed
const XMSS_Parameters & xmss_parameters() const
XMSS_Parameters m_xmss_params
std::vector< uint8_t > raw_public_key() const
std::string algo_name() const override
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
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)