2016-01-27 06:46:40 -05:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2016-10-31 10:24:28 -04:00
|
|
|
"bytes"
|
2016-02-23 12:16:13 -05:00
|
|
|
"crypto/cipher"
|
2016-01-28 06:33:58 -05:00
|
|
|
"crypto/md5"
|
2016-09-25 16:19:49 -04:00
|
|
|
"errors"
|
2016-01-27 06:46:40 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ShadowsocksAccount) Equals(another protocol.Account) bool {
|
|
|
|
if account, ok := another.(*ShadowsocksAccount); ok {
|
|
|
|
return bytes.Equal(this.Key, account.Key)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-25 16:19:49 -04:00
|
|
|
func (this *Account) GetCipher() (Cipher, error) {
|
2016-09-25 16:07:32 -04:00
|
|
|
switch this.CipherType {
|
|
|
|
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
|
2016-09-25 16:07:32 -04:00
|
|
|
case CipherType_CHACHA20_IEFT:
|
2016-09-25 16:19:49 -04:00
|
|
|
return &ChaCha20{IVBytes: 12}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unsupported cipher.")
|
2016-09-17 18:41:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Account) AsAccount() (protocol.Account, error) {
|
2016-10-31 10:24:28 -04:00
|
|
|
cipher, err := this.GetCipher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ShadowsocksAccount{
|
2016-10-31 11:35:18 -04:00
|
|
|
Cipher: cipher,
|
|
|
|
Key: this.GetCipherKey(),
|
2016-11-02 11:17:57 -04:00
|
|
|
OneTimeAuth: this.Ota,
|
2016-10-31 10:24:28 -04:00
|
|
|
}, nil
|
2016-09-17 18:41:21 -04:00
|
|
|
}
|
|
|
|
|
2016-09-25 16:19:49 -04:00
|
|
|
func (this *Account) GetCipherKey() []byte {
|
|
|
|
ct, err := this.GetCipher()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return PasswordToCipherKey(this.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
|
2016-02-23 12:16:13 -05:00
|
|
|
NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error)
|
|
|
|
NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error)
|
2016-01-27 06:46:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type AesCfb struct {
|
|
|
|
KeyBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AesCfb) KeySize() int {
|
|
|
|
return this.KeyBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AesCfb) IVSize() int {
|
|
|
|
return 16
|
|
|
|
}
|
|
|
|
|
2016-02-23 12:16:13 -05:00
|
|
|
func (this *AesCfb) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
2016-02-25 15:50:10 -05:00
|
|
|
stream := crypto.NewAesEncryptionStream(key, iv)
|
2016-02-23 12:16:13 -05:00
|
|
|
return stream, nil
|
2016-01-27 06:46:40 -05:00
|
|
|
}
|
|
|
|
|
2016-02-23 12:16:13 -05:00
|
|
|
func (this *AesCfb) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
2016-02-25 15:50:10 -05:00
|
|
|
stream := crypto.NewAesDecryptionStream(key, iv)
|
2016-02-23 12:16:13 -05:00
|
|
|
return stream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChaCha20 struct {
|
|
|
|
IVBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) KeySize() int {
|
|
|
|
return 32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) IVSize() int {
|
|
|
|
return this.IVBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
|
|
|
return crypto.NewChaCha20Stream(key, iv), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
|
|
|
return crypto.NewChaCha20Stream(key, iv), 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
|
|
|
}
|