]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/md5.cc
Ftp method
[apt.git] / apt-pkg / contrib / md5.cc
CommitLineData
17a10bf5
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: md5.cc,v 1.1 1998/10/31 05:19:59 jgg Exp $
4/* ######################################################################
5
6 MD5Sum - MD5 Message Digest Algorithm.
7
8 This code implements the MD5 message-digest algorithm.
9 The algorithm is due to Ron Rivest. This code was
10 written by Colin Plumb in 1993, no copyright is claimed.
11 This code is in the public domain; do with it what you wish.
12
13 Equivalent code is available from RSA Data Security, Inc.
14 This code has been tested against that, and is equivalent,
15 except that you don't need to include two pages of legalese
16 with every copy.
17
18 To compute the message digest of a chunk of bytes, declare an
19 MD5Context structure, pass it to MD5Init, call MD5Update as
20 needed on buffers full of bytes, and then call MD5Final, which
21 will fill a supplied 16-byte array with the digest.
22
23 Changed so as no longer to depend on Colin Plumb's `usual.h' header
24 definitions; now uses stuff from dpkg's config.h.
25 - Ian Jackson <ijackson@nyx.cs.du.edu>.
26
27 Changed into a C++ interface and made work with APT's config.h.
28 - Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
29
30 Still in the public domain.
31
32 The classes use arrays of char that are a specific size. We cast those
33 arrays to UINT32's and go from there. This allows us to advoid using
34 config.h in a public header or internally newing memory.
35
36 Some of the terms may be quite bogus, I don't really know the details of
37 MD5, just converted the code ;> - JGG
38
39 ##################################################################### */
40 /*}}}*/
41// Include Files /*{{{*/
42#ifdef __GNUG__
43#pragma interface "apt-pkg/md5.h"
44#endif
45
46#include <apt-pkg/md5.h>
47#include <string.h>
48#include <system.h>
49#include <unistd.h>
50#include <config.h>
51 /*}}}*/
52
53// byteSwap - Swap bytes in a buffer /*{{{*/
54// ---------------------------------------------------------------------
55/* This byteswap function will swap byte in a buffer of data */
56#ifdef WORDS_BIGENDIAN
57static void byteSwap(UINT32 *buf, unsigned words)
58{
59 unsigned char *p = (unsigned char *)buf;
60
61 do
62 {
63 *buf++ = (UINT32)((unsigned)p[3] << 8 | p[2]) << 16 |
64 ((unsigned)p[1] << 8 | p[0]);
65 p += 4;
66 } while (--words);
67}
68#else
69#define byteSwap(buf,words)
70#endif
71 /*}}}*/
72// MD5Transform - Alters an existing MD5 hash /*{{{*/
73// ---------------------------------------------------------------------
74/* The core of the MD5 algorithm, this alters an existing MD5 hash to
75 reflect the addition of 16 longwords of new data. MD5Update blocks
76 the data and converts bytes into longwords for this routine. */
77
78// The four core functions - F1 is optimized somewhat
79// #define F1(x, y, z) (x & y | ~x & z)
80#define F1(x, y, z) (z ^ (x & (y ^ z)))
81#define F2(x, y, z) F1(z, x, y)
82#define F3(x, y, z) (x ^ y ^ z)
83#define F4(x, y, z) (y ^ (x | ~z))
84
85// This is the central step in the MD5 algorithm.
86#define MD5STEP(f,w,x,y,z,in,s) \
87 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
88
89static void MD5Transform(UINT32 buf[4], UINT32 const in[16])
90{
91 register UINT32 a, b, c, d;
92
93 a = buf[0];
94 b = buf[1];
95 c = buf[2];
96 d = buf[3];
97
98 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
99 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
100 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
101 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
102 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
103 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
104 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
105 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
106 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
107 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
108 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
109 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
110 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
111 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
112 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
113 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
114
115 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
116 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
117 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
118 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
119 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
120 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
121 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
122 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
123 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
124 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
125 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
126 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
127 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
128 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
129 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
130 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
131
132 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
133 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
134 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
135 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
136 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
137 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
138 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
139 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
140 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
141 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
142 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
143 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
144 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
145 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
146 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
147 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
148
149 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
150 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
151 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
152 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
153 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
154 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
155 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
156 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
157 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
158 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
159 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
160 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
161 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
162 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
163 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
164 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
165
166 buf[0] += a;
167 buf[1] += b;
168 buf[2] += c;
169 buf[3] += d;
170}
171 /*}}}*/
172// hex_digit - Convert a hex character into an integer /*{{{*/
173// ---------------------------------------------------------------------
174/* The original version of this could only handle lower case. These routines
175 do output to lowercase hex but can handle upper okay as well.*/
176static int hex_digit(int c)
177{
178
179 if (c >= '0' && c <= '9')
180 return c - '0';
181 if (c >= 'a' && c <= 'f')
182 return c - 'a' + 10;
183 if (c >= 'A' && c <= 'F')
184 return c - 'A' + 10;
185 return 0;
186}
187 /*}}}*/
188// MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/
189// ---------------------------------------------------------------------
190/* The string form of a MD5 is a 32 character hex number */
191MD5SumValue::MD5SumValue(string Str)
192{
193 memset(Sum,0,sizeof(Sum));
194 Set(Str);
195}
196 /*}}}*/
197// MD5SumValue::MD5SumValue - Default constructor /*{{{*/
198// ---------------------------------------------------------------------
199/* Sets the value to 0 */
200MD5SumValue::MD5SumValue()
201{
202 memset(Sum,0,sizeof(Sum));
203}
204 /*}}}*/
205// MD5SumValue::Set - Set the sum from a string /*{{{*/
206// ---------------------------------------------------------------------
207/* Converts the hex string into a set of chars */
208bool MD5SumValue::Set(string Str)
209{
210 // Check for correct length
211 if (Str.length() != 32)
212 return false;
213
214 // Check for only hex digits
215 const char *I = Str.begin();
216 for (;I != Str.end(); I++)
217 if (isxdigit(*I) == 0)
218 return false;
219
220 // Convert each digit. We store it in the same order as the string
221 int J = 0;
222 for (I = Str.begin(); I != Str.end();J++, I += 2)
223 {
224 Sum[J] = hex_digit(I[0]) << 4;
225 Sum[J] += hex_digit(I[1]);
226 }
227
228 return true;
229}
230 /*}}}*/
231// MD5SumValue::Value - Convert the number into a string /*{{{*/
232// ---------------------------------------------------------------------
233/* Converts the set of chars into a hex string in lower case */
234string MD5SumValue::Value() const
235{
236 char Conv[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',
237 'c','d','e','f'};
238 char Result[33];
239 Result[32] = 0;
240
241 // Convert each char into two letters
242 int J = 0;
243 int I = 0;
244 for (; I != 32; J++, I += 2)
245 {
246 Result[I] = Conv[Sum[J] >> 4];
247 Result[I + 1] = Conv[Sum[J] & 0xF];
248 }
249
250 return string(Result);
251}
252 /*}}}*/
253// MD5SumValue::operator == - Comparitor /*{{{*/
254// ---------------------------------------------------------------------
255/* Call memcmp on the buffer */
256bool MD5SumValue::operator ==(const MD5SumValue &rhs) const
257{
258 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
259}
260 /*}}}*/
261// MD5Summation::MD5Summation - Initialize the summer /*{{{*/
262// ---------------------------------------------------------------------
263/* This assigns the deep magic initial values */
264MD5Summation::MD5Summation()
265{
266 UINT32 *buf = (UINT32 *)Buf;
267 UINT32 *bytes = (UINT32 *)Bytes;
268
269 buf[0] = 0x67452301;
270 buf[1] = 0xefcdab89;
271 buf[2] = 0x98badcfe;
272 buf[3] = 0x10325476;
273
274 bytes[0] = 0;
275 bytes[1] = 0;
276 Done = false;
277}
278 /*}}}*/
279// MD5Summation::Add - 'Add' a data set to the sum /*{{{*/
280// ---------------------------------------------------------------------
281/* */
282bool MD5Summation::Add(const unsigned char *data,unsigned long len)
283{
284 if (Done == true)
285 return false;
286
287 UINT32 *buf = (UINT32 *)Buf;
288 UINT32 *bytes = (UINT32 *)Bytes;
289 UINT32 *in = (UINT32 *)In;
290
291 // Update byte count and carry (this could be done with a long long?)
292 UINT32 t = bytes[0];
293 if ((bytes[0] = t + len) < t)
294 bytes[1]++;
295
296 // Space available in ctx->in (at least 1)
297 t = 64 - (t & 0x3f);
298 if (t > len)
299 {
300 memcpy((unsigned char *)in + 64 - t,data,len);
301 return true;
302 }
303
304 // First chunk is an odd size
305 memcpy((unsigned char *)in + 64 - t,data,t);
306 byteSwap(in, 16);
307 MD5Transform(buf,in);
308 data += t;
309 len -= t;
310
311 // Process data in 64-byte chunks
312 while (len >= 64)
313 {
314 memcpy(in,data,64);
315 byteSwap(in,16);
316 MD5Transform(buf,in);
317 data += 64;
318 len -= 64;
319 }
320
321 // Handle any remaining bytes of data.
322 memcpy(in,data,len);
323
324 return true;
325}
326 /*}}}*/
327// MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/
328// ---------------------------------------------------------------------
329/* */
330bool MD5Summation::AddFD(int Fd,unsigned long Size)
331{
332 unsigned char Buf[64*64];
333 int Res = 0;
334 while (Size != 0)
335 {
336 Res = read(Fd,Buf,MIN(Size,sizeof(Buf)));
337 if (Res < 0 || (unsigned)Res != MIN(Size,sizeof(Buf)))
338 return false;
339 Size -= Res;
340 Add(Buf,Res);
341 }
342 return true;
343}
344 /*}}}*/
345// MD5Summation::Result - Returns the value of the sum /*{{{*/
346// ---------------------------------------------------------------------
347/* Because this must add in the last bytes of the series it prevents anyone
348 from calling add after. */
349MD5SumValue MD5Summation::Result()
350{
351 UINT32 *buf = (UINT32 *)Buf;
352 UINT32 *bytes = (UINT32 *)Bytes;
353 UINT32 *in = (UINT32 *)In;
354
355 if (Done == false)
356 {
357 // Number of bytes in In
358 int count = bytes[0] & 0x3f;
359 unsigned char *p = (unsigned char *)in + count;
360
361 // Set the first char of padding to 0x80. There is always room.
362 *p++ = 0x80;
363
364 // Bytes of padding needed to make 56 bytes (-8..55)
365 count = 56 - 1 - count;
366
367 // Padding forces an extra block
368 if (count < 0)
369 {
370 memset(p,0,count + 8);
371 byteSwap(in, 16);
372 MD5Transform(buf,in);
373 p = (unsigned char *)in;
374 count = 56;
375 }
376
377 memset(p, 0, count);
378 byteSwap(in, 14);
379
380 // Append length in bits and transform
381 in[14] = bytes[0] << 3;
382 in[15] = bytes[1] << 3 | bytes[0] >> 29;
383 MD5Transform(buf,in);
384 byteSwap(buf,4);
385 Done = true;
386 }
387
388 MD5SumValue V;
389 memcpy(V.Sum,buf,16);
390 return V;
391}
392 /*}}}*/