-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathaes.go
More file actions
163 lines (149 loc) · 4.13 KB
/
aes.go
File metadata and controls
163 lines (149 loc) · 4.13 KB
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
package crypto
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
"strings"
lua "github.com/yuin/gopher-lua"
)
type mode uint
const (
GCM mode = iota + 1
CBC
CTR
)
var modeNames = map[string]mode{
"GCM": GCM,
"CBC": CBC,
"CTR": CTR,
}
func (m mode) String() string {
switch m {
case GCM:
return "GCM"
case CBC:
return "CBC"
case CTR:
return "CTR"
default:
return "unknown"
}
}
func parseString(s string) (mode, error) {
ret, ok := modeNames[strings.ToUpper(s)]
if !ok {
return 0, fmt.Errorf("invalid mode: %s", s)
}
return ret, nil
}
func decodeParams(l *lua.LState) (m mode, key, iv, data []byte, err error) {
modeString := l.ToString(1)
m, err = parseString(modeString)
if err != nil {
return 0, nil, nil, nil, err
}
keyStr := l.ToString(2)
key, err = hex.DecodeString(keyStr)
if err != nil {
return 0, nil, nil, nil, fmt.Errorf("failed to decode key: %v", err)
}
ivStr := l.ToString(3)
iv, err = hex.DecodeString(ivStr)
if err != nil {
return 0, nil, nil, nil, fmt.Errorf("failed to decode IV: %v", err)
}
dataStr := l.ToString(4)
data, err = hex.DecodeString(dataStr)
if err != nil {
return 0, nil, nil, nil, fmt.Errorf("failed to decode data: %v", err)
}
return m, key, iv, data, nil
}
// encryptAES implements AES encryption given mode, key, plaintext, and init value.
// Init value is either initialization vector or nonce, depending on the mode.
func encryptAES(m mode, key, init, plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
switch m {
case GCM:
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(init) != aesGCM.NonceSize() {
return nil, fmt.Errorf("incorrect GCM nonce size: %d, expected: %d", len(init), aesGCM.NonceSize())
}
ciphertext := aesGCM.Seal(nil, init, plaintext, nil)
return ciphertext, nil
case CBC:
if len(init) != block.BlockSize() {
return nil, fmt.Errorf("invalid IV size: %d, expected: %d", len(init), block.BlockSize())
}
padded := pad(plaintext, aes.BlockSize)
mode := cipher.NewCBCEncrypter(block, init)
ciphertext := make([]byte, len(padded))
mode.CryptBlocks(ciphertext, padded)
return ciphertext, nil
case CTR:
if len(init) != block.BlockSize() {
return nil, fmt.Errorf("invalid IV size: %d, expected: %d", len(init), block.BlockSize())
}
stream := cipher.NewCTR(block, init)
ciphertext := make([]byte, len(plaintext))
stream.XORKeyStream(ciphertext, plaintext)
return ciphertext, nil
default:
return nil, fmt.Errorf("unsupported mode: %d", m)
}
}
// decryptAES implements AES decryption given mode, key, ciphertext, and init value.
// Init value is either initialization vector or nonce, depending on the mode.
func decryptAES(m mode, key, init, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
switch m {
case GCM:
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
l := len(init)
if l != aesGCM.NonceSize() {
return nil, fmt.Errorf("incorrect GCM nonce size: %d, expected: %d", len(init), aesGCM.NonceSize())
}
plaintext, err := aesGCM.Open(nil, init, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
case CBC:
if len(ciphertext)%aes.BlockSize != 0 {
return nil, fmt.Errorf("ciphertext is not a multiple of block size")
}
mode := cipher.NewCBCDecrypter(block, init)
plaintext := make([]byte, len(ciphertext))
mode.CryptBlocks(plaintext, ciphertext)
// Padding reversal is intentionally delegated to the application layer.
// On constrained devices with fixed-length payloads, padding is sometimes omitted
// to avoid unnecessary processing load and data overhead.
return plaintext, nil
case CTR:
stream := cipher.NewCTR(block, init)
plaintext := make([]byte, len(ciphertext))
stream.XORKeyStream(plaintext, ciphertext)
return plaintext, nil
default:
return nil, fmt.Errorf("unsupported mode: %s", m)
}
}
func pad(data []byte, blockSize int) []byte {
padLen := blockSize - len(data)%blockSize
padding := bytes.Repeat([]byte{byte(padLen)}, padLen)
return append(data, padding...)
}