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

randombit / botan / 13215274653

08 Feb 2025 11:38AM UTC coverage: 91.655% (-0.009%) from 91.664%
13215274653

Pull #4650

github

web-flow
Merge 107f31833 into bc555cd3c
Pull Request #4650: Reorganize code and reduce header dependencies

94836 of 103471 relevant lines covered (91.65%)

11230958.94 hits per line

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

93.4
/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", []() {
2✔
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", []() {
2✔
41
            // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
42
            auto invalid_type = static_cast<Botan::DL_Group::PrimeType>(9);
1✔
43
            Botan::Null_RNG null_rng;
1✔
44
            Botan::DL_Group dl(null_rng, invalid_type, 1024);
1✔
45
         });
×
46
   #endif
47

48
         return result;
1✔
49
      }
×
50

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

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

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

60
         const auto group1 = Botan::DL_Group::from_PEM(pem1);
1✔
61

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

66
         const auto group2 = Botan::DL_Group::from_PEM(pem2);
1✔
67

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

72
         const auto group3 = Botan::DL_Group::from_PEM(pem3);
1✔
73

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

78
         return result;
1✔
79
      }
4✔
80
};
81

82
BOTAN_REGISTER_TEST("pubkey", "dl_group", DL_Group_Tests);
83

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

89
         result.start_timer();
1✔
90

91
         auto& rng = this->rng();
1✔
92

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

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

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

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

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

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

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

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

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

139
         Botan::DL_Group dsa_from_seed(rng, seed, 1024, 160);
1✔
140

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

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

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

150
         result.end_timer();
1✔
151

152
         return {result};
3✔
153
      }
9✔
154
};
155

156
BOTAN_REGISTER_TEST("pubkey", "dl_group_gen", DL_Generate_Group_Tests);
157

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

165
            "modp/srp/1024",   "modp/srp/1536",   "modp/srp/2048",   "modp/srp/3072",   "modp/srp/4096",
166
            "modp/srp/6144",   "modp/srp/8192",
167

168
            "dsa/jce/1024",    "dsa/botan/2048",  "dsa/botan/3072",
169

170
            "ffdhe/ietf/2048", "ffdhe/ietf/3072", "ffdhe/ietf/4096", "ffdhe/ietf/6144", "ffdhe/ietf/8192",
171
         };
1✔
172

173
         Test::Result result("DL_Group named");
1✔
174
         result.start_timer();
1✔
175

176
         for(const std::string& name : dl_named) {
23✔
177
            // Confirm we can load every group we expect
178
            auto group = Botan::DL_Group::from_name(name);
22✔
179

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

183
            const size_t strength = group.estimated_strength();
22✔
184

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

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

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

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

202
         return {result};
3✔
203
      }
2✔
204
};
205

206
BOTAN_REGISTER_TEST("pubkey", "dl_group_named", DL_Named_Group_Tests);
207

208
}  // namespace
209

210
#endif
211

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