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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

93.85
/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
         Botan::DL_Group group;
40✔
36
         if(q == 0) {
40✔
37
            group = Botan::DL_Group(p, g);
24✔
38
         } else {
39
            group = Botan::DL_Group(p, q, g);
56✔
40
         }
41

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

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

51
         Botan::DL_Group group;
40✔
52
         if(q == 0) {
40✔
53
            group = Botan::DL_Group(p, g);
24✔
54
         } else {
55
            group = Botan::DL_Group(p, q, g);
56✔
56
         }
57

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

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

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

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

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

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

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

85
         result.end_timer();
1✔
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(keygen_params), Botan::BigInt(raw_key_bits));
4✔
122
      }
123
};
124

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

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

130
#endif
131

132
}  // namespace
133

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

© 2025 Coveralls, Inc