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