]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha1.cc
9416895ac8d5afa0dd7245cb3d579e882b87365f
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 /*{{{*/
34 #include <apt-pkg/sha1.h>
35 #include <apt-pkg/strutl.h>
36 #include <apt-pkg/macros.h>
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 */
49 #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
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))
57 #define blk0(i) block->l[i]
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))
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);
69 static void SHA1Transform(uint32_t state
[5],uint8_t const buffer
[64])
80 uint8_t workspace
[64];
81 block
= (CHAR64LONG16
*)workspace
;
82 memcpy(block
,buffer
,sizeof(workspace
));
84 /* Copy context->state[] to working vars */
91 /* 4 rounds of 20 operations each. Loop unrolled. */
173 /* Add the working vars back into context.state[] */
182 // SHA1Summation::SHA1Summation - Constructor /*{{{*/
183 // ---------------------------------------------------------------------
185 SHA1Summation::SHA1Summation()
187 uint32_t *state
= (uint32_t *)State
;
188 uint32_t *count
= (uint32_t *)Count
;
190 /* SHA1 initialization constants */
191 state
[0] = 0x67452301;
192 state
[1] = 0xEFCDAB89;
193 state
[2] = 0x98BADCFE;
194 state
[3] = 0x10325476;
195 state
[4] = 0xC3D2E1F0;
201 // SHA1Summation::Result - Return checksum value /*{{{*/
202 // ---------------------------------------------------------------------
203 /* Add() may not be called after this */
204 SHA1SumValue
SHA1Summation::Result()
206 uint32_t *state
= (uint32_t *)State
;
207 uint32_t *count
= (uint32_t *)Count
;
212 unsigned char finalcount
[8];
214 for (unsigned i
= 0; i
< 8; i
++)
216 // Endian independent
217 finalcount
[i
] = (unsigned char) ((count
[(i
>= 4 ? 0 : 1)]
218 >> ((3 - (i
& 3)) * 8)) & 255);
221 Add((unsigned char *) "\200",1);
222 while ((count
[0] & 504) != 448)
223 Add((unsigned char *) "\0",1);
225 Add(finalcount
,8); /* Should cause a SHA1Transform() */
231 // Transfer over the result
233 unsigned char res
[20];
234 for (unsigned i
= 0; i
< 20; i
++)
236 res
[i
] = (unsigned char)
237 ((state
[i
>> 2] >> ((3 - (i
& 3)) * 8)) & 255);
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
)
251 uint32_t *state
= (uint32_t *)State
;
252 uint32_t *count
= (uint32_t *)Count
;
253 uint8_t *buffer
= (uint8_t *)Buffer
;
256 j
= (count
[0] >> 3) & 63;
257 if ((count
[0] += len
<< 3) < (len
<< 3))
259 count
[1] += (len
>> 29);
262 memcpy(&buffer
[j
],data
,(i
= 64 - j
));
263 SHA1Transform(state
,buffer
);
264 for (; i
+ 63 < len
; i
+= 64)
266 SHA1Transform(state
,&data
[i
]);
272 memcpy(&buffer
[j
],&data
[i
],len
- i
);