]>
Commit | Line | Data |
---|---|---|
0545caaa | 1 | /* |
ef57eb7b | 2 | * Copyright (C) 1996-2016 The Squid Software Foundation and contributors |
0545caaa AJ |
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 | ||
0e2cd867 | 9 | /* |
a34ac434 | 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 | |
c3031d67 | 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. | |
a34ac434 | 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 | * | |
c3031d67 | 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>. | |
a34ac434 | 37 | * Still in the public domain. |
38 | * | |
0e2cd867 | 39 | */ |
f7f3304a | 40 | #include "squid.h" |
3d4022fa | 41 | #include "md5.h" |
42 | ||
09cd7204 AJ |
43 | #if !HAVE_NETTLE_MD5_H |
44 | ||
dac27377 | 45 | #if HAVE_STRING_H |
f53969cc | 46 | #include <string.h> /* for memcpy() */ |
dac27377 | 47 | #endif |
a34ac434 | 48 | #if HAVE_SYS_TYPES_H |
f53969cc | 49 | #include <sys/types.h> /* for stupid systems */ |
dac27377 | 50 | #endif |
6507d007 | 51 | |
a34ac434 | 52 | #ifdef WORDS_BIGENDIAN |
6507d007 | 53 | void |
c3031d67 | 54 | static byteSwap(uint32_t * buf, unsigned words) |
6507d007 | 55 | { |
a34ac434 | 56 | uint8_t *p = (uint8_t *) buf; |
57 | ||
58 | do { | |
26ac0430 AJ |
59 | *buf++ = (uint32_t) ((unsigned) p[3] << 8 | p[2]) << 16 | |
60 | ((unsigned) p[1] << 8 | p[0]); | |
61 | p += 4; | |
a34ac434 | 62 | } while (--words); |
6507d007 | 63 | } |
a34ac434 | 64 | #else |
65 | #define byteSwap(buf,words) | |
66 | #endif | |
6507d007 | 67 | |
68 | /* | |
a34ac434 | 69 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious |
70 | * initialization constants. | |
6507d007 | 71 | */ |
72 | void | |
c3031d67 | 73 | SquidMD5Init(struct SquidMD5Context *ctx) |
6507d007 | 74 | { |
a34ac434 | 75 | ctx->buf[0] = 0x67452301; |
76 | ctx->buf[1] = 0xefcdab89; | |
77 | ctx->buf[2] = 0x98badcfe; | |
78 | ctx->buf[3] = 0x10325476; | |
6507d007 | 79 | |
a34ac434 | 80 | ctx->bytes[0] = 0; |
81 | ctx->bytes[1] = 0; | |
6507d007 | 82 | } |
83 | ||
84 | /* | |
a34ac434 | 85 | * Update context to reflect the concatenation of another buffer full |
86 | * of bytes. | |
6507d007 | 87 | */ |
88 | void | |
c3031d67 | 89 | SquidMD5Update(struct SquidMD5Context *ctx, const void *_buf, unsigned len) |
6507d007 | 90 | { |
a34ac434 | 91 | uint8_t const *buf = _buf; |
92 | uint32_t t; | |
6507d007 | 93 | |
a34ac434 | 94 | /* Update byte count */ |
6507d007 | 95 | |
a34ac434 | 96 | t = ctx->bytes[0]; |
97 | if ((ctx->bytes[0] = t + len) < t) | |
f53969cc | 98 | ctx->bytes[1]++; /* Carry from low to high */ |
6507d007 | 99 | |
f53969cc | 100 | t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ |
a34ac434 | 101 | if (t > len) { |
26ac0430 AJ |
102 | memcpy((uint8_t *) ctx->in + 64 - t, buf, len); |
103 | return; | |
a34ac434 | 104 | } |
105 | /* First chunk is an odd size */ | |
106 | memcpy((uint8_t *) ctx->in + 64 - t, buf, t); | |
107 | byteSwap(ctx->in, 16); | |
c3031d67 | 108 | SquidMD5Transform(ctx->buf, ctx->in); |
a34ac434 | 109 | buf += t; |
110 | len -= t; | |
111 | ||
112 | /* Process data in 64-byte chunks */ | |
113 | while (len >= 64) { | |
26ac0430 AJ |
114 | memcpy(ctx->in, buf, 64); |
115 | byteSwap(ctx->in, 16); | |
116 | SquidMD5Transform(ctx->buf, ctx->in); | |
117 | buf += 64; | |
118 | len -= 64; | |
a34ac434 | 119 | } |
6507d007 | 120 | |
a34ac434 | 121 | /* Handle any remaining bytes of data. */ |
122 | memcpy(ctx->in, buf, len); | |
6507d007 | 123 | } |
124 | ||
125 | /* | |
26ac0430 | 126 | * Final wrapup - pad to 64-byte boundary with the bit pattern |
a34ac434 | 127 | * 1 0* (64-bit count of bits processed, MSB-first) |
6507d007 | 128 | */ |
a34ac434 | 129 | void |
c3031d67 | 130 | SquidMD5Final(unsigned char digest[16], struct SquidMD5Context *ctx) |
6507d007 | 131 | { |
f53969cc | 132 | int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ |
a34ac434 | 133 | uint8_t *p = (uint8_t *) ctx->in + count; |
6507d007 | 134 | |
a34ac434 | 135 | /* Set the first char of padding to 0x80. There is always room. */ |
136 | *p++ = 0x80; | |
6507d007 | 137 | |
a34ac434 | 138 | /* Bytes of padding needed to make 56 bytes (-8..55) */ |
139 | count = 56 - 1 - count; | |
6507d007 | 140 | |
f53969cc | 141 | if (count < 0) { /* Padding forces an extra block */ |
26ac0430 AJ |
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; | |
a34ac434 | 147 | } |
148 | memset(p, 0, count); | |
149 | byteSwap(ctx->in, 14); | |
6507d007 | 150 | |
a34ac434 | 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; | |
c3031d67 | 154 | SquidMD5Transform(ctx->buf, ctx->in); |
6507d007 | 155 | |
a34ac434 | 156 | byteSwap(ctx->buf, 4); |
157 | memcpy(digest, ctx->buf, 16); | |
f53969cc | 158 | memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ |
6507d007 | 159 | } |
160 | ||
a34ac434 | 161 | #ifndef ASM_MD5 |
6507d007 | 162 | |
a34ac434 | 163 | /* The four core functions - F1 is optimized somewhat */ |
6507d007 | 164 | |
a34ac434 | 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)) | |
6507d007 | 170 | |
a34ac434 | 171 | /* This is the central step in the MD5 algorithm. */ |
172 | #define MD5STEP(f,w,x,y,z,in,s) \ | |
f53969cc | 173 | (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) |
6507d007 | 174 | |
175 | /* | |
a34ac434 | 176 | * The core of the MD5 algorithm, this alters an existing MD5 hash to |
c3031d67 | 177 | * reflect the addition of 16 longwords of new data. SquidMD5Update blocks |
a34ac434 | 178 | * the data and converts bytes into longwords for this routine. |
6507d007 | 179 | */ |
a34ac434 | 180 | void |
c3031d67 | 181 | SquidMD5Transform(uint32_t buf[4], uint32_t const in[16]) |
6507d007 | 182 | { |
a34ac434 | 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; | |
6507d007 | 262 | } |
461eef68 | 263 | |
3d4022fa | 264 | #endif /* !ASM_MD5 */ |
09cd7204 | 265 | #endif /* HAVE_ETTLE_MD5_H */ |
f53969cc | 266 |