-
Notifications
You must be signed in to change notification settings - Fork 21
/
aead_chacha20poly1305.go
223 lines (190 loc) · 5.71 KB
/
aead_chacha20poly1305.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
package sodium
// #cgo pkg-config: libsodium
// #include <stdlib.h>
// #include <sodium.h>
import "C"
var (
cryptoAEADChaCha20Poly1305IETFKeyBytes = int(C.crypto_aead_chacha20poly1305_ietf_keybytes())
cryptoAEADChaCha20Poly1305IETFNPubBytes = int(C.crypto_aead_chacha20poly1305_ietf_npubbytes())
cryptoAEADChaCha20Poly1305IETFABytes = int(C.crypto_aead_chacha20poly1305_ietf_abytes())
)
type AEADCPNonce struct {
Bytes
}
func (AEADCPNonce) Size() int {
return cryptoAEADChaCha20Poly1305IETFNPubBytes
}
func (n *AEADCPNonce) Next() {
C.sodium_increment((*C.uchar)(&n.Bytes[0]), (C.size_t)(cryptoAEADChaCha20Poly1305IETFNPubBytes))
}
type AEADCPKey struct {
Bytes
}
func (AEADCPKey) Size() int {
return cryptoAEADChaCha20Poly1305IETFKeyBytes
}
func MakeAEADCPKey() AEADCPKey {
b := make([]byte, cryptoAEADChaCha20Poly1305IETFKeyBytes);
C.crypto_aead_chacha20poly1305_ietf_keygen((*C.uchar)(&b[0]))
return AEADCPKey{b}
}
type AEADCPMAC struct {
Bytes
}
func (AEADCPMAC) Size() int {
return cryptoAEADChaCha20Poly1305IETFABytes
}
//AEADCPEncrypt encrypts message with AEADCPKey, and AEADCPNonce.
//Message then authenticated with additional data 'ad'.
//Authentication tag is append to the encrypted data.
func (b Bytes) AEADCPEncrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
c = make([]byte, bl+cryptoAEADChaCha20Poly1305IETFABytes)
cp, _ := plen(c)
var outlen C.ulonglong
adp, adl := plen(ad)
if int(C.crypto_aead_chacha20poly1305_ietf_encrypt(
(*C.uchar)(cp),
&outlen,
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(adp),
(C.ulonglong)(adl),
(*C.uchar)(nil),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
panic("see libsodium")
}
c = c[:outlen]
return
}
//AEADCPDecrypt decrypts message with AEADCPKey, and AEADCPNonce.
//The appended authenticated tag is verified with additional data 'ad' before decryption.
//
//It returns an error if decryption failed.
func (b Bytes) AEADCPDecrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
m = make([]byte, bl-cryptoAEADChaCha20Poly1305IETFABytes)
mp, _ := plen(m)
adp, adl := plen(ad)
var outlen C.ulonglong
if int(C.crypto_aead_chacha20poly1305_ietf_decrypt(
(*C.uchar)(mp),
&outlen,
(*C.uchar)(nil),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(adp),
(C.ulonglong)(adl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
err = ErrDecryptAEAD
}
m = m[:outlen]
return
}
//AEADCPEncryptDetached encrypts message with AEADCPKey, and AEADCPNonce.
//Message then authenticated with additional data 'ad'.
//Authentication tag is separated saved as 'mac'.
func (b Bytes) AEADCPEncryptDetached(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes, mac AEADCPMAC) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
adp, adl := plen(ad)
c = make([]byte, b.Length())
cp, _ := plen(c)
macb := make([]byte, cryptoAEADChaCha20Poly1305IETFABytes)
var outlen C.ulonglong
if int(C.crypto_aead_chacha20poly1305_ietf_encrypt_detached(
(*C.uchar)(cp),
(*C.uchar)(&macb[0]),
&outlen,
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(adp),
(C.ulonglong)(adl),
(*C.uchar)(nil),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
panic("see libsodium")
}
mac = AEADCPMAC{macb[:outlen]}
return
}
//AEADCPDecryptDetached decrypts message with AEADCPKey, and AEADCPNonce.
//The separated authenticated tag is verified with additional data 'ad' before decryption.
//
//It returns an error if decryption failed.
func (b Bytes) AEADCPDecryptDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error) {
checkTypedSize(&mac, "public mac")
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
adp, adl := plen(ad)
m = make([]byte, bl)
mp, _ := plen(m)
if int(C.crypto_aead_chacha20poly1305_ietf_decrypt_detached(
(*C.uchar)(mp),
(*C.uchar)(nil),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&mac.Bytes[0]),
(*C.uchar)(adp),
(C.ulonglong)(adl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
err = ErrDecryptAEAD
}
return
}
//AEADCPVerify decrypts message with AEADCPKey, and AEADCPNonce without writing decrypted data.
//The appended authenticated tag is verified with additional data 'ad' before decryption.
//
//It returns an error if decryption failed.
func (b Bytes) AEADCPVerify(ad Bytes, n AEADCPNonce, k AEADCPKey) (err error) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
adp, adl := plen(ad)
if int(C.crypto_aead_chacha20poly1305_ietf_decrypt(
(*C.uchar)(nil),
(*C.ulonglong)(nil),
(*C.uchar)(nil),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(adp),
(C.ulonglong)(adl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
err = ErrDecryptAEAD
}
return
}
//AEADCPVerifyDetached decrypts message with AEADCPKey, and AEADCPNonce without writing decrypted data.
//The separated authenticated tag is verified with additional data 'ad' before decryption.
//
//It returns an error if decryption failed.
func (b Bytes) AEADCPVerifyDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (err error) {
checkTypedSize(&mac, "public mac")
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
adp, adl := plen(ad)
if int(C.crypto_aead_chacha20poly1305_ietf_decrypt_detached(
(*C.uchar)(nil),
(*C.uchar)(nil),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&mac.Bytes[0]),
(*C.uchar)(adp),
(C.ulonglong)(adl),
(*C.uchar)(&n.Bytes[0]),
(*C.uchar)(&k.Bytes[0]))) != 0 {
err = ErrDecryptAEAD
}
return
}