md5.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. *+SNOTICE
  25. *
  26. * $XConsortium: md5.C /main/4 1996/04/21 19:49:12 drk $
  27. *
  28. * RESTRICTED CONFIDENTIAL INFORMATION:
  29. *
  30. * The information in this document is subject to special
  31. * restrictions in a confidential disclosure agreement bertween
  32. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  33. * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
  34. * Sun's specific written approval. This documment and all copies
  35. * and derivative works thereof must be returned or destroyed at
  36. * Sun's request.
  37. *
  38. * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
  39. *
  40. *+ENOTICE
  41. */
  42. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  43. */
  44. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  45. rights reserved.
  46. License to copy and use this software is granted provided that it
  47. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  48. Algorithm" in all material mentioning or referencing this software
  49. or this function.
  50. License is also granted to make and use derivative works provided
  51. that such works are identified as "derived from the RSA Data
  52. Security, Inc. MD5 Message-Digest Algorithm" in all material
  53. mentioning or referencing the derived work.
  54. RSA Data Security, Inc. makes no representations concerning either
  55. the merchantability of this software or the suitability of this
  56. software for any particular purpose. It is provided "as is"
  57. without express or implied warranty of any kind.
  58. These notices must be retained in any copies of any part of this
  59. documentation and/or software.
  60. */
  61. #include <string.h>
  62. #include "md5.h"
  63. /* Constants for MD5Transform routine.
  64. */
  65. #define S11 7
  66. #define S12 12
  67. #define S13 17
  68. #define S14 22
  69. #define S21 5
  70. #define S22 9
  71. #define S23 14
  72. #define S24 20
  73. #define S31 4
  74. #define S32 11
  75. #define S33 16
  76. #define S34 23
  77. #define S41 6
  78. #define S42 10
  79. #define S43 15
  80. #define S44 21
  81. static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  82. static void Encode PROTO_LIST
  83. ((unsigned char *, UINT4 *, unsigned int));
  84. static void Decode PROTO_LIST
  85. ((UINT4 *, unsigned char *, unsigned int));
  86. static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  87. static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  88. static unsigned char PADDING[64] = {
  89. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  92. };
  93. /* F, G, H and I are basic MD5 functions.
  94. */
  95. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  96. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  97. #define H(x, y, z) ((x) ^ (y) ^ (z))
  98. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  99. /* ROTATE_LEFT rotates x left n bits.
  100. */
  101. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  102. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  103. Rotation is separate from addition to prevent recomputation.
  104. */
  105. #define FF(a, b, c, d, x, s, ac) { \
  106. (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  107. (a) = ROTATE_LEFT ((a), (s)); \
  108. (a) += (b); \
  109. }
  110. #define GG(a, b, c, d, x, s, ac) { \
  111. (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  112. (a) = ROTATE_LEFT ((a), (s)); \
  113. (a) += (b); \
  114. }
  115. #define HH(a, b, c, d, x, s, ac) { \
  116. (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  117. (a) = ROTATE_LEFT ((a), (s)); \
  118. (a) += (b); \
  119. }
  120. #define II(a, b, c, d, x, s, ac) { \
  121. (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  122. (a) = ROTATE_LEFT ((a), (s)); \
  123. (a) += (b); \
  124. }
  125. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  126. */
  127. extern "C"
  128. void MD5Init (MD5_CTX *context)
  129. {
  130. context->count[0] = context->count[1] = 0;
  131. /* Load magic initialization constants.
  132. */
  133. context->state[0] = 0x67452301;
  134. context->state[1] = 0xefcdab89;
  135. context->state[2] = 0x98badcfe;
  136. context->state[3] = 0x10325476;
  137. }
  138. /* MD5 block update operation. Continues an MD5 message-digest
  139. operation, processing another message block, and updating the
  140. context.
  141. */
  142. extern "C"
  143. void MD5Update (MD5_CTX *context,
  144. unsigned char *input,
  145. unsigned int inputLen)
  146. {
  147. unsigned int i, index, partLen;
  148. /* Compute number of bytes mod 64 */
  149. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  150. /* Update number of bits */
  151. if ((context->count[0] += ((UINT4)inputLen << 3))
  152. < ((UINT4)inputLen << 3))
  153. context->count[1]++;
  154. context->count[1] += ((UINT4)inputLen >> 29);
  155. partLen = 64 - index;
  156. /* Transform as many times as possible.
  157. */
  158. if (inputLen >= partLen) {
  159. MD5_memcpy
  160. ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  161. MD5Transform (context->state, context->buffer);
  162. for (i = partLen; i + 63 < inputLen; i += 64)
  163. MD5Transform (context->state, &input[i]);
  164. index = 0;
  165. }
  166. else
  167. i = 0;
  168. /* Buffer remaining input */
  169. MD5_memcpy
  170. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  171. inputLen-i);
  172. }
  173. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  174. the message digest and zeroizing the context.
  175. */
  176. extern "C"
  177. void MD5Final (unsigned char digest[16],
  178. MD5_CTX *context)
  179. {
  180. unsigned char bits[8];
  181. unsigned int index, padLen;
  182. /* Save number of bits */
  183. Encode (bits, context->count, 8);
  184. /* Pad out to 56 mod 64.
  185. */
  186. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  187. padLen = (index < 56) ? (56 - index) : (120 - index);
  188. MD5Update (context, PADDING, padLen);
  189. /* Append length (before padding) */
  190. MD5Update (context, bits, 8);
  191. /* Store state in digest */
  192. Encode (digest, context->state, 16);
  193. /* Zeroize sensitive information.
  194. */
  195. MD5_memset ((POINTER)context, 0, sizeof (*context));
  196. }
  197. /* MD5 basic transformation. Transforms state based on block.
  198. */
  199. static void MD5Transform (UINT4 state[4],
  200. unsigned char block[64])
  201. {
  202. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  203. Decode (x, block, 64);
  204. /* Round 1 */
  205. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  206. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  207. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  208. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  209. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  210. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  211. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  212. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  213. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  214. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  215. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  216. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  217. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  218. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  219. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  220. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  221. /* Round 2 */
  222. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  223. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  224. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  225. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  226. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  227. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  228. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  229. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  230. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  231. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  232. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  233. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  234. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  235. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  236. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  237. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  238. /* Round 3 */
  239. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  240. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  241. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  242. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  243. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  244. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  245. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  246. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  247. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  248. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  249. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  250. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  251. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  252. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  253. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  254. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  255. /* Round 4 */
  256. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  257. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  258. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  259. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  260. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  261. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  262. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  263. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  264. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  265. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  266. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  267. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  268. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  269. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  270. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  271. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  272. state[0] += a;
  273. state[1] += b;
  274. state[2] += c;
  275. state[3] += d;
  276. /* Zeroize sensitive information.
  277. */
  278. MD5_memset ((POINTER)x, 0, sizeof (x));
  279. }
  280. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  281. a multiple of 4.
  282. */
  283. static void Encode (unsigned char *output,
  284. UINT4 *input,
  285. unsigned int len)
  286. {
  287. unsigned int i, j;
  288. for (i = 0, j = 0; j < len; i++, j += 4) {
  289. output[j] = (unsigned char)(input[i] & 0xff);
  290. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  291. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  292. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  293. }
  294. }
  295. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  296. a multiple of 4.
  297. */
  298. static void Decode (UINT4 *output,
  299. unsigned char *input,
  300. unsigned int len)
  301. {
  302. unsigned int i, j;
  303. for (i = 0, j = 0; j < len; i++, j += 4)
  304. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  305. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  306. }
  307. /* Note: Replace "for loop" with standard memcpy if possible.
  308. */
  309. static void MD5_memcpy (POINTER output,
  310. POINTER input,
  311. unsigned int len)
  312. {
  313. memcpy(output, input, len);
  314. }
  315. /* Note: Replace "for loop" with standard memset if possible.
  316. */
  317. static void MD5_memset (POINTER output,
  318. int value,
  319. unsigned int len)
  320. {
  321. memset(output, value, len);
  322. }