Skip to content

Commit

Permalink
Merge pull request #106 from xqdoo00o/master
Browse files Browse the repository at this point in the history
Use a better chacha20 package
  • Loading branch information
riobard authored Aug 22, 2018
2 parents 26eb243 + 1490033 commit 18fc4a9
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 18fc4a9

Please sign in to comment.