Coverage Report

Created: 2022-05-14 06:06

/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
namespace Botan {
15
16
std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const
17
0
   {
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
   {
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
14.4k
         {
35
14.4k
         m_l_times_priv = m_group.inverse_mod_order(m_group.get_cofactor()) * key.private_value();
36
14.4k
         }
37
38
0
      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
14.4k
         {
42
14.4k
         PointGFp input_point = m_group.get_cofactor() * m_group.OS2ECP(w, w_len);
43
14.4k
         input_point.randomize_repr(m_rng);
44
45
14.4k
         const PointGFp S = m_group.blinded_var_point_multiply(
46
14.4k
            input_point, m_l_times_priv, m_rng, m_ws);
47
48
14.4k
         if(S.on_the_curve() == false)
49
0
            throw Internal_Error("ECDH agreed value was not on the curve");
50
14.4k
         return BigInt::encode_1363(S.get_affine_x(), m_group.get_p_bytes());
51
14.4k
         }
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
61
std::unique_ptr<PK_Ops::Key_Agreement>
62
ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
63
                                         const std::string& params,
64
                                         const std::string& provider) const
65
14.4k
   {
66
14.4k
   if(provider == "base" || provider.empty())
67
14.4k
      return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
68
69
0
   throw Provider_Not_Found(algo_name(), provider);
70
14.4k
   }
71
72
73
}