1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-03 10:15:20 -04:00
v2fly/proxy/shadowsocks/config.go

226 lines
5.3 KiB
Go
Raw Normal View History

2016-01-27 06:46:40 -05:00
package shadowsocks
import (
2016-10-31 10:24:28 -04:00
"bytes"
2017-11-25 18:51:54 -05:00
"crypto/aes"
2016-02-23 12:16:13 -05:00
"crypto/cipher"
2016-01-28 06:33:58 -05:00
"crypto/md5"
2017-11-25 18:51:54 -05:00
"crypto/sha1"
"io"
2016-12-16 11:31:13 -05:00
2017-11-25 18:51:54 -05:00
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/protocol"
2016-01-27 06:46:40 -05:00
)
2016-10-31 10:24:28 -04:00
type ShadowsocksAccount struct {
2016-10-31 11:35:18 -04:00
Cipher Cipher
Key []byte
2016-11-02 11:17:57 -04:00
OneTimeAuth Account_OneTimeAuth
2016-10-31 10:24:28 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *ShadowsocksAccount) Equals(another protocol.Account) bool {
2016-10-31 10:24:28 -04:00
if account, ok := another.(*ShadowsocksAccount); ok {
2016-11-27 15:39:09 -05:00
return bytes.Equal(v.Key, account.Key)
2016-10-31 10:24:28 -04:00
}
return false
}
2017-11-25 18:51:54 -05:00
func createAesGcm(key []byte) cipher.AEAD {
block, err := aes.NewCipher(key)
common.Must(err)
gcm, err := cipher.NewGCM(block)
common.Must(err)
return gcm
}
func createChacha20Poly1305(key []byte) cipher.AEAD {
chacha20, err := chacha20poly1305.New(key)
common.Must(err)
return chacha20
}
2016-11-27 15:39:09 -05:00
func (v *Account) GetCipher() (Cipher, error) {
switch v.CipherType {
2016-09-25 16:07:32 -04:00
case CipherType_AES_128_CFB:
2016-09-25 16:19:49 -04:00
return &AesCfb{KeyBytes: 16}, nil
2016-09-25 16:07:32 -04:00
case CipherType_AES_256_CFB:
2016-09-25 16:19:49 -04:00
return &AesCfb{KeyBytes: 32}, nil
2016-09-25 16:07:32 -04:00
case CipherType_CHACHA20:
2016-09-25 16:19:49 -04:00
return &ChaCha20{IVBytes: 8}, nil
2017-01-02 19:37:27 -05:00
case CipherType_CHACHA20_IETF:
2016-09-25 16:19:49 -04:00
return &ChaCha20{IVBytes: 12}, nil
2017-11-25 18:51:54 -05:00
case CipherType_AES_128_GCM:
return &AEADCipher{
KeyBytes: 16,
IVBytes: 16,
AEADAuthCreator: createAesGcm,
}, nil
case CipherType_AES_256_GCM:
return &AEADCipher{
KeyBytes: 32,
IVBytes: 32,
AEADAuthCreator: createAesGcm,
}, nil
case CipherType_CHACHA20_POLY1305:
return &AEADCipher{
KeyBytes: 32,
IVBytes: 32,
AEADAuthCreator: createChacha20Poly1305,
}, nil
2016-09-25 16:19:49 -04:00
default:
2017-04-08 19:43:25 -04:00
return nil, newError("Unsupported cipher.")
2016-09-17 18:41:21 -04:00
}
}
2016-11-27 15:39:09 -05:00
func (v *Account) AsAccount() (protocol.Account, error) {
cipher, err := v.GetCipher()
2016-10-31 10:24:28 -04:00
if err != nil {
2017-04-08 19:43:25 -04:00
return nil, newError("failed to get cipher").Base(err)
2016-10-31 10:24:28 -04:00
}
return &ShadowsocksAccount{
2016-10-31 11:35:18 -04:00
Cipher: cipher,
2016-11-27 15:39:09 -05:00
Key: v.GetCipherKey(),
OneTimeAuth: v.Ota,
2016-10-31 10:24:28 -04:00
}, nil
2016-09-17 18:41:21 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *Account) GetCipherKey() []byte {
ct, err := v.GetCipher()
2016-09-25 16:19:49 -04:00
if err != nil {
return nil
}
2016-11-27 15:39:09 -05:00
return PasswordToCipherKey(v.Password, ct.KeySize())
2016-09-17 18:41:21 -04:00
}
2016-01-27 06:46:40 -05:00
type Cipher interface {
KeySize() int
IVSize() int
2017-11-25 18:51:54 -05:00
NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error)
NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error)
2017-11-25 18:58:57 -05:00
IsAEAD() bool
2016-01-27 06:46:40 -05:00
}
type AesCfb struct {
KeyBytes int
}
2017-11-25 18:58:57 -05:00
func (*AesCfb) IsAEAD() bool {
return false
}
2016-11-27 15:39:09 -05:00
func (v *AesCfb) KeySize() int {
return v.KeyBytes
2016-01-27 06:46:40 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *AesCfb) IVSize() int {
2016-12-16 17:02:00 -05:00
return 16
2016-01-27 06:46:40 -05:00
}
2017-11-25 18:51:54 -05:00
func (v *AesCfb) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
2016-02-25 15:50:10 -05:00
stream := crypto.NewAesEncryptionStream(key, iv)
2017-11-25 18:51:54 -05:00
return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
2016-01-27 06:46:40 -05:00
}
2017-11-25 18:51:54 -05:00
func (v *AesCfb) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
2016-02-25 15:50:10 -05:00
stream := crypto.NewAesDecryptionStream(key, iv)
2017-11-25 18:51:54 -05:00
return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
}
type AEADCipher struct {
KeyBytes int
IVBytes int
AEADAuthCreator func(key []byte) cipher.AEAD
}
2017-11-25 18:58:57 -05:00
func (*AEADCipher) IsAEAD() bool {
return true
}
2017-11-25 18:51:54 -05:00
func (c *AEADCipher) KeySize() int {
return c.KeyBytes
}
func (c *AEADCipher) IVSize() int {
return c.IVBytes
}
func (c *AEADCipher) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
nonce := crypto.NewIncreasingAEADNonceGenerator()
subkey := make([]byte, c.KeyBytes)
hkdfSHA1(key, iv, subkey)
auth := &crypto.AEADAuthenticator{
AEAD: c.AEADAuthCreator(subkey),
NonceGenerator: nonce,
}
return crypto.NewAuthenticationWriter(auth, &crypto.AEADChunkSizeParser{
Auth: auth,
}, writer, protocol.TransferTypeStream), nil
}
func (c *AEADCipher) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
nonce := crypto.NewIncreasingAEADNonceGenerator()
subkey := make([]byte, c.KeyBytes)
hkdfSHA1(key, iv, subkey)
auth := &crypto.AEADAuthenticator{
AEAD: c.AEADAuthCreator(subkey),
NonceGenerator: nonce,
}
return crypto.NewAuthenticationReader(auth, &crypto.AEADChunkSizeParser{
Auth: auth,
}, reader, protocol.TransferTypeStream), nil
2016-02-23 12:16:13 -05:00
}
type ChaCha20 struct {
IVBytes int
}
2017-11-25 18:58:57 -05:00
func (*ChaCha20) IsAEAD() bool {
return false
}
2016-11-27 15:39:09 -05:00
func (v *ChaCha20) KeySize() int {
2016-02-23 12:16:13 -05:00
return 32
}
2016-11-27 15:39:09 -05:00
func (v *ChaCha20) IVSize() int {
return v.IVBytes
2016-02-23 12:16:13 -05:00
}
2017-11-25 18:51:54 -05:00
func (v *ChaCha20) NewEncryptionWriter(key []byte, iv []byte, writer io.Writer) (buf.Writer, error) {
stream := crypto.NewChaCha20Stream(key, iv)
return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
2016-02-23 12:16:13 -05:00
}
2017-11-25 18:51:54 -05:00
func (v *ChaCha20) NewDecryptionReader(key []byte, iv []byte, reader io.Reader) (buf.Reader, error) {
stream := crypto.NewChaCha20Stream(key, iv)
return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil
2016-01-27 06:46:40 -05:00
}
2016-01-28 06:33:58 -05:00
func PasswordToCipherKey(password string, keySize int) []byte {
pwdBytes := []byte(password)
key := make([]byte, 0, keySize)
md5Sum := md5.Sum(pwdBytes)
key = append(key, md5Sum[:]...)
for len(key) < keySize {
md5Hash := md5.New()
md5Hash.Write(md5Sum[:])
md5Hash.Write(pwdBytes)
md5Hash.Sum(md5Sum[:0])
key = append(key, md5Sum[:]...)
}
return key
2016-01-27 06:46:40 -05:00
}
2017-11-25 18:51:54 -05:00
func hkdfSHA1(secret, salt, outkey []byte) {
r := hkdf.New(sha1.New, secret, salt, []byte("ss-subkey"))
common.Must2(io.ReadFull(r, outkey))
}