]> git.saurik.com Git - apt-legacy.git/blob - apt-pkg/contrib/sha1.cc
Apparently apt-ftparchive uses Berkeley DB.
[apt-legacy.git] / apt-pkg / contrib / sha1.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: sha1.cc,v 1.3 2001/05/13 05:15:03 jgg Exp $
4 /* ######################################################################
5
6 SHA1 - SHA-1 Secure Hash Algorithm.
7
8 This file is a Public Domain wrapper for the Public Domain SHA1
9 calculation code that is at it's end.
10
11 The algorithm was originally implemented by
12 Steve Reid <sreid@sea-to-sky.net> and later modified by
13 James H. Brown <jbrown@burgoyne.com>.
14
15 Modifications for APT were done by Alfredo K. Kojima and Jason
16 Gunthorpe.
17
18 Still in the public domain.
19
20 Test Vectors (from FIPS PUB 180-1)
21 "abc"
22 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
23 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
24 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
25 A million repetitions of "a"
26 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
27
28 #####################################################################
29 */
30 /*}}} */
31 // Include Files /*{{{*/
32 #ifdef __GNUG__
33 #pragma implementation "apt-pkg/sha1.h"
34 #endif
35
36 #include <apt-pkg/sha1.h>
37 #include <apt-pkg/strutl.h>
38
39 #include <string.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42 #include <config.h>
43 #include <system.h>
44 /*}}}*/
45
46 // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/
47 // ---------------------------------------------------------------------
48 /* The core of the SHA-1 algorithm. This alters an existing SHA-1 hash to
49 reflect the addition of 16 longwords of new data. Other routines convert
50 incoming stream data into 16 long word chunks for this routine */
51
52 #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
53
54 /* blk0() and blk() perform the initial expand. */
55 /* I got the idea of expanding during the round function from SSLeay */
56 #ifndef WORDS_BIGENDIAN
57 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
58 |(rol(block->l[i],8)&0x00FF00FF))
59 #else
60 #define blk0(i) block->l[i]
61 #endif
62 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
63 ^block->l[(i+2)&15]^block->l[i&15],1))
64
65 /* (R0+R1),R2,R3,R4 are the different operations used in SHA1 */
66 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
67 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
68 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
69 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
70 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
71
72 static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64])
73 {
74 uint32_t a,b,c,d,e;
75 typedef union
76 {
77 uint8_t c[64];
78 uint32_t l[16];
79 }
80 CHAR64LONG16;
81 CHAR64LONG16 *block;
82
83 uint8_t workspace[64];
84 block = (CHAR64LONG16 *)workspace;
85 memcpy(block,buffer,sizeof(workspace));
86
87 /* Copy context->state[] to working vars */
88 a = state[0];
89 b = state[1];
90 c = state[2];
91 d = state[3];
92 e = state[4];
93
94 /* 4 rounds of 20 operations each. Loop unrolled. */
95 R0(a,b,c,d,e,0);
96 R0(e,a,b,c,d,1);
97 R0(d,e,a,b,c,2);
98 R0(c,d,e,a,b,3);
99 R0(b,c,d,e,a,4);
100 R0(a,b,c,d,e,5);
101 R0(e,a,b,c,d,6);
102 R0(d,e,a,b,c,7);
103 R0(c,d,e,a,b,8);
104 R0(b,c,d,e,a,9);
105 R0(a,b,c,d,e,10);
106 R0(e,a,b,c,d,11);
107 R0(d,e,a,b,c,12);
108 R0(c,d,e,a,b,13);
109 R0(b,c,d,e,a,14);
110 R0(a,b,c,d,e,15);
111 R1(e,a,b,c,d,16);
112 R1(d,e,a,b,c,17);
113 R1(c,d,e,a,b,18);
114 R1(b,c,d,e,a,19);
115 R2(a,b,c,d,e,20);
116 R2(e,a,b,c,d,21);
117 R2(d,e,a,b,c,22);
118 R2(c,d,e,a,b,23);
119 R2(b,c,d,e,a,24);
120 R2(a,b,c,d,e,25);
121 R2(e,a,b,c,d,26);
122 R2(d,e,a,b,c,27);
123 R2(c,d,e,a,b,28);
124 R2(b,c,d,e,a,29);
125 R2(a,b,c,d,e,30);
126 R2(e,a,b,c,d,31);
127 R2(d,e,a,b,c,32);
128 R2(c,d,e,a,b,33);
129 R2(b,c,d,e,a,34);
130 R2(a,b,c,d,e,35);
131 R2(e,a,b,c,d,36);
132 R2(d,e,a,b,c,37);
133 R2(c,d,e,a,b,38);
134 R2(b,c,d,e,a,39);
135 R3(a,b,c,d,e,40);
136 R3(e,a,b,c,d,41);
137 R3(d,e,a,b,c,42);
138 R3(c,d,e,a,b,43);
139 R3(b,c,d,e,a,44);
140 R3(a,b,c,d,e,45);
141 R3(e,a,b,c,d,46);
142 R3(d,e,a,b,c,47);
143 R3(c,d,e,a,b,48);
144 R3(b,c,d,e,a,49);
145 R3(a,b,c,d,e,50);
146 R3(e,a,b,c,d,51);
147 R3(d,e,a,b,c,52);
148 R3(c,d,e,a,b,53);
149 R3(b,c,d,e,a,54);
150 R3(a,b,c,d,e,55);
151 R3(e,a,b,c,d,56);
152 R3(d,e,a,b,c,57);
153 R3(c,d,e,a,b,58);
154 R3(b,c,d,e,a,59);
155 R4(a,b,c,d,e,60);
156 R4(e,a,b,c,d,61);
157 R4(d,e,a,b,c,62);
158 R4(c,d,e,a,b,63);
159 R4(b,c,d,e,a,64);
160 R4(a,b,c,d,e,65);
161 R4(e,a,b,c,d,66);
162 R4(d,e,a,b,c,67);
163 R4(c,d,e,a,b,68);
164 R4(b,c,d,e,a,69);
165 R4(a,b,c,d,e,70);
166 R4(e,a,b,c,d,71);
167 R4(d,e,a,b,c,72);
168 R4(c,d,e,a,b,73);
169 R4(b,c,d,e,a,74);
170 R4(a,b,c,d,e,75);
171 R4(e,a,b,c,d,76);
172 R4(d,e,a,b,c,77);
173 R4(c,d,e,a,b,78);
174 R4(b,c,d,e,a,79);
175
176 /* Add the working vars back into context.state[] */
177 state[0] += a;
178 state[1] += b;
179 state[2] += c;
180 state[3] += d;
181 state[4] += e;
182 }
183 /*}}}*/
184
185 // SHA1SumValue::SHA1SumValue - Constructs the summation from a string /*{{{*/
186 // ---------------------------------------------------------------------
187 /* The string form of a SHA1 is a 40 character hex number */
188 SHA1SumValue::SHA1SumValue(string Str)
189 {
190 memset(Sum,0,sizeof(Sum));
191 Set(Str);
192 }
193
194 /*}}} */
195 // SHA1SumValue::SHA1SumValue - Default constructor /*{{{*/
196 // ---------------------------------------------------------------------
197 /* Sets the value to 0 */
198 SHA1SumValue::SHA1SumValue()
199 {
200 memset(Sum,0,sizeof(Sum));
201 }
202
203 /*}}} */
204 // SHA1SumValue::Set - Set the sum from a string /*{{{*/
205 // ---------------------------------------------------------------------
206 /* Converts the hex string into a set of chars */
207 bool SHA1SumValue::Set(string Str)
208 {
209 return Hex2Num(Str,Sum,sizeof(Sum));
210 }
211
212 /*}}} */
213 // SHA1SumValue::Value - Convert the number into a string /*{{{*/
214 // ---------------------------------------------------------------------
215 /* Converts the set of chars into a hex string in lower case */
216 string SHA1SumValue::Value() const
217 {
218 char Conv[16] =
219 { '0','1','2','3','4','5','6','7','8','9','a','b',
220 'c','d','e','f'
221 };
222 char Result[41];
223 Result[40] = 0;
224
225 // Convert each char into two letters
226 int J = 0;
227 int I = 0;
228 for (; I != 40; J++,I += 2)
229 {
230 Result[I] = Conv[Sum[J] >> 4];
231 Result[I + 1] = Conv[Sum[J] & 0xF];
232 }
233
234 return string(Result);
235 }
236
237 /*}}} */
238 // SHA1SumValue::operator == - Comparator /*{{{*/
239 // ---------------------------------------------------------------------
240 /* Call memcmp on the buffer */
241 bool SHA1SumValue::operator == (const SHA1SumValue & rhs) const
242 {
243 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
244 }
245 /*}}}*/
246 // SHA1Summation::SHA1Summation - Constructor /*{{{*/
247 // ---------------------------------------------------------------------
248 /* */
249 SHA1Summation::SHA1Summation()
250 {
251 uint32_t *state = (uint32_t *)State;
252 uint32_t *count = (uint32_t *)Count;
253
254 /* SHA1 initialization constants */
255 state[0] = 0x67452301;
256 state[1] = 0xEFCDAB89;
257 state[2] = 0x98BADCFE;
258 state[3] = 0x10325476;
259 state[4] = 0xC3D2E1F0;
260 count[0] = 0;
261 count[1] = 0;
262 Done = false;
263 }
264 /*}}}*/
265 // SHA1Summation::Result - Return checksum value /*{{{*/
266 // ---------------------------------------------------------------------
267 /* Add() may not be called after this */
268 SHA1SumValue SHA1Summation::Result()
269 {
270 uint32_t *state = (uint32_t *)State;
271 uint32_t *count = (uint32_t *)Count;
272
273 // Apply the padding
274 if (Done == false)
275 {
276 unsigned char finalcount[8];
277
278 for (unsigned i = 0; i < 8; i++)
279 {
280 // Endian independent
281 finalcount[i] = (unsigned char) ((count[(i >= 4 ? 0 : 1)]
282 >> ((3 - (i & 3)) * 8)) & 255);
283 }
284
285 Add((unsigned char *) "\200",1);
286 while ((count[0] & 504) != 448)
287 Add((unsigned char *) "\0",1);
288
289 Add(finalcount,8); /* Should cause a SHA1Transform() */
290
291 }
292
293 Done = true;
294
295 // Transfer over the result
296 SHA1SumValue Value;
297 for (unsigned i = 0; i < 20; i++)
298 {
299 Value.Sum[i] = (unsigned char)
300 ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
301 }
302 return Value;
303 }
304 /*}}}*/
305 // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/
306 // ---------------------------------------------------------------------
307 /* May not be called after Result() is called */
308 bool SHA1Summation::Add(const unsigned char *data,unsigned long len)
309 {
310 if (Done)
311 return false;
312
313 uint32_t *state = (uint32_t *)State;
314 uint32_t *count = (uint32_t *)Count;
315 uint8_t *buffer = (uint8_t *)Buffer;
316 uint32_t i,j;
317
318 j = (count[0] >> 3) & 63;
319 if ((count[0] += len << 3) < (len << 3))
320 count[1]++;
321 count[1] += (len >> 29);
322 if ((j + len) > 63)
323 {
324 memcpy(&buffer[j],data,(i = 64 - j));
325 SHA1Transform(state,buffer);
326 for (; i + 63 < len; i += 64)
327 {
328 SHA1Transform(state,&data[i]);
329 }
330 j = 0;
331 }
332 else
333 i = 0;
334 memcpy(&buffer[j],&data[i],len - i);
335
336 return true;
337 }
338 /*}}}*/
339 // SHA1Summation::AddFD - Add content of file into the checksum /*{{{*/
340 // ---------------------------------------------------------------------
341 /* */
342 bool SHA1Summation::AddFD(int Fd,unsigned long Size)
343 {
344 unsigned char Buf[64 * 64];
345 int Res = 0;
346 int ToEOF = (Size == 0);
347 while (Size != 0 || ToEOF)
348 {
349 unsigned n = sizeof(Buf);
350 if (!ToEOF) n = min(Size,(unsigned long)n);
351 Res = read(Fd,Buf,n);
352 if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
353 return false;
354 if (ToEOF && Res == 0) // EOF
355 break;
356 Size -= Res;
357 Add(Buf,Res);
358 }
359 return true;
360 }
361 /*}}}*/