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

randombit / botan / 12993346889

27 Jan 2025 04:15PM UTC coverage: 91.248% (+0.002%) from 91.246%
12993346889

push

github

butteronarchbtw
add (s)afi encoding & decoding

94261 of 103302 relevant lines covered (91.25%)

11708235.22 hits per line

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

93.75
/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, this->rng(), "Raw");
1✔
73

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

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

84
         return {result};
3✔
85
      }
6✔
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") {}
2✔
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(this->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"}; }
1✔
113

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

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

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

127
BOTAN_REGISTER_TEST("pubkey", "dh_keygen", Diffie_Hellman_Keygen_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