Mercurial > dropbear
comparison pkcs_1_mgf1.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 hash_idx, | 18 int hash_idx, |
19 unsigned char *mask, unsigned long masklen) | 19 unsigned char *mask, unsigned long masklen) |
20 { | 20 { |
21 unsigned long hLen, counter, x; | 21 unsigned long hLen, counter, x; |
22 int err; | 22 int err; |
23 hash_state md; | 23 hash_state *md; |
24 unsigned char buf[MAXBLOCKSIZE]; | 24 unsigned char *buf; |
25 | 25 |
26 _ARGCHK(seed != NULL); | 26 _ARGCHK(seed != NULL); |
27 _ARGCHK(mask != NULL); | 27 _ARGCHK(mask != NULL); |
28 | 28 |
29 /* ensure valid hash */ | 29 /* ensure valid hash */ |
32 } | 32 } |
33 | 33 |
34 /* get hash output size */ | 34 /* get hash output size */ |
35 hLen = hash_descriptor[hash_idx].hashsize; | 35 hLen = hash_descriptor[hash_idx].hashsize; |
36 | 36 |
37 /* allocate memory */ | |
38 md = XMALLOC(sizeof(hash_state)); | |
39 buf = XMALLOC(hLen); | |
40 if (md == NULL || buf == NULL) { | |
41 if (md != NULL) { | |
42 XFREE(md); | |
43 } | |
44 if (buf != NULL) { | |
45 XFREE(buf); | |
46 } | |
47 return CRYPT_MEM; | |
48 } | |
49 | |
37 /* start counter */ | 50 /* start counter */ |
38 counter = 0; | 51 counter = 0; |
39 | 52 |
40 while (masklen > 0) { | 53 while (masklen > 0) { |
41 /* handle counter */ | 54 /* handle counter */ |
42 STORE32H(counter, buf); | 55 STORE32H(counter, buf); |
43 ++counter; | 56 ++counter; |
44 | 57 |
45 /* get hash of seed || counter */ | 58 /* get hash of seed || counter */ |
46 hash_descriptor[hash_idx].init(&md); | 59 if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) { |
47 if ((err = hash_descriptor[hash_idx].process(&md, seed, seedlen)) != CRYPT_OK) { | 60 goto __ERR; |
48 return err; | |
49 } | 61 } |
50 if ((err = hash_descriptor[hash_idx].process(&md, buf, 4)) != CRYPT_OK) { | 62 if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) { |
51 return err; | 63 goto __ERR; |
52 } | 64 } |
53 if ((err = hash_descriptor[hash_idx].done(&md, buf)) != CRYPT_OK) { | 65 if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) { |
54 return err; | 66 goto __ERR; |
67 } | |
68 if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) { | |
69 goto __ERR; | |
55 } | 70 } |
56 | 71 |
57 /* store it */ | 72 /* store it */ |
58 for (x = 0; x < hLen && masklen > 0; x++, masklen--) { | 73 for (x = 0; x < hLen && masklen > 0; x++, masklen--) { |
59 *mask++ = buf[x]; | 74 *mask++ = buf[x]; |
60 } | 75 } |
61 } | 76 } |
62 | 77 |
63 return CRYPT_OK; | 78 err = CRYPT_OK; |
79 __ERR: | |
80 #ifdef CLEAN_STACK | |
81 zeromem(buf, hLen); | |
82 zeromem(md, sizeof(hash_state)); | |
83 #endif | |
84 | |
85 XFREE(buf); | |
86 XFREE(md); | |
87 | |
88 return err; | |
64 } | 89 } |
65 | 90 |
66 #endif /* PKCS_1 */ | 91 #endif /* PKCS_1 */ |