Botan 2.19.5 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
14 namespace Botan {
15
16 namespace {
17
18/**
19* ECDH operation
20*/
21 class ECDH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF
22 {
23 public:
24
25 ECDH_KA_Operation(const ECDH_PrivateKey& key, const std::string& kdf, RandomNumberGenerator& rng) :
26 PK_Ops::Key_Agreement_with_KDF(kdf),
27 m_group(key.domain()),
28 m_rng(rng)
29 {
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 {
37 PointGFp input_point = m_group.get_cofactor() * m_group.OS2ECP(w, w_len);
38 input_point.randomize_repr(m_rng);
39
40 const PointGFp S = m_group.blinded_var_point_multiply(
41 input_point, m_l_times_priv, m_rng, m_ws);
42
43 if(S.on_the_curve() == false)
44 throw Internal_Error("ECDH agreed value was not on the curve");
45 return BigInt::encode_1363(S.get_affine_x(), m_group.get_p_bytes());
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}
55
56std::unique_ptr<PK_Ops::Key_Agreement>
57 ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
58 const std::string& params,
59 const std::string& provider) const
60 {
61 if(provider == "base" || provider.empty())
62 return std::unique_ptr<PK_Ops::Key_Agreement>(new ECDH_KA_Operation(*this, params, rng));
63
64 throw Provider_Not_Found(algo_name(), provider);
65 }
66
67
68}
Botan::BigInt::encode_1363
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:111
Botan::ECDH_PrivateKey::create_key_agreement_op
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:57
Botan::ECDH_PublicKey::algo_name
std::string algo_name() const override
Definition: ecdh.h:45
Botan::Provider_Not_Found
Definition: exceptn.h:279
Botan::RandomNumberGenerator
Definition: rng.h:26
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
Botan::OCSP::Response_Status_Code::Internal_Error
@ Internal_Error
Botan
Definition: alg_id.cpp:13