From 149003339a6b23143a6871edab0bb232c54a2ec1 Mon Sep 17 00:00:00 2001 From: David xu Date: Wed, 15 Aug 2018 18:37:34 +0800 Subject: [PATCH] Better chacha20 implementation Replace by lib github.com/aead/chacha20 --- shadowstream/cipher.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/shadowstream/cipher.go b/shadowstream/cipher.go index dea233e1..fa916c9c 100644 --- a/shadowstream/cipher.go +++ b/shadowstream/cipher.go @@ -5,7 +5,8 @@ import ( "crypto/cipher" "strconv" - "github.com/Yawning/chacha20" + "github.com/aead/chacha20" + "github.com/aead/chacha20/chacha" ) // Cipher generates a pair of stream ciphers for encryption and decryption. @@ -54,10 +55,10 @@ func AESCFB(key []byte) (Cipher, error) { // IETF-variant of chacha20 type chacha20ietfkey []byte -func (k chacha20ietfkey) IVSize() int { return chacha20.INonceSize } +func (k chacha20ietfkey) IVSize() int { return chacha.INonceSize } func (k chacha20ietfkey) Decrypter(iv []byte) cipher.Stream { return k.Encrypter(iv) } func (k chacha20ietfkey) Encrypter(iv []byte) cipher.Stream { - ciph, err := chacha20.NewCipher(k, iv) + ciph, err := chacha20.NewCipher(iv, k) if err != nil { panic(err) // should never happen } @@ -65,18 +66,18 @@ func (k chacha20ietfkey) Encrypter(iv []byte) cipher.Stream { } func Chacha20IETF(key []byte) (Cipher, error) { - if len(key) != chacha20.KeySize { - return nil, KeySizeError(chacha20.KeySize) + if len(key) != chacha.KeySize { + return nil, KeySizeError(chacha.KeySize) } return chacha20ietfkey(key), nil } type xchacha20key []byte -func (k xchacha20key) IVSize() int { return chacha20.XNonceSize } +func (k xchacha20key) IVSize() int { return chacha.XNonceSize } func (k xchacha20key) Decrypter(iv []byte) cipher.Stream { return k.Encrypter(iv) } func (k xchacha20key) Encrypter(iv []byte) cipher.Stream { - ciph, err := chacha20.NewCipher(k, iv) + ciph, err := chacha20.NewCipher(iv, k) if err != nil { panic(err) // should never happen } @@ -84,8 +85,8 @@ func (k xchacha20key) Encrypter(iv []byte) cipher.Stream { } func Xchacha20(key []byte) (Cipher, error) { - if len(key) != chacha20.KeySize { - return nil, KeySizeError(chacha20.KeySize) + if len(key) != chacha.KeySize { + return nil, KeySizeError(chacha.KeySize) } return xchacha20key(key), nil }