/src/Botan-3.4.0/src/lib/pubkey/ecdh/ecdh.cpp
Line | Count | Source |
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 | | |
15 | | namespace Botan { |
16 | | |
17 | 0 | std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const { |
18 | 0 | return std::make_unique<ECDH_PublicKey>(domain(), public_point()); |
19 | 0 | } |
20 | | |
21 | | namespace { |
22 | | |
23 | | /** |
24 | | * ECDH operation |
25 | | */ |
26 | | class 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 | 0 | PK_Ops::Key_Agreement_with_KDF(kdf), m_group(key.domain()), m_rng(rng) { |
30 | 0 | m_l_times_priv = m_group.inverse_mod_order(m_group.get_cofactor()) * key.private_value(); |
31 | 0 | } |
32 | | |
33 | 0 | size_t agreed_value_size() const override { return m_group.get_p_bytes(); } |
34 | | |
35 | 0 | secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override { |
36 | 0 | EC_Point input_point = m_group.get_cofactor() * m_group.OS2ECP(w, w_len); |
37 | 0 | input_point.randomize_repr(m_rng); |
38 | |
|
39 | 0 | const EC_Point S = m_group.blinded_var_point_multiply(input_point, m_l_times_priv, m_rng, m_ws); |
40 | |
|
41 | 0 | if(S.on_the_curve() == false) { |
42 | 0 | throw Internal_Error("ECDH agreed value was not on the curve"); |
43 | 0 | } |
44 | 0 | return BigInt::encode_1363(S.get_affine_x(), m_group.get_p_bytes()); |
45 | 0 | } |
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 | | |
56 | 0 | std::unique_ptr<Private_Key> ECDH_PublicKey::generate_another(RandomNumberGenerator& rng) const { |
57 | 0 | return std::make_unique<ECDH_PrivateKey>(rng, domain()); |
58 | 0 | } |
59 | | |
60 | | std::unique_ptr<PK_Ops::Key_Agreement> ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng, |
61 | | std::string_view params, |
62 | 0 | std::string_view provider) const { |
63 | 0 | if(provider == "base" || provider.empty()) { |
64 | 0 | return std::make_unique<ECDH_KA_Operation>(*this, params, rng); |
65 | 0 | } |
66 | | |
67 | 0 | throw Provider_Not_Found(algo_name(), provider); |
68 | 0 | } |
69 | | |
70 | | } // namespace Botan |