8#include <botan/x25519.h>
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/pk_ops_impl.h>
19class X25519_PublicKey_Data final {
21 explicit X25519_PublicKey_Data(std::vector<uint8_t> key) : m_key(std::move(key)) {}
23 const std::vector<uint8_t>& key()
const {
return m_key; }
26 std::vector<uint8_t> m_key;
29class X25519_PrivateKey_Data final {
40 return m_private->key();
44 return m_private->key();
48 const uint8_t basepoint[32] = {9};
54void size_check(
size_t size,
const char* thing) {
56 throw Decoding_Error(
fmt(
"Invalid size {} for X25519 {}", size, thing));
69 std::shared_ptr<const X25519_PublicKey_Data>& pk_out,
70 std::shared_ptr<const X25519_PrivateKey_Data>& sk_out) {
72 std::vector<uint8_t> pub(32);
74 pk_out = std::make_shared<const X25519_PublicKey_Data>(std::move(pub));
75 sk_out = std::make_shared<const X25519_PrivateKey_Data>(std::move(secret));
92 throw Decoding_Error(
"Unexpected parameters for X25519 public key");
97 size_check(pub.size(),
"public key");
98 m_public = std::make_shared<const X25519_PublicKey_Data>(std::vector<uint8_t>(pub.begin(), pub.end()));
110 return std::make_unique<X25519_PrivateKey>(rng);
114 if(secret_key.size() != 32) {
128 throw Decoding_Error(
"Unexpected parameters for X25519 private key");
134 size_check(secret_key.size(),
"private key");
135 load_x25519_keypair(std::move(secret_key),
m_public, m_private);
139 return std::make_unique<X25519_PublicKey>(
public_value());
147 std::vector<uint8_t> public_point(32);
149 return public_point ==
m_public->key();
153 size_check(w_len,
"public value");
154 return curve25519(m_private->key(), w);
164 X25519_KA_Operation(std::shared_ptr<const X25519_PrivateKey_Data> key, std::string_view kdf) :
165 PK_Ops::Key_Agreement_with_KDF(kdf), m_key(std::move(key)) {}
167 size_t agreed_value_size()
const override {
return 32; }
170 size_check(w_len,
"public value");
171 auto shared_key = curve25519(m_key->key(), w);
182 if(
CT::all_zeros(shared_key.data(), shared_key.size()).as_bool()) {
183 throw Invalid_Argument(
"X25519 public point appears to be of low order");
190 std::shared_ptr<const X25519_PrivateKey_Data> m_key;
196 std::string_view params,
197 std::string_view provider)
const {
198 if(provider ==
"base" || provider.empty()) {
199 return std::make_unique<X25519_KA_Operation>(m_private, params);
#define BOTAN_ASSERT_NOMSG(expr)
bool parameters_are_empty() const
virtual OID object_identifier() const
BER_Decoder & decode(bool &out)
BER_Decoder & discard_remaining()
secure_vector< uint8_t > get_contents()
DER_Encoder & encode(bool b)
void random_vec(std::span< uint8_t > v)
secure_vector< uint8_t > private_key_bits() const override
secure_vector< uint8_t > agree(const uint8_t w[], size_t w_len) const
std::unique_ptr< Public_Key > public_key() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
const secure_vector< uint8_t > & get_x() const
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
secure_vector< uint8_t > raw_private_key_bits() const override
X25519_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
std::shared_ptr< const X25519_PublicKey_Data > m_public
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::string algo_name() const override
std::vector< uint8_t > raw_public_key_bits() const override
AlgorithmIdentifier algorithm_identifier() const override
X25519_PublicKey()=default
std::vector< uint8_t > public_key_bits() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
X25519_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
std::string fmt(std::string_view format, const T &... args)
void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32])
std::vector< T, secure_allocator< T > > secure_vector
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32])