10#include <botan/internal/blinding.h>
11#include <botan/internal/dl_scheme.h>
12#include <botan/internal/pk_ops_impl.h>
21 m_public_key = std::make_shared<DL_PublicKey>(
group, y);
25 return m_public_key->public_key_as_bytes();
29 return m_public_key->estimated_strength();
33 return m_public_key->p_bits();
37 return m_public_key->get_int_field(
algo_name(), field);
41 return m_public_key->group();
49 return m_public_key->DER_encode();
53 return m_public_key->check_key(rng, strong);
57 return std::make_unique<DH_PrivateKey>(rng,
group());
61 m_private_key = std::make_shared<DL_PrivateKey>(
group, rng);
62 m_public_key = m_private_key->public_key();
66 m_private_key = std::make_shared<DL_PrivateKey>(
group, x);
67 m_public_key = m_private_key->public_key();
72 m_public_key = m_private_key->public_key();
76 return std::unique_ptr<DH_PublicKey>(
new DH_PublicKey(m_public_key));
84 return m_private_key->DER_encode();
88 return m_private_key->raw_private_key_bits();
92 return m_private_key->get_int_field(
algo_name(), field);
102 DH_KA_Operation(
const std::shared_ptr<const DL_PrivateKey>& key,
103 std::string_view kdf,
105 PK_Ops::Key_Agreement_with_KDF(kdf),
107 m_key_bits(m_key->private_key().bits()),
109 m_key->group().get_p(),
111 [](const
BigInt& k) {
return k; },
112 [
this](
const BigInt& k) {
113 const BigInt inv_k =
inverse_mod(k, group().get_p());
114 return powermod_x_p(inv_k);
117 size_t agreed_value_size()
const override {
return group().p_bytes(); }
119 secure_vector<uint8_t> raw_agree(
const uint8_t w[],
size_t w_len)
override;
122 const DL_Group& group()
const {
return m_key->group(); }
124 BigInt powermod_x_p(
const BigInt& v)
const {
return group().power_b_p(v, m_key->private_key(), m_key_bits); }
126 std::shared_ptr<const DL_PrivateKey> m_key;
127 std::shared_ptr<const Montgomery_Params> m_monty_p;
128 const size_t m_key_bits;
132secure_vector<uint8_t> DH_KA_Operation::raw_agree(
const uint8_t w[],
size_t w_len) {
133 BigInt v = BigInt::decode(w, w_len);
135 if(v <= 1 || v >= group().get_p()) {
136 throw Invalid_Argument(
"DH agreement - invalid key provided");
139 v = m_blinder.
blind(v);
143 return BigInt::encode_1363(v, group().p_bytes());
149 std::string_view params,
150 std::string_view provider)
const {
151 if(provider ==
"base" || provider.empty()) {
152 return std::make_unique<DH_KA_Operation>(this->m_private_key, params, rng);
virtual OID object_identifier() const
BigInt blind(const BigInt &x) const
BigInt unblind(const BigInt &x) const
std::vector< uint8_t > public_value() const override
secure_vector< uint8_t > raw_private_key_bits() const override
const BigInt & get_int_field(std::string_view field) const override
secure_vector< uint8_t > private_key_bits() const override
std::unique_ptr< Public_Key > public_key() 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
size_t key_length() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::vector< uint8_t > public_value() const
std::vector< uint8_t > public_key_bits() const override
friend class DH_PrivateKey
AlgorithmIdentifier algorithm_identifier() const override
const BigInt & get_int_field(std::string_view field) const override
std::string algo_name() const override
const DL_Group & group() const
size_t estimated_strength() const override
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
BigInt inverse_mod(const BigInt &n, const BigInt &mod)