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>
20 const uint8_t basepoint[32] = {9};
26void size_check(
size_t size,
const char* thing) {
28 throw Decoding_Error(
fmt(
"Invalid size {} for X25519 {}", size, thing));
52 m_public.assign(pub.begin(), pub.end());
54 size_check(
m_public.size(),
"public key");
66 return std::make_unique<X25519_PrivateKey>(rng);
70 if(secret_key.size() != 32) {
75 m_private = secret_key;
88 size_check(m_private.size(),
"private key");
94 return std::make_unique<X25519_PublicKey>(
public_value());
102 std::vector<uint8_t> public_point(32);
108 size_check(w_len,
"public value");
109 return curve25519(m_private, w);
120 PK_Ops::Key_Agreement_with_KDF(kdf), m_key(key) {}
122 size_t agreed_value_size()
const override {
return 32; }
125 auto shared_key = m_key.agree(w, w_len);
136 if(
CT::all_zeros(shared_key.data(), shared_key.size()).as_bool()) {
137 throw Invalid_Argument(
"X25519 public point appears to be of low order");
144 const X25519_PrivateKey& m_key;
150 std::string_view params,
151 std::string_view provider)
const {
152 if(provider ==
"base" || provider.empty()) {
153 return std::make_unique<X25519_KA_Operation>(*
this, params);
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
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
X25519_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
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
std::vector< uint8_t > m_public
X25519_PublicKey()=default
std::vector< uint8_t > public_key_bits() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
int(* final)(unsigned char *, CTX *)
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])