3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include <mycrypt.h> |
|
12 |
|
13 /* PKCS #5, Algorithm #2 */ |
|
14 #ifdef PKCS_5 |
|
15 |
|
16 int pkcs_5_alg2(const unsigned char *password, unsigned long password_len, |
|
17 const unsigned char *salt, unsigned long salt_len, |
|
18 int iteration_count, int hash_idx, |
|
19 unsigned char *out, unsigned long *outlen) |
|
20 { |
|
21 int err, itts; |
|
22 unsigned long stored, left, x, y, blkno; |
143
|
23 unsigned char *buf[2]; |
|
24 hmac_state *hmac; |
3
|
25 |
|
26 _ARGCHK(password != NULL); |
|
27 _ARGCHK(salt != NULL); |
|
28 _ARGCHK(out != NULL); |
|
29 _ARGCHK(outlen != NULL); |
|
30 |
|
31 /* test hash IDX */ |
|
32 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
33 return err; |
|
34 } |
|
35 |
143
|
36 buf[0] = XMALLOC(MAXBLOCKSIZE * 2); |
|
37 hmac = XMALLOC(sizeof(hmac_state)); |
|
38 if (hmac == NULL || buf[0] == NULL) { |
|
39 if (hmac != NULL) { |
|
40 XFREE(hmac); |
|
41 } |
|
42 if (buf[0] != NULL) { |
|
43 XFREE(buf[0]); |
|
44 } |
|
45 return CRYPT_MEM; |
|
46 } |
|
47 /* buf[1] points to the second block of MAXBLOCKSIZE bytes */ |
|
48 buf[1] = buf[0] + MAXBLOCKSIZE; |
|
49 |
3
|
50 left = *outlen; |
|
51 blkno = 1; |
|
52 stored = 0; |
|
53 while (left != 0) { |
|
54 /* process block number blkno */ |
143
|
55 zeromem(buf[0], MAXBLOCKSIZE*2); |
3
|
56 |
|
57 /* store current block number and increment for next pass */ |
|
58 STORE32H(blkno, buf[1]); |
|
59 ++blkno; |
|
60 |
|
61 /* get PRF(P, S||int(blkno)) */ |
143
|
62 if ((err = hmac_init(hmac, hash_idx, password, password_len)) != CRYPT_OK) { |
|
63 goto __ERR; |
3
|
64 } |
143
|
65 if ((err = hmac_process(hmac, salt, salt_len)) != CRYPT_OK) { |
|
66 goto __ERR; |
3
|
67 } |
143
|
68 if ((err = hmac_process(hmac, buf[1], 4)) != CRYPT_OK) { |
|
69 goto __ERR; |
3
|
70 } |
143
|
71 x = MAXBLOCKSIZE; |
|
72 if ((err = hmac_done(hmac, buf[0], &x)) != CRYPT_OK) { |
|
73 goto __ERR; |
3
|
74 } |
|
75 |
|
76 /* now compute repeated and XOR it in buf[1] */ |
143
|
77 XMEMCPY(buf[1], buf[0], x); |
|
78 for (itts = 1; itts < iteration_count; ++itts) { |
3
|
79 if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) { |
143
|
80 goto __ERR; |
3
|
81 } |
|
82 for (y = 0; y < x; y++) { |
|
83 buf[1][y] ^= buf[0][y]; |
|
84 } |
|
85 } |
|
86 |
|
87 /* now emit upto x bytes of buf[1] to output */ |
|
88 for (y = 0; y < x && left != 0; ++y) { |
|
89 out[stored++] = buf[1][y]; |
|
90 --left; |
|
91 } |
|
92 } |
|
93 *outlen = stored; |
|
94 |
143
|
95 err = CRYPT_OK; |
|
96 __ERR: |
3
|
97 #ifdef CLEAN_STACK |
143
|
98 zeromem(buf[0], MAXBLOCKSIZE*2); |
|
99 zeromem(hmac, sizeof(hmac_state)); |
3
|
100 #endif |
143
|
101 |
|
102 XFREE(hmac); |
|
103 XFREE(buf[0]); |
|
104 |
|
105 return err; |
3
|
106 } |
|
107 |
|
108 #endif |
|
109 |