mycrypt_hash.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. #ifdef SHA256
  10. struct sha256_state {
  11. ulong64 length;
  12. ulong32 state[8], curlen;
  13. unsigned char buf[64];
  14. };
  15. #endif
  16. #ifdef SHA1
  17. struct sha1_state {
  18. ulong64 length;
  19. ulong32 state[5], curlen;
  20. unsigned char buf[64];
  21. };
  22. #endif
  23. #ifdef MD5
  24. struct md5_state {
  25. ulong64 length;
  26. ulong32 state[4], curlen;
  27. unsigned char buf[64];
  28. };
  29. #endif
  30. #ifdef MD4
  31. struct md4_state {
  32. ulong64 length;
  33. ulong32 state[4], curlen;
  34. unsigned char buf[64];
  35. };
  36. #endif
  37. #ifdef TIGER
  38. struct tiger_state {
  39. ulong64 state[3], length;
  40. unsigned long curlen;
  41. unsigned char buf[64];
  42. };
  43. #endif
  44. #ifdef MD2
  45. struct md2_state {
  46. unsigned char chksum[16], X[48], buf[16];
  47. unsigned long curlen;
  48. };
  49. #endif
  50. #ifdef RIPEMD128
  51. struct rmd128_state {
  52. ulong64 length;
  53. unsigned char buf[64];
  54. ulong32 curlen, state[4];
  55. };
  56. #endif
  57. #ifdef RIPEMD160
  58. struct rmd160_state {
  59. ulong64 length;
  60. unsigned char buf[64];
  61. ulong32 curlen, state[5];
  62. };
  63. #endif
  64. #ifdef WHIRLPOOL
  65. struct whirlpool_state {
  66. ulong64 length, state[8];
  67. unsigned char buf[64];
  68. ulong32 curlen;
  69. };
  70. #endif
  71. typedef union Hash_state {
  72. #ifdef WHIRLPOOL
  73. struct whirlpool_state whirlpool;
  74. #endif
  75. #ifdef SHA512
  76. struct sha512_state sha512;
  77. #endif
  78. #ifdef SHA256
  79. struct sha256_state sha256;
  80. #endif
  81. #ifdef SHA1
  82. struct sha1_state sha1;
  83. #endif
  84. #ifdef MD5
  85. struct md5_state md5;
  86. #endif
  87. #ifdef MD4
  88. struct md4_state md4;
  89. #endif
  90. #ifdef MD2
  91. struct md2_state md2;
  92. #endif
  93. #ifdef TIGER
  94. struct tiger_state tiger;
  95. #endif
  96. #ifdef RIPEMD128
  97. struct rmd128_state rmd128;
  98. #endif
  99. #ifdef RIPEMD160
  100. struct rmd160_state rmd160;
  101. #endif
  102. } hash_state;
  103. extern struct _hash_descriptor {
  104. char *name;
  105. unsigned char ID;
  106. unsigned long hashsize; /* digest output size in bytes */
  107. unsigned long blocksize; /* the block size the hash uses */
  108. unsigned char DER[64]; /* DER encoded identifier */
  109. unsigned long DERlen; /* length of DER encoding */
  110. void (*init)(hash_state *);
  111. int (*process)(hash_state *, const unsigned char *, unsigned long);
  112. int (*done)(hash_state *, unsigned char *);
  113. int (*test)(void);
  114. } hash_descriptor[];
  115. #ifdef WHIRLPOOL
  116. void whirlpool_init(hash_state * md);
  117. int whirlpool_process(hash_state * md, const unsigned char *buf, unsigned long len);
  118. int whirlpool_done(hash_state * md, unsigned char *hash);
  119. int whirlpool_test(void);
  120. extern const struct _hash_descriptor whirlpool_desc;
  121. #endif
  122. #ifdef SHA512
  123. void sha512_init(hash_state * md);
  124. int sha512_process(hash_state * md, const unsigned char *buf, unsigned long len);
  125. int sha512_done(hash_state * md, unsigned char *hash);
  126. int sha512_test(void);
  127. extern const struct _hash_descriptor sha512_desc;
  128. #endif
  129. #ifdef SHA384
  130. #ifndef SHA512
  131. #error SHA512 is required for SHA384
  132. #endif
  133. void sha384_init(hash_state * md);
  134. #define sha384_process sha512_process
  135. int sha384_done(hash_state * md, unsigned char *hash);
  136. int sha384_test(void);
  137. extern const struct _hash_descriptor sha384_desc;
  138. #endif
  139. #ifdef SHA256
  140. void sha256_init(hash_state * md);
  141. int sha256_process(hash_state * md, const unsigned char *buf, unsigned long len);
  142. int sha256_done(hash_state * md, unsigned char *hash);
  143. int sha256_test(void);
  144. extern const struct _hash_descriptor sha256_desc;
  145. #ifdef SHA224
  146. #ifndef SHA256
  147. #error SHA256 is required for SHA224
  148. #endif
  149. void sha224_init(hash_state * md);
  150. #define sha224_process sha256_process
  151. int sha224_done(hash_state * md, unsigned char *hash);
  152. int sha224_test(void);
  153. extern const struct _hash_descriptor sha224_desc;
  154. #endif
  155. #endif
  156. #ifdef SHA1
  157. void sha1_init(hash_state * md);
  158. int sha1_process(hash_state * md, const unsigned char *buf, unsigned long len);
  159. int sha1_done(hash_state * md, unsigned char *hash);
  160. int sha1_test(void);
  161. extern const struct _hash_descriptor sha1_desc;
  162. #endif
  163. #ifdef MD5
  164. void md5_init(hash_state * md);
  165. int md5_process(hash_state * md, const unsigned char *buf, unsigned long len);
  166. int md5_done(hash_state * md, unsigned char *hash);
  167. int md5_test(void);
  168. extern const struct _hash_descriptor md5_desc;
  169. #endif
  170. #ifdef MD4
  171. void md4_init(hash_state * md);
  172. int md4_process(hash_state * md, const unsigned char *buf, unsigned long len);
  173. int md4_done(hash_state * md, unsigned char *hash);
  174. int md4_test(void);
  175. extern const struct _hash_descriptor md4_desc;
  176. #endif
  177. #ifdef MD2
  178. void md2_init(hash_state * md);
  179. int md2_process(hash_state * md, const unsigned char *buf, unsigned long len);
  180. int md2_done(hash_state * md, unsigned char *hash);
  181. int md2_test(void);
  182. extern const struct _hash_descriptor md2_desc;
  183. #endif
  184. #ifdef TIGER
  185. void tiger_init(hash_state * md);
  186. int tiger_process(hash_state * md, const unsigned char *buf, unsigned long len);
  187. int tiger_done(hash_state * md, unsigned char *hash);
  188. int tiger_test(void);
  189. extern const struct _hash_descriptor tiger_desc;
  190. #endif
  191. #ifdef RIPEMD128
  192. void rmd128_init(hash_state * md);
  193. int rmd128_process(hash_state * md, const unsigned char *buf, unsigned long len);
  194. int rmd128_done(hash_state * md, unsigned char *hash);
  195. int rmd128_test(void);
  196. extern const struct _hash_descriptor rmd128_desc;
  197. #endif
  198. #ifdef RIPEMD160
  199. void rmd160_init(hash_state * md);
  200. int rmd160_process(hash_state * md, const unsigned char *buf, unsigned long len);
  201. int rmd160_done(hash_state * md, unsigned char *hash);
  202. int rmd160_test(void);
  203. extern const struct _hash_descriptor rmd160_desc;
  204. #endif
  205. int find_hash(const char *name);
  206. int find_hash_id(unsigned char ID);
  207. int find_hash_any(const char *name, int digestlen);
  208. int register_hash(const struct _hash_descriptor *hash);
  209. int unregister_hash(const struct _hash_descriptor *hash);
  210. int hash_is_valid(int idx);
  211. int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen);
  212. int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outlen);
  213. int hash_file(int hash, const char *fname, unsigned char *dst, unsigned long *outlen);
  214. /* a simple macro for making hash "process" functions */
  215. #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
  216. int func_name (hash_state * md, const unsigned char *buf, unsigned long len) \
  217. { \
  218. unsigned long n; \
  219. _ARGCHK(md != NULL); \
  220. _ARGCHK(buf != NULL); \
  221. if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
  222. return CRYPT_INVALID_ARG; \
  223. } \
  224. while (len > 0) { \
  225. if (md-> state_var .curlen == 0 && len >= block_size) { \
  226. compress_name (md, (unsigned char *)buf); \
  227. md-> state_var .length += block_size * 8; \
  228. buf += block_size; \
  229. len -= block_size; \
  230. } else { \
  231. n = MIN(len, (block_size - md-> state_var .curlen)); \
  232. memcpy(md-> state_var .buf + md-> state_var.curlen, buf, (size_t)n); \
  233. md-> state_var .curlen += n; \
  234. buf += n; \
  235. len -= n; \
  236. if (md-> state_var .curlen == block_size) { \
  237. compress_name (md, md-> state_var .buf); \
  238. md-> state_var .length += 8*block_size; \
  239. md-> state_var .curlen = 0; \
  240. } \
  241. } \
  242. } \
  243. return CRYPT_OK; \
  244. }
  245. #ifdef HMAC
  246. typedef struct Hmac_state {
  247. hash_state md;
  248. int hash;
  249. hash_state hashstate;
  250. unsigned char *key;
  251. } hmac_state;
  252. int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen);
  253. int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len);
  254. int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen);
  255. int hmac_test(void);
  256. int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,
  257. const unsigned char *data, unsigned long len,
  258. unsigned char *dst, unsigned long *dstlen);
  259. int hmac_file(int hash, const char *fname, const unsigned char *key,
  260. unsigned long keylen,
  261. unsigned char *dst, unsigned long *dstlen);
  262. #endif
  263. #ifdef OMAC
  264. typedef struct {
  265. int cipher_idx,
  266. buflen,
  267. blklen;
  268. unsigned char block[MAXBLOCKSIZE],
  269. prev[MAXBLOCKSIZE],
  270. Lu[2][MAXBLOCKSIZE];
  271. symmetric_key key;
  272. } omac_state;
  273. int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);
  274. int omac_process(omac_state *state, const unsigned char *buf, unsigned long len);
  275. int omac_done(omac_state *state, unsigned char *out, unsigned long *outlen);
  276. int omac_memory(int cipher, const unsigned char *key, unsigned long keylen,
  277. const unsigned char *msg, unsigned long msglen,
  278. unsigned char *out, unsigned long *outlen);
  279. int omac_file(int cipher, const unsigned char *key, unsigned long keylen,
  280. const char *filename, unsigned char *out, unsigned long *outlen);
  281. int omac_test(void);
  282. #endif /* OMAC */
  283. #ifdef PMAC
  284. typedef struct {
  285. unsigned char Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
  286. Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
  287. Lr[MAXBLOCKSIZE], /* L * x^-1 */
  288. block[MAXBLOCKSIZE], /* currently accumulated block */
  289. checksum[MAXBLOCKSIZE]; /* current checksum */
  290. symmetric_key key; /* scheduled key for cipher */
  291. unsigned long block_index; /* index # for current block */
  292. int cipher_idx, /* cipher idx */
  293. block_len, /* length of block */
  294. buflen; /* number of bytes in the buffer */
  295. } pmac_state;
  296. int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen);
  297. int pmac_process(pmac_state *state, const unsigned char *buf, unsigned long len);
  298. int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen);
  299. int pmac_memory(int cipher, const unsigned char *key, unsigned long keylen,
  300. const unsigned char *msg, unsigned long msglen,
  301. unsigned char *out, unsigned long *outlen);
  302. int pmac_file(int cipher, const unsigned char *key, unsigned long keylen,
  303. const char *filename, unsigned char *out, unsigned long *outlen);
  304. int pmac_test(void);
  305. /* internal functions */
  306. int pmac_ntz(unsigned long x);
  307. void pmac_shift_xor(pmac_state *pmac);
  308. #endif /* PMAC */
  309. #ifdef EAX_MODE
  310. #if !(defined(OMAC) && defined(CTR))
  311. #error EAX_MODE requires OMAC and CTR
  312. #endif
  313. typedef struct {
  314. unsigned char N[MAXBLOCKSIZE];
  315. symmetric_CTR ctr;
  316. omac_state headeromac, ctomac;
  317. } eax_state;
  318. int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
  319. const unsigned char *nonce, unsigned long noncelen,
  320. const unsigned char *header, unsigned long headerlen);
  321. int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length);
  322. int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length);
  323. int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length);
  324. int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen);
  325. int eax_encrypt_authenticate_memory(int cipher,
  326. const unsigned char *key, unsigned long keylen,
  327. const unsigned char *nonce, unsigned long noncelen,
  328. const unsigned char *header, unsigned long headerlen,
  329. const unsigned char *pt, unsigned long ptlen,
  330. unsigned char *ct,
  331. unsigned char *tag, unsigned long *taglen);
  332. int eax_decrypt_verify_memory(int cipher,
  333. const unsigned char *key, unsigned long keylen,
  334. const unsigned char *nonce, unsigned long noncelen,
  335. const unsigned char *header, unsigned long headerlen,
  336. const unsigned char *ct, unsigned long ctlen,
  337. unsigned char *pt,
  338. unsigned char *tag, unsigned long taglen,
  339. int *res);
  340. int eax_test(void);
  341. #endif /* EAX MODE */
  342. #ifdef OCB_MODE
  343. typedef struct {
  344. unsigned char L[MAXBLOCKSIZE], /* L value */
  345. Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
  346. Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
  347. Lr[MAXBLOCKSIZE], /* L * x^-1 */
  348. R[MAXBLOCKSIZE], /* R value */
  349. checksum[MAXBLOCKSIZE]; /* current checksum */
  350. symmetric_key key; /* scheduled key for cipher */
  351. unsigned long block_index; /* index # for current block */
  352. int cipher, /* cipher idx */
  353. block_len; /* length of block */
  354. } ocb_state;
  355. int ocb_init(ocb_state *ocb, int cipher,
  356. const unsigned char *key, unsigned long keylen, const unsigned char *nonce);
  357. int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct);
  358. int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt);
  359. int ocb_done_encrypt(ocb_state *ocb,
  360. const unsigned char *pt, unsigned long ptlen,
  361. unsigned char *ct,
  362. unsigned char *tag, unsigned long *taglen);
  363. int ocb_done_decrypt(ocb_state *ocb,
  364. const unsigned char *ct, unsigned long ctlen,
  365. unsigned char *pt,
  366. const unsigned char *tag, unsigned long taglen, int *res);
  367. int ocb_encrypt_authenticate_memory(int cipher,
  368. const unsigned char *key, unsigned long keylen,
  369. const unsigned char *nonce,
  370. const unsigned char *pt, unsigned long ptlen,
  371. unsigned char *ct,
  372. unsigned char *tag, unsigned long *taglen);
  373. int ocb_decrypt_verify_memory(int cipher,
  374. const unsigned char *key, unsigned long keylen,
  375. const unsigned char *nonce,
  376. const unsigned char *ct, unsigned long ctlen,
  377. unsigned char *pt,
  378. const unsigned char *tag, unsigned long taglen,
  379. int *res);
  380. int ocb_test(void);
  381. /* internal functions */
  382. void ocb_shift_xor(ocb_state *ocb, unsigned char *Z);
  383. int ocb_ntz(unsigned long x);
  384. int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
  385. unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode);
  386. #endif /* OCB_MODE */