aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/stream/chacha/chacha_sse2/chacha_sse2.cpp
blob: 9641be67b235413cb4f2415397354e41a095b72a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* SSE2 ChaCha
* (C) 2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/chacha.h>
#include <emmintrin.h>

namespace Botan {

//static
BOTAN_FUNC_ISA("sse2")
void ChaCha::chacha_sse2_x4(uint8_t output[64*4], uint32_t input[16], size_t rounds)
   {
   BOTAN_ASSERT(rounds % 2 == 0, "Valid rounds");

   const __m128i* input_mm = reinterpret_cast<const __m128i*>(input);
   __m128i* output_mm = reinterpret_cast<__m128i*>(output);

   __m128i input0 = _mm_loadu_si128(input_mm);
   __m128i input1 = _mm_loadu_si128(input_mm + 1);
   __m128i input2 = _mm_loadu_si128(input_mm + 2);
   __m128i input3 = _mm_loadu_si128(input_mm + 3);

   // TODO: try transposing, which would avoid the permutations each round

#define mm_rotl(r, n) \
   _mm_or_si128(_mm_slli_epi32(r, n), _mm_srli_epi32(r, 32-n))

   __m128i r0_0 = input0;
   __m128i r0_1 = input1;
   __m128i r0_2 = input2;
   __m128i r0_3 = input3;

   __m128i r1_0 = input0;
   __m128i r1_1 = input1;
   __m128i r1_2 = input2;
   __m128i r1_3 = _mm_add_epi64(r0_3, _mm_set_epi32(0, 0, 0, 1));

   __m128i r2_0 = input0;
   __m128i r2_1 = input1;
   __m128i r2_2 = input2;
   __m128i r2_3 = _mm_add_epi64(r0_3, _mm_set_epi32(0, 0, 0, 2));

   __m128i r3_0 = input0;
   __m128i r3_1 = input1;
   __m128i r3_2 = input2;
   __m128i r3_3 = _mm_add_epi64(r0_3, _mm_set_epi32(0, 0, 0, 3));

   for(size_t r = 0; r != rounds / 2; ++r)
      {
      r0_0 = _mm_add_epi32(r0_0, r0_1);
      r1_0 = _mm_add_epi32(r1_0, r1_1);
      r2_0 = _mm_add_epi32(r2_0, r2_1);
      r3_0 = _mm_add_epi32(r3_0, r3_1);

      r0_3 = _mm_xor_si128(r0_3, r0_0);
      r1_3 = _mm_xor_si128(r1_3, r1_0);
      r2_3 = _mm_xor_si128(r2_3, r2_0);
      r3_3 = _mm_xor_si128(r3_3, r3_0);

      r0_3 = mm_rotl(r0_3, 16);
      r1_3 = mm_rotl(r1_3, 16);
      r2_3 = mm_rotl(r2_3, 16);
      r3_3 = mm_rotl(r3_3, 16);

      r0_2 = _mm_add_epi32(r0_2, r0_3);
      r1_2 = _mm_add_epi32(r1_2, r1_3);
      r2_2 = _mm_add_epi32(r2_2, r2_3);
      r3_2 = _mm_add_epi32(r3_2, r3_3);

      r0_1 = _mm_xor_si128(r0_1, r0_2);
      r1_1 = _mm_xor_si128(r1_1, r1_2);
      r2_1 = _mm_xor_si128(r2_1, r2_2);
      r3_1 = _mm_xor_si128(r3_1, r3_2);

      r0_1 = mm_rotl(r0_1, 12);
      r1_1 = mm_rotl(r1_1, 12);
      r2_1 = mm_rotl(r2_1, 12);
      r3_1 = mm_rotl(r3_1, 12);

      r0_0 = _mm_add_epi32(r0_0, r0_1);
      r1_0 = _mm_add_epi32(r1_0, r1_1);
      r2_0 = _mm_add_epi32(r2_0, r2_1);
      r3_0 = _mm_add_epi32(r3_0, r3_1);

      r0_3 = _mm_xor_si128(r0_3, r0_0);
      r1_3 = _mm_xor_si128(r1_3, r1_0);
      r2_3 = _mm_xor_si128(r2_3, r2_0);
      r3_3 = _mm_xor_si128(r3_3, r3_0);

      r0_3 = mm_rotl(r0_3, 8);
      r1_3 = mm_rotl(r1_3, 8);
      r2_3 = mm_rotl(r2_3, 8);
      r3_3 = mm_rotl(r3_3, 8);

      r0_2 = _mm_add_epi32(r0_2, r0_3);
      r1_2 = _mm_add_epi32(r1_2, r1_3);
      r2_2 = _mm_add_epi32(r2_2, r2_3);
      r3_2 = _mm_add_epi32(r3_2, r3_3);

      r0_1 = _mm_xor_si128(r0_1, r0_2);
      r1_1 = _mm_xor_si128(r1_1, r1_2);
      r2_1 = _mm_xor_si128(r2_1, r2_2);
      r3_1 = _mm_xor_si128(r3_1, r3_2);

      r0_1 = mm_rotl(r0_1, 7);
      r1_1 = mm_rotl(r1_1, 7);
      r2_1 = mm_rotl(r2_1, 7);
      r3_1 = mm_rotl(r3_1, 7);

      r0_1 = _mm_shuffle_epi32(r0_1, _MM_SHUFFLE(0, 3, 2, 1));
      r0_2 = _mm_shuffle_epi32(r0_2, _MM_SHUFFLE(1, 0, 3, 2));
      r0_3 = _mm_shuffle_epi32(r0_3, _MM_SHUFFLE(2, 1, 0, 3));

      r1_1 = _mm_shuffle_epi32(r1_1, _MM_SHUFFLE(0, 3, 2, 1));
      r1_2 = _mm_shuffle_epi32(r1_2, _MM_SHUFFLE(1, 0, 3, 2));
      r1_3 = _mm_shuffle_epi32(r1_3, _MM_SHUFFLE(2, 1, 0, 3));

      r2_1 = _mm_shuffle_epi32(r2_1, _MM_SHUFFLE(0, 3, 2, 1));
      r2_2 = _mm_shuffle_epi32(r2_2, _MM_SHUFFLE(1, 0, 3, 2));
      r2_3 = _mm_shuffle_epi32(r2_3, _MM_SHUFFLE(2, 1, 0, 3));

      r3_1 = _mm_shuffle_epi32(r3_1, _MM_SHUFFLE(0, 3, 2, 1));
      r3_2 = _mm_shuffle_epi32(r3_2, _MM_SHUFFLE(1, 0, 3, 2));
      r3_3 = _mm_shuffle_epi32(r3_3, _MM_SHUFFLE(2, 1, 0, 3));

      r0_0 = _mm_add_epi32(r0_0, r0_1);
      r1_0 = _mm_add_epi32(r1_0, r1_1);
      r2_0 = _mm_add_epi32(r2_0, r2_1);
      r3_0 = _mm_add_epi32(r3_0, r3_1);

      r0_3 = _mm_xor_si128(r0_3, r0_0);
      r1_3 = _mm_xor_si128(r1_3, r1_0);
      r2_3 = _mm_xor_si128(r2_3, r2_0);
      r3_3 = _mm_xor_si128(r3_3, r3_0);

      r0_3 = mm_rotl(r0_3, 16);
      r1_3 = mm_rotl(r1_3, 16);
      r2_3 = mm_rotl(r2_3, 16);
      r3_3 = mm_rotl(r3_3, 16);

      r0_2 = _mm_add_epi32(r0_2, r0_3);
      r1_2 = _mm_add_epi32(r1_2, r1_3);
      r2_2 = _mm_add_epi32(r2_2, r2_3);
      r3_2 = _mm_add_epi32(r3_2, r3_3);

      r0_1 = _mm_xor_si128(r0_1, r0_2);
      r1_1 = _mm_xor_si128(r1_1, r1_2);
      r2_1 = _mm_xor_si128(r2_1, r2_2);
      r3_1 = _mm_xor_si128(r3_1, r3_2);

      r0_1 = mm_rotl(r0_1, 12);
      r1_1 = mm_rotl(r1_1, 12);
      r2_1 = mm_rotl(r2_1, 12);
      r3_1 = mm_rotl(r3_1, 12);

      r0_0 = _mm_add_epi32(r0_0, r0_1);
      r1_0 = _mm_add_epi32(r1_0, r1_1);
      r2_0 = _mm_add_epi32(r2_0, r2_1);
      r3_0 = _mm_add_epi32(r3_0, r3_1);

      r0_3 = _mm_xor_si128(r0_3, r0_0);
      r1_3 = _mm_xor_si128(r1_3, r1_0);
      r2_3 = _mm_xor_si128(r2_3, r2_0);
      r3_3 = _mm_xor_si128(r3_3, r3_0);

      r0_3 = mm_rotl(r0_3, 8);
      r1_3 = mm_rotl(r1_3, 8);
      r2_3 = mm_rotl(r2_3, 8);
      r3_3 = mm_rotl(r3_3, 8);

      r0_2 = _mm_add_epi32(r0_2, r0_3);
      r1_2 = _mm_add_epi32(r1_2, r1_3);
      r2_2 = _mm_add_epi32(r2_2, r2_3);
      r3_2 = _mm_add_epi32(r3_2, r3_3);

      r0_1 = _mm_xor_si128(r0_1, r0_2);
      r1_1 = _mm_xor_si128(r1_1, r1_2);
      r2_1 = _mm_xor_si128(r2_1, r2_2);
      r3_1 = _mm_xor_si128(r3_1, r3_2);

      r0_1 = mm_rotl(r0_1, 7);
      r1_1 = mm_rotl(r1_1, 7);
      r2_1 = mm_rotl(r2_1, 7);
      r3_1 = mm_rotl(r3_1, 7);

      r0_1 = _mm_shuffle_epi32(r0_1, _MM_SHUFFLE(2, 1, 0, 3));
      r0_2 = _mm_shuffle_epi32(r0_2, _MM_SHUFFLE(1, 0, 3, 2));
      r0_3 = _mm_shuffle_epi32(r0_3, _MM_SHUFFLE(0, 3, 2, 1));

      r1_1 = _mm_shuffle_epi32(r1_1, _MM_SHUFFLE(2, 1, 0, 3));
      r1_2 = _mm_shuffle_epi32(r1_2, _MM_SHUFFLE(1, 0, 3, 2));
      r1_3 = _mm_shuffle_epi32(r1_3, _MM_SHUFFLE(0, 3, 2, 1));

      r2_1 = _mm_shuffle_epi32(r2_1, _MM_SHUFFLE(2, 1, 0, 3));
      r2_2 = _mm_shuffle_epi32(r2_2, _MM_SHUFFLE(1, 0, 3, 2));
      r2_3 = _mm_shuffle_epi32(r2_3, _MM_SHUFFLE(0, 3, 2, 1));

      r3_1 = _mm_shuffle_epi32(r3_1, _MM_SHUFFLE(2, 1, 0, 3));
      r3_2 = _mm_shuffle_epi32(r3_2, _MM_SHUFFLE(1, 0, 3, 2));
      r3_3 = _mm_shuffle_epi32(r3_3, _MM_SHUFFLE(0, 3, 2, 1));
      }

   r0_0 = _mm_add_epi32(r0_0, input0);
   r0_1 = _mm_add_epi32(r0_1, input1);
   r0_2 = _mm_add_epi32(r0_2, input2);
   r0_3 = _mm_add_epi32(r0_3, input3);

   r1_0 = _mm_add_epi32(r1_0, input0);
   r1_1 = _mm_add_epi32(r1_1, input1);
   r1_2 = _mm_add_epi32(r1_2, input2);
   r1_3 = _mm_add_epi32(r1_3, input3);
   r1_3 = _mm_add_epi64(r1_3, _mm_set_epi32(0, 0, 0, 1));

   r2_0 = _mm_add_epi32(r2_0, input0);
   r2_1 = _mm_add_epi32(r2_1, input1);
   r2_2 = _mm_add_epi32(r2_2, input2);
   r2_3 = _mm_add_epi32(r2_3, input3);
   r2_3 = _mm_add_epi64(r2_3, _mm_set_epi32(0, 0, 0, 2));

   r3_0 = _mm_add_epi32(r3_0, input0);
   r3_1 = _mm_add_epi32(r3_1, input1);
   r3_2 = _mm_add_epi32(r3_2, input2);
   r3_3 = _mm_add_epi32(r3_3, input3);
   r3_3 = _mm_add_epi64(r3_3, _mm_set_epi32(0, 0, 0, 3));

   _mm_storeu_si128(output_mm + 0, r0_0);
   _mm_storeu_si128(output_mm + 1, r0_1);
   _mm_storeu_si128(output_mm + 2, r0_2);
   _mm_storeu_si128(output_mm + 3, r0_3);

   _mm_storeu_si128(output_mm + 4, r1_0);
   _mm_storeu_si128(output_mm + 5, r1_1);
   _mm_storeu_si128(output_mm + 6, r1_2);
   _mm_storeu_si128(output_mm + 7, r1_3);

   _mm_storeu_si128(output_mm + 8, r2_0);
   _mm_storeu_si128(output_mm + 9, r2_1);
   _mm_storeu_si128(output_mm + 10, r2_2);
   _mm_storeu_si128(output_mm + 11, r2_3);

   _mm_storeu_si128(output_mm + 12, r3_0);
   _mm_storeu_si128(output_mm + 13, r3_1);
   _mm_storeu_si128(output_mm + 14, r3_2);
   _mm_storeu_si128(output_mm + 15, r3_3);

#undef mm_rotl

   input[12] += 4;
   if(input[12] < 4)
      input[13]++;
   }

}