RigsofRods
Soft-body Physics Simulation
SHA1.cpp
Go to the documentation of this file.
1 /*
2  100% free public domain implementation of the SHA-1 algorithm
3  by Dominik Reichl <dominik.reichl@t-online.de>
4  Web: http://www.dominik-reichl.de/
5 
6  This version is modified for Rigs of Rods project
7  https://sourceforge.net/projects/rigsofrods/
8 
9  Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
10  - You can set the endianness in your files, no need to modify the
11  header file of the CSHA1 class any more
12  - Aligned data support
13  - Made support/compilation of the utility functions (ReportHash
14  and HashFile) optional (useful, if bytes count, for example in
15  embedded environments)
16 
17  Version 1.5 - 2005-01-01
18  - 64-bit compiler compatibility added
19  - Made variable wiping optional (define SHA1_WIPE_VARIABLES)
20  - Removed unnecessary variable initializations
21  - ROL32 improvement for the Microsoft compiler (using _rotl)
22 
23  ======== Test Vectors (from FIPS PUB 180-1) ========
24 
25  SHA1("abc") =
26  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
27 
28  SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
29  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
30 
31  SHA1(A million repetitions of "a") =
32  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
33 */
34 
35 #include "SHA1.h"
36 
37 using namespace RoR;
38 
39 #ifdef SHA1_UTILITY_FUNCTIONS
40 #define SHA1_MAX_FILE_BUFFER 8000
41 #endif
42 
43 // Rotate x bits to the left
44 #ifndef ROL32
45 #ifdef _MSC_VER
46 #define ROL32(_val32, _nBits) _rotl(_val32, _nBits)
47 #else
48 #define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
49 #endif
50 #endif
51 
52 #ifdef SHA1_LITTLE_ENDIAN
53 #define SHABLK0(i) (m_block->l[i] = \
54  (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
55 #else
56 #define SHABLK0(i) (m_block->l[i])
57 #endif
58 
59 #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
60  ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
61 
62 // SHA-1 rounds
63 #define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
64 #define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
65 #define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
66 #define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
67 #define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
68 
70 {
72 
73  std::memset(m_buffer, 0, sizeof(m_buffer));
74  std::memset(m_digest, 0, sizeof(m_digest));
75  std::memset(m_workspace, 0, sizeof(m_workspace));
76  std::memset(__reserved1, 0, sizeof(__reserved1));
77  std::memset(__reserved2, 0, sizeof(__reserved2));
78 
79  Reset();
80 }
81 
83 {
84  Reset();
85 }
86 
88 {
89  // SHA1 initialization constants
90  m_state[0] = 0x67452301;
91  m_state[1] = 0xEFCDAB89;
92  m_state[2] = 0x98BADCFE;
93  m_state[3] = 0x10325476;
94  m_state[4] = 0xC3D2E1F0;
95 
96  m_count[0] = 0;
97  m_count[1] = 0;
98 }
99 
100 void CSHA1::Transform(uint32_t *state, uint8_t *buffer)
101 {
102  // Copy state[] to working vars
103  uint32_t a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
104 
105  memcpy(m_block, buffer, 64);
106 
107  // 4 rounds of 20 operations each. Loop unrolled.
108  _R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
109  _R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
110  _R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
111  _R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
112  _R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
113  _R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
114  _R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
115  _R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
116  _R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
117  _R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
118  _R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
119  _R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
120  _R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
121  _R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
122  _R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
123  _R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
124  _R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
125  _R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
126  _R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
127  _R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
128 
129  // Add the working vars back into state
130  state[0] += a;
131  state[1] += b;
132  state[2] += c;
133  state[3] += d;
134  state[4] += e;
135 
136  // Wipe variables
137 #ifdef SHA1_WIPE_VARIABLES
138  a = b = c = d = e = 0;
139 #endif
140 }
141 
142 // Use this function to hash in binary data and strings
143 void CSHA1::UpdateHash(uint8_t *data, uint32_t len)
144 {
145  uint32_t i, j;
146 
147  j = (m_count[0] >> 3) & 63;
148 
149  if ((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
150 
151  m_count[1] += (len >> 29);
152 
153  if ((j + len) > 63)
154  {
155  i = 64 - j;
156  memcpy(&m_buffer[j], data, i);
158 
159  for ( ; i + 63 < len; i += 64) Transform(m_state, &data[i]);
160 
161  j = 0;
162  }
163  else i = 0;
164 
165  memcpy(&m_buffer[j], &data[i], len - i);
166 }
167 
169 {
170  uint32_t i;
171  uint8_t finalcount[8];
172 
173  for (i = 0; i < 8; i++)
174  finalcount[i] = (uint8_t)((m_count[((i >= 4) ? 0 : 1)]
175  >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
176 
177  UpdateHash((uint8_t *)"\200", 1);
178 
179  while ((m_count[0] & 504) != 448)
180  UpdateHash((uint8_t *)"\0", 1);
181 
182  UpdateHash(finalcount, 8); // Cause a SHA1Transform()
183 
184  for (i = 0; i < 20; i++)
185  {
186  m_digest[i] = (uint8_t)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
187  }
188 
189  // Wipe variables for security reasons
190 #ifdef SHA1_WIPE_VARIABLES
191  i = 0;
192  memset(m_buffer, 0, 64);
193  memset(m_state, 0, 20);
194  memset(m_count, 0, 8);
195  memset(finalcount, 0, 8);
197 #endif
198 }
199 
200 #ifdef SHA1_UTILITY_FUNCTIONS
201 // Get the final hash as a pre-formatted string
202 std::string CSHA1::ReportHash()
203 {
204  unsigned char i;
205  char szTemp[16] = {};
206  char szReport[41] = {}; // 40 characters of hash + terminator NULL-character
207 
208  for (i = 0; i < 20; i++)
209  {
210  sprintf(szTemp, "%02X", m_digest[i]);
211  strcat(szReport, szTemp); // Beware: strcat() adds NULL-character
212  }
213 
214  return szReport;
215 }
216 #endif
217 
218 // Get the raw message digest
219 void CSHA1::GetHash(uint8_t *puDest)
220 {
221  memcpy(puDest, m_digest, 20);
222 }
RoR::CSHA1::Final
void Final()
Definition: SHA1.cpp:168
RoR::CSHA1::__reserved1
uint32_t __reserved1[1]
Definition: SHA1.h:94
RoR::CSHA1::m_digest
uint8_t m_digest[20]
Definition: SHA1.h:96
RoR::CSHA1::~CSHA1
~CSHA1()
Definition: SHA1.cpp:82
RoR::CSHA1::m_block
SHA1_WORKSPACE_BLOCK * m_block
Definition: SHA1.h:119
RoR::CSHA1::m_count
uint32_t m_count[2]
Definition: SHA1.h:93
RoR::CSHA1::__reserved2
uint32_t __reserved2[3]
Definition: SHA1.h:97
RoR::CSHA1::Reset
void Reset()
Definition: SHA1.cpp:87
_R4
#define _R4(v, w, x, y, z, i)
Definition: SHA1.cpp:67
RoR::CSHA1::UpdateHash
void UpdateHash(uint8_t *data, uint32_t len)
Definition: SHA1.cpp:143
_R0
#define _R0(v, w, x, y, z, i)
Definition: SHA1.cpp:63
SHA1.h
SHA1_WORKSPACE_BLOCK
Definition: SHA1.h:76
_R1
#define _R1(v, w, x, y, z, i)
Definition: SHA1.cpp:64
_R2
#define _R2(v, w, x, y, z, i)
Definition: SHA1.cpp:65
RoR::CSHA1::m_state
uint32_t m_state[5]
Definition: SHA1.h:92
_R3
#define _R3(v, w, x, y, z, i)
Definition: SHA1.cpp:66
RoR::CSHA1::ReportHash
std::string ReportHash()
Definition: SHA1.cpp:202
RoR::CSHA1::m_workspace
uint8_t m_workspace[64]
Definition: SHA1.h:118
RoR::CSHA1::Transform
void Transform(uint32_t *state, uint8_t *buffer)
Definition: SHA1.cpp:100
RoR::CSHA1::m_buffer
uint8_t m_buffer[64]
Definition: SHA1.h:95
RoR::CSHA1::GetHash
void GetHash(uint8_t *puDest)
Definition: SHA1.cpp:219
RoR
Definition: AppContext.h:36
RoR::CSHA1::CSHA1
CSHA1()
Definition: SHA1.cpp:69