-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.go
290 lines (261 loc) · 8.69 KB
/
cipher.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package encrypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/md5"
"crypto/sha256"
"errors"
"strconv"
"golang.org/x/crypto/tea"
)
// KeyIvLen key and iv length interface
type KeyIvLen interface {
KeyLen() int
IvLen() int
}
// complexCipher cipher information
type complexCipher struct {
keyLen int
ivLen int
newCipher func(key []byte) (cipher.Block, error)
newEncrypt func(block cipher.Block, iv []byte) cipher.Stream
newDecrypt func(block cipher.Block, iv []byte) cipher.Stream
}
// KeyLen return key len
func (sf complexCipher) KeyLen() int { return sf.keyLen }
// IvLen return iv len
func (sf complexCipher) IvLen() int { return sf.ivLen }
// simpleCiphers cipher information
type simpleCipher struct {
keyLen int
ivLen int
newStream func(key, iv []byte) (cipher.Stream, error)
}
// KeyLen return key len
func (sf simpleCipher) KeyLen() int { return sf.keyLen }
// IvLen return iv len
func (sf simpleCipher) IvLen() int { return sf.ivLen }
var complexCiphers = map[string]complexCipher{
"aes-128-cfb": {16, 16, aes.NewCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"aes-192-cfb": {24, 16, aes.NewCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"aes-256-cfb": {32, 16, aes.NewCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"aes-128-ctr": {16, 16, aes.NewCipher, cipher.NewCTR, cipher.NewCTR},
"aes-192-ctr": {24, 16, aes.NewCipher, cipher.NewCTR, cipher.NewCTR},
"aes-256-ctr": {32, 16, aes.NewCipher, cipher.NewCTR, cipher.NewCTR},
"aes-128-ofb": {16, 16, aes.NewCipher, cipher.NewOFB, cipher.NewOFB},
"aes-192-ofb": {24, 16, aes.NewCipher, cipher.NewOFB, cipher.NewOFB},
"aes-256-ofb": {32, 16, aes.NewCipher, cipher.NewOFB, cipher.NewOFB},
"des-cfb": {8, 8, des.NewCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"des-ctr": {8, 8, des.NewCipher, cipher.NewCTR, cipher.NewCTR},
"des-ofb": {8, 8, des.NewCipher, cipher.NewOFB, cipher.NewOFB},
"3des-cfb": {24, 8, des.NewTripleDESCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"3des-ctr": {24, 8, des.NewTripleDESCipher, cipher.NewCTR, cipher.NewCTR},
"3des-ofb": {24, 8, des.NewTripleDESCipher, cipher.NewOFB, cipher.NewOFB},
"blowfish-cfb": {16, 8, NewBlowfishCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"blowfish-ctr": {16, 8, NewBlowfishCipher, cipher.NewCTR, cipher.NewCTR},
"blowfish-ofb": {16, 8, NewBlowfishCipher, cipher.NewOFB, cipher.NewOFB},
"cast5-cfb": {16, 8, NewCast5Cipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"cast5-ctr": {16, 8, NewCast5Cipher, cipher.NewCTR, cipher.NewCTR},
"cast5-ofb": {16, 8, NewCast5Cipher, cipher.NewOFB, cipher.NewOFB},
"twofish-128-cfb": {16, 16, NewTwofishCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"twofish-192-cfb": {24, 16, NewTwofishCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"twofish-256-cfb": {32, 16, NewTwofishCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"twofish-128-ctr": {16, 16, NewTwofishCipher, cipher.NewCTR, cipher.NewCTR},
"twofish-192-ctr": {24, 16, NewTwofishCipher, cipher.NewCTR, cipher.NewCTR},
"twofish-256-ctr": {32, 16, NewTwofishCipher, cipher.NewCTR, cipher.NewCTR},
"twofish-128-ofb": {16, 16, NewTwofishCipher, cipher.NewOFB, cipher.NewOFB},
"twofish-192-ofb": {24, 16, NewTwofishCipher, cipher.NewOFB, cipher.NewOFB},
"twofish-256-ofb": {32, 16, NewTwofishCipher, cipher.NewOFB, cipher.NewOFB},
"xtea-cfb": {16, 8, NewXteaCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"xtea-ctr": {16, 8, NewXteaCipher, cipher.NewCTR, cipher.NewCTR},
"xtea-ofb": {16, 8, NewXteaCipher, cipher.NewOFB, cipher.NewOFB},
"tea-cfb": {16, 8, tea.NewCipher, cipher.NewCFBEncrypter, cipher.NewCFBDecrypter},
"tea-ctr": {16, 8, tea.NewCipher, cipher.NewCTR, cipher.NewCTR},
"tea-ofb": {16, 8, tea.NewCipher, cipher.NewOFB, cipher.NewOFB},
}
var simpleCiphers = map[string]simpleCipher{
"rc4-md5": {16, 16, NewRc4Md5},
"rc4-md5-6": {16, 6, NewRc4Md5},
"chacha20": {32, 12, NewChacha20},
"chacha20-ietf": {32, 24, NewChacha20},
"salsa20": {32, 8, NewSalsa20},
}
// Cipher implement write and read cipher.Stream
type Cipher struct {
Write cipher.Stream
Read cipher.Stream
}
// NewCipher new cipher
// method support:
// aes-128-cfb
// aes-192-cfb
// aes-256-cfb
// aes-128-ctr
// aes-192-ctr
// aes-256-ctr
// aes-128-ofb
// aes-192-ofb
// aes-256-ofb
// des-cfb
// des-ctr
// des-ofb
// 3des-cfb
// 3des-ctr
// 3des-ofb
// blowfish-cfb
// blowfish-ctr
// blowfish-ofb
// cast5-cfb
// cast5-ctr
// cast5-ofb
// twofish-128-cfb
// twofish-192-cfb
// twofish-256-cfb
// twofish-128-ctr
// twofish-192-ctr
// twofish-256-ctr
// twofish-128-ofb
// twofish-192-ofb
// twofish-256-ofb
// tea-cfb
// tea-ctr
// tea-ofb
// xtea-cfb
// xtea-ctr
// xtea-ofb
// rc4-md5
// rc4-md5-6
// chacha20
// chacha20-ietf
// salsa20
func NewCipher(method, password string) (*Cipher, error) {
if password == "" {
return nil, errors.New("password required")
}
if info, ok := complexCiphers[method]; ok {
key := Evp2Key(password, info.keyLen)
// hash(key) -> read IV
riv := sha256.New().Sum(key)[:info.IvLen()]
rd, err := newCipherWithCodec(key, riv, info.newCipher, info.newEncrypt)
if err != nil {
return nil, err
}
// hash(read IV) -> write IV
wiv := sha256.New().Sum(riv)[:info.IvLen()]
wr, err := newCipherWithCodec(key, wiv, info.newCipher, info.newDecrypt)
if err != nil {
return nil, err
}
return &Cipher{wr, rd}, nil
}
if info, ok := simpleCiphers[method]; ok {
key := Evp2Key(password, info.keyLen)
// hash(key) -> read IV
riv := sha256.New().Sum(key)[:info.IvLen()]
wr, err := info.newStream(key[:info.keyLen], riv[:info.ivLen])
if err != nil {
return nil, err
}
// hash(read IV) -> write IV
wiv := sha256.New().Sum(riv)[:info.IvLen()]
rd, err := info.newStream(key[:info.keyLen], wiv[:info.ivLen])
if err != nil {
return nil, err
}
return &Cipher{wr, rd}, nil
}
return nil, errors.New("unsupported encryption method: " + method)
}
// NewStream new stream
func NewStream(method string, key, iv []byte, encrypt bool) (cipher.Stream, error) {
check := func(info KeyIvLen) error {
if len(key) < info.KeyLen() {
return errors.New("invalid key size " + strconv.Itoa(len(key)))
}
if len(iv) < info.IvLen() {
return errors.New("invalid IV length " + strconv.Itoa(len(iv)))
}
return nil
}
if info, ok := complexCiphers[method]; ok {
if err := check(info); err != nil {
return nil, err
}
encdec := info.newDecrypt
if encrypt {
encdec = info.newEncrypt
}
return newCipherWithCodec(key[:info.keyLen], iv[:info.ivLen], info.newCipher, encdec)
}
if info, ok := simpleCiphers[method]; ok {
if err := check(info); err != nil {
return nil, err
}
return info.newStream(key[:info.keyLen], iv[:info.ivLen])
}
return nil, errors.New("unsupported encryption method: " + method)
}
// GetCipher 根据方法获得 Cipher information
func GetCipher(method string) (KeyIvLen, bool) {
if info, ok := complexCiphers[method]; ok {
return info, ok
}
info, ok := simpleCiphers[method]
return info, ok
}
// CipherMethods 获取Cipher的所有支持方法
func CipherMethods() []string {
keys := make([]string, 0, len(complexCiphers)+len(simpleCiphers))
for k := range complexCiphers {
keys = append(keys, k)
}
for k := range simpleCiphers {
keys = append(keys, k)
}
return keys
}
// HasCipherMethod 是否有method方法
func HasCipherMethod(method string) (ok bool) {
if _, ok = complexCiphers[method]; !ok {
_, ok = simpleCiphers[method]
}
return
}
// Valid method password is valid or not
func Valid(method, password string) bool {
_, err := NewCipher(method, password)
return err == nil
}
// Evp2Key evp to key
func Evp2Key(password string, keyLen int) (key []byte) {
const md5Len = 16
cnt := (keyLen-1)/md5Len + 1
m := make([]byte, cnt*md5Len)
copy(m, md5sum([]byte(password)))
// Repeatedly call md5 until bytes generated is enough.
// Each call to md5 uses data: prev md5 sum + password.
d := make([]byte, md5Len+len(password))
for start, i := 0, 1; i < cnt; i++ {
start += md5Len
copy(d, m[start-md5Len:start])
copy(d[md5Len:], password)
copy(m[start:], md5sum(d))
}
return m[:keyLen]
}
func md5sum(b []byte) []byte {
bb := md5.Sum(b)
return bb[:]
}
// New new with newCipher and key,iv
func newCipherWithCodec(key, iv []byte,
newCipher func(key []byte) (cipher.Block, error),
newStream func(block cipher.Block, iv []byte) cipher.Stream,
) (cipher.Stream, error) {
block, err := newCipher(key)
if err != nil {
return nil, err
}
return newStream(block, iv), nil
}