]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
71e16022 MS |
3 | * |
4 | * Private MD5 implementation for CUPS. | |
5 | * | |
6 | * Copyright 2007-2010 by Apple Inc. | |
7 | * Copyright 2005 by Easy Software Products | |
8 | * | |
9 | * Copyright (C) 1999 Aladdin Enterprises. All rights reserved. | |
10 | * | |
11 | * This software is provided 'as-is', without any express or implied | |
12 | * warranty. In no event will the authors be held liable for any damages | |
13 | * arising from the use of this software. | |
14 | * | |
15 | * Permission is granted to anyone to use this software for any purpose, | |
16 | * including commercial applications, and to alter it and redistribute it | |
17 | * freely, subject to the following restrictions: | |
18 | * | |
19 | * 1. The origin of this software must not be misrepresented; you must not | |
20 | * claim that you wrote the original software. If you use this software | |
21 | * in a product, an acknowledgment in the product documentation would be | |
22 | * appreciated but is not required. | |
23 | * 2. Altered source versions must be plainly marked as such, and must not be | |
24 | * misrepresented as being the original software. | |
25 | * 3. This notice may not be removed or altered from any source distribution. | |
26 | * | |
27 | * L. Peter Deutsch | |
28 | * ghost@aladdin.com | |
ef416fc2 | 29 | */ |
ef416fc2 | 30 | /* |
31 | Independent implementation of MD5 (RFC 1321). | |
32 | ||
33 | This code implements the MD5 Algorithm defined in RFC 1321. | |
34 | It is derived directly from the text of the RFC and not from the | |
35 | reference implementation. | |
36 | ||
37 | The original and principal author of md5.c is L. Peter Deutsch | |
38 | <ghost@aladdin.com>. Other authors are noted in the change history | |
39 | that follows (in reverse chronological order): | |
40 | ||
41 | 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. | |
42 | 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5). | |
43 | 1999-05-03 lpd Original version. | |
44 | */ | |
45 | ||
71e16022 MS |
46 | #include "md5-private.h" |
47 | #include "string-private.h" | |
ef416fc2 | 48 | |
49 | #define T1 0xd76aa478 | |
50 | #define T2 0xe8c7b756 | |
51 | #define T3 0x242070db | |
52 | #define T4 0xc1bdceee | |
53 | #define T5 0xf57c0faf | |
54 | #define T6 0x4787c62a | |
55 | #define T7 0xa8304613 | |
56 | #define T8 0xfd469501 | |
57 | #define T9 0x698098d8 | |
58 | #define T10 0x8b44f7af | |
59 | #define T11 0xffff5bb1 | |
60 | #define T12 0x895cd7be | |
61 | #define T13 0x6b901122 | |
62 | #define T14 0xfd987193 | |
63 | #define T15 0xa679438e | |
64 | #define T16 0x49b40821 | |
65 | #define T17 0xf61e2562 | |
66 | #define T18 0xc040b340 | |
67 | #define T19 0x265e5a51 | |
68 | #define T20 0xe9b6c7aa | |
69 | #define T21 0xd62f105d | |
70 | #define T22 0x02441453 | |
71 | #define T23 0xd8a1e681 | |
72 | #define T24 0xe7d3fbc8 | |
73 | #define T25 0x21e1cde6 | |
74 | #define T26 0xc33707d6 | |
75 | #define T27 0xf4d50d87 | |
76 | #define T28 0x455a14ed | |
77 | #define T29 0xa9e3e905 | |
78 | #define T30 0xfcefa3f8 | |
79 | #define T31 0x676f02d9 | |
80 | #define T32 0x8d2a4c8a | |
81 | #define T33 0xfffa3942 | |
82 | #define T34 0x8771f681 | |
83 | #define T35 0x6d9d6122 | |
84 | #define T36 0xfde5380c | |
85 | #define T37 0xa4beea44 | |
86 | #define T38 0x4bdecfa9 | |
87 | #define T39 0xf6bb4b60 | |
88 | #define T40 0xbebfbc70 | |
89 | #define T41 0x289b7ec6 | |
90 | #define T42 0xeaa127fa | |
91 | #define T43 0xd4ef3085 | |
92 | #define T44 0x04881d05 | |
93 | #define T45 0xd9d4d039 | |
94 | #define T46 0xe6db99e5 | |
95 | #define T47 0x1fa27cf8 | |
96 | #define T48 0xc4ac5665 | |
97 | #define T49 0xf4292244 | |
98 | #define T50 0x432aff97 | |
99 | #define T51 0xab9423a7 | |
100 | #define T52 0xfc93a039 | |
101 | #define T53 0x655b59c3 | |
102 | #define T54 0x8f0ccc92 | |
103 | #define T55 0xffeff47d | |
104 | #define T56 0x85845dd1 | |
105 | #define T57 0x6fa87e4f | |
106 | #define T58 0xfe2ce6e0 | |
107 | #define T59 0xa3014314 | |
108 | #define T60 0x4e0811a1 | |
109 | #define T61 0xf7537e82 | |
110 | #define T62 0xbd3af235 | |
111 | #define T63 0x2ad7d2bb | |
112 | #define T64 0xeb86d391 | |
113 | ||
114 | static void | |
115 | _cups_md5_process(_cups_md5_state_t *pms, const unsigned char *data /*[64]*/) | |
116 | { | |
117 | unsigned int | |
118 | a = pms->abcd[0], b = pms->abcd[1], | |
119 | c = pms->abcd[2], d = pms->abcd[3]; | |
120 | unsigned int t; | |
121 | ||
122 | #ifndef ARCH_IS_BIG_ENDIAN | |
123 | # define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */ | |
124 | #endif | |
125 | #if ARCH_IS_BIG_ENDIAN | |
126 | ||
127 | /* | |
128 | * On big-endian machines, we must arrange the bytes in the right | |
129 | * order. (This also works on machines of unknown byte order.) | |
130 | */ | |
131 | unsigned int X[16]; | |
132 | const unsigned char *xp = data; | |
133 | int i; | |
134 | ||
135 | for (i = 0; i < 16; ++i, xp += 4) | |
136 | X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); | |
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 | ||
272 | void | |
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 | ||
282 | void | |
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 | ||
320 | void | |
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 | */ |