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

randombit / botan / 16244527778

13 Jul 2025 02:43AM UTC coverage: 90.572% (-0.003%) from 90.575%
16244527778

push

github

web-flow
Merge pull request #4978 from randombit/jack/clang-tidy-misc-redundant-expression

Enable and fix clang-tidy warning misc-redundant-expression

99098 of 109413 relevant lines covered (90.57%)

12337696.32 hits per line

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

92.19
/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/ecdh.h>
12
#endif
13

14
namespace Botan_Tests {
15

16
namespace {
17

18
#if defined(BOTAN_HAS_ECDH)
19

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

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

26
      bool skip_this_test(const std::string& group_id, const VarMap&) override {
156✔
27
         return !Botan::EC_Group::supports_named_group(group_id);
156✔
28
      }
29

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

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

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

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

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

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

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

67
            result.start_timer();
28✔
68

69
            const std::string kdf = "Raw";
28✔
70

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

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

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

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

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

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

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

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

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

114
            result.end_timer();
28✔
115

116
            results.push_back(result);
28✔
117
         }
28✔
118

119
         return results;
1✔
120
      }
×
121
};
122

123
BOTAN_REGISTER_TEST("pubkey", "ecdh_kat", ECDH_KAT_Tests);
124
BOTAN_REGISTER_TEST("pubkey", "ecdh_keygen", ECDH_Keygen_Tests);
125
BOTAN_REGISTER_TEST("pubkey", "ecdh_all_groups", ECDH_AllGroups_Tests);
126

127
#endif
128

129
}  // namespace
130

131
}  // 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