-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpbkdf2_crypto++.cpp
392 lines (320 loc) · 12.3 KB
/
pbkdf2_crypto++.cpp
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <cryptopp/hex.h>
#include <cryptopp/sha.h>
#include <cryptopp/pwdbased.h>
// MD5 is Weak! Allow weakness ONLY if you need PBKDF2_HMAC_MD5
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
#include <getopt.h>
#include <iostream>
#include <string>
// locale includes for tolower()
#include <locale>
using namespace std;
using namespace CryptoPP;
#define MD5_cryptopp 100
#define SHA_1_cryptopp 2100
#define SHA_224_cryptopp 2200
#define SHA_256_cryptopp 2300
#define SHA_384_cryptopp 2400
#define SHA_512_cryptopp 2500
// Binary printing courtesy https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
(byte & 0x10 ? 1 : 0), \
(byte & 0x08 ? 1 : 0), \
(byte & 0x04 ? 1 : 0), \
(byte & 0x02 ? 1 : 0), \
(byte & 0x01 ? 1 : 0)
string PBKDF2_HMAC_SHA_512_string(string pass, string salt, uint iterations, uint outputBytes)
{
SecByteBlock result(outputBytes);
string hexResult;
PKCS5_PBKDF2_HMAC<SHA512> pbkdf;
pbkdf.DeriveKey(result, result.size(),0x00,(byte *) pass.data(), pass.size(),(byte *) salt.data(), salt.size(),iterations);
ArraySource resultEncoder(result,result.size(), true, new HexEncoder(new StringSink(hexResult)));
return hexResult;
}
string PBKDF2_HMAC_SHA_384_string(string pass, string salt, uint iterations, uint outputBytes)
{
SecByteBlock result(outputBytes);
string hexResult;
PKCS5_PBKDF2_HMAC<SHA384> pbkdf;
pbkdf.DeriveKey(result, result.size(),0x00,(byte *) pass.data(), pass.size(),(byte *) salt.data(), salt.size(),iterations);
ArraySource resultEncoder(result,result.size(), true, new HexEncoder(new StringSink(hexResult)));
return hexResult;
}
string PBKDF2_HMAC_SHA_256_string(string pass, string salt, uint iterations, uint outputBytes)
{
SecByteBlock result(outputBytes);
string hexResult;
PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
pbkdf.DeriveKey(result, result.size(),0x00,(byte *) pass.data(), pass.size(),(byte *) salt.data(), salt.size(),iterations);
ArraySource resultEncoder(result,result.size(), true, new HexEncoder(new StringSink(hexResult)));
return hexResult;
}
string PBKDF2_HMAC_SHA_224_string(string pass, string salt, uint iterations, uint outputBytes)
{
SecByteBlock result(outputBytes);
string hexResult;
PKCS5_PBKDF2_HMAC<SHA224> pbkdf;
pbkdf.DeriveKey(result, result.size(),0x00,(byte *) pass.data(), pass.size(),(byte *) salt.data(), salt.size(),iterations);
ArraySource resultEncoder(result,result.size(), true, new HexEncoder(new StringSink(hexResult)));
return hexResult;
}
string PBKDF2_HMAC_SHA_1_string(string pass, string salt, uint iterations, uint outputBytes)
{
SecByteBlock result(outputBytes);
string hexResult;
PKCS5_PBKDF2_HMAC<SHA1> pbkdf;
pbkdf.DeriveKey(result, result.size(),0x00,(byte *) pass.data(), pass.size(),(byte *) salt.data(), salt.size(),iterations);
ArraySource resultEncoder(result,result.size(), true, new HexEncoder(new StringSink(hexResult)));
return hexResult;
}
string PBKDF2_HMAC_MD5_string(string pass, string salt, uint iterations, uint outputBytes)
{
SecByteBlock result(outputBytes);
string hexResult;
PKCS5_PBKDF2_HMAC<Weak::MD5> pbkdf;
pbkdf.DeriveKey(result, result.size(),0x00,(byte *) pass.data(), pass.size(),(byte *) salt.data(), salt.size(),iterations);
ArraySource resultEncoder(result,result.size(), true, new HexEncoder(new StringSink(hexResult)));
return hexResult;
}
int main(int argc, char ** argv)
{
std::string pass, salt, hexResult, expected, algoString;
int iterations = 0, outputBytes = 0, algo = 0, c;
byte verbose = 0, help = 0, interactive = 0;
std::locale loc;
opterr = 0;
while ((c = getopt (argc, argv, "nhva:p:P:s:S:i:o:O:e:")) != -1)
switch (c)
{
case 'a':
algoString = optarg;
break;
case 'p':
pass = optarg;
break;
case 's':
salt = optarg;
break;
case 'i':
iterations = atoi(optarg);
break;
case 'o':
outputBytes = atoi(optarg);
break;
case 'v':
verbose = 1;
break;
case 'h':
help = 1;
break;
case 'n':
interactive = 1;
break;
case 'e':
expected = optarg;
break;
case '?':
cout << "Case ?";
if (optopt == 'c')
std::cout << "Option - " << optopt << " requires an argument." << std::endl;
else if (isprint (optopt))
std::cout << "Unknown option '" << optopt << "'" << std::endl;
else
std::cout << "Unknown option character '" << optopt << "'" << std::endl;
return 1;
default:
cout << "Case default" << std::endl;
break;
}
if (help)
{
std::cout << "Compiled with Crypto++ version " << CRYPTOPP_VERSION;
if (CRYPTOPP_BOOL_X64)
{
std::cout << " x64 ";
};
if (CRYPTOPP_BOOL_X86)
{
std::cout << " x86 ";
};
#if defined(CRYPTOPP_BOOL_X86)
std::cout << " CRYPTOPP_BOOL_X86 1";
#endif
#if defined(CRYPTOPP_X86_ASM_AVAILABLE)
std::cout << " CRYPTOPP_X86_ASM_AVAILABLE 1";
#endif
#if defined(CRYPTOPP_X64_MASM_AVAILABLE)
std::cout << " CRYPTOPP_X64_MASM_AVAILABLE 1";
#endif
#if defined(CRYPTOPP_GENERATE_X64_MASM)
std::cout << " CRYPTOPP_GENERATE_X64_MASM 1";
#endif
#if defined(CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE)
std::cout << " CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE 1";
#endif
std::cout << std::endl << "Running with Crypto++ version (detecting current library not yet implemented)" << std::endl;
cout << "Example: " << argv[0] << " -a SHA-512 -p password -s salt -i 131072 -o 64" << std::endl;
cout << "\nOptions: " << std::endl;
cout << " -h help" << std::endl;
cout << " -v Verbose" << std::endl;
cout << " -a algo algorithm, valid values SHA-512|SHA-384|SHA-256|SHA-224|SHA-1|MD5 Note that in particular, SHA-384 and SHA-512 use 64-bit operations which as of 2014 penalize GPU's (attackers) much, much more than CPU's (you). Use one of these two if at all possible." << std::endl;
cout << " -p password Password to hash" << std::endl;
cout << " -P passwordfmt NOT YET IMPLEMENTED - always string" << std::endl;
cout << " -s salt Salt for the hash. Should be long and cryptographically random." << std::endl;
cout << " -S saltfmt NOT YET IMPLEMENTED - always string" << std::endl;
cout << " -i iterations Number of iterations, as high as you can handle the delay for, at least 16384 recommended." << std::endl;
cout << " -o bytes Number of bytes of output; for password hashing, keep less than or equal to native hash size (MD5 <=16, SHA-1 <=20, SHA-256 <=32, SHA-512 <=64)" << std::endl;
cout << " -O outputfmt Output format NOT YET IMPLEMENTED - always HEX (lowercase)" << std::endl;
cout << " -e hash Expected hash (in the same format as outputfmt) results in output of 0 <actual> <expected> = different, 1 = same NOT YET IMPLEMENTED" << std::endl;
cout << " -n Interactive mode - needs no other arguments - asks for password, salt, iterations, outputBytes, and algorithm" << std::endl;
};
if (interactive)
{
std::cout << std::endl << "Enter pass: " << std::endl;
std::getline(std::cin,pass);
std::cout << "Enter salt: " << std::endl;
std::getline(std::cin,salt);
std::cout << "Enter iterations: " << std::endl;
std::cin >> iterations;
std::cout << "Enter outputBytes: " << std::endl;
std::cin >> outputBytes;
std::cout << "Enter algorithm - valid values are one of SHA-512|SHA-384|SHA-256|SHA-224|SHA-1|MD5: " << std::endl;
std::cin >> algoString;
// std::cout << "Enter expected result hash (lower case hex): ";
// std::cin >> expected;
};
if(algoString.compare("")==0)
{
cout << "Use -h for help." << std::endl;
}
if (algoString.compare("SHA-512")==0)
{
algo = SHA_512_cryptopp;
}
else if (algoString.compare("SHA-384")==0)
{
algo = SHA_384_cryptopp;
}
else if (algoString.compare("SHA-256")==0)
{
algo = SHA_256_cryptopp;
}
else if (algoString.compare("SHA-224")==0)
{
algo = SHA_224_cryptopp;
}
else if (algoString.compare("SHA-1")==0)
{
algo = SHA_1_cryptopp;
}
else if (algoString.compare("MD5")==0)
{
algo = MD5_cryptopp;
}
else
{
cout << "ERROR: algorithm '" << algoString << "' unknown." << std::endl;
return 4;
}
if (verbose)
{
cout << "Interpreted arguments: algo " << algo << " password " << pass << " salt " << salt << " iterations " << iterations << " outputbytes " << outputBytes << std::endl << std::endl;
}
if (algo <= 0)
{
cout << "You must select a known algorithm identifier." << std::endl;
return 10;
}
if (iterations <= 0)
{
cout << "You must select at least one iteration (and preferably tens of thousands or (much) more." << std::endl;
return 11;
}
if (outputBytes <= 0)
{
cout << "You must select at least one byte of output length." << std::endl;
return 12;
}
switch (algo)
{
case SHA_512_cryptopp:
if (verbose && outputBytes > 64)
{
cout << "WARNING: If you intend to use the result for password hashing, you should not choose a length greater than the native output size of the underlying hash function." << std::endl;
}
hexResult = PBKDF2_HMAC_SHA_512_string(pass, salt, iterations, outputBytes);
break;
case SHA_384_cryptopp:
if (verbose && outputBytes > 48)
{
cout << "WARNING: If you intend to use the result for password hashing, you should not choose a length greater than the native output size of the underlying hash function." << std::endl;
}
hexResult = PBKDF2_HMAC_SHA_384_string(pass, salt, iterations, outputBytes);
break;
case SHA_256_cryptopp:
if (verbose && outputBytes > 32)
{
cout << "WARNING: If you intend to use the result for password hashing, you should not choose a length greater than the native output size of the underlying hash function." << std::endl;
}
hexResult = PBKDF2_HMAC_SHA_256_string(pass, salt, iterations, outputBytes);
break;
case SHA_224_cryptopp:
if (verbose && outputBytes > 28)
{
cout << "WARNING: If you intend to use the result for password hashing, you should not choose a length greater than the native output size of the underlying hash function." << std::endl;
}
hexResult = PBKDF2_HMAC_SHA_224_string(pass, salt, iterations, outputBytes);
break;
case SHA_1_cryptopp:
if (verbose && outputBytes > 20)
{
cout << "WARNING: If you intend to use the result for password hashing, you should not choose a length greater than the native output size of the underlying hash function." << std::endl;
}
hexResult = PBKDF2_HMAC_SHA_1_string(pass, salt, iterations, outputBytes);
break;
case MD5_cryptopp:
if (verbose && outputBytes > 16)
{
cout << "WARNING: If you intend to use the result for password hashing, you should not choose a length greater than the native output size of the underlying hash function." << std::endl;
}
hexResult = PBKDF2_HMAC_MD5_string(pass, salt, iterations, outputBytes);
break;
default:
cout << "Invalid algorithm choice. Internal value : " << algo << std::endl;
return 2;
}
// lowercase it
for (std::string::size_type i=0; i<hexResult.length(); ++i)
hexResult[i] = std::tolower(hexResult[i],loc);
if (expected.size() < 1)
{
// Normal output
cout << hexResult << std::endl;
}
else
{
// Did it match or not?
if (expected.compare(hexResult)==0)
{
cout << "1" << std::endl;
}
else
{
cout << "0 " << hexResult << " " << expected << std::endl;
}
}
/*
If you wanted to return the hex values of the salt, this is a way to do that:
SecByteBlock saltSBB(salt.size());
saltSBB.Assign((unsigned char *)salt.data(),salt.size());
string hexsalt;
ArraySource saltEncoder(saltSBB,saltSBB.size(), true, new HexEncoder(new StringSink(hexsalt)));
*/
return 0;
}