-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchacha_poly.go
182 lines (170 loc) · 7.33 KB
/
chacha_poly.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
/* chacha_poly.go
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package wolfSSL
// #include <wolfssl/options.h>
// #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
// #ifndef HAVE_CHACHA
// #define CHACHA20_POLY1305_AEAD_KEYSIZE 1
// #define CHACHA20_POLY1305_AEAD_IV_SIZE 1
// #define CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE 1
// #define XCHACHA20_POLY1305_AEAD_NONCE_SIZE 1
// int wc_ChaCha20Poly1305_Encrypt(
// byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
// byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
// byte* inAAD, word32 inAADLen,
// byte* inPlaintext, word32 inPlaintextLen,
// byte* outCiphertext,
// byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]) {
// return -174;
// }
// int wc_ChaCha20Poly1305_Decrypt(
// byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
// byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
// byte* inAAD, word32 inAADLen,
// byte* inCiphertext, word32 inCiphertextLen,
// byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
// byte* outPlaintext) {
// return -174;
// }
// #endif
// #ifndef HAVE_XCHACHA
// int wc_XChaCha20Poly1305_Encrypt(
// byte *dst, size_t dst_space,
// const byte *src, size_t src_len,
// const byte *ad, size_t ad_len,
// const byte *nonce, size_t nonce_len,
// const byte *key, size_t key_len) {
// return -174;
// }
// int wc_XChaCha20Poly1305_Decrypt(
// byte *dst, size_t dst_space,
// const byte *src, size_t src_len,
// const byte *ad, size_t ad_len,
// const byte *nonce, size_t nonce_len,
// const byte *key, size_t key_len) {
// return -174;
// }
// #endif
import "C"
import (
"unsafe"
)
const CHACHA20_POLY1305_AEAD_KEYSIZE = int(C.CHACHA20_POLY1305_AEAD_KEYSIZE)
const CHACHA20_POLY1305_AEAD_IV_SIZE = int(C.CHACHA20_POLY1305_AEAD_IV_SIZE)
const CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE = int(C.CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE)
const XCHACHA20_POLY1305_AEAD_NONCE_SIZE = int(C.XCHACHA20_POLY1305_AEAD_NONCE_SIZE)
const CHACHA20_POLY1305_AEAD_NONCE_SIZE = XCHACHA20_POLY1305_AEAD_NONCE_SIZE/2
func Wc_ChaCha20Poly1305_Encrypt(inKey, inIv, inAAD, inPlain, outCipher, outAuthTag []byte) int {
var sanInAAD *C.uchar
if len(inAAD) > 0 {
sanInAAD = (*C.uchar)(unsafe.Pointer(&inAAD[0]))
} else {
sanInAAD = (*C.uchar)(unsafe.Pointer(nil))
}
var sanInPlain *C.uchar
if len(inPlain) > 0 {
sanInPlain = (*C.uchar)(unsafe.Pointer(&inPlain[0]))
} else {
sanInPlain = (*C.uchar)(unsafe.Pointer(nil))
}
var sanOutCipher *C.uchar
if len(outCipher) > 0 {
sanOutCipher = (*C.uchar)(unsafe.Pointer(&outCipher[0]))
} else {
emptyStringArray := []byte("")
sanOutCipher = (*C.uchar)(unsafe.Pointer(&emptyStringArray))
}
return int(C.wc_ChaCha20Poly1305_Encrypt((*C.uchar)(unsafe.Pointer(&inKey[0])), (*C.uchar)(unsafe.Pointer(&inIv[0])),
sanInAAD, C.word32(len(inAAD)), sanInPlain, C.word32(len(inPlain)),
sanOutCipher, (*C.uchar)(unsafe.Pointer(&outAuthTag[0]))))
}
func Wc_ChaCha20Poly1305_Decrypt(inKey, inIv, inAAD, inCipher, inAuthTag , outPlain []byte) int {
var sanInAAD *C.uchar
if len(inAAD) > 0 {
sanInAAD = (*C.uchar)(unsafe.Pointer(&inAAD[0]))
} else {
sanInAAD = (*C.uchar)(unsafe.Pointer(nil))
}
var sanInCipher *C.uchar
if len(inCipher) > 0 {
sanInCipher = (*C.uchar)(unsafe.Pointer(&inCipher[0]))
} else {
emptyStringArray := []byte("")
sanInCipher = (*C.uchar)(unsafe.Pointer(&emptyStringArray))
}
return int(C.wc_ChaCha20Poly1305_Decrypt((*C.uchar)(unsafe.Pointer(&inKey[0])), (*C.uchar)(unsafe.Pointer(&inIv[0])),
sanInAAD, C.word32(len(inAAD)), sanInCipher, C.word32(len(inCipher)),
(*C.uchar)(unsafe.Pointer(&inAuthTag[0])), (*C.uchar)(unsafe.Pointer(&outPlain[0]))))
}
func Wc_ChaCha20Poly1305_Appended_Tag_Encrypt(inKey, inIv, inAAD, inPlain, outCipher []byte) ([]byte, int) {
var outAuthTag [CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]byte
var longOutCipher []byte
if len(outCipher) < (len(inPlain) + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
longOutCipher = make([]byte, len(inPlain) + CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE)
} else {
longOutCipher = outCipher
}
ret := Wc_ChaCha20Poly1305_Encrypt(inKey, inIv, inAAD, inPlain, longOutCipher[:(len(longOutCipher)-CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE)], outAuthTag[:])
copy(longOutCipher[(len(longOutCipher)-CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE):], outAuthTag[:])
return longOutCipher, ret
}
func Wc_ChaCha20Poly1305_Appended_Tag_Decrypt(inKey, inIv, inAAD, inCipher, outPlain []byte) int {
var inAuthTag [CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]byte
copy(inAuthTag[:], inCipher[(len(inCipher)-CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE):])
ret := Wc_ChaCha20Poly1305_Decrypt(inKey, inIv, inAAD, inCipher[:(len(inCipher)-CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE)], inAuthTag[:] , outPlain)
return ret
}
func Wc_XChaCha20Poly1305_Encrypt(outCipher, inPlain, inAAD, inIv, inKey []byte) int {
var sanInAAD *C.uchar
if len(inAAD) > 0 {
sanInAAD = (*C.uchar)(unsafe.Pointer(&inAAD[0]))
} else {
sanInAAD = (*C.uchar)(unsafe.Pointer(nil))
}
var sanInPlain *C.uchar
if len(inPlain) > 0 {
sanInPlain = (*C.uchar)(unsafe.Pointer(&inPlain[0]))
} else {
sanInPlain = (*C.uchar)(unsafe.Pointer(nil))
}
return int(C.wc_XChaCha20Poly1305_Encrypt((*C.uchar)(unsafe.Pointer(&outCipher[0])), C.size_t(len(outCipher)),
sanInPlain, C.size_t(len(inPlain)), sanInAAD, C.size_t(len(inAAD)),
(*C.uchar)(unsafe.Pointer(&inIv[0])), C.size_t(len(inIv)),
(*C.uchar)(unsafe.Pointer(&inKey[0])), C.size_t(len(inKey))))
}
func Wc_XChaCha20Poly1305_Decrypt(outPlain, inCipher, inAAD, inIv, inKey []byte) int {
var sanInAAD *C.uchar
if len(inAAD) > 0 {
sanInAAD = (*C.uchar)(unsafe.Pointer(&inAAD[0]))
} else {
sanInAAD = (*C.uchar)(unsafe.Pointer(nil))
}
var sanInCipher *C.uchar
if len(inCipher) > 0 {
sanInCipher = (*C.uchar)(unsafe.Pointer(&inCipher[0]))
} else {
sanInCipher = (*C.uchar)(unsafe.Pointer(nil))
}
return int(C.wc_XChaCha20Poly1305_Decrypt((*C.uchar)(unsafe.Pointer(&outPlain[0])), C.size_t(len(outPlain)),
sanInCipher, C.size_t(len(inCipher)), sanInAAD, C.size_t(len(inAAD)),
(*C.uchar)(unsafe.Pointer(&inIv[0])), C.size_t(len(inIv)),
(*C.uchar)(unsafe.Pointer(&inKey[0])), C.size_t(len(inKey))))
}