]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha1.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: sha1.cc,v 1.3 2001/05/13 05:15:03 jgg Exp $
4 /* ######################################################################
6 SHA1 - SHA-1 Secure Hash Algorithm.
8 This file is a Public Domain wrapper for the Public Domain SHA1
9 calculation code that is at it's end.
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>.
15 Modifications for APT were done by Alfredo K. Kojima and Jason
18 Still in the public domain.
20 Test Vectors (from FIPS PUB 180-1)
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
28 #####################################################################
31 // Include Files /*{{{*/
32 #include <apt-pkg/sha1.h>
33 #include <apt-pkg/strutl.h>
42 // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/
43 // ---------------------------------------------------------------------
44 /* The core of the SHA-1 algorithm. This alters an existing SHA-1 hash to
45 reflect the addition of 16 longwords of new data. Other routines convert
46 incoming stream data into 16 long word chunks for this routine */
48 #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
50 /* blk0() and blk() perform the initial expand. */
51 /* I got the idea of expanding during the round function from SSLeay */
52 #ifndef WORDS_BIGENDIAN
53 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
54 |(rol(block->l[i],8)&0x00FF00FF))
56 #define blk0(i) block->l[i]
58 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
59 ^block->l[(i+2)&15]^block->l[i&15],1))
61 /* (R0+R1),R2,R3,R4 are the different operations used in SHA1 */
62 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
63 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
64 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
65 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
66 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
68 static void SHA1Transform(uint32_t state
[5],uint8_t const buffer
[64])
79 uint8_t workspace
[64];
80 block
= (CHAR64LONG16
*)workspace
;
81 memcpy(block
,buffer
,sizeof(workspace
));
83 /* Copy context->state[] to working vars */
90 /* 4 rounds of 20 operations each. Loop unrolled. */
172 /* Add the working vars back into context.state[] */
181 // SHA1SumValue::SHA1SumValue - Constructs the summation from a string /*{{{*/
182 // ---------------------------------------------------------------------
183 /* The string form of a SHA1 is a 40 character hex number */
184 SHA1SumValue::SHA1SumValue(string Str
)
186 memset(Sum
,0,sizeof(Sum
));
191 // SHA1SumValue::SHA1SumValue - Default constructor /*{{{*/
192 // ---------------------------------------------------------------------
193 /* Sets the value to 0 */
194 SHA1SumValue::SHA1SumValue()
196 memset(Sum
,0,sizeof(Sum
));
200 // SHA1SumValue::Set - Set the sum from a string /*{{{*/
201 // ---------------------------------------------------------------------
202 /* Converts the hex string into a set of chars */
203 bool SHA1SumValue::Set(string Str
)
205 return Hex2Num(Str
,Sum
,sizeof(Sum
));
209 // SHA1SumValue::Value - Convert the number into a string /*{{{*/
210 // ---------------------------------------------------------------------
211 /* Converts the set of chars into a hex string in lower case */
212 string
SHA1SumValue::Value() const
215 { '0','1','2','3','4','5','6','7','8','9','a','b',
221 // Convert each char into two letters
224 for (; I
!= 40; J
++,I
+= 2)
226 Result
[I
] = Conv
[Sum
[J
] >> 4];
227 Result
[I
+ 1] = Conv
[Sum
[J
] & 0xF];
230 return string(Result
);
234 // SHA1SumValue::operator == - Comparator /*{{{*/
235 // ---------------------------------------------------------------------
236 /* Call memcmp on the buffer */
237 bool SHA1SumValue::operator == (const SHA1SumValue
& rhs
) const
239 return memcmp(Sum
,rhs
.Sum
,sizeof(Sum
)) == 0;
242 // SHA1Summation::SHA1Summation - Constructor /*{{{*/
243 // ---------------------------------------------------------------------
245 SHA1Summation::SHA1Summation()
247 uint32_t *state
= (uint32_t *)State
;
248 uint32_t *count
= (uint32_t *)Count
;
250 /* SHA1 initialization constants */
251 state
[0] = 0x67452301;
252 state
[1] = 0xEFCDAB89;
253 state
[2] = 0x98BADCFE;
254 state
[3] = 0x10325476;
255 state
[4] = 0xC3D2E1F0;
261 // SHA1Summation::Result - Return checksum value /*{{{*/
262 // ---------------------------------------------------------------------
263 /* Add() may not be called after this */
264 SHA1SumValue
SHA1Summation::Result()
266 uint32_t *state
= (uint32_t *)State
;
267 uint32_t *count
= (uint32_t *)Count
;
272 unsigned char finalcount
[8];
274 for (unsigned i
= 0; i
< 8; i
++)
276 // Endian independent
277 finalcount
[i
] = (unsigned char) ((count
[(i
>= 4 ? 0 : 1)]
278 >> ((3 - (i
& 3)) * 8)) & 255);
281 Add((unsigned char *) "\200",1);
282 while ((count
[0] & 504) != 448)
283 Add((unsigned char *) "\0",1);
285 Add(finalcount
,8); /* Should cause a SHA1Transform() */
291 // Transfer over the result
293 for (unsigned i
= 0; i
< 20; i
++)
295 Value
.Sum
[i
] = (unsigned char)
296 ((state
[i
>> 2] >> ((3 - (i
& 3)) * 8)) & 255);
301 // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/
302 // ---------------------------------------------------------------------
303 /* May not be called after Result() is called */
304 bool SHA1Summation::Add(const unsigned char *data
,unsigned long len
)
309 uint32_t *state
= (uint32_t *)State
;
310 uint32_t *count
= (uint32_t *)Count
;
311 uint8_t *buffer
= (uint8_t *)Buffer
;
314 j
= (count
[0] >> 3) & 63;
315 if ((count
[0] += len
<< 3) < (len
<< 3))
317 count
[1] += (len
>> 29);
320 memcpy(&buffer
[j
],data
,(i
= 64 - j
));
321 SHA1Transform(state
,buffer
);
322 for (; i
+ 63 < len
; i
+= 64)
324 SHA1Transform(state
,&data
[i
]);
330 memcpy(&buffer
[j
],&data
[i
],len
- i
);
335 // SHA1Summation::AddFD - Add content of file into the checksum /*{{{*/
336 // ---------------------------------------------------------------------
338 bool SHA1Summation::AddFD(int Fd
,unsigned long Size
)
340 unsigned char Buf
[64 * 64];
342 int ToEOF
= (Size
== 0);
343 while (Size
!= 0 || ToEOF
)
345 unsigned n
= sizeof(Buf
);
346 if (!ToEOF
) n
= min(Size
,(unsigned long)n
);
347 Res
= read(Fd
,Buf
,n
);
348 if (Res
< 0 || (!ToEOF
&& (unsigned) Res
!= n
)) // error, or short read
350 if (ToEOF
&& Res
== 0) // EOF