]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/md5.c
<rdar://problem/15386424> cups.org: gunzip bug prevents use of some gzip'd PPD files...
[thirdparty/cups.git] / cups / md5.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
71e16022 3 *
da003234 4 * Private MD5 implementation for CUPS.
71e16022 5 *
da003234
MS
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 2005 by Easy Software Products
8 * Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
71e16022 9 *
da003234
MS
10 * This software is provided 'as-is', without any express or implied
11 * warranty. In no event will the authors be held liable for any damages
12 * arising from the use of this software.
71e16022 13 *
da003234
MS
14 * Permission is granted to anyone to use this software for any purpose,
15 * including commercial applications, and to alter it and redistribute it
16 * freely, subject to the following restrictions:
71e16022 17 *
da003234
MS
18 * 1. The origin of this software must not be misrepresented; you must not
19 * claim that you wrote the original software. If you use this software
20 * in a product, an acknowledgment in the product documentation would be
21 * appreciated but is not required.
22 * 2. Altered source versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software.
24 * 3. This notice may not be removed or altered from any source distribution.
71e16022 25 *
da003234
MS
26 * L. Peter Deutsch
27 * ghost@aladdin.com
ef416fc2 28 */
ef416fc2 29/*
30 Independent implementation of MD5 (RFC 1321).
31
32 This code implements the MD5 Algorithm defined in RFC 1321.
33 It is derived directly from the text of the RFC and not from the
34 reference implementation.
35
36 The original and principal author of md5.c is L. Peter Deutsch
37 <ghost@aladdin.com>. Other authors are noted in the change history
38 that follows (in reverse chronological order):
39
40 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
41 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
42 1999-05-03 lpd Original version.
43 */
44
71e16022
MS
45#include "md5-private.h"
46#include "string-private.h"
ef416fc2 47
48#define T1 0xd76aa478
49#define T2 0xe8c7b756
50#define T3 0x242070db
51#define T4 0xc1bdceee
52#define T5 0xf57c0faf
53#define T6 0x4787c62a
54#define T7 0xa8304613
55#define T8 0xfd469501
56#define T9 0x698098d8
57#define T10 0x8b44f7af
58#define T11 0xffff5bb1
59#define T12 0x895cd7be
60#define T13 0x6b901122
61#define T14 0xfd987193
62#define T15 0xa679438e
63#define T16 0x49b40821
64#define T17 0xf61e2562
65#define T18 0xc040b340
66#define T19 0x265e5a51
67#define T20 0xe9b6c7aa
68#define T21 0xd62f105d
69#define T22 0x02441453
70#define T23 0xd8a1e681
71#define T24 0xe7d3fbc8
72#define T25 0x21e1cde6
73#define T26 0xc33707d6
74#define T27 0xf4d50d87
75#define T28 0x455a14ed
76#define T29 0xa9e3e905
77#define T30 0xfcefa3f8
78#define T31 0x676f02d9
79#define T32 0x8d2a4c8a
80#define T33 0xfffa3942
81#define T34 0x8771f681
82#define T35 0x6d9d6122
83#define T36 0xfde5380c
84#define T37 0xa4beea44
85#define T38 0x4bdecfa9
86#define T39 0xf6bb4b60
87#define T40 0xbebfbc70
88#define T41 0x289b7ec6
89#define T42 0xeaa127fa
90#define T43 0xd4ef3085
91#define T44 0x04881d05
92#define T45 0xd9d4d039
93#define T46 0xe6db99e5
94#define T47 0x1fa27cf8
95#define T48 0xc4ac5665
96#define T49 0xf4292244
97#define T50 0x432aff97
98#define T51 0xab9423a7
99#define T52 0xfc93a039
100#define T53 0x655b59c3
101#define T54 0x8f0ccc92
102#define T55 0xffeff47d
103#define T56 0x85845dd1
104#define T57 0x6fa87e4f
105#define T58 0xfe2ce6e0
106#define T59 0xa3014314
107#define T60 0x4e0811a1
108#define T61 0xf7537e82
109#define T62 0xbd3af235
110#define T63 0x2ad7d2bb
111#define T64 0xeb86d391
112
113static void
114_cups_md5_process(_cups_md5_state_t *pms, const unsigned char *data /*[64]*/)
115{
116 unsigned int
117 a = pms->abcd[0], b = pms->abcd[1],
118 c = pms->abcd[2], d = pms->abcd[3];
119 unsigned int t;
120
121#ifndef ARCH_IS_BIG_ENDIAN
122# define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */
123#endif
124#if ARCH_IS_BIG_ENDIAN
125
126 /*
127 * On big-endian machines, we must arrange the bytes in the right
128 * order. (This also works on machines of unknown byte order.)
129 */
130 unsigned int X[16];
131 const unsigned char *xp = data;
132 int i;
133
134 for (i = 0; i < 16; ++i, xp += 4)
da003234
MS
135 X[i] = (unsigned)xp[0] + ((unsigned)xp[1] << 8) +
136 ((unsigned)xp[2] << 16) + ((unsigned)xp[3] << 24);
ef416fc2 137
138#else /* !ARCH_IS_BIG_ENDIAN */
139
140 /*
141 * On little-endian machines, we can process properly aligned data
142 * without copying it.
143 */
144 unsigned int xbuf[16];
145 const unsigned int *X;
146
147 if (!((data - (const unsigned char *)0) & 3)) {
148 /* data are properly aligned */
149 X = (const unsigned int *)data;
150 } else {
151 /* not aligned */
152 memcpy(xbuf, data, 64);
153 X = xbuf;
154 }
155#endif
156
157#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
158
159 /* Round 1. */
160 /* Let [abcd k s i] denote the operation
161 a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
162#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
163#define SET(a, b, c, d, k, s, Ti)\
164 t = a + F(b,c,d) + X[k] + Ti;\
165 a = ROTATE_LEFT(t, s) + b
166 /* Do the following 16 operations. */
167 SET(a, b, c, d, 0, 7, T1);
168 SET(d, a, b, c, 1, 12, T2);
169 SET(c, d, a, b, 2, 17, T3);
170 SET(b, c, d, a, 3, 22, T4);
171 SET(a, b, c, d, 4, 7, T5);
172 SET(d, a, b, c, 5, 12, T6);
173 SET(c, d, a, b, 6, 17, T7);
174 SET(b, c, d, a, 7, 22, T8);
175 SET(a, b, c, d, 8, 7, T9);
176 SET(d, a, b, c, 9, 12, T10);
177 SET(c, d, a, b, 10, 17, T11);
178 SET(b, c, d, a, 11, 22, T12);
179 SET(a, b, c, d, 12, 7, T13);
180 SET(d, a, b, c, 13, 12, T14);
181 SET(c, d, a, b, 14, 17, T15);
182 SET(b, c, d, a, 15, 22, T16);
183#undef SET
184
185 /* Round 2. */
186 /* Let [abcd k s i] denote the operation
187 a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
188#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
189#define SET(a, b, c, d, k, s, Ti)\
190 t = a + G(b,c,d) + X[k] + Ti;\
191 a = ROTATE_LEFT(t, s) + b
192 /* Do the following 16 operations. */
193 SET(a, b, c, d, 1, 5, T17);
194 SET(d, a, b, c, 6, 9, T18);
195 SET(c, d, a, b, 11, 14, T19);
196 SET(b, c, d, a, 0, 20, T20);
197 SET(a, b, c, d, 5, 5, T21);
198 SET(d, a, b, c, 10, 9, T22);
199 SET(c, d, a, b, 15, 14, T23);
200 SET(b, c, d, a, 4, 20, T24);
201 SET(a, b, c, d, 9, 5, T25);
202 SET(d, a, b, c, 14, 9, T26);
203 SET(c, d, a, b, 3, 14, T27);
204 SET(b, c, d, a, 8, 20, T28);
205 SET(a, b, c, d, 13, 5, T29);
206 SET(d, a, b, c, 2, 9, T30);
207 SET(c, d, a, b, 7, 14, T31);
208 SET(b, c, d, a, 12, 20, T32);
209#undef SET
210
211 /* Round 3. */
212 /* Let [abcd k s t] denote the operation
213 a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
214#define H(x, y, z) ((x) ^ (y) ^ (z))
215#define SET(a, b, c, d, k, s, Ti)\
216 t = a + H(b,c,d) + X[k] + Ti;\
217 a = ROTATE_LEFT(t, s) + b
218 /* Do the following 16 operations. */
219 SET(a, b, c, d, 5, 4, T33);
220 SET(d, a, b, c, 8, 11, T34);
221 SET(c, d, a, b, 11, 16, T35);
222 SET(b, c, d, a, 14, 23, T36);
223 SET(a, b, c, d, 1, 4, T37);
224 SET(d, a, b, c, 4, 11, T38);
225 SET(c, d, a, b, 7, 16, T39);
226 SET(b, c, d, a, 10, 23, T40);
227 SET(a, b, c, d, 13, 4, T41);
228 SET(d, a, b, c, 0, 11, T42);
229 SET(c, d, a, b, 3, 16, T43);
230 SET(b, c, d, a, 6, 23, T44);
231 SET(a, b, c, d, 9, 4, T45);
232 SET(d, a, b, c, 12, 11, T46);
233 SET(c, d, a, b, 15, 16, T47);
234 SET(b, c, d, a, 2, 23, T48);
235#undef SET
236
237 /* Round 4. */
238 /* Let [abcd k s t] denote the operation
239 a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
240#define I(x, y, z) ((y) ^ ((x) | ~(z)))
241#define SET(a, b, c, d, k, s, Ti)\
242 t = a + I(b,c,d) + X[k] + Ti;\
243 a = ROTATE_LEFT(t, s) + b
244 /* Do the following 16 operations. */
245 SET(a, b, c, d, 0, 6, T49);
246 SET(d, a, b, c, 7, 10, T50);
247 SET(c, d, a, b, 14, 15, T51);
248 SET(b, c, d, a, 5, 21, T52);
249 SET(a, b, c, d, 12, 6, T53);
250 SET(d, a, b, c, 3, 10, T54);
251 SET(c, d, a, b, 10, 15, T55);
252 SET(b, c, d, a, 1, 21, T56);
253 SET(a, b, c, d, 8, 6, T57);
254 SET(d, a, b, c, 15, 10, T58);
255 SET(c, d, a, b, 6, 15, T59);
256 SET(b, c, d, a, 13, 21, T60);
257 SET(a, b, c, d, 4, 6, T61);
258 SET(d, a, b, c, 11, 10, T62);
259 SET(c, d, a, b, 2, 15, T63);
260 SET(b, c, d, a, 9, 21, T64);
261#undef SET
262
263 /* Then perform the following additions. (That is increment each
264 of the four registers by the value it had before this block
265 was started.) */
266 pms->abcd[0] += a;
267 pms->abcd[1] += b;
268 pms->abcd[2] += c;
269 pms->abcd[3] += d;
270}
271
272void
757d2cad 273_cupsMD5Init(_cups_md5_state_t *pms)
ef416fc2 274{
275 pms->count[0] = pms->count[1] = 0;
276 pms->abcd[0] = 0x67452301;
277 pms->abcd[1] = 0xefcdab89;
278 pms->abcd[2] = 0x98badcfe;
279 pms->abcd[3] = 0x10325476;
280}
281
282void
757d2cad 283_cupsMD5Append(_cups_md5_state_t *pms, const unsigned char *data, int nbytes)
ef416fc2 284{
285 const unsigned char *p = data;
286 int left = nbytes;
287 int offset = (pms->count[0] >> 3) & 63;
288 unsigned int nbits = (unsigned int)(nbytes << 3);
289
290 if (nbytes <= 0)
291 return;
292
293 /* Update the message length. */
294 pms->count[1] += nbytes >> 29;
295 pms->count[0] += nbits;
296 if (pms->count[0] < nbits)
297 pms->count[1]++;
298
299 /* Process an initial partial block. */
300 if (offset) {
301 int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
302
303 memcpy(pms->buf + offset, p, copy);
304 if (offset + copy < 64)
305 return;
306 p += copy;
307 left -= copy;
308 _cups_md5_process(pms, pms->buf);
309 }
310
311 /* Process full blocks. */
312 for (; left >= 64; p += 64, left -= 64)
313 _cups_md5_process(pms, p);
314
315 /* Process a final partial block. */
316 if (left)
317 memcpy(pms->buf, p, left);
318}
319
320void
757d2cad 321_cupsMD5Finish(_cups_md5_state_t *pms, unsigned char digest[16])
ef416fc2 322{
323 static const unsigned char pad[64] = {
324 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
325 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
326 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
327 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
328 };
329 unsigned char data[8];
330 int i;
331
332 /* Save the length before padding. */
333 for (i = 0; i < 8; ++i)
334 data[i] = (unsigned char)(pms->count[i >> 2] >> ((i & 3) << 3));
335 /* Pad to 56 bytes mod 64. */
757d2cad 336 _cupsMD5Append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
ef416fc2 337 /* Append the length. */
757d2cad 338 _cupsMD5Append(pms, data, 8);
ef416fc2 339 for (i = 0; i < 16; ++i)
340 digest[i] = (unsigned char)(pms->abcd[i >> 2] >> ((i & 3) << 3));
341}
71e16022
MS
342
343
344/*
f2d18633 345 * End of "$Id$".
71e16022 346 */