19#include <botan/xmss.h>
21#include <botan/ber_dec.h>
22#include <botan/der_enc.h>
23#include <botan/pk_options.h>
25#include <botan/internal/buffer_slicer.h>
26#include <botan/internal/concat_util.h>
27#include <botan/internal/int_utils.h>
28#include <botan/internal/loadstor.h>
29#include <botan/internal/pk_options_impl.h>
30#include <botan/internal/stateful_key_index_registry.h>
31#include <botan/internal/xmss_hash.h>
32#include <botan/internal/xmss_signature_operation.h>
33#include <botan/internal/xmss_tree_builder.h>
45 if(key_bits.size() == xmss_params.raw_private_key_size() ||
46 key_bits.size() == xmss_params.raw_legacy_private_key_size()) {
47 raw_key.assign(key_bits.begin(), key_bits.end());
66 std::span<const uint8_t> private_seed,
67 std::span<const uint8_t> public_seed) {
69 std::array{
static_cast<uint8_t
>(wots_derivation_method)});
75 params.total_number_of_signatures(),
77 derivation_uses_public_seed ? public_seed : std::span<const uint8_t>());
82class XMSS_PrivateKey_Internal final {
86 std::span<const uint8_t> public_seed,
89 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)),
90 m_wots_params(m_xmss_params.wots_parameters()),
91 m_wots_derivation_method(wots_derivation_method),
92 m_prf(std::move(prf)),
93 m_private_seed(std::move(private_seed)),
94 m_keyid(xmss_key_id(m_xmss_params, m_wots_derivation_method, m_private_seed, public_seed)) {
96 BOTAN_ARG_CHECK(is_known_wots_derivation_method(m_wots_derivation_method),
97 "XMSS: unknown WOTS+ derivation method");
98 BOTAN_ARG_CHECK(m_prf.size() == m_xmss_params.element_size(),
"XMSS: unexpected byte length of PRF value");
100 "XMSS: unexpected byte length of private seed");
104 m_xmss_params(XMSS_Parameters::from_id(xmss_algo_id)), m_wots_params(m_xmss_params.wots_parameters()) {
113 static_assert(
sizeof(size_t) >= 4,
"size_t is big enough to support leaf index");
117 if(raw_key.size() != m_xmss_params.raw_private_key_size() &&
118 raw_key.size() != m_xmss_params.raw_legacy_private_key_size()) {
119 throw Decoding_Error(
"Invalid XMSS private key size");
122 BufferSlicer s(raw_key);
126 s.skip(4 + m_xmss_params.element_size());
127 const auto public_seed = s.take(m_xmss_params.element_size());
129 const auto unused_leaf_bytes = s.take(
sizeof(uint32_t));
132 m_prf = s.copy_as_secure_vector(m_xmss_params.element_size());
133 m_private_seed = s.copy_as_secure_vector(m_xmss_params.element_size());
137 m_wots_derivation_method =
140 if(!is_known_wots_derivation_method(m_wots_derivation_method)) {
141 throw Decoding_Error(
"XMSS private key has an unknown WOTS+ derivation method");
146 m_keyid = xmss_key_id(m_xmss_params, m_wots_derivation_method, m_private_seed, public_seed);
151 set_unused_leaf_index(unused_leaf);
155 std::vector<uint8_t> unused_index(4);
158 std::vector<uint8_t> wots_derivation_method;
159 wots_derivation_method.push_back(
static_cast<uint8_t
>(m_wots_derivation_method));
162 raw_public_key, unused_index, m_prf, m_private_seed, wots_derivation_method);
169 const XMSS_WOTS_Parameters& wots_parameters()
const {
return m_wots_params; }
176 void set_unused_leaf_index(
size_t idx)
const {
178 if(idx > (1ULL << m_xmss_params.tree_height())) {
179 throw Decoding_Error(
"XMSS private key leaf index out of bounds");
185 size_t reserve_unused_leaf_index()
const {
187 if(!idx.has_value()) {
188 throw Invalid_State(
"XMSS private key, one time signatures exhausted");
191 return static_cast<size_t>(idx.value());
194 size_t unused_leaf_index()
const {
199 uint64_t remaining_signatures()
const {
204 XMSS_Parameters m_xmss_params;
205 XMSS_WOTS_Parameters m_wots_params;
210 Stateful_Key_Index_Registry::KeyId m_keyid;
218 m_private(std::make_shared<XMSS_PrivateKey_Internal>(
xmss_parameters().oid(), key_bits)) {}
220struct XMSS_PrivateKey::Keygen_Material {
227XMSS_PrivateKey::Keygen_Material XMSS_PrivateKey::generate_keygen_material(
229 RandomNumberGenerator& rng,
234 const size_t n = params.element_size();
240 auto prf = rng.random_vec(n);
241 auto private_seed = rng.random_vec(n);
243 const XMSS_Address adrs;
244 XMSS_Hash hash(params);
246 auto root = builder.tree_hash(0, params.tree_height(), adrs, hash);
248 return Keygen_Material{std::move(private_seed), std::move(prf), std::move(
public_seed), std::move(
root)};
259 Keygen_Material material) :
260 XMSS_PublicKey(xmss_algo_id, std::move(material.
root), std::move(material.public_seed)),
261 m_private(std::make_shared<XMSS_PrivateKey_Internal>(xmss_algo_id,
262 wots_derivation_method,
264 std::move(material.private_seed),
265 std::move(material.prf))) {}
275 m_private(std::make_shared<XMSS_PrivateKey_Internal>(
277 m_private->set_unused_leaf_index(idx_leaf);
292 size_t target_node_height,
297 .
tree_hash(start_idx, target_node_height, adrs, hash);
300XMSS_WOTS_PrivateKey XMSS_PrivateKey::wots_private_key_for(
const XMSS_Address& adrs, XMSS_Hash& hash)
const {
301 return XMSS_Tree_Builder(
303 .wots_private_key_for(adrs, hash);
310size_t XMSS_PrivateKey::reserve_unused_leaf_index() {
311 return m_private->reserve_unused_leaf_index();
315 return m_private->unused_leaf_index();
323 return m_private->remaining_signatures();
327 return m_private->prf_value();
335 return m_private->wots_derivation_method();
351 return std::make_unique<XMSS_Signature_Operation>(*
this);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
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)
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()
static XMSS_Parameters from_id(xmss_algorithm_t id)
std::unique_ptr< Public_Key > public_key() const override
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
size_t remaining_signatures() const
size_t unused_leaf_index() const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
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)
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
std::string algo_name() const override
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, RandomNumberGenerator &rng)
secure_vector< uint8_t > tree_hash(size_t start_idx, size_t target_node_height, const XMSS_Address &adrs, XMSS_Hash &hash) const
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
constexpr RT checked_cast_to(AT i)
void require_software_provider(const OptionsT &options, std::string_view algo_name)
void validate_for_hash_based_signature(const PK_Signature_Options_Reader &options, std::string_view algo_name, std::string_view hash_fn)
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)