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