md5.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* 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-1997, 1999-2001, 2005-2006, 2008-2011 Free Software
  4. Foundation, Inc.
  5. This file is part of the GNU C Library.
  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 3, 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. #include <config.h>
  19. #include "md5.h"
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #if USE_UNLOCKED_IO
  25. # include "unlocked-io.h"
  26. #endif
  27. #ifdef _LIBC
  28. # include <endian.h>
  29. # if __BYTE_ORDER == __BIG_ENDIAN
  30. # define WORDS_BIGENDIAN 1
  31. # endif
  32. /* We need to keep the namespace clean so define the MD5 function
  33. protected using leading __ . */
  34. # define md5_init_ctx __md5_init_ctx
  35. # define md5_process_block __md5_process_block
  36. # define md5_process_bytes __md5_process_bytes
  37. # define md5_finish_ctx __md5_finish_ctx
  38. # define md5_read_ctx __md5_read_ctx
  39. # define md5_stream __md5_stream
  40. # define md5_buffer __md5_buffer
  41. #endif
  42. #ifdef WORDS_BIGENDIAN
  43. # define SWAP(n) \
  44. (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
  45. #else
  46. # define SWAP(n) (n)
  47. #endif
  48. #define BLOCKSIZE 32768
  49. #if BLOCKSIZE % 64 != 0
  50. # error "invalid BLOCKSIZE"
  51. #endif
  52. /* This array contains the bytes used to pad the buffer to the next
  53. 64-byte boundary. (RFC 1321, 3.1: Step 1) */
  54. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  55. /* Initialize structure containing state of computation.
  56. (RFC 1321, 3.3: Step 3) */
  57. void
  58. md5_init_ctx (struct md5_ctx *ctx)
  59. {
  60. ctx->A = 0x67452301;
  61. ctx->B = 0xefcdab89;
  62. ctx->C = 0x98badcfe;
  63. ctx->D = 0x10325476;
  64. ctx->total[0] = ctx->total[1] = 0;
  65. ctx->buflen = 0;
  66. }
  67. /* Copy the 4 byte value from v into the memory location pointed to by *cp,
  68. If your architecture allows unaligned access this is equivalent to
  69. * (uint32_t *) cp = v */
  70. static inline void
  71. set_uint32 (char *cp, uint32_t v)
  72. {
  73. memcpy (cp, &v, sizeof v);
  74. }
  75. /* Put result from CTX in first 16 bytes following RESBUF. The result
  76. must be in little endian byte order. */
  77. void *
  78. md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
  79. {
  80. char *r = resbuf;
  81. set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
  82. set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
  83. set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
  84. set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
  85. return resbuf;
  86. }
  87. /* Process the remaining bytes in the internal buffer and the usual
  88. prolog according to the standard and write the result to RESBUF. */
  89. void *
  90. md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
  91. {
  92. /* Take yet unprocessed bytes into account. */
  93. uint32_t bytes = ctx->buflen;
  94. size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
  95. /* Now count remaining bytes. */
  96. ctx->total[0] += bytes;
  97. if (ctx->total[0] < bytes)
  98. ++ctx->total[1];
  99. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  100. ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
  101. ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
  102. memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
  103. /* Process last bytes. */
  104. md5_process_block (ctx->buffer, size * 4, ctx);
  105. return md5_read_ctx (ctx, resbuf);
  106. }
  107. /* Compute MD5 message digest for bytes read from STREAM. The
  108. resulting message digest number will be written into the 16 bytes
  109. beginning at RESBLOCK. */
  110. int
  111. md5_stream (FILE *stream, void *resblock)
  112. {
  113. struct md5_ctx ctx;
  114. size_t sum;
  115. char *buffer = malloc (BLOCKSIZE + 72);
  116. if (!buffer)
  117. return 1;
  118. /* Initialize the computation context. */
  119. md5_init_ctx (&ctx);
  120. /* Iterate over full file contents. */
  121. while (1)
  122. {
  123. /* We read the file in blocks of BLOCKSIZE bytes. One call of the
  124. computation function processes the whole buffer so that with the
  125. next round of the loop another block can be read. */
  126. size_t n;
  127. sum = 0;
  128. /* Read block. Take care for partial reads. */
  129. while (1)
  130. {
  131. n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
  132. sum += n;
  133. if (sum == BLOCKSIZE)
  134. break;
  135. if (n == 0)
  136. {
  137. /* Check for the error flag IFF N == 0, so that we don't
  138. exit the loop after a partial read due to e.g., EAGAIN
  139. or EWOULDBLOCK. */
  140. if (ferror (stream))
  141. {
  142. free (buffer);
  143. return 1;
  144. }
  145. goto process_partial_block;
  146. }
  147. /* We've read at least one byte, so ignore errors. But always
  148. check for EOF, since feof may be true even though N > 0.
  149. Otherwise, we could end up calling fread after EOF. */
  150. if (feof (stream))
  151. goto process_partial_block;
  152. }
  153. /* Process buffer with BLOCKSIZE bytes. Note that
  154. BLOCKSIZE % 64 == 0
  155. */
  156. md5_process_block (buffer, BLOCKSIZE, &ctx);
  157. }
  158. process_partial_block:
  159. /* Process any remaining bytes. */
  160. if (sum > 0)
  161. md5_process_bytes (buffer, sum, &ctx);
  162. /* Construct result in desired memory. */
  163. md5_finish_ctx (&ctx, resblock);
  164. free (buffer);
  165. return 0;
  166. }
  167. /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
  168. result is always in little endian byte order, so that a byte-wise
  169. output yields to the wanted ASCII representation of the message
  170. digest. */
  171. void *
  172. md5_buffer (const char *buffer, size_t len, void *resblock)
  173. {
  174. struct md5_ctx ctx;
  175. /* Initialize the computation context. */
  176. md5_init_ctx (&ctx);
  177. /* Process whole buffer but last len % 64 bytes. */
  178. md5_process_bytes (buffer, len, &ctx);
  179. /* Put result in desired memory area. */
  180. return md5_finish_ctx (&ctx, resblock);
  181. }
  182. void
  183. md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
  184. {
  185. /* When we already have some bits in our internal buffer concatenate
  186. both inputs first. */
  187. if (ctx->buflen != 0)
  188. {
  189. size_t left_over = ctx->buflen;
  190. size_t add = 128 - left_over > len ? len : 128 - left_over;
  191. memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
  192. ctx->buflen += add;
  193. if (ctx->buflen > 64)
  194. {
  195. md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
  196. ctx->buflen &= 63;
  197. /* The regions in the following copy operation cannot overlap. */
  198. memcpy (ctx->buffer,
  199. &((char *) ctx->buffer)[(left_over + add) & ~63],
  200. ctx->buflen);
  201. }
  202. buffer = (const char *) buffer + add;
  203. len -= add;
  204. }
  205. /* Process available complete blocks. */
  206. if (len >= 64)
  207. {
  208. #if !_STRING_ARCH_unaligned
  209. # define alignof(type) offsetof (struct { char c; type x; }, x)
  210. # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
  211. if (UNALIGNED_P (buffer))
  212. while (len > 64)
  213. {
  214. md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
  215. buffer = (const char *) buffer + 64;
  216. len -= 64;
  217. }
  218. else
  219. #endif
  220. {
  221. md5_process_block (buffer, len & ~63, ctx);
  222. buffer = (const char *) buffer + (len & ~63);
  223. len &= 63;
  224. }
  225. }
  226. /* Move remaining bytes in internal buffer. */
  227. if (len > 0)
  228. {
  229. size_t left_over = ctx->buflen;
  230. memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
  231. left_over += len;
  232. if (left_over >= 64)
  233. {
  234. md5_process_block (ctx->buffer, 64, ctx);
  235. left_over -= 64;
  236. memcpy (ctx->buffer, &ctx->buffer[16], left_over);
  237. }
  238. ctx->buflen = left_over;
  239. }
  240. }
  241. /* These are the four functions used in the four steps of the MD5 algorithm
  242. and defined in the RFC 1321. The first function is a little bit optimized
  243. (as found in Colin Plumbs public domain implementation). */
  244. /* #define FF(b, c, d) ((b & c) | (~b & d)) */
  245. #define FF(b, c, d) (d ^ (b & (c ^ d)))
  246. #define FG(b, c, d) FF (d, b, c)
  247. #define FH(b, c, d) (b ^ c ^ d)
  248. #define FI(b, c, d) (c ^ (b | ~d))
  249. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  250. It is assumed that LEN % 64 == 0. */
  251. void
  252. md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
  253. {
  254. uint32_t correct_words[16];
  255. const uint32_t *words = buffer;
  256. size_t nwords = len / sizeof (uint32_t);
  257. const uint32_t *endp = words + nwords;
  258. uint32_t A = ctx->A;
  259. uint32_t B = ctx->B;
  260. uint32_t C = ctx->C;
  261. uint32_t D = ctx->D;
  262. /* First increment the byte count. RFC 1321 specifies the possible
  263. length of the file up to 2^64 bits. Here we only compute the
  264. number of bytes. Do a double word increment. */
  265. ctx->total[0] += len;
  266. if (ctx->total[0] < len)
  267. ++ctx->total[1];
  268. /* Process all bytes in the buffer with 64 bytes in each round of
  269. the loop. */
  270. while (words < endp)
  271. {
  272. uint32_t *cwp = correct_words;
  273. uint32_t A_save = A;
  274. uint32_t B_save = B;
  275. uint32_t C_save = C;
  276. uint32_t D_save = D;
  277. /* First round: using the given function, the context and a constant
  278. the next context is computed. Because the algorithms processing
  279. unit is a 32-bit word and it is determined to work on words in
  280. little endian byte order we perhaps have to change the byte order
  281. before the computation. To reduce the work for the next steps
  282. we store the swapped words in the array CORRECT_WORDS. */
  283. #define OP(a, b, c, d, s, T) \
  284. do \
  285. { \
  286. a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
  287. ++words; \
  288. CYCLIC (a, s); \
  289. a += b; \
  290. } \
  291. while (0)
  292. /* It is unfortunate that C does not provide an operator for
  293. cyclic rotation. Hope the C compiler is smart enough. */
  294. #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  295. /* Before we start, one word to the strange constants.
  296. They are defined in RFC 1321 as
  297. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  298. Here is an equivalent invocation using Perl:
  299. perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
  300. */
  301. /* Round 1. */
  302. OP (A, B, C, D, 7, 0xd76aa478);
  303. OP (D, A, B, C, 12, 0xe8c7b756);
  304. OP (C, D, A, B, 17, 0x242070db);
  305. OP (B, C, D, A, 22, 0xc1bdceee);
  306. OP (A, B, C, D, 7, 0xf57c0faf);
  307. OP (D, A, B, C, 12, 0x4787c62a);
  308. OP (C, D, A, B, 17, 0xa8304613);
  309. OP (B, C, D, A, 22, 0xfd469501);
  310. OP (A, B, C, D, 7, 0x698098d8);
  311. OP (D, A, B, C, 12, 0x8b44f7af);
  312. OP (C, D, A, B, 17, 0xffff5bb1);
  313. OP (B, C, D, A, 22, 0x895cd7be);
  314. OP (A, B, C, D, 7, 0x6b901122);
  315. OP (D, A, B, C, 12, 0xfd987193);
  316. OP (C, D, A, B, 17, 0xa679438e);
  317. OP (B, C, D, A, 22, 0x49b40821);
  318. /* For the second to fourth round we have the possibly swapped words
  319. in CORRECT_WORDS. Redefine the macro to take an additional first
  320. argument specifying the function to use. */
  321. #undef OP
  322. #define OP(f, a, b, c, d, k, s, T) \
  323. do \
  324. { \
  325. a += f (b, c, d) + correct_words[k] + T; \
  326. CYCLIC (a, s); \
  327. a += b; \
  328. } \
  329. while (0)
  330. /* Round 2. */
  331. OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
  332. OP (FG, D, A, B, C, 6, 9, 0xc040b340);
  333. OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
  334. OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  335. OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
  336. OP (FG, D, A, B, C, 10, 9, 0x02441453);
  337. OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
  338. OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  339. OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
  340. OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
  341. OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
  342. OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
  343. OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
  344. OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  345. OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
  346. OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  347. /* Round 3. */
  348. OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
  349. OP (FH, D, A, B, C, 8, 11, 0x8771f681);
  350. OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
  351. OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
  352. OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
  353. OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  354. OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  355. OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
  356. OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
  357. OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
  358. OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
  359. OP (FH, B, C, D, A, 6, 23, 0x04881d05);
  360. OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
  361. OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
  362. OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  363. OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
  364. /* Round 4. */
  365. OP (FI, A, B, C, D, 0, 6, 0xf4292244);
  366. OP (FI, D, A, B, C, 7, 10, 0x432aff97);
  367. OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
  368. OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
  369. OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
  370. OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  371. OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
  372. OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
  373. OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  374. OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  375. OP (FI, C, D, A, B, 6, 15, 0xa3014314);
  376. OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
  377. OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
  378. OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
  379. OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  380. OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
  381. /* Add the starting values of the context. */
  382. A += A_save;
  383. B += B_save;
  384. C += C_save;
  385. D += D_save;
  386. }
  387. /* Put checksum in context given as argument. */
  388. ctx->A = A;
  389. ctx->B = B;
  390. ctx->C = C;
  391. ctx->D = D;
  392. }