]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/md5.cc
[contrib/error.cc] place a colon between errno and error text in output
[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
41 #include <string.h>
42 #include <unistd.h>
43 #include <netinet/in.h> // For htonl
44 #include <inttypes.h>
45 #include <config.h>
46 #include <system.h>
47
48 /*}}}*/
49
50 // byteSwap - Swap bytes in a buffer /*{{{*/
51 // ---------------------------------------------------------------------
52 /* Swap n 32 bit longs in given buffer */
53 #ifdef WORDS_BIGENDIAN
54 static void byteSwap(uint32_t *buf, unsigned words)
55 {
56 uint8_t *p = (uint8_t *)buf;
57
58 do
59 {
60 *buf++ = (uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
61 ((unsigned)p[1] << 8 | p[0]);
62 p += 4;
63 } while (--words);
64 }
65 #else
66 #define byteSwap(buf,words)
67 #endif
68 /*}}}*/
69 // MD5Transform - Alters an existing MD5 hash /*{{{*/
70 // ---------------------------------------------------------------------
71 /* The core of the MD5 algorithm, this alters an existing MD5 hash to
72 reflect the addition of 16 longwords of new data. Add blocks
73 the data and converts bytes into longwords for this routine. */
74
75 // The four core functions - F1 is optimized somewhat
76 // #define F1(x, y, z) (x & y | ~x & z)
77 #define F1(x, y, z) (z ^ (x & (y ^ z)))
78 #define F2(x, y, z) F1(z, x, y)
79 #define F3(x, y, z) (x ^ y ^ z)
80 #define F4(x, y, z) (y ^ (x | ~z))
81
82 // This is the central step in the MD5 algorithm.
83 #define MD5STEP(f,w,x,y,z,in,s) \
84 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
85
86 static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
87 {
88 register uint32_t a, b, c, d;
89
90 a = buf[0];
91 b = buf[1];
92 c = buf[2];
93 d = buf[3];
94
95 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
96 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
97 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
98 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
99 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
100 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
101 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
102 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
103 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
104 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
105 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
106 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
107 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
108 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
109 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
110 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
111
112 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
113 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
114 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
115 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
116 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
117 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
118 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
119 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
120 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
121 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
122 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
123 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
124 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
125 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
126 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
127 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
128
129 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
130 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
131 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
132 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
133 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
134 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
135 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
136 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
137 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
138 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
139 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
140 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
141 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
142 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
143 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
144 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
145
146 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
147 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
148 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
149 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
150 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
151 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
152 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
153 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
154 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
155 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
156 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
157 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
158 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
159 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
160 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
161 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
162
163 buf[0] += a;
164 buf[1] += b;
165 buf[2] += c;
166 buf[3] += d;
167 }
168 /*}}}*/
169 // MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/
170 // ---------------------------------------------------------------------
171 /* The string form of a MD5 is a 32 character hex number */
172 MD5SumValue::MD5SumValue(string Str)
173 {
174 memset(Sum,0,sizeof(Sum));
175 Set(Str);
176 }
177 /*}}}*/
178 // MD5SumValue::MD5SumValue - Default constructor /*{{{*/
179 // ---------------------------------------------------------------------
180 /* Sets the value to 0 */
181 MD5SumValue::MD5SumValue()
182 {
183 memset(Sum,0,sizeof(Sum));
184 }
185 /*}}}*/
186 // MD5SumValue::Set - Set the sum from a string /*{{{*/
187 // ---------------------------------------------------------------------
188 /* Converts the hex string into a set of chars */
189 bool MD5SumValue::Set(string Str)
190 {
191 return Hex2Num(Str,Sum,sizeof(Sum));
192 }
193 /*}}}*/
194 // MD5SumValue::Value - Convert the number into a string /*{{{*/
195 // ---------------------------------------------------------------------
196 /* Converts the set of chars into a hex string in lower case */
197 string MD5SumValue::Value() const
198 {
199 char Conv[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',
200 'c','d','e','f'};
201 char Result[33];
202 Result[32] = 0;
203
204 // Convert each char into two letters
205 int J = 0;
206 int I = 0;
207 for (; I != 32; J++, I += 2)
208 {
209 Result[I] = Conv[Sum[J] >> 4];
210 Result[I + 1] = Conv[Sum[J] & 0xF];
211 }
212
213 return string(Result);
214 }
215 /*}}}*/
216 // MD5SumValue::operator == - Comparitor /*{{{*/
217 // ---------------------------------------------------------------------
218 /* Call memcmp on the buffer */
219 bool MD5SumValue::operator ==(const MD5SumValue &rhs) const
220 {
221 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
222 }
223 /*}}}*/
224 // MD5Summation::MD5Summation - Initialize the summer /*{{{*/
225 // ---------------------------------------------------------------------
226 /* This assigns the deep magic initial values */
227 MD5Summation::MD5Summation()
228 {
229 uint32_t *buf = (uint32_t *)Buf;
230 uint32_t *bytes = (uint32_t *)Bytes;
231
232 buf[0] = 0x67452301;
233 buf[1] = 0xefcdab89;
234 buf[2] = 0x98badcfe;
235 buf[3] = 0x10325476;
236
237 bytes[0] = 0;
238 bytes[1] = 0;
239 Done = false;
240 }
241 /*}}}*/
242 // MD5Summation::Add - 'Add' a data set to the hash /*{{{*/
243 // ---------------------------------------------------------------------
244 /* */
245 bool MD5Summation::Add(const unsigned char *data,unsigned long len)
246 {
247 if (Done == true)
248 return false;
249
250 uint32_t *buf = (uint32_t *)Buf;
251 uint32_t *bytes = (uint32_t *)Bytes;
252 uint32_t *in = (uint32_t *)In;
253
254 // Update byte count and carry (this could be done with a long long?)
255 uint32_t t = bytes[0];
256 if ((bytes[0] = t + len) < t)
257 bytes[1]++;
258
259 // Space available (at least 1)
260 t = 64 - (t & 0x3f);
261 if (t > len)
262 {
263 memcpy((unsigned char *)in + 64 - t,data,len);
264 return true;
265 }
266
267 // First chunk is an odd size
268 memcpy((unsigned char *)in + 64 - t,data,t);
269 byteSwap(in, 16);
270 MD5Transform(buf,in);
271 data += t;
272 len -= t;
273
274 // Process data in 64-byte chunks
275 while (len >= 64)
276 {
277 memcpy(in,data,64);
278 byteSwap(in,16);
279 MD5Transform(buf,in);
280 data += 64;
281 len -= 64;
282 }
283
284 // Handle any remaining bytes of data.
285 memcpy(in,data,len);
286
287 return true;
288 }
289 /*}}}*/
290 // MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/
291 // ---------------------------------------------------------------------
292 /* */
293 bool MD5Summation::AddFD(int Fd,unsigned long Size)
294 {
295 unsigned char Buf[64*64];
296 int Res = 0;
297 while (Size != 0)
298 {
299 Res = read(Fd,Buf,min(Size,(unsigned long)sizeof(Buf)));
300 if (Res < 0 || (unsigned)Res != min(Size,(unsigned long)sizeof(Buf)))
301 return false;
302 Size -= Res;
303 Add(Buf,Res);
304 }
305 return true;
306 }
307 /*}}}*/
308 // MD5Summation::Result - Returns the value of the sum /*{{{*/
309 // ---------------------------------------------------------------------
310 /* Because this must add in the last bytes of the series it prevents anyone
311 from calling add after. */
312 MD5SumValue MD5Summation::Result()
313 {
314 uint32_t *buf = (uint32_t *)Buf;
315 uint32_t *bytes = (uint32_t *)Bytes;
316 uint32_t *in = (uint32_t *)In;
317
318 if (Done == false)
319 {
320 // Number of bytes in In
321 int count = bytes[0] & 0x3f;
322 unsigned char *p = (unsigned char *)in + count;
323
324 // Set the first char of padding to 0x80. There is always room.
325 *p++ = 0x80;
326
327 // Bytes of padding needed to make 56 bytes (-8..55)
328 count = 56 - 1 - count;
329
330 // Padding forces an extra block
331 if (count < 0)
332 {
333 memset(p,0,count + 8);
334 byteSwap(in, 16);
335 MD5Transform(buf,in);
336 p = (unsigned char *)in;
337 count = 56;
338 }
339
340 memset(p, 0, count);
341 byteSwap(in, 14);
342
343 // Append length in bits and transform
344 in[14] = bytes[0] << 3;
345 in[15] = bytes[1] << 3 | bytes[0] >> 29;
346 MD5Transform(buf,in);
347 byteSwap(buf,4);
348 Done = true;
349 }
350
351 MD5SumValue V;
352 memcpy(V.Sum,buf,16);
353 return V;
354 }
355 /*}}}*/