Botan 3.6.1
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),
30 m_group(key.domain()),
31 m_l_times_priv(mul_cofactor_inv(m_group, key._private_key())),
32 m_rng(rng) {}
33
34 size_t agreed_value_size() const override { return m_group.get_p_bytes(); }
35
36 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override {
37 if(m_group.has_cofactor()) {
38 EC_AffinePoint input_point(m_group, m_group.get_cofactor() * m_group.OS2ECP(w, w_len));
39 return input_point.mul(m_l_times_priv, m_rng, m_ws).x_bytes();
40 } else {
41 if(auto input_point = EC_AffinePoint::deserialize(m_group, {w, w_len})) {
42 return input_point->mul(m_l_times_priv, m_rng, m_ws).x_bytes();
43 } else {
44 throw Decoding_Error("ECDH - Invalid elliptic curve point");
45 }
46 }
47 }
48
49 private:
50 static EC_Scalar mul_cofactor_inv(const EC_Group& group, const EC_Scalar& x) {
51 // We implement BSI TR-03111 ECKAEG which only matters in the (rare/deprecated)
52 // case of a curve with cofactor.
53
54 if(group.has_cofactor()) {
55 // We could precompute this but cofactors are rare
56 return x * EC_Scalar::from_bigint(group, group.get_cofactor()).invert();
57 } else {
58 return x;
59 }
60 }
61
62 const EC_Group m_group;
63 const EC_Scalar m_l_times_priv;
64 RandomNumberGenerator& m_rng;
65 std::vector<BigInt> m_ws;
66};
67
68} // namespace
69
70std::unique_ptr<Private_Key> ECDH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
71 return std::make_unique<ECDH_PrivateKey>(rng, domain());
72}
73
74std::unique_ptr<PK_Ops::Key_Agreement> ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
75 std::string_view params,
76 std::string_view provider) const {
77 if(provider == "base" || provider.empty()) {
78 return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
79 }
80
81 throw Provider_Not_Found(algo_name(), provider);
82}
83
84} // 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:74
std::string algo_name() const override
Definition ecdh.h:41
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition ecdh.cpp:70
static std::optional< EC_AffinePoint > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
Definition ec_apoint.cpp:97
const EC_Group & domain() const
Definition ecc_key.cpp:59
const EC_Point & public_point() const
Definition ecc_key.cpp:64
static EC_Scalar from_bigint(const EC_Group &group, const BigInt &bn)
Definition ec_scalar.cpp:65
EC_Scalar invert() const
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61