11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
13#include <botan/mem_ops.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/pk_ops_impl.h>
17#include <botan/internal/x448_internal.h>
21class X448_PublicKey_Data final {
23 explicit X448_PublicKey_Data(std::array<uint8_t, X448_LEN> key) : m_key(key) {}
25 const std::array<uint8_t, X448_LEN>& key()
const {
return m_key; }
28 std::array<uint8_t, X448_LEN> m_key;
31class X448_PrivateKey_Data final {
42 const auto& sk = m_private->key();
43 return {sk.begin(), sk.end()};
47void x448_basepoint_from_data(std::span<uint8_t, X448_LEN> mypublic, std::span<const uint8_t, X448_LEN> secret) {
56 if(decoded_bits.size() !=
X448_LEN) {
57 throw Decoding_Error(
"Invalid size for X448 private key");
65 std::shared_ptr<const X448_PublicKey_Data>& pk_out,
66 std::shared_ptr<const X448_PrivateKey_Data>& sk_out) {
68 std::array<uint8_t, X448_LEN> pub{};
71 x448_basepoint_from_data(pub, std::span(secret).first<X448_LEN>());
74 pk_out = std::make_shared<const X448_PublicKey_Data>(pub);
75 sk_out = std::make_shared<const X448_PrivateKey_Data>(std::move(secret));
90 return {pub.begin(), pub.end()};
98 return std::make_unique<X448_PrivateKey>(rng);
105 throw Decoding_Error(
"Unexpected parameters for X448 public key");
111 std::array<uint8_t, X448_LEN> pub_arr{};
113 m_public = std::make_shared<const X448_PublicKey_Data>(pub_arr);
120 throw Decoding_Error(
"Unexpected parameters for X448 private key");
132 return std::make_unique<X448_PublicKey>(
public_value());
140 const auto& sk = m_private->key();
142 std::array<uint8_t, X448_LEN> public_point{};
145 x448_basepoint_from_data(public_point, std::span(sk).first<X448_LEN>());
146 return CT::is_equal(public_point.data(), pub.data(), pub.size()).as_bool();
156 X448_KA_Operation(std::shared_ptr<const X448_PrivateKey_Data> key, std::string_view kdf) :
157 PK_Ops::Key_Agreement_with_KDF(kdf), m_key(std::move(key)) {}
159 size_t agreed_value_size()
const override {
return X448_LEN; }
162 const auto& sk = m_key->key();
166 const std::span<const uint8_t> w(w_data, w_len);
168 throw Decoding_Error(
"Invalid size for X448 public key");
186 if(
CT::all_zeros(shared_secret.data(), shared_secret.size()).as_bool()) {
187 throw Invalid_Argument(
"X448 public point appears to be of low order");
190 return shared_secret;
194 std::shared_ptr<const X448_PrivateKey_Data> m_key;
200 std::string_view params,
201 std::string_view provider)
const {
202 if(provider ==
"base" || provider.empty()) {
203 return std::make_unique<X448_KA_Operation>(m_private, params);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
bool parameters_are_empty() const
virtual OID object_identifier() const
secure_vector< uint8_t > get_contents()
DER_Encoder & encode(bool b)
std::unique_ptr< Public_Key > public_key() const override
secure_vector< uint8_t > private_key_bits() const override
secure_vector< uint8_t > raw_private_key_bits() const override
X448_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
std::vector< uint8_t > public_value() const override
std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
AlgorithmIdentifier algorithm_identifier() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::vector< uint8_t > raw_public_key_bits() const override
std::shared_ptr< const X448_PublicKey_Data > m_public
bool check_key(RandomNumberGenerator &rng, bool strong) const override
X448_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
std::vector< uint8_t > public_key_bits() const override
std::string algo_name() const override
constexpr auto scoped_poison(const Ts &... xs)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
constexpr void unpoison(const T *p, size_t n)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
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)
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).
std::vector< T, secure_allocator< T > > secure_vector
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.