Botan  1.11.20
sha1_sse2.cpp
Go to the documentation of this file.
1 /*
2 * SHA-1 using SSE2
3 * Based on public domain code by Dean Gaudet
4 * (http://arctic.org/~dean/crypto/sha1.html)
5 * (C) 2009-2011 Jack Lloyd
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9
10 #include <botan/internal/hash_utils.h>
11 #include <botan/sha1_sse2.h>
12 #include <botan/cpuid.h>
13 #include <emmintrin.h>
14
15 namespace Botan {
16
17 BOTAN_REGISTER_HASH_NOARGS_IF(CPUID::has_sse2(), SHA_160_SSE2, "SHA-160",
19
20 namespace SHA1_SSE2_F {
21
22 namespace {
23
24 /*
25 * First 16 bytes just need byte swapping. Preparing just means
26 * adding in the round constants.
27 */
28
29 #define prep00_15(P, W) \
30  do { \
31  W = _mm_shufflehi_epi16(W, _MM_SHUFFLE(2, 3, 0, 1)); \
32  W = _mm_shufflelo_epi16(W, _MM_SHUFFLE(2, 3, 0, 1)); \
33  W = _mm_or_si128(_mm_slli_epi16(W, 8), \
34  _mm_srli_epi16(W, 8)); \
35  P.u128 = _mm_add_epi32(W, K00_19); \
36  } while(0)
37
38 /*
39 For each multiple of 4, t, we want to calculate this:
40
41 W[t+0] = rol(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
42 W[t+1] = rol(W[t-2] ^ W[t-7] ^ W[t-13] ^ W[t-15], 1);
43 W[t+2] = rol(W[t-1] ^ W[t-6] ^ W[t-12] ^ W[t-14], 1);
44 W[t+3] = rol(W[t] ^ W[t-5] ^ W[t-11] ^ W[t-13], 1);
45
46 we'll actually calculate this:
47
48 W[t+0] = rol(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
49 W[t+1] = rol(W[t-2] ^ W[t-7] ^ W[t-13] ^ W[t-15], 1);
50 W[t+2] = rol(W[t-1] ^ W[t-6] ^ W[t-12] ^ W[t-14], 1);
51 W[t+3] = rol( 0 ^ W[t-5] ^ W[t-11] ^ W[t-13], 1);
52 W[t+3] ^= rol(W[t+0], 1);
53
54 the parameters are:
55
56 W0 = &W[t-16];
57 W1 = &W[t-12];
58 W2 = &W[t- 8];
59 W3 = &W[t- 4];
60
61 and on output:
62 prepared = W0 + K
63 W0 = W[t]..W[t+3]
64 */
65
66 /* note that there is a step here where i want to do a rol by 1, which
67 * normally would look like this:
68 *
69 * r1 = psrld r0,\$31
70 * r0 = pslld r0,\$1
71 * r0 = por r0,r1
72 *
73 * but instead i do this:
74 *
75 * r1 = pcmpltd r0,zero
76 * r0 = paddd r0,r0
77 * r0 = psub r0,r1
78 *
79 * because pcmpltd and paddd are availabe in both MMX units on
80 * efficeon, pentium-m, and opteron but shifts are available in
81 * only one unit.
82 */
83 #define prep(prep, XW0, XW1, XW2, XW3, K) \
84  do { \
85  __m128i r0, r1, r2, r3; \
86  \
87  /* load W[t-4] 16-byte aligned, and shift */ \
88  r3 = _mm_srli_si128((XW3), 4); \
89  r0 = (XW0); \
90  /* get high 64-bits of XW0 into low 64-bits */ \
91  r1 = _mm_shuffle_epi32((XW0), _MM_SHUFFLE(1,0,3,2)); \
92  /* load high 64-bits of r1 */ \
93  r1 = _mm_unpacklo_epi64(r1, (XW1)); \
94  r2 = (XW2); \
95  \
96  r0 = _mm_xor_si128(r1, r0); \
97  r2 = _mm_xor_si128(r3, r2); \
98  r0 = _mm_xor_si128(r2, r0); \
99  /* unrotated W[t]..W[t+2] in r0 ... still need W[t+3] */ \
100  \
101  r2 = _mm_slli_si128(r0, 12); \
102  r1 = _mm_cmplt_epi32(r0, _mm_setzero_si128()); \
103  r0 = _mm_add_epi32(r0, r0); /* shift left by 1 */ \
104  r0 = _mm_sub_epi32(r0, r1); /* r0 has W[t]..W[t+2] */ \
105  \
106  r3 = _mm_srli_epi32(r2, 30); \
107  r2 = _mm_slli_epi32(r2, 2); \
108  \
109  r0 = _mm_xor_si128(r0, r3); \
110  r0 = _mm_xor_si128(r0, r2); /* r0 now has W[t+3] */ \
111  \
112  (XW0) = r0; \
113  (prep).u128 = _mm_add_epi32(r0, K); \
114  } while(0)
115
116 /*
117 * SHA-160 F1 Function
118 */
119 inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
120  {
121  E += (D ^ (B & (C ^ D))) + msg + rotate_left(A, 5);
122  B = rotate_left(B, 30);
123  }
124
125 /*
126 * SHA-160 F2 Function
127 */
128 inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
129  {
130  E += (B ^ C ^ D) + msg + rotate_left(A, 5);
131  B = rotate_left(B, 30);
132  }
133
134 /*
135 * SHA-160 F3 Function
136 */
137 inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
138  {
139  E += ((B & C) | ((B | C) & D)) + msg + rotate_left(A, 5);
140  B = rotate_left(B, 30);
141  }
142
143 /*
144 * SHA-160 F4 Function
145 */
146 inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg)
147  {
148  E += (B ^ C ^ D) + msg + rotate_left(A, 5);
149  B = rotate_left(B, 30);
150  }
151
152 }
153
154 }
155
156 /*
157 * SHA-160 Compression Function using SSE for message expansion
158 */
159 void SHA_160_SSE2::compress_n(const byte input_bytes[], size_t blocks)
160  {
161  using namespace SHA1_SSE2_F;
162
163  const __m128i K00_19 = _mm_set1_epi32(0x5A827999);
164  const __m128i K20_39 = _mm_set1_epi32(0x6ED9EBA1);
165  const __m128i K40_59 = _mm_set1_epi32(0x8F1BBCDC);
166  const __m128i K60_79 = _mm_set1_epi32(0xCA62C1D6);
167
168  u32bit A = digest[0],
169  B = digest[1],
170  C = digest[2],
171  D = digest[3],
172  E = digest[4];
173
174  const __m128i* input = reinterpret_cast<const __m128i*>(input_bytes);
175
176  for(size_t i = 0; i != blocks; ++i)
177  {
178  union v4si {
179  u32bit u32[4];
180  __m128i u128;
181  };
182
183  v4si P0, P1, P2, P3;
184
186  prep00_15(P0, W0);
187
189  prep00_15(P1, W1);
190
192  prep00_15(P2, W2);
193
195  prep00_15(P3, W3);
196
197  /*
198  Using SSE4; slower on Core2 and Nehalem
199  #define GET_P_32(P, i) _mm_extract_epi32(P.u128, i)
200
201  Much slower on all tested platforms
202  #define GET_P_32(P,i) _mm_cvtsi128_si32(_mm_srli_si128(P.u128, i*4))
203  */
204
205 #define GET_P_32(P, i) P.u32[i]
206
207  F1(A, B, C, D, E, GET_P_32(P0, 0));
208  F1(E, A, B, C, D, GET_P_32(P0, 1));
209  F1(D, E, A, B, C, GET_P_32(P0, 2));
210  F1(C, D, E, A, B, GET_P_32(P0, 3));
211  prep(P0, W0, W1, W2, W3, K00_19);
212
213  F1(B, C, D, E, A, GET_P_32(P1, 0));
214  F1(A, B, C, D, E, GET_P_32(P1, 1));
215  F1(E, A, B, C, D, GET_P_32(P1, 2));
216  F1(D, E, A, B, C, GET_P_32(P1, 3));
217  prep(P1, W1, W2, W3, W0, K20_39);
218
219  F1(C, D, E, A, B, GET_P_32(P2, 0));
220  F1(B, C, D, E, A, GET_P_32(P2, 1));
221  F1(A, B, C, D, E, GET_P_32(P2, 2));
222  F1(E, A, B, C, D, GET_P_32(P2, 3));
223  prep(P2, W2, W3, W0, W1, K20_39);
224
225  F1(D, E, A, B, C, GET_P_32(P3, 0));
226  F1(C, D, E, A, B, GET_P_32(P3, 1));
227  F1(B, C, D, E, A, GET_P_32(P3, 2));
228  F1(A, B, C, D, E, GET_P_32(P3, 3));
229  prep(P3, W3, W0, W1, W2, K20_39);
230
231  F1(E, A, B, C, D, GET_P_32(P0, 0));
232  F1(D, E, A, B, C, GET_P_32(P0, 1));
233  F1(C, D, E, A, B, GET_P_32(P0, 2));
234  F1(B, C, D, E, A, GET_P_32(P0, 3));
235  prep(P0, W0, W1, W2, W3, K20_39);
236
237  F2(A, B, C, D, E, GET_P_32(P1, 0));
238  F2(E, A, B, C, D, GET_P_32(P1, 1));
239  F2(D, E, A, B, C, GET_P_32(P1, 2));
240  F2(C, D, E, A, B, GET_P_32(P1, 3));
241  prep(P1, W1, W2, W3, W0, K20_39);
242
243  F2(B, C, D, E, A, GET_P_32(P2, 0));
244  F2(A, B, C, D, E, GET_P_32(P2, 1));
245  F2(E, A, B, C, D, GET_P_32(P2, 2));
246  F2(D, E, A, B, C, GET_P_32(P2, 3));
247  prep(P2, W2, W3, W0, W1, K40_59);
248
249  F2(C, D, E, A, B, GET_P_32(P3, 0));
250  F2(B, C, D, E, A, GET_P_32(P3, 1));
251  F2(A, B, C, D, E, GET_P_32(P3, 2));
252  F2(E, A, B, C, D, GET_P_32(P3, 3));
253  prep(P3, W3, W0, W1, W2, K40_59);
254
255  F2(D, E, A, B, C, GET_P_32(P0, 0));
256  F2(C, D, E, A, B, GET_P_32(P0, 1));
257  F2(B, C, D, E, A, GET_P_32(P0, 2));
258  F2(A, B, C, D, E, GET_P_32(P0, 3));
259  prep(P0, W0, W1, W2, W3, K40_59);
260
261  F2(E, A, B, C, D, GET_P_32(P1, 0));
262  F2(D, E, A, B, C, GET_P_32(P1, 1));
263  F2(C, D, E, A, B, GET_P_32(P1, 2));
264  F2(B, C, D, E, A, GET_P_32(P1, 3));
265  prep(P1, W1, W2, W3, W0, K40_59);
266
267  F3(A, B, C, D, E, GET_P_32(P2, 0));
268  F3(E, A, B, C, D, GET_P_32(P2, 1));
269  F3(D, E, A, B, C, GET_P_32(P2, 2));
270  F3(C, D, E, A, B, GET_P_32(P2, 3));
271  prep(P2, W2, W3, W0, W1, K40_59);
272
273  F3(B, C, D, E, A, GET_P_32(P3, 0));
274  F3(A, B, C, D, E, GET_P_32(P3, 1));
275  F3(E, A, B, C, D, GET_P_32(P3, 2));
276  F3(D, E, A, B, C, GET_P_32(P3, 3));
277  prep(P3, W3, W0, W1, W2, K60_79);
278
279  F3(C, D, E, A, B, GET_P_32(P0, 0));
280  F3(B, C, D, E, A, GET_P_32(P0, 1));
281  F3(A, B, C, D, E, GET_P_32(P0, 2));
282  F3(E, A, B, C, D, GET_P_32(P0, 3));
283  prep(P0, W0, W1, W2, W3, K60_79);
284
285  F3(D, E, A, B, C, GET_P_32(P1, 0));
286  F3(C, D, E, A, B, GET_P_32(P1, 1));
287  F3(B, C, D, E, A, GET_P_32(P1, 2));
288  F3(A, B, C, D, E, GET_P_32(P1, 3));
289  prep(P1, W1, W2, W3, W0, K60_79);
290
291  F3(E, A, B, C, D, GET_P_32(P2, 0));
292  F3(D, E, A, B, C, GET_P_32(P2, 1));
293  F3(C, D, E, A, B, GET_P_32(P2, 2));
294  F3(B, C, D, E, A, GET_P_32(P2, 3));
295  prep(P2, W2, W3, W0, W1, K60_79);
296
297  F4(A, B, C, D, E, GET_P_32(P3, 0));
298  F4(E, A, B, C, D, GET_P_32(P3, 1));
299  F4(D, E, A, B, C, GET_P_32(P3, 2));
300  F4(C, D, E, A, B, GET_P_32(P3, 3));
301  prep(P3, W3, W0, W1, W2, K60_79);
302
303  F4(B, C, D, E, A, GET_P_32(P0, 0));
304  F4(A, B, C, D, E, GET_P_32(P0, 1));
305  F4(E, A, B, C, D, GET_P_32(P0, 2));
306  F4(D, E, A, B, C, GET_P_32(P0, 3));
307
308  F4(C, D, E, A, B, GET_P_32(P1, 0));
309  F4(B, C, D, E, A, GET_P_32(P1, 1));
310  F4(A, B, C, D, E, GET_P_32(P1, 2));
311  F4(E, A, B, C, D, GET_P_32(P1, 3));
312
313  F4(D, E, A, B, C, GET_P_32(P2, 0));
314  F4(C, D, E, A, B, GET_P_32(P2, 1));
315  F4(B, C, D, E, A, GET_P_32(P2, 2));
316  F4(A, B, C, D, E, GET_P_32(P2, 3));
317
318  F4(E, A, B, C, D, GET_P_32(P3, 0));
319  F4(D, E, A, B, C, GET_P_32(P3, 1));
320  F4(C, D, E, A, B, GET_P_32(P3, 2));
321  F4(B, C, D, E, A, GET_P_32(P3, 3));
322
323  A = (digest[0] += A);
324  B = (digest[1] += B);
325  C = (digest[2] += C);
326  D = (digest[3] += D);
327  E = (digest[4] += E);
328
329  input += (hash_block_size() / 16);
330  }
331
332 #undef GET_P_32
333  }
334
335 #undef prep00_15
336 #undef prep
337
338 }
#define BOTAN_SIMD_ALGORITHM_PRIO
T rotate_left(T input, size_t rot)
Definition: rotate.h:21
std::uint32_t u32bit
Definition: types.h:33
secure_vector< u32bit > digest
Definition: sha160.h:50
void F2(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:30
void F3(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:40
Definition: alg_id.cpp:13
#define prep(prep, XW0, XW1, XW2, XW3, K)
Definition: sha1_sse2.cpp:83
static bool has_sse2()
Definition: cpuid.h:46
void F4(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:50
BOTAN_REGISTER_HASH_NOARGS_IF(CPUID::has_sse2(), SHA_160_SSE2,"SHA-160","sse2", BOTAN_SIMD_ALGORITHM_PRIO)
#define GET_P_32(P, i)
void F1(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:20
size_t hash_block_size() const override
Definition: mdx_hash.h:32
#define prep00_15(P, W)
Definition: sha1_sse2.cpp:29
std::uint8_t byte
Definition: types.h:31