]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/md5.cc
* hack around local file:/ uri problem
[apt.git] / apt-pkg / contrib / md5.cc
CommitLineData
17a10bf5
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
c1ebf56d 3// $Id: md5.cc,v 1.12 2001/05/13 05:15:03 jgg Exp $
17a10bf5
AL
4/* ######################################################################
5
6 MD5Sum - MD5 Message Digest Algorithm.
7
6e52073f
AL
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.
17a10bf5 12
6e52073f
AL
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
17a10bf5
AL
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
67a51bcc
AL
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
17a10bf5 34
17a10bf5
AL
35 ##################################################################### */
36 /*}}}*/
37// Include Files /*{{{*/
38#ifdef __GNUG__
be4401bf 39#pragma implementation "apt-pkg/md5.h"
17a10bf5
AL
40#endif
41
42#include <apt-pkg/md5.h>
6e52073f
AL
43#include <apt-pkg/strutl.h>
44
17a10bf5 45#include <string.h>
17a10bf5 46#include <unistd.h>
67a51bcc 47#include <netinet/in.h> // For htonl
021988ea 48#include <inttypes.h>
312e87da 49#include <config.h>
1ae93c94 50#include <system.h>
17a10bf5
AL
51 /*}}}*/
52
53// byteSwap - Swap bytes in a buffer /*{{{*/
54// ---------------------------------------------------------------------
67a51bcc 55/* Swap n 32 bit longs in given buffer */
83ab33fc 56#ifdef WORDS_BIGENDIAN
95baf595 57static void byteSwap(uint32_t *buf, unsigned words)
17a10bf5 58{
83ab33fc
AL
59 uint8_t *p = (uint8_t *)buf;
60
61 do
17a10bf5 62 {
59257a21 63 *buf++ = (uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
83ab33fc
AL
64 ((unsigned)p[1] << 8 | p[0]);
65 p += 4;
66 } while (--words);
17a10bf5 67}
83ab33fc
AL
68#else
69#define byteSwap(buf,words)
70#endif
17a10bf5
AL
71 /*}}}*/
72// MD5Transform - Alters an existing MD5 hash /*{{{*/
73// ---------------------------------------------------------------------
74/* The core of the MD5 algorithm, this alters an existing MD5 hash to
6e52073f 75 reflect the addition of 16 longwords of new data. Add blocks
17a10bf5
AL
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
021988ea 89static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
17a10bf5 90{
021988ea 91 register uint32_t a, b, c, d;
17a10bf5
AL
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 /*}}}*/
17a10bf5
AL
172// MD5SumValue::MD5SumValue - Constructs the summation from a string /*{{{*/
173// ---------------------------------------------------------------------
174/* The string form of a MD5 is a 32 character hex number */
175MD5SumValue::MD5SumValue(string Str)
176{
177 memset(Sum,0,sizeof(Sum));
178 Set(Str);
179}
180 /*}}}*/
181// MD5SumValue::MD5SumValue - Default constructor /*{{{*/
182// ---------------------------------------------------------------------
183/* Sets the value to 0 */
184MD5SumValue::MD5SumValue()
185{
186 memset(Sum,0,sizeof(Sum));
187}
188 /*}}}*/
189// MD5SumValue::Set - Set the sum from a string /*{{{*/
190// ---------------------------------------------------------------------
191/* Converts the hex string into a set of chars */
192bool MD5SumValue::Set(string Str)
6e52073f 193{
c1ebf56d 194 return Hex2Num(Str,Sum,sizeof(Sum));
17a10bf5
AL
195}
196 /*}}}*/
197// MD5SumValue::Value - Convert the number into a string /*{{{*/
198// ---------------------------------------------------------------------
199/* Converts the set of chars into a hex string in lower case */
200string MD5SumValue::Value() const
201{
202 char Conv[16] = {'0','1','2','3','4','5','6','7','8','9','a','b',
203 'c','d','e','f'};
204 char Result[33];
205 Result[32] = 0;
206
207 // Convert each char into two letters
208 int J = 0;
209 int I = 0;
210 for (; I != 32; J++, I += 2)
211 {
212 Result[I] = Conv[Sum[J] >> 4];
213 Result[I + 1] = Conv[Sum[J] & 0xF];
214 }
215
216 return string(Result);
217}
218 /*}}}*/
219// MD5SumValue::operator == - Comparitor /*{{{*/
220// ---------------------------------------------------------------------
6e52073f 221/* Call memcmp on the buffer */
17a10bf5
AL
222bool MD5SumValue::operator ==(const MD5SumValue &rhs) const
223{
224 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
225}
226 /*}}}*/
227// MD5Summation::MD5Summation - Initialize the summer /*{{{*/
228// ---------------------------------------------------------------------
229/* This assigns the deep magic initial values */
230MD5Summation::MD5Summation()
231{
021988ea
AL
232 uint32_t *buf = (uint32_t *)Buf;
233 uint32_t *bytes = (uint32_t *)Bytes;
17a10bf5
AL
234
235 buf[0] = 0x67452301;
236 buf[1] = 0xefcdab89;
237 buf[2] = 0x98badcfe;
238 buf[3] = 0x10325476;
239
240 bytes[0] = 0;
241 bytes[1] = 0;
242 Done = false;
243}
244 /*}}}*/
6e52073f 245// MD5Summation::Add - 'Add' a data set to the hash /*{{{*/
17a10bf5
AL
246// ---------------------------------------------------------------------
247/* */
248bool MD5Summation::Add(const unsigned char *data,unsigned long len)
249{
250 if (Done == true)
251 return false;
252
021988ea
AL
253 uint32_t *buf = (uint32_t *)Buf;
254 uint32_t *bytes = (uint32_t *)Bytes;
255 uint32_t *in = (uint32_t *)In;
17a10bf5
AL
256
257 // Update byte count and carry (this could be done with a long long?)
021988ea 258 uint32_t t = bytes[0];
17a10bf5
AL
259 if ((bytes[0] = t + len) < t)
260 bytes[1]++;
261
6e52073f 262 // Space available (at least 1)
17a10bf5
AL
263 t = 64 - (t & 0x3f);
264 if (t > len)
265 {
266 memcpy((unsigned char *)in + 64 - t,data,len);
267 return true;
268 }
269
270 // First chunk is an odd size
271 memcpy((unsigned char *)in + 64 - t,data,t);
95baf595 272 byteSwap(in, 16);
17a10bf5
AL
273 MD5Transform(buf,in);
274 data += t;
275 len -= t;
276
277 // Process data in 64-byte chunks
278 while (len >= 64)
279 {
280 memcpy(in,data,64);
95baf595 281 byteSwap(in,16);
17a10bf5
AL
282 MD5Transform(buf,in);
283 data += 64;
284 len -= 64;
285 }
286
287 // Handle any remaining bytes of data.
288 memcpy(in,data,len);
289
290 return true;
291}
292 /*}}}*/
293// MD5Summation::AddFD - Add the contents of a FD to the hash /*{{{*/
294// ---------------------------------------------------------------------
295/* */
296bool MD5Summation::AddFD(int Fd,unsigned long Size)
297{
298 unsigned char Buf[64*64];
299 int Res = 0;
300 while (Size != 0)
301 {
3573e286
MV
302 Res = read(Fd,Buf,min(Size,(unsigned long)sizeof(Buf)));
303 if (Res < 0 || (unsigned)Res != min(Size,(unsigned long)sizeof(Buf)))
17a10bf5
AL
304 return false;
305 Size -= Res;
306 Add(Buf,Res);
307 }
308 return true;
309}
310 /*}}}*/
311// MD5Summation::Result - Returns the value of the sum /*{{{*/
312// ---------------------------------------------------------------------
313/* Because this must add in the last bytes of the series it prevents anyone
314 from calling add after. */
315MD5SumValue MD5Summation::Result()
316{
021988ea
AL
317 uint32_t *buf = (uint32_t *)Buf;
318 uint32_t *bytes = (uint32_t *)Bytes;
319 uint32_t *in = (uint32_t *)In;
17a10bf5
AL
320
321 if (Done == false)
322 {
323 // Number of bytes in In
324 int count = bytes[0] & 0x3f;
325 unsigned char *p = (unsigned char *)in + count;
326
327 // Set the first char of padding to 0x80. There is always room.
328 *p++ = 0x80;
329
330 // Bytes of padding needed to make 56 bytes (-8..55)
331 count = 56 - 1 - count;
332
333 // Padding forces an extra block
334 if (count < 0)
335 {
336 memset(p,0,count + 8);
95baf595 337 byteSwap(in, 16);
17a10bf5
AL
338 MD5Transform(buf,in);
339 p = (unsigned char *)in;
340 count = 56;
341 }
342
343 memset(p, 0, count);
95baf595 344 byteSwap(in, 14);
17a10bf5
AL
345
346 // Append length in bits and transform
347 in[14] = bytes[0] << 3;
348 in[15] = bytes[1] << 3 | bytes[0] >> 29;
349 MD5Transform(buf,in);
95baf595 350 byteSwap(buf,4);
17a10bf5
AL
351 Done = true;
352 }
353
354 MD5SumValue V;
355 memcpy(V.Sum,buf,16);
356 return V;
357}
358 /*}}}*/