@@ -21,26 +21,20 @@ constexpr static inline uint32_t rotl32(uint32_t v, int c) { return (v << c) | (
21
21
22
22
#define REPEAT10 (a ) do { {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; } while (0 )
23
23
24
- static const unsigned char sigma[] = " expand 32-byte k" ;
25
-
26
24
void ChaCha20Aligned::SetKey32 (const unsigned char * k)
27
25
{
28
- input[0 ] = ReadLE32 (sigma + 0 );
29
- input[1 ] = ReadLE32 (sigma + 4 );
30
- input[2 ] = ReadLE32 (sigma + 8 );
31
- input[3 ] = ReadLE32 (sigma + 12 );
32
- input[4 ] = ReadLE32 (k + 0 );
33
- input[5 ] = ReadLE32 (k + 4 );
34
- input[6 ] = ReadLE32 (k + 8 );
35
- input[7 ] = ReadLE32 (k + 12 );
36
- input[8 ] = ReadLE32 (k + 16 );
37
- input[9 ] = ReadLE32 (k + 20 );
38
- input[10 ] = ReadLE32 (k + 24 );
39
- input[11 ] = ReadLE32 (k + 28 );
40
- input[12 ] = 0 ;
41
- input[13 ] = 0 ;
42
- input[14 ] = 0 ;
43
- input[15 ] = 0 ;
26
+ input[0 ] = ReadLE32 (k + 0 );
27
+ input[1 ] = ReadLE32 (k + 4 );
28
+ input[2 ] = ReadLE32 (k + 8 );
29
+ input[3 ] = ReadLE32 (k + 12 );
30
+ input[4 ] = ReadLE32 (k + 16 );
31
+ input[5 ] = ReadLE32 (k + 20 );
32
+ input[6 ] = ReadLE32 (k + 24 );
33
+ input[7 ] = ReadLE32 (k + 28 );
34
+ input[8 ] = 0 ;
35
+ input[9 ] = 0 ;
36
+ input[10 ] = 0 ;
37
+ input[11 ] = 0 ;
44
38
}
45
39
46
40
ChaCha20Aligned::ChaCha20Aligned ()
@@ -55,45 +49,41 @@ ChaCha20Aligned::ChaCha20Aligned(const unsigned char* key32)
55
49
56
50
void ChaCha20Aligned::SetIV (uint64_t iv)
57
51
{
58
- input[14 ] = iv;
59
- input[15 ] = iv >> 32 ;
52
+ input[10 ] = iv;
53
+ input[11 ] = iv >> 32 ;
60
54
}
61
55
62
56
void ChaCha20Aligned::Seek64 (uint64_t pos)
63
57
{
64
- input[12 ] = pos;
65
- input[13 ] = pos >> 32 ;
58
+ input[8 ] = pos;
59
+ input[9 ] = pos >> 32 ;
66
60
}
67
61
68
62
inline void ChaCha20Aligned::Keystream64 (unsigned char * c, size_t blocks)
69
63
{
70
64
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
71
- uint32_t j0 , j1 , j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
65
+ uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
72
66
73
67
if (!blocks) return ;
74
68
75
- j0 = input[0 ];
76
- j1 = input[1 ];
77
- j2 = input[2 ];
78
- j3 = input[3 ];
79
- j4 = input[4 ];
80
- j5 = input[5 ];
81
- j6 = input[6 ];
82
- j7 = input[7 ];
83
- j8 = input[8 ];
84
- j9 = input[9 ];
85
- j10 = input[10 ];
86
- j11 = input[11 ];
87
- j12 = input[12 ];
88
- j13 = input[13 ];
89
- j14 = input[14 ];
90
- j15 = input[15 ];
69
+ j4 = input[0 ];
70
+ j5 = input[1 ];
71
+ j6 = input[2 ];
72
+ j7 = input[3 ];
73
+ j8 = input[4 ];
74
+ j9 = input[5 ];
75
+ j10 = input[6 ];
76
+ j11 = input[7 ];
77
+ j12 = input[8 ];
78
+ j13 = input[9 ];
79
+ j14 = input[10 ];
80
+ j15 = input[11 ];
91
81
92
82
for (;;) {
93
- x0 = j0 ;
94
- x1 = j1 ;
95
- x2 = j2 ;
96
- x3 = j3 ;
83
+ x0 = 0x61707865 ;
84
+ x1 = 0x3320646e ;
85
+ x2 = 0x79622d32 ;
86
+ x3 = 0x6b206574 ;
97
87
x4 = j4;
98
88
x5 = j5;
99
89
x6 = j6;
@@ -119,10 +109,10 @@ inline void ChaCha20Aligned::Keystream64(unsigned char* c, size_t blocks)
119
109
QUARTERROUND ( x3, x4, x9,x14);
120
110
);
121
111
122
- x0 += j0 ;
123
- x1 += j1 ;
124
- x2 += j2 ;
125
- x3 += j3 ;
112
+ x0 += 0x61707865 ;
113
+ x1 += 0x3320646e ;
114
+ x2 += 0x79622d32 ;
115
+ x3 += 0x6b206574 ;
126
116
x4 += j4;
127
117
x5 += j5;
128
118
x6 += j6;
@@ -157,8 +147,8 @@ inline void ChaCha20Aligned::Keystream64(unsigned char* c, size_t blocks)
157
147
WriteLE32 (c + 60 , x15);
158
148
159
149
if (blocks == 1 ) {
160
- input[12 ] = j12;
161
- input[13 ] = j13;
150
+ input[8 ] = j12;
151
+ input[9 ] = j13;
162
152
return ;
163
153
}
164
154
blocks -= 1 ;
@@ -169,32 +159,28 @@ inline void ChaCha20Aligned::Keystream64(unsigned char* c, size_t blocks)
169
159
inline void ChaCha20Aligned::Crypt64 (const unsigned char * m, unsigned char * c, size_t blocks)
170
160
{
171
161
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
172
- uint32_t j0 , j1 , j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
162
+ uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
173
163
174
164
if (!blocks) return ;
175
165
176
- j0 = input[0 ];
177
- j1 = input[1 ];
178
- j2 = input[2 ];
179
- j3 = input[3 ];
180
- j4 = input[4 ];
181
- j5 = input[5 ];
182
- j6 = input[6 ];
183
- j7 = input[7 ];
184
- j8 = input[8 ];
185
- j9 = input[9 ];
186
- j10 = input[10 ];
187
- j11 = input[11 ];
188
- j12 = input[12 ];
189
- j13 = input[13 ];
190
- j14 = input[14 ];
191
- j15 = input[15 ];
166
+ j4 = input[0 ];
167
+ j5 = input[1 ];
168
+ j6 = input[2 ];
169
+ j7 = input[3 ];
170
+ j8 = input[4 ];
171
+ j9 = input[5 ];
172
+ j10 = input[6 ];
173
+ j11 = input[7 ];
174
+ j12 = input[8 ];
175
+ j13 = input[9 ];
176
+ j14 = input[10 ];
177
+ j15 = input[11 ];
192
178
193
179
for (;;) {
194
- x0 = j0 ;
195
- x1 = j1 ;
196
- x2 = j2 ;
197
- x3 = j3 ;
180
+ x0 = 0x61707865 ;
181
+ x1 = 0x3320646e ;
182
+ x2 = 0x79622d32 ;
183
+ x3 = 0x6b206574 ;
198
184
x4 = j4;
199
185
x5 = j5;
200
186
x6 = j6;
@@ -220,10 +206,10 @@ inline void ChaCha20Aligned::Crypt64(const unsigned char* m, unsigned char* c, s
220
206
QUARTERROUND ( x3, x4, x9,x14);
221
207
);
222
208
223
- x0 += j0 ;
224
- x1 += j1 ;
225
- x2 += j2 ;
226
- x3 += j3 ;
209
+ x0 += 0x61707865 ;
210
+ x1 += 0x3320646e ;
211
+ x2 += 0x79622d32 ;
212
+ x3 += 0x6b206574 ;
227
213
x4 += j4;
228
214
x5 += j5;
229
215
x6 += j6;
@@ -275,8 +261,8 @@ inline void ChaCha20Aligned::Crypt64(const unsigned char* m, unsigned char* c, s
275
261
WriteLE32 (c + 60 , x15);
276
262
277
263
if (blocks == 1 ) {
278
- input[12 ] = j12;
279
- input[13 ] = j13;
264
+ input[8 ] = j12;
265
+ input[9 ] = j13;
280
266
return ;
281
267
}
282
268
blocks -= 1 ;
0 commit comments