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

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

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

93.33
/src/tests/test_dl_group.cpp
1
/*
2
* (C) 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_DL_GROUP)
10
   #include <botan/dl_group.h>
11
   #include <botan/internal/workfactor.h>
12
#endif
13

14
namespace Botan_Tests {
15

16
#if defined(BOTAN_HAS_DL_GROUP)
17

18
namespace {
19

20
class DL_Group_Tests final : public Test {
×
21
   public:
22
      std::vector<Test::Result> run() override {
1✔
23
         std::vector<Test::Result> results;
1✔
24

25
         results.push_back(test_dl_encoding());
2✔
26
         results.push_back(test_dl_errors());
2✔
27

28
         return results;
1✔
29
      }
×
30

31
   private:
32
      static Test::Result test_dl_errors() {
1✔
33
         Test::Result result("DL_Group errors");
1✔
34
         result.test_throws("Uninitialized", "DL_Group uninitialized", []() {
3✔
35
            Botan::DL_Group dl;
1✔
36
            dl.get_p();
1✔
37
         });
×
38

39
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
40
         result.test_throws("Bad generator param", "DL_Group unknown PrimeType", []() {
3✔
41
            auto invalid_type = static_cast<Botan::DL_Group::PrimeType>(9);
1✔
42
            Botan::DL_Group dl(Test::rng(), invalid_type, 1024);
1✔
43
         });
×
44
   #endif
45

46
         return result;
1✔
47
      }
×
48

49
      static Test::Result test_dl_encoding() {
1✔
50
         Test::Result result("DL_Group encoding");
1✔
51

52
         const Botan::DL_Group orig("modp/ietf/1024");
1✔
53

54
         const std::string pem1 = orig.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
1✔
55
         const std::string pem2 = orig.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
1✔
56
         const std::string pem3 = orig.PEM_encode(Botan::DL_Group_Format::PKCS_3);
1✔
57

58
         Botan::DL_Group group1(pem1);
1✔
59

60
         result.test_eq("Same p in X9.42 decoding", group1.get_p(), orig.get_p());
1✔
61
         result.test_eq("Same q in X9.42 decoding", group1.get_q(), orig.get_q());
1✔
62
         result.test_eq("Same g in X9.42 decoding", group1.get_g(), orig.get_g());
1✔
63

64
         Botan::DL_Group group2(pem2);
1✔
65

66
         result.test_eq("Same p in X9.57 decoding", group2.get_p(), orig.get_p());
1✔
67
         result.test_eq("Same q in X9.57 decoding", group2.get_q(), orig.get_q());
1✔
68
         result.test_eq("Same g in X9.57 decoding", group2.get_g(), orig.get_g());
1✔
69

70
         Botan::DL_Group group3(pem3);
1✔
71

72
         result.test_eq("Same p in X9.57 decoding", group3.get_p(), orig.get_p());
1✔
73
         // no q in PKCS #3 format
74
         result.test_eq("Same g in X9.57 decoding", group3.get_g(), orig.get_g());
1✔
75

76
         return result;
1✔
77
      }
7✔
78
};
79

80
BOTAN_REGISTER_TEST("pubkey", "dl_group", DL_Group_Tests);
81

82
class DL_Generate_Group_Tests final : public Test {
×
83
   public:
84
      std::vector<Test::Result> run() override {
1✔
85
         Test::Result result("DL_Group generate");
1✔
86

87
         result.start_timer();
1✔
88

89
         auto& rng = Test::rng();
1✔
90

91
         Botan::DL_Group dh1050(rng, Botan::DL_Group::Prime_Subgroup, 1050, 175);
1✔
92
         result.test_eq("DH p size", dh1050.get_p().bits(), 1050);
1✔
93
         result.test_eq("DH q size", dh1050.get_q().bits(), 175);
1✔
94
         result.test_lte("DH g size", dh1050.get_g().bits(), 1050);
1✔
95
         result.test_eq("DH group verifies", dh1050.verify_group(rng, false), true);
1✔
96

97
         Botan::DL_Group dh_implicit_q(rng, Botan::DL_Group::Prime_Subgroup, 1040);
1✔
98
         result.test_eq("DH p size", dh_implicit_q.get_p().bits(), 1040);
1✔
99
         result.test_eq("DH q size", dh_implicit_q.get_q().bits(), Botan::dl_exponent_size(1040));
1✔
100
         result.test_lte("DH g size", dh_implicit_q.get_g().bits(), 1040);
1✔
101
         result.test_eq("DH group verifies", dh_implicit_q.verify_group(rng, false), true);
1✔
102

103
         if(Test::run_long_tests()) {
1✔
104
            Botan::DL_Group dh_strong(rng, Botan::DL_Group::Strong, 1025);
1✔
105
            result.test_eq("DH p size", dh_strong.get_p().bits(), 1025);
1✔
106
            result.test_eq("DH q size", dh_strong.get_q().bits(), 1024);
1✔
107
            result.test_eq("DH group verifies", dh_strong.verify_group(rng, false), true);
2✔
108
         }
1✔
109

110
   #if defined(BOTAN_HAS_SHA1)
111
         Botan::DL_Group dsa1024(rng, Botan::DL_Group::DSA_Kosherizer, 1024);
1✔
112
         result.test_eq("DSA p size", dsa1024.get_p().bits(), 1024);
1✔
113
         result.test_eq("DSA q size", dsa1024.get_q().bits(), 160);
1✔
114
         result.test_lte("DSA g size", dsa1024.get_g().bits(), 1024);
1✔
115
         result.test_eq("DSA group verifies", dsa1024.verify_group(rng, false), true);
1✔
116

117
         const std::vector<uint8_t> short_seed(16);
1✔
118
         const std::vector<uint8_t> invalid_seed(20);
1✔
119
         const std::vector<uint8_t> working_seed = Botan::hex_decode("0000000000000000000000000000000000000021");
1✔
120

121
         result.test_throws("DSA seed does not generate group",
3✔
122
                            "DL_Group: The seed given does not generate a DSA group",
123
                            [&rng, &invalid_seed]() { Botan::DL_Group dsa(rng, invalid_seed, 1024, 160); });
1✔
124

125
         result.test_throws(
3✔
126
            "DSA seed is too short",
127
            "Generating a DSA parameter set with a 160 bit long q requires a seed at least as many bits long",
128
            [&rng, &short_seed]() { Botan::DL_Group dsa(rng, short_seed, 1024, 160); });
1✔
129

130
         // From FIPS 186-3 test data
131
         const std::vector<uint8_t> seed = Botan::hex_decode("1F5DA0AF598EEADEE6E6665BF880E63D8B609BA2");
2✔
132

133
         result.test_throws("invalid params", [&]() { Botan::DL_Group invalid(rng, seed, 1024, 224); });
3✔
134
         result.test_throws("invalid params", [&]() { Botan::DL_Group invalid(rng, seed, 3072, 224); });
3✔
135
         result.test_throws("invalid params", [&]() { Botan::DL_Group invalid(rng, seed, 2048, 256); });
3✔
136

137
         Botan::DL_Group dsa_from_seed(rng, seed, 1024, 160);
1✔
138

139
         result.test_eq(
2✔
140
            "DSA q from seed", dsa_from_seed.get_q(), Botan::BigInt("0xAB1A788BCE3C557A965A5BFA6908FAA665FDEB7D"));
1✔
141

142
         // Modulo just to avoid embedding entire 1024-bit P in src file
143
         result.test_eq("DSA p from seed", static_cast<size_t>(dsa_from_seed.get_p() % 4294967291), size_t(2513712339));
1✔
144

145
         result.test_eq("DSA group from seed verifies", dsa_from_seed.verify_group(rng, false), true);
1✔
146
   #endif
147

148
         result.end_timer();
1✔
149

150
         return {result};
3✔
151
      }
8✔
152
};
153

154
BOTAN_REGISTER_TEST("pubkey", "dl_group_gen", DL_Generate_Group_Tests);
155

156
class DL_Named_Group_Tests final : public Test {
×
157
   public:
158
      std::vector<Test::Result> run() override {
1✔
159
         const std::vector<std::string> dl_named = {
1✔
160
            "modp/ietf/1024",  "modp/ietf/1536",  "modp/ietf/2048",  "modp/ietf/3072",  "modp/ietf/4096",
161
            "modp/ietf/6144",  "modp/ietf/8192",
162

163
            "modp/srp/1024",   "modp/srp/1536",   "modp/srp/2048",   "modp/srp/3072",   "modp/srp/4096",
164
            "modp/srp/6144",   "modp/srp/8192",
165

166
            "dsa/jce/1024",    "dsa/botan/2048",  "dsa/botan/3072",
167

168
            "ffdhe/ietf/2048", "ffdhe/ietf/3072", "ffdhe/ietf/4096", "ffdhe/ietf/6144", "ffdhe/ietf/8192",
169
         };
23✔
170

171
         Test::Result result("DL_Group named");
1✔
172
         result.start_timer();
1✔
173

174
         for(const std::string& name : dl_named) {
23✔
175
            // Confirm we can load every group we expect
176
            Botan::DL_Group group(name);
22✔
177

178
            result.test_ne("DL_Group p is set", group.get_p(), 0);
44✔
179
            result.test_ne("DL_Group g is set", group.get_g(), 0);
44✔
180

181
            const size_t strength = group.estimated_strength();
22✔
182

183
            // 8192 bit ~~ 2**202 strength
184
            result.confirm("Plausible strength", strength >= 80 && strength < 210);
44✔
185

186
            result.confirm("Expected source", group.source() == Botan::DL_Group_Source::Builtin);
44✔
187

188
            if(name.find("modp/srp/") == std::string::npos) {
22✔
189
               result.test_ne("DL_Group q is set", group.get_q(), 0);
30✔
190
            } else {
191
               result.test_eq("DL_Group q is not set for SRP groups", group.get_q(), 0);
14✔
192
            }
193

194
            if(group.p_bits() <= 1536 || Test::run_long_tests()) {
22✔
195
               result.test_eq(name + " verifies", group.verify_group(Test::rng()), true);
44✔
196
            }
197
         }
22✔
198
         result.end_timer();
1✔
199

200
         return {result};
3✔
201
      }
1✔
202
};
203

204
BOTAN_REGISTER_TEST("pubkey", "dl_group_named", DL_Named_Group_Tests);
205

206
}  // namespace
207

208
#endif
209

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