mycrypt_hash.h 17 KB

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