md5.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /*
10  * This code implements the MD5 message-digest algorithm.
11  * The algorithm is due to Ron Rivest. This code was
12  * written by Colin Plumb in 1993, no copyright is claimed.
13  * This code is in the public domain; do with it what you wish.
14  *
15  * Equivalent code is available from RSA Data Security, Inc.
16  * This code has been tested against that, and is equivalent,
17  * except that you don't need to include two pages of legalese
18  * with every copy.
19  *
20  * To compute the message digest of a chunk of bytes, declare an
21  * SquidMD5Context structure, pass it to SquidMD5Init, call
22  * SquidMD5Update as needed on buffers full of bytes, and then call
23  * SquidMD5Final, which will fill a supplied 16-byte array with the
24  * digest.
25  *
26  * Changed so as no longer to depend on Colin Plumb's `usual.h' header
27  * definitions; now uses stuff from dpkg's config.h.
28  * - Ian Jackson <ian@chiark.greenend.org.uk>.
29  * Still in the public domain.
30  *
31  * Changed SquidMD5Update to take a void * for easier use and some
32  * other minor cleanup. - Henrik Nordstrom <henrik@henriknordstrom.net>.
33  * Still in the public domain.
34  *
35  * Prefixed all symbols with "Squid" so they don't collide with
36  * other libraries. Duane Wessels <wessels@squid-cache.org>.
37  * Still in the public domain.
38  *
39  */
40 #include "squid.h"
41 #include "md5.h"
42 
43 #if !HAVE_NETTLE_MD5_H
44 
45 #if HAVE_STRING_H
46 #include <string.h> /* for memcpy() */
47 #endif
48 #if HAVE_SYS_TYPES_H
49 #include <sys/types.h> /* for stupid systems */
50 #endif
51 
52 #ifdef WORDS_BIGENDIAN
53 void
54 static byteSwap(uint32_t * buf, unsigned words)
55 {
56  uint8_t *p = (uint8_t *) buf;
57 
58  do {
59  *buf++ = (uint32_t) ((unsigned) p[3] << 8 | p[2]) << 16 |
60  ((unsigned) p[1] << 8 | p[0]);
61  p += 4;
62  } while (--words);
63 }
64 #else
65 #define byteSwap(buf,words)
66 #endif
67 
68 /*
69  * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
70  * initialization constants.
71  */
72 void
74 {
75  ctx->buf[0] = 0x67452301;
76  ctx->buf[1] = 0xefcdab89;
77  ctx->buf[2] = 0x98badcfe;
78  ctx->buf[3] = 0x10325476;
79 
80  ctx->bytes[0] = 0;
81  ctx->bytes[1] = 0;
82 }
83 
84 /*
85  * Update context to reflect the concatenation of another buffer full
86  * of bytes.
87  */
88 void
89 SquidMD5Update(struct SquidMD5Context *ctx, const void *_buf, unsigned len)
90 {
91  uint8_t const *buf = _buf;
92  uint32_t t;
93 
94  /* Update byte count */
95 
96  t = ctx->bytes[0];
97  if ((ctx->bytes[0] = t + len) < t)
98  ctx->bytes[1]++; /* Carry from low to high */
99 
100  t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
101  if (t > len) {
102  memcpy((uint8_t *) ctx->in + 64 - t, buf, len);
103  return;
104  }
105  /* First chunk is an odd size */
106  memcpy((uint8_t *) ctx->in + 64 - t, buf, t);
107  byteSwap(ctx->in, 16);
108  SquidMD5Transform(ctx->buf, ctx->in);
109  buf += t;
110  len -= t;
111 
112  /* Process data in 64-byte chunks */
113  while (len >= 64) {
114  memcpy(ctx->in, buf, 64);
115  byteSwap(ctx->in, 16);
116  SquidMD5Transform(ctx->buf, ctx->in);
117  buf += 64;
118  len -= 64;
119  }
120 
121  /* Handle any remaining bytes of data. */
122  memcpy(ctx->in, buf, len);
123 }
124 
125 /*
126  * Final wrapup - pad to 64-byte boundary with the bit pattern
127  * 1 0* (64-bit count of bits processed, MSB-first)
128  */
129 void
130 SquidMD5Final(unsigned char digest[16], struct SquidMD5Context *ctx)
131 {
132  int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
133  uint8_t *p = (uint8_t *) ctx->in + count;
134 
135  /* Set the first char of padding to 0x80. There is always room. */
136  *p++ = 0x80;
137 
138  /* Bytes of padding needed to make 56 bytes (-8..55) */
139  count = 56 - 1 - count;
140 
141  if (count < 0) { /* Padding forces an extra block */
142  memset(p, 0, count + 8);
143  byteSwap(ctx->in, 16);
144  SquidMD5Transform(ctx->buf, ctx->in);
145  p = (uint8_t *) ctx->in;
146  count = 56;
147  }
148  memset(p, 0, count);
149  byteSwap(ctx->in, 14);
150 
151  /* Append length in bits and transform */
152  ctx->in[14] = ctx->bytes[0] << 3;
153  ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
154  SquidMD5Transform(ctx->buf, ctx->in);
155 
156  byteSwap(ctx->buf, 4);
157  memcpy(digest, ctx->buf, 16);
158  memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
159 }
160 
161 #ifndef ASM_MD5
162 
163 /* The four core functions - F1 is optimized somewhat */
164 
165 /* #define F1(x, y, z) (x & y | ~x & z) */
166 #define F1(x, y, z) (z ^ (x & (y ^ z)))
167 #define F2(x, y, z) F1(z, x, y)
168 #define F3(x, y, z) (x ^ y ^ z)
169 #define F4(x, y, z) (y ^ (x | ~z))
170 
171 /* This is the central step in the MD5 algorithm. */
172 #define MD5STEP(f,w,x,y,z,in,s) \
173  (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
174 
175 /*
176  * The core of the MD5 algorithm, this alters an existing MD5 hash to
177  * reflect the addition of 16 longwords of new data. SquidMD5Update blocks
178  * the data and converts bytes into longwords for this routine.
179  */
180 void
181 SquidMD5Transform(uint32_t buf[4], uint32_t const in[16])
182 {
183  register uint32_t a, b, c, d;
184 
185  a = buf[0];
186  b = buf[1];
187  c = buf[2];
188  d = buf[3];
189 
190  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
191  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
192  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
193  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
194  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
195  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
196  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
197  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
198  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
199  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
200  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
201  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
202  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
203  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
204  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
205  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
206 
207  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
208  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
209  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
210  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
211  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
212  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
213  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
214  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
215  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
216  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
217  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
218  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
219  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
220  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
221  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
222  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
223 
224  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
225  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
226  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
227  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
228  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
229  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
230  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
231  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
232  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
233  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
234  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
235  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
236  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
237  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
238  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
239  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
240 
241  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
242  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
243  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
244  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
245  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
246  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
247  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
248  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
249  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
250  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
251  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
252  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
253  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
254  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
255  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
256  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
257 
258  buf[0] += a;
259  buf[1] += b;
260  buf[2] += c;
261  buf[3] += d;
262 }
263 
264 #endif /* !ASM_MD5 */
265 #endif /* HAVE_ETTLE_MD5_H */
266 
uint32_t in[16]
Definition: md5.h:58
#define F4(x, y, z)
Definition: md5.c:169
uint32_t bytes[2]
Definition: md5.h:57
uint32_t buf[4]
Definition: md5.h:56
void SquidMD5Update(struct SquidMD5Context *ctx, const void *_buf, unsigned len)
Definition: md5.c:89
#define byteSwap(buf, words)
Definition: md5.c:65
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
#define F3(x, y, z)
Definition: md5.c:168
void SquidMD5Transform(uint32_t buf[4], uint32_t const in[16])
Definition: md5.c:181
#define MD5STEP(f, w, x, y, z, in, s)
Definition: md5.c:172
#define F1(x, y, z)
Definition: md5.c:166
int a
Definition: membanger.c:50
void SquidMD5Final(unsigned char digest[16], struct SquidMD5Context *ctx)
Definition: md5.c:130
void SquidMD5Init(struct SquidMD5Context *ctx)
Definition: md5.c:73
#define F2(x, y, z)
Definition: md5.c:167

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors