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

randombit / botan / 12976324217

26 Jan 2025 04:16PM UTC coverage: 91.256% (+0.001%) from 91.255%
12976324217

push

github

web-flow
Merge pull request #4598 from randombit/jack/group-init-cleanup

Cleanup EC_Group and DL_Group construction

93964 of 102968 relevant lines covered (91.26%)

11550712.53 hits per line

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

90.91
/src/tests/test_dh.cpp
1
/*
2
* (C) 2014,2015 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_DIFFIE_HELLMAN)
10
   #include "test_pubkey.h"
11
   #include <botan/dh.h>
12
   #include <botan/dl_group.h>
13
   #include <botan/pubkey.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
21

22
class Diffie_Hellman_KAT_Tests final : public PK_Key_Agreement_Test {
23
   public:
24
      Diffie_Hellman_KAT_Tests() :
1✔
25
            PK_Key_Agreement_Test("Diffie-Hellman", "pubkey/dh.vec", "P,G,X,Y,K", "Q,KDF,OutLen") {}
2✔
26

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

29
      std::unique_ptr<Botan::Private_Key> load_our_key(const std::string& /*header*/, const VarMap& vars) override {
40✔
30
         const Botan::BigInt p = vars.get_req_bn("P");
40✔
31
         const Botan::BigInt q = vars.get_opt_bn("Q", 0);
80✔
32
         const Botan::BigInt g = vars.get_req_bn("G");
40✔
33
         const Botan::BigInt x = vars.get_req_bn("X");
40✔
34

35
         auto group = [&]() {
×
36
            if(q == 0) {
40✔
37
               return Botan::DL_Group(p, g);
12✔
38
            } else {
39
               return Botan::DL_Group(p, q, g);
28✔
40
            }
41
         }();
40✔
42

43
         return std::make_unique<Botan::DH_PrivateKey>(group, x);
80✔
44
      }
148✔
45

46
      std::vector<uint8_t> load_their_key(const std::string& /*header*/, const VarMap& vars) override {
40✔
47
         const Botan::BigInt p = vars.get_req_bn("P");
40✔
48
         const Botan::BigInt q = vars.get_opt_bn("Q", 0);
80✔
49
         const Botan::BigInt g = vars.get_req_bn("G");
40✔
50
         const Botan::BigInt y = vars.get_req_bn("Y");
40✔
51

52
         auto group = [&]() {
×
53
            if(q == 0) {
40✔
54
               return Botan::DL_Group(p, g);
12✔
55
            } else {
56
               return Botan::DL_Group(p, q, g);
28✔
57
            }
58
         }();
40✔
59

60
         Botan::DH_PublicKey key(group, y);
40✔
61
         return key.public_value();
40✔
62
      }
228✔
63

64
      std::vector<Test::Result> run_final_tests() override {
1✔
65
         Test::Result result("DH negative tests");
1✔
66

67
         const BigInt g("2");
1✔
68
         const BigInt p("58458002095536094658683755258523362961421200751439456159756164191494576279467");
1✔
69
         const Botan::DL_Group group(p, g);
1✔
70

71
         const Botan::BigInt x("46205663093589612668746163860870963912226379131190812163519349848291472898748");
1✔
72
         auto privkey = std::make_unique<Botan::DH_PrivateKey>(group, x);
1✔
73

74
         auto kas = std::make_unique<Botan::PK_Key_Agreement>(*privkey, this->rng(), "Raw");
1✔
75

76
         result.test_throws("agreement input too big", "DH agreement - invalid key provided", [&kas]() {
2✔
77
            const BigInt too_big("584580020955360946586837552585233629614212007514394561597561641914945762794672");
1✔
78
            kas->derive_key(16, BigInt::encode(too_big));
3✔
79
         });
×
80

81
         result.test_throws("agreement input too small", "DH agreement - invalid key provided", [&kas]() {
2✔
82
            const BigInt too_small("1");
1✔
83
            kas->derive_key(16, BigInt::encode(too_small));
3✔
84
         });
×
85

86
         return {result};
3✔
87
      }
6✔
88
};
89

90
class DH_Invalid_Key_Tests final : public Text_Based_Test {
×
91
   public:
92
      DH_Invalid_Key_Tests() : Text_Based_Test("pubkey/dh_invalid.vec", "P,Q,G,InvalidKey") {}
2✔
93

94
      bool clear_between_callbacks() const override { return false; }
7✔
95

96
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
7✔
97
         Test::Result result("DH invalid keys");
7✔
98

99
         const Botan::BigInt p = vars.get_req_bn("P");
7✔
100
         const Botan::BigInt q = vars.get_req_bn("Q");
7✔
101
         const Botan::BigInt g = vars.get_req_bn("G");
7✔
102
         const Botan::BigInt pubkey = vars.get_req_bn("InvalidKey");
7✔
103

104
         Botan::DL_Group group(p, q, g);
7✔
105

106
         auto key = std::make_unique<Botan::DH_PublicKey>(group, pubkey);
7✔
107
         result.test_eq("public key fails check", key->check_key(this->rng(), false), false);
7✔
108
         return result;
7✔
109
      }
42✔
110
};
111

112
class Diffie_Hellman_Keygen_Tests final : public PK_Key_Generation_Test {
×
113
   public:
114
      std::vector<std::string> keygen_params() const override { return {"modp/ietf/1024"}; }
1✔
115

116
      std::string algo_name() const override { return "DH"; }
1✔
117

118
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
1✔
119
                                                             std::string_view /*provider*/,
120
                                                             std::span<const uint8_t> raw_key_bits) const override {
121
         return std::make_unique<Botan::DH_PublicKey>(Botan::DL_Group::from_name(keygen_params),
3✔
122
                                                      Botan::BigInt(raw_key_bits));
3✔
123
      }
124
};
125

126
BOTAN_REGISTER_TEST("pubkey", "dh_kat", Diffie_Hellman_KAT_Tests);
127
BOTAN_REGISTER_TEST("pubkey", "dh_invalid", DH_Invalid_Key_Tests);
128

129
BOTAN_REGISTER_TEST("pubkey", "dh_keygen", Diffie_Hellman_Keygen_Tests);
130

131
#endif
132

133
}  // namespace
134

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