-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChaCha20.h
295 lines (256 loc) · 9.83 KB
/
ChaCha20.h
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* This file is part of the ChaCha20 library (https://github.com/marcizhu/ChaCha20)
*
* Copyright (C) 2022 Marc Izquierdo
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*
Single file library. #include it as many times as you need, and
#define CHACHA20_IMPLEMENTATION in *one* c/cpp file BEFORE including it
*/
#ifndef __CHACHA20_H__
#define __CHACHA20_H__
/******************************************************************************
* HEADER *
******************************************************************************/
#include <stdint.h>
#include <stddef.h>
/** @brief Alias for ChaCha20 key type */
typedef uint8_t key256_t[32];
/** @brief Alias for ChaCha20 nonce type */
typedef uint8_t nonce96_t[12];
/** @brief ChaCha20 context */
typedef struct
{
uint32_t state[4*4];
} ChaCha20_Ctx;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Initialize the ChaCha20 Context
*
* Initialize the ChaCha20 context with the given 256-bit key, nonce and block
* count. The block count can be safely set to 0.
*
* @param ctx Pointer to ChaCha20 context
* @param key 256-bit key
* @param nonce 96-bit nonce
* @param count 32-bit block count
*/
void ChaCha20_init(ChaCha20_Ctx* ctx, const key256_t key, const nonce96_t nonce, uint32_t count);
/**
* @brief XOR a given buffer
*
* Encrypts/decrypts a given buffer, automatically incrementing the block count
* if necessary. It is possible to encript/decrypt a document using multiple
* calls to this function, but in such case it is required that all but the last
* call use a buffer length that is integer multiple of 64 bytes (e.g. 256 or
* 65536 bytes).
*
* In ChaCha20, encryption and decryption are the same opperation since it uses
* an XOR between the given buffer and the key stream. Thus, you can use this
* function both for encryption and decryption.
*
* @pre The context must be initialized prior to this call, and the buffer must
* not be @code{c} NULL @endcode.
*
* @param ctx Pointer to ChaCha20 context
* @param buffer Pointer to buffer
* @param bufflen Length of the buffer
*/
void ChaCha20_xor(ChaCha20_Ctx* ctx, uint8_t* buffer, size_t bufflen);
#ifdef __cplusplus
} // extern "C"
#endif
#ifdef CHACHA20_IMPLEMENTATION
/******************************************************************************
* IMPLEMENTATION *
******************************************************************************/
#include <assert.h>
#define CHACHA20_CONSTANT "expand 32-byte k"
#define CHACHA20_ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define CHACHA20_QR(a, b, c, d) \
a += b; d ^= a; d = CHACHA20_ROTL(d, 16); \
c += d; b ^= c; b = CHACHA20_ROTL(b, 12); \
a += b; d ^= a; d = CHACHA20_ROTL(d, 8); \
c += d; b ^= c; b = CHACHA20_ROTL(b, 7)
#ifdef __cplusplus
extern "C" {
#endif
static uint32_t pack4(const uint8_t* a)
{
uint32_t res =
(uint32_t)a[0] << 0 * 8
| (uint32_t)a[1] << 1 * 8
| (uint32_t)a[2] << 2 * 8
| (uint32_t)a[3] << 3 * 8;
return res;
}
static void ChaCha20_block_next(const uint32_t in[16], uint32_t out[16], uint8_t** keystream)
{
for(int i = 0; i < 4*4; i++)
out[i] = in[i];
// Round 1/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]); // Column 0
CHACHA20_QR(out[1], out[5], out[ 9], out[13]); // Column 1
CHACHA20_QR(out[2], out[6], out[10], out[14]); // Column 2
CHACHA20_QR(out[3], out[7], out[11], out[15]); // Column 3
CHACHA20_QR(out[0], out[5], out[10], out[15]); // Diagonal 1 (main diagonal)
CHACHA20_QR(out[1], out[6], out[11], out[12]); // Diagonal 2
CHACHA20_QR(out[2], out[7], out[ 8], out[13]); // Diagonal 3
CHACHA20_QR(out[3], out[4], out[ 9], out[14]); // Diagonal 4
// Round 2/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 3/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 4/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 5/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 6/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 7/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 8/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 9/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
// Round 10/10
CHACHA20_QR(out[0], out[4], out[ 8], out[12]);
CHACHA20_QR(out[1], out[5], out[ 9], out[13]);
CHACHA20_QR(out[2], out[6], out[10], out[14]);
CHACHA20_QR(out[3], out[7], out[11], out[15]);
CHACHA20_QR(out[0], out[5], out[10], out[15]);
CHACHA20_QR(out[1], out[6], out[11], out[12]);
CHACHA20_QR(out[2], out[7], out[ 8], out[13]);
CHACHA20_QR(out[3], out[4], out[ 9], out[14]);
for(int i = 0; i < 4*4; i++)
out[i] += in[i];
if(keystream != NULL)
*keystream = (uint8_t*)out;
}
void ChaCha20_init(ChaCha20_Ctx* ctx, const key256_t key, const nonce96_t nonce, uint32_t count)
{
ctx->state[ 0] = pack4((const uint8_t*)CHACHA20_CONSTANT + 0 * 4);
ctx->state[ 1] = pack4((const uint8_t*)CHACHA20_CONSTANT + 1 * 4);
ctx->state[ 2] = pack4((const uint8_t*)CHACHA20_CONSTANT + 2 * 4);
ctx->state[ 3] = pack4((const uint8_t*)CHACHA20_CONSTANT + 3 * 4);
ctx->state[ 4] = pack4(key + 0 * 4);
ctx->state[ 5] = pack4(key + 1 * 4);
ctx->state[ 6] = pack4(key + 2 * 4);
ctx->state[ 7] = pack4(key + 3 * 4);
ctx->state[ 8] = pack4(key + 4 * 4);
ctx->state[ 9] = pack4(key + 5 * 4);
ctx->state[10] = pack4(key + 6 * 4);
ctx->state[11] = pack4(key + 7 * 4);
ctx->state[12] = count;
ctx->state[13] = pack4(nonce + 0 * 4);
ctx->state[14] = pack4(nonce + 1 * 4);
ctx->state[15] = pack4(nonce + 2 * 4);
}
void ChaCha20_xor(ChaCha20_Ctx* ctx, uint8_t* buffer, size_t bufflen)
{
uint32_t tmp[4*4];
uint8_t* keystream = NULL;
for(size_t i = 0; i < bufflen; i += 64)
{
ChaCha20_block_next(ctx->state, tmp, &keystream);
ctx->state[12]++;
if(ctx->state[12] == 0)
{
ctx->state[13]++;
assert(ctx->state[13] != 0);
}
for(size_t j = i; j < i + 64; j++)
{
if(j >= bufflen)
break;
buffer[j] = buffer[j] ^ keystream[j - i];
}
}
}
#ifdef __cplusplus
} // extern "C"
#endif
#ifndef CHACHA20_NO_UNDEF
#undef CHACHA20_CONSTANT
#undef CHACHA20_ROTL
#undef CHACHA20_QR
#endif
#endif // CHACHA20_IMPLEMENTATION
#endif // __CHACHA20_H__