SHA1.C 12.4 KB
Newer Older
Mark Olesen's avatar
Mark Olesen committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Henry Weller's avatar
Henry Weller committed
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
Mark Olesen's avatar
Mark Olesen committed
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
Mark Olesen's avatar
Mark Olesen committed
15
16
17
18
19
20
21

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
Mark Olesen's avatar
Mark Olesen committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Description
    Functions to compute SHA1 message digest of files or memory blocks
    according to the NIST specification FIPS-180-1.

    Adapted from the gnulib implementation written by Scott G. Miller with
    credits to Robert Klep <robert@ilse.nl> -- Expansion function fix

    Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008 Free Software
    Foundation, Inc.

\*---------------------------------------------------------------------------*/

#include "SHA1.H"
#include "IOstreams.H"

#include <cstring>

#if defined (__GLIBC__)
42
    #include <endian.h>
Mark Olesen's avatar
Mark Olesen committed
43
44
45
46
47
#endif


// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

48
//! \cond fileScope
49
50
//  The bytes used to pad buffer to the next 64-byte boundary.
//  (RFC 1321, 3.1: Step 1)
Mark Olesen's avatar
Mark Olesen committed
51
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
52
//! \endcond
Mark Olesen's avatar
Mark Olesen committed
53
54
55
56
57
58


// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //

inline uint32_t Foam::SHA1::swapBytes(uint32_t n)
{
59
    #ifdef __BYTE_ORDER
60
        #if (__BYTE_ORDER == __BIG_ENDIAN)
Mark Olesen's avatar
Mark Olesen committed
61
        return n;
62
        #else
63
64
65
66
67
68
69
        return
        (
            ((n) << 24)
          | (((n) & 0xff00) << 8)
          | (((n) >> 8) & 0xff00)
          | ((n) >> 24)
        );
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        #endif
    #else
        const short x = 0x0100;

        // yields 0x01 for big endian
        if (*(reinterpret_cast<const char*>(&x)))
        {
            return n;
        }
        else
        {
            return
            (
                ((n) << 24)
              | (((n) & 0xff00) << 8)
              | (((n) >> 8) & 0xff00)
              | ((n) >> 24)
            );
        }
89
    #endif
Mark Olesen's avatar
Mark Olesen committed
90
91
92
}


93
inline void Foam::SHA1::set_uint32(unsigned char *cp, uint32_t v)
Mark Olesen's avatar
Mark Olesen committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{
    memcpy(cp, &v, sizeof(uint32_t));
}


// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

void Foam::SHA1::processBytes(const void *data, size_t len)
{
    // already finalized, thus need to restart from nothing
    if (finalized_)
    {
        clear();
    }

    // complete filling of internal buffer
    if (bufLen_)
    {
        size_t remaining = bufLen_;
        size_t add =
        (
            sizeof(buffer_) - remaining > len
          ? len
          : sizeof(buffer_) - remaining
        );

        unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_);

        memcpy(&bufp[remaining], data, add);
        bufLen_ += add;

        if (bufLen_ > 64)
        {
            processBlock(buffer_, bufLen_ & ~63);

            bufLen_ &= 63;
130
131
            // The regions in the following copy operation do not
            // (cannot) overlap
Mark Olesen's avatar
Mark Olesen committed
132
133
134
135
136
137
138
139
            memcpy(buffer_, &bufp[(remaining + add) & ~63], bufLen_);
        }

        data = reinterpret_cast<const unsigned char*>(data) + add;
        len -= add;
    }

    // Process available complete blocks
140
141
142
143
144
145
    while (len >= 64)
    {
        processBlock(memcpy(buffer_, data, 64), 64);
        data = reinterpret_cast<const unsigned char*>(data) + 64;
        len -= 64;
    }
Mark Olesen's avatar
Mark Olesen committed
146
147
148
149
150
151
152
153
154
155
156
157
158

    // Move remaining bytes in internal buffer.
    if (len > 0)
    {
        unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_);
        size_t remaining = bufLen_;

        memcpy (&bufp[remaining], data, len);
        remaining += len;
        if (remaining >= 64)
        {
            processBlock(buffer_, 64);
            remaining -= 64;
159
            memcpy(buffer_, &buffer_[16], remaining);
Mark Olesen's avatar
Mark Olesen committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        }
        bufLen_ = remaining;
    }
}


// SHA1 round constants
#define K1 0x5a827999
#define K2 0x6ed9eba1
#define K3 0x8f1bbcdc
#define K4 0xca62c1d6

// Round functions.  Note that F2 is the same as F4.
#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
#define F2(B,C,D) (B ^ C ^ D)
#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
#define F4(B,C,D) (B ^ C ^ D)

// Process LEN bytes of BUFFER, it is assumed that LEN % 64 == 0.
// Most of this code comes from GnuPG's cipher/sha1.c

181
void Foam::SHA1::processBlock(const void *data, size_t len)
Mark Olesen's avatar
Mark Olesen committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
    const uint32_t *words = reinterpret_cast<const uint32_t*>(data);
    size_t nwords = len / sizeof(uint32_t);
    const uint32_t *endp = words + nwords;

    // calculate with sixteen words of 32-bits
    uint32_t x[16];
    uint32_t a = hashsumA_;
    uint32_t b = hashsumB_;
    uint32_t c = hashsumC_;
    uint32_t d = hashsumD_;
    uint32_t e = hashsumE_;

    // First increment the byte count.
    // RFC 1321 specifies the possible length of the file up to 2^64 bits.
    // Here we only compute the number of bytes.  Do a double word increment.
    bufTotal_[0] += len;
    if (bufTotal_[0] < len)
    {
        ++bufTotal_[1];
    }

    // rotate left uint32_t by n bits
205
    #define rol_uint32(x, nbits)  (((x) << (nbits)) | ((x) >> (32 - (nbits))))
Mark Olesen's avatar
Mark Olesen committed
206

207
208
    #define M(I) ( tm = x[I & 0x0F] ^ x[(I-14) & 0x0F]                         \
               ^ x[(I-8) & 0x0F] ^ x[(I-3) & 0x0F]                             \
Mark Olesen's avatar
Mark Olesen committed
209
210
               , (x[I & 0x0F] = rol_uint32(tm, 1)) )

211
212
213
214
215
    #define R(A,B,C,D,E,F,K,M)                                                 \
        do                                                                     \
        {                                                                      \
            E += rol_uint32(A, 5) + F(B, C, D) + K + M;                        \
            B = rol_uint32(B, 30);                                             \
216
        } while (0)
Mark Olesen's avatar
Mark Olesen committed
217
218
219
220

    while (words < endp)
    {
        uint32_t tm;
221
        for (int t = 0; t < 16; ++t)
Mark Olesen's avatar
Mark Olesen committed
222
        {
223
224
            x[t] = swapBytes(*words);
            ++words;
Mark Olesen's avatar
Mark Olesen committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
        }

        R( a, b, c, d, e, F1, K1, x[ 0] );
        R( e, a, b, c, d, F1, K1, x[ 1] );
        R( d, e, a, b, c, F1, K1, x[ 2] );
        R( c, d, e, a, b, F1, K1, x[ 3] );
        R( b, c, d, e, a, F1, K1, x[ 4] );
        R( a, b, c, d, e, F1, K1, x[ 5] );
        R( e, a, b, c, d, F1, K1, x[ 6] );
        R( d, e, a, b, c, F1, K1, x[ 7] );
        R( c, d, e, a, b, F1, K1, x[ 8] );
        R( b, c, d, e, a, F1, K1, x[ 9] );
        R( a, b, c, d, e, F1, K1, x[10] );
        R( e, a, b, c, d, F1, K1, x[11] );
        R( d, e, a, b, c, F1, K1, x[12] );
        R( c, d, e, a, b, F1, K1, x[13] );
        R( b, c, d, e, a, F1, K1, x[14] );
        R( a, b, c, d, e, F1, K1, x[15] );
        R( e, a, b, c, d, F1, K1, M(16) );
        R( d, e, a, b, c, F1, K1, M(17) );
        R( c, d, e, a, b, F1, K1, M(18) );
        R( b, c, d, e, a, F1, K1, M(19) );
        R( a, b, c, d, e, F2, K2, M(20) );
        R( e, a, b, c, d, F2, K2, M(21) );
        R( d, e, a, b, c, F2, K2, M(22) );
        R( c, d, e, a, b, F2, K2, M(23) );
        R( b, c, d, e, a, F2, K2, M(24) );
        R( a, b, c, d, e, F2, K2, M(25) );
        R( e, a, b, c, d, F2, K2, M(26) );
        R( d, e, a, b, c, F2, K2, M(27) );
        R( c, d, e, a, b, F2, K2, M(28) );
        R( b, c, d, e, a, F2, K2, M(29) );
        R( a, b, c, d, e, F2, K2, M(30) );
        R( e, a, b, c, d, F2, K2, M(31) );
        R( d, e, a, b, c, F2, K2, M(32) );
        R( c, d, e, a, b, F2, K2, M(33) );
        R( b, c, d, e, a, F2, K2, M(34) );
        R( a, b, c, d, e, F2, K2, M(35) );
        R( e, a, b, c, d, F2, K2, M(36) );
        R( d, e, a, b, c, F2, K2, M(37) );
        R( c, d, e, a, b, F2, K2, M(38) );
        R( b, c, d, e, a, F2, K2, M(39) );
        R( a, b, c, d, e, F3, K3, M(40) );
        R( e, a, b, c, d, F3, K3, M(41) );
        R( d, e, a, b, c, F3, K3, M(42) );
        R( c, d, e, a, b, F3, K3, M(43) );
        R( b, c, d, e, a, F3, K3, M(44) );
        R( a, b, c, d, e, F3, K3, M(45) );
        R( e, a, b, c, d, F3, K3, M(46) );
        R( d, e, a, b, c, F3, K3, M(47) );
        R( c, d, e, a, b, F3, K3, M(48) );
        R( b, c, d, e, a, F3, K3, M(49) );
        R( a, b, c, d, e, F3, K3, M(50) );
        R( e, a, b, c, d, F3, K3, M(51) );
        R( d, e, a, b, c, F3, K3, M(52) );
        R( c, d, e, a, b, F3, K3, M(53) );
        R( b, c, d, e, a, F3, K3, M(54) );
        R( a, b, c, d, e, F3, K3, M(55) );
        R( e, a, b, c, d, F3, K3, M(56) );
        R( d, e, a, b, c, F3, K3, M(57) );
        R( c, d, e, a, b, F3, K3, M(58) );
        R( b, c, d, e, a, F3, K3, M(59) );
        R( a, b, c, d, e, F4, K4, M(60) );
        R( e, a, b, c, d, F4, K4, M(61) );
        R( d, e, a, b, c, F4, K4, M(62) );
        R( c, d, e, a, b, F4, K4, M(63) );
        R( b, c, d, e, a, F4, K4, M(64) );
        R( a, b, c, d, e, F4, K4, M(65) );
        R( e, a, b, c, d, F4, K4, M(66) );
        R( d, e, a, b, c, F4, K4, M(67) );
        R( c, d, e, a, b, F4, K4, M(68) );
        R( b, c, d, e, a, F4, K4, M(69) );
        R( a, b, c, d, e, F4, K4, M(70) );
        R( e, a, b, c, d, F4, K4, M(71) );
        R( d, e, a, b, c, F4, K4, M(72) );
        R( c, d, e, a, b, F4, K4, M(73) );
        R( b, c, d, e, a, F4, K4, M(74) );
        R( a, b, c, d, e, F4, K4, M(75) );
        R( e, a, b, c, d, F4, K4, M(76) );
        R( d, e, a, b, c, F4, K4, M(77) );
        R( c, d, e, a, b, F4, K4, M(78) );
        R( b, c, d, e, a, F4, K4, M(79) );

        a = hashsumA_ += a;
        b = hashsumB_ += b;
        c = hashsumC_ += c;
        d = hashsumD_ += d;
        e = hashsumE_ += e;
    }
}


317
void Foam::SHA1::calcDigest(SHA1Digest& dig) const
Mark Olesen's avatar
Mark Olesen committed
318
319
320
321
322
{
    if (bufTotal_[0] || bufTotal_[1])
    {
        unsigned char *r = dig.v_;

323
324
325
326
327
        set_uint32(r + 0 * sizeof(uint32_t), swapBytes(hashsumA_));
        set_uint32(r + 1 * sizeof(uint32_t), swapBytes(hashsumB_));
        set_uint32(r + 2 * sizeof(uint32_t), swapBytes(hashsumC_));
        set_uint32(r + 3 * sizeof(uint32_t), swapBytes(hashsumD_));
        set_uint32(r + 4 * sizeof(uint32_t), swapBytes(hashsumE_));
Mark Olesen's avatar
Mark Olesen committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    }
    else
    {
        // no data!
        dig.clear();
    }
}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

void Foam::SHA1::clear()
{
    hashsumA_ = 0x67452301;
    hashsumB_ = 0xefcdab89;
    hashsumC_ = 0x98badcfe;
    hashsumD_ = 0x10325476;
    hashsumE_ = 0xc3d2e1f0;

    bufTotal_[0] = bufTotal_[1] = 0;
    bufLen_ = 0;

    finalized_ = false;
}


bool Foam::SHA1::finalize()
{
    if (!finalized_)
    {
        finalized_ = true;

        // account for unprocessed bytes
        uint32_t bytes = bufLen_;
        size_t size = (bytes < 56 ? 64 : 128) / sizeof(uint32_t);

        // count remaining bytes.
        bufTotal_[0] += bytes;
        if (bufTotal_[0] < bytes)
        {
            ++bufTotal_[1];
        }

        // finalized, but no data!
        if (!bufTotal_[0] && !bufTotal_[1])
        {
            return false;
        }

        // place the 64-bit file length in *bits* at the end of the buffer.
        buffer_[size-2] = swapBytes((bufTotal_[1] << 3) | (bufTotal_[0] >> 29));
        buffer_[size-1] = swapBytes(bufTotal_[0] << 3);

        unsigned char* bufp = reinterpret_cast<unsigned char *>(buffer_);

        memcpy(&bufp[bytes], fillbuf, (size-2) * sizeof(uint32_t) - bytes);

        // Process remaining bytes
        processBlock(buffer_, size * sizeof(uint32_t));
    }

    return true;
}


393
Foam::SHA1Digest Foam::SHA1::digest() const
Mark Olesen's avatar
Mark Olesen committed
394
{
395
    SHA1Digest dig;
Mark Olesen's avatar
Mark Olesen committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

    if (finalized_)
    {
        calcDigest(dig);
    }
    else
    {
        // avoid disturbing our data - use a copy
        SHA1 sha(*this);
        if (sha.finalize())
        {
            sha.calcDigest(dig);
        }
    }

    return dig;
}


415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#undef K1
#undef K2
#undef K3
#undef K4

#undef F1
#undef F2
#undef F3
#undef F4

#undef rol_uint32
#undef M
#undef R

Mark Olesen's avatar
Mark Olesen committed
431
// ************************************************************************* //