comparison pkcs_5_2.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 5d99163f7e32
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
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;
23 unsigned char buf[2][MAXBLOCKSIZE];
24 hmac_state hmac;
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 left = *outlen;
37 blkno = 1;
38 stored = 0;
39 while (left != 0) {
40 /* process block number blkno */
41 zeromem(buf, sizeof(buf));
42
43 /* store current block number and increment for next pass */
44 STORE32H(blkno, buf[1]);
45 ++blkno;
46
47 /* get PRF(P, S||int(blkno)) */
48 if ((err = hmac_init(&hmac, hash_idx, password, password_len)) != CRYPT_OK) {
49 return err;
50 }
51 if ((err = hmac_process(&hmac, salt, salt_len)) != CRYPT_OK) {
52 return err;
53 }
54 if ((err = hmac_process(&hmac, buf[1], 4)) != CRYPT_OK) {
55 return err;
56 }
57 x = sizeof(buf[0]);
58 if ((err = hmac_done(&hmac, buf[0], &x)) != CRYPT_OK) {
59 return err;
60 }
61
62 /* now compute repeated and XOR it in buf[1] */
63 memcpy(buf[1], buf[0], x);
64 for (itts = 2; itts < iteration_count; ++itts) {
65 if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) {
66 return err;
67 }
68 for (y = 0; y < x; y++) {
69 buf[1][y] ^= buf[0][y];
70 }
71 }
72
73 /* now emit upto x bytes of buf[1] to output */
74 for (y = 0; y < x && left != 0; ++y) {
75 out[stored++] = buf[1][y];
76 --left;
77 }
78 }
79 *outlen = stored;
80
81 #ifdef CLEAN_STACK
82 zeromem(buf, sizeof(buf));
83 #endif
84 return CRYPT_OK;
85 }
86
87 #endif
88