Botan 3.14.1
Crypto and TLS for C&
ed448.cpp
Go to the documentation of this file.
1/*
2 * Ed448 Signature Algorithm (RFC 8032)
3 * (C) 2024 Jack Lloyd
4 * 2024 Fabian Albert - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#include <botan/ed448.h>
10
11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
13#include <botan/hash.h>
14#include <botan/pk_options_readers.h>
15#include <botan/rng.h>
16#include <botan/internal/ct_utils.h>
17#include <botan/internal/ed448_internal.h>
18#include <botan/internal/pk_ops_impl.h>
19#include <botan/internal/pk_options_impl.h>
20
21#include <utility>
22
23namespace Botan {
24
25class Ed448_PublicKey_Data final {
26 public:
27 explicit Ed448_PublicKey_Data(std::array<uint8_t, ED448_LEN> key) : m_key(key) {}
28
29 const std::array<uint8_t, ED448_LEN>& key() const { return m_key; }
30
31 private:
32 std::array<uint8_t, ED448_LEN> m_key;
33};
34
35class Ed448_PrivateKey_Data final {
36 public:
37 explicit Ed448_PrivateKey_Data(secure_vector<uint8_t> key) : m_key(std::move(key)) {}
38
39 const secure_vector<uint8_t>& key() const { return m_key; }
40
41 private:
43};
44
46 const auto& sk = m_private->key();
47 return {sk.begin(), sk.end()};
48}
49
53
54bool Ed448_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
55 // All possible checks are performed when the key is decoded
56 return true;
57}
58
59Ed448_PublicKey::Ed448_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
60 Ed448_PublicKey(key_bits) {
61 // RFC 8410 Section 3: "the parameters MUST be absent".
62 if(!alg_id.parameters_are_empty()) {
63 throw Decoding_Error("Unexpected parameters for Ed448 public key");
64 }
65}
66
67Ed448_PublicKey::Ed448_PublicKey(std::span<const uint8_t> key_bits) {
68 if(key_bits.size() != ED448_LEN) {
69 throw Decoding_Error("Invalid length for Ed448 public key");
70 }
71 std::array<uint8_t, ED448_LEN> pub{};
72 copy_mem(pub, key_bits.first<ED448_LEN>());
73
74 // Rejects the identity, other points of small order, and points outside
75 // the prime order subgroup, so that no later check is needed
77
78 m_public = std::make_shared<const Ed448_PublicKey_Data>(pub);
79}
80
81std::vector<uint8_t> Ed448_PublicKey::raw_public_key_bits() const {
82 const auto& pub = m_public->key();
83 return {pub.begin(), pub.end()};
84}
85
86std::vector<uint8_t> Ed448_PublicKey::public_key_bits() const {
87 return raw_public_key_bits();
88}
89
90std::unique_ptr<Private_Key> Ed448_PublicKey::generate_another(RandomNumberGenerator& rng) const {
91 return std::make_unique<Ed448_PrivateKey>(rng);
92}
93
94Ed448_PrivateKey::Ed448_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
95 // RFC 8410 Section 3: "the parameters MUST be absent".
96 if(!alg_id.parameters_are_empty()) {
97 throw Decoding_Error("Unexpected parameters for Ed448 private key");
98 }
99
102
103 if(bits.size() != ED448_LEN) {
104 throw Decoding_Error("Invalid size for Ed448 private key");
105 }
106 const auto pub = create_pk_from_sk(std::span(bits).first<ED448_LEN>());
107 m_public = std::make_shared<const Ed448_PublicKey_Data>(pub);
108 m_private = std::make_shared<const Ed448_PrivateKey_Data>(std::move(bits));
109}
110
112
113Ed448_PrivateKey::Ed448_PrivateKey(std::span<const uint8_t> key_bits) {
114 if(key_bits.size() != ED448_LEN) {
115 throw Decoding_Error("Invalid size for Ed448 private key");
116 }
117 secure_vector<uint8_t> sk(key_bits.begin(), key_bits.end());
118 std::array<uint8_t, ED448_LEN> pub{};
119 {
120 const auto scope = CT::scoped_poison(sk);
121 pub = create_pk_from_sk(std::span(sk).first<ED448_LEN>());
122 CT::unpoison(pub);
123 }
124 m_public = std::make_shared<const Ed448_PublicKey_Data>(pub);
125 m_private = std::make_shared<const Ed448_PrivateKey_Data>(std::move(sk));
126}
127
128std::unique_ptr<Public_Key> Ed448_PrivateKey::public_key() const {
129 return std::unique_ptr<Ed448_PublicKey>(new Ed448_PublicKey(m_public));
130}
131
133 const auto& sk = m_private->key();
134 BOTAN_ASSERT_NOMSG(sk.size() == ED448_LEN);
136}
137
138bool Ed448_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
139 const auto& sk = m_private->key();
140 BOTAN_ASSERT_NOMSG(sk.size() == ED448_LEN);
141 const auto scope = CT::scoped_poison(sk);
142 const auto public_point = create_pk_from_sk(std::span(sk).first<ED448_LEN>());
143 CT::unpoison(public_point);
144 return public_point == m_public->key();
145}
146
147namespace {
148
149/// Interface to abstract either a pure message or a prehashed message
150class Ed448_Message {
151 public:
152 virtual void update(std::span<const uint8_t> msg) = 0;
153 virtual std::vector<uint8_t> get_and_clear() = 0;
154
155 Ed448_Message() = default;
156 virtual ~Ed448_Message() = default;
157 Ed448_Message(const Ed448_Message&) = delete;
158 Ed448_Message& operator=(const Ed448_Message&) = delete;
159 Ed448_Message(Ed448_Message&&) = delete;
160 Ed448_Message& operator=(Ed448_Message&&) = delete;
161};
162
163class Prehashed_Ed448_Message final : public Ed448_Message {
164 public:
165 void update(std::span<const uint8_t> msg) override { m_hash->update(msg); }
166
167 std::vector<uint8_t> get_and_clear() override { return m_hash->final_stdvec(); }
168
169 explicit Prehashed_Ed448_Message(std::string_view hash) : m_hash(HashFunction::create_or_throw(hash)) {}
170
171 private:
172 std::unique_ptr<HashFunction> m_hash;
173};
174
175class Pure_Ed448_Message final : public Ed448_Message {
176 public:
177 void update(std::span<const uint8_t> msg) override { m_msg.insert(m_msg.end(), msg.begin(), msg.end()); }
178
179 std::vector<uint8_t> get_and_clear() override { return std::exchange(m_msg, {}); }
180
181 private:
182 std::vector<uint8_t> m_msg;
183};
184
185/**
186* Ed448 verifying operation
187*/
188class Ed448_Verify_Operation final : public PK_Ops::Verification {
189 public:
190 explicit Ed448_Verify_Operation(std::shared_ptr<const Ed448_PublicKey_Data> public_key,
191 std::optional<std::string> prehash_function = std::nullopt) :
192 m_public_key(std::move(public_key)), m_prehash_function(std::move(prehash_function)) {
193 if(m_prehash_function) {
194 m_message = std::make_unique<Prehashed_Ed448_Message>(*m_prehash_function);
195 } else {
196 m_message = std::make_unique<Pure_Ed448_Message>();
197 }
198 }
199
200 void update(std::span<const uint8_t> input) override { m_message->update(input); }
201
202 bool is_valid_signature(std::span<const uint8_t> sig) override {
203 const auto msg = m_message->get_and_clear();
204 try {
205 return verify_signature(
206 std::span(m_public_key->key()).first<ED448_LEN>(), m_prehash_function.has_value(), {}, sig, msg);
207 } catch(Decoding_Error&) {
208 return false;
209 }
210 }
211
212 std::string hash_function() const override { return m_prehash_function.value_or("SHAKE-256(912)"); }
213
214 private:
215 std::shared_ptr<const Ed448_PublicKey_Data> m_public_key;
216 std::unique_ptr<Ed448_Message> m_message;
217 std::optional<std::string> m_prehash_function;
218};
219
220/**
221* Ed448 signing operation
222*/
223class Ed448_Sign_Operation final : public PK_Ops::Signature {
224 public:
225 Ed448_Sign_Operation(std::shared_ptr<const Ed448_PublicKey_Data> public_key,
226 std::shared_ptr<const Ed448_PrivateKey_Data> private_key,
227 std::optional<std::string> prehash_function = std::nullopt) :
228 m_public_key(std::move(public_key)),
229 m_private_key(std::move(private_key)),
230 m_prehash_function(std::move(prehash_function)) {
231 if(m_prehash_function) {
232 m_message = std::make_unique<Prehashed_Ed448_Message>(*m_prehash_function);
233 } else {
234 m_message = std::make_unique<Pure_Ed448_Message>();
235 }
236 }
237
238 void update(std::span<const uint8_t> input) override { m_message->update(input); }
239
240 std::vector<uint8_t> sign(RandomNumberGenerator& /*rng*/) override {
241 const auto& sk = m_private_key->key();
242 BOTAN_ASSERT_NOMSG(sk.size() == ED448_LEN);
243 const auto scope = CT::scoped_poison(sk);
244 const auto sig = sign_message(std::span(sk).first<ED448_LEN>(),
245 std::span(m_public_key->key()).first<ED448_LEN>(),
246 m_prehash_function.has_value(),
247 {},
248 m_message->get_and_clear());
249 CT::unpoison(sig);
250 return {sig.begin(), sig.end()};
251 }
252
253 size_t signature_length() const override { return 2 * ED448_LEN; }
254
255 AlgorithmIdentifier algorithm_identifier() const override;
256
257 std::string hash_function() const override { return m_prehash_function.value_or("SHAKE-256(912)"); }
258
259 private:
260 std::shared_ptr<const Ed448_PublicKey_Data> m_public_key;
261 std::shared_ptr<const Ed448_PrivateKey_Data> m_private_key;
262 std::unique_ptr<Ed448_Message> m_message;
263 std::optional<std::string> m_prehash_function;
264};
265
266AlgorithmIdentifier Ed448_Sign_Operation::algorithm_identifier() const {
267 // RFC 8410 Section 3 defines the OIDs for "the algorithms being ECDH and
268 // EdDSA in pure mode"; there is no identifier for Ed448ph
269 if(m_prehash_function) {
270 throw Not_Implemented("Ed448ph signatures do not have an algorithm identifier");
271 }
272 return AlgorithmIdentifier(OID::from_string("Ed448"), AlgorithmIdentifier::USE_EMPTY_PARAM);
273}
274
275} // namespace
276
277std::unique_ptr<PK_Ops::Verification> Ed448_PublicKey::_create_verification_op(
278 const PK_Signature_Options_Reader& options) const {
280
281 if(options.using_prehash()) {
282 return std::make_unique<Ed448_Verify_Operation>(m_public, options.prehash_function().value_or("SHAKE-256(512)"));
283 } else {
284 return std::make_unique<Ed448_Verify_Operation>(m_public);
285 }
286}
287
288std::unique_ptr<PK_Ops::Verification> Ed448_PublicKey::create_x509_verification_op(const AlgorithmIdentifier& alg_id,
289 std::string_view provider) const {
290 if(provider == "base" || provider.empty()) {
291 if(alg_id != this->algorithm_identifier()) {
292 throw Decoding_Error("Unexpected AlgorithmIdentifier for Ed448 X509 signature");
293 }
294
295 return std::make_unique<Ed448_Verify_Operation>(m_public);
296 }
297 throw Provider_Not_Found(algo_name(), provider);
298}
299
300std::unique_ptr<PK_Ops::Signature> Ed448_PrivateKey::_create_signature_op(
301 RandomNumberGenerator& rng, const PK_Signature_Options_Reader& options) const {
302 BOTAN_UNUSED(rng);
303
305
307
308 if(options.using_prehash()) {
309 return std::make_unique<Ed448_Sign_Operation>(
310 m_public, m_private, options.prehash_function().value_or("SHAKE-256(512)"));
311 } else {
312 return std::make_unique<Ed448_Sign_Operation>(m_public, m_private);
313 }
314}
315
316} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
bool parameters_are_empty() const
Definition asn1_obj.h:715
virtual OID object_identifier() const
Definition pk_keys.cpp:24
static Limits DER()
Definition ber_dec.h:44
BER_Decoder & decode(bool &out)
Definition ber_dec.h:361
BER_Decoder & verify_end()
Definition ber_dec.cpp:471
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:161
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
Ed448_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition ed448.cpp:94
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
Definition ed448.cpp:300
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ed448.cpp:138
secure_vector< uint8_t > raw_private_key_bits() const override
Definition ed448.cpp:45
secure_vector< uint8_t > private_key_bits() const override
Definition ed448.cpp:132
std::unique_ptr< Public_Key > public_key() const override
Definition ed448.cpp:128
AlgorithmIdentifier algorithm_identifier() const override
Definition ed448.cpp:50
std::unique_ptr< PK_Ops::Verification > _create_verification_op(const PK_Signature_Options_Reader &options) const override
Definition ed448.cpp:277
std::string algo_name() const override
Definition ed448.h:33
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition ed448.cpp:90
std::vector< uint8_t > public_key_bits() const override
Definition ed448.cpp:86
std::shared_ptr< const Ed448_PublicKey_Data > m_public
Definition ed448.h:77
Ed448_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition ed448.cpp:59
friend class Ed448_PrivateKey
Definition ed448.h:80
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
Definition ed448.cpp:288
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ed448.cpp:54
std::vector< uint8_t > raw_public_key_bits() const override
Definition ed448.cpp:81
const std::optional< std::string > & prehash_function() const
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 ed448_validate_public_key_point(std::span< const uint8_t, ED448_LEN > pk)
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
std::array< uint8_t, ED448_LEN > create_pk_from_sk(std::span< const uint8_t, ED448_LEN > sk)
Create a public key point from a secret key (RFC 8032 5.2.5).
constexpr size_t ED448_LEN
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
void require_software_provider(const OptionsT &options, std::string_view algo_name)
bool verify_signature(std::span< const uint8_t, ED448_LEN > pk, bool phflag, std::span< const uint8_t > context, std::span< const uint8_t > sig, std::span< const uint8_t > msg)
Verify a signature(RFC 8032 5.2.7).
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
std::array< uint8_t, 2 *ED448_LEN > sign_message(std::span< const uint8_t, ED448_LEN > sk, std::span< const uint8_t, ED448_LEN > pk, bool pgflag, std::span< const uint8_t > context, std::span< const uint8_t > msg)
Sign a message using a keypair (RFC 8032 5.2.6).