md5.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <sys/types.h>
  21. #if STDC_HEADERS || defined _LIBC
  22. # include <stdlib.h>
  23. # include <string.h>
  24. #else
  25. # ifndef HAVE_MEMCPY
  26. # define memcpy(d, s, n) (bcopy ((s), (d), (n)), (d))
  27. # endif
  28. #endif
  29. #include "md5.h"
  30. #ifdef _LIBC
  31. # include <endian.h>
  32. # if __BYTE_ORDER == __BIG_ENDIAN
  33. # define WORDS_BIGENDIAN 1
  34. # endif
  35. /* We need to keep the namespace clean so define the MD5 function
  36. protected using leading __ . */
  37. # define md5_init_ctx __md5_init_ctx
  38. # define md5_process_bytes __md5_process_bytes
  39. # define md5_finish_ctx __md5_finish_ctx
  40. # define md5_read_ctx __md5_read_ctx
  41. # define md5_stream __md5_stream
  42. # define md5_buffer __md5_buffer
  43. #endif
  44. #ifdef WORDS_BIGENDIAN
  45. # define SWAP(n) \
  46. (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
  47. #else
  48. # define SWAP(n) (n)
  49. #endif
  50. /* This array contains the bytes used to pad the buffer to the next
  51. 64-byte boundary. (RFC 1321, 3.1: Step 1) */
  52. static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
  53. /* Initialize structure containing state of computation.
  54. (RFC 1321, 3.3: Step 3) */
  55. void
  56. md5_init_ctx (struct md5_ctx *ctx)
  57. {
  58. ctx->A = 0x67452301;
  59. ctx->B = 0xefcdab89;
  60. ctx->C = 0x98badcfe;
  61. ctx->D = 0x10325476;
  62. ctx->total[0] = ctx->total[1] = 0;
  63. ctx->buflen = 0;
  64. }
  65. /* Put result from CTX in first 16 bytes following RESBUF. The result
  66. must be in little endian byte order.
  67. IMPORTANT: On some systems it is required that RESBUF is correctly
  68. aligned for a 32 bits value. */
  69. void *
  70. md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
  71. {
  72. ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
  73. ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
  74. ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
  75. ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
  76. return resbuf;
  77. }
  78. /* Process the remaining bytes in the internal buffer and the usual
  79. prolog according to the standard and write the result to RESBUF.
  80. IMPORTANT: On some systems it is required that RESBUF is correctly
  81. aligned for a 32 bits value. */
  82. void *
  83. md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
  84. {
  85. /* Take yet unprocessed bytes into account. */
  86. md5_uint32 bytes = ctx->buflen;
  87. size_t pad;
  88. /* Now count remaining bytes. */
  89. ctx->total[0] += bytes;
  90. if (ctx->total[0] < bytes)
  91. ++ctx->total[1];
  92. pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
  93. memcpy (&ctx->buffer[bytes], fillbuf, pad);
  94. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  95. ctx->buffer32[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
  96. ctx->buffer32[(bytes + pad + 4) / 4] = SWAP ((ctx->total[1] << 3) |
  97. (ctx->total[0] >> 29));
  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 (ctx->buflen > 64)
  174. {
  175. __md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
  176. ctx->buflen &= 63;
  177. /* The regions in the following copy operation cannot overlap. */
  178. memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
  179. ctx->buflen);
  180. }
  181. buffer = (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. __md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
  199. buffer = (const char *) buffer + 64;
  200. len -= 64;
  201. }
  202. else
  203. #endif
  204. {
  205. __md5_process_block (buffer, len & ~63, ctx);
  206. buffer = (const char *) buffer + (len & ~63);
  207. len &= 63;
  208. }
  209. }
  210. /* Move remaining bytes in internal buffer. */
  211. if (len > 0)
  212. {
  213. size_t left_over = ctx->buflen;
  214. memcpy (&ctx->buffer[left_over], buffer, len);
  215. left_over += len;
  216. if (left_over >= 64)
  217. {
  218. __md5_process_block (ctx->buffer, 64, ctx);
  219. left_over -= 64;
  220. memcpy (ctx->buffer, &ctx->buffer[64], left_over);
  221. }
  222. ctx->buflen = left_over;
  223. }
  224. }
  225. #include <md5-block.c>