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

randombit / botan / 23949924622

03 Apr 2026 02:35PM UTC coverage: 89.531% (+0.05%) from 89.479%
23949924622

Pull #5478

github

web-flow
Merge 543049103 into eaf12915e
Pull Request #5478: Add PKCS#12 KDF

105670 of 118026 relevant lines covered (89.53%)

11548366.4 hits per line

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

90.64
/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
#endif
14

15
#if defined(BOTAN_HAS_PBKDF) || defined(BOTAN_HAS_PKCS12_KDF)
16
   #include <botan/pwdhash.h>
17
#endif
18

19
#if defined(BOTAN_HAS_RFC4880)
20
   #include <botan/rfc4880.h>
21
#endif
22

23
namespace Botan_Tests {
24

25
namespace {
26

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

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

39
         Test::Result result(pbkdf_name);
38✔
40
         auto pbkdf = Botan::PBKDF::create(pbkdf_name);
38✔
41

42
         if(!pbkdf) {
38✔
43
            result.note_missing(pbkdf_name);
10✔
44
            return result;
45
         }
46

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

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

53
         auto pwdhash_fam = Botan::PasswordHashFamily::create(pbkdf_name);
28✔
54

55
         if(!pwdhash_fam) {
28✔
56
            result.note_missing("No PasswordHashFamily for " + pbkdf_name);
×
57
            return result;
×
58
         }
59

60
         auto pwdhash = pwdhash_fam->from_params(iterations);
28✔
61

62
         std::vector<uint8_t> pwdhash_derived(outlen);
28✔
63
         pwdhash->hash(pwdhash_derived, passphrase, salt);
28✔
64

65
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
28✔
66

67
         return result;
28✔
68
      }
226✔
69
};
70

71
BOTAN_REGISTER_SMOKE_TEST("pbkdf", "pbkdf_kat", PBKDF_KAT_Tests);
72

73
class Pwdhash_Tests : public Test {
1✔
74
   public:
75
      std::vector<Test::Result> run() override {
1✔
76
         std::vector<Test::Result> results;
1✔
77

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

88
         const uint64_t run_time = 3;
1✔
89
         const uint64_t tune_time = 1;
1✔
90
         const size_t max_mem = 32;
1✔
91

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

96
            if(pwdhash_fam) {
9✔
97
               result.start_timer();
9✔
98

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

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

113
                  std::unique_ptr<Botan::PasswordHash> pwhash;
9✔
114

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

122
                  std::vector<uint8_t> output2(32);
9✔
123
                  pwhash->hash(output2, password, salt);
9✔
124

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

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

136
            results.push_back(result);
9✔
137
         }
9✔
138

139
         return results;
1✔
140
      }
1✔
141
};
142

143
BOTAN_REGISTER_TEST("pbkdf", "pwdhash", Pwdhash_Tests);
144

145
#endif
146

147
#if defined(BOTAN_HAS_PKCS12_KDF)
148

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

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

159
         Test::Result result(algo_spec);
10✔
160

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

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

169
         auto pwdhash = pwdhash_fam->from_iterations(iterations);
10✔
170
         result.test_sz_eq("iterations", pwdhash->iterations(), iterations);
10✔
171

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

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

182
         return result;
10✔
183
      }
70✔
184
};
185

186
BOTAN_REGISTER_TEST("pbkdf", "pkcs12_kdf_kat", PKCS12_KDF_KAT_Tests);
187

188
#endif
189

190
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
191

192
class Bcrypt_PBKDF_KAT_Tests final : public Text_Based_Test {
×
193
   public:
194
      Bcrypt_PBKDF_KAT_Tests() : Text_Based_Test("bcrypt_pbkdf.vec", "Passphrase,Salt,Iterations,Output") {}
2✔
195

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

202
         Test::Result result("bcrypt PBKDF");
36✔
203

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

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

211
         auto pwdhash = pwdhash_fam->from_iterations(rounds);
36✔
212

213
         std::vector<uint8_t> derived(expected.size());
36✔
214
         pwdhash->hash(derived, passphrase, salt);
36✔
215

216
         result.test_bin_eq("derived key", derived, expected);
36✔
217

218
         return result;
36✔
219
      }
180✔
220
};
221

222
BOTAN_REGISTER_TEST("pbkdf", "bcrypt_pbkdf", Bcrypt_PBKDF_KAT_Tests);
223

224
#endif
225

226
#if defined(BOTAN_HAS_SCRYPT)
227

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

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

240
         Test::Result result("scrypt");
15✔
241

242
         if(N >= 1048576 && Test::run_long_tests() == false) {
15✔
243
            return result;
244
         }
245

246
         auto pwdhash_fam = Botan::PasswordHashFamily::create("Scrypt");
15✔
247

248
         if(!pwdhash_fam) {
15✔
249
            result.test_failure("Scrypt is missing PasswordHashFamily");
×
250
            return result;
251
         }
252

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

255
         std::vector<uint8_t> pwdhash_derived(expected.size());
15✔
256
         pwdhash->hash(pwdhash_derived, passphrase, salt);
15✔
257

258
         result.test_bin_eq("pwdhash derived key", pwdhash_derived, expected);
15✔
259

260
         return result;
15✔
261
      }
75✔
262
};
263

264
BOTAN_REGISTER_TEST("pbkdf", "scrypt", Scrypt_KAT_Tests);
265

266
#endif
267

268
#if defined(BOTAN_HAS_ARGON2)
269

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

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

284
         Test::Result result(mode);
1,071✔
285

286
         auto pwdhash_fam = Botan::PasswordHashFamily::create(mode);
1,071✔
287

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

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

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

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

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

302
         return result;
1,071✔
303
      }
6,654✔
304
};
305

306
BOTAN_REGISTER_SERIALIZED_TEST("pbkdf", "argon2", Argon2_KAT_Tests);
307

308
#endif
309

310
#if defined(BOTAN_HAS_PGP_S2K)
311

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

317
         // The maximum representable iteration count
318
         const size_t max_iter = 65011712;
1✔
319

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

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

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

332
         uint8_t last_enc = 0;
333

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

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

345
            last_enc = enc;
1,015,809✔
346
         }
347

348
         return std::vector<Test::Result>{result};
3✔
349
      }
2✔
350
};
351

352
BOTAN_REGISTER_TEST("pbkdf", "pgp_s2k_iter", PGP_S2K_Iter_Test);
353

354
#endif
355

356
}  // namespace
357

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