9#include <botan/pk_options_readers.h>
10#include <botan/sphincsplus.h>
13#include <botan/internal/buffer_slicer.h>
14#include <botan/internal/buffer_stuffer.h>
15#include <botan/internal/concat_util.h>
16#include <botan/internal/int_utils.h>
17#include <botan/internal/pk_ops_impl.h>
18#include <botan/internal/pk_options_impl.h>
19#include <botan/internal/sp_fors.h>
20#include <botan/internal/sp_hash.h>
21#include <botan/internal/sp_hypertree.h>
22#include <botan/internal/sp_treehash.h>
23#include <botan/internal/sp_types.h>
24#include <botan/internal/sp_wots.h>
25#include <botan/internal/sp_xmss.h>
29#if !defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) and !defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE) and \
30 !defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) and !defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
33 "botan module 'sphincsplus_common' is useful only when enabling at least 'sphincsplus_sha2', 'sphincsplus_shake', 'slh_dsa_sha2', or 'slh_dsa_shake'");
43 BOTAN_ARG_CHECK(params.is_slh_dsa() || context.empty(),
"Context is not supported for SPHINCS+");
44#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
45 if(params.is_slh_dsa()) {
47 const uint8_t input_mode_byte = 0x00;
51 .message = std::move(msg),
55#if defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHA2) || defined(BOTAN_HAS_SPHINCS_PLUS_WITH_SHAKE)
56 if(!params.is_slh_dsa()) {
60 .message = std::move(msg),
64 throw Internal_Error(
"Missing message preparation logic for SLH-DSA or SPHINCS+");
68class SphincsPlus_PublicKeyInternal final {
70 SphincsPlus_PublicKeyInternal(Sphincs_Parameters params,
73 m_params(params), m_public_seed(std::move(public_seed)), m_sphincs_root(std::move(sphincs_root)) {}
75 SphincsPlus_PublicKeyInternal(Sphincs_Parameters params, std::span<const uint8_t> key_bits) : m_params(params) {
76 if(key_bits.size() != m_params.public_key_bytes()) {
77 throw Decoding_Error(
"SLH-DSA (or SPHINCS+) Public Key doesn't have the expected length");
80 BufferSlicer s(key_bits);
93 const Sphincs_Parameters& parameters()
const {
return m_params; }
96 Sphincs_Parameters m_params;
101class SphincsPlus_PrivateKeyInternal final {
104 m_secret_seed(std::move(secret_seed)), m_prf(std::move(prf)) {}
106 SphincsPlus_PrivateKeyInternal(
const Sphincs_Parameters& params, std::span<const uint8_t> key_bits) {
107 if(key_bits.size() != params.private_key_bytes() - params.public_key_bytes()) {
108 throw Decoding_Error(
"SLH-DSA (or SPHINCS+) Private Key doesn't have the expected length");
111 BufferSlicer s(key_bits);
135 m_public(std::make_shared<SphincsPlus_PublicKeyInternal>(params, pub_key)) {
137 throw Not_Implemented(
"This SPHINCS+ parameter set is not available in this configuration");
145 throw Decoding_Error(
"Unexpected parameters for SLH-DSA/SPHINCS+ public key");
152 return m_public->parameters().n() * 8;
156 return m_public->parameters().is_slh_dsa() ?
"SLH-DSA" :
"SPHINCS+";
160 return m_public->parameters().bitsec();
164 return m_public->parameters().algorithm_identifier();
168 return m_public->parameters().object_identifier();
187 return std::make_unique<SphincsPlus_PrivateKey>(rng,
m_public->parameters());
194 explicit SphincsPlus_Verification_Operation(std::shared_ptr<const SphincsPlus_PublicKeyInternal> pub_key) :
195 m_public(std::move(pub_key)),
198 BOTAN_ARG_CHECK(m_context.size() <= 255,
"Context must not exceed 255 bytes");
200 if(!m_public->parameters().is_available()) {
201 throw Not_Implemented(
"This SPHINCS+ parameter set is not available in this configuration");
209 void update(std::span<const uint8_t> msg)
override {
211 m_msg_buffer.get().insert(m_msg_buffer.end(), msg.begin(), msg.end());
217 bool is_valid_signature(std::span<const uint8_t> sig)
override {
218 const auto internal_msg = prepare_message(std::exchange(m_msg_buffer, {}), m_public->parameters(), m_context);
219 return slh_verify_internal(internal_msg, sig);
222 std::string hash_function()
const override {
return m_hashes->msg_hash_function_name(); }
226 bool slh_verify_internal(
const SphincsMessageInternal& msg, std::span<const uint8_t> sig) {
227 const auto& p = m_public->parameters();
228 if(sig.size() != p.sphincs_signature_bytes()) {
235 auto [mhash, tree_idx, leaf_idx] = m_hashes->H_msg(msg_random_s, m_public->root(), msg);
239 fors_addr.set_tree_address(tree_idx).set_keypair_address(leaf_idx);
240 const auto fors_sig_s = s.take<
ForsSignature>(p.fors_signature_bytes());
246 return ht_verify(fors_root, ht_sig_s, m_public->root(), tree_idx, leaf_idx, p, *m_hashes);
249 std::shared_ptr<const SphincsPlus_PublicKeyInternal> m_public;
250 std::unique_ptr<Sphincs_Hash_Functions> m_hashes;
261 auto op = std::make_unique<SphincsPlus_Verification_Operation>(
m_public);
269 if(provider.empty() || provider ==
"base") {
273 throw Decoding_Error(
"Unexpected AlgorithmIdentifier for SLH-DSA (or SPHINCS+) signature");
275 return std::make_unique<SphincsPlus_Verification_Operation>(
m_public);
286std::span<const uint8_t> slice_off_public_key(
const OID& oid, std::span<const uint8_t> key_bits) {
293 if(key_bits.size() != params.private_key_bytes()) {
294 throw Decoding_Error(
"Sphincs Private Key doesn't have the expected length");
297 return key_bits.subspan(params.private_key_bytes() - params.public_key_bytes());
311 throw Decoding_Error(
"Unexpected parameters for SLH-DSA/SPHINCS+ private key");
318 throw Not_Implemented(
"This SPHINCS+ parameter set is not available in this configuration");
324 m_private = std::make_shared<SphincsPlus_PrivateKeyInternal>(params, private_key.first(private_portion_bytes));
335 throw Not_Implemented(
"This SPHINCS+ parameter set is not available in this configuration");
340 m_private = std::make_shared<SphincsPlus_PrivateKeyInternal>(std::move(sk_seed), std::move(sk_prf));
346 m_public = std::make_shared<SphincsPlus_PublicKeyInternal>(params, std::move(pub_seed), std::move(
root));
360 return std::make_unique<SphincsPlus_PublicKey>(*
this);
368 const auto& params =
m_public->parameters();
380 SphincsPlus_Signature_Operation(std::shared_ptr<const SphincsPlus_PrivateKeyInternal> private_key,
381 std::shared_ptr<const SphincsPlus_PublicKeyInternal> public_key,
383 m_private(std::move(private_key)),
384 m_public(std::move(public_key)),
386 m_randomized(randomized),
388 BOTAN_ARG_CHECK(m_context.size() <= 255,
"Context must not exceed 255 bytes");
390 "The selected SLH-DSA (or SPHINCS+) instance is not available in this build.");
393 void update(std::span<const uint8_t> msg)
override {
395 m_msg_buffer.get().insert(m_msg_buffer.end(), msg.begin(), msg.end());
398 std::vector<uint8_t> sign(RandomNumberGenerator& rng)
override {
399 std::optional<SphincsOptionalRandomness> addrnd = std::nullopt;
403 const auto internal_msg = prepare_message(std::exchange(m_msg_buffer, {}), m_public->parameters(), m_context);
405 return slh_sign_internal(internal_msg, addrnd);
408 size_t signature_length()
const override {
return m_public->parameters().sphincs_signature_bytes(); }
410 AlgorithmIdentifier algorithm_identifier()
const override {
411 return m_public->parameters().algorithm_identifier();
414 std::string hash_function()
const override {
return m_hashes->msg_hash_function_name(); }
418 std::vector<uint8_t> slh_sign_internal(
const SphincsMessageInternal& message,
419 std::optional<StrongSpan<const SphincsOptionalRandomness>> addrnd) {
420 const auto& p = m_public->parameters();
422 std::vector<uint8_t> sphincs_sig_buffer(p.sphincs_signature_bytes());
423 BufferStuffer sphincs_sig(sphincs_sig_buffer);
427 const auto opt_rand =
428 (addrnd.has_value()) ? addrnd.value() : StrongSpan<const SphincsOptionalRandomness>(m_public->seed());
431 m_hashes->PRF_msg(msg_random_s, m_private->prf(), opt_rand, message);
434 auto [mhash, tree_idx, leaf_idx] = m_hashes->H_msg(msg_random_s, m_public->root(), message);
438 fors_addr.set_tree_address(tree_idx).set_keypair_address(leaf_idx);
456 return sphincs_sig_buffer;
459 std::shared_ptr<const SphincsPlus_PrivateKeyInternal> m_private;
460 std::shared_ptr<const SphincsPlus_PublicKeyInternal> m_public;
461 std::unique_ptr<Sphincs_Hash_Functions> m_hashes;
479 auto op = std::make_unique<SphincsPlus_Signature_Operation>(m_private,
m_public, randomized);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
bool parameters_are_empty() const
const std::optional< std::string > & hash_function() const
bool using_deterministic_signature() const
void random_vec(std::span< uint8_t > v)
secure_vector< uint8_t > raw_private_key_bits() const override
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
~SphincsPlus_PrivateKey() override
SphincsPlus_PrivateKey(std::span< const uint8_t > private_key, Sphincs_Parameter_Set type, Sphincs_Hash_Type hash)
secure_vector< uint8_t > private_key_bits() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::unique_ptr< Public_Key > public_key() const override
std::vector< uint8_t > public_key_bits() const override
std::vector< uint8_t > raw_public_key_bits() const override
std::shared_ptr< const SphincsPlus_PublicKeyInternal > m_public
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
bool supports_operation(PublicKeyOperation op) const override
~SphincsPlus_PublicKey() override
std::string algo_name() const override
size_t key_length() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
bool check_key(RandomNumberGenerator &rng, bool strong) const override
AlgorithmIdentifier algorithm_identifier() const override
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
SphincsPlus_PublicKey()=default
size_t estimated_strength() const override
SphincsPlus_PublicKey(std::span< const uint8_t > pub_key, Sphincs_Parameter_Set type, Sphincs_Hash_Type hash)
OID object_identifier() const override
static std::unique_ptr< Sphincs_Hash_Functions > create(const Sphincs_Parameters &sphincs_params, const SphincsPublicSeed &pub_seed)
uint32_t private_key_bytes() const
uint32_t public_key_bytes() const
bool is_available() const
static Sphincs_Parameters create(Sphincs_Parameter_Set set, Sphincs_Hash_Type hash)
Gf448Elem root(const Gf448Elem &elem)
Compute the root of elem in the field.
Strong< std::vector< uint8_t >, struct SphincsTreeNode_ > SphincsTreeNode
Either an XMSS or FORS tree node or leaf.
SphincsTreeNode xmss_gen_root(const Sphincs_Parameters ¶ms, const SphincsSecretSeed &secret_seed, Sphincs_Hash_Functions &hashes)
Strong< secure_vector< uint8_t >, struct SphincsMessageRandomness_ > SphincsMessageRandomness
SphincsTreeNode fors_sign_and_pkgen(StrongSpan< ForsSignature > sig_out, const SphincsHashedMessage &hashed_message, const SphincsSecretSeed &secret_seed, const Sphincs_Address &address, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 16: fors_sign (with simultaneous FORS pk generation).
Strong< std::vector< uint8_t >, struct ForsSignature_ > ForsSignature
constexpr RT checked_cast_to(AT i)
void ht_sign(StrongSpan< SphincsHypertreeSignature > out_sig, const SphincsTreeNode &message_to_sign, const SphincsSecretSeed &secret_seed, XmssTreeIndexInLayer tree_index_in_layer, TreeNodeIndex idx_leaf, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 12: ht_sign.
bool ht_verify(const SphincsTreeNode &signed_msg, StrongSpan< const SphincsHypertreeSignature > ht_sig, const SphincsTreeNode &pk_root, XmssTreeIndexInLayer tree_index_in_layer, TreeNodeIndex idx_leaf, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 13: ht_verify.
Strong< std::vector< uint8_t >, struct SphincsXmssSignature_ > SphincsHypertreeSignature
Strong< std::vector< uint8_t >, struct SphincsInputMessage_ > SphincsInputMessage
Strong< std::vector< uint8_t >, struct SphincsPublicSeed_ > SphincsPublicSeed
Strong< secure_vector< uint8_t >, struct SphincsSecretSeed_ > SphincsSecretSeed
void require_software_provider(const OptionsT &options, std::string_view algo_name)
Strong< secure_vector< uint8_t >, struct SphincsSecretPRF_ > SphincsSecretPRF
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)
SphincsTreeNode fors_public_key_from_signature(const SphincsHashedMessage &hashed_message, StrongSpan< const ForsSignature > signature, const Sphincs_Address &address, const Sphincs_Parameters ¶ms, Sphincs_Hash_Functions &hashes)
FIPS 205, Algorithm 17: fors_pkFromSig.
std::vector< T, secure_allocator< T > > secure_vector
Strong< secure_vector< uint8_t >, struct SphincsOptionalRandomness_ > SphincsOptionalRandomness
constexpr auto store_be(ParamTs &&... params)
Strong< std::vector< uint8_t >, struct SphincsContext_ > SphincsContext
M' representation of FIPS 205 (the input to slh_sign_internal and slh_verify_internal).