1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */ 2 /* SPDX-License-Identifier: Unlicense */ 3 4 /* ---- HASH FUNCTIONS ---- */ 5 #if defined(LTC_SHA3) || defined(LTC_KECCAK) 6 struct sha3_state { 7 ulong64 saved; /* the portion of the input message that we didn't consume yet */ 8 ulong64 s[25]; 9 unsigned char sb[25 * 8]; /* used for storing `ulong64 s[25]` as little-endian bytes */ 10 unsigned short byte_index; /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */ 11 unsigned short word_index; /* 0..24--the next word to integrate input (starts from 0) */ 12 unsigned short capacity_words; /* the double size of the hash output in words (e.g. 16 for Keccak 512) */ 13 unsigned short xof_flag; 14 }; 15 #endif 16 17 #ifdef LTC_SHA512 18 struct sha512_state { 19 ulong64 length, state[8]; 20 unsigned long curlen; 21 unsigned char buf[128]; 22 }; 23 #endif 24 25 #ifdef LTC_SHA256 26 struct sha256_state { 27 ulong64 length; 28 ulong32 state[8], curlen; 29 unsigned char buf[64]; 30 }; 31 #endif 32 33 #ifdef LTC_SHA1 34 struct sha1_state { 35 ulong64 length; 36 ulong32 state[5], curlen; 37 unsigned char buf[64]; 38 }; 39 #endif 40 41 #ifdef LTC_MD5 42 struct md5_state { 43 ulong64 length; 44 ulong32 state[4], curlen; 45 unsigned char buf[64]; 46 }; 47 #endif 48 49 #ifdef LTC_MD4 50 struct md4_state { 51 ulong64 length; 52 ulong32 state[4], curlen; 53 unsigned char buf[64]; 54 }; 55 #endif 56 57 #ifdef LTC_TIGER 58 struct tiger_state { 59 ulong64 state[3], length; 60 unsigned long curlen; 61 unsigned char buf[64]; 62 }; 63 #endif 64 65 #ifdef LTC_MD2 66 struct md2_state { 67 unsigned char chksum[16], X[48], buf[16]; 68 unsigned long curlen; 69 }; 70 #endif 71 72 #ifdef LTC_RIPEMD128 73 struct rmd128_state { 74 ulong64 length; 75 unsigned char buf[64]; 76 ulong32 curlen, state[4]; 77 }; 78 #endif 79 80 #ifdef LTC_RIPEMD160 81 struct rmd160_state { 82 ulong64 length; 83 unsigned char buf[64]; 84 ulong32 curlen, state[5]; 85 }; 86 #endif 87 88 #ifdef LTC_RIPEMD256 89 struct rmd256_state { 90 ulong64 length; 91 unsigned char buf[64]; 92 ulong32 curlen, state[8]; 93 }; 94 #endif 95 96 #ifdef LTC_RIPEMD320 97 struct rmd320_state { 98 ulong64 length; 99 unsigned char buf[64]; 100 ulong32 curlen, state[10]; 101 }; 102 #endif 103 104 #ifdef LTC_WHIRLPOOL 105 struct whirlpool_state { 106 ulong64 length, state[8]; 107 unsigned char buf[64]; 108 ulong32 curlen; 109 }; 110 #endif 111 112 #ifdef LTC_CHC_HASH 113 struct chc_state { 114 ulong64 length; 115 unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE]; 116 ulong32 curlen; 117 }; 118 #endif 119 120 #ifdef LTC_BLAKE2S 121 struct blake2s_state { 122 ulong32 h[8]; 123 ulong32 t[2]; 124 ulong32 f[2]; 125 unsigned char buf[64]; 126 unsigned long curlen; 127 unsigned long outlen; 128 unsigned char last_node; 129 }; 130 #endif 131 132 #ifdef LTC_BLAKE2B 133 struct blake2b_state { 134 ulong64 h[8]; 135 ulong64 t[2]; 136 ulong64 f[2]; 137 unsigned char buf[128]; 138 unsigned long curlen; 139 unsigned long outlen; 140 unsigned char last_node; 141 }; 142 #endif 143 144 typedef union Hash_state { 145 char dummy[1]; 146 #ifdef LTC_CHC_HASH 147 struct chc_state chc; 148 #endif 149 #ifdef LTC_WHIRLPOOL 150 struct whirlpool_state whirlpool; 151 #endif 152 #if defined(LTC_SHA3) || defined(LTC_KECCAK) 153 struct sha3_state sha3; 154 #endif 155 #ifdef LTC_SHA512 156 struct sha512_state sha512; 157 #endif 158 #ifdef LTC_SHA256 159 struct sha256_state sha256; 160 #endif 161 #ifdef LTC_SHA1 162 struct sha1_state sha1; 163 #endif 164 #ifdef LTC_MD5 165 struct md5_state md5; 166 #endif 167 #ifdef LTC_MD4 168 struct md4_state md4; 169 #endif 170 #ifdef LTC_MD2 171 struct md2_state md2; 172 #endif 173 #ifdef LTC_TIGER 174 struct tiger_state tiger; 175 #endif 176 #ifdef LTC_RIPEMD128 177 struct rmd128_state rmd128; 178 #endif 179 #ifdef LTC_RIPEMD160 180 struct rmd160_state rmd160; 181 #endif 182 #ifdef LTC_RIPEMD256 183 struct rmd256_state rmd256; 184 #endif 185 #ifdef LTC_RIPEMD320 186 struct rmd320_state rmd320; 187 #endif 188 #ifdef LTC_BLAKE2S 189 struct blake2s_state blake2s; 190 #endif 191 #ifdef LTC_BLAKE2B 192 struct blake2b_state blake2b; 193 #endif 194 195 void *data; 196 } hash_state; 197 198 /** hash descriptor */ 199 extern const struct ltc_hash_descriptor { 200 /** name of hash */ 201 const char *name; 202 /** internal ID */ 203 unsigned char ID; 204 /** Size of digest in octets */ 205 unsigned long hashsize; 206 /** Input block size in octets */ 207 unsigned long blocksize; 208 /** ASN.1 OID */ 209 unsigned long OID[16]; 210 /** Length of DER encoding */ 211 unsigned long OIDlen; 212 213 /** Init a hash state 214 @param hash The hash to initialize 215 @return CRYPT_OK if successful 216 */ 217 int (*init)(hash_state *hash); 218 /** Process a block of data 219 @param hash The hash state 220 @param in The data to hash 221 @param inlen The length of the data (octets) 222 @return CRYPT_OK if successful 223 */ 224 int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen); 225 /** Produce the digest and store it 226 @param hash The hash state 227 @param out [out] The destination of the digest 228 @return CRYPT_OK if successful 229 */ 230 int (*done)(hash_state *hash, unsigned char *out); 231 /** Self-test 232 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 233 */ 234 int (*test)(void); 235 236 /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */ 237 int (*hmac_block)(const unsigned char *key, unsigned long keylen, 238 const unsigned char *in, unsigned long inlen, 239 unsigned char *out, unsigned long *outlen); 240 241 } *hash_descriptor[]; 242 243 #ifdef LTC_CHC_HASH 244 int chc_register(int cipher); 245 int chc_init(hash_state * md); 246 int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen); 247 int chc_done(hash_state * md, unsigned char *out); 248 int chc_test(void); 249 extern const struct ltc_hash_descriptor chc_desc; 250 #endif 251 252 #ifdef LTC_WHIRLPOOL 253 int whirlpool_init(hash_state * md); 254 int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen); 255 int whirlpool_done(hash_state * md, unsigned char *out); 256 int whirlpool_test(void); 257 extern const struct ltc_hash_descriptor whirlpool_desc; 258 #endif 259 260 #if defined(LTC_SHA3) || defined(LTC_KECCAK) 261 /* sha3_NNN_init are shared by SHA3 and KECCAK */ 262 int sha3_512_init(hash_state * md); 263 int sha3_384_init(hash_state * md); 264 int sha3_256_init(hash_state * md); 265 int sha3_224_init(hash_state * md); 266 /* sha3_process is the same for all variants of SHA3 + KECCAK */ 267 int sha3_process(hash_state * md, const unsigned char *in, unsigned long inlen); 268 #endif 269 270 #ifdef LTC_SHA3 271 int sha3_512_test(void); 272 extern const struct ltc_hash_descriptor sha3_512_desc; 273 int sha3_384_test(void); 274 extern const struct ltc_hash_descriptor sha3_384_desc; 275 int sha3_256_test(void); 276 extern const struct ltc_hash_descriptor sha3_256_desc; 277 int sha3_224_test(void); 278 extern const struct ltc_hash_descriptor sha3_224_desc; 279 int sha3_done(hash_state *md, unsigned char *out); 280 /* SHAKE128 + SHAKE256 */ 281 int sha3_shake_init(hash_state *md, int num); 282 #define sha3_shake_process(a,b,c) sha3_process(a,b,c) 283 int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen); 284 int sha3_shake_test(void); 285 int sha3_shake_memory(int num, const unsigned char *in, unsigned long inlen, unsigned char *out, const unsigned long *outlen); 286 #endif 287 288 #ifdef LTC_KECCAK 289 #define keccak_512_init(a) sha3_512_init(a) 290 #define keccak_384_init(a) sha3_384_init(a) 291 #define keccak_256_init(a) sha3_256_init(a) 292 #define keccak_224_init(a) sha3_224_init(a) 293 #define keccak_process(a,b,c) sha3_process(a,b,c) 294 extern const struct ltc_hash_descriptor keccak_512_desc; 295 int keccak_512_test(void); 296 extern const struct ltc_hash_descriptor keccak_384_desc; 297 int keccak_384_test(void); 298 extern const struct ltc_hash_descriptor keccak_256_desc; 299 int keccak_256_test(void); 300 extern const struct ltc_hash_descriptor keccak_224_desc; 301 int keccak_224_test(void); 302 int keccak_done(hash_state *md, unsigned char *out); 303 #endif 304 305 #ifdef LTC_SHA512 306 int sha512_init(hash_state * md); 307 int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen); 308 int sha512_done(hash_state * md, unsigned char *out); 309 int sha512_test(void); 310 extern const struct ltc_hash_descriptor sha512_desc; 311 #endif 312 313 #ifdef LTC_SHA384 314 #ifndef LTC_SHA512 315 #error LTC_SHA512 is required for LTC_SHA384 316 #endif 317 int sha384_init(hash_state * md); 318 #define sha384_process sha512_process 319 int sha384_done(hash_state * md, unsigned char *out); 320 int sha384_test(void); 321 extern const struct ltc_hash_descriptor sha384_desc; 322 #endif 323 324 #ifdef LTC_SHA512_256 325 #ifndef LTC_SHA512 326 #error LTC_SHA512 is required for LTC_SHA512_256 327 #endif 328 int sha512_256_init(hash_state * md); 329 #define sha512_256_process sha512_process 330 int sha512_256_done(hash_state * md, unsigned char *out); 331 int sha512_256_test(void); 332 extern const struct ltc_hash_descriptor sha512_256_desc; 333 #endif 334 335 #ifdef LTC_SHA512_224 336 #ifndef LTC_SHA512 337 #error LTC_SHA512 is required for LTC_SHA512_224 338 #endif 339 int sha512_224_init(hash_state * md); 340 #define sha512_224_process sha512_process 341 int sha512_224_done(hash_state * md, unsigned char *out); 342 int sha512_224_test(void); 343 extern const struct ltc_hash_descriptor sha512_224_desc; 344 #endif 345 346 #ifdef LTC_SHA256 347 int sha256_init(hash_state * md); 348 int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen); 349 int sha256_done(hash_state * md, unsigned char *out); 350 int sha256_test(void); 351 extern const struct ltc_hash_descriptor sha256_desc; 352 353 #ifdef LTC_SHA224 354 #ifndef LTC_SHA256 355 #error LTC_SHA256 is required for LTC_SHA224 356 #endif 357 int sha224_init(hash_state * md); 358 #define sha224_process sha256_process 359 int sha224_done(hash_state * md, unsigned char *out); 360 int sha224_test(void); 361 extern const struct ltc_hash_descriptor sha224_desc; 362 #endif 363 #endif 364 365 #ifdef LTC_SHA1 366 int sha1_init(hash_state * md); 367 int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen); 368 int sha1_done(hash_state * md, unsigned char *out); 369 int sha1_test(void); 370 extern const struct ltc_hash_descriptor sha1_desc; 371 #endif 372 373 #ifdef LTC_BLAKE2S 374 extern const struct ltc_hash_descriptor blake2s_256_desc; 375 int blake2s_256_init(hash_state * md); 376 int blake2s_256_test(void); 377 378 extern const struct ltc_hash_descriptor blake2s_224_desc; 379 int blake2s_224_init(hash_state * md); 380 int blake2s_224_test(void); 381 382 extern const struct ltc_hash_descriptor blake2s_160_desc; 383 int blake2s_160_init(hash_state * md); 384 int blake2s_160_test(void); 385 386 extern const struct ltc_hash_descriptor blake2s_128_desc; 387 int blake2s_128_init(hash_state * md); 388 int blake2s_128_test(void); 389 390 int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); 391 int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen); 392 int blake2s_done(hash_state * md, unsigned char *out); 393 #endif 394 395 #ifdef LTC_BLAKE2B 396 extern const struct ltc_hash_descriptor blake2b_512_desc; 397 int blake2b_512_init(hash_state * md); 398 int blake2b_512_test(void); 399 400 extern const struct ltc_hash_descriptor blake2b_384_desc; 401 int blake2b_384_init(hash_state * md); 402 int blake2b_384_test(void); 403 404 extern const struct ltc_hash_descriptor blake2b_256_desc; 405 int blake2b_256_init(hash_state * md); 406 int blake2b_256_test(void); 407 408 extern const struct ltc_hash_descriptor blake2b_160_desc; 409 int blake2b_160_init(hash_state * md); 410 int blake2b_160_test(void); 411 412 int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen); 413 int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen); 414 int blake2b_done(hash_state * md, unsigned char *out); 415 #endif 416 417 #ifdef LTC_MD5 418 int md5_init(hash_state * md); 419 int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen); 420 int md5_done(hash_state * md, unsigned char *out); 421 int md5_test(void); 422 extern const struct ltc_hash_descriptor md5_desc; 423 #endif 424 425 #ifdef LTC_MD4 426 int md4_init(hash_state * md); 427 int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen); 428 int md4_done(hash_state * md, unsigned char *out); 429 int md4_test(void); 430 extern const struct ltc_hash_descriptor md4_desc; 431 #endif 432 433 #ifdef LTC_MD2 434 int md2_init(hash_state * md); 435 int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen); 436 int md2_done(hash_state * md, unsigned char *out); 437 int md2_test(void); 438 extern const struct ltc_hash_descriptor md2_desc; 439 #endif 440 441 #ifdef LTC_TIGER 442 int tiger_init(hash_state * md); 443 int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen); 444 int tiger_done(hash_state * md, unsigned char *out); 445 int tiger_test(void); 446 extern const struct ltc_hash_descriptor tiger_desc; 447 #endif 448 449 #ifdef LTC_RIPEMD128 450 int rmd128_init(hash_state * md); 451 int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen); 452 int rmd128_done(hash_state * md, unsigned char *out); 453 int rmd128_test(void); 454 extern const struct ltc_hash_descriptor rmd128_desc; 455 #endif 456 457 #ifdef LTC_RIPEMD160 458 int rmd160_init(hash_state * md); 459 int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen); 460 int rmd160_done(hash_state * md, unsigned char *out); 461 int rmd160_test(void); 462 extern const struct ltc_hash_descriptor rmd160_desc; 463 #endif 464 465 #ifdef LTC_RIPEMD256 466 int rmd256_init(hash_state * md); 467 int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen); 468 int rmd256_done(hash_state * md, unsigned char *out); 469 int rmd256_test(void); 470 extern const struct ltc_hash_descriptor rmd256_desc; 471 #endif 472 473 #ifdef LTC_RIPEMD320 474 int rmd320_init(hash_state * md); 475 int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen); 476 int rmd320_done(hash_state * md, unsigned char *out); 477 int rmd320_test(void); 478 extern const struct ltc_hash_descriptor rmd320_desc; 479 #endif 480 481 482 int find_hash(const char *name); 483 int find_hash_id(unsigned char ID); 484 int find_hash_oid(const unsigned long *ID, unsigned long IDlen); 485 int find_hash_any(const char *name, int digestlen); 486 int register_hash(const struct ltc_hash_descriptor *hash); 487 int unregister_hash(const struct ltc_hash_descriptor *hash); 488 int register_all_hashes(void); 489 int hash_is_valid(int idx); 490 491 LTC_MUTEX_PROTO(ltc_hash_mutex) 492 493 int hash_memory(int hash, 494 const unsigned char *in, unsigned long inlen, 495 unsigned char *out, unsigned long *outlen); 496 int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, 497 const unsigned char *in, unsigned long inlen, ...) 498 LTC_NULL_TERMINATED; 499 500 #ifndef LTC_NO_FILE 501 int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen); 502 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen); 503 #endif 504