-
Notifications
You must be signed in to change notification settings - Fork 1
/
chacha20.go
409 lines (377 loc) · 11 KB
/
chacha20.go
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// chacha20.go - public domain ChaCha20 encryption/decryption.
// Public domain is per <https://creativecommons.org/publicdomain/zero/1.0/>
//
// I used clang -E to preprocess chacha-ref.c from
// <https://cr.yp.to/chacha.html> to produce a prototype chacha20.go file,
// then hand- and sed-edited it to make true Go source code. I added
// New, Seek, XORKeyStream, Read and SetRounds, and made the default number of
// rounds 20.
//
// From the C file (chacha-ref.c):
// chacha-ref.c version 20080118
// D. J. Bernstein
// Public domain.
//
// This file (chacha20.go):
// Author: Ron Charlton <Ron[@]RonCharlton[.]org>
// Created: 2022-08-28
// Public domain.
//
// Type byte must be an alias for uint8.
//
// Example use (encrypt a file to another file; not sufficient for crypto. use):
// // 32-byte key and 8-byte iv assumed.
// // (error checks omitted)
// b, err := os.ReadFile("myfile")
// ctx := chacha20.New(key, iv)
// ctx.Encrypt(b, b)
// err = os.WriteFile("myfile.encrypted", b, 0644)
//
// chacha20.go v4.25 Encrypt on a 3.504 GHz M2 Mac Studio (go test -bench=.):
//
// Rounds MB/s
// ------ ----
// 8 807
// 12 657
// 20 485
//
// $Id: chacha20.go,v 4.26 2024-09-15 14:13:30-04 ron Exp $
////
// Package chacha20 provides public domain ChaCha20 encryption and decryption.
// Package chacha20 is derived from public domain chacha-ref.c at
// <https://cr.yp.to/chacha.html>.
//
// Some chacha20 methods panic when the ChaCha keystream is exhausted
// after producing about 1.2 zettabytes. A zettabyte is so much data that
// it is nearly impossible to generate that much. At 1 ns/block it
// would take 584+ years to generate 1.2 zettabytes.
package chacha20
import (
"encoding/binary"
"io"
)
// Rounds can be 8, 12 or 20. Lower numbers are likely less secure.
// Higher numbers consume more compute time. ChaCha20 requires 20.
const defaultRounds = 20
// Using individual variables instead of an array provides 32% faster code.
func salsa20_wordtobyte(input []uint32, rounds int, output []byte) {
var t uint32
var z int
a := input[0]
b := input[1]
c := input[2]
d := input[3]
e := input[4]
f := input[5]
g := input[6]
h := input[7]
i := input[8]
j := input[9]
k := input[10]
l := input[11]
m := input[12]
n := input[13]
o := input[14]
p := input[15]
for z = rounds; z > 0; z -= 2 {
a += e
t = m ^ a
m = (t << 16) | (t >> (32 - 16))
i += m
t = e ^ i
e = (t << 12) | (t >> (32 - 12))
a += e
t = m ^ a
m = (t << 8) | (t >> (32 - 8))
i += m
t = e ^ i
e = (t << 7) | (t >> (32 - 7))
b += f
t = n ^ b
n = (t << 16) | (t >> (32 - 16))
j += n
t = f ^ j
f = (t << 12) | (t >> (32 - 12))
b += f
t = n ^ b
n = (t << 8) | (t >> (32 - 8))
j += n
t = f ^ j
f = (t << 7) | (t >> (32 - 7))
c += g
t = o ^ c
o = (t << 16) | (t >> (32 - 16))
k += o
t = g ^ k
g = (t << 12) | (t >> (32 - 12))
c += g
t = o ^ c
o = (t << 8) | (t >> (32 - 8))
k += o
t = g ^ k
g = (t << 7) | (t >> (32 - 7))
d += h
t = p ^ d
p = (t << 16) | (t >> (32 - 16))
l += p
t = h ^ l
h = (t << 12) | (t >> (32 - 12))
d += h
t = p ^ d
p = (t << 8) | (t >> (32 - 8))
l += p
t = h ^ l
h = (t << 7) | (t >> (32 - 7))
a += f
t = p ^ a
p = (t << 16) | (t >> (32 - 16))
k += p
t = f ^ k
f = (t << 12) | (t >> (32 - 12))
a += f
t = p ^ a
p = (t << 8) | (t >> (32 - 8))
k += p
t = f ^ k
f = (t << 7) | (t >> (32 - 7))
b += g
t = m ^ b
m = (t << 16) | (t >> (32 - 16))
l += m
t = g ^ l
g = (t << 12) | (t >> (32 - 12))
b += g
t = m ^ b
m = (t << 8) | (t >> (32 - 8))
l += m
t = g ^ l
g = (t << 7) | (t >> (32 - 7))
c += h
t = n ^ c
n = (t << 16) | (t >> (32 - 16))
i += n
t = h ^ i
h = (t << 12) | (t >> (32 - 12))
c += h
t = n ^ c
n = (t << 8) | (t >> (32 - 8))
i += n
t = h ^ i
h = (t << 7) | (t >> (32 - 7))
d += e
t = o ^ d
o = (t << 16) | (t >> (32 - 16))
j += o
t = e ^ j
e = (t << 12) | (t >> (32 - 12))
d += e
t = o ^ d
o = (t << 8) | (t >> (32 - 8))
j += o
t = e ^ j
e = (t << 7) | (t >> (32 - 7))
}
a += input[0]
binary.LittleEndian.PutUint32(output[4*0:], a)
b += input[1]
binary.LittleEndian.PutUint32(output[4*1:], b)
c += input[2]
binary.LittleEndian.PutUint32(output[4*2:], c)
d += input[3]
binary.LittleEndian.PutUint32(output[4*3:], d)
e += input[4]
binary.LittleEndian.PutUint32(output[4*4:], e)
f += input[5]
binary.LittleEndian.PutUint32(output[4*5:], f)
g += input[6]
binary.LittleEndian.PutUint32(output[4*6:], g)
h += input[7]
binary.LittleEndian.PutUint32(output[4*7:], h)
i += input[8]
binary.LittleEndian.PutUint32(output[4*8:], i)
j += input[9]
binary.LittleEndian.PutUint32(output[4*9:], j)
k += input[10]
binary.LittleEndian.PutUint32(output[4*10:], k)
l += input[11]
binary.LittleEndian.PutUint32(output[4*11:], l)
m += input[12]
binary.LittleEndian.PutUint32(output[4*12:], m)
n += input[13]
binary.LittleEndian.PutUint32(output[4*13:], n)
o += input[14]
binary.LittleEndian.PutUint32(output[4*14:], o)
p += input[15]
binary.LittleEndian.PutUint32(output[4*15:], p)
}
// ChaCha20_ctx contains state information for a ChaCha20 context.
type ChaCha20_ctx struct {
input []uint32
output []byte
next int
eof bool
rounds int
}
// ChaCha block length in bytes
const blockLen = 64
// New allocates a new ChaCha20 context and sets it up
// with the caller's key and iv. The default number of rounds is 20.
func New(key, iv []byte) (ctx *ChaCha20_ctx) {
ctx = &ChaCha20_ctx{
input: make([]uint32, 16),
output: make([]byte, blockLen),
next: blockLen,
rounds: defaultRounds,
}
ctx.KeySetup(key)
ctx.IvSetup(iv)
return
}
// SetRounds sets the number of rounds used by Encrypt, Decrypt, Read,
// XORKeyStream and Keystream for a ChaCha20 context.
// The valid values for r are 8, 12 and 20.
// SetRounds ignores any other value. ChaCha20's default number
// of rounds is 20. Fewer rounds may be less secure. More
// rounds consume more compute time. ChaCha8 requires 8 rounds, ChaCha12
// requires 12 and ChaCha20 requires 20.
func (x *ChaCha20_ctx) SetRounds(r int) {
if r == 8 || r == 12 || r == 20 {
x.rounds = r
}
}
// Seek moves x directly to 64-byte block number n in constant time.
func (x *ChaCha20_ctx) Seek(n uint64) {
x.input[12] = uint32(n)
x.input[13] = uint32(n >> 32)
x.eof = false
x.next = blockLen
}
var sigma = []byte("expand 32-byte k")
var tau = []byte("expand 16-byte k")
// KeySetup sets up ChaCha20 context x with key k. KeySetup panics if len(k)
// is not 16 or 32. A key length of 32 is recommended.
func (x *ChaCha20_ctx) KeySetup(k []byte) {
var constants []byte
kbytes := len(k)
if kbytes != 16 && kbytes != 32 {
panic("chacha20: invalid key length; must be 16 or 32 bytes.")
}
x.input[4] = binary.LittleEndian.Uint32(k[0:])
x.input[5] = binary.LittleEndian.Uint32(k[4:])
x.input[6] = binary.LittleEndian.Uint32(k[8:])
x.input[7] = binary.LittleEndian.Uint32(k[12:])
if kbytes == 32 {
k = k[16:]
constants = sigma
} else {
constants = tau
}
x.input[8] = binary.LittleEndian.Uint32(k[0:])
x.input[9] = binary.LittleEndian.Uint32(k[4:])
x.input[10] = binary.LittleEndian.Uint32(k[8:])
x.input[11] = binary.LittleEndian.Uint32(k[12:])
x.input[0] = binary.LittleEndian.Uint32(constants[0:])
x.input[1] = binary.LittleEndian.Uint32(constants[4:])
x.input[2] = binary.LittleEndian.Uint32(constants[8:])
x.input[3] = binary.LittleEndian.Uint32(constants[12:])
x.next = blockLen
}
// IvSetup sets initialization vector iv as a nonce for ChaCha20 context x.
// It also sets the context's counter to 0. IvSetup panics if len(iv) is not 8.
func (x *ChaCha20_ctx) IvSetup(iv []byte) {
if len(iv) != 8 {
panic("chacha20: invalid iv length; must be 8.")
}
x.input[12] = 0
x.input[13] = 0
x.input[14] = binary.LittleEndian.Uint32(iv[0:])
x.input[15] = binary.LittleEndian.Uint32(iv[4:])
x.next = blockLen
x.eof = false
}
// Encrypt puts ciphertext into c given plaintext m. Any length is allowed
// for m. The same memory may be used for m and c. Encrypt panics if len(c) is
// less than len(m). It returns io.EOF when the keystream is exhausted
// after producing 1.2 zettabytes. It will panic if called with the
// the same context after io.EOF is returned, unless re-initialized.
func (x *ChaCha20_ctx) Encrypt(m, c []byte) (n int, err error) {
size := len(m)
if size == 0 {
return 0, nil
}
if len(c) < size {
panic("chacha20.Encrypt: insufficient space; c is shorter than m.")
}
idx := x.next
if x.eof && idx >= blockLen {
panic("chacha20: keystream is exhausted")
}
for n = 0; n < size; n++ {
if idx >= blockLen {
if x.eof {
break
}
salsa20_wordtobyte(x.input, x.rounds, x.output)
x.input[12] += 1
if x.input[12] == 0 {
x.input[13] += 1
/* stopping at 2^70 bytes per nonce is user's responsibility */
/* At 1 ns/block: 584+ years to exhaust the keystream.
* (2^70 bytes)/(64 bytes/ns) RonC
*/
if x.input[13] == 0 {
x.eof = true
}
}
idx = 0
}
c[n] = m[n] ^ x.output[idx]
idx++
}
x.next = idx
if x.eof && idx >= blockLen {
err = io.EOF
}
return
}
// Decrypt puts plaintext into m given ciphertext c. Any length is allowed
// for c. The same memory may be used for c and m. Decrypt panics if len(m) is
// less than len(c). It returns io.EOF when the keystream is exhausted
// after producing 1.2 zettabytes. It will panic if called with the
// the same context after io.EOF is returned, unless re-initialized.
func (x *ChaCha20_ctx) Decrypt(c, m []byte) (int, error) {
if len(m) < len(c) {
panic("chacha20.Decrypt: insufficient space; m is shorter than c.")
}
return x.Encrypt(c, m)
}
// Keystream fills stream with cryptographically secure pseudorandom bytes
// from x's keystream when a random key and iv are used. Keystream
// panics when the ChaCha keystream is exhausted after producing 1.2 zettabytes.
func (x *ChaCha20_ctx) Keystream(stream []byte) {
c := make([]byte, len(stream)) // faster than for loop with assignment
x.Encrypt(c, stream)
}
// The idea for adding XORKeyStream and Read came from skeeto's public
// domain ChaCha implementation. Neither is copied or ported from that
// implementation.
// XORKeyStream implements the crypto/cipher.Stream interface.
// XORKeyStream XORs src bytes with ChaCha's key stream and puts the result
// in dst. XORKeyStream panics if len(dst) is less than len(src), or
// when the ChaCha keystream is exhausted after producing 1.2 zettabytes.
func (x *ChaCha20_ctx) XORKeyStream(dst, src []byte) {
if len(dst) < len(src) {
panic("chacha20.XORKeyStream: insufficient space; dst is shorter than src.")
}
x.Encrypt(src, dst)
}
// Read fills b with cryptographically secure pseudorandom bytes from x's
// keystream when a random key and iv are used.
// Read implements the io.Reader interface.
// Read returns io.EOF when the keystream is exhausted after producing 1.2
// zettabytes. It will panic if called with the
// the same context after io.EOF is returned, unless re-initialized.
func (x *ChaCha20_ctx) Read(b []byte) (int, error) {
c := make([]byte, len(b)) // faster than for loop with assignment
return x.Encrypt(c, b)
}