Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/stream/chacha/chacha_avx2/chacha_avx2.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/chacha.h>
8
#include <botan/internal/simd_avx2.h>
9
10
namespace Botan {
11
12
//static
13
BOTAN_FUNC_ISA("avx2")
14
void ChaCha::chacha_avx2_x8(uint8_t output[64*8], uint32_t state[16], size_t rounds)
15
48.8k
   {
16
48.8k
   SIMD_8x32::reset_registers();
17
48.8k
18
48.8k
   BOTAN_ASSERT(rounds % 2 == 0, "Valid rounds");
19
48.8k
   const SIMD_8x32 CTR0 = SIMD_8x32(0, 1, 2, 3, 4, 5, 6, 7);
20
48.8k
21
48.8k
   const uint32_t C = 0xFFFFFFFF - state[12];
22
48.8k
   const SIMD_8x32 CTR1 = SIMD_8x32(0, C < 1, C < 2, C < 3, C < 4, C < 5, C < 6, C < 7);
23
48.8k
24
48.8k
   SIMD_8x32 R00 = SIMD_8x32::splat(state[ 0]);
25
48.8k
   SIMD_8x32 R01 = SIMD_8x32::splat(state[ 1]);
26
48.8k
   SIMD_8x32 R02 = SIMD_8x32::splat(state[ 2]);
27
48.8k
   SIMD_8x32 R03 = SIMD_8x32::splat(state[ 3]);
28
48.8k
   SIMD_8x32 R04 = SIMD_8x32::splat(state[ 4]);
29
48.8k
   SIMD_8x32 R05 = SIMD_8x32::splat(state[ 5]);
30
48.8k
   SIMD_8x32 R06 = SIMD_8x32::splat(state[ 6]);
31
48.8k
   SIMD_8x32 R07 = SIMD_8x32::splat(state[ 7]);
32
48.8k
   SIMD_8x32 R08 = SIMD_8x32::splat(state[ 8]);
33
48.8k
   SIMD_8x32 R09 = SIMD_8x32::splat(state[ 9]);
34
48.8k
   SIMD_8x32 R10 = SIMD_8x32::splat(state[10]);
35
48.8k
   SIMD_8x32 R11 = SIMD_8x32::splat(state[11]);
36
48.8k
   SIMD_8x32 R12 = SIMD_8x32::splat(state[12]) + CTR0;
37
48.8k
   SIMD_8x32 R13 = SIMD_8x32::splat(state[13]) + CTR1;
38
48.8k
   SIMD_8x32 R14 = SIMD_8x32::splat(state[14]);
39
48.8k
   SIMD_8x32 R15 = SIMD_8x32::splat(state[15]);
40
48.8k
41
537k
   for(size_t r = 0; r != rounds / 2; ++r)
42
488k
      {
43
488k
      R00 += R04;
44
488k
      R01 += R05;
45
488k
      R02 += R06;
46
488k
      R03 += R07;
47
488k
48
488k
      R12 ^= R00;
49
488k
      R13 ^= R01;
50
488k
      R14 ^= R02;
51
488k
      R15 ^= R03;
52
488k
53
488k
      R12 = R12.rotl<16>();
54
488k
      R13 = R13.rotl<16>();
55
488k
      R14 = R14.rotl<16>();
56
488k
      R15 = R15.rotl<16>();
57
488k
58
488k
      R08 += R12;
59
488k
      R09 += R13;
60
488k
      R10 += R14;
61
488k
      R11 += R15;
62
488k
63
488k
      R04 ^= R08;
64
488k
      R05 ^= R09;
65
488k
      R06 ^= R10;
66
488k
      R07 ^= R11;
67
488k
68
488k
      R04 = R04.rotl<12>();
69
488k
      R05 = R05.rotl<12>();
70
488k
      R06 = R06.rotl<12>();
71
488k
      R07 = R07.rotl<12>();
72
488k
73
488k
      R00 += R04;
74
488k
      R01 += R05;
75
488k
      R02 += R06;
76
488k
      R03 += R07;
77
488k
78
488k
      R12 ^= R00;
79
488k
      R13 ^= R01;
80
488k
      R14 ^= R02;
81
488k
      R15 ^= R03;
82
488k
83
488k
      R12 = R12.rotl<8>();
84
488k
      R13 = R13.rotl<8>();
85
488k
      R14 = R14.rotl<8>();
86
488k
      R15 = R15.rotl<8>();
87
488k
88
488k
      R08 += R12;
89
488k
      R09 += R13;
90
488k
      R10 += R14;
91
488k
      R11 += R15;
92
488k
93
488k
      R04 ^= R08;
94
488k
      R05 ^= R09;
95
488k
      R06 ^= R10;
96
488k
      R07 ^= R11;
97
488k
98
488k
      R04 = R04.rotl<7>();
99
488k
      R05 = R05.rotl<7>();
100
488k
      R06 = R06.rotl<7>();
101
488k
      R07 = R07.rotl<7>();
102
488k
103
488k
      R00 += R05;
104
488k
      R01 += R06;
105
488k
      R02 += R07;
106
488k
      R03 += R04;
107
488k
108
488k
      R15 ^= R00;
109
488k
      R12 ^= R01;
110
488k
      R13 ^= R02;
111
488k
      R14 ^= R03;
112
488k
113
488k
      R15 = R15.rotl<16>();
114
488k
      R12 = R12.rotl<16>();
115
488k
      R13 = R13.rotl<16>();
116
488k
      R14 = R14.rotl<16>();
117
488k
118
488k
      R10 += R15;
119
488k
      R11 += R12;
120
488k
      R08 += R13;
121
488k
      R09 += R14;
122
488k
123
488k
      R05 ^= R10;
124
488k
      R06 ^= R11;
125
488k
      R07 ^= R08;
126
488k
      R04 ^= R09;
127
488k
128
488k
      R05 = R05.rotl<12>();
129
488k
      R06 = R06.rotl<12>();
130
488k
      R07 = R07.rotl<12>();
131
488k
      R04 = R04.rotl<12>();
132
488k
133
488k
      R00 += R05;
134
488k
      R01 += R06;
135
488k
      R02 += R07;
136
488k
      R03 += R04;
137
488k
138
488k
      R15 ^= R00;
139
488k
      R12 ^= R01;
140
488k
      R13 ^= R02;
141
488k
      R14 ^= R03;
142
488k
143
488k
      R15 = R15.rotl<8>();
144
488k
      R12 = R12.rotl<8>();
145
488k
      R13 = R13.rotl<8>();
146
488k
      R14 = R14.rotl<8>();
147
488k
148
488k
      R10 += R15;
149
488k
      R11 += R12;
150
488k
      R08 += R13;
151
488k
      R09 += R14;
152
488k
153
488k
      R05 ^= R10;
154
488k
      R06 ^= R11;
155
488k
      R07 ^= R08;
156
488k
      R04 ^= R09;
157
488k
158
488k
      R05 = R05.rotl<7>();
159
488k
      R06 = R06.rotl<7>();
160
488k
      R07 = R07.rotl<7>();
161
488k
      R04 = R04.rotl<7>();
162
488k
      }
163
48.8k
164
48.8k
   R00 += SIMD_8x32::splat(state[0]);
165
48.8k
   R01 += SIMD_8x32::splat(state[1]);
166
48.8k
   R02 += SIMD_8x32::splat(state[2]);
167
48.8k
   R03 += SIMD_8x32::splat(state[3]);
168
48.8k
   R04 += SIMD_8x32::splat(state[4]);
169
48.8k
   R05 += SIMD_8x32::splat(state[5]);
170
48.8k
   R06 += SIMD_8x32::splat(state[6]);
171
48.8k
   R07 += SIMD_8x32::splat(state[7]);
172
48.8k
   R08 += SIMD_8x32::splat(state[8]);
173
48.8k
   R09 += SIMD_8x32::splat(state[9]);
174
48.8k
   R10 += SIMD_8x32::splat(state[10]);
175
48.8k
   R11 += SIMD_8x32::splat(state[11]);
176
48.8k
   R12 += SIMD_8x32::splat(state[12]) + CTR0;
177
48.8k
   R13 += SIMD_8x32::splat(state[13]) + CTR1;
178
48.8k
   R14 += SIMD_8x32::splat(state[14]);
179
48.8k
   R15 += SIMD_8x32::splat(state[15]);
180
48.8k
181
48.8k
   SIMD_8x32::transpose(R00, R01, R02, R03, R04, R05, R06, R07);
182
48.8k
   SIMD_8x32::transpose(R08, R09, R10, R11, R12, R13, R14, R15);
183
48.8k
184
48.8k
   R00.store_le(output);
185
48.8k
   R08.store_le(output + 32*1);
186
48.8k
   R01.store_le(output + 32*2);
187
48.8k
   R09.store_le(output + 32*3);
188
48.8k
   R02.store_le(output + 32*4);
189
48.8k
   R10.store_le(output + 32*5);
190
48.8k
   R03.store_le(output + 32*6);
191
48.8k
   R11.store_le(output + 32*7);
192
48.8k
   R04.store_le(output + 32*8);
193
48.8k
   R12.store_le(output + 32*9);
194
48.8k
   R05.store_le(output + 32*10);
195
48.8k
   R13.store_le(output + 32*11);
196
48.8k
   R06.store_le(output + 32*12);
197
48.8k
   R14.store_le(output + 32*13);
198
48.8k
   R07.store_le(output + 32*14);
199
48.8k
   R15.store_le(output + 32*15);
200
48.8k
201
48.8k
   SIMD_8x32::zero_registers();
202
48.8k
203
48.8k
   state[12] += 8;
204
48.8k
   if(state[12] < 8)
205
0
      state[13]++;
206
48.8k
   }
207
}