md5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* md5.c - Functions to compute MD5 message digest of files or memory blocks
  2. according to the definition of MD5 in RFC 1321 from April 1992.
  3. Copyright (C) 1995, 1996, 2011 Free Software Foundation, Inc.
  4. NOTE: This source is derived from an old version taken from the GNU C
  5. Library (glibc).
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <sys/types.h>
  22. #if STDC_HEADERS || defined _LIBC
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #else
  26. # ifndef HAVE_MEMCPY
  27. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  28. # endif
  29. #endif
  30. #include "ansidecl.h"
  31. #include "md5.h"
  32. #ifdef _LIBC
  33. # include <endian.h>
  34. # if __BYTE_ORDER == __BIG_ENDIAN
  35. # define WORDS_BIGENDIAN 1
  36. # endif
  37. #endif
  38. #ifdef WORDS_BIGENDIAN
  39. # define SWAP(n) \
  40. (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
  41. #else
  42. # define SWAP(n) (n)
  43. #endif
  44. /* This array contains the bytes used to pad the buffer to the next
  45. 64-byte boundary. (RFC 1321, 3.1: Step 1) */
  46. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  47. /* Initialize structure containing state of computation.
  48. (RFC 1321, 3.3: Step 3) */
  49. void
  50. md5_init_ctx (struct md5_ctx *ctx)
  51. {
  52. ctx->A = (md5_uint32) 0x67452301;
  53. ctx->B = (md5_uint32) 0xefcdab89;
  54. ctx->C = (md5_uint32) 0x98badcfe;
  55. ctx->D = (md5_uint32) 0x10325476;
  56. ctx->total[0] = ctx->total[1] = 0;
  57. ctx->buflen = 0;
  58. }
  59. /* Put result from CTX in first 16 bytes following RESBUF. The result
  60. must be in little endian byte order.
  61. IMPORTANT: RESBUF may not be aligned as strongly as MD5_UNIT32 so we
  62. put things in a local (aligned) buffer first, then memcpy into RESBUF. */
  63. void *
  64. md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
  65. {
  66. md5_uint32 buffer[4];
  67. buffer[0] = SWAP (ctx->A);
  68. buffer[1] = SWAP (ctx->B);
  69. buffer[2] = SWAP (ctx->C);
  70. buffer[3] = SWAP (ctx->D);
  71. memcpy (resbuf, buffer, 16);
  72. return resbuf;
  73. }
  74. /* Process the remaining bytes in the internal buffer and the usual
  75. prolog according to the standard and write the result to RESBUF.
  76. IMPORTANT: On some systems it is required that RESBUF is correctly
  77. aligned for a 32 bits value. */
  78. void *
  79. md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
  80. {
  81. /* Take yet unprocessed bytes into account. */
  82. md5_uint32 bytes = ctx->buflen;
  83. md5_uint32 swap_bytes;
  84. size_t pad;
  85. /* Now count remaining bytes. */
  86. ctx->total[0] += bytes;
  87. if (ctx->total[0] < bytes)
  88. ++ctx->total[1];
  89. pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
  90. memcpy (&ctx->buffer[bytes], fillbuf, pad);
  91. /* Put the 64-bit file length in *bits* at the end of the buffer.
  92. Use memcpy to avoid aliasing problems. On most systems, this
  93. will be optimized away to the same code. */
  94. swap_bytes = SWAP (ctx->total[0] << 3);
  95. memcpy (&ctx->buffer[bytes + pad], &swap_bytes, sizeof (swap_bytes));
  96. swap_bytes = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
  97. memcpy (&ctx->buffer[bytes + pad + 4], &swap_bytes, sizeof (swap_bytes));
  98. /* Process last bytes. */
  99. md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
  100. return md5_read_ctx (ctx, resbuf);
  101. }
  102. /* Compute MD5 message digest for bytes read from STREAM. The
  103. resulting message digest number will be written into the 16 bytes
  104. beginning at RESBLOCK. */
  105. int
  106. md5_stream (FILE *stream, void *resblock)
  107. {
  108. /* Important: BLOCKSIZE must be a multiple of 64. */
  109. #define BLOCKSIZE 4096
  110. struct md5_ctx ctx;
  111. char buffer[BLOCKSIZE + 72];
  112. size_t sum;
  113. /* Initialize the computation context. */
  114. md5_init_ctx (&ctx);
  115. /* Iterate over full file contents. */
  116. while (1)
  117. {
  118. /* We read the file in blocks of BLOCKSIZE bytes. One call of the
  119. computation function processes the whole buffer so that with the
  120. next round of the loop another block can be read. */
  121. size_t n;
  122. sum = 0;
  123. /* Read block. Take care for partial reads. */
  124. do
  125. {
  126. n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
  127. sum += n;
  128. }
  129. while (sum < BLOCKSIZE && n != 0);
  130. if (n == 0 && ferror (stream))
  131. return 1;
  132. /* If end of file is reached, end the loop. */
  133. if (n == 0)
  134. break;
  135. /* Process buffer with BLOCKSIZE bytes. Note that
  136. BLOCKSIZE % 64 == 0
  137. */
  138. md5_process_block (buffer, BLOCKSIZE, &ctx);
  139. }
  140. /* Add the last bytes if necessary. */
  141. if (sum > 0)
  142. md5_process_bytes (buffer, sum, &ctx);
  143. /* Construct result in desired memory. */
  144. md5_finish_ctx (&ctx, resblock);
  145. return 0;
  146. }
  147. /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
  148. result is always in little endian byte order, so that a byte-wise
  149. output yields to the wanted ASCII representation of the message
  150. digest. */
  151. void *
  152. md5_buffer (const char *buffer, size_t len, void *resblock)
  153. {
  154. struct md5_ctx ctx;
  155. /* Initialize the computation context. */
  156. md5_init_ctx (&ctx);
  157. /* Process whole buffer but last len % 64 bytes. */
  158. md5_process_bytes (buffer, len, &ctx);
  159. /* Put result in desired memory area. */
  160. return md5_finish_ctx (&ctx, resblock);
  161. }
  162. void
  163. md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
  164. {
  165. /* When we already have some bits in our internal buffer concatenate
  166. both inputs first. */
  167. if (ctx->buflen != 0)
  168. {
  169. size_t left_over = ctx->buflen;
  170. size_t add = 128 - left_over > len ? len : 128 - left_over;
  171. memcpy (&ctx->buffer[left_over], buffer, add);
  172. ctx->buflen += add;
  173. if (left_over + add > 64)
  174. {
  175. md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx);
  176. /* The regions in the following copy operation cannot overlap. */
  177. memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
  178. (left_over + add) & 63);
  179. ctx->buflen = (left_over + add) & 63;
  180. }
  181. buffer = (const void *) ((const char *) buffer + add);
  182. len -= add;
  183. }
  184. /* Process available complete blocks. */
  185. if (len > 64)
  186. {
  187. #if !_STRING_ARCH_unaligned
  188. /* To check alignment gcc has an appropriate operator. Other
  189. compilers don't. */
  190. # if __GNUC__ >= 2
  191. # define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
  192. # else
  193. # define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
  194. # endif
  195. if (UNALIGNED_P (buffer))
  196. while (len > 64)
  197. {
  198. memcpy (ctx->buffer, buffer, 64);
  199. md5_process_block (ctx->buffer, 64, ctx);
  200. buffer = (const char *) buffer + 64;
  201. len -= 64;
  202. }
  203. else
  204. #endif
  205. {
  206. md5_process_block (buffer, len & ~63, ctx);
  207. buffer = (const void *) ((const char *) buffer + (len & ~63));
  208. len &= 63;
  209. }
  210. }
  211. /* Move remaining bytes in internal buffer. */
  212. if (len > 0)
  213. {
  214. memcpy (ctx->buffer, buffer, len);
  215. ctx->buflen = len;
  216. }
  217. }
  218. /* These are the four functions used in the four steps of the MD5 algorithm
  219. and defined in the RFC 1321. The first function is a little bit optimized
  220. (as found in Colin Plumbs public domain implementation). */
  221. /* #define FF(b, c, d) ((b & c) | (~b & d)) */
  222. #define FF(b, c, d) (d ^ (b & (c ^ d)))
  223. #define FG(b, c, d) FF (d, b, c)
  224. #define FH(b, c, d) (b ^ c ^ d)
  225. #define FI(b, c, d) (c ^ (b | ~d))
  226. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  227. It is assumed that LEN % 64 == 0. */
  228. void
  229. md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
  230. {
  231. md5_uint32 correct_words[16];
  232. const md5_uint32 *words = (const md5_uint32 *) buffer;
  233. size_t nwords = len / sizeof (md5_uint32);
  234. const md5_uint32 *endp = words + nwords;
  235. md5_uint32 A = ctx->A;
  236. md5_uint32 B = ctx->B;
  237. md5_uint32 C = ctx->C;
  238. md5_uint32 D = ctx->D;
  239. /* First increment the byte count. RFC 1321 specifies the possible
  240. length of the file up to 2^64 bits. Here we only compute the
  241. number of bytes. Do a double word increment. */
  242. ctx->total[0] += len;
  243. ctx->total[1] += ((len >> 31) >> 1) + (ctx->total[0] < len);
  244. /* Process all bytes in the buffer with 64 bytes in each round of
  245. the loop. */
  246. while (words < endp)
  247. {
  248. md5_uint32 *cwp = correct_words;
  249. md5_uint32 A_save = A;
  250. md5_uint32 B_save = B;
  251. md5_uint32 C_save = C;
  252. md5_uint32 D_save = D;
  253. /* First round: using the given function, the context and a constant
  254. the next context is computed. Because the algorithms processing
  255. unit is a 32-bit word and it is determined to work on words in
  256. little endian byte order we perhaps have to change the byte order
  257. before the computation. To reduce the work for the next steps
  258. we store the swapped words in the array CORRECT_WORDS. */
  259. #define OP(a, b, c, d, s, T) \
  260. do \
  261. { \
  262. a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
  263. ++words; \
  264. CYCLIC (a, s); \
  265. a += b; \
  266. } \
  267. while (0)
  268. /* It is unfortunate that C does not provide an operator for
  269. cyclic rotation. Hope the C compiler is smart enough. */
  270. #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  271. /* Before we start, one word to the strange constants.
  272. They are defined in RFC 1321 as
  273. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  274. */
  275. /* Round 1. */
  276. OP (A, B, C, D, 7, (md5_uint32) 0xd76aa478);
  277. OP (D, A, B, C, 12, (md5_uint32) 0xe8c7b756);
  278. OP (C, D, A, B, 17, (md5_uint32) 0x242070db);
  279. OP (B, C, D, A, 22, (md5_uint32) 0xc1bdceee);
  280. OP (A, B, C, D, 7, (md5_uint32) 0xf57c0faf);
  281. OP (D, A, B, C, 12, (md5_uint32) 0x4787c62a);
  282. OP (C, D, A, B, 17, (md5_uint32) 0xa8304613);
  283. OP (B, C, D, A, 22, (md5_uint32) 0xfd469501);
  284. OP (A, B, C, D, 7, (md5_uint32) 0x698098d8);
  285. OP (D, A, B, C, 12, (md5_uint32) 0x8b44f7af);
  286. OP (C, D, A, B, 17, (md5_uint32) 0xffff5bb1);
  287. OP (B, C, D, A, 22, (md5_uint32) 0x895cd7be);
  288. OP (A, B, C, D, 7, (md5_uint32) 0x6b901122);
  289. OP (D, A, B, C, 12, (md5_uint32) 0xfd987193);
  290. OP (C, D, A, B, 17, (md5_uint32) 0xa679438e);
  291. OP (B, C, D, A, 22, (md5_uint32) 0x49b40821);
  292. /* For the second to fourth round we have the possibly swapped words
  293. in CORRECT_WORDS. Redefine the macro to take an additional first
  294. argument specifying the function to use. */
  295. #undef OP
  296. #define OP(a, b, c, d, k, s, T) \
  297. do \
  298. { \
  299. a += FX (b, c, d) + correct_words[k] + T; \
  300. CYCLIC (a, s); \
  301. a += b; \
  302. } \
  303. while (0)
  304. #define FX(b, c, d) FG (b, c, d)
  305. /* Round 2. */
  306. OP (A, B, C, D, 1, 5, (md5_uint32) 0xf61e2562);
  307. OP (D, A, B, C, 6, 9, (md5_uint32) 0xc040b340);
  308. OP (C, D, A, B, 11, 14, (md5_uint32) 0x265e5a51);
  309. OP (B, C, D, A, 0, 20, (md5_uint32) 0xe9b6c7aa);
  310. OP (A, B, C, D, 5, 5, (md5_uint32) 0xd62f105d);
  311. OP (D, A, B, C, 10, 9, (md5_uint32) 0x02441453);
  312. OP (C, D, A, B, 15, 14, (md5_uint32) 0xd8a1e681);
  313. OP (B, C, D, A, 4, 20, (md5_uint32) 0xe7d3fbc8);
  314. OP (A, B, C, D, 9, 5, (md5_uint32) 0x21e1cde6);
  315. OP (D, A, B, C, 14, 9, (md5_uint32) 0xc33707d6);
  316. OP (C, D, A, B, 3, 14, (md5_uint32) 0xf4d50d87);
  317. OP (B, C, D, A, 8, 20, (md5_uint32) 0x455a14ed);
  318. OP (A, B, C, D, 13, 5, (md5_uint32) 0xa9e3e905);
  319. OP (D, A, B, C, 2, 9, (md5_uint32) 0xfcefa3f8);
  320. OP (C, D, A, B, 7, 14, (md5_uint32) 0x676f02d9);
  321. OP (B, C, D, A, 12, 20, (md5_uint32) 0x8d2a4c8a);
  322. #undef FX
  323. #define FX(b, c, d) FH (b, c, d)
  324. /* Round 3. */
  325. OP (A, B, C, D, 5, 4, (md5_uint32) 0xfffa3942);
  326. OP (D, A, B, C, 8, 11, (md5_uint32) 0x8771f681);
  327. OP (C, D, A, B, 11, 16, (md5_uint32) 0x6d9d6122);
  328. OP (B, C, D, A, 14, 23, (md5_uint32) 0xfde5380c);
  329. OP (A, B, C, D, 1, 4, (md5_uint32) 0xa4beea44);
  330. OP (D, A, B, C, 4, 11, (md5_uint32) 0x4bdecfa9);
  331. OP (C, D, A, B, 7, 16, (md5_uint32) 0xf6bb4b60);
  332. OP (B, C, D, A, 10, 23, (md5_uint32) 0xbebfbc70);
  333. OP (A, B, C, D, 13, 4, (md5_uint32) 0x289b7ec6);
  334. OP (D, A, B, C, 0, 11, (md5_uint32) 0xeaa127fa);
  335. OP (C, D, A, B, 3, 16, (md5_uint32) 0xd4ef3085);
  336. OP (B, C, D, A, 6, 23, (md5_uint32) 0x04881d05);
  337. OP (A, B, C, D, 9, 4, (md5_uint32) 0xd9d4d039);
  338. OP (D, A, B, C, 12, 11, (md5_uint32) 0xe6db99e5);
  339. OP (C, D, A, B, 15, 16, (md5_uint32) 0x1fa27cf8);
  340. OP (B, C, D, A, 2, 23, (md5_uint32) 0xc4ac5665);
  341. #undef FX
  342. #define FX(b, c, d) FI (b, c, d)
  343. /* Round 4. */
  344. OP (A, B, C, D, 0, 6, (md5_uint32) 0xf4292244);
  345. OP (D, A, B, C, 7, 10, (md5_uint32) 0x432aff97);
  346. OP (C, D, A, B, 14, 15, (md5_uint32) 0xab9423a7);
  347. OP (B, C, D, A, 5, 21, (md5_uint32) 0xfc93a039);
  348. OP (A, B, C, D, 12, 6, (md5_uint32) 0x655b59c3);
  349. OP (D, A, B, C, 3, 10, (md5_uint32) 0x8f0ccc92);
  350. OP (C, D, A, B, 10, 15, (md5_uint32) 0xffeff47d);
  351. OP (B, C, D, A, 1, 21, (md5_uint32) 0x85845dd1);
  352. OP (A, B, C, D, 8, 6, (md5_uint32) 0x6fa87e4f);
  353. OP (D, A, B, C, 15, 10, (md5_uint32) 0xfe2ce6e0);
  354. OP (C, D, A, B, 6, 15, (md5_uint32) 0xa3014314);
  355. OP (B, C, D, A, 13, 21, (md5_uint32) 0x4e0811a1);
  356. OP (A, B, C, D, 4, 6, (md5_uint32) 0xf7537e82);
  357. OP (D, A, B, C, 11, 10, (md5_uint32) 0xbd3af235);
  358. OP (C, D, A, B, 2, 15, (md5_uint32) 0x2ad7d2bb);
  359. OP (B, C, D, A, 9, 21, (md5_uint32) 0xeb86d391);
  360. /* Add the starting values of the context. */
  361. A += A_save;
  362. B += B_save;
  363. C += C_save;
  364. D += D_save;
  365. }
  366. /* Put checksum in context given as argument. */
  367. ctx->A = A;
  368. ctx->B = B;
  369. ctx->C = C;
  370. ctx->D = D;
  371. }