chc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. #ifdef CHC_HASH
  13. #define UNDEFED_HASH -17
  14. /* chc settings */
  15. static int cipher_idx=UNDEFED_HASH, /* which cipher */
  16. cipher_blocksize; /* blocksize of cipher */
  17. const struct _hash_descriptor chc_desc = {
  18. "chc_hash", 12, 0, 0, { 0 }, 0,
  19. &chc_init,
  20. &chc_process,
  21. &chc_done,
  22. &chc_test
  23. };
  24. /* initialize the CHC state with a given cipher */
  25. int chc_register(int cipher)
  26. {
  27. int err, kl, idx;
  28. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  29. return err;
  30. }
  31. /* will it be valid? */
  32. kl = cipher_descriptor[cipher].block_length;
  33. /* must be >64 bit block */
  34. if (kl <= 8) {
  35. return CRYPT_INVALID_CIPHER;
  36. }
  37. /* can we use the ideal keysize? */
  38. if ((err = cipher_descriptor[cipher].keysize(&kl)) != CRYPT_OK) {
  39. return err;
  40. }
  41. /* we require that key size == block size be a valid choice */
  42. if (kl != cipher_descriptor[cipher].block_length) {
  43. return CRYPT_INVALID_CIPHER;
  44. }
  45. /* determine if chc_hash has been register_hash'ed already */
  46. if ((err = hash_is_valid(idx = find_hash("chc_hash"))) != CRYPT_OK) {
  47. return err;
  48. }
  49. /* store into descriptor */
  50. hash_descriptor[idx].hashsize =
  51. hash_descriptor[idx].blocksize = cipher_descriptor[cipher].block_length;
  52. /* store the idx and block size */
  53. cipher_idx = cipher;
  54. cipher_blocksize = cipher_descriptor[cipher].block_length;
  55. return CRYPT_OK;
  56. }
  57. /* "hash init" is simply encrypt 0 with the 0 key. Simple way to make an IV */
  58. int chc_init(hash_state *md)
  59. {
  60. symmetric_key *key;
  61. unsigned char buf[MAXBLOCKSIZE];
  62. int err;
  63. _ARGCHK(md != NULL);
  64. /* is the cipher valid? */
  65. if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) {
  66. return err;
  67. }
  68. if (cipher_blocksize != cipher_descriptor[cipher_idx].block_length) {
  69. return CRYPT_INVALID_CIPHER;
  70. }
  71. if ((key = XMALLOC(sizeof(*key))) == NULL) {
  72. return CRYPT_MEM;
  73. }
  74. /* zero key and what not */
  75. zeromem(buf, cipher_blocksize);
  76. if ((err = cipher_descriptor[cipher_idx].setup(buf, cipher_blocksize, 0, key)) != CRYPT_OK) {
  77. XFREE(key);
  78. return err;
  79. }
  80. /* encrypt zero block */
  81. cipher_descriptor[cipher_idx].ecb_encrypt(buf, md->chc.state, key);
  82. /* zero other members */
  83. md->chc.length = 0;
  84. md->chc.curlen = 0;
  85. zeromem(md->chc.buf, sizeof(md->chc.buf));
  86. XFREE(key);
  87. return CRYPT_OK;
  88. }
  89. /*
  90. key <= state
  91. T0,T1 <= block
  92. T0 <= encrypt T0
  93. state <= state xor T0 xor T1
  94. */
  95. static int chc_compress(hash_state *md, unsigned char *buf)
  96. {
  97. unsigned char T[2][MAXBLOCKSIZE];
  98. symmetric_key *key;
  99. int err, x;
  100. if ((key = XMALLOC(sizeof(*key))) == NULL) {
  101. return CRYPT_MEM;
  102. }
  103. if ((err = cipher_descriptor[cipher_idx].setup(md->chc.state, cipher_blocksize, 0, key)) != CRYPT_OK) {
  104. XFREE(key);
  105. return err;
  106. }
  107. memcpy(T[1], buf, cipher_blocksize);
  108. cipher_descriptor[cipher_idx].ecb_encrypt(buf, T[0], key);
  109. for (x = 0; x < cipher_blocksize; x++) {
  110. md->chc.state[x] ^= T[0][x] ^ T[1][x];
  111. }
  112. XFREE(key);
  113. #ifdef CLEAN_STACK
  114. zeromem(T, sizeof(T));
  115. zeromem(&key, sizeof(key));
  116. #endif
  117. return CRYPT_OK;
  118. }
  119. HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize)
  120. int chc_process(hash_state * md, const unsigned char *buf, unsigned long len)
  121. {
  122. int err;
  123. _ARGCHK(md != NULL);
  124. _ARGCHK(buf != NULL);
  125. /* is the cipher valid? */
  126. if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) {
  127. return err;
  128. }
  129. if (cipher_blocksize != cipher_descriptor[cipher_idx].block_length) {
  130. return CRYPT_INVALID_CIPHER;
  131. }
  132. return _chc_process(md, buf, len);
  133. }
  134. int chc_done(hash_state *md, unsigned char *buf)
  135. {
  136. int err;
  137. _ARGCHK(md != NULL);
  138. _ARGCHK(buf != NULL);
  139. /* is the cipher valid? */
  140. if ((err = cipher_is_valid(cipher_idx)) != CRYPT_OK) {
  141. return err;
  142. }
  143. if (cipher_blocksize != cipher_descriptor[cipher_idx].block_length) {
  144. return CRYPT_INVALID_CIPHER;
  145. }
  146. if (md->chc.curlen >= sizeof(md->chc.buf)) {
  147. return CRYPT_INVALID_ARG;
  148. }
  149. /* increase the length of the message */
  150. md->chc.length += md->chc.curlen * 8;
  151. /* append the '1' bit */
  152. md->chc.buf[md->chc.curlen++] = (unsigned char)0x80;
  153. /* if the length is currently above l-8 bytes we append zeros
  154. * then compress. Then we can fall back to padding zeros and length
  155. * encoding like normal.
  156. */
  157. if (md->chc.curlen > (unsigned long)(cipher_blocksize - 8)) {
  158. while (md->chc.curlen < (unsigned long)cipher_blocksize) {
  159. md->chc.buf[md->chc.curlen++] = (unsigned char)0;
  160. }
  161. chc_compress(md, md->chc.buf);
  162. md->chc.curlen = 0;
  163. }
  164. /* pad upto l-8 bytes of zeroes */
  165. while (md->chc.curlen < (unsigned long)(cipher_blocksize - 8)) {
  166. md->chc.buf[md->chc.curlen++] = (unsigned char)0;
  167. }
  168. /* store length */
  169. STORE64L(md->chc.length, md->chc.buf+(cipher_blocksize-8));
  170. chc_compress(md, md->chc.buf);
  171. /* copy output */
  172. XMEMCPY(buf, md->chc.state, cipher_blocksize);
  173. #ifdef CLEAN_STACK
  174. zeromem(md, sizeof(hash_state));
  175. #endif
  176. return CRYPT_OK;
  177. }
  178. int chc_test(void)
  179. {
  180. static const struct {
  181. unsigned char *msg,
  182. md[MAXBLOCKSIZE];
  183. int len;
  184. } tests[] = {
  185. {
  186. (unsigned char *)"hello world",
  187. { 0xcf, 0x57, 0x9d, 0xc3, 0x0a, 0x0e, 0xea, 0x61,
  188. 0x0d, 0x54, 0x47, 0xc4, 0x3c, 0x06, 0xf5, 0x4e },
  189. 16
  190. }
  191. };
  192. int x, oldhashidx, idx;
  193. unsigned char out[MAXBLOCKSIZE];
  194. hash_state md;
  195. /* AES can be under rijndael or aes... try to find it */
  196. if ((idx = find_cipher("aes")) == -1) {
  197. if ((idx = find_cipher("rijndael")) == -1) {
  198. return CRYPT_NOP;
  199. }
  200. }
  201. oldhashidx = cipher_idx;
  202. chc_register(idx);
  203. for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
  204. chc_init(&md);
  205. chc_process(&md, tests[x].msg, strlen((char *)tests[x].msg));
  206. chc_done(&md, out);
  207. if (memcmp(out, tests[x].md, tests[x].len)) {
  208. return CRYPT_FAIL_TESTVECTOR;
  209. }
  210. }
  211. if (oldhashidx != UNDEFED_HASH) {
  212. chc_register(oldhashidx);
  213. }
  214. return CRYPT_OK;
  215. }
  216. #endif