md5.c 13 KB

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