Botan 3.5.0
Crypto and TLS for C&
ecdh.cpp
Go to the documentation of this file.
1/*
2* ECDH implemenation
3* (C) 2007 Manuel Hartl, FlexSecure GmbH
4* 2007 Falko Strenzke, FlexSecure GmbH
5* 2008-2010 Jack Lloyd
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/ecdh.h>
11
12#include <botan/numthry.h>
13#include <botan/internal/pk_ops_impl.h>
14
15namespace Botan {
16
17std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const {
18 return std::make_unique<ECDH_PublicKey>(domain(), public_point());
19}
20
21namespace {
22
23/**
24* ECDH operation
25*/
26class ECDH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
27 public:
28 ECDH_KA_Operation(const ECDH_PrivateKey& key, std::string_view kdf, RandomNumberGenerator& rng) :
29 PK_Ops::Key_Agreement_with_KDF(kdf), m_group(key.domain()), m_rng(rng) {
30 m_l_times_priv = m_group.inverse_mod_order(m_group.get_cofactor()) * key.private_value();
31 }
32
33 size_t agreed_value_size() const override { return m_group.get_p_bytes(); }
34
35 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override {
36 EC_Point input_point = m_group.get_cofactor() * m_group.OS2ECP(w, w_len);
37 input_point.randomize_repr(m_rng);
38
39 const EC_Point S = m_group.blinded_var_point_multiply(input_point, m_l_times_priv, m_rng, m_ws);
40
41 if(S.on_the_curve() == false) {
42 throw Internal_Error("ECDH agreed value was not on the curve");
43 }
44
45 return S.x_bytes();
46 }
47
48 private:
49 const EC_Group m_group;
50 BigInt m_l_times_priv;
51 RandomNumberGenerator& m_rng;
52 std::vector<BigInt> m_ws;
53};
54
55} // namespace
56
57std::unique_ptr<Private_Key> ECDH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
58 return std::make_unique<ECDH_PrivateKey>(rng, domain());
59}
60
61std::unique_ptr<PK_Ops::Key_Agreement> ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
62 std::string_view params,
63 std::string_view provider) const {
64 if(provider == "base" || provider.empty()) {
65 return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
66 }
67
68 throw Provider_Not_Found(algo_name(), provider);
69}
70
71} // namespace Botan
std::unique_ptr< Public_Key > public_key() const override
Definition ecdh.cpp:17
std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition ecdh.cpp:61
std::string algo_name() const override
Definition ecdh.h:41
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition ecdh.cpp:57
const BigInt & private_value() const
Definition ecc_key.cpp:82
const EC_Group & domain() const
Definition ecc_key.h:56
const EC_Point & public_point() const
Definition ecc_key.h:40
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61