md5.c 9.0 KB

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