From 45e1575108688eeab34e97a1b313e4974d692eb8 Mon Sep 17 00:00:00 2001 From: defia Date: Tue, 3 Feb 2015 12:22:18 +0800 Subject: [PATCH 1/3] add support and test for chacha20 encrypt --- shadowsocks/encrypt.go | 11 +++++++++-- shadowsocks/encrypt_test.go | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/shadowsocks/encrypt.go b/shadowsocks/encrypt.go index 3dbb673..e9a563b 100644 --- a/shadowsocks/encrypt.go +++ b/shadowsocks/encrypt.go @@ -2,8 +2,6 @@ package shadowsocks import ( "bytes" - "golang.org/x/crypto/blowfish" - "golang.org/x/crypto/cast5" "crypto/aes" "crypto/cipher" "crypto/des" @@ -13,6 +11,10 @@ import ( "encoding/binary" "errors" "io" + + "github.com/codahale/chacha20" + "golang.org/x/crypto/blowfish" + "golang.org/x/crypto/cast5" ) var errEmptyPassword = errors.New("empty key") @@ -137,6 +139,10 @@ func newRC4MD5Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) { return rc4.NewCipher(rc4key) } +func newChaCha20Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) { + return chacha20.New(key, iv) +} + type cipherInfo struct { keyLen int ivLen int @@ -153,6 +159,7 @@ var cipherMethod = map[string]*cipherInfo{ "rc4-md5": {16, 16, newRC4MD5Stream}, "rc4": {16, 0, nil}, "table": {16, 0, nil}, + "chacha20": {32, 8, newChaCha20Stream}, } func CheckCipherMethod(method string) error { diff --git a/shadowsocks/encrypt_test.go b/shadowsocks/encrypt_test.go index e9ff06d..e135f64 100644 --- a/shadowsocks/encrypt_test.go +++ b/shadowsocks/encrypt_test.go @@ -143,6 +143,10 @@ func TestRC4MD5(t *testing.T) { testBlockCipher(t, "rc4-md5") } +func TestChaCha20(t *testing.T) { + testBlockCipher(t, "chacha20") +} + var cipherKey = make([]byte, 64) func init() { @@ -200,3 +204,8 @@ func BenchmarkRC4MD5Init(b *testing.B) { ci := cipherMethod["rc4-md5"] benchmarkCipherInit(b, ci) } + +func BenchmarkChaCha20Init(b *testing.B) { + ci := cipherMethod["chacha20"] + benchmarkCipherInit(b, ci) +} From dcb3b78febd9ee3b54b1a90620c64aad9f119135 Mon Sep 17 00:00:00 2001 From: defia Date: Tue, 3 Feb 2015 18:37:59 +0800 Subject: [PATCH 2/3] fix travis build failed --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c68e325..646aed1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ go: install: - go get golang.org/x/crypto/blowfish - go get golang.org/x/crypto/cast5 + - go get github.com/codahale/chacha20 - go install ./cmd/shadowsocks-local - go install ./cmd/shadowsocks-server script: From 08ad3023b032bc50c1e0566703899c4004837d2d Mon Sep 17 00:00:00 2001 From: defia Date: Wed, 11 Feb 2015 16:42:16 +0800 Subject: [PATCH 3/3] fix PipeThenClose will read once more when eof occurs --- shadowsocks/pipe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shadowsocks/pipe.go b/shadowsocks/pipe.go index 35f356f..9e53bb7 100644 --- a/shadowsocks/pipe.go +++ b/shadowsocks/pipe.go @@ -35,7 +35,7 @@ func PipeThenClose(src, dst net.Conn, timeoutOpt int) { // read may return EOF with n > 0 // should always process n > 0 bytes before handling error if n > 0 { - if _, err = dst.Write(buf[0:n]); err != nil { + if _, err := dst.Write(buf[0:n]); err != nil { Debug.Println("write:", err) break }