Fix formatting
[platform/upstream/glibc.git] / crypt / md5.c
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-2014 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <sys/types.h>
27
28 #if STDC_HEADERS || defined _LIBC
29 # include <stdlib.h>
30 # include <string.h>
31 #else
32 # ifndef HAVE_MEMCPY
33 #  define memcpy(d, s, n) (bcopy ((s), (d), (n)), (d))
34 # endif
35 #endif
36
37 #include "md5.h"
38
39 #ifdef _LIBC
40 # include <endian.h>
41 # if __BYTE_ORDER == __BIG_ENDIAN
42 #  define WORDS_BIGENDIAN 1
43 # endif
44 /* We need to keep the namespace clean so define the MD5 function
45    protected using leading __ .  */
46 # define md5_init_ctx __md5_init_ctx
47 # define md5_process_bytes __md5_process_bytes
48 # define md5_finish_ctx __md5_finish_ctx
49 # define md5_read_ctx __md5_read_ctx
50 # define md5_stream __md5_stream
51 # define md5_buffer __md5_buffer
52 #endif
53
54 #ifdef WORDS_BIGENDIAN
55 # define SWAP(n)                                                        \
56     (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
57 #else
58 # define SWAP(n) (n)
59 #endif
60
61
62 /* This array contains the bytes used to pad the buffer to the next
63    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
64 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
65
66
67 /* Initialize structure containing state of computation.
68    (RFC 1321, 3.3: Step 3)  */
69 void
70 md5_init_ctx (ctx)
71      struct md5_ctx *ctx;
72 {
73   ctx->A = 0x67452301;
74   ctx->B = 0xefcdab89;
75   ctx->C = 0x98badcfe;
76   ctx->D = 0x10325476;
77
78   ctx->total[0] = ctx->total[1] = 0;
79   ctx->buflen = 0;
80 }
81
82 /* Put result from CTX in first 16 bytes following RESBUF.  The result
83    must be in little endian byte order.
84
85    IMPORTANT: On some systems it is required that RESBUF is correctly
86    aligned for a 32 bits value.  */
87 void *
88 md5_read_ctx (ctx, resbuf)
89      const struct md5_ctx *ctx;
90      void *resbuf;
91 {
92   ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
93   ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
94   ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
95   ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
96
97   return resbuf;
98 }
99
100 /* Process the remaining bytes in the internal buffer and the usual
101    prolog according to the standard and write the result to RESBUF.
102
103    IMPORTANT: On some systems it is required that RESBUF is correctly
104    aligned for a 32 bits value.  */
105 void *
106 md5_finish_ctx (ctx, resbuf)
107      struct md5_ctx *ctx;
108      void *resbuf;
109 {
110   /* Take yet unprocessed bytes into account.  */
111   md5_uint32 bytes = ctx->buflen;
112   size_t pad;
113
114   /* Now count remaining bytes.  */
115   ctx->total[0] += bytes;
116   if (ctx->total[0] < bytes)
117     ++ctx->total[1];
118
119   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
120   memcpy (&ctx->buffer[bytes], fillbuf, pad);
121
122   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
123   ctx->buffer32[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
124   ctx->buffer32[(bytes + pad + 4) / 4] = SWAP ((ctx->total[1] << 3) |
125                                                (ctx->total[0] >> 29));
126
127   /* Process last bytes.  */
128   __md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
129
130   return md5_read_ctx (ctx, resbuf);
131 }
132
133 /* Compute MD5 message digest for bytes read from STREAM.  The
134    resulting message digest number will be written into the 16 bytes
135    beginning at RESBLOCK.  */
136 int
137 md5_stream (stream, resblock)
138      FILE *stream;
139      void *resblock;
140 {
141   /* Important: BLOCKSIZE must be a multiple of 64.  */
142 #define BLOCKSIZE 4096
143   struct md5_ctx ctx;
144   char buffer[BLOCKSIZE + 72];
145   size_t sum;
146
147   /* Initialize the computation context.  */
148   md5_init_ctx (&ctx);
149
150   /* Iterate over full file contents.  */
151   while (1)
152     {
153       /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
154          computation function processes the whole buffer so that with the
155          next round of the loop another block can be read.  */
156       size_t n;
157       sum = 0;
158
159       /* Read block.  Take care for partial reads.  */
160       do
161         {
162           n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
163
164           sum += n;
165         }
166       while (sum < BLOCKSIZE && n != 0);
167       if (n == 0 && ferror (stream))
168         return 1;
169
170       /* If end of file is reached, end the loop.  */
171       if (n == 0)
172         break;
173
174       /* Process buffer with BLOCKSIZE bytes.  Note that
175                         BLOCKSIZE % 64 == 0
176        */
177       __md5_process_block (buffer, BLOCKSIZE, &ctx);
178     }
179
180   /* Add the last bytes if necessary.  */
181   if (sum > 0)
182     md5_process_bytes (buffer, sum, &ctx);
183
184   /* Construct result in desired memory.  */
185   md5_finish_ctx (&ctx, resblock);
186   return 0;
187 }
188
189 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
190    result is always in little endian byte order, so that a byte-wise
191    output yields to the wanted ASCII representation of the message
192    digest.  */
193 void *
194 md5_buffer (buffer, len, resblock)
195      const char *buffer;
196      size_t len;
197      void *resblock;
198 {
199   struct md5_ctx ctx;
200
201   /* Initialize the computation context.  */
202   md5_init_ctx (&ctx);
203
204   /* Process whole buffer but last len % 64 bytes.  */
205   md5_process_bytes (buffer, len, &ctx);
206
207   /* Put result in desired memory area.  */
208   return md5_finish_ctx (&ctx, resblock);
209 }
210
211
212 void
213 md5_process_bytes (buffer, len, ctx)
214      const void *buffer;
215      size_t len;
216      struct md5_ctx *ctx;
217 {
218   /* When we already have some bits in our internal buffer concatenate
219      both inputs first.  */
220   if (ctx->buflen != 0)
221     {
222       size_t left_over = ctx->buflen;
223       size_t add = 128 - left_over > len ? len : 128 - left_over;
224
225       memcpy (&ctx->buffer[left_over], buffer, add);
226       ctx->buflen += add;
227
228       if (ctx->buflen > 64)
229         {
230           __md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
231
232           ctx->buflen &= 63;
233           /* The regions in the following copy operation cannot overlap.  */
234           memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
235                   ctx->buflen);
236         }
237
238       buffer = (const char *) buffer + add;
239       len -= add;
240     }
241
242   /* Process available complete blocks.  */
243   if (len >= 64)
244     {
245 #if !_STRING_ARCH_unaligned
246 /* To check alignment gcc has an appropriate operator.  Other
247    compilers don't.  */
248 # if __GNUC__ >= 2
249 #  define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
250 # else
251 #  define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
252 # endif
253       if (UNALIGNED_P (buffer))
254         while (len > 64)
255           {
256             __md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
257             buffer = (const char *) buffer + 64;
258             len -= 64;
259           }
260       else
261 #endif
262         {
263           __md5_process_block (buffer, len & ~63, ctx);
264           buffer = (const char *) buffer + (len & ~63);
265           len &= 63;
266         }
267     }
268
269   /* Move remaining bytes in internal buffer.  */
270   if (len > 0)
271     {
272       size_t left_over = ctx->buflen;
273
274       memcpy (&ctx->buffer[left_over], buffer, len);
275       left_over += len;
276       if (left_over >= 64)
277         {
278           __md5_process_block (ctx->buffer, 64, ctx);
279           left_over -= 64;
280           memcpy (ctx->buffer, &ctx->buffer[64], left_over);
281         }
282       ctx->buflen = left_over;
283     }
284 }
285
286 #include <md5-block.c>