Botan 3.14.1
Crypto and TLS for C&
x448.cpp
Go to the documentation of this file.
1/*
2* X448
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/x448.h>
10
11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
13#include <botan/mem_ops.h>
14#include <botan/rng.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/pk_ops_impl.h>
17#include <botan/internal/pk_options_impl.h>
18#include <botan/internal/x448_internal.h>
19
20namespace Botan {
21
22class X448_PublicKey_Data final {
23 public:
24 explicit X448_PublicKey_Data(std::array<uint8_t, X448_LEN> key) : m_key(key) {}
25
26 const std::array<uint8_t, X448_LEN>& key() const { return m_key; }
27
28 private:
29 std::array<uint8_t, X448_LEN> m_key;
30};
31
32class X448_PrivateKey_Data final {
33 public:
34 explicit X448_PrivateKey_Data(secure_vector<uint8_t> key) : m_key(std::move(key)) {}
35
36 const secure_vector<uint8_t>& key() const { return m_key; }
37
38 private:
40};
41
43 const auto& sk = m_private->key();
44 return {sk.begin(), sk.end()};
45}
46
47namespace {
48void x448_basepoint_from_data(std::span<uint8_t, X448_LEN> mypublic, std::span<const uint8_t, X448_LEN> secret) {
49 const auto bp = x448_basepoint(decode_scalar(secret));
50 const auto bp_bytes = encode_point(bp);
51 copy_mem(mypublic, bp_bytes);
52}
53
54secure_vector<uint8_t> ber_decode_sk(std::span<const uint8_t> key_bits) {
55 secure_vector<uint8_t> decoded_bits;
56 BER_Decoder(key_bits, BER_Decoder::Limits::DER()).decode(decoded_bits, ASN1_Type::OctetString).verify_end();
57 if(decoded_bits.size() != X448_LEN) {
58 throw Decoding_Error("Invalid size for X448 private key");
59 }
60 return decoded_bits;
61}
62
63// Given a secret key compute the public value and build the immutable public
64// and private key data objects.
65void load_x448_keypair(secure_vector<uint8_t> secret,
66 std::shared_ptr<const X448_PublicKey_Data>& pk_out,
67 std::shared_ptr<const X448_PrivateKey_Data>& sk_out) {
68 BOTAN_ASSERT_NOMSG(secret.size() == X448_LEN);
69 std::array<uint8_t, X448_LEN> pub{};
70 {
71 const auto scope = CT::scoped_poison(secret);
72 x448_basepoint_from_data(pub, std::span(secret).first<X448_LEN>());
73 CT::unpoison(pub);
74 }
75 pk_out = std::make_shared<const X448_PublicKey_Data>(pub);
76 sk_out = std::make_shared<const X448_PrivateKey_Data>(std::move(secret));
77}
78
79} // namespace
80
84
85bool X448_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
86 /*
87 * Every clamped scalar is a multiple of the cofactor, so multiplying by one
88 * sends exactly the points of low order to the identity (all zero output).
89 * Nothing else can be checked for a Montgomery u-coordinate.
90 */
91 const std::array<uint8_t, X448_LEN> scalar{}; // clamped to 2^447 by decode_scalar
92 const auto out = encode_point(x448(decode_scalar(scalar), decode_point(m_public->key())));
93 return !CT::all_zeros(out.data(), out.size()).as_bool();
94}
95
96std::vector<uint8_t> X448_PublicKey::raw_public_key_bits() const {
97 const auto& pub = m_public->key();
98 return {pub.begin(), pub.end()};
99}
100
101std::vector<uint8_t> X448_PublicKey::public_key_bits() const {
102 return raw_public_key_bits();
103}
104
105std::unique_ptr<Private_Key> X448_PublicKey::generate_another(RandomNumberGenerator& rng) const {
106 return std::make_unique<X448_PrivateKey>(rng);
107}
108
109X448_PublicKey::X448_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
110 X448_PublicKey(key_bits) {
111 // RFC 8410 Section 3: "the parameters MUST be absent".
112 if(!alg_id.parameters_are_empty()) {
113 throw Decoding_Error("Unexpected parameters for X448 public key");
114 }
115}
116
117X448_PublicKey::X448_PublicKey(std::span<const uint8_t> pub) {
118 if(pub.size() != X448_LEN) {
119 throw Decoding_Error("Invalid size for X448 public key");
120 }
121 std::array<uint8_t, X448_LEN> pub_arr{};
122 copy_mem(pub_arr, pub);
123 m_public = std::make_shared<const X448_PublicKey_Data>(pub_arr);
124}
125
126X448_PrivateKey::X448_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
127 X448_PrivateKey(ber_decode_sk(key_bits)) {
128 // RFC 8410 Section 3: "the parameters MUST be absent".
129 if(!alg_id.parameters_are_empty()) {
130 throw Decoding_Error("Unexpected parameters for X448 private key");
131 }
132}
133
134X448_PrivateKey::X448_PrivateKey(std::span<const uint8_t> secret_key) {
135 if(secret_key.size() != X448_LEN) {
136 throw Decoding_Error("Invalid size for X448 private key");
137 }
138 load_x448_keypair(secure_vector<uint8_t>(secret_key.begin(), secret_key.end()), m_public, m_private);
139}
140
142
143std::unique_ptr<Public_Key> X448_PrivateKey::public_key() const {
144 return std::make_unique<X448_PublicKey>(public_value());
145}
146
150
151bool X448_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const {
152 const auto& sk = m_private->key();
153 const auto& pub = m_public->key();
154 std::array<uint8_t, X448_LEN> public_point{};
155 BOTAN_ASSERT_NOMSG(sk.size() == X448_LEN);
156 const auto scope = CT::scoped_poison(sk);
157 x448_basepoint_from_data(public_point, std::span(sk).first<X448_LEN>());
158 return CT::is_equal(public_point.data(), pub.data(), pub.size()).as_bool();
159}
160
161namespace {
162
163/**
164* X448 operation
165*/
166class X448_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
167 public:
168 X448_KA_Operation(std::shared_ptr<const X448_PrivateKey_Data> key,
169 const PK_Key_Agreement_Options_Reader& options) :
170 PK_Ops::Key_Agreement_with_KDF(options), m_key(std::move(key)) {}
171
172 size_t agreed_value_size() const override { return X448_LEN; }
173
174 secure_vector<uint8_t> raw_agree(const uint8_t w_data[], size_t w_len) override {
175 const auto& sk = m_key->key();
176 BOTAN_ASSERT_NOMSG(sk.size() == X448_LEN);
177 const auto scope = CT::scoped_poison(sk);
178
179 const std::span<const uint8_t> w(w_data, w_len);
180 if(w.size() != X448_LEN) {
181 throw Decoding_Error("Invalid size for X448 public key");
182 }
183 const auto k = decode_scalar(sk);
184 const auto u = decode_point(w);
185
186 auto shared_secret = encode_point(x448(k, u));
187 CT::unpoison(shared_secret);
188
189 // RFC 7748 Section 6.2
190 // As with X25519, both sides MAY check, without leaking extra
191 // information about the value of K, whether the resulting shared K
192 // is the all-zero value and abort if so.
193 //
194 // TODO: once the generic Key Agreement operation creation is equipped
195 // with a more flexible parameterization, this check could be
196 // made optional.
197 // For instance: `sk->agree().with_optional_sanity_checks(true)`.
198 // See also: https://github.com/randombit/botan/pull/4318
199 if(CT::all_zeros(shared_secret.data(), shared_secret.size()).as_bool()) {
200 throw Invalid_Argument("X448 public point appears to be of low order");
201 }
202
203 return shared_secret;
204 }
205
206 private:
207 std::shared_ptr<const X448_PrivateKey_Data> m_key;
208};
209
210} // namespace
211
212std::unique_ptr<PK_Ops::Key_Agreement> X448_PrivateKey::_create_key_agreement_op(
213 RandomNumberGenerator& /*rng*/, const PK_Key_Agreement_Options_Reader& options) const {
215
216 return std::make_unique<X448_KA_Operation>(m_private, options);
217}
218
219} // namespace Botan
#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
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:161
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
std::unique_ptr< PK_Ops::Key_Agreement > _create_key_agreement_op(RandomNumberGenerator &rng, const PK_Key_Agreement_Options_Reader &options) const override
Definition x448.cpp:212
std::unique_ptr< Public_Key > public_key() const override
Definition x448.cpp:143
secure_vector< uint8_t > private_key_bits() const override
Definition x448.cpp:147
secure_vector< uint8_t > raw_private_key_bits() const override
Definition x448.cpp:42
X448_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition x448.cpp:126
std::vector< uint8_t > public_value() const override
Definition x448.h:95
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition x448.cpp:151
AlgorithmIdentifier algorithm_identifier() const override
Definition x448.cpp:81
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition x448.cpp:105
std::vector< uint8_t > raw_public_key_bits() const override
Definition x448.cpp:96
std::shared_ptr< const X448_PublicKey_Data > m_public
Definition x448.h:63
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition x448.cpp:85
X448_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition x448.cpp:109
std::vector< uint8_t > public_key_bits() const override
Definition x448.cpp:101
std::string algo_name() const override
Definition x448.h:39
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:803
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:790
Point448 x448_basepoint(const ScalarX448 &k)
Multiply a scalar with the base group element (5).
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
Point448 decode_point(std::span< const uint8_t > p_bytes)
Decode a point from a byte array. RFC 7748 Section 5 (decodeUCoordinate).
constexpr size_t X448_LEN
secure_vector< uint8_t > encode_point(const Point448 &p)
Encode a point to a 56 byte vector. RFC 7748 Section 5 (encodeUCoordinate).
void require_software_provider(const OptionsT &options, std::string_view algo_name)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
ScalarX448 decode_scalar(std::span< const uint8_t > scalar_bytes)
Decode a scalar from a byte array. RFC 7748 Section 5 (decodeScalar448).
Point448 x448(const ScalarX448 &k, const Point448 &u)
Multiply a scalar k with a point u.