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/pk_options_impl.h>
18#include <botan/internal/x448_internal.h>
22class X448_PublicKey_Data final {
24 explicit X448_PublicKey_Data(std::array<uint8_t, X448_LEN> key) : m_key(key) {}
26 const std::array<uint8_t, X448_LEN>& key()
const {
return m_key; }
29 std::array<uint8_t, X448_LEN> m_key;
32class X448_PrivateKey_Data final {
43 const auto& sk = m_private->key();
44 return {sk.begin(), sk.end()};
48void x448_basepoint_from_data(std::span<uint8_t, X448_LEN> mypublic, std::span<const uint8_t, X448_LEN> secret) {
57 if(decoded_bits.size() !=
X448_LEN) {
58 throw Decoding_Error(
"Invalid size for X448 private key");
66 std::shared_ptr<const X448_PublicKey_Data>& pk_out,
67 std::shared_ptr<const X448_PrivateKey_Data>& sk_out) {
69 std::array<uint8_t, X448_LEN> pub{};
72 x448_basepoint_from_data(pub, std::span(secret).first<X448_LEN>());
75 pk_out = std::make_shared<const X448_PublicKey_Data>(pub);
76 sk_out = std::make_shared<const X448_PrivateKey_Data>(std::move(secret));
91 const std::array<uint8_t, X448_LEN> scalar{};
98 return {pub.begin(), pub.end()};
106 return std::make_unique<X448_PrivateKey>(rng);
113 throw Decoding_Error(
"Unexpected parameters for X448 public key");
121 std::array<uint8_t, X448_LEN> pub_arr{};
123 m_public = std::make_shared<const X448_PublicKey_Data>(pub_arr);
130 throw Decoding_Error(
"Unexpected parameters for X448 private key");
144 return std::make_unique<X448_PublicKey>(
public_value());
152 const auto& sk = m_private->key();
154 std::array<uint8_t, X448_LEN> public_point{};
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();
168 X448_KA_Operation(std::shared_ptr<const X448_PrivateKey_Data> key,
170 PK_Ops::Key_Agreement_with_KDF(options), m_key(std::move(key)) {}
172 size_t agreed_value_size()
const override {
return X448_LEN; }
175 const auto& sk = m_key->key();
179 const std::span<const uint8_t> w(w_data, w_len);
181 throw Decoding_Error(
"Invalid size for X448 public key");
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");
203 return shared_secret;
207 std::shared_ptr<const X448_PrivateKey_Data> m_key;
216 return std::make_unique<X448_KA_Operation>(m_private, options);
#define BOTAN_ASSERT_NOMSG(expr)
bool parameters_are_empty() const
virtual OID object_identifier() const
secure_vector< uint8_t > get_contents()
DER_Encoder & encode(bool b)
std::unique_ptr< PK_Ops::Key_Agreement > _create_key_agreement_op(RandomNumberGenerator &rng, const PK_Key_Agreement_Options_Reader &options) const override
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
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).
void require_software_provider(const OptionsT &options, std::string_view algo_name)
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.