-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.go
216 lines (167 loc) · 3.94 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
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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
)
var (
aesKeyHash = []byte{0x0E, 0x6B, 0x42, 0x74, 0x7E, 0xDF, 0x51, 0xDC, 0xE7, 0x8E, 0xD0, 0xA0, 0xA8, 0xFB, 0x22, 0xE9, 0x71, 0xC3, 0x16, 0x83}
errNoExecutable = errors.New("aes: launcher.exe does not exist")
errNoEncryptionKeys = errors.New("aes: failed to find encryption keys")
errClearCache = errors.New("aes: clear cache")
)
const (
cacheFileName = "cache.bin"
cacheFileVersion = 1
cacheFileMagic = "REcf"
)
type aesCrypto struct {
Key []byte
Cipher cipher.Block
}
type cacheFile struct {
Version byte
Hash []byte
Key []byte
}
func (rgl *rglInst) initCrypto() error {
executable, err := ioutil.ReadFile(path.Join(rgl.Path, "launcher.exe"))
if err != nil {
return errNoExecutable
}
reader := NewReader(bytes.NewBuffer(executable))
sha1 := sha1.New()
if _, err := io.Copy(sha1, reader); err != nil {
return err
}
// Reset offset after copying data into sha1 interface
reader.SetOffset(0)
// Get 20 bytes hash of sha1 sum
currentHash := sha1.Sum(nil)[:20]
cache, err := rgl.loadCache()
if err != nil || !bytes.Equal(currentHash, cache.Hash) {
cache, err = rgl.createCache(reader, currentHash)
if err != nil {
return errNoEncryptionKeys
}
err := cache.saveCache()
if err != nil {
// We can just continue without saving...
fmt.Printf("Failed to save cache file: %s", err)
}
}
cipher, err := aes.NewCipher(cache.Key)
if err != nil {
return err
}
rgl.Crypto = &aesCrypto{
Key: cache.Key,
Cipher: cipher,
}
return nil
}
func (rgl *rglInst) loadCache() (*cacheFile, error) {
if _, err := os.Stat(cacheFileName); err != nil {
return nil, err
}
content, err := ioutil.ReadFile(cacheFileName)
if err != nil {
return nil, err
}
if len(content) < (4 + 1 + 20 + 32) { // Magic, version, hash, keys
return nil, errClearCache
}
reader := NewReader(bytes.NewBuffer(content))
magic := make([]byte, 4)
if _, err = reader.Read(magic); err != nil {
return nil, err
}
if string(magic) != cacheFileMagic { // 'RGL Extractor cache file'
return nil, errClearCache
}
version, err := reader.ReadByte()
if err != nil {
return nil, err
}
if version == cacheFileVersion { // Current version reader
hash := make([]byte, 20)
if _, err := reader.Read(hash); err != nil {
return nil, err
}
key := make([]byte, 32)
if _, err := reader.Read(key); err != nil {
return nil, err
}
cache := &cacheFile{
Version: version,
Hash: hash,
Key: key,
}
return cache, nil
}
return nil, errClearCache
}
func (rgl *rglInst) createCache(reader *Reader, hash []byte) (*cacheFile, error) {
sha1 := sha1.New()
var offset int64
buffer := make([]byte, 32)
for {
_, err := reader.Read(buffer)
if err != nil {
return nil, err
}
sha1.Reset()
sha1.Write(buffer)
if bytes.Equal(aesKeyHash, sha1.Sum(nil)) {
break
}
offset += 8
reader.SetOffset(offset)
}
cacheFile := &cacheFile{
Version: cacheFileVersion,
Hash: hash,
Key: buffer,
}
return cacheFile, nil
}
func (cf *cacheFile) saveCache() error {
file, err := os.OpenFile(cacheFileName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
if _, err = file.Write([]byte(cacheFileMagic)); err != nil {
return err
}
if _, err = file.Write([]byte{cf.Version}); err != nil {
return err
}
if _, err = file.Write(cf.Hash); err != nil {
return err
}
if _, err = file.Write(cf.Key); err != nil {
return err
}
return nil
}
func (aes *aesCrypto) decrypt(data []byte) []byte {
length := len(data) - len(data)%16
result := make([]byte, len(data))
blockSize := 16
for bs, be := 0, blockSize; bs < length; bs, be = bs+blockSize, be+blockSize {
aes.Cipher.Decrypt(result[bs:be], data[bs:be])
}
rest := data[length:]
if len(rest) > 0 {
result = append(result[:length], rest[:]...)
}
return result
}