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 #1 */ |
|
14 #ifdef PKCS_5 |
|
15 |
|
16 int pkcs_5_alg1(const unsigned char *password, unsigned long password_len, |
|
17 const unsigned char *salt, |
|
18 int iteration_count, int hash_idx, |
|
19 unsigned char *out, unsigned long *outlen) |
|
20 { |
|
21 int err; |
|
22 unsigned long x; |
|
23 hash_state md; |
|
24 unsigned char buf[MAXBLOCKSIZE]; |
|
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 |
|
36 /* hash initial password + salt */ |
|
37 hash_descriptor[hash_idx].init(&md); |
|
38 hash_descriptor[hash_idx].process(&md, password, password_len); |
|
39 hash_descriptor[hash_idx].process(&md, salt, 8); |
|
40 hash_descriptor[hash_idx].done(&md, buf); |
|
41 |
|
42 while (--iteration_count) { |
|
43 // code goes here. |
|
44 x = sizeof(buf); |
|
45 if ((err = hash_memory(hash_idx, buf, hash_descriptor[hash_idx].hashsize, buf, &x)) != CRYPT_OK) { |
|
46 return err; |
|
47 } |
|
48 } |
|
49 |
|
50 /* copy upto outlen bytes */ |
|
51 for (x = 0; x < hash_descriptor[hash_idx].hashsize && x < *outlen; x++) { |
|
52 out[x] = buf[x]; |
|
53 } |
|
54 *outlen = x; |
|
55 |
|
56 #ifdef CLEAN_STACK |
|
57 zeromem(buf, sizeof(buf)); |
|
58 #endif |
|
59 |
|
60 return CRYPT_OK; |
|
61 } |
|
62 |
|
63 #endif |