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