| 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 <apt-pkg/sha1.h> |
| 33 | #include <apt-pkg/strutl.h> |
| 34 | #include <apt-pkg/macros.h> |
| 35 | |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| 38 | #include <inttypes.h> |
| 39 | #include <config.h> |
| 40 | /*}}}*/ |
| 41 | |
| 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 */ |
| 47 | |
| 48 | #define rol(value,bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
| 49 | |
| 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)) |
| 55 | #else |
| 56 | #define blk0(i) block->l[i] |
| 57 | #endif |
| 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)) |
| 60 | |
| 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); |
| 67 | |
| 68 | static void SHA1Transform(uint32_t state[5],uint8_t const buffer[64]) |
| 69 | { |
| 70 | uint32_t a,b,c,d,e; |
| 71 | typedef union |
| 72 | { |
| 73 | uint8_t c[64]; |
| 74 | uint32_t l[16]; |
| 75 | } |
| 76 | CHAR64LONG16; |
| 77 | CHAR64LONG16 workspace, *block; |
| 78 | |
| 79 | block = &workspace; |
| 80 | memcpy(block,buffer,sizeof(workspace)); |
| 81 | |
| 82 | /* Copy context->state[] to working vars */ |
| 83 | a = state[0]; |
| 84 | b = state[1]; |
| 85 | c = state[2]; |
| 86 | d = state[3]; |
| 87 | e = state[4]; |
| 88 | |
| 89 | /* 4 rounds of 20 operations each. Loop unrolled. */ |
| 90 | R0(a,b,c,d,e,0); |
| 91 | R0(e,a,b,c,d,1); |
| 92 | R0(d,e,a,b,c,2); |
| 93 | R0(c,d,e,a,b,3); |
| 94 | R0(b,c,d,e,a,4); |
| 95 | R0(a,b,c,d,e,5); |
| 96 | R0(e,a,b,c,d,6); |
| 97 | R0(d,e,a,b,c,7); |
| 98 | R0(c,d,e,a,b,8); |
| 99 | R0(b,c,d,e,a,9); |
| 100 | R0(a,b,c,d,e,10); |
| 101 | R0(e,a,b,c,d,11); |
| 102 | R0(d,e,a,b,c,12); |
| 103 | R0(c,d,e,a,b,13); |
| 104 | R0(b,c,d,e,a,14); |
| 105 | R0(a,b,c,d,e,15); |
| 106 | R1(e,a,b,c,d,16); |
| 107 | R1(d,e,a,b,c,17); |
| 108 | R1(c,d,e,a,b,18); |
| 109 | R1(b,c,d,e,a,19); |
| 110 | R2(a,b,c,d,e,20); |
| 111 | R2(e,a,b,c,d,21); |
| 112 | R2(d,e,a,b,c,22); |
| 113 | R2(c,d,e,a,b,23); |
| 114 | R2(b,c,d,e,a,24); |
| 115 | R2(a,b,c,d,e,25); |
| 116 | R2(e,a,b,c,d,26); |
| 117 | R2(d,e,a,b,c,27); |
| 118 | R2(c,d,e,a,b,28); |
| 119 | R2(b,c,d,e,a,29); |
| 120 | R2(a,b,c,d,e,30); |
| 121 | R2(e,a,b,c,d,31); |
| 122 | R2(d,e,a,b,c,32); |
| 123 | R2(c,d,e,a,b,33); |
| 124 | R2(b,c,d,e,a,34); |
| 125 | R2(a,b,c,d,e,35); |
| 126 | R2(e,a,b,c,d,36); |
| 127 | R2(d,e,a,b,c,37); |
| 128 | R2(c,d,e,a,b,38); |
| 129 | R2(b,c,d,e,a,39); |
| 130 | R3(a,b,c,d,e,40); |
| 131 | R3(e,a,b,c,d,41); |
| 132 | R3(d,e,a,b,c,42); |
| 133 | R3(c,d,e,a,b,43); |
| 134 | R3(b,c,d,e,a,44); |
| 135 | R3(a,b,c,d,e,45); |
| 136 | R3(e,a,b,c,d,46); |
| 137 | R3(d,e,a,b,c,47); |
| 138 | R3(c,d,e,a,b,48); |
| 139 | R3(b,c,d,e,a,49); |
| 140 | R3(a,b,c,d,e,50); |
| 141 | R3(e,a,b,c,d,51); |
| 142 | R3(d,e,a,b,c,52); |
| 143 | R3(c,d,e,a,b,53); |
| 144 | R3(b,c,d,e,a,54); |
| 145 | R3(a,b,c,d,e,55); |
| 146 | R3(e,a,b,c,d,56); |
| 147 | R3(d,e,a,b,c,57); |
| 148 | R3(c,d,e,a,b,58); |
| 149 | R3(b,c,d,e,a,59); |
| 150 | R4(a,b,c,d,e,60); |
| 151 | R4(e,a,b,c,d,61); |
| 152 | R4(d,e,a,b,c,62); |
| 153 | R4(c,d,e,a,b,63); |
| 154 | R4(b,c,d,e,a,64); |
| 155 | R4(a,b,c,d,e,65); |
| 156 | R4(e,a,b,c,d,66); |
| 157 | R4(d,e,a,b,c,67); |
| 158 | R4(c,d,e,a,b,68); |
| 159 | R4(b,c,d,e,a,69); |
| 160 | R4(a,b,c,d,e,70); |
| 161 | R4(e,a,b,c,d,71); |
| 162 | R4(d,e,a,b,c,72); |
| 163 | R4(c,d,e,a,b,73); |
| 164 | R4(b,c,d,e,a,74); |
| 165 | R4(a,b,c,d,e,75); |
| 166 | R4(e,a,b,c,d,76); |
| 167 | R4(d,e,a,b,c,77); |
| 168 | R4(c,d,e,a,b,78); |
| 169 | R4(b,c,d,e,a,79); |
| 170 | |
| 171 | /* Add the working vars back into context.state[] */ |
| 172 | state[0] += a; |
| 173 | state[1] += b; |
| 174 | state[2] += c; |
| 175 | state[3] += d; |
| 176 | state[4] += e; |
| 177 | } |
| 178 | /*}}}*/ |
| 179 | |
| 180 | // SHA1SumValue::SHA1SumValue - Constructs the summation from a string /*{{{*/ |
| 181 | // --------------------------------------------------------------------- |
| 182 | /* The string form of a SHA1 is a 40 character hex number */ |
| 183 | SHA1SumValue::SHA1SumValue(string Str) |
| 184 | { |
| 185 | memset(Sum,0,sizeof(Sum)); |
| 186 | Set(Str); |
| 187 | } |
| 188 | |
| 189 | /*}}} */ |
| 190 | // SHA1SumValue::SHA1SumValue - Default constructor /*{{{*/ |
| 191 | // --------------------------------------------------------------------- |
| 192 | /* Sets the value to 0 */ |
| 193 | SHA1SumValue::SHA1SumValue() |
| 194 | { |
| 195 | memset(Sum,0,sizeof(Sum)); |
| 196 | } |
| 197 | |
| 198 | /*}}} */ |
| 199 | // SHA1SumValue::Set - Set the sum from a string /*{{{*/ |
| 200 | // --------------------------------------------------------------------- |
| 201 | /* Converts the hex string into a set of chars */ |
| 202 | bool SHA1SumValue::Set(string Str) |
| 203 | { |
| 204 | return Hex2Num(Str,Sum,sizeof(Sum)); |
| 205 | } |
| 206 | |
| 207 | /*}}} */ |
| 208 | // SHA1SumValue::Value - Convert the number into a string /*{{{*/ |
| 209 | // --------------------------------------------------------------------- |
| 210 | /* Converts the set of chars into a hex string in lower case */ |
| 211 | string SHA1SumValue::Value() const |
| 212 | { |
| 213 | char Conv[16] = |
| 214 | { '0','1','2','3','4','5','6','7','8','9','a','b', |
| 215 | 'c','d','e','f' |
| 216 | }; |
| 217 | char Result[41]; |
| 218 | Result[40] = 0; |
| 219 | |
| 220 | // Convert each char into two letters |
| 221 | int J = 0; |
| 222 | int I = 0; |
| 223 | for (; I != 40; J++,I += 2) |
| 224 | { |
| 225 | Result[I] = Conv[Sum[J] >> 4]; |
| 226 | Result[I + 1] = Conv[Sum[J] & 0xF]; |
| 227 | } |
| 228 | |
| 229 | return string(Result); |
| 230 | } |
| 231 | |
| 232 | /*}}} */ |
| 233 | // SHA1SumValue::operator == - Comparator /*{{{*/ |
| 234 | // --------------------------------------------------------------------- |
| 235 | /* Call memcmp on the buffer */ |
| 236 | bool SHA1SumValue::operator == (const SHA1SumValue & rhs) const |
| 237 | { |
| 238 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; |
| 239 | } |
| 240 | /*}}}*/ |
| 241 | // SHA1Summation::SHA1Summation - Constructor /*{{{*/ |
| 242 | // --------------------------------------------------------------------- |
| 243 | /* */ |
| 244 | SHA1Summation::SHA1Summation() |
| 245 | { |
| 246 | uint32_t *state = (uint32_t *)State; |
| 247 | uint32_t *count = (uint32_t *)Count; |
| 248 | |
| 249 | /* SHA1 initialization constants */ |
| 250 | state[0] = 0x67452301; |
| 251 | state[1] = 0xEFCDAB89; |
| 252 | state[2] = 0x98BADCFE; |
| 253 | state[3] = 0x10325476; |
| 254 | state[4] = 0xC3D2E1F0; |
| 255 | count[0] = 0; |
| 256 | count[1] = 0; |
| 257 | Done = false; |
| 258 | } |
| 259 | /*}}}*/ |
| 260 | // SHA1Summation::Result - Return checksum value /*{{{*/ |
| 261 | // --------------------------------------------------------------------- |
| 262 | /* Add() may not be called after this */ |
| 263 | SHA1SumValue SHA1Summation::Result() |
| 264 | { |
| 265 | uint32_t *state = (uint32_t *)State; |
| 266 | uint32_t *count = (uint32_t *)Count; |
| 267 | |
| 268 | // Apply the padding |
| 269 | if (Done == false) |
| 270 | { |
| 271 | unsigned char finalcount[8]; |
| 272 | |
| 273 | for (unsigned i = 0; i < 8; i++) |
| 274 | { |
| 275 | // Endian independent |
| 276 | finalcount[i] = (unsigned char) ((count[(i >= 4 ? 0 : 1)] |
| 277 | >> ((3 - (i & 3)) * 8)) & 255); |
| 278 | } |
| 279 | |
| 280 | Add((unsigned char *) "\200",1); |
| 281 | while ((count[0] & 504) != 448) |
| 282 | Add((unsigned char *) "\0",1); |
| 283 | |
| 284 | Add(finalcount,8); /* Should cause a SHA1Transform() */ |
| 285 | |
| 286 | } |
| 287 | |
| 288 | Done = true; |
| 289 | |
| 290 | // Transfer over the result |
| 291 | SHA1SumValue Value; |
| 292 | for (unsigned i = 0; i < 20; i++) |
| 293 | { |
| 294 | Value.Sum[i] = (unsigned char) |
| 295 | ((state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); |
| 296 | } |
| 297 | return Value; |
| 298 | } |
| 299 | /*}}}*/ |
| 300 | // SHA1Summation::Add - Adds content of buffer into the checksum /*{{{*/ |
| 301 | // --------------------------------------------------------------------- |
| 302 | /* May not be called after Result() is called */ |
| 303 | bool SHA1Summation::Add(const unsigned char *data,unsigned long len) |
| 304 | { |
| 305 | if (Done) |
| 306 | return false; |
| 307 | |
| 308 | uint32_t *state = (uint32_t *)State; |
| 309 | uint32_t *count = (uint32_t *)Count; |
| 310 | uint8_t *buffer = (uint8_t *)Buffer; |
| 311 | uint32_t i,j; |
| 312 | |
| 313 | j = (count[0] >> 3) & 63; |
| 314 | if ((count[0] += len << 3) < (len << 3)) |
| 315 | count[1]++; |
| 316 | count[1] += (len >> 29); |
| 317 | if ((j + len) > 63) |
| 318 | { |
| 319 | memcpy(&buffer[j],data,(i = 64 - j)); |
| 320 | SHA1Transform(state,buffer); |
| 321 | for (; i + 63 < len; i += 64) |
| 322 | { |
| 323 | SHA1Transform(state,&data[i]); |
| 324 | } |
| 325 | j = 0; |
| 326 | } |
| 327 | else |
| 328 | i = 0; |
| 329 | memcpy(&buffer[j],&data[i],len - i); |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | /*}}}*/ |
| 334 | // SHA1Summation::AddFD - Add content of file into the checksum /*{{{*/ |
| 335 | // --------------------------------------------------------------------- |
| 336 | /* */ |
| 337 | bool SHA1Summation::AddFD(int Fd,unsigned long Size) |
| 338 | { |
| 339 | unsigned char Buf[64 * 64]; |
| 340 | int Res = 0; |
| 341 | int ToEOF = (Size == 0); |
| 342 | while (Size != 0 || ToEOF) |
| 343 | { |
| 344 | unsigned n = sizeof(Buf); |
| 345 | if (!ToEOF) n = min(Size,(unsigned long)n); |
| 346 | Res = read(Fd,Buf,n); |
| 347 | if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read |
| 348 | return false; |
| 349 | if (ToEOF && Res == 0) // EOF |
| 350 | break; |
| 351 | Size -= Res; |
| 352 | Add(Buf,Res); |
| 353 | } |
| 354 | return true; |
| 355 | } |
| 356 | /*}}}*/ |