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