]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/md5.cc
merge with debian-experimental
[apt.git] / apt-pkg / contrib / md5.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: md5.cc,v 1.12 2001/05/13 05:15:03 jgg Exp $
4 /* ######################################################################
5
6 MD5Sum - MD5 Message Digest Algorithm.
7
8 This code implements the MD5 message-digest algorithm. The algorithm is
9 due to Ron Rivest. This code was written by Colin Plumb in 1993, no
10 copyright is claimed. This code is in the public domain; do with it what
11 you wish.
12
13 Equivalent code is available from RSA Data Security, Inc. This code has
14 been tested against that, and is equivalent, except that you don't need to
15 include two pages of legalese with every copy.
16
17 To compute the message digest of a chunk of bytes, instantiate the class,
18 and repeatedly call one of the Add() members. When finished the Result
19 method will return the Hash and finalize the value.
20
21 Changed so as no longer to depend on Colin Plumb's `usual.h' header
22 definitions; now uses stuff from dpkg's config.h.
23 - Ian Jackson <ijackson@nyx.cs.du.edu>.
24
25 Changed into a C++ interface and made work with APT's config.h.
26 - Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
27
28 Still in the public domain.
29
30 The classes use arrays of char that are a specific size. We cast those
31 arrays to uint8_t's and go from there. This allows us to advoid using
32 the uncommon inttypes.h in a public header or internally newing memory.
33 In theory if C9x becomes nicely accepted
34
35 ##################################################################### */
36 /*}}}*/
37 // Include Files /*{{{*/
38 #include <apt-pkg/md5.h>
39 #include <apt-pkg/strutl.h>
40 #include <apt-pkg/macros.h>
41
42 #include <string.h>
43 #include <unistd.h>
44 #include <netinet/in.h> // For htonl
45 #include <inttypes.h>
46 #include <config.h>
47 /*}}}*/
48
49 // byteSwap - Swap bytes in a buffer /*{{{*/
50 // ---------------------------------------------------------------------
51 /* Swap n 32 bit longs in given buffer */
52 #ifdef WORDS_BIGENDIAN
53 static void byteSwap(uint32_t *buf, unsigned words)
54 {
55 uint8_t *p = (uint8_t *)buf;
56
57 do
58 {
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 // MD5Transform - Alters an existing MD5 hash /*{{{*/
69 // ---------------------------------------------------------------------
70 /* The core of the MD5 algorithm, this alters an existing MD5 hash to
71 reflect the addition of 16 longwords of new data. Add blocks
72 the data and converts bytes into longwords for this routine. */
73
74 // The four core functions - F1 is optimized somewhat
75 // #define F1(x, y, z) (x & y | ~x & z)
76 #define F1(x, y, z) (z ^ (x & (y ^ z)))
77 #define F2(x, y, z) F1(z, x, y)
78 #define F3(x, y, z) (x ^ y ^ z)
79 #define F4(x, y, z) (y ^ (x | ~z))
80
81 // This is the central step in the MD5 algorithm.
82 #define MD5STEP(f,w,x,y,z,in,s) \
83 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
84
85 static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
86 {
87 register uint32_t a, b, c, d;
88
89 a = buf[0];
90 b = buf[1];
91 c = buf[2];
92 d = buf[3];
93
94 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
95 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
96 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
97 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
98 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
99 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
100 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
101 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
102 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
103 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
104 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
105 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
106 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
107 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
108 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
109 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
110
111 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
112 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
113 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
114 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
115 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
116 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
117 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
118 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
119 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
120 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
121 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
122 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
123 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
124 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
125 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
126 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
127
128 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
129 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
130 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
131 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
132 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
133 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
134 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
135 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
136 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
137 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
138 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
139 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
140 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
141 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
142 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
143 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
144
145 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
146 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
147 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
148 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
149 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
150 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
151 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
152 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
153 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
154 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
155 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
156 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
157 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
158 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
159 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
160 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
161
162 buf[0] += a;
163 buf[1] += b;
164 buf[2] += c;
165 buf[3] += d;
166 }
167 /*}}}*/
168 // MD5Summation::MD5Summation - Initialize the summer /*{{{*/
169 // ---------------------------------------------------------------------
170 /* This assigns the deep magic initial values */
171 MD5Summation::MD5Summation()
172 {
173 uint32_t *buf = (uint32_t *)Buf;
174 uint32_t *bytes = (uint32_t *)Bytes;
175
176 buf[0] = 0x67452301;
177 buf[1] = 0xefcdab89;
178 buf[2] = 0x98badcfe;
179 buf[3] = 0x10325476;
180
181 bytes[0] = 0;
182 bytes[1] = 0;
183 Done = false;
184 }
185 /*}}}*/
186 // MD5Summation::Add - 'Add' a data set to the hash /*{{{*/
187 // ---------------------------------------------------------------------
188 /* */
189 bool MD5Summation::Add(const unsigned char *data,unsigned long len)
190 {
191 if (Done == true)
192 return false;
193
194 uint32_t *buf = (uint32_t *)Buf;
195 uint32_t *bytes = (uint32_t *)Bytes;
196 uint32_t *in = (uint32_t *)In;
197
198 // Update byte count and carry (this could be done with a long long?)
199 uint32_t t = bytes[0];
200 if ((bytes[0] = t + len) < t)
201 bytes[1]++;
202
203 // Space available (at least 1)
204 t = 64 - (t & 0x3f);
205 if (t > len)
206 {
207 memcpy((unsigned char *)in + 64 - t,data,len);
208 return true;
209 }
210
211 // First chunk is an odd size
212 memcpy((unsigned char *)in + 64 - t,data,t);
213 byteSwap(in, 16);
214 MD5Transform(buf,in);
215 data += t;
216 len -= t;
217
218 // Process data in 64-byte chunks
219 while (len >= 64)
220 {
221 memcpy(in,data,64);
222 byteSwap(in,16);
223 MD5Transform(buf,in);
224 data += 64;
225 len -= 64;
226 }
227
228 // Handle any remaining bytes of data.
229 memcpy(in,data,len);
230
231 return true;
232 }
233 /*}}}*/
234 // MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/
235 // ---------------------------------------------------------------------
236 /* */
237 bool MD5Summation::AddFD(int Fd,unsigned long Size)
238 {
239 unsigned char Buf[64*64];
240 int Res = 0;
241 int ToEOF = (Size == 0);
242 while (Size != 0 || ToEOF)
243 {
244 unsigned n = sizeof(Buf);
245 if (!ToEOF) n = min(Size,(unsigned long)n);
246 Res = read(Fd,Buf,n);
247 if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
248 return false;
249 if (ToEOF && Res == 0) // EOF
250 break;
251 Size -= Res;
252 Add(Buf,Res);
253 }
254 return true;
255 }
256 /*}}}*/
257 // MD5Summation::Result - Returns the value of the sum /*{{{*/
258 // ---------------------------------------------------------------------
259 /* Because this must add in the last bytes of the series it prevents anyone
260 from calling add after. */
261 MD5SumValue MD5Summation::Result()
262 {
263 uint32_t *buf = (uint32_t *)Buf;
264 uint32_t *bytes = (uint32_t *)Bytes;
265 uint32_t *in = (uint32_t *)In;
266
267 if (Done == false)
268 {
269 // Number of bytes in In
270 int count = bytes[0] & 0x3f;
271 unsigned char *p = (unsigned char *)in + count;
272
273 // Set the first char of padding to 0x80. There is always room.
274 *p++ = 0x80;
275
276 // Bytes of padding needed to make 56 bytes (-8..55)
277 count = 56 - 1 - count;
278
279 // Padding forces an extra block
280 if (count < 0)
281 {
282 memset(p,0,count + 8);
283 byteSwap(in, 16);
284 MD5Transform(buf,in);
285 p = (unsigned char *)in;
286 count = 56;
287 }
288
289 memset(p, 0, count);
290 byteSwap(in, 14);
291
292 // Append length in bits and transform
293 in[14] = bytes[0] << 3;
294 in[15] = bytes[1] << 3 | bytes[0] >> 29;
295 MD5Transform(buf,in);
296 byteSwap(buf,4);
297 Done = true;
298 }
299
300 MD5SumValue V;
301 V.Set((unsigned char *)buf);
302 return V;
303 }
304 /*}}}*/