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

randombit / botan / 23981747673

04 Apr 2026 03:21PM UTC coverage: 89.456% (+0.002%) from 89.454%
23981747673

Pull #5478

github

web-flow
Merge d3e86fe9e into 417709dd7
Pull Request #5478: Add PKCS#12 KDF

105700 of 118158 relevant lines covered (89.46%)

11669478.03 hits per line

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

90.54
/src/tests/test_pbkdf.cpp
1
/*
2
* (C) 2014,2015,2019 Jack Lloyd
3
* (C) 2018 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_PBKDF)
11
   #include <botan/exceptn.h>
12
   #include <botan/pbkdf.h>
13
   #include <botan/pwdhash.h>
14
#endif
15

16
#if defined(BOTAN_HAS_RFC4880)
17
   #include <botan/rfc4880.h>
18
#endif
19

20
namespace Botan_Tests {
21

22
namespace {
23

24
#if defined(BOTAN_HAS_PBKDF)
25
class PBKDF_KAT_Tests final : public Text_Based_Test {
×
26
   public:
27
      PBKDF_KAT_Tests() : Text_Based_Test("pbkdf", "Iterations,Salt,Passphrase,Output") {}
2✔
28

29
      Test::Result run_one_test(const std::string& pbkdf_name, const VarMap& vars) override {
38✔
30
         const size_t iterations = vars.get_req_sz("Iterations");
38✔
31
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
38✔
32
         const std::string passphrase = vars.get_req_str("Passphrase");
38✔
33
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
38✔
34
         const size_t outlen = expected.size();
38✔
35

36
         Test::Result result(pbkdf_name);
38✔
37
         auto pbkdf = Botan::PBKDF::create(pbkdf_name);
38✔
38

39
         if(!pbkdf) {
38✔
40
            result.note_missing(pbkdf_name);
10✔
41
            return result;
42
         }
43

44
         result.test_str_eq("Expected name", pbkdf->name(), pbkdf_name);
28✔
45

46
         const Botan::secure_vector<uint8_t> derived =
28✔
47
            pbkdf->derive_key(outlen, passphrase, salt.data(), salt.size(), iterations).bits_of();
28✔
48
         result.test_bin_eq("derived key", derived, expected);
28✔
49

50
         auto pwdhash_fam = Botan::PasswordHashFamily::create(pbkdf_name);
28✔
51

52
         if(!pwdhash_fam) {
28✔
53
            result.note_missing("No PasswordHashFamily for " + pbkdf_name);
×
54
            return result;
×
55
         }
56

57
         auto pwdhash = pwdhash_fam->from_params(iterations);
28✔
58

59
         std::vector<uint8_t> pwdhash_derived(outlen);
28✔
60
         pwdhash->hash(pwdhash_derived, passphrase, salt);
28✔
61

62
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
28✔
63

64
         return result;
28✔
65
      }
226✔
66
};
67

68
BOTAN_REGISTER_SMOKE_TEST("pbkdf", "pbkdf_kat", PBKDF_KAT_Tests);
69

70
class Pwdhash_Tests : public Test {
1✔
71
   public:
72
      std::vector<Test::Result> run() override {
1✔
73
         std::vector<Test::Result> results;
1✔
74

75
         const std::vector<std::string> all_pwdhash = {"Scrypt",
1✔
76
                                                       "PBKDF2(SHA-256)",
77
                                                       "OpenPGP-S2K(SHA-384)",
78
                                                       "Argon2d",
79
                                                       "Argon2i",
80
                                                       "Argon2id",
81
                                                       "Bcrypt-PBKDF",
82
                                                       "PKCS12-KDF(SHA-1,1)",
83
                                                       "PKCS12-KDF(SHA-256,2)"};
1✔
84

85
         const uint64_t run_time = 3;
1✔
86
         const uint64_t tune_time = 1;
1✔
87
         const size_t max_mem = 32;
1✔
88

89
         for(const std::string& pwdhash : all_pwdhash) {
10✔
90
            Test::Result result("Pwdhash " + pwdhash);
9✔
91
            auto pwdhash_fam = Botan::PasswordHashFamily::create(pwdhash);
9✔
92

93
            if(pwdhash_fam) {
9✔
94
               result.start_timer();
9✔
95

96
               std::unique_ptr<Botan::PasswordHash> tuned_pwhash;
9✔
97
               try {
9✔
98
                  tuned_pwhash = pwdhash_fam->tune_params(32, run_time, max_mem, tune_time);
9✔
99
               } catch(const Botan::Not_Implemented&) {
×
100
                  // tune_params requires os_utils for clock access; not available in minimized builds
101
                  result.test_note("tune_params not available in current build config");
×
102
               }
×
103

104
               if(tuned_pwhash != nullptr) {
9✔
105
                  std::vector<uint8_t> output1(32);
9✔
106
                  const std::vector<uint8_t> salt(8);
9✔
107
                  const std::string password = "test";
9✔
108
                  tuned_pwhash->hash(output1, password, salt);
9✔
109

110
                  std::unique_ptr<Botan::PasswordHash> pwhash;
9✔
111

112
                  if(pwdhash_fam->name() == "Scrypt" || pwdhash_fam->name().starts_with("Argon2")) {
9✔
113
                     pwhash = pwdhash_fam->from_params(
4✔
114
                        tuned_pwhash->memory_param(), tuned_pwhash->iterations(), tuned_pwhash->parallelism());
8✔
115
                  } else {
116
                     pwhash = pwdhash_fam->from_params(tuned_pwhash->iterations());
5✔
117
                  }
118

119
                  std::vector<uint8_t> output2(32);
9✔
120
                  pwhash->hash(output2, password, salt);
9✔
121

122
                  result.test_bin_eq("PasswordHash produced same output when run with same params", output1, output2);
9✔
123

124
                  auto default_pwhash = pwdhash_fam->default_params();
9✔
125
                  std::vector<uint8_t> output3(32);
9✔
126
                  default_pwhash->hash(output3, password, salt);
18✔
127
               }
45✔
128
               result.end_timer();
9✔
129
            } else {
×
130
               result.test_note("No such algo", pwdhash);
×
131
            }
132

133
            results.push_back(result);
9✔
134
         }
9✔
135

136
         return results;
1✔
137
      }
1✔
138
};
139

140
BOTAN_REGISTER_TEST("pbkdf", "pwdhash", Pwdhash_Tests);
141

142
#endif
143

144
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
145

146
class Bcrypt_PBKDF_KAT_Tests final : public Text_Based_Test {
×
147
   public:
148
      Bcrypt_PBKDF_KAT_Tests() : Text_Based_Test("bcrypt_pbkdf.vec", "Passphrase,Salt,Iterations,Output") {}
2✔
149

150
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
36✔
151
         const size_t rounds = vars.get_req_sz("Iterations");
36✔
152
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
36✔
153
         const std::string passphrase = vars.get_req_str("Passphrase");
36✔
154
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
36✔
155

156
         Test::Result result("bcrypt PBKDF");
36✔
157

158
         auto pwdhash_fam = Botan::PasswordHashFamily::create("Bcrypt-PBKDF");
36✔
159

160
         if(!pwdhash_fam) {
36✔
161
            result.test_failure("Bcrypt-PBKDF is missing PasswordHashFamily");
×
162
            return result;
163
         }
164

165
         auto pwdhash = pwdhash_fam->from_iterations(rounds);
36✔
166

167
         std::vector<uint8_t> derived(expected.size());
36✔
168
         pwdhash->hash(derived, passphrase, salt);
36✔
169

170
         result.test_bin_eq("derived key", derived, expected);
36✔
171

172
         return result;
36✔
173
      }
180✔
174
};
175

176
BOTAN_REGISTER_TEST("pbkdf", "bcrypt_pbkdf", Bcrypt_PBKDF_KAT_Tests);
177

178
#endif
179

180
#if defined(BOTAN_HAS_SCRYPT)
181

182
class Scrypt_KAT_Tests final : public Text_Based_Test {
×
183
   public:
184
      Scrypt_KAT_Tests() : Text_Based_Test("scrypt.vec", "Passphrase,Salt,N,R,P,Output") {}
1✔
185

186
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
15✔
187
         const size_t N = vars.get_req_sz("N");
15✔
188
         const size_t R = vars.get_req_sz("R");
15✔
189
         const size_t P = vars.get_req_sz("P");
15✔
190
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
15✔
191
         const std::string passphrase = vars.get_req_str("Passphrase");
15✔
192
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
15✔
193

194
         Test::Result result("scrypt");
15✔
195

196
         if(N >= 1048576 && Test::run_long_tests() == false) {
15✔
197
            return result;
198
         }
199

200
         auto pwdhash_fam = Botan::PasswordHashFamily::create("Scrypt");
15✔
201

202
         if(!pwdhash_fam) {
15✔
203
            result.test_failure("Scrypt is missing PasswordHashFamily");
×
204
            return result;
205
         }
206

207
         auto pwdhash = pwdhash_fam->from_params(N, R, P);
15✔
208

209
         std::vector<uint8_t> pwdhash_derived(expected.size());
15✔
210
         pwdhash->hash(pwdhash_derived, passphrase, salt);
15✔
211

212
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
15✔
213

214
         return result;
15✔
215
      }
75✔
216
};
217

218
BOTAN_REGISTER_TEST("pbkdf", "scrypt", Scrypt_KAT_Tests);
219

220
#endif
221

222
#if defined(BOTAN_HAS_ARGON2)
223

224
class Argon2_KAT_Tests final : public Text_Based_Test {
×
225
   public:
226
      Argon2_KAT_Tests() : Text_Based_Test("argon2.vec", "Passphrase,Salt,P,M,T,Output", "Secret,AD") {}
2✔
227

228
      Test::Result run_one_test(const std::string& mode, const VarMap& vars) override {
1,071✔
229
         const size_t P = vars.get_req_sz("P");
1,071✔
230
         const size_t M = vars.get_req_sz("M");
1,071✔
231
         const size_t T = vars.get_req_sz("T");
1,071✔
232
         const std::vector<uint8_t> key = vars.get_opt_bin("Secret");
1,071✔
233
         const std::vector<uint8_t> ad = vars.get_opt_bin("AD");
1,071✔
234
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
1,071✔
235
         const std::vector<uint8_t> passphrase = vars.get_req_bin("Passphrase");
1,071✔
236
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
1,071✔
237

238
         Test::Result result(mode);
1,071✔
239

240
         auto pwdhash_fam = Botan::PasswordHashFamily::create(mode);
1,071✔
241

242
         if(!pwdhash_fam) {
1,071✔
243
            result.test_failure("Argon2 is missing PasswordHashFamily");
×
244
            return result;
245
         }
246

247
         auto pwdhash = pwdhash_fam->from_params(M, T, P);
1,071✔
248

249
         const std::string passphrase_str(passphrase.begin(), passphrase.end());
2,142✔
250

251
         std::vector<uint8_t> pwdhash_derived(expected.size());
1,071✔
252
         pwdhash->hash(pwdhash_derived, passphrase_str, salt, ad, key);
1,071✔
253

254
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
1,071✔
255

256
         return result;
1,071✔
257
      }
6,654✔
258
};
259

260
BOTAN_REGISTER_SERIALIZED_TEST("pbkdf", "argon2", Argon2_KAT_Tests);
261

262
#endif
263

264
#if defined(BOTAN_HAS_PGP_S2K)
265

266
class PGP_S2K_Iter_Test final : public Test {
1✔
267
   public:
268
      std::vector<Test::Result> run() override {
1✔
269
         Test::Result result("PGP_S2K iteration encoding");
1✔
270

271
         // The maximum representable iteration count
272
         const size_t max_iter = 65011712;
1✔
273

274
         result.test_sz_eq("Encoding of large value accepted", Botan::RFC4880_encode_count(max_iter * 2), size_t(255));
1✔
275
         result.test_sz_eq("Encoding of small value accepted", Botan::RFC4880_encode_count(0), size_t(0));
1✔
276

277
         for(size_t c = 0; c != 256; ++c) {
257✔
278
            const size_t dec = Botan::RFC4880_decode_count(static_cast<uint8_t>(c));
256✔
279
            const size_t comp_dec = (16 + (c & 0x0F)) << ((c >> 4) + 6);
256✔
280
            result.test_sz_eq("Decoded value matches PGP formula", dec, comp_dec);
256✔
281

282
            const size_t enc = Botan::RFC4880_encode_count(comp_dec);
256✔
283
            result.test_sz_eq("Encoded value matches PGP formula", enc, c);
256✔
284
         }
285

286
         uint8_t last_enc = 0;
287

288
         for(size_t i = 0; i <= max_iter; i += 64) {
1,015,810✔
289
            const uint8_t enc = Botan::RFC4880_encode_count(i);
1,015,809✔
290
            result.test_sz_lte("Encoded value non-decreasing", last_enc, enc);
1,015,809✔
291

292
            /*
293
            The iteration count as encoded may not be exactly the
294
            value requested, but should never be less
295
            */
296
            const size_t dec = Botan::RFC4880_decode_count(enc);
1,015,809✔
297
            result.test_sz_gte("Decoded value is >= requested", dec, i);
1,015,809✔
298

299
            last_enc = enc;
1,015,809✔
300
         }
301

302
         return std::vector<Test::Result>{result};
3✔
303
      }
2✔
304
};
305

306
BOTAN_REGISTER_TEST("pbkdf", "pgp_s2k_iter", PGP_S2K_Iter_Test);
307

308
#endif
309

310
}  // namespace
311

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