-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcm.go
242 lines (226 loc) · 6 KB
/
gcm.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
package aes
import (
stdaes "crypto/aes"
"crypto/cipher"
"fmt"
"strconv"
"github.com/colduction/aes/padding"
)
const (
gcmBlockSize int = 16
gcmMinTagSize int = 12
gcmStdNonceSize int = gcmMinTagSize
)
type (
GCMDataSizeError int
GCMStdNonceSizeError int
GCMTagSizeError int
)
func (i GCMStdNonceSizeError) Error() string {
return fmt.Sprintf("aes-gcm: invalid nonce standard size %s, it must equal 12 bytes", strconv.FormatInt(int64(i), 10))
}
func (i GCMDataSizeError) Error() string {
return fmt.Sprintf("aes-gcm: invalid data size %s", strconv.FormatInt(int64(i), 10))
}
func (i GCMTagSizeError) Error() string {
return fmt.Sprintf("aes-gcm: incorrect tag size %s, sizes betweeen 12 and 16 bytes are allowed", strconv.FormatInt(int64(i), 10))
}
func (gcm) ValidStdNonceSize(length int) error {
if length != gcmStdNonceSize {
return GCMStdNonceSizeError(length)
}
return nil
}
func (gcm) ValidTagSize(length int) error {
if length < gcmMinTagSize || length > gcmBlockSize {
return GCMTagSizeError(length)
}
return nil
}
func (gcm) ValidDataSize(length, blocksize int) error {
if uint64(length) > ((1<<32)-2)*uint64(blocksize) {
return GCMDataSizeError(length)
}
return nil
}
// Encrypts input using AES in GCM mode
func (gcm) Encrypt(input, key, nonce, additionalData []byte, pad padding.Padding, dst ...byte) ([]byte, error) {
lenInput := len(input)
if lenInput == 0 {
return nil, InvalidDataError(lenInput)
}
err := GCM.ValidStdNonceSize(len(nonce))
if err != nil {
return nil, err
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = GCM.ValidDataSize(lenInput, block.BlockSize()); err != nil {
return nil, err
}
if pad != nil {
if input, err = pad.Pad(input, block.BlockSize()); err != nil {
return nil, err
}
}
aed, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return aed.Seal(dst, nonce, input, additionalData), nil
}
// Encrypts input using AES in GCM mode with custom nonce size and default tag size (16)
func (gcm) EncryptWithNonceSize(input, key, nonce, additionalData []byte, pad padding.Padding, dst ...byte) ([]byte, error) {
lenInput := len(input)
if lenInput == 0 {
return nil, InvalidDataError(lenInput)
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = GCM.ValidDataSize(lenInput, block.BlockSize()); err != nil {
return nil, err
}
if pad != nil {
if input, err = pad.Pad(input, block.BlockSize()); err != nil {
return nil, err
}
}
aed, err := cipher.NewGCMWithNonceSize(block, len(nonce))
if err != nil {
return nil, err
}
return aed.Seal(dst, nonce, input, additionalData), nil
}
// Encrypts input using AES in GCM mode with custom tag size and default nonce size (12)
func (gcm) EncryptWithTagSize(input, key, nonce, additionalData []byte, tagSize int, pad padding.Padding, dst ...byte) ([]byte, error) {
lenInput := len(input)
if lenInput == 0 {
return nil, InvalidDataError(lenInput)
}
err := GCM.ValidStdNonceSize(len(nonce))
if err != nil {
return nil, err
}
if err = GCM.ValidTagSize(tagSize); err != nil {
return nil, err
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = GCM.ValidDataSize(lenInput, block.BlockSize()); err != nil {
return nil, err
}
if pad != nil {
if input, err = pad.Pad(input, block.BlockSize()); err != nil {
return nil, err
}
}
aed, err := cipher.NewGCMWithTagSize(block, tagSize)
if err != nil {
return nil, err
}
return aed.Seal(dst, nonce, input, additionalData), nil
}
// Decrypts ciphertext using AES in GCM mode
func (gcm) Decrypt(ciphertext, key, nonce, additionalData []byte, pad padding.Padding, dst ...byte) ([]byte, error) {
lenCt := len(ciphertext)
if lenCt == 0 {
return nil, InvalidCiphertextError(lenCt)
}
err := GCM.ValidStdNonceSize(len(nonce))
if err != nil {
return nil, err
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = GCM.ValidDataSize(lenCt, block.BlockSize()); err != nil {
return nil, err
}
mode, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
pt, err := mode.Open(dst, nonce, ciphertext, additionalData)
if err != nil {
return nil, err
}
if pad != nil {
pt, err = pad.Unpad(pt, block.BlockSize())
if err != nil {
return nil, err
}
}
return pt, nil
}
// Decrypts ciphertext using AES in GCM mode with custom nonce size and default tag size (16)
func (gcm) DecryptWithNonceSize(ciphertext, key, nonce, additionalData []byte, pad padding.Padding, dst ...byte) ([]byte, error) {
lenCt := len(ciphertext)
if lenCt == 0 {
return nil, InvalidCiphertextError(lenCt)
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = GCM.ValidDataSize(lenCt, block.BlockSize()); err != nil {
return nil, err
}
mode, err := cipher.NewGCMWithNonceSize(block, len(nonce))
if err != nil {
return nil, err
}
pt, err := mode.Open(dst, nonce, ciphertext, additionalData)
if err != nil {
return nil, err
}
if pad != nil {
pt, err = pad.Unpad(pt, block.BlockSize())
if err != nil {
return nil, err
}
}
return pt, nil
}
// Decrypts ciphertext using AES in GCM mode with custom tag size and default nonce size (12)
func (gcm) DecryptWithTagSize(ciphertext, key, nonce, additionalData []byte, tagSize int, pad padding.Padding, dst ...byte) ([]byte, error) {
lenCt := len(ciphertext)
if lenCt == 0 {
return nil, InvalidCiphertextError(lenCt)
}
err := GCM.ValidStdNonceSize(len(nonce))
if err != nil {
return nil, err
}
if err = GCM.ValidTagSize(tagSize); err != nil {
return nil, err
}
block, err := stdaes.NewCipher(key)
if err != nil {
return nil, err
}
if err = GCM.ValidDataSize(lenCt, block.BlockSize()); err != nil {
return nil, err
}
mode, err := cipher.NewGCMWithTagSize(block, tagSize)
if err != nil {
return nil, err
}
pt, err := mode.Open(dst, nonce, ciphertext, additionalData)
if err != nil {
return nil, err
}
if pad != nil {
pt, err = pad.Unpad(pt, block.BlockSize())
if err != nil {
return nil, err
}
}
return pt, nil
}