Coverage Report

Created: 2022-06-23 06:44

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