]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha1.cc
Compare size before data when ordering cache bucket entries
[apt.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 #include <config.h>
33
34 #include <apt-pkg/sha1.h>
35
36 #include <stdint.h>
37 #include <string.h>
38 /*}}}*/
39
40 // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/
41 // ---------------------------------------------------------------------
42 /* The core of the SHA-1 algorithm. This alters an existing SHA-1 hash to
43 reflect the addition of 16 longwords of new data. Other routines convert
44 incoming stream data into 16 long word chunks for this routine */
45
46 #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
47
48 /* blk0() and blk() perform the initial expand. */
49 /* I got the idea of expanding during the round function from SSLeay */
50 #ifndef WORDS_BIGENDIAN
51 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
52 |(rol(block->l[i],8)&0x00FF00FF))
53 #else
54 #define blk0(i) block->l[i]
55 #endif
56 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
57 ^block->l[(i+2)&15]^block->l[i&15],1))
58
59 /* (R0+R1),R2,R3,R4 are the different operations used in SHA1 */
60 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
61 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
62 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
63 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
64 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
65
66 static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64])
67 {
68 uint32_t a,b,c,d,e;
69 typedef union
70 {
71 uint8_t c[64];
72 uint32_t l[16];
73 }
74 CHAR64LONG16;
75 CHAR64LONG16 workspace, *block;
76
77 block = &workspace;
78 memcpy(block,buffer,sizeof(workspace));
79
80 /* Copy context->state[] to working vars */
81 a = state[0];
82 b = state[1];
83 c = state[2];
84 d = state[3];
85 e = state[4];
86
87 /* 4 rounds of 20 operations each. Loop unrolled. */
88 R0(a,b,c,d,e,0);
89 R0(e,a,b,c,d,1);
90 R0(d,e,a,b,c,2);
91 R0(c,d,e,a,b,3);
92 R0(b,c,d,e,a,4);
93 R0(a,b,c,d,e,5);
94 R0(e,a,b,c,d,6);
95 R0(d,e,a,b,c,7);
96 R0(c,d,e,a,b,8);
97 R0(b,c,d,e,a,9);
98 R0(a,b,c,d,e,10);
99 R0(e,a,b,c,d,11);
100 R0(d,e,a,b,c,12);
101 R0(c,d,e,a,b,13);
102 R0(b,c,d,e,a,14);
103 R0(a,b,c,d,e,15);
104 R1(e,a,b,c,d,16);
105 R1(d,e,a,b,c,17);
106 R1(c,d,e,a,b,18);
107 R1(b,c,d,e,a,19);
108 R2(a,b,c,d,e,20);
109 R2(e,a,b,c,d,21);
110 R2(d,e,a,b,c,22);
111 R2(c,d,e,a,b,23);
112 R2(b,c,d,e,a,24);
113 R2(a,b,c,d,e,25);
114 R2(e,a,b,c,d,26);
115 R2(d,e,a,b,c,27);
116 R2(c,d,e,a,b,28);
117 R2(b,c,d,e,a,29);
118 R2(a,b,c,d,e,30);
119 R2(e,a,b,c,d,31);
120 R2(d,e,a,b,c,32);
121 R2(c,d,e,a,b,33);
122 R2(b,c,d,e,a,34);
123 R2(a,b,c,d,e,35);
124 R2(e,a,b,c,d,36);
125 R2(d,e,a,b,c,37);
126 R2(c,d,e,a,b,38);
127 R2(b,c,d,e,a,39);
128 R3(a,b,c,d,e,40);
129 R3(e,a,b,c,d,41);
130 R3(d,e,a,b,c,42);
131 R3(c,d,e,a,b,43);
132 R3(b,c,d,e,a,44);
133 R3(a,b,c,d,e,45);
134 R3(e,a,b,c,d,46);
135 R3(d,e,a,b,c,47);
136 R3(c,d,e,a,b,48);
137 R3(b,c,d,e,a,49);
138 R3(a,b,c,d,e,50);
139 R3(e,a,b,c,d,51);
140 R3(d,e,a,b,c,52);
141 R3(c,d,e,a,b,53);
142 R3(b,c,d,e,a,54);
143 R3(a,b,c,d,e,55);
144 R3(e,a,b,c,d,56);
145 R3(d,e,a,b,c,57);
146 R3(c,d,e,a,b,58);
147 R3(b,c,d,e,a,59);
148 R4(a,b,c,d,e,60);
149 R4(e,a,b,c,d,61);
150 R4(d,e,a,b,c,62);
151 R4(c,d,e,a,b,63);
152 R4(b,c,d,e,a,64);
153 R4(a,b,c,d,e,65);
154 R4(e,a,b,c,d,66);
155 R4(d,e,a,b,c,67);
156 R4(c,d,e,a,b,68);
157 R4(b,c,d,e,a,69);
158 R4(a,b,c,d,e,70);
159 R4(e,a,b,c,d,71);
160 R4(d,e,a,b,c,72);
161 R4(c,d,e,a,b,73);
162 R4(b,c,d,e,a,74);
163 R4(a,b,c,d,e,75);
164 R4(e,a,b,c,d,76);
165 R4(d,e,a,b,c,77);
166 R4(c,d,e,a,b,78);
167 R4(b,c,d,e,a,79);
168
169 /* Add the working vars back into context.state[] */
170 state[0] += a;
171 state[1] += b;
172 state[2] += c;
173 state[3] += d;
174 state[4] += e;
175 }
176 /*}}}*/
177
178 // SHA1Summation::SHA1Summation - Constructor /*{{{*/
179 // ---------------------------------------------------------------------
180 /* */
181 SHA1Summation::SHA1Summation()
182 {
183 uint32_t *state = (uint32_t *)State;
184 uint32_t *count = (uint32_t *)Count;
185
186 /* SHA1 initialization constants */
187 state[0] = 0x67452301;
188 state[1] = 0xEFCDAB89;
189 state[2] = 0x98BADCFE;
190 state[3] = 0x10325476;
191 state[4] = 0xC3D2E1F0;
192 count[0] = 0;
193 count[1] = 0;
194 Done = false;
195 }
196 /*}}}*/
197 // SHA1Summation::Result - Return checksum value /*{{{*/
198 // ---------------------------------------------------------------------
199 /* Add() may not be called after this */
200 SHA1SumValue SHA1Summation::Result()
201 {
202 uint32_t *state = (uint32_t *)State;
203 uint32_t *count = (uint32_t *)Count;
204
205 // Apply the padding
206 if (Done == false)
207 {
208 unsigned char finalcount[8];
209
210 for (unsigned i = 0; i < 8; i++)
211 {
212 // Endian independent
213 finalcount[i] = (unsigned char) ((count[(i >= 4 ? 0 : 1)]
214 >> ((3 - (i & 3)) * 8)) & 255);
215 }
216
217 Add((unsigned char *) "\200",1);
218 while ((count[0] & 504) != 448)
219 Add((unsigned char *) "\0",1);
220
221 Add(finalcount,8); /* Should cause a SHA1Transform() */
222
223 }
224
225 Done = true;
226
227 // Transfer over the result
228 SHA1SumValue Value;
229 unsigned char res[20];
230 for (unsigned i = 0; i < 20; i++)
231 {
232 res[i] = (unsigned char)
233 ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
234 }
235 Value.Set(res);
236 return Value;
237 }
238 /*}}}*/
239 // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/
240 // ---------------------------------------------------------------------
241 /* May not be called after Result() is called */
242 bool SHA1Summation::Add(const unsigned char *data,unsigned long long len)
243 {
244 if (Done)
245 return false;
246 if (len == 0)
247 return true;
248
249 uint32_t *state = (uint32_t *)State;
250 uint32_t *count = (uint32_t *)Count;
251 uint8_t *buffer = (uint8_t *)Buffer;
252 uint32_t i,j;
253
254 j = (count[0] >> 3) & 63;
255 if ((count[0] += len << 3) < (len << 3))
256 count[1]++;
257 count[1] += (len >> 29);
258 if ((j + len) > 63)
259 {
260 memcpy(&buffer[j],data,(i = 64 - j));
261 SHA1Transform(state,buffer);
262 for (; i + 63 < len; i += 64)
263 {
264 SHA1Transform(state,&data[i]);
265 }
266 j = 0;
267 }
268 else
269 i = 0;
270 memcpy(&buffer[j],&data[i],len - i);
271
272 return true;
273 }
274 /*}}}*/