-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathaead_xchacha20poly1305.go
222 lines (190 loc) · 5.77 KB
/
aead_xchacha20poly1305.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 (
cryptoAEADXChaCha20Poly1305IETFKeyBytes = int(C.crypto_aead_xchacha20poly1305_ietf_keybytes())
cryptoAEADXChaCha20Poly1305IETFNPubBytes = int(C.crypto_aead_xchacha20poly1305_ietf_npubbytes())
cryptoAEADXChaCha20Poly1305IETFABytes = int(C.crypto_aead_xchacha20poly1305_ietf_abytes())
)
type AEADXCPNonce struct {
Bytes
}
func (AEADXCPNonce) Size() int {
return cryptoAEADXChaCha20Poly1305IETFNPubBytes
}
func (n *AEADXCPNonce) Next() {
C.sodium_increment((*C.uchar)(&n.Bytes[0]), (C.size_t)(cryptoAEADChaCha20Poly1305IETFNPubBytes))
}
type AEADXCPKey struct {
Bytes
}
func MakeAEADXCPKey() AEADXCPKey {
b := make([]byte, cryptoAEADXChaCha20Poly1305IETFKeyBytes);
C.crypto_aead_xchacha20poly1305_ietf_keygen((*C.uchar)(&b[0]))
return AEADXCPKey{b}
}
func (AEADXCPKey) Size() int {
return cryptoAEADXChaCha20Poly1305IETFKeyBytes
}
type AEADXCPMAC struct {
Bytes
}
func (AEADXCPMAC) Size() int {
return cryptoAEADXChaCha20Poly1305IETFABytes
}
//AEADXCPEncrypt encrypts message with AEADXCPKey, and AEADXCPNonce.
//Message then authenticated with additional data 'ad'.
//Authentication tag is append to the encrypted data.
func (b Bytes) AEADXCPEncrypt(ad Bytes, n AEADXCPNonce, k AEADXCPKey) (c Bytes) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
c = make([]byte, bl+cryptoAEADXChaCha20Poly1305IETFABytes)
cp, _ := plen(c)
var outlen C.ulonglong
adp, adl := plen(ad)
if int(C.crypto_aead_xchacha20poly1305_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
}
//AEADXCPDecrypt decrypts message with AEADXCPKey, and AEADXCPNonce.
//The appended authenticated tag is verified with additional data 'ad' before decryption.
//
//It returns an error if decryption failed.
func (b Bytes) AEADXCPDecrypt(ad Bytes, n AEADXCPNonce, k AEADXCPKey) (m Bytes, err error) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
m = make([]byte, bl-cryptoAEADXChaCha20Poly1305IETFABytes)
mp, _ := plen(m)
adp, adl := plen(ad)
var outlen C.ulonglong
if int(C.crypto_aead_xchacha20poly1305_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
}
//AEADXCPEncryptDetached encrypts message with AEADXCPKey, and AEADXCPNonce.
//Message then authenticated with additional data 'ad'.
//Authentication tag is separated saved as 'mac'.
func (b Bytes) AEADXCPEncryptDetached(ad Bytes, n AEADXCPNonce, k AEADXCPKey) (c Bytes, mac AEADXCPMAC) {
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, cryptoAEADXChaCha20Poly1305IETFABytes)
var outlen C.ulonglong
if int(C.crypto_aead_xchacha20poly1305_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 = AEADXCPMAC{macb[:outlen]}
return
}
//AEADXCPDecryptDetached decrypts message with AEADXCPKey, and AEADXCPNonce.
//The separated authenticated tag is verified with additional data 'ad' before decryption.
//
//It returns an error if decryption failed.
func (b Bytes) AEADXCPDecryptDetached(mac AEADXCPMAC, ad Bytes, n AEADXCPNonce, k AEADXCPKey) (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_xchacha20poly1305_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
}
//AEADXCPVerify decrypts message with AEADXCPKey, and AEADXCPNonce 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) AEADXCPVerify(ad Bytes, n AEADXCPNonce, k AEADXCPKey) (err error) {
checkTypedSize(&n, "public nonce")
checkTypedSize(&k, "secret key")
bp, bl := plen(b)
adp, adl := plen(ad)
if int(C.crypto_aead_xchacha20poly1305_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
}
//AEADXCPVerifyDetached decrypts message with AEADXCPKey, and AEADXCPNonce 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) AEADXCPVerifyDetached(mac AEADXCPMAC, ad Bytes, n AEADXCPNonce, k AEADXCPKey) (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_xchacha20poly1305_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
}