Skip to content

Commit

Permalink
Better chacha20 implementation
Browse files Browse the repository at this point in the history
Replace by lib github.com/aead/chacha20
  • Loading branch information
xqdoo00o authored Aug 15, 2018
1 parent 26eb243 commit 1490033
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions shadowstream/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -54,38 +55,38 @@ 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
}
return ciph
}

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
}
return ciph
}

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
}

0 comments on commit 1490033

Please sign in to comment.