md4.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* Submitted by Dobes Vandermeer ([email protected]) */
  12. #include "mycrypt.h"
  13. #ifdef MD4
  14. const struct _hash_descriptor md4_desc =
  15. {
  16. "md4",
  17. 6,
  18. 16,
  19. 64,
  20. /* DER encoding (not yet supported) */
  21. { 0x00 },
  22. 0,
  23. &md4_init,
  24. &md4_process,
  25. &md4_done,
  26. &md4_test
  27. };
  28. #define S11 3
  29. #define S12 7
  30. #define S13 11
  31. #define S14 19
  32. #define S21 3
  33. #define S22 5
  34. #define S23 9
  35. #define S24 13
  36. #define S31 3
  37. #define S32 9
  38. #define S33 11
  39. #define S34 15
  40. /* F, G and H are basic MD4 functions. */
  41. #define F(x, y, z) (z ^ (x & (y ^ z)))
  42. #define G(x, y, z) ((x & y) | (z & (x | y)))
  43. #define H(x, y, z) ((x) ^ (y) ^ (z))
  44. /* ROTATE_LEFT rotates x left n bits. */
  45. #define ROTATE_LEFT(x, n) ROL(x, n)
  46. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  47. /* Rotation is separate from addition to prevent recomputation */
  48. #define FF(a, b, c, d, x, s) { \
  49. (a) += F ((b), (c), (d)) + (x); \
  50. (a) = ROTATE_LEFT ((a), (s)); \
  51. }
  52. #define GG(a, b, c, d, x, s) { \
  53. (a) += G ((b), (c), (d)) + (x) + 0x5a827999UL; \
  54. (a) = ROTATE_LEFT ((a), (s)); \
  55. }
  56. #define HH(a, b, c, d, x, s) { \
  57. (a) += H ((b), (c), (d)) + (x) + 0x6ed9eba1UL; \
  58. (a) = ROTATE_LEFT ((a), (s)); \
  59. }
  60. #ifdef CLEAN_STACK
  61. static void _md4_compress(hash_state *md, unsigned char *buf)
  62. #else
  63. static void md4_compress(hash_state *md, unsigned char *buf)
  64. #endif
  65. {
  66. ulong32 x[16], a, b, c, d;
  67. int i;
  68. /* copy state */
  69. a = md->md4.state[0];
  70. b = md->md4.state[1];
  71. c = md->md4.state[2];
  72. d = md->md4.state[3];
  73. /* copy the state into 512-bits into W[0..15] */
  74. for (i = 0; i < 16; i++) {
  75. LOAD32L(x[i], buf + (4*i));
  76. }
  77. /* Round 1 */
  78. FF (a, b, c, d, x[ 0], S11); /* 1 */
  79. FF (d, a, b, c, x[ 1], S12); /* 2 */
  80. FF (c, d, a, b, x[ 2], S13); /* 3 */
  81. FF (b, c, d, a, x[ 3], S14); /* 4 */
  82. FF (a, b, c, d, x[ 4], S11); /* 5 */
  83. FF (d, a, b, c, x[ 5], S12); /* 6 */
  84. FF (c, d, a, b, x[ 6], S13); /* 7 */
  85. FF (b, c, d, a, x[ 7], S14); /* 8 */
  86. FF (a, b, c, d, x[ 8], S11); /* 9 */
  87. FF (d, a, b, c, x[ 9], S12); /* 10 */
  88. FF (c, d, a, b, x[10], S13); /* 11 */
  89. FF (b, c, d, a, x[11], S14); /* 12 */
  90. FF (a, b, c, d, x[12], S11); /* 13 */
  91. FF (d, a, b, c, x[13], S12); /* 14 */
  92. FF (c, d, a, b, x[14], S13); /* 15 */
  93. FF (b, c, d, a, x[15], S14); /* 16 */
  94. /* Round 2 */
  95. GG (a, b, c, d, x[ 0], S21); /* 17 */
  96. GG (d, a, b, c, x[ 4], S22); /* 18 */
  97. GG (c, d, a, b, x[ 8], S23); /* 19 */
  98. GG (b, c, d, a, x[12], S24); /* 20 */
  99. GG (a, b, c, d, x[ 1], S21); /* 21 */
  100. GG (d, a, b, c, x[ 5], S22); /* 22 */
  101. GG (c, d, a, b, x[ 9], S23); /* 23 */
  102. GG (b, c, d, a, x[13], S24); /* 24 */
  103. GG (a, b, c, d, x[ 2], S21); /* 25 */
  104. GG (d, a, b, c, x[ 6], S22); /* 26 */
  105. GG (c, d, a, b, x[10], S23); /* 27 */
  106. GG (b, c, d, a, x[14], S24); /* 28 */
  107. GG (a, b, c, d, x[ 3], S21); /* 29 */
  108. GG (d, a, b, c, x[ 7], S22); /* 30 */
  109. GG (c, d, a, b, x[11], S23); /* 31 */
  110. GG (b, c, d, a, x[15], S24); /* 32 */
  111. /* Round 3 */
  112. HH (a, b, c, d, x[ 0], S31); /* 33 */
  113. HH (d, a, b, c, x[ 8], S32); /* 34 */
  114. HH (c, d, a, b, x[ 4], S33); /* 35 */
  115. HH (b, c, d, a, x[12], S34); /* 36 */
  116. HH (a, b, c, d, x[ 2], S31); /* 37 */
  117. HH (d, a, b, c, x[10], S32); /* 38 */
  118. HH (c, d, a, b, x[ 6], S33); /* 39 */
  119. HH (b, c, d, a, x[14], S34); /* 40 */
  120. HH (a, b, c, d, x[ 1], S31); /* 41 */
  121. HH (d, a, b, c, x[ 9], S32); /* 42 */
  122. HH (c, d, a, b, x[ 5], S33); /* 43 */
  123. HH (b, c, d, a, x[13], S34); /* 44 */
  124. HH (a, b, c, d, x[ 3], S31); /* 45 */
  125. HH (d, a, b, c, x[11], S32); /* 46 */
  126. HH (c, d, a, b, x[ 7], S33); /* 47 */
  127. HH (b, c, d, a, x[15], S34); /* 48 */
  128. /* Update our state */
  129. md->md4.state[0] = md->md4.state[0] + a;
  130. md->md4.state[1] = md->md4.state[1] + b;
  131. md->md4.state[2] = md->md4.state[2] + c;
  132. md->md4.state[3] = md->md4.state[3] + d;
  133. }
  134. #ifdef CLEAN_STACK
  135. static void md4_compress(hash_state *md, unsigned char *buf)
  136. {
  137. _md4_compress(md, buf);
  138. burn_stack(sizeof(ulong32) * 20 + sizeof(int));
  139. }
  140. #endif
  141. void md4_init(hash_state * md)
  142. {
  143. _ARGCHK(md != NULL);
  144. md->md4.state[0] = 0x67452301UL;
  145. md->md4.state[1] = 0xefcdab89UL;
  146. md->md4.state[2] = 0x98badcfeUL;
  147. md->md4.state[3] = 0x10325476UL;
  148. md->md4.length = 0;
  149. md->md4.curlen = 0;
  150. }
  151. HASH_PROCESS(md4_process, md4_compress, md4, 64)
  152. int md4_done(hash_state * md, unsigned char *hash)
  153. {
  154. int i;
  155. _ARGCHK(md != NULL);
  156. _ARGCHK(hash != NULL);
  157. if (md->md4.curlen >= sizeof(md->md4.buf)) {
  158. return CRYPT_INVALID_ARG;
  159. }
  160. /* increase the length of the message */
  161. md->md4.length += md->md4.curlen * 8;
  162. /* append the '1' bit */
  163. md->md4.buf[md->md4.curlen++] = (unsigned char)0x80;
  164. /* if the length is currently above 56 bytes we append zeros
  165. * then compress. Then we can fall back to padding zeros and length
  166. * encoding like normal.
  167. */
  168. if (md->md4.curlen > 56) {
  169. while (md->md4.curlen < 64) {
  170. md->md4.buf[md->md4.curlen++] = (unsigned char)0;
  171. }
  172. md4_compress(md, md->md4.buf);
  173. md->md4.curlen = 0;
  174. }
  175. /* pad upto 56 bytes of zeroes */
  176. while (md->md4.curlen < 56) {
  177. md->md4.buf[md->md4.curlen++] = (unsigned char)0;
  178. }
  179. /* store length */
  180. STORE64L(md->md4.length, md->md4.buf+56);
  181. md4_compress(md, md->md4.buf);
  182. /* copy output */
  183. for (i = 0; i < 4; i++) {
  184. STORE32L(md->md4.state[i], hash+(4*i));
  185. }
  186. #ifdef CLEAN_STACK
  187. zeromem(md, sizeof(hash_state));
  188. #endif
  189. return CRYPT_OK;
  190. }
  191. int md4_test(void)
  192. {
  193. #ifndef LTC_TEST
  194. return CRYPT_NOP;
  195. #else
  196. static const struct md4_test_case {
  197. char *input;
  198. unsigned char digest[16];
  199. } cases[] = {
  200. { "",
  201. {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
  202. 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
  203. { "a",
  204. {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46,
  205. 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} },
  206. { "abc",
  207. {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
  208. 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} },
  209. { "message digest",
  210. {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
  211. 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} },
  212. { "abcdefghijklmnopqrstuvwxyz",
  213. {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
  214. 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} },
  215. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
  216. {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
  217. 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} },
  218. { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
  219. {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
  220. 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} },
  221. };
  222. int i;
  223. hash_state md;
  224. unsigned char digest[16];
  225. for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) {
  226. md4_init(&md);
  227. md4_process(&md, (unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input));
  228. md4_done(&md, digest);
  229. if (memcmp(digest, cases[i].digest, 16) != 0) {
  230. return CRYPT_FAIL_TESTVECTOR;
  231. }
  232. }
  233. return CRYPT_OK;
  234. #endif
  235. }
  236. #endif