Skip to content

Commit

Permalink
Merge pull request #71 from defia:develop
Browse files Browse the repository at this point in the history
add support and test for chacha20 encrypt
  • Loading branch information
cyfdecyf committed May 10, 2015
2 parents 7aed8e8 + 08ad302 commit bd99354
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ install:
- go get golang.org/x/crypto/blowfish
- go get golang.org/x/crypto/cast5
- go get golang.org/x/crypto/salsa20
- go get github.com/codahale/chacha20
- go install ./cmd/shadowsocks-local
- go install ./cmd/shadowsocks-server
script:
Expand Down
10 changes: 8 additions & 2 deletions shadowsocks/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"crypto/rc4"
"encoding/binary"
"errors"
"github.com/codahale/chacha20"
"golang.org/x/crypto/blowfish"
"golang.org/x/crypto/cast5"
"golang.org/x/crypto/salsa20/salsa"
Expand Down Expand Up @@ -138,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 salsaStreamCipher struct {
nonce [8]byte
key [32]byte
Expand Down Expand Up @@ -185,15 +190,16 @@ type cipherInfo struct {
}

var cipherMethod = map[string]*cipherInfo{
"rc4": {16, 0, nil},
"table": {16, 0, nil},
"aes-128-cfb": {16, 16, newAESStream},
"aes-192-cfb": {24, 16, newAESStream},
"aes-256-cfb": {32, 16, newAESStream},
"des-cfb": {8, 8, newDESStream},
"bf-cfb": {16, 8, newBlowFishStream},
"cast5-cfb": {16, 8, newCast5Stream},
"rc4-md5": {16, 16, newRC4MD5Stream},
"rc4": {16, 0, nil},
"table": {16, 0, nil},
"chacha20": {32, 8, newChaCha20Stream},
"salsa20": {32, 8, newSalsa20Stream},
}

Expand Down
9 changes: 9 additions & 0 deletions shadowsocks/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func TestRC4MD5(t *testing.T) {
testBlockCipher(t, "rc4-md5")
}

func TestChaCha20(t *testing.T) {
testBlockCipher(t, "chacha20")
}

var cipherKey = make([]byte, 64)
var cipherIv = make([]byte, 64)

Expand Down Expand Up @@ -305,3 +309,8 @@ func BenchmarkRC4MD5Decrypt(b *testing.B) {
func BenchmarkSalsa20Decrypt(b *testing.B) {
benchmarkCipherDecrypt(b, "salsa20")
}

func BenchmarkChaCha20Init(b *testing.B) {
ci := cipherMethod["chacha20"]
benchmarkCipherInit(b, ci)
}

0 comments on commit bd99354

Please sign in to comment.