/
pbkdf2_hmac.c
414 lines (316 loc) · 10.7 KB
/
pbkdf2_hmac.c
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <Windows.h>
#include "pbkdf2_hmac.h"
#define MAX_HASH_BUFFER_SIZE 640
/**
* Internal function for XORing blocks of memory. The memory pointed to by the
* @a dst argument is exclusive-or'd with the memory pointed to by @a dst.
*
* Both buffers @a dst and @a src must contain at least @a length bytes.
*/
static void memxor(BYTE *dst, const BYTE *src, DWORD length)
{
while(length != 0) {
dst[0] ^= src[0];
dst++;
src++;
length--;
}
}
/**
* Internal function for initializing HMAC. Computes the inner padded key and
* outer padded key (@a ipad and @a opad) for the specified password. Requires
* an initialized hash in the @a hash_init parameter.
*
* @param hash_init An initialized hash that will be duplicated if necessary.
* The hash in this parameter is not modified.
* @param passwd_len Length of the password in the @a passwd_data buffer, in
* bytes.
* @param passwd_data Buffer containing the password.
* @param hash_block_size Size of the internal block of the @a hash_init hash.
* @param ipad Pointer to a buffer that receives the inner padded key. Must be
* at least @a hash_block_size bytes in size.
* @param opad Pointer to a buffer that receives the outer padded key. Must be
* at least @a hash_block_size bytes in size.
* @return Zero on success, nonzero on failure.
*/
static int hmac_init(HCRYPTHASH hash_init,
DWORD passwd_len,
const BYTE *passwd_data,
DWORD hash_block_size,
BYTE *ipad,
BYTE *opad)
{
BYTE buffer[MAX_HASH_BUFFER_SIZE] = { 0 };
BOOL r = FALSE;
int result = 0;
DWORD length = 0;
DWORD i = 0;
HCRYPTHASH hash = NULL;
if(passwd_len > hash_block_size) {
r = CryptDuplicateHash(hash_init, NULL, 0, &hash);
if(r == FALSE || hash == NULL) {
result = -1;
goto end_error;
}
r = CryptHashData(hash, passwd_data, passwd_len, 0);
if(r == FALSE) {
result = -1;
goto end_error;
}
length = MAX_HASH_BUFFER_SIZE;
r = CryptGetHashParam(hash, HP_HASHVAL, buffer, &length, 0);
if(r == FALSE) {
result = -1;
goto end_error;
}
} else {
memcpy(buffer, passwd_data, passwd_len);
length = passwd_len;
}
memset(opad, 0x5C, hash_block_size);
memset(ipad, 0x36, hash_block_size);
for(i = 0; i < hash_block_size; i++) {
ipad[i] ^= buffer[i];
opad[i] ^= buffer[i];
}
end_error:
if(hash != NULL) {
CryptDestroyHash(hash);
hash = NULL;
}
RtlSecureZeroMemory(buffer, MAX_HASH_BUFFER_SIZE);
return result;
}
/**
* Internal function for creating a HMAC hash.
*
* @param hash_init An initialized hash that will be duplicated.
* @param ipad Previously initialized inner padded key.
* @param block_size Size of the internal block of the @a hash_init hash.
* @param out_hash Pointer to a variable that receives the created hash.
* @return Zero on success, nonzero on failure.
*/
static int hmac_start(HCRYPTHASH hash_init,
const BYTE *ipad,
DWORD block_size,
HCRYPTHASH *out_hash)
{
BOOL r = FALSE;
int result = 0;
HCRYPTHASH hash = NULL;
if(hash_init == NULL) return -1;
if(ipad == NULL) return -1;
if(block_size == 0) return -1;
if(out_hash == NULL) return -1;
r = CryptDuplicateHash(hash_init, NULL, 0, &hash);
if(r == FALSE || hash == NULL) {
result = -1;
goto end_error;
}
r = CryptHashData(hash, ipad, block_size, 0);
if(r == FALSE) {
result = -1;
goto end_error;
}
*out_hash = hash;
end_error:
if(result != 0) {
CryptDestroyHash(hash);
*out_hash = NULL;
}
return result;
}
/**
* Internal function for digesting HMAC data.
*/
static int hmac_process(HCRYPTHASH hash,
DWORD length,
const BYTE *data)
{
BOOL r;
if(hash == NULL) return -1;
r = CryptHashData(hash, data, length, 0);
return (r != FALSE) ? 0 : -1;
}
/**
* Internal function for finishing a HMAC hash.
*
* @param hash_init An initialized hash that will be duplicated.
* @param hash Pointer to the HMAC hash.
* @param opad Previously initialized outer padded key.
* @param block_size Size of the internal block of the @a hash_init hash.
* @param out_data Pointer to a buffer that receives the HMAC hash.
* @return Zero on success, nonzero on failure.
*/
static int hmac_end(HCRYPTHASH hash_init,
HCRYPTHASH *hash,
const BYTE *opad,
DWORD block_size,
BYTE *out_data)
{
DWORD length = 0;
BOOL r = FALSE;
int result = 0;
if(hash_init == NULL) return -1;
if(hash == NULL) return -1;
if(*hash == NULL) return -1;
if(opad == NULL) return -1;
if(block_size == 0) return -1;
if(out_data == NULL) return -1;
length = MAX_HASH_BUFFER_SIZE;
r = CryptGetHashParam(*hash, HP_HASHVAL, out_data, &length, 0);
if(*hash != NULL) {
CryptDestroyHash(*hash);
*hash = NULL;
}
if(r == FALSE) {
return -1;
}
r = CryptDuplicateHash(hash_init, NULL, 0, hash);
if(r == FALSE || *hash == NULL) {
result = -1;
goto end_error;
}
r = CryptHashData(*hash, opad, block_size, 0);
if(r == FALSE) {
result = -1;
goto end_error;
}
r = CryptHashData(*hash, out_data, length, 0);
if(r == FALSE) {
result = -1;
goto end_error;
}
length = MAX_HASH_BUFFER_SIZE;
r = CryptGetHashParam(*hash, HP_HASHVAL, out_data, &length, 0);
if(hash != NULL) {
CryptDestroyHash(*hash);
*hash = NULL;
}
if(r == FALSE) {
result = -1;
goto end_error;
}
end_error:
return result;
}
static void encode_uint32(DWORD value,
BYTE buffer[4])
{
buffer[0] = (value >> 24) & 0xFF;
buffer[1] = (value >> 16) & 0xFF;
buffer[2] = (value >> 8) & 0xFF;
buffer[3] = (value ) & 0xFF;
}
int pbkdf2_derive_bytes_hmac(HCRYPTHASH hash_init,
DWORD hash_block_size,
DWORD iter_count,
DWORD passwd_len,
const BYTE *passwd_data,
DWORD salt_len,
const BYTE *salt_data,
DWORD out_len,
BYTE *out_data)
{
BYTE ipad[MAX_HASH_BUFFER_SIZE];
BYTE opad[MAX_HASH_BUFFER_SIZE];
BYTE xor_sum[MAX_HASH_BUFFER_SIZE];
BYTE iter_hash[MAX_HASH_BUFFER_SIZE];
BYTE block[MAX_HASH_BUFFER_SIZE];
BYTE temp[4];
BOOL r = FALSE;
int result = 0;
HCRYPTPROV provider = NULL;
HCRYPTHASH hash = NULL;
HCRYPTHASH hash_salt = NULL;
DWORD hash_size = 0;
DWORD length = 0;
DWORD block_index = 1;
/* Sanity check arguments. */
if(hash_init == NULL) return -1;
if(hash_block_size == 0) return -1;
if(iter_count == 0) return -1;
if(out_len == 0) return 0;
if(out_data == NULL) return -1;
if(hash_block_size > MAX_HASH_BUFFER_SIZE) return -1;
/* Get the hash output size. */
length = 4;
r = CryptGetHashParam(hash_init, HP_HASHSIZE, &hash_size, &length, 0);
if(r == FALSE || hash_size > MAX_HASH_BUFFER_SIZE) {
return -1;
}
/* Initialize HMAC ipad & opad. */
result = hmac_init(hash_init, passwd_len, passwd_data, hash_block_size, ipad, opad);
if(result != 0) {
goto end_error;
}
/* Generate hashes until no more bytes need to be filled.
* Each block is the XOR-sum B = U_1 ^ U_2 ... U_{iter_count}.
* Output the bytes B, then continue on to the next block. Blocks do not
* depend on previous blocks, so this while-loop could be parallelized. */
while(out_len != 0) {
DWORD iter;
/* First iteration: U_1 = HMAC(password, salt || block_index) */
encode_uint32(block_index, temp);
/* We can compute the hash as far as the salt and then duplicate it for
* the later blocks. This will be especially advantageous if the salt is
* long. */
if(hash_salt == NULL) {
hmac_start(hash_init, ipad, hash_block_size, &hash);
hmac_process(hash, salt_len, salt_data);
r = CryptDuplicateHash(hash, NULL, 0, &hash_salt);
} else {
r = CryptDuplicateHash(hash_salt, NULL, 0, &hash);
}
if(r == FALSE) {
/* CryptDuplicateHash failed. */
result = -1;
goto end_error;
}
hmac_process(hash, sizeof(temp), temp);
result = hmac_end(hash_init, &hash, opad, hash_block_size, block);
if(result != 0) {
/* Hashing error. */
goto end_error;
}
memcpy(xor_sum, block, hash_size);
/* The rest of the iterations:
* U_i = HMAC(password, U_{i - 1}) */
for(iter = 1; iter < iter_count; iter++) {
hmac_start(hash_init, ipad, hash_block_size, &hash);
hmac_process(hash, hash_size, block);
result = hmac_end(hash_init, &hash, opad, hash_block_size, iter_hash);
if(result != 0) {
/* Hashing error. */
goto end_error;
}
/* XOR with the sum. */
memxor(xor_sum, iter_hash, hash_size);
memcpy(block, iter_hash, hash_size);
}
if(out_len >= hash_size) {
/* Advance to the next block. */
memcpy(out_data, xor_sum, hash_size);
out_data += hash_size;
out_len -= hash_size;
block_index++;
} else {
/* This was the final block. */
memcpy(out_data, xor_sum, out_len);
break;
}
}
end_error:
RtlSecureZeroMemory(ipad, MAX_HASH_BUFFER_SIZE);
RtlSecureZeroMemory(opad, MAX_HASH_BUFFER_SIZE);
RtlSecureZeroMemory(xor_sum, MAX_HASH_BUFFER_SIZE);
RtlSecureZeroMemory(iter_hash, MAX_HASH_BUFFER_SIZE);
RtlSecureZeroMemory(block, MAX_HASH_BUFFER_SIZE);
if(hash != NULL) {
CryptDestroyHash(hash);
}
if(hash_salt != NULL) {
CryptDestroyHash(hash_salt);
}
return result;
}