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

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

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

93.64
/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.start_timer();
1✔
35

36
         result.test_throws("Uninitialized", "DL_Group uninitialized", []() {
2✔
37
            Botan::DL_Group dl;
1✔
38
            dl.get_p();
1✔
39
         });
×
40

41
   #if !defined(BOTAN_HAS_SANITIZER_UNDEFINED)
42
         result.test_throws("Bad generator param", "DL_Group unknown PrimeType", []() {
2✔
43
            // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
44
            auto invalid_type = static_cast<Botan::DL_Group::PrimeType>(9);
1✔
45
            Botan::Null_RNG null_rng;
1✔
46
            Botan::DL_Group dl(null_rng, invalid_type, 1024);
1✔
47
         });
×
48
   #endif
49

50
         result.end_timer();
1✔
51
         return result;
1✔
52
      }
×
53

54
      static Test::Result test_dl_encoding() {
1✔
55
         Test::Result result("DL_Group encoding");
1✔
56
         result.start_timer();
1✔
57

58
         const Botan::DL_Group orig("modp/ietf/1024");
1✔
59

60
         const std::string pem1 = orig.PEM_encode(Botan::DL_Group_Format::ANSI_X9_42);
1✔
61
         const std::string pem2 = orig.PEM_encode(Botan::DL_Group_Format::ANSI_X9_57);
1✔
62
         const std::string pem3 = orig.PEM_encode(Botan::DL_Group_Format::PKCS_3);
1✔
63

64
         Botan::DL_Group group1(pem1);
1✔
65

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

70
         Botan::DL_Group group2(pem2);
1✔
71

72
         result.test_eq("Same p in X9.57 decoding", group2.get_p(), orig.get_p());
1✔
73
         result.test_eq("Same q in X9.57 decoding", group2.get_q(), orig.get_q());
1✔
74
         result.test_eq("Same g in X9.57 decoding", group2.get_g(), orig.get_g());
1✔
75

76
         Botan::DL_Group group3(pem3);
1✔
77

78
         result.test_eq("Same p in X9.57 decoding", group3.get_p(), orig.get_p());
1✔
79
         // no q in PKCS #3 format
80
         result.test_eq("Same g in X9.57 decoding", group3.get_g(), orig.get_g());
1✔
81

82
         result.end_timer();
1✔
83
         return result;
1✔
84
      }
4✔
85
};
86

87
BOTAN_REGISTER_TEST("pubkey", "dl_group", DL_Group_Tests);
88

89
class DL_Generate_Group_Tests final : public Test {
×
90
   public:
91
      std::vector<Test::Result> run() override {
1✔
92
         Test::Result result("DL_Group generate");
1✔
93

94
         result.start_timer();
1✔
95

96
         auto& rng = this->rng();
1✔
97

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

104
         Botan::DL_Group dh_implicit_q(rng, Botan::DL_Group::Prime_Subgroup, 1040);
1✔
105
         result.test_eq("DH p size", dh_implicit_q.get_p().bits(), 1040);
1✔
106
         result.test_eq("DH q size", dh_implicit_q.get_q().bits(), Botan::dl_exponent_size(1040));
1✔
107
         result.test_lte("DH g size", dh_implicit_q.get_g().bits(), 1040);
1✔
108
         result.test_eq("DH group verifies", dh_implicit_q.verify_group(rng, false), true);
1✔
109

110
         if(Test::run_long_tests()) {
1✔
111
            Botan::DL_Group dh_strong(rng, Botan::DL_Group::Strong, 1025);
1✔
112
            result.test_eq("DH p size", dh_strong.get_p().bits(), 1025);
1✔
113
            result.test_eq("DH q size", dh_strong.get_q().bits(), 1024);
1✔
114
            result.test_eq("DH group verifies", dh_strong.verify_group(rng, false), true);
2✔
115
         }
1✔
116

117
   #if defined(BOTAN_HAS_SHA1)
118
         Botan::DL_Group dsa1024(rng, Botan::DL_Group::DSA_Kosherizer, 1024);
1✔
119
         result.test_eq("DSA p size", dsa1024.get_p().bits(), 1024);
1✔
120
         result.test_eq("DSA q size", dsa1024.get_q().bits(), 160);
1✔
121
         result.test_lte("DSA g size", dsa1024.get_g().bits(), 1024);
1✔
122
         result.test_eq("DSA group verifies", dsa1024.verify_group(rng, false), true);
1✔
123

124
         const std::vector<uint8_t> short_seed(16);
1✔
125
         const std::vector<uint8_t> invalid_seed(20);
1✔
126
         const std::vector<uint8_t> working_seed = Botan::hex_decode("0000000000000000000000000000000000000021");
1✔
127

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

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

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

140
         result.test_throws("invalid params", [&]() { Botan::DL_Group invalid(rng, seed, 1024, 224); });
3✔
141
         result.test_throws("invalid params", [&]() { Botan::DL_Group invalid(rng, seed, 3072, 224); });
3✔
142
         result.test_throws("invalid params", [&]() { Botan::DL_Group invalid(rng, seed, 2048, 256); });
3✔
143

144
         Botan::DL_Group dsa_from_seed(rng, seed, 1024, 160);
1✔
145

146
         result.test_eq(
2✔
147
            "DSA q from seed", dsa_from_seed.get_q(), Botan::BigInt("0xAB1A788BCE3C557A965A5BFA6908FAA665FDEB7D"));
1✔
148

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

152
         result.test_eq("DSA group from seed verifies", dsa_from_seed.verify_group(rng, false), true);
1✔
153
   #endif
154

155
         result.end_timer();
1✔
156

157
         return {result};
3✔
158
      }
9✔
159
};
160

161
BOTAN_REGISTER_TEST("pubkey", "dl_group_gen", DL_Generate_Group_Tests);
162

163
class DL_Named_Group_Tests final : public Test {
×
164
   public:
165
      std::vector<Test::Result> run() override {
1✔
166
         const std::vector<std::string> dl_named = {
1✔
167
            "modp/ietf/1024",  "modp/ietf/1536",  "modp/ietf/2048",  "modp/ietf/3072",  "modp/ietf/4096",
168
            "modp/ietf/6144",  "modp/ietf/8192",
169

170
            "modp/srp/1024",   "modp/srp/1536",   "modp/srp/2048",   "modp/srp/3072",   "modp/srp/4096",
171
            "modp/srp/6144",   "modp/srp/8192",
172

173
            "dsa/jce/1024",    "dsa/botan/2048",  "dsa/botan/3072",
174

175
            "ffdhe/ietf/2048", "ffdhe/ietf/3072", "ffdhe/ietf/4096", "ffdhe/ietf/6144", "ffdhe/ietf/8192",
176
         };
1✔
177

178
         Test::Result result("DL_Group named");
1✔
179
         result.start_timer();
1✔
180

181
         for(const std::string& name : dl_named) {
23✔
182
            // Confirm we can load every group we expect
183
            Botan::DL_Group group(name);
22✔
184

185
            result.test_ne("DL_Group p is set", group.get_p(), 0);
44✔
186
            result.test_ne("DL_Group g is set", group.get_g(), 0);
44✔
187

188
            const size_t strength = group.estimated_strength();
22✔
189

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

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

195
            if(name.find("modp/srp/") == std::string::npos) {
22✔
196
               result.test_ne("DL_Group q is set", group.get_q(), 0);
30✔
197
            } else {
198
               result.test_eq("DL_Group q is not set for SRP groups", group.get_q(), 0);
14✔
199
            }
200

201
            if(group.p_bits() <= 1536 || Test::run_long_tests()) {
22✔
202
               result.test_eq(name + " verifies", group.verify_group(this->rng()), true);
44✔
203
            }
204
         }
22✔
205
         result.end_timer();
1✔
206

207
         return {result};
3✔
208
      }
2✔
209
};
210

211
BOTAN_REGISTER_TEST("pubkey", "dl_group_named", DL_Named_Group_Tests);
212

213
}  // namespace
214

215
#endif
216

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