1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* (C) 2014,2015,2019 Jack Lloyd
* (C) 2018 Ribose Inc
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_PBKDF)
#include <botan/pbkdf.h>
#include <botan/pwdhash.h>
#endif
#if defined(BOTAN_HAS_PGP_S2K)
#include <botan/pgp_s2k.h>
#endif
#if defined(BOTAN_HAS_SCRYPT)
#include <botan/scrypt.h>
#endif
#if defined(BOTAN_HAS_ARGON2)
#include <botan/argon2.h>
#endif
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
#include <botan/bcrypt_pbkdf.h>
#endif
namespace Botan_Tests {
namespace {
#if defined(BOTAN_HAS_PBKDF)
class PBKDF_KAT_Tests final : public Text_Based_Test
{
public:
PBKDF_KAT_Tests() : Text_Based_Test("pbkdf", "Iterations,Salt,Passphrase,Output") {}
Test::Result run_one_test(const std::string& pbkdf_name, const VarMap& vars) override
{
const size_t iterations = vars.get_req_sz("Iterations");
const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
const std::string passphrase = vars.get_req_str("Passphrase");
const std::vector<uint8_t> expected = vars.get_req_bin("Output");
const size_t outlen = expected.size();
Test::Result result(pbkdf_name);
std::unique_ptr<Botan::PBKDF> pbkdf(Botan::PBKDF::create(pbkdf_name));
if(!pbkdf)
{
result.note_missing(pbkdf_name);
return result;
}
result.test_eq("Expected name", pbkdf->name(), pbkdf_name);
const Botan::secure_vector<uint8_t> derived =
pbkdf->derive_key(outlen, passphrase, salt.data(), salt.size(), iterations).bits_of();
result.test_eq("derived key", derived, expected);
auto pwdhash_fam = Botan::PasswordHashFamily::create(pbkdf_name);
if(!pwdhash_fam)
{
result.note_missing("No PasswordHashFamily for " + pbkdf_name);
return result;
}
auto pwdhash = pwdhash_fam->from_params(iterations);
std::vector<uint8_t> pwdhash_derived(outlen);
pwdhash->derive_key(pwdhash_derived.data(), pwdhash_derived.size(),
passphrase.c_str(), passphrase.size(),
salt.data(), salt.size());
result.test_eq("pwdhash derived key", pwdhash_derived, expected);
return result;
}
};
BOTAN_REGISTER_TEST("pbkdf", PBKDF_KAT_Tests);
class Pwdhash_Tests : public Test
{
public:
std::vector<Test::Result> run() override
{
std::vector<Test::Result> results;
const std::vector<std::string> all_pwdhash = {
"Scrypt",
"PBKDF2(SHA-256)",
"OpenPGP-S2K(SHA-384)",
"Argon2d",
"Argon2i",
"Argon2id",
"Bcrypt-PBKDF"
};
for(std::string pwdhash : all_pwdhash)
{
Test::Result result("Pwdhash " + pwdhash);
auto pwdhash_fam = Botan::PasswordHashFamily::create(pwdhash);
if(pwdhash_fam)
{
result.start_timer();
const std::vector<uint8_t> salt(8);
const std::string password = "test";
auto tuned_pwhash = pwdhash_fam->tune(32, std::chrono::milliseconds(10));
std::vector<uint8_t> output1(32);
tuned_pwhash->derive_key(output1.data(), output1.size(),
password.c_str(), password.size(),
salt.data(), salt.size());
std::unique_ptr<Botan::PasswordHash> pwhash;
if(pwdhash_fam->name() == "Scrypt" || pwdhash_fam->name().find("Argon2") == 0)
{
pwhash = pwdhash_fam->from_params(tuned_pwhash->memory_param(),
tuned_pwhash->iterations(),
tuned_pwhash->parallelism());
}
else
{
pwhash = pwdhash_fam->from_params(tuned_pwhash->iterations());
}
std::vector<uint8_t> output2(32);
pwhash->derive_key(output2.data(), output2.size(),
password.c_str(), password.size(),
salt.data(), salt.size());
result.test_eq("PasswordHash produced same output when run with same params",
output1, output2);
auto default_pwhash = pwdhash_fam->default_params();
std::vector<uint8_t> output3(32);
default_pwhash->derive_key(output3.data(), output3.size(),
password.c_str(), password.size(),
salt.data(), salt.size());
result.end_timer();
}
else
result.test_note("No such algo " + pwdhash);
results.push_back(result);
}
return results;
}
};
BOTAN_REGISTER_TEST("pwdhash", Pwdhash_Tests);
#endif
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
class Bcrypt_PBKDF_KAT_Tests final : public Text_Based_Test
{
public:
Bcrypt_PBKDF_KAT_Tests() : Text_Based_Test("bcrypt_pbkdf.vec", "Passphrase,Salt,Iterations,Output") {}
Test::Result run_one_test(const std::string&, const VarMap& vars) override
{
const size_t rounds = vars.get_req_sz("Iterations");
const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
const std::string passphrase = vars.get_req_str("Passphrase");
const std::vector<uint8_t> expected = vars.get_req_bin("Output");
Test::Result result("bcrypt PBKDF");
std::vector<uint8_t> output(expected.size());
Botan::bcrypt_pbkdf(output.data(), output.size(),
passphrase.data(), passphrase.size(),
salt.data(), salt.size(),
rounds);
result.test_eq("derived key", output, expected);
return result;
}
};
BOTAN_REGISTER_TEST("bcrypt_pbkdf", Bcrypt_PBKDF_KAT_Tests);
#endif
#if defined(BOTAN_HAS_SCRYPT)
class Scrypt_KAT_Tests final : public Text_Based_Test
{
public:
Scrypt_KAT_Tests() : Text_Based_Test("scrypt.vec", "Passphrase,Salt,N,R,P,Output") {}
Test::Result run_one_test(const std::string&, const VarMap& vars) override
{
const size_t N = vars.get_req_sz("N");
const size_t R = vars.get_req_sz("R");
const size_t P = vars.get_req_sz("P");
const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
const std::string passphrase = vars.get_req_str("Passphrase");
const std::vector<uint8_t> expected = vars.get_req_bin("Output");
Test::Result result("scrypt");
if(N >= 1048576 && Test::run_long_tests() == false)
return result;
std::vector<uint8_t> output(expected.size());
Botan::scrypt(output.data(), output.size(),
passphrase, salt.data(), salt.size(),
N, R, P);
result.test_eq("derived key", output, expected);
auto pwdhash_fam = Botan::PasswordHashFamily::create("Scrypt");
if(!pwdhash_fam)
{
result.test_failure("Scrypt is missing PasswordHashFamily");
return result;
}
auto pwdhash = pwdhash_fam->from_params(N, R, P);
std::vector<uint8_t> pwdhash_derived(expected.size());
pwdhash->derive_key(pwdhash_derived.data(), pwdhash_derived.size(),
passphrase.c_str(), passphrase.size(),
salt.data(), salt.size());
result.test_eq("pwdhash derived key", pwdhash_derived, expected);
return result;
}
};
BOTAN_REGISTER_TEST("scrypt", Scrypt_KAT_Tests);
#endif
#if defined(BOTAN_HAS_ARGON2)
class Argon2_KAT_Tests final : public Text_Based_Test
{
public:
Argon2_KAT_Tests() : Text_Based_Test("argon2.vec", "Passphrase,Salt,P,M,T,Output", "Secret,AD") {}
Test::Result run_one_test(const std::string& mode, const VarMap& vars) override
{
const size_t P = vars.get_req_sz("P");
const size_t M = vars.get_req_sz("M");
const size_t T = vars.get_req_sz("T");
const std::vector<uint8_t> key = vars.get_opt_bin("Secret");
const std::vector<uint8_t> ad = vars.get_opt_bin("AD");
const std::vector<uint8_t> salt = vars.get_req_bin("Salt");
const std::vector<uint8_t> passphrase = vars.get_req_bin("Passphrase");
const std::vector<uint8_t> expected = vars.get_req_bin("Output");
uint8_t family;
if(mode == "Argon2d")
family = 0;
else if(mode == "Argon2i")
family = 1;
else if(mode == "Argon2id")
family = 2;
else
throw Test_Error("Unknown Argon2 mode");
Test::Result result(mode);
std::vector<uint8_t> output(expected.size());
Botan::argon2(output.data(), output.size(),
reinterpret_cast<const char*>(passphrase.data()),
passphrase.size(),
salt.data(), salt.size(),
key.data(), key.size(),
ad.data(), ad.size(),
family, P, M, T);
result.test_eq("derived key", output, expected);
return result;
}
};
BOTAN_REGISTER_TEST("argon2", Argon2_KAT_Tests);
#endif
#if defined(BOTAN_HAS_PGP_S2K)
class PGP_S2K_Iter_Test final : public Test
{
public:
std::vector<Test::Result> run() override
{
Test::Result result("PGP_S2K iteration encoding");
// The maximum representable iteration count
const size_t max_iter = 65011712;
result.test_eq("Encoding of large value accepted",
Botan::RFC4880_encode_count(max_iter * 2), size_t(255));
result.test_eq("Encoding of small value accepted",
Botan::RFC4880_encode_count(0), size_t(0));
for(size_t c = 0; c != 256; ++c)
{
const size_t dec = Botan::RFC4880_decode_count(static_cast<uint8_t>(c));
const size_t comp_dec = (16 + (c & 0x0F)) << ((c >> 4) + 6);
result.test_eq("Decoded value matches PGP formula", dec, comp_dec);
const size_t enc = Botan::RFC4880_encode_count(comp_dec);
result.test_eq("Encoded value matches PGP formula", enc, c);
}
uint8_t last_enc = 0;
for(size_t i = 0; i <= max_iter; i += 64)
{
const uint8_t enc = Botan::RFC4880_encode_count(i);
result.test_lte("Encoded value non-decreasing", last_enc, enc);
/*
The iteration count as encoded may not be exactly the
value requested, but should never be less
*/
const size_t dec = Botan::RFC4880_decode_count(enc);
result.test_gte("Decoded value is >= requested", dec, i);
last_enc = enc;
}
return std::vector<Test::Result>{result};
}
};
BOTAN_REGISTER_TEST("pgp_s2k_iter", PGP_S2K_Iter_Test);
#endif
}
}
|