| 1 |
|
| 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 |
char dummy[1]; |
| 107 |
#ifdef LTC_CHC_HASH |
| 108 |
struct chc_state chc; |
| 109 |
#endif |
| 110 |
#ifdef LTC_WHIRLPOOL |
| 111 |
struct whirlpool_state whirlpool; |
| 112 |
#endif |
| 113 |
#ifdef LTC_SHA512 |
| 114 |
struct sha512_state sha512; |
| 115 |
#endif |
| 116 |
#ifdef LTC_SHA256 |
| 117 |
struct sha256_state sha256; |
| 118 |
#endif |
| 119 |
#ifdef LTC_SHA1 |
| 120 |
struct sha1_state sha1; |
| 121 |
#endif |
| 122 |
#ifdef LTC_MD5 |
| 123 |
struct md5_state md5; |
| 124 |
#endif |
| 125 |
#ifdef LTC_MD4 |
| 126 |
struct md4_state md4; |
| 127 |
#endif |
| 128 |
#ifdef LTC_MD2 |
| 129 |
struct md2_state md2; |
| 130 |
#endif |
| 131 |
#ifdef LTC_TIGER |
| 132 |
struct tiger_state tiger; |
| 133 |
#endif |
| 134 |
#ifdef LTC_RIPEMD128 |
| 135 |
struct rmd128_state rmd128; |
| 136 |
#endif |
| 137 |
#ifdef LTC_RIPEMD160 |
| 138 |
struct rmd160_state rmd160; |
| 139 |
#endif |
| 140 |
#ifdef LTC_RIPEMD256 |
| 141 |
struct rmd256_state rmd256; |
| 142 |
#endif |
| 143 |
#ifdef LTC_RIPEMD320 |
| 144 |
struct rmd320_state rmd320; |
| 145 |
#endif |
| 146 |
void *data; |
| 147 |
} hash_state; |
| 148 |
|
| 149 |
|
| 150 |
extern struct ltc_hash_descriptor { |
| 151 |
|
| 152 |
char *name; |
| 153 |
|
| 154 |
unsigned char ID; |
| 155 |
|
| 156 |
unsigned long hashsize; |
| 157 |
|
| 158 |
unsigned long blocksize; |
| 159 |
|
| 160 |
unsigned long OID[16]; |
| 161 |
|
| 162 |
unsigned long OIDlen; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
int (*init)(hash_state *hash); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
int (*done)(hash_state *hash, unsigned char *out); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
int (*test)(void); |
| 186 |
|
| 187 |
|
| 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 LTC_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 LTC_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 LTC_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 LTC_SHA384 |
| 220 |
#ifndef LTC_SHA512 |
| 221 |
#error LTC_SHA512 is required for LTC_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 LTC_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 LTC_SHA224 |
| 238 |
#ifndef LTC_SHA256 |
| 239 |
#error LTC_SHA256 is required for LTC_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 LTC_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 LTC_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 LTC_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 LTC_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 LTC_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 LTC_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 LTC_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 LTC_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 LTC_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 |
|
| 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 |
|
| 378 |
|
| 379 |
|