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
258
259
260
261
262
263
|
/* Copyright (c) 2015, Vsevolod Stakhov
* Copyright (c) 2015, Andrew Moon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "cryptobox.h"
#include "chacha.h"
#include "platform_config.h"
extern unsigned long cpu_config;
typedef struct chacha_impl_t {
unsigned long cpu_flags;
const char *desc;
void (*chacha) (const chacha_key *key, const chacha_iv *iv,
const unsigned char *in, unsigned char *out, size_t inlen,
size_t rounds);
void (*xchacha) (const chacha_key *key, const chacha_iv24 *iv,
const unsigned char *in, unsigned char *out, size_t inlen,
size_t rounds);
void (*chacha_blocks) (chacha_state_internal *state,
const unsigned char *in, unsigned char *out, size_t bytes);
void (*hchacha) (const unsigned char key[32], const unsigned char iv[16],
unsigned char out[32], size_t rounds);
} chacha_impl_t;
#define CHACHA_DECLARE(ext) \
void chacha_##ext(const chacha_key *key, const chacha_iv *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \
void xchacha_##ext(const chacha_key *key, const chacha_iv24 *iv, const unsigned char *in, unsigned char *out, size_t inlen, size_t rounds); \
void chacha_blocks_##ext(chacha_state_internal *state, const unsigned char *in, unsigned char *out, size_t bytes); \
void hchacha_##ext(const unsigned char key[32], const unsigned char iv[16], unsigned char out[32], size_t rounds);
#define CHACHA_IMPL(cpuflags, desc, ext) \
{(cpuflags), desc, chacha_##ext, xchacha_##ext, chacha_blocks_##ext, hchacha_##ext}
#if defined(HAVE_AVX2)
CHACHA_DECLARE(avx2)
#define CHACHA_AVX2 CHACHA_IMPL(CPUID_AVX2, "avx2", avx2)
#endif
#if defined(HAVE_AVX)
CHACHA_DECLARE(avx)
#define CHACHA_AVX CHACHA_IMPL(CPUID_AVX, "avx", avx)
#endif
#if defined(HAVE_SSE2)
CHACHA_DECLARE(sse2)
#define CHACHA_SSE2 CHACHA_IMPL(CPUID_SSE2, "sse2", sse2)
#endif
CHACHA_DECLARE(ref)
#define CHACHA_GENERIC CHACHA_IMPL(0, "generic", ref)
static const chacha_impl_t chacha_list[] = {
CHACHA_GENERIC,
#if defined(CHACHA_AVX2)
CHACHA_AVX2,
#endif
#if defined(CHACHA_AVX)
CHACHA_AVX,
#endif
#if defined(CHACHA_SSE2)
CHACHA_SSE2
#endif
};
static const chacha_impl_t *chacha_impl = &chacha_list[0];
static int
chacha_is_aligned (const void *p)
{
return ((size_t) p & (sizeof(size_t) - 1)) == 0;
}
const char *
chacha_load (void)
{
guint i;
if (cpu_config != 0) {
for (i = 0; i < G_N_ELEMENTS (chacha_list); i ++) {
if (chacha_list[i].cpu_flags & cpu_config) {
chacha_impl = &chacha_list[i];
break;
}
}
}
return chacha_impl->desc;
}
void chacha_init (chacha_state *S, const chacha_key *key,
const chacha_iv *iv, size_t rounds)
{
chacha_state_internal *state = (chacha_state_internal *) S;
memcpy (state->s + 0, key, 32);
memset (state->s + 32, 0, 8);
memcpy (state->s + 40, iv, 8);
state->rounds = rounds;
state->leftover = 0;
}
/* processes inlen bytes (can do partial blocks), handling input/output alignment */
static void
chacha_consume (chacha_state_internal *state,
const unsigned char *in, unsigned char *out, size_t inlen)
{
unsigned char buffer[16 * CHACHA_BLOCKBYTES];
int in_aligned, out_aligned;
/* it's ok to call with 0 bytes */
if (!inlen)
return;
/* if everything is aligned, handle directly */
in_aligned = chacha_is_aligned (in);
out_aligned = chacha_is_aligned (out);
if (in_aligned && out_aligned) {
chacha_impl->chacha_blocks (state, in, out, inlen);
return;
}
/* copy the unaligned data to an aligned buffer and process in chunks */
while (inlen) {
const size_t bytes = (inlen > sizeof(buffer)) ? sizeof(buffer) : inlen;
const unsigned char *src = in;
unsigned char *dst = (out_aligned) ? out : buffer;
if (!in_aligned) {
memcpy (buffer, in, bytes);
src = buffer;
}
chacha_impl->chacha_blocks (state, src, dst, bytes);
if (!out_aligned)
memcpy (out, buffer, bytes);
if (in)
in += bytes;
out += bytes;
inlen -= bytes;
}
}
/* hchacha */
void hchacha (const unsigned char key[32],
const unsigned char iv[16], unsigned char out[32], size_t rounds)
{
chacha_impl->hchacha (key, iv, out, rounds);
}
/* update, returns number of bytes written to out */
size_t
chacha_update (chacha_state *S, const unsigned char *in, unsigned char *out,
size_t inlen)
{
chacha_state_internal *state = (chacha_state_internal *) S;
unsigned char *out_start = out;
size_t bytes;
/* enough for at least one block? */
while ((state->leftover + inlen) >= CHACHA_BLOCKBYTES) {
/* handle the previous data */
if (state->leftover) {
bytes = (CHACHA_BLOCKBYTES - state->leftover);
if (in) {
memcpy (state->buffer + state->leftover, in, bytes);
in += bytes;
}
chacha_consume (state, (in) ? state->buffer : NULL, out,
CHACHA_BLOCKBYTES);
inlen -= bytes;
out += CHACHA_BLOCKBYTES;
state->leftover = 0;
}
/* handle the direct data */
bytes = (inlen & ~(CHACHA_BLOCKBYTES - 1));
if (bytes) {
chacha_consume (state, in, out, bytes);
inlen -= bytes;
if (in)
in += bytes;
out += bytes;
}
}
/* handle leftover data */
if (inlen) {
if (in)
memcpy (state->buffer + state->leftover, in, inlen);
else
memset (state->buffer + state->leftover, 0, inlen);
state->leftover += inlen;
}
return out - out_start;
}
/* finalize, write out any leftover data */
size_t
chacha_final (chacha_state *S, unsigned char *out)
{
chacha_state_internal *state = (chacha_state_internal *) S;
size_t leftover = state->leftover;
if (leftover) {
if (chacha_is_aligned (out)) {
chacha_impl->chacha_blocks (state, state->buffer, out, leftover);
}
else {
chacha_impl->chacha_blocks (state, state->buffer, state->buffer,
leftover);
memcpy (out, state->buffer, leftover);
}
}
rspamd_explicit_memzero (S, sizeof(chacha_state));
return leftover;
}
/* one-shot, input/output assumed to be word aligned */
void
chacha (const chacha_key *key, const chacha_iv *iv,
const unsigned char *in, unsigned char *out, size_t inlen,
size_t rounds)
{
chacha_impl->chacha (key, iv, in, out, inlen, rounds);
}
/*
xchacha, chacha with a 192 bit nonce
*/
void
xchacha_init (chacha_state *S, const chacha_key *key,
const chacha_iv24 *iv, size_t rounds)
{
chacha_key subkey;
hchacha (key->b, iv->b, subkey.b, rounds);
chacha_init (S, &subkey, (chacha_iv *) (iv->b + 16), rounds);
}
/* one-shot, input/output assumed to be word aligned */
void
xchacha (const chacha_key *key, const chacha_iv24 *iv,
const unsigned char *in, unsigned char *out, size_t inlen,
size_t rounds)
{
chacha_impl->xchacha (key, iv, in, out, inlen, rounds);
}
|