/
crypt.go
127 lines (107 loc) · 2.12 KB
/
crypt.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
package wzexplorer
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
"strconv"
)
var (
IvGMS = []byte{0x4D, 0x23, 0xC7, 0x2B}
IvEMS = []byte{0xB9, 0x7D, 0x63, 0xE9}
IvEmpty = []byte{0x00, 0x00, 0x00, 0x00}
)
var (
key = []byte{
0x13, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00,
0xB4, 0x00, 0x00, 0x00,
0x1B, 0x00, 0x00, 0x00,
0x0F, 0x00, 0x00, 0x00,
0x33, 0x00, 0x00, 0x00,
0x52, 0x00, 0x00, 0x00,
}
)
type Crypt struct {
xor []byte
iv []byte
block cipher.Block
}
func newCrypt(iv []byte) (*Crypt, error) {
if len(iv) != 4 {
return nil, errors.New("invalid iv size")
}
c := &Crypt{}
sum := 0
for i := 0; i < len(iv); i++ {
sum += int(iv[i])
}
if sum > 0 {
c.iv = bytes.Repeat(iv, 4)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
c.block = block
}
return c, nil
}
func (c *Crypt) ExpandXorTable(size int) {
if size < len(c.xor) {
return
}
calcSize := size - len(c.xor)
chunk := calcSize >> 4
if calcSize%16 > 0 {
chunk = chunk + 1
}
expandSize := chunk << 4
expand := make([]byte, expandSize, expandSize)
if c.block != nil {
for i := 0; i < expandSize; i += 16 {
c.block.Encrypt(expand[i:], c.iv)
copy(c.iv, expand[i:])
}
}
c.xor = append(c.xor, expand...)
}
func (c *Crypt) Xor() []byte {
return c.xor
}
func (c *Crypt) Transform(data []byte) {
if c.block != nil {
c.ExpandXorTable(len(data))
for i := 0; i < len(data); i++ {
data[i] ^= c.xor[i]
}
}
}
type CryptProvider struct {
version int
hash int
crypt *Crypt
}
func NewCryptProvider(version int, iv []byte) (*CryptProvider, error) {
cp := &CryptProvider{}
crypt, err := newCrypt(iv)
if err != nil {
return nil, err
}
cp.crypt = crypt
asciiVersion := strconv.FormatInt(int64(version), 10)
for i := 0; i < len(asciiVersion); i++ {
cp.hash = (cp.hash << 5) + int(asciiVersion[i]) + 1
}
return cp, nil
}
func (cp *CryptProvider) Verify(target uint16) error {
v := uint16(0xff)
for i := 0; i < 4; i++ {
v ^= uint16((cp.hash >> (i << 3)) & 0xff)
}
if v != target {
return errors.New("invalid version")
}
return nil
}