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

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

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

97.09
/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/hex.h>
12
   #include <botan/internal/workfactor.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_DL_GROUP)
18

19
namespace {
20

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

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

29
         return results;
1✔
30
      }
×
31

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

40
         return result;
1✔
41
      }
×
42

43
      static Test::Result test_dl_encoding() {
1✔
44
         Test::Result result("DL_Group encoding");
1✔
45

46
         const auto orig = Botan::DL_Group::from_name("modp/ietf/1024");
1✔
47

48
         const std::string pem1 = orig.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
1✔
49
         const std::string pem2 = orig.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
1✔
50
         const std::string pem3 = orig.PEM_encode(Botan::DL_Group_Format::PKCS_3);
1✔
51

52
         const auto group1 = Botan::DL_Group::from_PEM(pem1);
1✔
53

54
         result.test_eq("Same p in X9.42 decoding", group1.get_p(), orig.get_p());
1✔
55
         result.test_eq("Same q in X9.42 decoding", group1.get_q(), orig.get_q());
1✔
56
         result.test_eq("Same g in X9.42 decoding", group1.get_g(), orig.get_g());
1✔
57

58
         const auto group2 = Botan::DL_Group::from_PEM(pem2);
1✔
59

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

64
         const auto group3 = Botan::DL_Group::from_PEM(pem3);
1✔
65

66
         result.test_eq("Same p in X9.57 decoding", group3.get_p(), orig.get_p());
1✔
67
         // no q in PKCS #3 format
68
         result.test_eq("Same g in X9.57 decoding", group3.get_g(), orig.get_g());
1✔
69

70
         return result;
1✔
71
      }
4✔
72
};
73

74
BOTAN_REGISTER_TEST("pubkey", "dl_group", DL_Group_Tests);
75

76
class DL_Generate_Group_Tests final : public Test {
1✔
77
   public:
78
      std::vector<Test::Result> run() override {
1✔
79
         Test::Result result("DL_Group generate");
1✔
80

81
         result.start_timer();
1✔
82

83
         auto& rng = this->rng();
1✔
84

85
         const Botan::DL_Group dh1050(rng, Botan::DL_Group::Prime_Subgroup, 1050, 175);
1✔
86
         result.test_eq("DH p size", dh1050.get_p().bits(), 1050);
1✔
87
         result.test_eq("DH q size", dh1050.get_q().bits(), 175);
1✔
88
         result.test_lte("DH g size", dh1050.get_g().bits(), 1050);
1✔
89
         result.test_eq("DH group verifies", dh1050.verify_group(rng, false), true);
1✔
90

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

97
         if(Test::run_long_tests()) {
1✔
98
            const Botan::DL_Group dh_strong(rng, Botan::DL_Group::Strong, 1025);
1✔
99
            result.test_eq("DH p size", dh_strong.get_p().bits(), 1025);
1✔
100
            result.test_eq("DH q size", dh_strong.get_q().bits(), 1024);
1✔
101
            result.test_eq("DH group verifies", dh_strong.verify_group(rng, false), true);
2✔
102
         }
1✔
103

104
   #if defined(BOTAN_HAS_SHA1)
105
         const Botan::DL_Group dsa1024(rng, Botan::DL_Group::DSA_Kosherizer, 1024);
1✔
106
         result.test_eq("DSA p size", dsa1024.get_p().bits(), 1024);
1✔
107
         result.test_eq("DSA q size", dsa1024.get_q().bits(), 160);
1✔
108
         result.test_lte("DSA g size", dsa1024.get_g().bits(), 1024);
1✔
109
         result.test_eq("DSA group verifies", dsa1024.verify_group(rng, false), true);
1✔
110

111
         const std::vector<uint8_t> invalid_seed(20);
1✔
112
         result.test_throws("DSA seed does not generate group",
2✔
113
                            "DL_Group: The seed given does not generate a DSA group",
114
                            [&rng, &invalid_seed]() { const Botan::DL_Group dsa(rng, invalid_seed, 1024, 160); });
2✔
115

116
         const std::vector<uint8_t> short_seed(16);
1✔
117
         result.test_throws(
2✔
118
            "DSA seed is too short",
119
            "Generating a DSA parameter set with a 160 bit long q requires a seed at least as many bits long",
120
            [&rng, &short_seed]() { const Botan::DL_Group dsa(rng, short_seed, 1024, 160); });
2✔
121

122
         const std::vector<uint8_t> working_seed = Botan::hex_decode("0000000000000000000000000000000000000021");
1✔
123
         const Botan::DL_Group dsa(rng, working_seed, 1024, 160);
1✔
124
         result.test_eq("DSA group from working seed verifies", dsa.verify_group(rng, false), true);
1✔
125

126
         // From FIPS 186-3 test data
127
         const std::vector<uint8_t> seed = Botan::hex_decode("1F5DA0AF598EEADEE6E6665BF880E63D8B609BA2");
1✔
128

129
         result.test_throws("invalid params", [&]() { const Botan::DL_Group invalid(rng, seed, 1024, 224); });
3✔
130
         result.test_throws("invalid params", [&]() { const Botan::DL_Group invalid(rng, seed, 3072, 224); });
3✔
131
         result.test_throws("invalid params", [&]() { const Botan::DL_Group invalid(rng, seed, 2048, 256); });
3✔
132

133
         const Botan::DL_Group dsa_from_seed(rng, seed, 1024, 160);
1✔
134

135
         result.test_eq(
1✔
136
            "DSA q from seed", dsa_from_seed.get_q(), Botan::BigInt("0xAB1A788BCE3C557A965A5BFA6908FAA665FDEB7D"));
2✔
137

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

141
         result.test_eq("DSA group from seed verifies", dsa_from_seed.verify_group(rng, false), true);
1✔
142
   #endif
143

144
         result.end_timer();
1✔
145

146
         return {result};
3✔
147
      }
10✔
148
};
149

150
BOTAN_REGISTER_TEST("pubkey", "dl_group_gen", DL_Generate_Group_Tests);
151

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

159
            "modp/srp/1024",   "modp/srp/1536",   "modp/srp/2048",   "modp/srp/3072",   "modp/srp/4096",
160
            "modp/srp/6144",   "modp/srp/8192",
161

162
            "dsa/jce/1024",    "dsa/botan/2048",  "dsa/botan/3072",
163

164
            "ffdhe/ietf/2048", "ffdhe/ietf/3072", "ffdhe/ietf/4096", "ffdhe/ietf/6144", "ffdhe/ietf/8192",
165
         };
1✔
166

167
         Test::Result result("DL_Group named");
1✔
168
         result.start_timer();
1✔
169

170
         for(const std::string& name : dl_named) {
23✔
171
            // Confirm we can load every group we expect
172
            auto group = Botan::DL_Group::from_name(name);
22✔
173

174
            result.test_ne("DL_Group p is set", group.get_p(), 0);
44✔
175
            result.test_ne("DL_Group g is set", group.get_g(), 0);
44✔
176

177
            const size_t strength = group.estimated_strength();
22✔
178

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

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

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

190
            if(group.p_bits() <= 1536 || Test::run_long_tests()) {
22✔
191
               result.test_eq(name + " verifies", group.verify_group(this->rng()), true);
44✔
192
            }
193
         }
22✔
194
         result.end_timer();
1✔
195

196
         return {result};
3✔
197
      }
2✔
198
};
199

200
BOTAN_REGISTER_TEST("pubkey", "dl_group_named", DL_Named_Group_Tests);
201

202
}  // namespace
203

204
#endif
205

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