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