~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/headers/tomcrypt_hash.h

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 WHIRLPOOL
 
74
struct whirlpool_state {
 
75
    ulong64 length, state[8];
 
76
    unsigned char buf[64];
 
77
    ulong32 curlen;
 
78
};
 
79
#endif
 
80
 
 
81
#ifdef CHC_HASH
 
82
struct chc_state {
 
83
    ulong64 length;
 
84
    unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
 
85
    ulong32 curlen;
 
86
};
 
87
#endif
 
88
 
 
89
typedef union Hash_state {
 
90
#ifdef CHC_HASH
 
91
    struct chc_state chc;
 
92
#endif
 
93
#ifdef WHIRLPOOL
 
94
    struct whirlpool_state whirlpool;
 
95
#endif
 
96
#ifdef SHA512
 
97
    struct sha512_state sha512;
 
98
#endif
 
99
#ifdef SHA256
 
100
    struct sha256_state sha256;
 
101
#endif
 
102
#ifdef SHA1
 
103
    struct sha1_state   sha1;
 
104
#endif
 
105
#ifdef MD5
 
106
    struct md5_state    md5;
 
107
#endif
 
108
#ifdef MD4
 
109
    struct md4_state    md4;
 
110
#endif
 
111
#ifdef MD2
 
112
    struct md2_state    md2;
 
113
#endif
 
114
#ifdef TIGER
 
115
    struct tiger_state  tiger;
 
116
#endif
 
117
#ifdef RIPEMD128
 
118
    struct rmd128_state rmd128;
 
119
#endif
 
120
#ifdef RIPEMD160
 
121
    struct rmd160_state rmd160;
 
122
#endif
 
123
    void *data;
 
124
} hash_state;
 
125
 
 
126
extern  struct ltc_hash_descriptor {
 
127
    /** name of hash */
 
128
    char *name;
 
129
    /** internal ID */
 
130
    unsigned char ID;
 
131
    /** Size of digest in octets */
 
132
    unsigned long hashsize;
 
133
    /** Input block size in octets */
 
134
    unsigned long blocksize;
 
135
    /** ASN.1 OID */
 
136
    unsigned long OID[16];
 
137
    /** Length of DER encoding */
 
138
    unsigned long OIDlen;
 
139
 
 
140
    /** Init a hash state
 
141
      @param hash   The hash to initialize
 
142
      @return CRYPT_OK if successful
 
143
    */
 
144
    int (*init)(hash_state *hash);
 
145
    /** Process a block of data 
 
146
      @param hash   The hash state
 
147
      @param in     The data to hash
 
148
      @param inlen  The length of the data (octets)
 
149
      @return CRYPT_OK if successful
 
150
    */
 
151
    int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
 
152
    /** Produce the digest and store it
 
153
      @param hash   The hash state
 
154
      @param out    [out] The destination of the digest
 
155
      @return CRYPT_OK if successful
 
156
    */
 
157
    int (*done)(hash_state *hash, unsigned char *out);
 
158
    /** Self-test
 
159
      @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
 
160
    */
 
161
    int (*test)(void);
 
162
} hash_descriptor[];
 
163
 
 
164
#ifdef CHC_HASH
 
165
int chc_register(int cipher);
 
166
int chc_init(hash_state * md);
 
167
int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
168
int chc_done(hash_state * md, unsigned char *hash);
 
169
int chc_test(void);
 
170
extern const struct ltc_hash_descriptor chc_desc;
 
171
#endif
 
172
 
 
173
#ifdef WHIRLPOOL
 
174
int whirlpool_init(hash_state * md);
 
175
int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
176
int whirlpool_done(hash_state * md, unsigned char *hash);
 
177
int whirlpool_test(void);
 
178
extern const struct ltc_hash_descriptor whirlpool_desc;
 
179
#endif
 
180
 
 
181
#ifdef SHA512
 
182
int sha512_init(hash_state * md);
 
183
int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
184
int sha512_done(hash_state * md, unsigned char *hash);
 
185
int sha512_test(void);
 
186
extern const struct ltc_hash_descriptor sha512_desc;
 
187
#endif
 
188
 
 
189
#ifdef SHA384
 
190
#ifndef SHA512
 
191
   #error SHA512 is required for SHA384
 
192
#endif
 
193
int sha384_init(hash_state * md);
 
194
#define sha384_process sha512_process
 
195
int sha384_done(hash_state * md, unsigned char *hash);
 
196
int sha384_test(void);
 
197
extern const struct ltc_hash_descriptor sha384_desc;
 
198
#endif
 
199
 
 
200
#ifdef SHA256
 
201
int sha256_init(hash_state * md);
 
202
int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
203
int sha256_done(hash_state * md, unsigned char *hash);
 
204
int sha256_test(void);
 
205
extern const struct ltc_hash_descriptor sha256_desc;
 
206
 
 
207
#ifdef SHA224
 
208
#ifndef SHA256
 
209
   #error SHA256 is required for SHA224
 
210
#endif
 
211
int sha224_init(hash_state * md);
 
212
#define sha224_process sha256_process
 
213
int sha224_done(hash_state * md, unsigned char *hash);
 
214
int sha224_test(void);
 
215
extern const struct ltc_hash_descriptor sha224_desc;
 
216
#endif
 
217
#endif
 
218
 
 
219
#ifdef SHA1
 
220
int sha1_init(hash_state * md);
 
221
int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
222
int sha1_done(hash_state * md, unsigned char *hash);
 
223
int sha1_test(void);
 
224
extern const struct ltc_hash_descriptor sha1_desc;
 
225
#endif
 
226
 
 
227
#ifdef MD5
 
228
int md5_init(hash_state * md);
 
229
int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
230
int md5_done(hash_state * md, unsigned char *hash);
 
231
int md5_test(void);
 
232
extern const struct ltc_hash_descriptor md5_desc;
 
233
#endif
 
234
 
 
235
#ifdef MD4
 
236
int md4_init(hash_state * md);
 
237
int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
238
int md4_done(hash_state * md, unsigned char *hash);
 
239
int md4_test(void);
 
240
extern const struct ltc_hash_descriptor md4_desc;
 
241
#endif
 
242
 
 
243
#ifdef MD2
 
244
int md2_init(hash_state * md);
 
245
int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
246
int md2_done(hash_state * md, unsigned char *hash);
 
247
int md2_test(void);
 
248
extern const struct ltc_hash_descriptor md2_desc;
 
249
#endif
 
250
 
 
251
#ifdef TIGER
 
252
int tiger_init(hash_state * md);
 
253
int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
254
int tiger_done(hash_state * md, unsigned char *hash);
 
255
int tiger_test(void);
 
256
extern const struct ltc_hash_descriptor tiger_desc;
 
257
#endif
 
258
 
 
259
#ifdef RIPEMD128
 
260
int rmd128_init(hash_state * md);
 
261
int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
262
int rmd128_done(hash_state * md, unsigned char *hash);
 
263
int rmd128_test(void);
 
264
extern const struct ltc_hash_descriptor rmd128_desc;
 
265
#endif
 
266
 
 
267
#ifdef RIPEMD160
 
268
int rmd160_init(hash_state * md);
 
269
int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen);
 
270
int rmd160_done(hash_state * md, unsigned char *hash);
 
271
int rmd160_test(void);
 
272
extern const struct ltc_hash_descriptor rmd160_desc;
 
273
#endif
 
274
 
 
275
int find_hash(const char *name);
 
276
int find_hash_id(unsigned char ID);
 
277
int find_hash_any(const char *name, int digestlen);
 
278
int register_hash(const struct ltc_hash_descriptor *hash);
 
279
int unregister_hash(const struct ltc_hash_descriptor *hash);
 
280
int hash_is_valid(int idx);
 
281
 
 
282
LTC_MUTEX_PROTO(ltc_hash_mutex);
 
283
 
 
284
int hash_memory(int hash, 
 
285
                const unsigned char *in,  unsigned long inlen, 
 
286
                      unsigned char *out, unsigned long *outlen);
 
287
int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
 
288
                      const unsigned char *in, unsigned long inlen, ...);
 
289
int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
 
290
int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
 
291
 
 
292
/* a simple macro for making hash "process" functions */
 
293
#define HASH_PROCESS(func_name, compress_name, state_var, block_size)                       \
 
294
int func_name (hash_state * md, const unsigned char *in, unsigned long inlen)               \
 
295
{                                                                                           \
 
296
    unsigned long n;                                                                        \
 
297
    int           err;                                                                      \
 
298
    LTC_ARGCHK(md != NULL);                                                                 \
 
299
    LTC_ARGCHK(in != NULL);                                                                 \
 
300
    if (md-> state_var .curlen > sizeof(md-> state_var .buf)) {                             \
 
301
       return CRYPT_INVALID_ARG;                                                            \
 
302
    }                                                                                       \
 
303
    while (inlen > 0) {                                                                     \
 
304
        if (md-> state_var .curlen == 0 && inlen >= block_size) {                           \
 
305
           if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) {               \
 
306
              return err;                                                                   \
 
307
           }                                                                                \
 
308
           md-> state_var .length += block_size * 8;                                        \
 
309
           in             += block_size;                                                    \
 
310
           inlen          -= block_size;                                                    \
 
311
        } else {                                                                            \
 
312
           n = MIN(inlen, (block_size - md-> state_var .curlen));                           \
 
313
           memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n);              \
 
314
           md-> state_var .curlen += n;                                                     \
 
315
           in             += n;                                                             \
 
316
           inlen          -= n;                                                             \
 
317
           if (md-> state_var .curlen == block_size) {                                      \
 
318
              if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) {            \
 
319
                 return err;                                                                \
 
320
              }                                                                             \
 
321
              md-> state_var .length += 8*block_size;                                       \
 
322
              md-> state_var .curlen = 0;                                                   \
 
323
           }                                                                                \
 
324
       }                                                                                    \
 
325
    }                                                                                       \
 
326
    return CRYPT_OK;                                                                        \
 
327
}
 
328
 
 
329
/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_hash.h,v $ */
 
330
/* $Revision: 1.12 $ */
 
331
/* $Date: 2005/06/19 18:00:28 $ */