md5.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * This file was transplanted with slight modifications from Linux sources
  3. * (fs/cifs/md5.c) into U-Boot by Bartlomiej Sieka <tur@semihalf.com>.
  4. */
  5. /*
  6. * This code implements the MD5 message-digest algorithm.
  7. * The algorithm is due to Ron Rivest. This code was
  8. * written by Colin Plumb in 1993, no copyright is claimed.
  9. * This code is in the public domain; do with it what you wish.
  10. *
  11. * Equivalent code is available from RSA Data Security, Inc.
  12. * This code has been tested against that, and is equivalent,
  13. * except that you don't need to include two pages of legalese
  14. * with every copy.
  15. *
  16. * To compute the message digest of a chunk of bytes, declare an
  17. * MD5Context structure, pass it to MD5Init, call MD5Update as
  18. * needed on buffers full of bytes, and then call MD5Final, which
  19. * will fill a supplied 16-byte array with the digest.
  20. */
  21. /* This code slightly modified to fit into Samba by
  22. abartlet@samba.org Jun 2001
  23. and to fit the cifs vfs by
  24. Steve French sfrench@us.ibm.com */
  25. #include "compiler.h"
  26. #ifndef USE_HOSTCC
  27. #include <common.h>
  28. #include <watchdog.h>
  29. #endif /* USE_HOSTCC */
  30. #include <u-boot/md5.h>
  31. static void
  32. MD5Transform(__u32 buf[4], __u32 const in[16]);
  33. /*
  34. * Note: this code is harmless on little-endian machines.
  35. */
  36. static void
  37. byteReverse(unsigned char *buf, unsigned longs)
  38. {
  39. __u32 t;
  40. do {
  41. t = (__u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
  42. ((unsigned) buf[1] << 8 | buf[0]);
  43. *(__u32 *) buf = t;
  44. buf += 4;
  45. } while (--longs);
  46. }
  47. /*
  48. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  49. * initialization constants.
  50. */
  51. static void
  52. MD5Init(struct MD5Context *ctx)
  53. {
  54. ctx->buf[0] = 0x67452301;
  55. ctx->buf[1] = 0xefcdab89;
  56. ctx->buf[2] = 0x98badcfe;
  57. ctx->buf[3] = 0x10325476;
  58. ctx->bits[0] = 0;
  59. ctx->bits[1] = 0;
  60. }
  61. /*
  62. * Update context to reflect the concatenation of another buffer full
  63. * of bytes.
  64. */
  65. static void
  66. MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
  67. {
  68. register __u32 t;
  69. /* Update bitcount */
  70. t = ctx->bits[0];
  71. if ((ctx->bits[0] = t + ((__u32) len << 3)) < t)
  72. ctx->bits[1]++; /* Carry from low to high */
  73. ctx->bits[1] += len >> 29;
  74. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  75. /* Handle any leading odd-sized chunks */
  76. if (t) {
  77. unsigned char *p = (unsigned char *) ctx->in + t;
  78. t = 64 - t;
  79. if (len < t) {
  80. memmove(p, buf, len);
  81. return;
  82. }
  83. memmove(p, buf, t);
  84. byteReverse(ctx->in, 16);
  85. MD5Transform(ctx->buf, (__u32 *) ctx->in);
  86. buf += t;
  87. len -= t;
  88. }
  89. /* Process data in 64-byte chunks */
  90. while (len >= 64) {
  91. memmove(ctx->in, buf, 64);
  92. byteReverse(ctx->in, 16);
  93. MD5Transform(ctx->buf, (__u32 *) ctx->in);
  94. buf += 64;
  95. len -= 64;
  96. }
  97. /* Handle any remaining bytes of data. */
  98. memmove(ctx->in, buf, len);
  99. }
  100. /*
  101. * Final wrapup - pad to 64-byte boundary with the bit pattern
  102. * 1 0* (64-bit count of bits processed, MSB-first)
  103. */
  104. static void
  105. MD5Final(unsigned char digest[16], struct MD5Context *ctx)
  106. {
  107. unsigned int count;
  108. unsigned char *p;
  109. /* Compute number of bytes mod 64 */
  110. count = (ctx->bits[0] >> 3) & 0x3F;
  111. /* Set the first char of padding to 0x80. This is safe since there is
  112. always at least one byte free */
  113. p = ctx->in + count;
  114. *p++ = 0x80;
  115. /* Bytes of padding needed to make 64 bytes */
  116. count = 64 - 1 - count;
  117. /* Pad out to 56 mod 64 */
  118. if (count < 8) {
  119. /* Two lots of padding: Pad the first block to 64 bytes */
  120. memset(p, 0, count);
  121. byteReverse(ctx->in, 16);
  122. MD5Transform(ctx->buf, (__u32 *) ctx->in);
  123. /* Now fill the next block with 56 bytes */
  124. memset(ctx->in, 0, 56);
  125. } else {
  126. /* Pad block to 56 bytes */
  127. memset(p, 0, count - 8);
  128. }
  129. byteReverse(ctx->in, 14);
  130. /* Append length in bits and transform */
  131. ctx->in32[14] = ctx->bits[0];
  132. ctx->in32[15] = ctx->bits[1];
  133. MD5Transform(ctx->buf, (__u32 *) ctx->in);
  134. byteReverse((unsigned char *) ctx->buf, 4);
  135. memmove(digest, ctx->buf, 16);
  136. memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
  137. }
  138. /* The four core functions - F1 is optimized somewhat */
  139. /* #define F1(x, y, z) (x & y | ~x & z) */
  140. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  141. #define F2(x, y, z) F1(z, x, y)
  142. #define F3(x, y, z) (x ^ y ^ z)
  143. #define F4(x, y, z) (y ^ (x | ~z))
  144. /* This is the central step in the MD5 algorithm. */
  145. #define MD5STEP(f, w, x, y, z, data, s) \
  146. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  147. /*
  148. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  149. * reflect the addition of 16 longwords of new data. MD5Update blocks
  150. * the data and converts bytes into longwords for this routine.
  151. */
  152. static void
  153. MD5Transform(__u32 buf[4], __u32 const in[16])
  154. {
  155. register __u32 a, b, c, d;
  156. a = buf[0];
  157. b = buf[1];
  158. c = buf[2];
  159. d = buf[3];
  160. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  161. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  162. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  163. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  164. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  165. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  166. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  167. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  168. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  169. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  170. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  171. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  172. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  173. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  174. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  175. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  176. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  177. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  178. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  179. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  180. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  181. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  182. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  183. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  184. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  185. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  186. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  187. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  188. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  189. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  190. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  191. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  192. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  193. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  194. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  195. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  196. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  197. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  198. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  199. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  200. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  201. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  202. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  203. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  204. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  205. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  206. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  207. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  208. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  209. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  210. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  211. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  212. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  213. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  214. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  215. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  216. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  217. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  218. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  219. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  220. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  221. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  222. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  223. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  224. buf[0] += a;
  225. buf[1] += b;
  226. buf[2] += c;
  227. buf[3] += d;
  228. }
  229. /*
  230. * Calculate and store in 'output' the MD5 digest of 'len' bytes at
  231. * 'input'. 'output' must have enough space to hold 16 bytes.
  232. */
  233. void
  234. md5 (unsigned char *input, int len, unsigned char output[16])
  235. {
  236. struct MD5Context context;
  237. MD5Init(&context);
  238. MD5Update(&context, input, len);
  239. MD5Final(output, &context);
  240. }
  241. /*
  242. * Calculate and store in 'output' the MD5 digest of 'len' bytes at 'input'.
  243. * 'output' must have enough space to hold 16 bytes. If 'chunk' Trigger the
  244. * watchdog every 'chunk_sz' bytes of input processed.
  245. */
  246. void
  247. md5_wd (unsigned char *input, int len, unsigned char output[16],
  248. unsigned int chunk_sz)
  249. {
  250. struct MD5Context context;
  251. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  252. unsigned char *end, *curr;
  253. int chunk;
  254. #endif
  255. MD5Init(&context);
  256. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  257. curr = input;
  258. end = input + len;
  259. while (curr < end) {
  260. chunk = end - curr;
  261. if (chunk > chunk_sz)
  262. chunk = chunk_sz;
  263. MD5Update(&context, curr, chunk);
  264. curr += chunk;
  265. WATCHDOG_RESET ();
  266. }
  267. #else
  268. MD5Update(&context, input, len);
  269. #endif
  270. MD5Final(output, &context);
  271. }