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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

91.94
/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

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

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

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

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

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

84
         return {result};
3✔
85
      }
5✔
86
};
87

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

92
      bool clear_between_callbacks() const override { return false; }
7✔
93

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

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

102
         Botan::DL_Group group(p, q, g);
7✔
103

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

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

114
      std::string algo_name() const override { return "DH"; }
3✔
115
};
116

117
BOTAN_REGISTER_TEST("pubkey", "dh_kat", Diffie_Hellman_KAT_Tests);
118
BOTAN_REGISTER_TEST("pubkey", "dh_invalid", DH_Invalid_Key_Tests);
119

120
BOTAN_REGISTER_TEST("pubkey", "dh_keygen", Diffie_Hellman_Keygen_Tests);
121

122
#endif
123

124
}
125

126
}
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