md5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2009 by Thermotemp GmbH. All rights reserved.
  3. *
  4. * This code is based on a public domain implementation of md5.c by
  5. * Colin Plumb, June 1993
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THERMOTEMP GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THERMOTEMP
  24. * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file gorp/hashes/md5.c
  37. * \brief MD5 Hash functions.
  38. *
  39. * \verbatim
  40. *
  41. * $Log$
  42. * Revision 1.3 2009/03/08 20:19:10 haraldkipp
  43. * Replaced const by const to make it compile with ICCAVR.
  44. *
  45. * Revision 1.2 2009/03/06 23:51:38 olereinhardt
  46. * Fixed minor compile bugs
  47. *
  48. * Revision 1.1 2009/03/06 17:00:33 olereinhardt
  49. * Added md5 routines, initial checkin
  50. *
  51. *
  52. * \endverbatim
  53. */
  54. #include <stdint.h>
  55. #include <string.h>
  56. #include <gorp/md5.h>
  57. /*!
  58. * \addtogroup xgHashes
  59. */
  60. /*@{*/
  61. #ifndef __BIG_ENDIAN__
  62. #define byteReverse(buf, len) /* Nothing */
  63. #else
  64. /*
  65. * Note: this code is harmless on little-endian machines.
  66. */
  67. static void byteReverse(uint8_t *buf, size_t longs)
  68. {
  69. uint32_t t;
  70. do {
  71. t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | ((unsigned) buf[1] << 8 | buf[0]);
  72. *(uint32_t *) buf = t;
  73. buf += 4;
  74. } while (--longs);
  75. }
  76. #endif
  77. /* The four core functions - F1 is optimized somewhat */
  78. /* #define F1(x, y, z) (x & y | ~x & z) */
  79. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  80. #define F2(x, y, z) F1(z, x, y)
  81. #define F3(x, y, z) (x ^ y ^ z)
  82. #define F4(x, y, z) (y ^ (x | ~z))
  83. /* This is the central step in the MD5 algorithm. */
  84. #define MD5STEP(f, w, x, y, z, data, s) \
  85. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  86. /*
  87. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  88. * reflect the addition of 16 longwords of new data. MD5Update blocks
  89. * the data and converts bytes into longwords for this routine.
  90. */
  91. static void NutMD5Transform(uint32_t buf[4], uint32_t const in[16])
  92. {
  93. register uint32_t a, b, c, d;
  94. a = buf[0];
  95. b = buf[1];
  96. c = buf[2];
  97. d = buf[3];
  98. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  99. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  100. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  101. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  102. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  103. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  104. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  105. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  106. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  107. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  108. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  109. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  110. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  111. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  112. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  113. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  114. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  115. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  116. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  117. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  118. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  119. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  120. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  121. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  122. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  123. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  124. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  125. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  126. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  127. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  128. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  129. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  130. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  131. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  132. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  133. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  134. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  135. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  136. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  137. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  138. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  139. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  140. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  141. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  142. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  143. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  144. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  145. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  146. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  147. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  148. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  149. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  150. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  151. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  152. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  153. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  154. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  155. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  156. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  157. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  158. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  159. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  160. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  161. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  162. buf[0] += a;
  163. buf[1] += b;
  164. buf[2] += c;
  165. buf[3] += d;
  166. }
  167. /*!
  168. * \brief Start MD5 accumulation.
  169. *
  170. * Start MD5 accumulation, set bit count to 0 and buffer to mysterious
  171. * initialization constants. Call this function to initialize every new
  172. * MD5 calculation.
  173. *
  174. * \param context Points to the md5 context buffer.
  175. */
  176. void NutMD5Init(MD5CONTEXT *context)
  177. {
  178. context->buf[0] = 0x67452301;
  179. context->buf[1] = 0xefcdab89;
  180. context->buf[2] = 0x98badcfe;
  181. context->buf[3] = 0x10325476;
  182. context->bits[0] = 0;
  183. context->bits[1] = 0;
  184. }
  185. /*!
  186. * \brief Update MD5 context
  187. *
  188. * Update context to reflect the concatenation of another data buffer.
  189. *
  190. * \param context Points to the md5 context buffer.
  191. * \param buf Points to the data buffer
  192. * \param len Length of the data buffer
  193. */
  194. void NutMD5Update(MD5CONTEXT *context, uint8_t const *buf, uint32_t len)
  195. {
  196. uint32_t t;
  197. /* Update bitcount */
  198. t = context->bits[0];
  199. if ((context->bits[0] = t + ((uint32_t) len << 3)) < t) {
  200. context->bits[1]++; /* Carry from low to high */
  201. }
  202. context->bits[1] += len >> 29;
  203. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  204. /* Handle any leading odd-sized chunks */
  205. if (t) {
  206. uint8_t *p = (uint8_t *) context->in + t;
  207. t = 64 - t;
  208. if (len < t) {
  209. memcpy(p, buf, len);
  210. return;
  211. }
  212. memcpy(p, buf, t);
  213. byteReverse(context->in, 16);
  214. NutMD5Transform(context->buf, (uint32_t *) context->in);
  215. buf += t;
  216. len -= t;
  217. }
  218. /* Process data in 64-byte chunks */
  219. while (len >= 64) {
  220. memcpy(context->in, buf, 64);
  221. byteReverse(context->in, 16);
  222. NutMD5Transform(context->buf, (uint32_t *) context->in);
  223. buf += 64;
  224. len -= 64;
  225. }
  226. /* Handle any remaining bytes of data. */
  227. memcpy(context->in, buf, len);
  228. }
  229. /*
  230. */
  231. /*!
  232. * \brief Final wrapup, calculate MD5 digest
  233. *
  234. * Final wrapup - pad to 64-byte boundary with the bit pattern
  235. * 1 0* (64-bit count of bits processed, MSB-first)
  236. * Fill in the digest into digest buffer
  237. *
  238. * \param context Points to the md5 context buffer.
  239. * \param digest Points to the digest buffer, which must be 16 bytes long
  240. */
  241. void NutMD5Final(MD5CONTEXT *context, uint8_t digest[16])
  242. {
  243. unsigned int count;
  244. uint8_t *p;
  245. /* Compute number of bytes mod 64 */
  246. count = (context->bits[0] >> 3) & 0x3F;
  247. /* Set the first char of padding to 0x80. This is safe since there is
  248. always at least one byte free */
  249. p = context->in + count;
  250. *p++ = 0x80;
  251. /* Bytes of padding needed to make 64 bytes */
  252. count = 64 - 1 - count;
  253. /* Pad out to 56 mod 64 */
  254. if (count < 8) {
  255. /* Two lots of padding: Pad the first block to 64 bytes */
  256. memset(p, 0, count);
  257. byteReverse(context->in, 16);
  258. NutMD5Transform(context->buf, (uint32_t *) context->in);
  259. /* Now fill the next block with 56 bytes */
  260. memset(context->in, 0, 56);
  261. } else {
  262. /* Pad block to 56 bytes */
  263. memset(p, 0, count - 8);
  264. }
  265. byteReverse(context->in, 14);
  266. /* Append length in bits and transform */
  267. memcpy(context->in + 56, &context->bits[0], sizeof(context->bits[0]));
  268. memcpy(context->in + 60, &context->bits[1], sizeof(context->bits[1]));
  269. NutMD5Transform(context->buf, (uint32_t *) context->in);
  270. byteReverse((unsigned char *) context->buf, 4);
  271. memcpy(digest, context->buf, 16);
  272. memset(context, 0, sizeof(MD5CONTEXT)); /* In case it's sensitive */
  273. }
  274. /*@}*/