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