Skip to content
Newer
Older
100755 56 lines (41 sloc) 1.07 KB
7aef368 (UNINTELLIGIBLE)
Zachary Schneirov authored
1 /*
2 * pbkdf2.c
3 *
4 */
5
6 #include "pbkdf2.h"
7 #include "hmacsha1.h"
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 int pbkdf2_sha1(const char *password, size_t Plen, const char *salt, size_t Slen,
13 unsigned int c, char *derivedKey, size_t dkLen) {
14 unsigned int hLen = 20;
15 char U[20], T[20];
16 unsigned int u, l, r, i, k;
17 char *tmp;
18
19 size_t tmplen = Slen + 4;
20
21 if (!c || !dkLen || dkLen > 4294967295U)
22 return 0;
23
24 l = ((dkLen - 1) / hLen) + 1;
25 r = dkLen - (l - 1) * hLen;
26
27 if (!(tmp = (char*)malloc(tmplen)))
28 return 0;
29
30 memcpy(tmp, salt, Slen);
31
32 for (i = 1; i <= l; i++) {
33 memset (T, 0, hLen);
34
35 for (u = 1; u <= c; u++) {
36 if (u == 1) {
37 tmp[Slen + 0] = (i & 0xff000000) >> 24;
38 tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
39 tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
40 tmp[Slen + 3] = (i & 0x000000ff) >> 0;
41
42 hmac_sha1 (password, Plen, tmp, tmplen, U);
43 } else
44 hmac_sha1 (password, Plen, U, hLen, U);
45
46 for (k = 0; k < hLen; k++)
47 T[k] ^= U[k];
48 }
49
50 memcpy(derivedKey + (i - 1) * hLen, T, i == l ? r : hLen);
51 }
52
53 free(tmp);
54
55 return 1;
56 }
Something went wrong with that request. Please try again.