Mercurial > dropbear
comparison pkcs_5_2.c @ 143:5d99163f7e32 libtomcrypt-orig
import of libtomcrypt 0.99
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 19 Dec 2004 11:34:45 +0000 |
parents | 7faae8f46238 |
children |
comparison
equal
deleted
inserted
replaced
15:6362d3854bb4 | 143:5d99163f7e32 |
---|---|
18 int iteration_count, int hash_idx, | 18 int iteration_count, int hash_idx, |
19 unsigned char *out, unsigned long *outlen) | 19 unsigned char *out, unsigned long *outlen) |
20 { | 20 { |
21 int err, itts; | 21 int err, itts; |
22 unsigned long stored, left, x, y, blkno; | 22 unsigned long stored, left, x, y, blkno; |
23 unsigned char buf[2][MAXBLOCKSIZE]; | 23 unsigned char *buf[2]; |
24 hmac_state hmac; | 24 hmac_state *hmac; |
25 | 25 |
26 _ARGCHK(password != NULL); | 26 _ARGCHK(password != NULL); |
27 _ARGCHK(salt != NULL); | 27 _ARGCHK(salt != NULL); |
28 _ARGCHK(out != NULL); | 28 _ARGCHK(out != NULL); |
29 _ARGCHK(outlen != NULL); | 29 _ARGCHK(outlen != NULL); |
31 /* test hash IDX */ | 31 /* test hash IDX */ |
32 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { | 32 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
33 return err; | 33 return err; |
34 } | 34 } |
35 | 35 |
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 | |
36 left = *outlen; | 50 left = *outlen; |
37 blkno = 1; | 51 blkno = 1; |
38 stored = 0; | 52 stored = 0; |
39 while (left != 0) { | 53 while (left != 0) { |
40 /* process block number blkno */ | 54 /* process block number blkno */ |
41 zeromem(buf, sizeof(buf)); | 55 zeromem(buf[0], MAXBLOCKSIZE*2); |
42 | 56 |
43 /* store current block number and increment for next pass */ | 57 /* store current block number and increment for next pass */ |
44 STORE32H(blkno, buf[1]); | 58 STORE32H(blkno, buf[1]); |
45 ++blkno; | 59 ++blkno; |
46 | 60 |
47 /* get PRF(P, S||int(blkno)) */ | 61 /* get PRF(P, S||int(blkno)) */ |
48 if ((err = hmac_init(&hmac, hash_idx, password, password_len)) != CRYPT_OK) { | 62 if ((err = hmac_init(hmac, hash_idx, password, password_len)) != CRYPT_OK) { |
49 return err; | 63 goto __ERR; |
50 } | 64 } |
51 if ((err = hmac_process(&hmac, salt, salt_len)) != CRYPT_OK) { | 65 if ((err = hmac_process(hmac, salt, salt_len)) != CRYPT_OK) { |
52 return err; | 66 goto __ERR; |
53 } | 67 } |
54 if ((err = hmac_process(&hmac, buf[1], 4)) != CRYPT_OK) { | 68 if ((err = hmac_process(hmac, buf[1], 4)) != CRYPT_OK) { |
55 return err; | 69 goto __ERR; |
56 } | 70 } |
57 x = sizeof(buf[0]); | 71 x = MAXBLOCKSIZE; |
58 if ((err = hmac_done(&hmac, buf[0], &x)) != CRYPT_OK) { | 72 if ((err = hmac_done(hmac, buf[0], &x)) != CRYPT_OK) { |
59 return err; | 73 goto __ERR; |
60 } | 74 } |
61 | 75 |
62 /* now compute repeated and XOR it in buf[1] */ | 76 /* now compute repeated and XOR it in buf[1] */ |
63 memcpy(buf[1], buf[0], x); | 77 XMEMCPY(buf[1], buf[0], x); |
64 for (itts = 2; itts < iteration_count; ++itts) { | 78 for (itts = 1; itts < iteration_count; ++itts) { |
65 if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) { | 79 if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) { |
66 return err; | 80 goto __ERR; |
67 } | 81 } |
68 for (y = 0; y < x; y++) { | 82 for (y = 0; y < x; y++) { |
69 buf[1][y] ^= buf[0][y]; | 83 buf[1][y] ^= buf[0][y]; |
70 } | 84 } |
71 } | 85 } |
76 --left; | 90 --left; |
77 } | 91 } |
78 } | 92 } |
79 *outlen = stored; | 93 *outlen = stored; |
80 | 94 |
95 err = CRYPT_OK; | |
96 __ERR: | |
81 #ifdef CLEAN_STACK | 97 #ifdef CLEAN_STACK |
82 zeromem(buf, sizeof(buf)); | 98 zeromem(buf[0], MAXBLOCKSIZE*2); |
99 zeromem(hmac, sizeof(hmac_state)); | |
83 #endif | 100 #endif |
84 return CRYPT_OK; | 101 |
102 XFREE(hmac); | |
103 XFREE(buf[0]); | |
104 | |
105 return err; | |
85 } | 106 } |
86 | 107 |
87 #endif | 108 #endif |
88 | 109 |