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

randombit / botan / 21753596263

06 Feb 2026 02:13PM UTC coverage: 90.063% (-0.01%) from 90.073%
21753596263

Pull #5289

github

web-flow
Merge 587099284 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102237 of 113517 relevant lines covered (90.06%)

11402137.11 hits per line

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

95.31
/src/tests/test_ecdh.cpp
1
/*
2
* (C) 2015,2016 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_ECDH)
10
   #include "test_pubkey.h"
11
   #include <botan/ec_group.h>
12
   #include <botan/ecdh.h>
13
   #include <botan/pubkey.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_ECDH)
21

22
class ECDH_KAT_Tests final : public PK_Key_Agreement_Test {
23
   public:
24
      ECDH_KAT_Tests() : PK_Key_Agreement_Test("ECDH", "pubkey/ecdh.vec", "Secret,CounterKey,K", "KDF") {}
2✔
25

26
      std::string default_kdf(const VarMap& /*unused*/) const override { return "Raw"; }
156✔
27

28
      bool skip_this_test(const std::string& group_id, const VarMap& /*vars*/) override {
156✔
29
         return !Botan::EC_Group::supports_named_group(group_id);
156✔
30
      }
31

32
      std::unique_ptr<Botan::Private_Key> load_our_key(const std::string& group_id, const VarMap& vars) override {
156✔
33
         const auto group = Botan::EC_Group::from_name(group_id);
156✔
34
         const Botan::BigInt secret = vars.get_req_bn("Secret");
156✔
35
         return std::make_unique<Botan::ECDH_PrivateKey>(this->rng(), group, secret);
468✔
36
      }
156✔
37

38
      std::vector<uint8_t> load_their_key(const std::string& /*header*/, const VarMap& vars) override {
156✔
39
         return vars.get_req_bin("CounterKey");
312✔
40
      }
41
};
42

43
class ECDH_Keygen_Tests final : public PK_Key_Generation_Test {
1✔
44
   public:
45
      std::vector<std::string> keygen_params() const override {
1✔
46
         return {
1✔
47
            "secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1", "frp256v1"};
1✔
48
      }
49

50
      std::string algo_name() const override { return "ECDH"; }
7✔
51

52
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
7✔
53
                                                             std::string_view /* provider */,
54
                                                             std::span<const uint8_t> raw_pk) const override {
55
         const auto group = Botan::EC_Group(keygen_params);
7✔
56
         const auto public_key = Botan::EC_AffinePoint(group, raw_pk);
7✔
57
         return std::make_unique<Botan::ECDH_PublicKey>(group, public_key);
21✔
58
      }
7✔
59
};
60

61
class ECDH_AllGroups_Tests : public Test {
1✔
62
   public:
63
      std::vector<Test::Result> run() override {
1✔
64
         std::vector<Test::Result> results;
1✔
65

66
         for(const std::string& group_name : Botan::EC_Group::known_named_groups()) {
29✔
67
            Test::Result result("ECDH " + group_name);
28✔
68

69
            result.start_timer();
28✔
70

71
            const std::string kdf = "Raw";
28✔
72

73
            try {
28✔
74
               const auto group = Botan::EC_Group::from_name(group_name);
28✔
75

76
               // Regression test: prohibit loading an all-zero private key
77
               result.test_throws<Botan::Invalid_Argument>("all-zero private key is unacceptable", [&] {
56✔
78
                  const auto one = Botan::EC_Scalar::one(group);
28✔
79
                  const auto zero = one - one;  // NOLINT(*-redundant-expression)
28✔
80
                  Botan::ECDH_PrivateKey(group, zero);
28✔
81
               });
56✔
82

83
               // Regression test: prohibit loading a public point that is the identity (point at infinity)
84
               result.test_throws<Botan::Invalid_Argument>("point at infinity isn't a valid public key", [&] {
56✔
85
                  const auto infinity = Botan::EC_AffinePoint::identity(group);
28✔
86
                  Botan::ECDH_PublicKey(group, infinity);
28✔
87
               });
28✔
88

89
               // Regression test: prohibit ECDH-agreement with all-zero public value
90
               result.test_throws<Botan::Decoding_Error>("ECDH public value is point-at-infinity", [&] {
56✔
91
                  const auto sk = Botan::ECDH_PrivateKey(rng(), group);
28✔
92
                  const Botan::PK_Key_Agreement ka(sk, rng(), kdf);
28✔
93
                  std::vector<uint8_t> sec1_infinity(1, 0x00);
28✔
94
                  const auto a_ss = ka.derive_key(0, sec1_infinity);
28✔
95
               });
56✔
96

97
               for(size_t i = 0; i != 100; ++i) {
2,828✔
98
                  const Botan::ECDH_PrivateKey a_priv(rng(), group);
2,800✔
99
                  const auto a_pub = a_priv.public_value();
2,800✔
100

101
                  const Botan::ECDH_PrivateKey b_priv(rng(), group);
2,800✔
102
                  const auto b_pub = b_priv.public_value();
2,800✔
103

104
                  const Botan::PK_Key_Agreement a_ka(a_priv, rng(), kdf);
2,800✔
105
                  const auto a_ss = a_ka.derive_key(0, b_pub);
2,800✔
106

107
                  const Botan::PK_Key_Agreement b_ka(b_priv, rng(), kdf);
2,800✔
108
                  const auto b_ss = b_ka.derive_key(0, a_pub);
2,800✔
109

110
                  result.test_eq("Same shared secret", a_ss.bits_of(), b_ss.bits_of());
11,200✔
111
               }
11,200✔
112
            } catch(std::exception& e) {
28✔
113
               result.test_failure("Exception", e.what());
×
114
            }
×
115

116
            result.end_timer();
28✔
117

118
            results.push_back(result);
28✔
119
         }
28✔
120

121
         return results;
1✔
122
      }
×
123
};
124

125
BOTAN_REGISTER_TEST("pubkey", "ecdh_kat", ECDH_KAT_Tests);
126
BOTAN_REGISTER_TEST("pubkey", "ecdh_keygen", ECDH_Keygen_Tests);
127
BOTAN_REGISTER_TEST("pubkey", "ecdh_all_groups", ECDH_AllGroups_Tests);
128

129
#endif
130

131
}  // namespace
132

133
}  // namespace Botan_Tests
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

© 2026 Coveralls, Inc