Botan 3.3.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 return BigInt::encode_1363(S.get_affine_x(), m_group.get_p_bytes());
45 }
46
47 private:
48 const EC_Group m_group;
49 BigInt m_l_times_priv;
50 RandomNumberGenerator& m_rng;
51 std::vector<BigInt> m_ws;
52};
53
54} // namespace
55
56std::unique_ptr<Private_Key> ECDH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
57 return std::make_unique<ECDH_PrivateKey>(rng, domain());
58}
59
60std::unique_ptr<PK_Ops::Key_Agreement> ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
61 std::string_view params,
62 std::string_view provider) const {
63 if(provider == "base" || provider.empty()) {
64 return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
65 }
66
67 throw Provider_Not_Found(algo_name(), provider);
68}
69
70} // namespace Botan
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:105
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:60
std::string algo_name() const override
Definition ecdh.h:41
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition ecdh.cpp:56
const BigInt & private_value() const
Definition ecc_key.cpp:77
const EC_Group & domain() const
Definition ecc_key.h:54
const EC_Point & public_point() const
Definition ecc_key.h:40
int(* final)(unsigned char *, CTX *)