OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

SHA1.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Functions to compute SHA1 message digest of files or memory blocks
00027     according to the NIST specification FIPS-180-1.
00028 
00029     Adapted from the gnulib implementation written by Scott G. Miller with
00030     credits to Robert Klep <robert@ilse.nl> -- Expansion function fix
00031 
00032     Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008 Free Software
00033     Foundation, Inc.
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 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00048 
00050 //  The bytes used to pad buffer to the next 64-byte boundary.
00051 //  (RFC 1321, 3.1: Step 1)
00052 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
00054 
00055 
00056 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
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     // yields 0x01 for big endian
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 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00092 
00093 
00094 void Foam::SHA1::processBytes(const void *data, size_t len)
00095 {
00096     // already finalized, thus need to restart from nothing
00097     if (finalized_)
00098     {
00099         clear();
00100     }
00101 
00102     // complete filling of internal buffer
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             // The regions in the following copy operation do not (cannot) overlap
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     // Process available complete blocks
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     // Move remaining bytes in internal buffer.
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 // SHA1 round constants
00175 #define K1 0x5a827999
00176 #define K2 0x6ed9eba1
00177 #define K3 0x8f1bbcdc
00178 #define K4 0xca62c1d6
00179 
00180 // Round functions.  Note that F2 is the same as F4.
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 // Process LEN bytes of BUFFER, it is assumed that LEN % 64 == 0.
00187 // Most of this code comes from GnuPG's cipher/sha1.c
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     // calculate with sixteen words of 32-bits
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     // First increment the byte count.
00205     // RFC 1321 specifies the possible length of the file up to 2^64 bits.
00206     // Here we only compute the number of bytes.  Do a double word increment.
00207     bufTotal_[0] += len;
00208     if (bufTotal_[0] < len)
00209     {
00210         ++bufTotal_[1];
00211     }
00212 
00213     // rotate left uint32_t by n bits
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         // no data!
00342         dig.clear();
00343     }
00344 }
00345 
00346 
00347 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00348 
00349 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
00350 
00351 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00352 
00353 
00354 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
00355 
00356 
00357 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
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         // account for unprocessed bytes
00382         uint32_t bytes = bufLen_;
00383         size_t size = (bytes < 56 ? 64 : 128) / sizeof(uint32_t);
00384 
00385         // count remaining bytes.
00386         bufTotal_[0] += bytes;
00387         if (bufTotal_[0] < bytes)
00388         {
00389             ++bufTotal_[1];
00390         }
00391 
00392         // finalized, but no data!
00393         if (!bufTotal_[0] && !bufTotal_[1])
00394         {
00395             return false;
00396         }
00397 
00398         // place the 64-bit file length in *bits* at the end of the buffer.
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         // Process remaining bytes
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         // avoid disturbing our data - use a copy
00425         SHA1 sha(*this);
00426         if (sha.finalize())
00427         {
00428             sha.calcDigest(dig);
00429         }
00430     }
00431 
00432     return dig;
00433 }
00434 
00435 
00436 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
00437 
00438 // void Foam::SHA1::operator=(const SHA1& rhs)
00439 // {
00440 //     // Check for assignment to self
00441 //     if (this == &rhs)
00442 //     {
00443 //         FatalErrorIn("Foam::SHA1::operator=(const Foam::SHA1&)")
00444 //             << "Attempted assignment to self"
00445 //             << abort(FatalError);
00446 //     }
00447 // }
00448 
00449 // * * * * * * * * * * * * * * Friend Functions  * * * * * * * * * * * * * * //
00450 
00451 
00452 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
00453 
00454 
00455 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd