md4.c 8.4 KB

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