Botan 3.0.0-alpha0
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#include <botan/numthry.h>
12#include <botan/internal/pk_ops_impl.h>
13
14namespace Botan {
15
16std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const
17 {
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 {
28 public:
29
30 ECDH_KA_Operation(const ECDH_PrivateKey& key, const std::string& kdf, RandomNumberGenerator& rng) :
31 PK_Ops::Key_Agreement_with_KDF(kdf),
32 m_group(key.domain()),
33 m_rng(rng)
34 {
35 m_l_times_priv = m_group.inverse_mod_order(m_group.get_cofactor()) * key.private_value();
36 }
37
38 size_t agreed_value_size() const override { return m_group.get_p_bytes(); }
39
40 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override
41 {
42 PointGFp input_point = m_group.get_cofactor() * m_group.OS2ECP(w, w_len);
43 input_point.randomize_repr(m_rng);
44
45 const PointGFp S = m_group.blinded_var_point_multiply(
46 input_point, m_l_times_priv, m_rng, m_ws);
47
48 if(S.on_the_curve() == false)
49 throw Internal_Error("ECDH agreed value was not on the curve");
50 return BigInt::encode_1363(S.get_affine_x(), m_group.get_p_bytes());
51 }
52 private:
53 const EC_Group m_group;
54 BigInt m_l_times_priv;
55 RandomNumberGenerator& m_rng;
56 std::vector<BigInt> m_ws;
57 };
58
59}
60
61std::unique_ptr<PK_Ops::Key_Agreement>
63 const std::string& params,
64 const std::string& provider) const
65 {
66 if(provider == "base" || provider.empty())
67 return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
68
69 throw Provider_Not_Found(algo_name(), provider);
70 }
71
72
73}
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:106
std::unique_ptr< Public_Key > public_key() const override
Definition: ecdh.cpp:16
std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: ecdh.cpp:62
std::string algo_name() const override
Definition: ecdh.h:45
const BigInt & private_value() const
Definition: ecc_key.cpp:98
const EC_Group & domain() const
Definition: ecc_key.h:56
const PointGFp & public_point() const
Definition: ecc_key.h:41
int(* final)(unsigned char *, CTX *)
Definition: alg_id.cpp:13