Skip to content

Commit

Permalink
refine chacha20 initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jul 23, 2016
1 parent 128e866 commit e304e27
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions common/crypto/chacha20.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@ import (
"github.com/aead/chacha20"
)

func makeNonce(nonce *[chacha20.NonceSize]byte, iv []byte) {
// NewChaCha20Stream creates a new Chacha/20 cipher stream. Caller must ensure that key is 32-bytes long and iv is either 8 or 12 bytes.
func NewChaCha20Stream(key []byte, iv []byte) cipher.Stream {
var keyArray [32]byte
var nonce [12]byte
copy(keyArray[:], key)
switch len(iv) {
case 8:
copy(nonce[4:], iv)
case 12:
copy(nonce[:], iv)
default:
panic("bad nonce length")
}
}

func NewChaCha20Stream(key []byte, iv []byte) cipher.Stream {
var Key [32]byte
var Nonce [12]byte
if len(key) != 32 {
panic("bad key length")
}
copy(Key[:], key)
makeNonce(&Nonce, iv)
return chacha20.NewCipher(&Nonce, &Key)
return chacha20.NewCipher(&nonce, &keyArray)
}

0 comments on commit e304e27

Please sign in to comment.