-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchacha.go
123 lines (104 loc) · 4.88 KB
/
chacha.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
package chacha
import (
"crypto/cipher"
"github.com/metacubex/chacha/chacha"
"github.com/metacubex/chacha/chachapoly1305"
"github.com/metacubex/chacha/poly1305"
)
const (
// NonceSize is the size of the ChaCha20 nonce in bytes.
NonceSize = chacha.NonceSize
// INonceSize is the size of the IETF-ChaCha20 nonce in bytes.
INonceSize = chacha.INonceSize
// XNonceSize is the size of the XChaCha20 nonce in bytes.
XNonceSize = chacha.XNonceSize
// KeySize is the size of the key in bytes.
KeySize = chacha.KeySize
// Overhead is the size of the Poly1305 authentication tag, and the
// difference between a ciphertext length and its plaintext.
Overhead = poly1305.TagSize
)
// NewChaCha20 returns a cipher.Stream implementing the ChaCha20 or XChaCha20
// stream cipher. The nonce must be unique for one key for all time.
// The length of the nonce determinds the version of ChaCha20:
// - NonceSize: ChaCha20/r with a 64 bit nonce and a 2^64 * 64 byte period.
// - INonceSize: ChaCha20/r as defined in RFC 7539 and a 2^32 * 64 byte period.
// - XNonceSize: XChaCha20/r with a 192 bit nonce and a 2^64 * 64 byte period.
// If the nonce is neither 64, 96 nor 192 bits long, a non-nil error is returned.
func NewChaCha20(nonce, key []byte) (cipher.Stream, error) {
return chacha.NewCipher(nonce, key, 20)
}
// NewChaCha12 returns a cipher.Stream implementing the ChaCha12 or XChaCha12
// stream cipher. The nonce must be unique for one key for all time.
// The length of the nonce determinds the version of ChaCha20:
// - NonceSize: ChaCha20/r with a 64 bit nonce and a 2^64 * 64 byte period.
// - INonceSize: ChaCha20/r as defined in RFC 7539 and a 2^32 * 64 byte period.
// - XNonceSize: XChaCha20/r with a 192 bit nonce and a 2^64 * 64 byte period.
// If the nonce is neither 64, 96 nor 192 bits long, a non-nil error is returned.
func NewChaCha12(nonce, key []byte) (cipher.Stream, error) {
return chacha.NewCipher(nonce, key, 12)
}
// NewChaCha8 returns a cipher.Stream implementing the ChaCha8 or XChaCha8
// stream cipher. The nonce must be unique for one key for all time.
// The length of the nonce determinds the version of ChaCha20:
// - NonceSize: ChaCha20/r with a 64 bit nonce and a 2^64 * 64 byte period.
// - INonceSize: ChaCha20/r as defined in RFC 7539 and a 2^32 * 64 byte period.
// - XNonceSize: XChaCha20/r with a 192 bit nonce and a 2^64 * 64 byte period.
// If the nonce is neither 64, 96 nor 192 bits long, a non-nil error is returned.
func NewChaCha8(nonce, key []byte) (cipher.Stream, error) {
return chacha.NewCipher(nonce, key, 8)
}
// NewChaCha20Poly1305 returns a cipher.AEAD implementing the
// ChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewChaCha20Poly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, NonceSize, 20)
}
// NewChaCha12Poly1305 returns a cipher.AEAD implementing the
// ChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewChaCha12Poly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, NonceSize, 12)
}
// NewChaCha8Poly1305 returns a cipher.AEAD implementing the
// ChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewChaCha8Poly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, NonceSize, 8)
}
// NewChaCha20IETFPoly1305 returns a cipher.AEAD implementing the
// ChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewChaCha20IETFPoly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, INonceSize, 20)
}
// NewChaCha12IETFPoly1305 returns a cipher.AEAD implementing the
// ChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewChaCha12IETFPoly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, INonceSize, 12)
}
// NewChaCha8IETFPoly1305 returns a cipher.AEAD implementing the
// ChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewChaCha8IETFPoly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, INonceSize, 8)
}
// NewXChaCha20IETFPoly1305 returns a cipher.AEAD implementing the
// XChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewXChaCha20IETFPoly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, XNonceSize, 20)
}
// NewXChaCha12IETFPoly1305 returns a cipher.AEAD implementing the
// XChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewXChaCha12IETFPoly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, XNonceSize, 12)
}
// NewXChaCha8IETFPoly1305 returns a cipher.AEAD implementing the
// XChaCha20Poly1305 construction specified in RFC 7539 with a
// 128 bit auth. tag.
func NewXChaCha8IETFPoly1305(key []byte) (cipher.AEAD, error) {
return chachapoly1305.NewCipher(key, XNonceSize, 8)
}