-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenSSL_PBKDF2.cpp
More file actions
260 lines (213 loc) · 8.4 KB
/
Copy pathOpenSSL_PBKDF2.cpp
File metadata and controls
260 lines (213 loc) · 8.4 KB
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
//-------|---------|---------|---------|---------|---------|---------|---------|
//
// cryptotest.cpp
//
//-------|---------|---------|---------|---------|---------|---------|---------|
//-----------------------------------------------------------------------------|
// AUTHORSHIP
//-----------------------------------------------------------------------------|
//
// Tim Lum
// twhlum@gmail.com
// Created: 2019.01.19
// Modified: 2019.01.19
//
//-----------------------------------------------------------------------------|
// ACKNOWLEDGMENTS
//-----------------------------------------------------------------------------|
//
// Template author:
// Tim Lum (github.com/Teabeans/CPP_Lean/template.h)
//
// Refer to ACKNOWLEDGEMENTS.txt
//
//-----------------------------------------------------------------------------|
// FILE DESCRIPTION
//-----------------------------------------------------------------------------|
//
// This file is a test of the OpenSSL library for PBKDF2 support
//-----------------------------------------------------------------------------|
// PACKAGE FILES
//-----------------------------------------------------------------------------|
//
// Refer to PACKAGEFILES.txt
//
//-----------------------------------------------------------------------------|
// USAGE
//-----------------------------------------------------------------------------|
//
// OpenSSL library must be installed beforehand with:
// $ sudo apt-get install libssl-dev
//
// Compile with:
// $ g++ -std=c++11 *.cpp -lssl -lcrypto -o test.out
//
// Run with:
// $ ./test.out
//
//-----------------------------------------------------------------------------|
// ERRATA
//-----------------------------------------------------------------------------|
//
// PBKDF2 HMAC SHA-256 test vectors from
// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
//
/*
PBKDF2 HMAC-SHA256 Test Vectors
Input:
P = "password" (8 octets)
S = "salt" (4 octets)
c = 1
dkLen = 32
Output:
DK = 12 0f b6 cf fc f8 b3 2c
43 e7 22 52 56 c4 f8 37
a8 65 48 c9 2c cc 35 48
08 05 98 7c b7 0b e1 7b (32 octets)
Input:
P = "password" (8 octets)
S = "salt" (4 octets)
c = 2
dkLen = 32
Output:
DK = ae 4d 0c 95 af 6b 46 d3
2d 0a df f9 28 f0 6d d0
2a 30 3f 8e f3 c2 51 df
d6 e2 d8 5a 95 47 4c 43 (32 octets)
Input:
P = "password" (8 octets)
S = "salt" (4 octets)
c = 4096
dkLen = 32
Output:
DK = c5 e4 78 d5 92 88 c8 41
aa 53 0d b6 84 5c 4c 8d
96 28 93 a0 01 ce 4e 11
a4 96 38 73 aa 98 13 4a (32 octets)
Input:
P = "password" (8 octets)
S = "salt" (4 octets)
c = 16777216
dkLen = 32
Output:
DK = cf 81 c6 6f e8 cf c0 4d
1f 31 ec b6 5d ab 40 89
f7 f1 79 e8 9b 3b 0b cb
17 ad 10 e3 ac 6e ba 46 (32 octets)
Input:
P = "passwordPASSWORDpassword" (24 octets)
S = "saltSALTsaltSALTsaltSALTsaltSALTsalt" (36 octets)
c = 4096
dkLen = 40
Output:
DK = 34 8c 89 db cb d3 2b 2f
32 d8 14 b8 11 6e 84 cf
2b 17 34 7e bc 18 00 18
1c 4e 2a 1f b8 dd 53 e1
c6 35 51 8c 7d ac 47 e9 (40 octets)
Input:
P = "pass\0word" (9 octets)
S = "sa\0lt" (5 octets)
c = 4096
dkLen = 16
Output:
DK = 89 b6 9d 05 16 f8 29 89
3c 69 62 26 65 0a 86 87 (16 octets)
*/
//-----------------------------------------------------------------------------|
// LICENSE
//-----------------------------------------------------------------------------|
//
// Refer to LICENSE.txt
//
//-----------------------------------------------------------------------------|
// CODE STANDARDS
//-----------------------------------------------------------------------------|
//
// Refer to CODESTANDARDS.txt
//
//-----------------------------------------------------------------------------|
// IMPORTS / INCLUDES
//-----------------------------------------------------------------------------|
// CONTAINERS
// #include <array> // http://www.cplusplus.com/reference/array/array/
// #include <deque> // http://www.cplusplus.com/reference/deque/deque/
// #include <forward_list> // http://www.cplusplus.com/reference/forward_list/forward_list/
// #include <list> // http://www.cplusplus.com/reference/list/list/
// #include <map> // http://www.cplusplus.com/reference/map/map/
// #include <queue> // http://www.cplusplus.com/reference/queue/queue/
// #include <set> // http://www.cplusplus.com/reference/set/set/
// #include <stack> // http://www.cplusplus.com/reference/stack/stack/
// #include <unordered_map> // http://www.cplusplus.com/reference/unordered_map/unordered_map/
// #include <unordered_set> // http://www.cplusplus.com/reference/unordered_set/unordered_set/
// #include <vector> // http://www.cplusplus.com/reference/vector/vector/
// INPUT / OUTPUT
#include <iostream> // For input/output (C++)
// #include <stdio.h> // For input/output (C)
#include <cstring> // Fpr strlen( )
#include <iomanip> // For width-formatted outputs
// #include <fstream> // For file-stream
// MULTITHREADING
// #include <pthread.h> // https://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
// #include <atomic> // http://www.cplusplus.com/reference/atomic/
// #include <condition_variable> // http://www.cplusplus.com/reference/condition_variable/
// #include <future> // http://www.cplusplus.com/reference/future/
// #include <mutex> // http://www.cplusplus.com/reference/mutex/
// #include <thread> // http://www.cplusplus.com/reference/thread/thread/
// EXCEPTIONS
// #include <exception> // http://www.cplusplus.com/reference/exception/exception/
// #include <assert.h> // http://www.cplusplus.com/reference/cassert/assert/
// OTHER
// #include <algorithm> // http://www.cplusplus.com/reference/algorithm
// #include <chrono> // http://www.cplusplus.com/reference/chrono/
// #include <time.h> // Superceded by <chrono>
// #include <random> // http://www.cplusplus.com/reference/random/
#include <string> // http://www.cplusplus.com/reference/string/
// #include <sstream> // http://www.cplusplus.com/reference/sstream/
// #include <utility> // http://www.cplusplus.com/reference/utility/
// #include <unistd.h> // Provides access to the POSIX operating system AP
#include <openssl/evp.h> // For the core OpenSSL library (Note: Must be installed)
#include <openssl/sha.h> // SHA1 and SHA2 hashes (used in key generation)
//-----------------------------------------------------------------------------|
// GLOBAL VARIABLES
//-----------------------------------------------------------------------------|
// None for this class
//-----------------------------------------------------------------------------|
// NAMESPACES
//-----------------------------------------------------------------------------|
// NOTE: Use of namespaces is discouraged
// using namespace std;
//-----------------------------------------------------------------------------|
// DRIVER (If applicable)
//-----------------------------------------------------------------------------|
int main( int argc, char* argv[] ) {
const char* password = "passwordPASSWORDpassword";
const char* theSalt = "saltSALTsaltSALTsaltSALTsaltSALTsalt";
int iterations = 4096;
int dkLen = 40; // Will be 32 byte eventually
int passLength = 24;
int saltLength = 36;
unsigned char PBKDF2Result[dkLen];
// From https://www.openssl.org/docs/man1.1.0/crypto/PKCS5_PBKDF2_HMAC_SHA1.html
PKCS5_PBKDF2_HMAC( password,
passLength,
(unsigned char*)theSalt,
saltLength,
iterations,
EVP_sha256(),
dkLen, // Desired byte-length of the derived key
PBKDF2Result );
for( int i = 0 ; i < dkLen ; i++ ) {
if( i % 8 == 0 ) {
std::cout << std::endl;
}
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)PBKDF2Result[i] << " ";
}
std::cout << std::endl;
return( 0 );
}
//-----------------------------------------------------------------------------|
// STATIC METHODS
//-----------------------------------------------------------------------------|
// None in this class
// End of file - crypotest.cpp