-
Notifications
You must be signed in to change notification settings - Fork 342
/
aes.go
85 lines (66 loc) · 1.87 KB
/
aes.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
package aes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"github.com/Shopify/go-lua"
)
func Open(l *lua.State) {
aesOpen := func(l *lua.State) int {
lua.NewLibrary(l, aesLibrary)
return 1
}
lua.Require(l, "crypto/aes", aesOpen, false)
l.Pop(1)
}
var aesLibrary = []lua.RegistryFunction{
{Name: "encryptCBC", Function: encryptCBC},
{Name: "decryptCBC", Function: decryptCBC},
}
func encryptCBC(l *lua.State) int {
key := []byte(lua.CheckString(l, 1))
plaintext := []byte(lua.CheckString(l, 2))
block, err := aes.NewCipher(key)
if err != nil {
lua.Errorf(l, "%s", err.Error())
panic("unreachable")
}
content := PKCS5Padding(plaintext, aes.BlockSize, len(plaintext))
ciphertext := make([]byte, aes.BlockSize+len(content))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
lua.Errorf(l, "%s", err.Error())
panic("unreachable")
}
encrypter := cipher.NewCBCEncrypter(block, iv)
encrypter.CryptBlocks(ciphertext[aes.BlockSize:], content)
l.PushString(string(ciphertext))
return 1
}
func decryptCBC(l *lua.State) int {
key := []byte(lua.CheckString(l, 1))
ciphertext := []byte(lua.CheckString(l, 2))
block, err := aes.NewCipher(key)
if err != nil {
lua.Errorf(l, "%s", err.Error())
panic("unreachable")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
decrypter := cipher.NewCBCDecrypter(block, iv)
decrypter.CryptBlocks(ciphertext, ciphertext)
l.PushString(string(PKCS5UnPadding(ciphertext)))
return 1
}
func PKCS5Padding(ciphertext []byte, blockSize int, after int) []byte {
block := blockSize - len(ciphertext)%blockSize
padding := bytes.Repeat([]byte{byte(block)}, block)
return append(ciphertext, padding...)
}
func PKCS5UnPadding(src []byte) []byte {
srcLength := len(src)
paddingLength := int(src[srcLength-1])
return src[:(srcLength - paddingLength)]
}