Botan 3.11.0
Crypto and TLS for C&
ecdh.cpp
Go to the documentation of this file.
1/*
2* ECDH implementation
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/bigint.h>
13#include <botan/ec_group.h>
14#include <botan/internal/pk_ops_impl.h>
15
16namespace Botan {
17
18std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const {
19 return std::make_unique<ECDH_PublicKey>(domain(), _public_ec_point());
20}
21
22namespace {
23
24/**
25* ECDH operation
26*/
27class ECDH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
28 public:
29 ECDH_KA_Operation(const ECDH_PrivateKey& key, std::string_view kdf, RandomNumberGenerator& rng) :
30 PK_Ops::Key_Agreement_with_KDF(kdf),
31 m_group(key.domain()),
32 m_l_times_priv(mul_cofactor_inv(m_group, key._private_key())),
33 m_rng(rng) {}
34
35 size_t agreed_value_size() const override { return m_group.get_p_bytes(); }
36
37 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override {
38 const auto input_point = [&] {
39 if(m_group.has_cofactor()) {
40#if defined(BOTAN_HAS_LEGACY_EC_POINT)
41 return EC_AffinePoint(m_group, m_group.get_cofactor() * m_group.OS2ECP(w, w_len));
42#else
43 throw Not_Implemented(
44 "Support for DH with cofactor adjustment not available in this build configuration");
45#endif
46 } else {
47 if(auto point = EC_AffinePoint::deserialize(m_group, {w, w_len})) {
48 return *point;
49 } else {
50 throw Decoding_Error("ECDH - Invalid elliptic curve point: not on curve");
51 }
52 }
53 }();
54
55 // Typical specs (such as BSI's TR-03111 Section 4.3.1) require that
56 // we check the resulting point of the multiplication to not be the
57 // point at infinity. However, since we ensure that our ECC private
58 // scalar can never be zero, checking the peer's input point is
59 // equivalent.
60 if(input_point.is_identity()) {
61 throw Decoding_Error("ECDH - Invalid elliptic curve point: identity");
62 }
63
64 return input_point.mul_x_only(m_l_times_priv, m_rng);
65 }
66
67 private:
68 static EC_Scalar mul_cofactor_inv(const EC_Group& group, const EC_Scalar& x) {
69 // We implement BSI TR-03111 ECKAEG which only matters in the (rare/deprecated)
70 // case of a curve with cofactor.
71
72 if(group.has_cofactor()) {
73 // We could precompute this but cofactors are rare
74 return x * EC_Scalar::from_bigint(group, group.get_cofactor()).invert_vartime();
75 } else {
76 return x;
77 }
78 }
79
80 const EC_Group m_group;
81 const EC_Scalar m_l_times_priv;
82 RandomNumberGenerator& m_rng;
83};
84
85} // namespace
86
87std::unique_ptr<Private_Key> ECDH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
88 return std::make_unique<ECDH_PrivateKey>(rng, domain());
89}
90
91std::vector<uint8_t> ECDH_PublicKey::public_value(EC_Point_Format format) const {
92 return _public_ec_point().serialize(format);
93}
94
95std::unique_ptr<PK_Ops::Key_Agreement> ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
96 std::string_view params,
97 std::string_view provider) const {
98 if(provider == "base" || provider.empty()) {
99 return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
100 }
101
102 throw Provider_Not_Found(algo_name(), provider);
103}
104
105} // namespace Botan
std::unique_ptr< Public_Key > public_key() const override
Definition ecdh.cpp:18
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:95
std::vector< uint8_t > public_value() const
Definition ecdh.h:55
std::string algo_name() const override
Definition ecdh.h:50
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition ecdh.cpp:87
static std::optional< EC_AffinePoint > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
std::vector< uint8_t > serialize(EC_Point_Format format) const
Return an encoding depending on the requested format.
const EC_Group & domain() const
Definition ecc_key.cpp:64
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.cpp:76
static EC_Scalar from_bigint(const EC_Group &group, const BigInt &bn)
Definition ec_scalar.cpp:69
EC_Scalar invert_vartime() const
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68