openMSX
sha1.cc
Go to the documentation of this file.
1 /*
2 Based on:
3 100% free public domain implementation of the SHA-1 algorithm
4 by Dominik Reichl <Dominik.Reichl@tiscali.de>
5 
6 Refactored in C++ style as part of openMSX
7 by Maarten ter Huurne and Wouter Vermaelen.
8 
9 === Test Vectors (from FIPS PUB 180-1) ===
10 
11 "abc"
12 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
13 
14 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
15 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
16 
17 A million repetitions of "a"
18 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
19 */
20 
21 #include "sha1.hh"
22 #include "MSXException.hh"
23 #include "endian.hh"
24 #include "likely.hh"
25 #include <cassert>
26 #include <cstring>
27 #ifdef __SSE2__
28 #include <emmintrin.h> // SSE2
29 #endif
30 
31 using std::string;
32 
33 namespace openmsx {
34 
35 // Rotate x bits to the left
36 inline static uint32_t rol32(uint32_t value, int bits)
37 {
38  return (value << bits) | (value >> (32 - bits));
39 }
40 
42 private:
43  uint32_t data[16];
44 
45  uint32_t next0(int i)
46  {
47  data[i] = Endian::readB32(&data[i]);
48  return data[i];
49  }
50  uint32_t next(int i)
51  {
52  return data[i & 15] = rol32(
53  data[(i + 13) & 15] ^ data[(i + 8) & 15] ^
54  data[(i + 2) & 15] ^ data[ i & 15]
55  , 1);
56  }
57 
58 public:
59  explicit WorkspaceBlock(const uint8_t buffer[64]);
60 
61  // SHA-1 rounds
62  void r0(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z, int i)
63  {
64  z += ((w & (x ^ y)) ^ y) + next0(i) + 0x5A827999 + rol32(v, 5);
65  w = rol32(w, 30);
66  }
67  void r1(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z, int i)
68  {
69  z += ((w & (x ^ y)) ^ y) + next(i) + 0x5A827999 + rol32(v, 5);
70  w = rol32(w, 30);
71  }
72  void r2(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z, int i)
73  {
74  z += (w ^ x ^ y) + next(i) + 0x6ED9EBA1 + rol32(v, 5);
75  w = rol32(w, 30);
76  }
77  void r3(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z, int i)
78  {
79  z += (((w | x) & y) | (w & x)) + next(i) + 0x8F1BBCDC + rol32(v, 5);
80  w = rol32(w, 30);
81  }
82  void r4(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z, int i)
83  {
84  z += (w ^ x ^ y) + next(i) + 0xCA62C1D6 + rol32(v, 5);
85  w = rol32(w, 30);
86  }
87 };
88 
89 WorkspaceBlock::WorkspaceBlock(const uint8_t buffer[64])
90 {
91  memcpy(data, buffer, sizeof(data));
92 }
93 
94 
95 // class Sha1Sum
96 
98 {
99  clear();
100 }
101 
103 {
104  if (str.size() != 40) {
105  throw MSXException("Invalid sha1, should be exactly 40 digits long: " + str);
106  }
107  parse40(str.data());
108 }
109 
110 static inline unsigned hex(char x, const char* str)
111 {
112  if (('0' <= x) && (x <= '9')) return x - '0';
113  if (('a' <= x) && (x <= 'f')) return x - 'a' + 10;
114  if (('A' <= x) && (x <= 'F')) return x - 'A' + 10;
115  throw MSXException("Invalid sha1, digits should be 0-9, a-f: " +
116  string(str, 40));
117 }
118 
119 #ifdef __SSE2__
120 // emulate some missing unsigned-8-bit comparison functions
121 static inline __m128i _mm_cmpge_epu8(__m128i a, __m128i b)
122 {
123  return _mm_cmpeq_epi8(_mm_max_epu8(a, b), a);
124 }
125 
126 static inline __m128i _mm_cmple_epu8(__m128i a, __m128i b)
127 {
128  return _mm_cmpge_epu8(b, a);
129 }
130 
131 // load 64-bit (possibly unaligned) and swap bytes
132 static inline uint64_t loadSwap64(const char* s)
133 {
134  return Endian::bswap64(*reinterpret_cast<const uint64_t*>(s));
135 }
136 #endif
137 
138 void Sha1Sum::parse40(const char* str)
139 {
140 #ifdef __SSE2__
141  // SSE2 version
142 
143  // load characters
144  __m128i s0 = _mm_set_epi64x(loadSwap64(str + 8), loadSwap64(str + 0));
145  __m128i s1 = _mm_set_epi64x(loadSwap64(str + 24), loadSwap64(str + 16));
146  __m128i s2 = _mm_set_epi64x('0' * 0x0101010101010101, loadSwap64(str + 32));
147 
148  // chars - '0'
149  __m128i cc0 = _mm_set1_epi8(char(-'0'));
150  __m128i s0_0 = _mm_add_epi8(s0, cc0);
151  __m128i s1_0 = _mm_add_epi8(s1, cc0);
152  __m128i s2_0 = _mm_add_epi8(s2, cc0);
153 
154  // (chars | 32) - 'a' (convert uppercase 'A'-'F' into lower case)
155  __m128i c32 = _mm_set1_epi8(32);
156  __m128i cca = _mm_set1_epi8(char(-'a'));
157  __m128i s0_a = _mm_add_epi8(_mm_or_si128(s0, c32), cca);
158  __m128i s1_a = _mm_add_epi8(_mm_or_si128(s1, c32), cca);
159  __m128i s2_a = _mm_add_epi8(_mm_or_si128(s2, c32), cca);
160 
161  // was in range '0'-'9'?
162  __m128i c9 = _mm_set1_epi8(9);
163  __m128i c0_0 = _mm_cmple_epu8(s0_0, c9);
164  __m128i c1_0 = _mm_cmple_epu8(s1_0, c9);
165  __m128i c2_0 = _mm_cmple_epu8(s2_0, c9);
166 
167  // was in range 'a'-'f'?
168  __m128i c5 = _mm_set1_epi8(5);
169  __m128i c0_a = _mm_cmple_epu8(s0_a, c5);
170  __m128i c1_a = _mm_cmple_epu8(s1_a, c5);
171  __m128i c2_a = _mm_cmple_epu8(s2_a, c5);
172 
173  // either '0'-'9' or 'a'-f' must be in range for all chars
174  __m128i ok0 = _mm_or_si128(c0_0, c0_a);
175  __m128i ok1 = _mm_or_si128(c1_0, c1_a);
176  __m128i ok2 = _mm_or_si128(c2_0, c2_a);
177  __m128i ok = _mm_and_si128(_mm_and_si128(ok0, ok1), ok2);
178  if (unlikely(_mm_movemask_epi8(ok) != 0xffff)) {
179  throw string("Invalid sha1, digits should be 0-9, a-f: " +
180  string(str, 40));
181  }
182 
183  // '0'-'9' to numeric value (or zero)
184  __m128i d0_0 = _mm_and_si128(s0_0, c0_0);
185  __m128i d1_0 = _mm_and_si128(s1_0, c1_0);
186  __m128i d2_0 = _mm_and_si128(s2_0, c2_0);
187 
188  // 'a'-'f' to numeric value (or zero)
189  __m128i c10 = _mm_set1_epi8(10);
190  __m128i d0_a = _mm_and_si128(_mm_add_epi8(s0_a, c10), c0_a);
191  __m128i d1_a = _mm_and_si128(_mm_add_epi8(s1_a, c10), c1_a);
192  __m128i d2_a = _mm_and_si128(_mm_add_epi8(s2_a, c10), c2_a);
193 
194  // combine 0-9 / 10-15 into 0-15
195  __m128i d0 = _mm_or_si128(d0_0, d0_a);
196  __m128i d1 = _mm_or_si128(d1_0, d1_a);
197  __m128i d2 = _mm_or_si128(d2_0, d2_a);
198 
199  // compact bytes to nibbles
200  __m128i c00ff = _mm_set1_epi16(0x00ff);
201  __m128i e0 = _mm_and_si128(_mm_or_si128(d0, _mm_srli_epi16(d0, 4)), c00ff);
202  __m128i e1 = _mm_and_si128(_mm_or_si128(d1, _mm_srli_epi16(d1, 4)), c00ff);
203  __m128i e2 = _mm_and_si128(_mm_or_si128(d2, _mm_srli_epi16(d2, 4)), c00ff);
204  __m128i f0 = _mm_packus_epi16(e0, e0);
205  __m128i f1 = _mm_packus_epi16(e1, e1);
206  __m128i f2 = _mm_packus_epi16(e2, e2);
207 
208  // store result
209  _mm_storeu_si128(reinterpret_cast<__m128i*>(a), _mm_unpacklo_epi64(f0, f1));
210  a[4] = _mm_cvtsi128_si32(f2);
211 #else
212  // equivalent c++ version
213  const char* p = str;
214  for (auto& ai : a) {
215  unsigned t = 0;
216  for (int j = 0; j < 8; ++j) {
217  t <<= 4;
218  t |= hex(*p++, str);
219  }
220  ai = t;
221  }
222 #endif
223 }
224 
225 static inline char digit(unsigned x)
226 {
227  return (x < 10) ? (x + '0') : (x - 10 + 'a');
228 }
229 std::string Sha1Sum::toString() const
230 {
231  char buf[40];
232  char* p = buf;
233  for (const auto& ai : a) {
234  for (int j = 28; j >= 0; j -= 4) {
235  *p++ = digit((ai >> j) & 0xf);
236  }
237  }
238  return string(buf, 40);
239 }
240 
241 bool Sha1Sum::empty() const
242 {
243  for (const auto& ai : a) {
244  if (ai != 0) return false;
245  }
246  return true;
247 }
249 {
250  for (auto& ai : a) ai = 0;
251 }
252 
253 
254 // class SHA1
255 
257 {
258  // SHA1 initialization constants
259  m_state.a[0] = 0x67452301;
260  m_state.a[1] = 0xEFCDAB89;
261  m_state.a[2] = 0x98BADCFE;
262  m_state.a[3] = 0x10325476;
263  m_state.a[4] = 0xC3D2E1F0;
264 
265  m_count = 0;
266  m_finalized = false;
267 }
268 
269 void SHA1::transform(const uint8_t buffer[64])
270 {
271  WorkspaceBlock block(buffer);
272 
273  // Copy m_state[] to working vars
274  uint32_t a = m_state.a[0];
275  uint32_t b = m_state.a[1];
276  uint32_t c = m_state.a[2];
277  uint32_t d = m_state.a[3];
278  uint32_t e = m_state.a[4];
279 
280  // 4 rounds of 20 operations each. Loop unrolled
281  block.r0(a,b,c,d,e, 0); block.r0(e,a,b,c,d, 1); block.r0(d,e,a,b,c, 2);
282  block.r0(c,d,e,a,b, 3); block.r0(b,c,d,e,a, 4); block.r0(a,b,c,d,e, 5);
283  block.r0(e,a,b,c,d, 6); block.r0(d,e,a,b,c, 7); block.r0(c,d,e,a,b, 8);
284  block.r0(b,c,d,e,a, 9); block.r0(a,b,c,d,e,10); block.r0(e,a,b,c,d,11);
285  block.r0(d,e,a,b,c,12); block.r0(c,d,e,a,b,13); block.r0(b,c,d,e,a,14);
286  block.r0(a,b,c,d,e,15); block.r1(e,a,b,c,d,16); block.r1(d,e,a,b,c,17);
287  block.r1(c,d,e,a,b,18); block.r1(b,c,d,e,a,19); block.r2(a,b,c,d,e,20);
288  block.r2(e,a,b,c,d,21); block.r2(d,e,a,b,c,22); block.r2(c,d,e,a,b,23);
289  block.r2(b,c,d,e,a,24); block.r2(a,b,c,d,e,25); block.r2(e,a,b,c,d,26);
290  block.r2(d,e,a,b,c,27); block.r2(c,d,e,a,b,28); block.r2(b,c,d,e,a,29);
291  block.r2(a,b,c,d,e,30); block.r2(e,a,b,c,d,31); block.r2(d,e,a,b,c,32);
292  block.r2(c,d,e,a,b,33); block.r2(b,c,d,e,a,34); block.r2(a,b,c,d,e,35);
293  block.r2(e,a,b,c,d,36); block.r2(d,e,a,b,c,37); block.r2(c,d,e,a,b,38);
294  block.r2(b,c,d,e,a,39); block.r3(a,b,c,d,e,40); block.r3(e,a,b,c,d,41);
295  block.r3(d,e,a,b,c,42); block.r3(c,d,e,a,b,43); block.r3(b,c,d,e,a,44);
296  block.r3(a,b,c,d,e,45); block.r3(e,a,b,c,d,46); block.r3(d,e,a,b,c,47);
297  block.r3(c,d,e,a,b,48); block.r3(b,c,d,e,a,49); block.r3(a,b,c,d,e,50);
298  block.r3(e,a,b,c,d,51); block.r3(d,e,a,b,c,52); block.r3(c,d,e,a,b,53);
299  block.r3(b,c,d,e,a,54); block.r3(a,b,c,d,e,55); block.r3(e,a,b,c,d,56);
300  block.r3(d,e,a,b,c,57); block.r3(c,d,e,a,b,58); block.r3(b,c,d,e,a,59);
301  block.r4(a,b,c,d,e,60); block.r4(e,a,b,c,d,61); block.r4(d,e,a,b,c,62);
302  block.r4(c,d,e,a,b,63); block.r4(b,c,d,e,a,64); block.r4(a,b,c,d,e,65);
303  block.r4(e,a,b,c,d,66); block.r4(d,e,a,b,c,67); block.r4(c,d,e,a,b,68);
304  block.r4(b,c,d,e,a,69); block.r4(a,b,c,d,e,70); block.r4(e,a,b,c,d,71);
305  block.r4(d,e,a,b,c,72); block.r4(c,d,e,a,b,73); block.r4(b,c,d,e,a,74);
306  block.r4(a,b,c,d,e,75); block.r4(e,a,b,c,d,76); block.r4(d,e,a,b,c,77);
307  block.r4(c,d,e,a,b,78); block.r4(b,c,d,e,a,79);
308 
309  // Add the working vars back into m_state[]
310  m_state.a[0] += a;
311  m_state.a[1] += b;
312  m_state.a[2] += c;
313  m_state.a[3] += d;
314  m_state.a[4] += e;
315 }
316 
317 // Use this function to hash in binary data and strings
318 void SHA1::update(const uint8_t* data, size_t len)
319 {
320  assert(!m_finalized);
321  uint32_t j = (m_count >> 3) & 63;
322 
323  m_count += uint64_t(len) << 3;
324 
325  size_t i;
326  if ((j + len) > 63) {
327  memcpy(&m_buffer[j], data, (i = 64 - j));
328  transform(m_buffer);
329  for (; i + 63 < len; i += 64) {
330  transform(&data[i]);
331  }
332  j = 0;
333  } else {
334  i = 0;
335  }
336  memcpy(&m_buffer[j], &data[i], len - i);
337 }
338 
339 void SHA1::finalize()
340 {
341  assert(!m_finalized);
342  uint8_t finalcount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
343  for (int i = 0; i < 8; i++) {
344  finalcount[i] = uint8_t(m_count >> ((7 - i) * 8));
345  }
346 
347  update(reinterpret_cast<const uint8_t*>("\200"), 1);
348  while ((m_count & 504) != 448) {
349  update(reinterpret_cast<const uint8_t*>("\0"), 1);
350  }
351  update(finalcount, 8); // cause a transform()
352  m_finalized = true;
353 }
354 
356 {
357  if (!m_finalized) finalize();
358  return m_state;
359 }
360 
361 Sha1Sum SHA1::calc(const uint8_t* data, size_t len)
362 {
363  SHA1 sha1;
364  sha1.update(data, len);
365  return sha1.digest();
366 }
367 
368 } // namespace openmsx
void update(const uint8_t *data, size_t len)
Incrementally calculate the hash value.
Definition: sha1.cc:318
void r0(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
Definition: sha1.cc:62
#define unlikely(x)
Definition: likely.hh:15
void r4(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
Definition: sha1.cc:82
WorkspaceBlock(const uint8_t buffer[64])
Definition: sha1.cc:89
static Sha1Sum calc(const uint8_t *data, size_t len)
Easier to use interface, if you can pass all data in one go.
Definition: sha1.cc:361
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void r2(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
Definition: sha1.cc:72
const char * data() const
Definition: string_ref.hh:68
bool empty() const
Definition: sha1.cc:241
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
Sha1Sum digest()
Get the final hash.
Definition: sha1.cc:355
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
Helper class to perform a sha1 calculation.
Definition: sha1.hh:74
std::string toString() const
Definition: sha1.cc:229
void parse40(const char *str)
Definition: sha1.cc:138
size_type size() const
Definition: string_ref.hh:55
void r3(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
Definition: sha1.cc:77
void clear()
Definition: sha1.cc:248
void r1(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
Definition: sha1.cc:67