Botan 3.14.1
Crypto and TLS for C&
hss_lms.cpp
Go to the documentation of this file.
1/**
2* HSS-LMS
3* (C) 2023 Jack Lloyd
4* 2023 Fabian Albert, René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/hss_lms.h>
10
11#include <botan/rng.h>
12#include <botan/internal/hss.h>
13#include <botan/internal/pk_ops_impl.h>
14#include <botan/internal/pk_options_impl.h>
15
16namespace Botan {
17
18HSS_LMS_PublicKey::HSS_LMS_PublicKey(std::span<const uint8_t> pub_key) :
20
21HSS_LMS_PublicKey::HSS_LMS_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
22 m_public(HSS_LMS_PublicKeyInternal::from_bytes_or_throw(key_bits)) {
23 /*
24 RFC 9802 Section 4:
25 The parameters field of the AlgorithmIdentifier for HSS [...] public keys MUST be absent.
26 */
27 if(!alg_id.parameters_are_empty()) {
28 throw Decoding_Error("Unexpected parameters for HSS-LMS public key");
29 }
30}
31
33
35 return 8 * m_public->lms_pub_key().lms_params().m();
36}
37
39 // draft-fluhrer-lms-more-parm-sets-11 Section 9.
40 // As shown in [Katz16], if we assume that the hash function can be
41 // modeled as a random oracle, then the security of the system is at
42 // least 8N-1 bits (where N is the size of the hash output in bytes);
43 return 8 * m_public->lms_pub_key().lms_params().m() - 1;
44}
45
46std::string HSS_LMS_PublicKey::algo_name() const {
47 return m_public->algo_name();
48}
49
51 return m_public->algorithm_identifier();
52}
53
55 return m_public->object_identifier();
56}
57
58bool HSS_LMS_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
59 // Nothing to check. Only useful checks are already done during parsing.
60 return true;
61}
62
63std::vector<uint8_t> HSS_LMS_PublicKey::raw_public_key_bits() const {
64 return m_public->to_bytes();
65}
66
67std::vector<uint8_t> HSS_LMS_PublicKey::public_key_bits() const {
68 // The raw encoding of HSS/LMS public keys always contains the necessary
69 // algorithm information.
70 return raw_public_key_bits();
71}
72
73namespace {
74
75class HSS_LMS_Verification_Operation final : public PK_Ops::Verification {
76 public:
77 explicit HSS_LMS_Verification_Operation(std::shared_ptr<const HSS_LMS_PublicKeyInternal> pub_key) :
78 m_public(std::move(pub_key)) {}
79
80 void update(std::span<const uint8_t> msg) override {
81 m_msg_buffer.insert(m_msg_buffer.end(), msg.begin(), msg.end());
82 }
83
84 bool is_valid_signature(std::span<const uint8_t> sig) override {
85 std::vector<uint8_t> message_to_verify = std::exchange(m_msg_buffer, {});
86 try {
87 const auto signature = HSS_Signature::from_bytes_or_throw(sig);
88 return m_public->verify_signature(message_to_verify, signature);
89 } catch(const Decoding_Error&) {
90 // Signature could not be decoded
91 return false;
92 }
93 }
94
95 std::string hash_function() const override { return m_public->lms_pub_key().lms_params().hash_name(); }
96
97 private:
98 std::shared_ptr<const HSS_LMS_PublicKeyInternal> m_public;
99 std::vector<uint8_t> m_msg_buffer;
100};
101
102} // namespace
103
104std::unique_ptr<PK_Ops::Verification> HSS_LMS_PublicKey::_create_verification_op(
105 const PK_Signature_Options_Reader& options) const {
106 validate_for_hash_based_signature(options, "HSS-LMS", m_public->lms_pub_key().lms_params().hash_name());
107
109
110 return std::make_unique<HSS_LMS_Verification_Operation>(m_public);
111}
112
113std::unique_ptr<PK_Ops::Verification> HSS_LMS_PublicKey::create_x509_verification_op(
114 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
115 if(provider.empty() || provider == "base") {
116 if(signature_algorithm != this->algorithm_identifier()) {
117 throw Decoding_Error("Unexpected AlgorithmIdentifier for HSS-LMS signature");
118 }
119 return std::make_unique<HSS_LMS_Verification_Operation>(m_public);
120 }
121 throw Provider_Not_Found(algo_name(), provider);
122}
123
127
128std::unique_ptr<Private_Key> HSS_LMS_PublicKey::generate_another(RandomNumberGenerator& /*rng*/) const {
129 // For this key type we cannot derive all required parameters from just
130 // the public key. It is however possible to call HSS_LMS_PrivateKey::generate_another().
131 throw Not_Implemented("Cannot generate a new HSS/LMS keypair from a public key");
132}
133
134HSS_LMS_PrivateKey::HSS_LMS_PrivateKey(std::span<const uint8_t> private_key) :
135 HSS_LMS_PrivateKey(AlgorithmIdentifier(), private_key) {}
136
137HSS_LMS_PrivateKey::HSS_LMS_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
138 // The HSS/LMS parameters are carried in the key bits; no AlgorithmIdentifier
139 // parameters are defined.
140 if(!alg_id.parameters_are_empty()) {
141 throw Decoding_Error("Unexpected parameters for HSS-LMS private key");
142 }
143
145 const auto scope = CT::scoped_poison(*m_private);
146 m_public = std::make_shared<HSS_LMS_PublicKeyInternal>(HSS_LMS_PublicKeyInternal::create(*m_private));
148}
149
151 const HSS_LMS_Params hss_params(algo_params);
152 m_private = std::make_shared<HSS_LMS_PrivateKeyInternal>(hss_params, rng);
153 const auto scope = CT::scoped_poison(*m_private);
154 m_public = std::make_shared<HSS_LMS_PublicKeyInternal>(HSS_LMS_PublicKeyInternal::create(*m_private));
156}
157
158HSS_LMS_PrivateKey::HSS_LMS_PrivateKey(std::shared_ptr<HSS_LMS_PrivateKeyInternal> sk) : m_private(std::move(sk)) {
159 const auto scope = CT::scoped_poison(*m_private);
160 m_public = std::make_shared<HSS_LMS_PublicKeyInternal>(HSS_LMS_PublicKeyInternal::create(*m_private));
162}
163
165
167 const auto scope = CT::scoped_poison(*m_private);
168 return CT::driveby_unpoison(m_private->to_bytes());
169}
170
174
175std::unique_ptr<Public_Key> HSS_LMS_PrivateKey::public_key() const {
176 return std::make_unique<HSS_LMS_PublicKey>(*this);
177}
178
179// We use a separate algorithm identifier for the private key since we use a Botan scoped OID for it.
180// This is necessary since the private key format is implementation specific, since it is not defined
181// in RFC 8554.
185
186std::optional<uint64_t> HSS_LMS_PrivateKey::remaining_operations() const {
187 return m_private->remaining_operations().get();
188}
189
190std::unique_ptr<Private_Key> HSS_LMS_PrivateKey::generate_another(RandomNumberGenerator& rng) const {
191 // Cannot use std::make_unique because the utilized constructor is private.
192 return std::unique_ptr<HSS_LMS_PrivateKey>(
193 new HSS_LMS_PrivateKey(std::make_shared<HSS_LMS_PrivateKeyInternal>(m_private->hss_params(), rng)));
194}
195
196namespace {
197
198class HSS_LMS_Signature_Operation final : public PK_Ops::Signature {
199 public:
200 HSS_LMS_Signature_Operation(std::shared_ptr<const HSS_LMS_PrivateKeyInternal> private_key,
201 std::shared_ptr<const HSS_LMS_PublicKeyInternal> public_key) :
202 m_private(std::move(private_key)), m_public(std::move(public_key)) {}
203
204 void update(std::span<const uint8_t> msg) override {
205 m_msg_buffer.insert(m_msg_buffer.end(), msg.begin(), msg.end());
206 }
207
208 std::vector<uint8_t> sign(RandomNumberGenerator& /*rng*/) override {
209 std::vector<uint8_t> message_to_sign = std::exchange(m_msg_buffer, {});
210 const auto scope = CT::scoped_poison(*m_private);
211 return CT::driveby_unpoison(m_private->sign(message_to_sign));
212 }
213
214 size_t signature_length() const override { return m_private->signature_size(); }
215
216 AlgorithmIdentifier algorithm_identifier() const override { return m_public->algorithm_identifier(); }
217
218 std::string hash_function() const override { return m_public->lms_pub_key().lms_params().hash_name(); }
219
220 private:
221 std::shared_ptr<const HSS_LMS_PrivateKeyInternal> m_private;
222 std::shared_ptr<const HSS_LMS_PublicKeyInternal> m_public;
223 std::vector<uint8_t> m_msg_buffer;
224};
225
226} // namespace
227
228std::unique_ptr<PK_Ops::Signature> HSS_LMS_PrivateKey::_create_signature_op(
229 RandomNumberGenerator& rng, const PK_Signature_Options_Reader& options) const {
230 BOTAN_UNUSED(rng);
231
232 validate_for_hash_based_signature(options, "HSS-LMS", m_public->lms_pub_key().lms_params().hash_name());
234
236
237 return std::make_unique<HSS_LMS_Signature_Operation>(m_private, m_public);
238}
239
240} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
bool parameters_are_empty() const
Definition asn1_obj.h:715
The HSS-LMS parameters.
Definition hss.h:42
static std::shared_ptr< HSS_LMS_PrivateKeyInternal > from_bytes_or_throw(std::span< const uint8_t > key_bytes)
Parse a private HSS-LMS key.
Definition hss.cpp:172
std::unique_ptr< Public_Key > public_key() const override
Definition hss_lms.cpp:175
AlgorithmIdentifier pkcs8_algorithm_identifier() const override
Definition hss_lms.cpp:182
HSS_LMS_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Load a private key from a PKCS #8 PrivateKeyInfo.
Definition hss_lms.cpp:137
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
Definition hss_lms.cpp:228
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const override
Definition hss_lms.cpp:190
secure_vector< uint8_t > raw_private_key_bits() const override
Definition hss_lms.cpp:171
std::optional< uint64_t > remaining_operations() const override
Definition hss_lms.cpp:186
secure_vector< uint8_t > private_key_bits() const override
Definition hss_lms.cpp:166
The internal HSS-LMS public key.
Definition hss.h:252
static HSS_LMS_PublicKeyInternal create(const HSS_LMS_PrivateKeyInternal &hss_sk)
Create the public HSS-LMS key from its private key.
Definition hss.cpp:347
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const override
Definition hss_lms.cpp:128
size_t key_length() const override
Definition hss_lms.cpp:34
bool supports_operation(PublicKeyOperation op) const override
Definition hss_lms.cpp:124
std::vector< uint8_t > raw_public_key_bits() const override
Definition hss_lms.cpp:63
std::string algo_name() const override
Definition hss_lms.cpp:46
OID object_identifier() const override
Definition hss_lms.cpp:54
std::shared_ptr< const HSS_LMS_PublicKeyInternal > m_public
Definition hss_lms.h:80
size_t estimated_strength() const override
Definition hss_lms.cpp:38
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
Definition hss_lms.cpp:113
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition hss_lms.cpp:58
HSS_LMS_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Load a public key from an X.509 SubjectPublicKeyInfo.
Definition hss_lms.cpp:21
std::vector< uint8_t > public_key_bits() const override
Definition hss_lms.cpp:67
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
Definition hss_lms.cpp:104
AlgorithmIdentifier algorithm_identifier() const override
Definition hss_lms.cpp:50
static HSS_Signature from_bytes_or_throw(std::span< const uint8_t > sig_bytes)
Parse a HSS-LMS signature.
Definition hss.cpp:422
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
constexpr decltype(auto) driveby_unpoison(T &&v)
Definition ct_utils.h:243
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
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)
PublicKeyOperation
Definition pk_keys.h:50
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128