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