1 /* ---- HASH FUNCTIONS ---- */
2 #ifdef LTC_SHA512
3 struct sha512_state {
4     ulong64  length, state[8];
5     unsigned long curlen;
6     unsigned char buf[128];
7 };
8 #endif
9 
10 #ifdef LTC_SHA256
11 struct sha256_state {
12     ulong64 length;
13     ulong32 state[8], curlen;
14     unsigned char buf[64];
15 };
16 #endif
17 
18 #ifdef LTC_SHA1
19 struct sha1_state {
20     ulong64 length;
21     ulong32 state[5], curlen;
22     unsigned char buf[64];
23 };
24 #endif
25 
26 #ifdef LTC_MD5
27 struct md5_state {
28     ulong64 length;
29     ulong32 state[4], curlen;
30     unsigned char buf[64];
31 };
32 #endif
33 
34 #ifdef LTC_MD4
35 struct md4_state {
36     ulong64 length;
37     ulong32 state[4], curlen;
38     unsigned char buf[64];
39 };
40 #endif
41 
42 #ifdef LTC_TIGER
43 struct tiger_state {
44     ulong64 state[3], length;
45     unsigned long curlen;
46     unsigned char buf[64];
47 };
48 #endif
49 
50 #ifdef LTC_MD2
51 struct md2_state {
52     unsigned char chksum[16], X[48], buf[16];
53     unsigned long curlen;
54 };
55 #endif
56 
57 #ifdef LTC_RIPEMD128
58 struct rmd128_state {
59     ulong64 length;
60     unsigned char buf[64];
61     ulong32 curlen, state[4];
62 };
63 #endif
64 
65 #ifdef LTC_RIPEMD160
66 struct rmd160_state {
67     ulong64 length;
68     unsigned char buf[64];
69     ulong32 curlen, state[5];
70 };
71 #endif
72 
73 #ifdef LTC_RIPEMD256
74 struct rmd256_state {
75     ulong64 length;
76     unsigned char buf[64];
77     ulong32 curlen, state[8];
78 };
79 #endif
80 
81 #ifdef LTC_RIPEMD320
82 struct rmd320_state {
83     ulong64 length;
84     unsigned char buf[64];
85     ulong32 curlen, state[10];
86 };
87 #endif
88 
89 #ifdef LTC_WHIRLPOOL
90 struct whirlpool_state {
91     ulong64 length, state[8];
92     unsigned char buf[64];
93     ulong32 curlen;
94 };
95 #endif
96 
97 #ifdef LTC_CHC_HASH
98 struct chc_state {
99     ulong64 length;
100     unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
101     ulong32 curlen;
102 };
103 #endif
104 
105 typedef union Hash_state {
106 #ifdef LTC_CHC_HASH
107     struct chc_state chc;
108 #endif
109 #ifdef LTC_WHIRLPOOL
110     struct whirlpool_state whirlpool;
111 #endif
112 #ifdef LTC_SHA512
113     struct sha512_state sha512;
114 #endif
115 #ifdef LTC_SHA256
116     struct sha256_state sha256;
117 #endif
118 #ifdef LTC_SHA1
119     struct sha1_state   sha1;
120 #endif
121 #ifdef LTC_MD5
122     struct md5_state    md5;
123 #endif
124 #ifdef LTC_MD4
125     struct md4_state    md4;
126 #endif
127 #ifdef LTC_MD2
128     struct md2_state    md2;
129 #endif
130 #ifdef LTC_TIGER
131     struct tiger_state  tiger;
132 #endif
133 #ifdef LTC_RIPEMD128
134     struct rmd128_state rmd128;
135 #endif
136 #ifdef LTC_RIPEMD160
137     struct rmd160_state rmd160;
138 #endif
139 #ifdef LTC_RIPEMD256
140     struct rmd256_state rmd256;
141 #endif
142 #ifdef LTC_RIPEMD320
143     struct rmd320_state rmd320;
144 #endif
145     void *data;
146 } hash_state;
147 
148 /** hash descriptor */
149 extern  struct ltc_hash_descriptor {
150     /** name of hash */
151     char *name;
152     /** internal ID */
153     unsigned char ID;
154     /** Size of digest in octets */
155     unsigned long hashsize;
156     /** Input block size in octets */
157     unsigned long blocksize;
158     /** ASN.1 OID */
159     unsigned long OID[16];
160     /** Length of DER encoding */
161     unsigned long OIDlen;
162 
163     /** Init a hash state
164       @param hash   The hash to initialize
165       @return CRYPT_OK if successful
166     */
167     int (*init)(hash_state *hash);
168     /** Process a block of data
169       @param hash   The hash state
170       @param in     The data to hash
171       @param inlen  The length of the data (octets)
172       @return CRYPT_OK if successful
173     */
174     int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
175     /** Produce the digest and store it
176       @param hash   The hash state
177       @param out    [out] The destination of the digest
178       @return CRYPT_OK if successful
179     */
180     int (*done)(hash_state *hash, unsigned char *out);
181     /** Self-test
182       @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
183     */
184     int (*test)(void);
185 
186     /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
187     int  (*hmac_block)(const unsigned char *key, unsigned long  keylen,
188                        const unsigned char *in,  unsigned long  inlen,
189                              unsigned char *out, unsigned long *outlen);
190 
191 } hash_descriptor[];
192 
193 #ifdef LTC_CHC_HASH
194 int chc_register(int cipher);
195 int chc_init(hash_state * md);
196 int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
197 int chc_done(hash_state * md, unsigned char *hash);
198 int chc_test(void);
199 extern const struct ltc_hash_descriptor chc_desc;
200 #endif
201 
202 #ifdef LTC_WHIRLPOOL
203 int whirlpool_init(hash_state * md);
204 int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen);
205 int whirlpool_done(hash_state * md, unsigned char *hash);
206 int whirlpool_test(void);
207 extern const struct ltc_hash_descriptor whirlpool_desc;
208 #endif
209 
210 #ifdef LTC_SHA512
211 int sha512_init(hash_state * md);
212 int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen);
213 int sha512_done(hash_state * md, unsigned char *hash);
214 int sha512_test(void);
215 extern const struct ltc_hash_descriptor sha512_desc;
216 #endif
217 
218 #ifdef LTC_SHA384
219 #ifndef LTC_SHA512
220    #error LTC_SHA512 is required for LTC_SHA384
221 #endif
222 int sha384_init(hash_state * md);
223 #define sha384_process sha512_process
224 int sha384_done(hash_state * md, unsigned char *hash);
225 int sha384_test(void);
226 extern const struct ltc_hash_descriptor sha384_desc;
227 #endif
228 
229 #ifdef LTC_SHA256
230 int sha256_init(hash_state * md);
231 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
232 int sha256_done(hash_state * md, unsigned char *hash);
233 int sha256_test(void);
234 extern const struct ltc_hash_descriptor sha256_desc;
235 
236 #ifdef LTC_SHA224
237 #ifndef LTC_SHA256
238    #error LTC_SHA256 is required for LTC_SHA224
239 #endif
240 int sha224_init(hash_state * md);
241 #define sha224_process sha256_process
242 int sha224_done(hash_state * md, unsigned char *hash);
243 int sha224_test(void);
244 extern const struct ltc_hash_descriptor sha224_desc;
245 #endif
246 #endif
247 
248 #ifdef LTC_SHA1
249 int sha1_init(hash_state * md);
250 int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen);
251 int sha1_done(hash_state * md, unsigned char *hash);
252 int sha1_test(void);
253 extern const struct ltc_hash_descriptor sha1_desc;
254 #endif
255 
256 #ifdef LTC_MD5
257 int md5_init(hash_state * md);
258 int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen);
259 int md5_done(hash_state * md, unsigned char *hash);
260 int md5_test(void);
261 extern const struct ltc_hash_descriptor md5_desc;
262 #endif
263 
264 #ifdef LTC_MD4
265 int md4_init(hash_state * md);
266 int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen);
267 int md4_done(hash_state * md, unsigned char *hash);
268 int md4_test(void);
269 extern const struct ltc_hash_descriptor md4_desc;
270 #endif
271 
272 #ifdef LTC_MD2
273 int md2_init(hash_state * md);
274 int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen);
275 int md2_done(hash_state * md, unsigned char *hash);
276 int md2_test(void);
277 extern const struct ltc_hash_descriptor md2_desc;
278 #endif
279 
280 #ifdef LTC_TIGER
281 int tiger_init(hash_state * md);
282 int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen);
283 int tiger_done(hash_state * md, unsigned char *hash);
284 int tiger_test(void);
285 extern const struct ltc_hash_descriptor tiger_desc;
286 #endif
287 
288 #ifdef LTC_RIPEMD128
289 int rmd128_init(hash_state * md);
290 int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen);
291 int rmd128_done(hash_state * md, unsigned char *hash);
292 int rmd128_test(void);
293 extern const struct ltc_hash_descriptor rmd128_desc;
294 #endif
295 
296 #ifdef LTC_RIPEMD160
297 int rmd160_init(hash_state * md);
298 int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen);
299 int rmd160_done(hash_state * md, unsigned char *hash);
300 int rmd160_test(void);
301 extern const struct ltc_hash_descriptor rmd160_desc;
302 #endif
303 
304 #ifdef LTC_RIPEMD256
305 int rmd256_init(hash_state * md);
306 int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
307 int rmd256_done(hash_state * md, unsigned char *hash);
308 int rmd256_test(void);
309 extern const struct ltc_hash_descriptor rmd256_desc;
310 #endif
311 
312 #ifdef LTC_RIPEMD320
313 int rmd320_init(hash_state * md);
314 int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen);
315 int rmd320_done(hash_state * md, unsigned char *hash);
316 int rmd320_test(void);
317 extern const struct ltc_hash_descriptor rmd320_desc;
318 #endif
319 
320 
321 int find_hash(const char *name);
322 int find_hash_id(unsigned char ID);
323 int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
324 int find_hash_any(const char *name, int digestlen);
325 int register_hash(const struct ltc_hash_descriptor *hash);
326 int unregister_hash(const struct ltc_hash_descriptor *hash);
327 int hash_is_valid(int idx);
328 
329 LTC_MUTEX_PROTO(ltc_hash_mutex)
330 
331 int hash_memory(int hash,
332                 const unsigned char *in,  unsigned long inlen,
333                       unsigned char *out, unsigned long *outlen);
334 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
335                       const unsigned char *in, unsigned long inlen, ...);
336 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
337 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
338 
339 /* a simple macro for making hash "process" functions */
340 #define HASH_PROCESS(func_name, compress_name, state_var, block_size)                       \
341 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen)               \
342 {                                                                                           \
343     unsigned long n;                                                                        \
344     int           err;                                                                      \
345     LTC_ARGCHK(md != NULL);                                                                 \
346     LTC_ARGCHK(in != NULL);                                                                 \
347     if (md-> state_var .curlen > sizeof(md-> state_var .buf)) {                             \
348        return CRYPT_INVALID_ARG;                                                            \
349     }                                                                                       \
350     while (inlen > 0) {                                                                     \
351         if (md-> state_var .curlen == 0 && inlen >= block_size) {                           \
352            if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) {               \
353               return err;                                                                   \
354            }                                                                                \
355            md-> state_var .length += block_size * 8;                                        \
356            in             += block_size;                                                    \
357            inlen          -= block_size;                                                    \
358         } else {                                                                            \
359            n = MIN(inlen, (block_size - md-> state_var .curlen));                           \
360            memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n);              \
361            md-> state_var .curlen += n;                                                     \
362            in             += n;                                                             \
363            inlen          -= n;                                                             \
364            if (md-> state_var .curlen == block_size) {                                      \
365               if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) {            \
366                  return err;                                                                \
367               }                                                                             \
368               md-> state_var .length += 8*block_size;                                       \
369               md-> state_var .curlen = 0;                                                   \
370            }                                                                                \
371        }                                                                                    \
372     }                                                                                       \
373     return CRYPT_OK;                                                                        \
374 }
375 
376 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_hash.h,v $ */
377 /* $Revision: 1.22 $ */
378 /* $Date: 2007/05/12 14:32:35 $ */
379