00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "SHA1.H"
00038 #include "IOstreams.H"
00039
00040 #include <cstring>
00041
00042 #if defined (__GLIBC__)
00043 # include <endian.h>
00044 #endif
00045
00046
00047
00048
00050
00051
00052 static const unsigned char fillbuf[64] = { 0x80, 0 };
00054
00055
00056
00057
00058 inline uint32_t Foam::SHA1::swapBytes(uint32_t n)
00059 {
00060 #ifdef __BYTE_ORDER
00061 # if (__BYTE_ORDER == __BIG_ENDIAN)
00062 return n;
00063 # else
00064 return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24));
00065 # endif
00066
00067 #else
00068
00069 const short x = 0x0100;
00070
00071
00072 if (*(reinterpret_cast<const char *>(&x)))
00073 {
00074 return n;
00075 }
00076 else
00077 {
00078 return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24));
00079 }
00080 #endif
00081 }
00082
00083
00084 inline void
00085 Foam::SHA1::set_uint32(unsigned char *cp, uint32_t v)
00086 {
00087 memcpy(cp, &v, sizeof(uint32_t));
00088 }
00089
00090
00091
00092
00093
00094 void Foam::SHA1::processBytes(const void *data, size_t len)
00095 {
00096
00097 if (finalized_)
00098 {
00099 clear();
00100 }
00101
00102
00103 if (bufLen_)
00104 {
00105 size_t remaining = bufLen_;
00106 size_t add =
00107 (
00108 sizeof(buffer_) - remaining > len
00109 ? len
00110 : sizeof(buffer_) - remaining
00111 );
00112
00113 unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_);
00114
00115 memcpy(&bufp[remaining], data, add);
00116 bufLen_ += add;
00117
00118 if (bufLen_ > 64)
00119 {
00120 processBlock(buffer_, bufLen_ & ~63);
00121
00122 bufLen_ &= 63;
00123
00124 memcpy(buffer_, &bufp[(remaining + add) & ~63], bufLen_);
00125 }
00126
00127 data = reinterpret_cast<const unsigned char*>(data) + add;
00128 len -= add;
00129 }
00130
00131
00132 if (len >= 64)
00133 {
00134 #if !_STRING_ARCH_unaligned
00135 # define alignof(type) offsetof (struct { char c; type x; }, x)
00136 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
00137 if (UNALIGNED_P (data))
00138 {
00139 while (len > 64)
00140 {
00141 processBlock(memcpy (buffer_, data, 64), 64);
00142 data = reinterpret_cast<const unsigned char*>(data) + 64;
00143 len -= 64;
00144 }
00145 }
00146 else
00147 #endif
00148 {
00149 processBlock(data, len & ~63);
00150 data = reinterpret_cast<const unsigned char*>(data) + (len & ~63);
00151 len &= 63;
00152 }
00153 }
00154
00155
00156 if (len > 0)
00157 {
00158 unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_);
00159 size_t remaining = bufLen_;
00160
00161 memcpy (&bufp[remaining], data, len);
00162 remaining += len;
00163 if (remaining >= 64)
00164 {
00165 processBlock(buffer_, 64);
00166 remaining -= 64;
00167 memcpy (buffer_, &buffer_[16], remaining);
00168 }
00169 bufLen_ = remaining;
00170 }
00171 }
00172
00173
00174
00175 #define K1 0x5a827999
00176 #define K2 0x6ed9eba1
00177 #define K3 0x8f1bbcdc
00178 #define K4 0xca62c1d6
00179
00180
00181 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
00182 #define F2(B,C,D) (B ^ C ^ D)
00183 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
00184 #define F4(B,C,D) (B ^ C ^ D)
00185
00186
00187
00188
00189 void
00190 Foam::SHA1::processBlock(const void *data, size_t len)
00191 {
00192 const uint32_t *words = reinterpret_cast<const uint32_t*>(data);
00193 size_t nwords = len / sizeof(uint32_t);
00194 const uint32_t *endp = words + nwords;
00195
00196
00197 uint32_t x[16];
00198 uint32_t a = hashsumA_;
00199 uint32_t b = hashsumB_;
00200 uint32_t c = hashsumC_;
00201 uint32_t d = hashsumD_;
00202 uint32_t e = hashsumE_;
00203
00204
00205
00206
00207 bufTotal_[0] += len;
00208 if (bufTotal_[0] < len)
00209 {
00210 ++bufTotal_[1];
00211 }
00212
00213
00214 #define rol_uint32(x, nbits) (((x) << (nbits)) | ((x) >> (32 - (nbits))))
00215
00216 #define M(I) ( tm = x[I & 0x0F] ^ x[(I-14) & 0x0F] \
00217 ^ x[(I-8) & 0x0F] ^ x[(I-3) & 0x0F] \
00218 , (x[I & 0x0F] = rol_uint32(tm, 1)) )
00219
00220
00221 #define R(A,B,C,D,E,F,K,M) \
00222 do \
00223 { \
00224 E += rol_uint32(A, 5) + F(B, C, D) + K + M; \
00225 B = rol_uint32(B, 30); \
00226 } while(0)
00227
00228 while (words < endp)
00229 {
00230 uint32_t tm;
00231 for (int t = 0; t < 16; t++)
00232 {
00233 x[t] = swapBytes (*words);
00234 words++;
00235 }
00236
00237 R( a, b, c, d, e, F1, K1, x[ 0] );
00238 R( e, a, b, c, d, F1, K1, x[ 1] );
00239 R( d, e, a, b, c, F1, K1, x[ 2] );
00240 R( c, d, e, a, b, F1, K1, x[ 3] );
00241 R( b, c, d, e, a, F1, K1, x[ 4] );
00242 R( a, b, c, d, e, F1, K1, x[ 5] );
00243 R( e, a, b, c, d, F1, K1, x[ 6] );
00244 R( d, e, a, b, c, F1, K1, x[ 7] );
00245 R( c, d, e, a, b, F1, K1, x[ 8] );
00246 R( b, c, d, e, a, F1, K1, x[ 9] );
00247 R( a, b, c, d, e, F1, K1, x[10] );
00248 R( e, a, b, c, d, F1, K1, x[11] );
00249 R( d, e, a, b, c, F1, K1, x[12] );
00250 R( c, d, e, a, b, F1, K1, x[13] );
00251 R( b, c, d, e, a, F1, K1, x[14] );
00252 R( a, b, c, d, e, F1, K1, x[15] );
00253 R( e, a, b, c, d, F1, K1, M(16) );
00254 R( d, e, a, b, c, F1, K1, M(17) );
00255 R( c, d, e, a, b, F1, K1, M(18) );
00256 R( b, c, d, e, a, F1, K1, M(19) );
00257 R( a, b, c, d, e, F2, K2, M(20) );
00258 R( e, a, b, c, d, F2, K2, M(21) );
00259 R( d, e, a, b, c, F2, K2, M(22) );
00260 R( c, d, e, a, b, F2, K2, M(23) );
00261 R( b, c, d, e, a, F2, K2, M(24) );
00262 R( a, b, c, d, e, F2, K2, M(25) );
00263 R( e, a, b, c, d, F2, K2, M(26) );
00264 R( d, e, a, b, c, F2, K2, M(27) );
00265 R( c, d, e, a, b, F2, K2, M(28) );
00266 R( b, c, d, e, a, F2, K2, M(29) );
00267 R( a, b, c, d, e, F2, K2, M(30) );
00268 R( e, a, b, c, d, F2, K2, M(31) );
00269 R( d, e, a, b, c, F2, K2, M(32) );
00270 R( c, d, e, a, b, F2, K2, M(33) );
00271 R( b, c, d, e, a, F2, K2, M(34) );
00272 R( a, b, c, d, e, F2, K2, M(35) );
00273 R( e, a, b, c, d, F2, K2, M(36) );
00274 R( d, e, a, b, c, F2, K2, M(37) );
00275 R( c, d, e, a, b, F2, K2, M(38) );
00276 R( b, c, d, e, a, F2, K2, M(39) );
00277 R( a, b, c, d, e, F3, K3, M(40) );
00278 R( e, a, b, c, d, F3, K3, M(41) );
00279 R( d, e, a, b, c, F3, K3, M(42) );
00280 R( c, d, e, a, b, F3, K3, M(43) );
00281 R( b, c, d, e, a, F3, K3, M(44) );
00282 R( a, b, c, d, e, F3, K3, M(45) );
00283 R( e, a, b, c, d, F3, K3, M(46) );
00284 R( d, e, a, b, c, F3, K3, M(47) );
00285 R( c, d, e, a, b, F3, K3, M(48) );
00286 R( b, c, d, e, a, F3, K3, M(49) );
00287 R( a, b, c, d, e, F3, K3, M(50) );
00288 R( e, a, b, c, d, F3, K3, M(51) );
00289 R( d, e, a, b, c, F3, K3, M(52) );
00290 R( c, d, e, a, b, F3, K3, M(53) );
00291 R( b, c, d, e, a, F3, K3, M(54) );
00292 R( a, b, c, d, e, F3, K3, M(55) );
00293 R( e, a, b, c, d, F3, K3, M(56) );
00294 R( d, e, a, b, c, F3, K3, M(57) );
00295 R( c, d, e, a, b, F3, K3, M(58) );
00296 R( b, c, d, e, a, F3, K3, M(59) );
00297 R( a, b, c, d, e, F4, K4, M(60) );
00298 R( e, a, b, c, d, F4, K4, M(61) );
00299 R( d, e, a, b, c, F4, K4, M(62) );
00300 R( c, d, e, a, b, F4, K4, M(63) );
00301 R( b, c, d, e, a, F4, K4, M(64) );
00302 R( a, b, c, d, e, F4, K4, M(65) );
00303 R( e, a, b, c, d, F4, K4, M(66) );
00304 R( d, e, a, b, c, F4, K4, M(67) );
00305 R( c, d, e, a, b, F4, K4, M(68) );
00306 R( b, c, d, e, a, F4, K4, M(69) );
00307 R( a, b, c, d, e, F4, K4, M(70) );
00308 R( e, a, b, c, d, F4, K4, M(71) );
00309 R( d, e, a, b, c, F4, K4, M(72) );
00310 R( c, d, e, a, b, F4, K4, M(73) );
00311 R( b, c, d, e, a, F4, K4, M(74) );
00312 R( a, b, c, d, e, F4, K4, M(75) );
00313 R( e, a, b, c, d, F4, K4, M(76) );
00314 R( d, e, a, b, c, F4, K4, M(77) );
00315 R( c, d, e, a, b, F4, K4, M(78) );
00316 R( b, c, d, e, a, F4, K4, M(79) );
00317
00318 a = hashsumA_ += a;
00319 b = hashsumB_ += b;
00320 c = hashsumC_ += c;
00321 d = hashsumD_ += d;
00322 e = hashsumE_ += e;
00323 }
00324 }
00325
00326
00327 void Foam::SHA1::calcDigest(SHA1Digest& dig) const
00328 {
00329 if (bufTotal_[0] || bufTotal_[1])
00330 {
00331 unsigned char *r = dig.v_;
00332
00333 set_uint32 (r + 0 * sizeof(uint32_t), swapBytes(hashsumA_));
00334 set_uint32 (r + 1 * sizeof(uint32_t), swapBytes(hashsumB_));
00335 set_uint32 (r + 2 * sizeof(uint32_t), swapBytes(hashsumC_));
00336 set_uint32 (r + 3 * sizeof(uint32_t), swapBytes(hashsumD_));
00337 set_uint32 (r + 4 * sizeof(uint32_t), swapBytes(hashsumE_));
00338 }
00339 else
00340 {
00341
00342 dig.clear();
00343 }
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 void Foam::SHA1::clear()
00361 {
00362 hashsumA_ = 0x67452301;
00363 hashsumB_ = 0xefcdab89;
00364 hashsumC_ = 0x98badcfe;
00365 hashsumD_ = 0x10325476;
00366 hashsumE_ = 0xc3d2e1f0;
00367
00368 bufTotal_[0] = bufTotal_[1] = 0;
00369 bufLen_ = 0;
00370
00371 finalized_ = false;
00372 }
00373
00374
00375 bool Foam::SHA1::finalize()
00376 {
00377 if (!finalized_)
00378 {
00379 finalized_ = true;
00380
00381
00382 uint32_t bytes = bufLen_;
00383 size_t size = (bytes < 56 ? 64 : 128) / sizeof(uint32_t);
00384
00385
00386 bufTotal_[0] += bytes;
00387 if (bufTotal_[0] < bytes)
00388 {
00389 ++bufTotal_[1];
00390 }
00391
00392
00393 if (!bufTotal_[0] && !bufTotal_[1])
00394 {
00395 return false;
00396 }
00397
00398
00399 buffer_[size-2] = swapBytes((bufTotal_[1] << 3) | (bufTotal_[0] >> 29));
00400 buffer_[size-1] = swapBytes(bufTotal_[0] << 3);
00401
00402 unsigned char* bufp = reinterpret_cast<unsigned char *>(buffer_);
00403
00404 memcpy(&bufp[bytes], fillbuf, (size-2) * sizeof(uint32_t) - bytes);
00405
00406
00407 processBlock(buffer_, size * sizeof(uint32_t));
00408 }
00409
00410 return true;
00411 }
00412
00413
00414 Foam::SHA1Digest Foam::SHA1::digest() const
00415 {
00416 SHA1Digest dig;
00417
00418 if (finalized_)
00419 {
00420 calcDigest(dig);
00421 }
00422 else
00423 {
00424
00425 SHA1 sha(*this);
00426 if (sha.finalize())
00427 {
00428 sha.calcDigest(dig);
00429 }
00430 }
00431
00432 return dig;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455