forked from Qv2ray/mmp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.go
148 lines (132 loc) · 3.71 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
package cipher
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/sha1"
"github.com/Qv2ray/mmp-go/infra/pool"
"github.com/qv2ray/smaead"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"
"io"
)
type CipherConf struct {
KeyLen int
SaltLen int
NonceLen int
TagLen int
NewCipher func(key []byte) (cipher.AEAD, error)
NewPartialCipher func(key []byte) (smaead.PartialAEAD, error)
}
const (
MaxNonceSize = 12
ATypeIPv4 = 1
ATypeDomain = 3
ATypeIpv6 = 4
)
var (
CiphersConf = map[string]CipherConf{
"chacha20-ietf-poly1305": {KeyLen: 32, SaltLen: 32, NonceLen: 12, TagLen: 16, NewCipher: chacha20poly1305.New, NewPartialCipher: NewPC20P1305},
"chacha20-poly1305": {KeyLen: 32, SaltLen: 32, NonceLen: 12, TagLen: 16, NewCipher: chacha20poly1305.New, NewPartialCipher: NewPC20P1305},
"aes-256-gcm": {KeyLen: 32, SaltLen: 32, NonceLen: 12, TagLen: 16, NewCipher: NewGcm, NewPartialCipher: NewPGcm},
"aes-128-gcm": {KeyLen: 16, SaltLen: 16, NonceLen: 12, TagLen: 16, NewCipher: NewGcm, NewPartialCipher: NewPGcm},
}
ZeroNonce [MaxNonceSize]byte
ReusedInfo = []byte("ss-subkey")
)
func (conf *CipherConf) Verify(buf []byte, masterKey []byte, salt []byte, cipherText []byte, subKey *[]byte) ([]byte, bool) {
var sk []byte
if subKey != nil && len(*subKey) == conf.KeyLen {
sk = *subKey
} else {
sk = pool.Get(conf.KeyLen)
defer pool.Put(sk)
kdf := hkdf.New(
sha1.New,
masterKey,
salt,
ReusedInfo,
)
io.ReadFull(kdf, sk)
if subKey != nil && cap(*subKey) >= conf.KeyLen {
*subKey = (*subKey)[:conf.KeyLen]
copy(*subKey, sk)
}
}
ciph, _ := conf.NewCipher(sk)
if _, err := ciph.Open(buf[:0], ZeroNonce[:conf.NonceLen], cipherText, nil); err != nil {
return nil, false
}
return buf[:len(cipherText)-ciph.Overhead()], true
}
// Warning:
// UnsafeVerifyATyp brings less than 25% performance improvement in most cases.
// It is dangerous and NOT recommended.
// Do not use it if you feel unnecessary.
func (conf *CipherConf) UnsafeVerifyATyp(buf []byte, masterKey []byte, salt []byte, cipherText []byte, subKey *[]byte) bool {
var sk []byte
if subKey != nil && len(*subKey) == conf.KeyLen {
sk = *subKey
} else {
sk = pool.Get(conf.KeyLen)
defer pool.Put(sk)
kdf := hkdf.New(
sha1.New,
masterKey,
salt,
ReusedInfo,
)
io.ReadFull(kdf, sk)
if subKey != nil && cap(*subKey) >= conf.KeyLen {
*subKey = (*subKey)[:conf.KeyLen]
copy(*subKey, sk)
}
}
ciph, _ := conf.NewPartialCipher(sk)
plain := ciph.OpenWithoutCheck(buf[:0], ZeroNonce[:conf.NonceLen], cipherText[:1])
atyp := plain[0]
switch atyp {
case ATypeIPv4, ATypeDomain, ATypeIpv6:
return true
}
return false
}
func MD5Sum(d []byte) []byte {
h := md5.New()
h.Write(d)
return h.Sum(nil)
}
func EVPBytesToKey(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))
start := 0
for i := 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 NewGcm(key []byte) (cipher.AEAD, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewGCM(block)
}
func NewPC20P1305(key []byte) (smaead.PartialAEAD, error) {
return smaead.NewPartialChacha20Poly1305(key)
}
func NewPGcm(key []byte) (smaead.PartialAEAD, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return smaead.NewPartialGCM(block)
}