• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13215274653

08 Feb 2025 11:38AM UTC coverage: 91.655% (-0.009%) from 91.664%
13215274653

Pull #4650

github

web-flow
Merge 107f31833 into bc555cd3c
Pull Request #4650: Reorganize code and reduce header dependencies

94836 of 103471 relevant lines covered (91.65%)

11230958.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/src/lib/pubkey/ecdh/ecdh.cpp
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/internal/pk_ops_impl.h>
13
#include <botan/bigint.h>
14

15
namespace Botan {
16

17
std::unique_ptr<Public_Key> ECDH_PrivateKey::public_key() const {
32✔
18
   return std::make_unique<ECDH_PublicKey>(domain(), _public_ec_point());
32✔
19
}
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) :
5,867✔
29
            PK_Ops::Key_Agreement_with_KDF(kdf),
30
            m_group(key.domain()),
5,867✔
31
            m_l_times_priv(mul_cofactor_inv(m_group, key._private_key())),
5,867✔
32
            m_rng(rng) {}
11,734✔
33

34
      size_t agreed_value_size() const override { return m_group.get_p_bytes(); }
170✔
35

36
      secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override {
5,883✔
37
         if(m_group.has_cofactor()) {
5,883✔
38
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
39
            EC_AffinePoint input_point(m_group, m_group.get_cofactor() * m_group.OS2ECP(w, w_len));
8✔
40
            return input_point.mul_x_only(m_l_times_priv, m_rng, m_ws);
8✔
41
#else
42
            throw Not_Implemented("Support for DH with cofactor adjustment not available in this build configuration");
43
#endif
44
         } else {
8✔
45
            if(auto input_point = EC_AffinePoint::deserialize(m_group, {w, w_len})) {
5,875✔
46
               return input_point->mul_x_only(m_l_times_priv, m_rng, m_ws);
5,875✔
47
            } else {
48
               throw Decoding_Error("ECDH - Invalid elliptic curve point");
×
49
            }
5,875✔
50
         }
51
      }
52

53
   private:
54
      static EC_Scalar mul_cofactor_inv(const EC_Group& group, const EC_Scalar& x) {
5,867✔
55
         // We implement BSI TR-03111 ECKAEG which only matters in the (rare/deprecated)
56
         // case of a curve with cofactor.
57

58
         if(group.has_cofactor()) {
5,867✔
59
            // We could precompute this but cofactors are rare
60
            return x * EC_Scalar::from_bigint(group, group.get_cofactor()).invert_vartime();
8✔
61
         } else {
62
            return x;
5,863✔
63
         }
64
      }
65

66
      const EC_Group m_group;
67
      const EC_Scalar m_l_times_priv;
68
      RandomNumberGenerator& m_rng;
69
      std::vector<BigInt> m_ws;
70
};
71

72
}  // namespace
73

74
std::unique_ptr<Private_Key> ECDH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
10✔
75
   return std::make_unique<ECDH_PrivateKey>(rng, domain());
20✔
76
}
77

78
std::vector<uint8_t> ECDH_PublicKey::public_value(EC_Point_Format format) const {
5,849✔
79
   return _public_ec_point().serialize(format);
5,849✔
80
}
81

82
std::unique_ptr<PK_Ops::Key_Agreement> ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
6,335✔
83
                                                                                std::string_view params,
84
                                                                                std::string_view provider) const {
85
   if(provider == "base" || provider.empty()) {
6,491✔
86
      return std::make_unique<ECDH_KA_Operation>(*this, params, rng);
5,867✔
87
   }
88

89
   throw Provider_Not_Found(algo_name(), provider);
936✔
90
}
91

92
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc