Coverage Report

Created: 2021-05-04 09:02

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