Coverage Report

Created: 2021-02-21 07:20

/src/botan/src/lib/pubkey/ecdh/ecdh.cpp
Line
Count
Source (jump to first uncovered line)
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
#if defined(BOTAN_HAS_OPENSSL)
15
  #include <botan/internal/openssl.h>
16
#endif
17
18
namespace Botan {
19
20
std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const
21
0
   {
22
0
   return std::unique_ptr<Public_Key>(new ECDH_PublicKey(domain(), public_point()));
23
0
   }
24
25
namespace {
26
27
/**
28
* ECDH operation
29
*/
30
class ECDH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF
31
   {
32
   public:
33
34
      ECDH_KA_Operation(const ECDH_PrivateKey& key, const std::string& kdf, RandomNumberGenerator& rng) :
35
         PK_Ops::Key_Agreement_with_KDF(kdf),
36
         m_group(key.domain()),
37
         m_rng(rng)
38
9.89k
         {
39
9.89k
         m_l_times_priv = m_group.inverse_mod_order(m_group.get_cofactor()) * key.private_value();
40
9.89k
         }
41
42
0
      size_t agreed_value_size() const override { return m_group.get_p_bytes(); }
43
44
      secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override
45
9.89k
         {
46
9.89k
         PointGFp input_point = m_group.get_cofactor() * m_group.OS2ECP(w, w_len);
47
9.89k
         input_point.randomize_repr(m_rng);
48
49
9.89k
         const PointGFp S = m_group.blinded_var_point_multiply(
50
9.89k
            input_point, m_l_times_priv, m_rng, m_ws);
51
52
9.89k
         if(S.on_the_curve() == false)
53
0
            throw Internal_Error("ECDH agreed value was not on the curve");
54
9.89k
         return BigInt::encode_1363(S.get_affine_x(), m_group.get_p_bytes());
55
9.89k
         }
56
   private:
57
      const EC_Group m_group;
58
      BigInt m_l_times_priv;
59
      RandomNumberGenerator& m_rng;
60
      std::vector<BigInt> m_ws;
61
   };
62
63
}
64
65
std::unique_ptr<PK_Ops::Key_Agreement>
66
ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
67
                                         const std::string& params,
68
                                         const std::string& provider) const
69
9.89k
   {
70
#if defined(BOTAN_HAS_OPENSSL)
71
   if(provider == "openssl" || provider.empty())
72
      {
73
      try
74
         {
75
         return make_openssl_ecdh_ka_op(*this, params);
76
         }
77
      catch(Lookup_Error&)
78
         {
79
         if(provider == "openssl")
80
            throw;
81
         }
82
      }
83
#endif
84
85
9.89k
   if(provider == "base" || provider.empty())
86
9.89k
      return std::unique_ptr<PK_Ops::Key_Agreement>(new ECDH_KA_Operation(*this, params, rng));
87
88
0
   throw Provider_Not_Found(algo_name(), provider);
89
0
   }
90
91
92
}