SHA1.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Functions to compute SHA1 message digest of files or memory blocks
26  according to the NIST specification FIPS-180-1.
27 
28  Adapted from the gnulib implementation written by Scott G. Miller with
29  credits to Robert Klep <robert@ilse.nl> -- Expansion function fix
30 
31  Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008 Free Software
32  Foundation, Inc.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #include "SHA1.H"
37 #include "IOstreams.H"
38 
39 #include <cstring>
40 
41 #if defined (__GLIBC__)
42  #include <endian.h>
43 #endif
44 
45 
46 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
47 
49 // The bytes used to pad buffer to the next 64-byte boundary.
50 // (RFC 1321, 3.1: Step 1)
51 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
53 
54 
55 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
56 
57 inline uint32_t Foam::SHA1::swapBytes(uint32_t n)
58 {
59  #ifdef __BYTE_ORDER
60  #if (__BYTE_ORDER == __BIG_ENDIAN)
61  return n;
62  #else
63  return
64  (
65  ((n) << 24)
66  | (((n) & 0xff00) << 8)
67  | (((n) >> 8) & 0xff00)
68  | ((n) >> 24)
69  );
70  #endif
71  #else
72  const short x = 0x0100;
73 
74  // yields 0x01 for big endian
75  if (*(reinterpret_cast<const char*>(&x)))
76  {
77  return n;
78  }
79  else
80  {
81  return
82  (
83  ((n) << 24)
84  | (((n) & 0xff00) << 8)
85  | (((n) >> 8) & 0xff00)
86  | ((n) >> 24)
87  );
88  }
89  #endif
90 }
91 
92 
93 inline void Foam::SHA1::set_uint32(unsigned char *cp, uint32_t v)
94 {
95  memcpy(cp, &v, sizeof(uint32_t));
96 }
97 
98 
99 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
100 
101 void Foam::SHA1::processBytes(const void *data, size_t len)
102 {
103  // already finalized, thus need to restart from nothing
104  if (finalized_)
105  {
106  clear();
107  }
108 
109  // complete filling of internal buffer
110  if (bufLen_)
111  {
112  size_t remaining = bufLen_;
113  size_t add =
114  (
115  sizeof(buffer_) - remaining > len
116  ? len
117  : sizeof(buffer_) - remaining
118  );
119 
120  unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_);
121 
122  memcpy(&bufp[remaining], data, add);
123  bufLen_ += add;
124 
125  if (bufLen_ > 64)
126  {
127  processBlock(buffer_, bufLen_ & ~63);
128 
129  bufLen_ &= 63;
130  // The regions in the following copy operation do not
131  // (cannot) overlap
132  memcpy(buffer_, &bufp[(remaining + add) & ~63], bufLen_);
133  }
134 
135  data = reinterpret_cast<const unsigned char*>(data) + add;
136  len -= add;
137  }
138 
139  // Process available complete blocks
140  while (len >= 64)
141  {
142  processBlock(memcpy(buffer_, data, 64), 64);
143  data = reinterpret_cast<const unsigned char*>(data) + 64;
144  len -= 64;
145  }
146 
147  // Move remaining bytes in internal buffer.
148  if (len > 0)
149  {
150  unsigned char* bufp = reinterpret_cast<unsigned char*>(buffer_);
151  size_t remaining = bufLen_;
152 
153  memcpy (&bufp[remaining], data, len);
154  remaining += len;
155  if (remaining >= 64)
156  {
157  processBlock(buffer_, 64);
158  remaining -= 64;
159  memcpy(buffer_, &buffer_[16], remaining);
160  }
161  bufLen_ = remaining;
162  }
163 }
164 
165 
166 // SHA1 round constants
167 #define K1 0x5a827999
168 #define K2 0x6ed9eba1
169 #define K3 0x8f1bbcdc
170 #define K4 0xca62c1d6
171 
172 // Round functions. Note that F2 is the same as F4.
173 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
174 #define F2(B,C,D) (B ^ C ^ D)
175 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
176 #define F4(B,C,D) (B ^ C ^ D)
177 
178 // Process LEN bytes of BUFFER, it is assumed that LEN % 64 == 0.
179 // Most of this code comes from GnuPG's cipher/sha1.c
180 
181 void Foam::SHA1::processBlock(const void *data, size_t len)
182 {
183  const uint32_t *words = reinterpret_cast<const uint32_t*>(data);
184  size_t nwords = len / sizeof(uint32_t);
185  const uint32_t *endp = words + nwords;
186 
187  // calculate with sixteen words of 32-bits
188  uint32_t x[16];
189  uint32_t a = hashsumA_;
190  uint32_t b = hashsumB_;
191  uint32_t c = hashsumC_;
192  uint32_t d = hashsumD_;
193  uint32_t e = hashsumE_;
194 
195  // First increment the byte count.
196  // RFC 1321 specifies the possible length of the file up to 2^64 bits.
197  // Here we only compute the number of bytes. Do a double word increment.
198  bufTotal_[0] += len;
199  if (bufTotal_[0] < len)
200  {
201  ++bufTotal_[1];
202  }
203 
204  // rotate left uint32_t by n bits
205  #define rol_uint32(x, nbits) (((x) << (nbits)) | ((x) >> (32 - (nbits))))
206 
207  #define M(I) ( tm = x[I & 0x0F] ^ x[(I-14) & 0x0F] \
208  ^ x[(I-8) & 0x0F] ^ x[(I-3) & 0x0F] \
209  , (x[I & 0x0F] = rol_uint32(tm, 1)) )
210 
211  #define R(A,B,C,D,E,F,K,M) \
212  do \
213  { \
214  E += rol_uint32(A, 5) + F(B, C, D) + K + M; \
215  B = rol_uint32(B, 30); \
216  } while (0)
217 
218  while (words < endp)
219  {
220  uint32_t tm;
221  for (int t = 0; t < 16; ++t)
222  {
223  x[t] = swapBytes(*words);
224  ++words;
225  }
226 
227  R( a, b, c, d, e, F1, K1, x[ 0] );
228  R( e, a, b, c, d, F1, K1, x[ 1] );
229  R( d, e, a, b, c, F1, K1, x[ 2] );
230  R( c, d, e, a, b, F1, K1, x[ 3] );
231  R( b, c, d, e, a, F1, K1, x[ 4] );
232  R( a, b, c, d, e, F1, K1, x[ 5] );
233  R( e, a, b, c, d, F1, K1, x[ 6] );
234  R( d, e, a, b, c, F1, K1, x[ 7] );
235  R( c, d, e, a, b, F1, K1, x[ 8] );
236  R( b, c, d, e, a, F1, K1, x[ 9] );
237  R( a, b, c, d, e, F1, K1, x[10] );
238  R( e, a, b, c, d, F1, K1, x[11] );
239  R( d, e, a, b, c, F1, K1, x[12] );
240  R( c, d, e, a, b, F1, K1, x[13] );
241  R( b, c, d, e, a, F1, K1, x[14] );
242  R( a, b, c, d, e, F1, K1, x[15] );
243  R( e, a, b, c, d, F1, K1, M(16) );
244  R( d, e, a, b, c, F1, K1, M(17) );
245  R( c, d, e, a, b, F1, K1, M(18) );
246  R( b, c, d, e, a, F1, K1, M(19) );
247  R( a, b, c, d, e, F2, K2, M(20) );
248  R( e, a, b, c, d, F2, K2, M(21) );
249  R( d, e, a, b, c, F2, K2, M(22) );
250  R( c, d, e, a, b, F2, K2, M(23) );
251  R( b, c, d, e, a, F2, K2, M(24) );
252  R( a, b, c, d, e, F2, K2, M(25) );
253  R( e, a, b, c, d, F2, K2, M(26) );
254  R( d, e, a, b, c, F2, K2, M(27) );
255  R( c, d, e, a, b, F2, K2, M(28) );
256  R( b, c, d, e, a, F2, K2, M(29) );
257  R( a, b, c, d, e, F2, K2, M(30) );
258  R( e, a, b, c, d, F2, K2, M(31) );
259  R( d, e, a, b, c, F2, K2, M(32) );
260  R( c, d, e, a, b, F2, K2, M(33) );
261  R( b, c, d, e, a, F2, K2, M(34) );
262  R( a, b, c, d, e, F2, K2, M(35) );
263  R( e, a, b, c, d, F2, K2, M(36) );
264  R( d, e, a, b, c, F2, K2, M(37) );
265  R( c, d, e, a, b, F2, K2, M(38) );
266  R( b, c, d, e, a, F2, K2, M(39) );
267  R( a, b, c, d, e, F3, K3, M(40) );
268  R( e, a, b, c, d, F3, K3, M(41) );
269  R( d, e, a, b, c, F3, K3, M(42) );
270  R( c, d, e, a, b, F3, K3, M(43) );
271  R( b, c, d, e, a, F3, K3, M(44) );
272  R( a, b, c, d, e, F3, K3, M(45) );
273  R( e, a, b, c, d, F3, K3, M(46) );
274  R( d, e, a, b, c, F3, K3, M(47) );
275  R( c, d, e, a, b, F3, K3, M(48) );
276  R( b, c, d, e, a, F3, K3, M(49) );
277  R( a, b, c, d, e, F3, K3, M(50) );
278  R( e, a, b, c, d, F3, K3, M(51) );
279  R( d, e, a, b, c, F3, K3, M(52) );
280  R( c, d, e, a, b, F3, K3, M(53) );
281  R( b, c, d, e, a, F3, K3, M(54) );
282  R( a, b, c, d, e, F3, K3, M(55) );
283  R( e, a, b, c, d, F3, K3, M(56) );
284  R( d, e, a, b, c, F3, K3, M(57) );
285  R( c, d, e, a, b, F3, K3, M(58) );
286  R( b, c, d, e, a, F3, K3, M(59) );
287  R( a, b, c, d, e, F4, K4, M(60) );
288  R( e, a, b, c, d, F4, K4, M(61) );
289  R( d, e, a, b, c, F4, K4, M(62) );
290  R( c, d, e, a, b, F4, K4, M(63) );
291  R( b, c, d, e, a, F4, K4, M(64) );
292  R( a, b, c, d, e, F4, K4, M(65) );
293  R( e, a, b, c, d, F4, K4, M(66) );
294  R( d, e, a, b, c, F4, K4, M(67) );
295  R( c, d, e, a, b, F4, K4, M(68) );
296  R( b, c, d, e, a, F4, K4, M(69) );
297  R( a, b, c, d, e, F4, K4, M(70) );
298  R( e, a, b, c, d, F4, K4, M(71) );
299  R( d, e, a, b, c, F4, K4, M(72) );
300  R( c, d, e, a, b, F4, K4, M(73) );
301  R( b, c, d, e, a, F4, K4, M(74) );
302  R( a, b, c, d, e, F4, K4, M(75) );
303  R( e, a, b, c, d, F4, K4, M(76) );
304  R( d, e, a, b, c, F4, K4, M(77) );
305  R( c, d, e, a, b, F4, K4, M(78) );
306  R( b, c, d, e, a, F4, K4, M(79) );
307 
308  a = hashsumA_ += a;
309  b = hashsumB_ += b;
310  c = hashsumC_ += c;
311  d = hashsumD_ += d;
312  e = hashsumE_ += e;
313  }
314 }
315 
316 
317 void Foam::SHA1::calcDigest(SHA1Digest& dig) const
318 {
319  if (bufTotal_[0] || bufTotal_[1])
320  {
321  unsigned char *r = dig.v_;
322 
323  set_uint32(r + 0 * sizeof(uint32_t), swapBytes(hashsumA_));
324  set_uint32(r + 1 * sizeof(uint32_t), swapBytes(hashsumB_));
325  set_uint32(r + 2 * sizeof(uint32_t), swapBytes(hashsumC_));
326  set_uint32(r + 3 * sizeof(uint32_t), swapBytes(hashsumD_));
327  set_uint32(r + 4 * sizeof(uint32_t), swapBytes(hashsumE_));
328  }
329  else
330  {
331  // no data!
332  dig.clear();
333  }
334 }
335 
336 
337 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
338 
340 {
341  hashsumA_ = 0x67452301;
342  hashsumB_ = 0xefcdab89;
343  hashsumC_ = 0x98badcfe;
344  hashsumD_ = 0x10325476;
345  hashsumE_ = 0xc3d2e1f0;
346 
347  bufTotal_[0] = bufTotal_[1] = 0;
348  bufLen_ = 0;
349 
350  finalized_ = false;
351 }
352 
353 
355 {
356  if (!finalized_)
357  {
358  finalized_ = true;
359 
360  // account for unprocessed bytes
361  uint32_t bytes = bufLen_;
362  size_t size = (bytes < 56 ? 64 : 128) / sizeof(uint32_t);
363 
364  // count remaining bytes.
365  bufTotal_[0] += bytes;
366  if (bufTotal_[0] < bytes)
367  {
368  ++bufTotal_[1];
369  }
370 
371  // finalized, but no data!
372  if (!bufTotal_[0] && !bufTotal_[1])
373  {
374  return false;
375  }
376 
377  // place the 64-bit file length in *bits* at the end of the buffer.
378  buffer_[size-2] = swapBytes((bufTotal_[1] << 3) | (bufTotal_[0] >> 29));
379  buffer_[size-1] = swapBytes(bufTotal_[0] << 3);
380 
381  unsigned char* bufp = reinterpret_cast<unsigned char *>(buffer_);
382 
383  memcpy(&bufp[bytes], fillbuf, (size-2) * sizeof(uint32_t) - bytes);
384 
385  // Process remaining bytes
386  processBlock(buffer_, size * sizeof(uint32_t));
387  }
388 
389  return true;
390 }
391 
392 
394 {
395  SHA1Digest dig;
396 
397  if (finalized_)
398  {
399  calcDigest(dig);
400  }
401  else
402  {
403  // avoid disturbing our data - use a copy
404  SHA1 sha(*this);
405  if (sha.finalize())
406  {
407  sha.calcDigest(dig);
408  }
409  }
410 
411  return dig;
412 }
413 
414 
415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416 
417 #undef K1
418 #undef K2
419 #undef K3
420 #undef K4
421 
422 #undef F1
423 #undef F2
424 #undef F3
425 #undef F4
426 
427 #undef rol_uint32
428 #undef M
429 #undef R
430 
431 // ************************************************************************* //
#define K1
Definition: SHA1.C:167
#define K4
Definition: SHA1.C:170
#define F1(B, C, D)
Definition: SHA1.C:173
#define K2
Definition: SHA1.C:168
#define K3
Definition: SHA1.C:169
The SHA1 message digest.
Definition: SHA1Digest.H:62
#define F2(B, C, D)
Definition: SHA1.C:174
const dimensionedScalar & c
Speed of light in a vacuum.
#define F4(B, C, D)
Definition: SHA1.C:176
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void clear()
Reset the hashed data before appending more.
Definition: SHA1.C:339
const dimensionedScalar & b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
#define F3(B, C, D)
Definition: SHA1.C:175
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
#define R(A, B, C, D, E, F, K, M)
label n
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
Functions to compute SHA1 message digest according to the NIST specification FIPS-180-1.
Definition: SHA1.H:68
#define M(I)
SHA1Digest digest() const
Calculate current digest from appended data.
Definition: SHA1.C:393
bool finalize()
Finalized the calculations (normally not needed directly).
Definition: SHA1.C:354