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

randombit / botan / 23429162741

23 Mar 2026 08:57AM UTC coverage: 89.429% (+0.001%) from 89.428%
23429162741

Pull #5478

github

web-flow
Merge ad264b826 into c868150bc
Pull Request #5478: Add PKCS#12 KDF

104928 of 117331 relevant lines covered (89.43%)

11861377.04 hits per line

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

92.68
/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/pbkdf.h>
12
#endif
13

14
#if defined(BOTAN_HAS_PKCS12_KDF)
15
   #include <botan/pkcs12_kdf.h>
16
#endif
17

18
#if defined(BOTAN_HAS_PBKDF) || defined(BOTAN_HAS_PKCS12_KDF)
19
   #include <botan/pwdhash.h>
20
#endif
21

22
#if defined(BOTAN_HAS_RFC4880)
23
   #include <botan/rfc4880.h>
24
#endif
25

26
namespace Botan_Tests {
27

28
namespace {
29

30
#if defined(BOTAN_HAS_PBKDF)
31
class PBKDF_KAT_Tests final : public Text_Based_Test {
×
32
   public:
33
      PBKDF_KAT_Tests() : Text_Based_Test("pbkdf", "Iterations,Salt,Passphrase,Output") {}
2✔
34

35
      Test::Result run_one_test(const std::string& pbkdf_name, const VarMap& vars) override {
38✔
36
         const size_t iterations = vars.get_req_sz("Iterations");
38✔
37
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
38✔
38
         const std::string passphrase = vars.get_req_str("Passphrase");
38✔
39
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
38✔
40
         const size_t outlen = expected.size();
38✔
41

42
         Test::Result result(pbkdf_name);
38✔
43
         auto pbkdf = Botan::PBKDF::create(pbkdf_name);
38✔
44

45
         if(!pbkdf) {
38✔
46
            result.note_missing(pbkdf_name);
10✔
47
            return result;
48
         }
49

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

52
         const Botan::secure_vector<uint8_t> derived =
28✔
53
            pbkdf->derive_key(outlen, passphrase, salt.data(), salt.size(), iterations).bits_of();
28✔
54
         result.test_bin_eq("derived key", derived, expected);
28✔
55

56
         auto pwdhash_fam = Botan::PasswordHashFamily::create(pbkdf_name);
28✔
57

58
         if(!pwdhash_fam) {
28✔
59
            result.note_missing("No PasswordHashFamily for " + pbkdf_name);
×
60
            return result;
×
61
         }
62

63
         auto pwdhash = pwdhash_fam->from_params(iterations);
28✔
64

65
         std::vector<uint8_t> pwdhash_derived(outlen);
28✔
66
         pwdhash->hash(pwdhash_derived, passphrase, salt);
28✔
67

68
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
28✔
69

70
         return result;
28✔
71
      }
226✔
72
};
73

74
BOTAN_REGISTER_SMOKE_TEST("pbkdf", "pbkdf_kat", PBKDF_KAT_Tests);
75

76
class Pwdhash_Tests : public Test {
1✔
77
   public:
78
      std::vector<Test::Result> run() override {
1✔
79
         std::vector<Test::Result> results;
1✔
80

81
         const std::vector<std::string> all_pwdhash = {"Scrypt",
1✔
82
                                                       "PBKDF2(SHA-256)",
83
                                                       "OpenPGP-S2K(SHA-384)",
84
                                                       "Argon2d",
85
                                                       "Argon2i",
86
                                                       "Argon2id",
87
                                                       "Bcrypt-PBKDF",
88
                                                       "PKCS12-KDF(SHA-1,1)",
89
                                                       "PKCS12-KDF(SHA-256,2)"};
1✔
90

91
         const uint64_t run_time = 3;
1✔
92
         const uint64_t tune_time = 1;
1✔
93
         const size_t max_mem = 32;
1✔
94

95
         for(const std::string& pwdhash : all_pwdhash) {
10✔
96
            Test::Result result("Pwdhash " + pwdhash);
9✔
97
            auto pwdhash_fam = Botan::PasswordHashFamily::create(pwdhash);
9✔
98

99
            if(pwdhash_fam) {
9✔
100
               result.start_timer();
9✔
101

102
               const std::vector<uint8_t> salt(8);
9✔
103
               const std::string password = "test";
9✔
104

105
               auto tuned_pwhash = pwdhash_fam->tune_params(32, run_time, max_mem, tune_time);
9✔
106

107
               std::vector<uint8_t> output1(32);
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);
9✔
127

128
               result.end_timer();
9✔
129
            } else {
54✔
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_PKCS12_KDF)
145

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

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

156
         Test::Result result(algo_spec);
10✔
157

158
         auto pwdhash_fam = Botan::PasswordHashFamily::create(algo_spec);
10✔
159
         if(!pwdhash_fam) {
10✔
160
            result.note_missing(algo_spec);
×
161
            return result;
162
         }
163

164
         result.test_str_eq("family name", pwdhash_fam->name(), algo_spec);
10✔
165

166
         auto pwdhash = pwdhash_fam->from_iterations(iterations);
10✔
167
         result.test_sz_eq("iterations", pwdhash->iterations(), iterations);
10✔
168

169
         std::vector<uint8_t> derived(expected.size());
10✔
170
         pwdhash->hash(derived, passphrase, salt);
10✔
171
         result.test_bin_eq("derived key", derived, expected);
10✔
172

173
         // Verify from_params(iterations) produces the same result
174
         auto pwdhash2 = pwdhash_fam->from_params(iterations);
10✔
175
         std::vector<uint8_t> derived2(expected.size());
10✔
176
         pwdhash2->hash(derived2, passphrase, salt);
10✔
177
         result.test_bin_eq("from_params matches from_iterations", derived2, expected);
10✔
178

179
         return result;
10✔
180
      }
70✔
181
};
182

183
BOTAN_REGISTER_TEST("pbkdf", "pkcs12_kdf_kat", PKCS12_KDF_KAT_Tests);
184

185
#endif
186

187
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
188

189
class Bcrypt_PBKDF_KAT_Tests final : public Text_Based_Test {
×
190
   public:
191
      Bcrypt_PBKDF_KAT_Tests() : Text_Based_Test("bcrypt_pbkdf.vec", "Passphrase,Salt,Iterations,Output") {}
2✔
192

193
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
36✔
194
         const size_t rounds = vars.get_req_sz("Iterations");
36✔
195
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
36✔
196
         const std::string passphrase = vars.get_req_str("Passphrase");
36✔
197
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
36✔
198

199
         Test::Result result("bcrypt PBKDF");
36✔
200

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

203
         if(!pwdhash_fam) {
36✔
204
            result.test_failure("Bcrypt-PBKDF is missing PasswordHashFamily");
×
205
            return result;
206
         }
207

208
         auto pwdhash = pwdhash_fam->from_iterations(rounds);
36✔
209

210
         std::vector<uint8_t> derived(expected.size());
36✔
211
         pwdhash->hash(derived, passphrase, salt);
36✔
212

213
         result.test_bin_eq("derived key", derived, expected);
36✔
214

215
         return result;
36✔
216
      }
180✔
217
};
218

219
BOTAN_REGISTER_TEST("pbkdf", "bcrypt_pbkdf", Bcrypt_PBKDF_KAT_Tests);
220

221
#endif
222

223
#if defined(BOTAN_HAS_SCRYPT)
224

225
class Scrypt_KAT_Tests final : public Text_Based_Test {
×
226
   public:
227
      Scrypt_KAT_Tests() : Text_Based_Test("scrypt.vec", "Passphrase,Salt,N,R,P,Output") {}
2✔
228

229
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
15✔
230
         const size_t N = vars.get_req_sz("N");
15✔
231
         const size_t R = vars.get_req_sz("R");
15✔
232
         const size_t P = vars.get_req_sz("P");
15✔
233
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
15✔
234
         const std::string passphrase = vars.get_req_str("Passphrase");
15✔
235
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
15✔
236

237
         Test::Result result("scrypt");
15✔
238

239
         if(N >= 1048576 && Test::run_long_tests() == false) {
15✔
240
            return result;
241
         }
242

243
         auto pwdhash_fam = Botan::PasswordHashFamily::create("Scrypt");
15✔
244

245
         if(!pwdhash_fam) {
15✔
246
            result.test_failure("Scrypt is missing PasswordHashFamily");
×
247
            return result;
248
         }
249

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

252
         std::vector<uint8_t> pwdhash_derived(expected.size());
15✔
253
         pwdhash->hash(pwdhash_derived, passphrase, salt);
15✔
254

255
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
15✔
256

257
         return result;
15✔
258
      }
75✔
259
};
260

261
BOTAN_REGISTER_TEST("pbkdf", "scrypt", Scrypt_KAT_Tests);
262

263
#endif
264

265
#if defined(BOTAN_HAS_ARGON2)
266

267
class Argon2_KAT_Tests final : public Text_Based_Test {
×
268
   public:
269
      Argon2_KAT_Tests() : Text_Based_Test("argon2.vec", "Passphrase,Salt,P,M,T,Output", "Secret,AD") {}
2✔
270

271
      Test::Result run_one_test(const std::string& mode, const VarMap& vars) override {
1,071✔
272
         const size_t P = vars.get_req_sz("P");
1,071✔
273
         const size_t M = vars.get_req_sz("M");
1,071✔
274
         const size_t T = vars.get_req_sz("T");
1,071✔
275
         const std::vector<uint8_t> key = vars.get_opt_bin("Secret");
1,071✔
276
         const std::vector<uint8_t> ad = vars.get_opt_bin("AD");
1,071✔
277
         const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
1,071✔
278
         const std::vector<uint8_t> passphrase = vars.get_req_bin("Passphrase");
1,071✔
279
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
1,071✔
280

281
         Test::Result result(mode);
1,071✔
282

283
         auto pwdhash_fam = Botan::PasswordHashFamily::create(mode);
1,071✔
284

285
         if(!pwdhash_fam) {
1,071✔
286
            result.test_failure("Argon2 is missing PasswordHashFamily");
×
287
            return result;
288
         }
289

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

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

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

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

299
         return result;
1,071✔
300
      }
6,654✔
301
};
302

303
BOTAN_REGISTER_SERIALIZED_TEST("pbkdf", "argon2", Argon2_KAT_Tests);
304

305
#endif
306

307
#if defined(BOTAN_HAS_PGP_S2K)
308

309
class PGP_S2K_Iter_Test final : public Test {
1✔
310
   public:
311
      std::vector<Test::Result> run() override {
1✔
312
         Test::Result result("PGP_S2K iteration encoding");
1✔
313

314
         // The maximum representable iteration count
315
         const size_t max_iter = 65011712;
1✔
316

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

320
         for(size_t c = 0; c != 256; ++c) {
257✔
321
            const size_t dec = Botan::RFC4880_decode_count(static_cast<uint8_t>(c));
256✔
322
            const size_t comp_dec = (16 + (c & 0x0F)) << ((c >> 4) + 6);
256✔
323
            result.test_sz_eq("Decoded value matches PGP formula", dec, comp_dec);
256✔
324

325
            const size_t enc = Botan::RFC4880_encode_count(comp_dec);
256✔
326
            result.test_sz_eq("Encoded value matches PGP formula", enc, c);
256✔
327
         }
328

329
         uint8_t last_enc = 0;
330

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

335
            /*
336
            The iteration count as encoded may not be exactly the
337
            value requested, but should never be less
338
            */
339
            const size_t dec = Botan::RFC4880_decode_count(enc);
1,015,809✔
340
            result.test_sz_gte("Decoded value is >= requested", dec, i);
1,015,809✔
341

342
            last_enc = enc;
1,015,809✔
343
         }
344

345
         return std::vector<Test::Result>{result};
3✔
346
      }
2✔
347
};
348

349
BOTAN_REGISTER_TEST("pbkdf", "pgp_s2k_iter", PGP_S2K_Iter_Test);
350

351
#endif
352

353
}  // namespace
354

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