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