mvMD5.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*******************************************************************************
  2. Copyright (C) Marvell International Ltd. and its affiliates
  3. This software file (the "File") is owned and distributed by Marvell
  4. International Ltd. and/or its affiliates ("Marvell") under the following
  5. alternative licensing terms. Once you have made an election to distribute the
  6. File under one of the following license alternatives, please (i) delete this
  7. introductory statement regarding license alternatives, (ii) delete the two
  8. license alternatives that you have not elected to use and (iii) preserve the
  9. Marvell copyright notice above.
  10. ********************************************************************************
  11. Marvell Commercial License Option
  12. If you received this File from Marvell and you have entered into a commercial
  13. license agreement (a "Commercial License") with Marvell, the File is licensed
  14. to you under the terms of the applicable Commercial License.
  15. ********************************************************************************
  16. Marvell GPL License Option
  17. If you received this File from Marvell, you may opt to use, redistribute and/or
  18. modify this File in accordance with the terms and conditions of the General
  19. Public License Version 2, June 1991 (the "GPL License"), a copy of which is
  20. available along with the File in the license.txt file or by writing to the Free
  21. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
  22. on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
  23. THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
  25. DISCLAIMED. The GPL License provides additional details about this warranty
  26. disclaimer.
  27. ********************************************************************************
  28. Marvell BSD License Option
  29. If you received this File from Marvell, you may opt to use, redistribute and/or
  30. modify this File under the following licensing terms.
  31. Redistribution and use in source and binary forms, with or without modification,
  32. are permitted provided that the following conditions are met:
  33. * Redistributions of source code must retain the above copyright notice,
  34. this list of conditions and the following disclaimer.
  35. * Redistributions in binary form must reproduce the above copyright
  36. notice, this list of conditions and the following disclaimer in the
  37. documentation and/or other materials provided with the distribution.
  38. * Neither the name of Marvell nor the names of its contributors may be
  39. used to endorse or promote products derived from this software without
  40. specific prior written permission.
  41. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  42. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  43. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  45. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  48. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *******************************************************************************/
  52. #include "mvOs.h"
  53. #include "mvMD5.h"
  54. static void mvMD5Transform(MV_U32 buf[4], MV_U32 const in[MV_MD5_MAC_LEN]);
  55. #ifdef MV_CPU_LE
  56. #define mvByteReverse(buf, len) /* Nothing */
  57. #else
  58. static void mvByteReverse(unsigned char *buf, unsigned longs);
  59. /*
  60. * Note: this code is harmless on little-endian machines.
  61. */
  62. static void mvByteReverse(unsigned char *buf, unsigned longs)
  63. {
  64. MV_U32 t;
  65. do
  66. {
  67. t = (MV_U32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
  68. ((unsigned) buf[1] << 8 | buf[0]);
  69. *(MV_U32 *) buf = t;
  70. buf += 4;
  71. } while (--longs);
  72. }
  73. #endif
  74. /*
  75. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  76. * initialization constants.
  77. */
  78. void mvMD5Init(MV_MD5_CONTEXT *ctx)
  79. {
  80. ctx->buf[0] = 0x67452301;
  81. ctx->buf[1] = 0xefcdab89;
  82. ctx->buf[2] = 0x98badcfe;
  83. ctx->buf[3] = 0x10325476;
  84. ctx->bits[0] = 0;
  85. ctx->bits[1] = 0;
  86. }
  87. /*
  88. * Update context to reflect the concatenation of another buffer full
  89. * of bytes.
  90. */
  91. void mvMD5Update(MV_MD5_CONTEXT *ctx, unsigned char const *buf, unsigned len)
  92. {
  93. MV_U32 t;
  94. /* Update bitcount */
  95. t = ctx->bits[0];
  96. if ((ctx->bits[0] = t + ((MV_U32) len << 3)) < t)
  97. ctx->bits[1]++; /* Carry from low to high */
  98. ctx->bits[1] += len >> 29;
  99. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  100. /* Handle any leading odd-sized chunks */
  101. if (t)
  102. {
  103. unsigned char *p = (unsigned char *) ctx->in + t;
  104. t = 64 - t;
  105. if (len < t)
  106. {
  107. memcpy(p, buf, len);
  108. return;
  109. }
  110. memcpy(p, buf, t);
  111. mvByteReverse(ctx->in, MV_MD5_MAC_LEN);
  112. mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in);
  113. buf += t;
  114. len -= t;
  115. }
  116. /* Process data in 64-byte chunks */
  117. while (len >= 64)
  118. {
  119. memcpy(ctx->in, buf, 64);
  120. mvByteReverse(ctx->in, MV_MD5_MAC_LEN);
  121. mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in);
  122. buf += 64;
  123. len -= 64;
  124. }
  125. /* Handle any remaining bytes of data. */
  126. memcpy(ctx->in, buf, len);
  127. }
  128. /*
  129. * Final wrapup - pad to 64-byte boundary with the bit pattern
  130. * 1 0* (64-bit count of bits processed, MSB-first)
  131. */
  132. void mvMD5Final(unsigned char digest[MV_MD5_MAC_LEN], MV_MD5_CONTEXT *ctx)
  133. {
  134. unsigned count;
  135. unsigned char *p;
  136. /* Compute number of bytes mod 64 */
  137. count = (ctx->bits[0] >> 3) & 0x3F;
  138. /* Set the first char of padding to 0x80. This is safe since there is
  139. always at least one byte free */
  140. p = ctx->in + count;
  141. *p++ = 0x80;
  142. /* Bytes of padding needed to make 64 bytes */
  143. count = 64 - 1 - count;
  144. /* Pad out to 56 mod 64 */
  145. if (count < 8)
  146. {
  147. /* Two lots of padding: Pad the first block to 64 bytes */
  148. memset(p, 0, count);
  149. mvByteReverse(ctx->in, MV_MD5_MAC_LEN);
  150. mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in);
  151. /* Now fill the next block with 56 bytes */
  152. memset(ctx->in, 0, 56);
  153. }
  154. else
  155. {
  156. /* Pad block to 56 bytes */
  157. memset(p, 0, count - 8);
  158. }
  159. mvByteReverse(ctx->in, 14);
  160. /* Append length in bits and transform */
  161. ((MV_U32 *) ctx->in)[14] = ctx->bits[0];
  162. ((MV_U32 *) ctx->in)[15] = ctx->bits[1];
  163. mvMD5Transform(ctx->buf, (MV_U32 *) ctx->in);
  164. mvByteReverse((unsigned char *) ctx->buf, 4);
  165. memcpy(digest, ctx->buf, MV_MD5_MAC_LEN);
  166. memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
  167. }
  168. /* The four core functions - F1 is optimized somewhat */
  169. /* #define F1(x, y, z) (x & y | ~x & z) */
  170. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  171. #define F2(x, y, z) F1(z, x, y)
  172. #define F3(x, y, z) (x ^ y ^ z)
  173. #define F4(x, y, z) (y ^ (x | ~z))
  174. /* This is the central step in the MD5 algorithm. */
  175. #define MD5STEP(f, w, x, y, z, data, s) \
  176. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  177. /*
  178. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  179. * reflect the addition of 16 longwords of new data. MD5Update blocks
  180. * the data and converts bytes into longwords for this routine.
  181. */
  182. static void mvMD5Transform(MV_U32 buf[4], MV_U32 const in[MV_MD5_MAC_LEN])
  183. {
  184. register MV_U32 a, b, c, d;
  185. a = buf[0];
  186. b = buf[1];
  187. c = buf[2];
  188. d = buf[3];
  189. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  190. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  191. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  192. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  193. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  194. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  195. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  196. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  197. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  198. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  199. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  200. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  201. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  202. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  203. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  204. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  205. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  206. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  207. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  208. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  209. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  210. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  211. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  212. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  213. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  214. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  215. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  216. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  217. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  218. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  219. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  220. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  221. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  222. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  223. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  224. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  225. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  226. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  227. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  228. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  229. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  230. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  231. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  232. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  233. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  234. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  235. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  236. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  237. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  238. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  239. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  240. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  241. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  242. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  243. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  244. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  245. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  246. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  247. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  248. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  249. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  250. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  251. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  252. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  253. buf[0] += a;
  254. buf[1] += b;
  255. buf[2] += c;
  256. buf[3] += d;
  257. }
  258. void mvMD5(unsigned char const *buf, unsigned len, unsigned char* digest)
  259. {
  260. MV_MD5_CONTEXT ctx;
  261. mvMD5Init(&ctx);
  262. mvMD5Update(&ctx, buf, len);
  263. mvMD5Final(digest, &ctx);
  264. }
  265. void mvHmacMd5(unsigned char const* text, int text_len,
  266. unsigned char const* key, int key_len,
  267. unsigned char* digest)
  268. {
  269. int i;
  270. MV_MD5_CONTEXT ctx;
  271. unsigned char k_ipad[64+1]; /* inner padding - key XORd with ipad */
  272. unsigned char k_opad[64+1]; /* outer padding - key XORd with opad */
  273. /* start out by storing key in pads */
  274. memset(k_ipad, 0, 64);
  275. memcpy(k_ipad, key, key_len);
  276. memset(k_opad, 0, 64);
  277. memcpy(k_opad, key, key_len);
  278. /* XOR key with ipad and opad values */
  279. for (i=0; i<64; i++)
  280. {
  281. k_ipad[i] ^= 0x36;
  282. k_opad[i] ^= 0x5c;
  283. }
  284. /* perform inner MD5 */
  285. mvMD5Init(&ctx); /* init ctx for 1st pass */
  286. mvMD5Update(&ctx, k_ipad, 64); /* start with inner pad */
  287. mvMD5Update(&ctx, text, text_len); /* then text of datagram */
  288. mvMD5Final(digest, &ctx); /* finish up 1st pass */
  289. /* perform outer MD5 */
  290. mvMD5Init(&ctx); /* init ctx for 2nd pass */
  291. mvMD5Update(&ctx, k_opad, 64); /* start with outer pad */
  292. mvMD5Update(&ctx, digest, 16); /* then results of 1st hash */
  293. mvMD5Final(digest, &ctx); /* finish up 2nd pass */
  294. }