From c5be7d0d62b712ce09490e3eea37444f6338c270 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Sun, 17 Jul 2016 23:23:30 +0200 Subject: [PATCH] Replace ChaCha20Poly1305 implementation Improve AEAD speed with slightly faster poly1305 implementation. Avoid memory allocations whenever possible. (AEAD) But currently missing AVX2 support. BenchmarkSeal64B-8 1561 ns/op 40.97 MB/s BenchmarkSeal1K-8 5570 ns/op 183.82 MB/s BenchmarkSeal64K-8 161271 ns/op 406.37 MB/s BenchmarkOpen64B-8 1747 ns/op 45.79 MB/s BenchmarkOpen1K-8 5741 ns/op 181.14 MB/s BenchmarkOpen64K-8 157116 ns/op 417.22 MB/s --- crypto/chacha20poly1305_aead.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crypto/chacha20poly1305_aead.go b/crypto/chacha20poly1305_aead.go index dd2106e7f..7903d2f3a 100644 --- a/crypto/chacha20poly1305_aead.go +++ b/crypto/chacha20poly1305_aead.go @@ -5,7 +5,7 @@ import ( "encoding/binary" "errors" - "github.com/lucas-clemente/chacha20poly1305" + "github.com/aead/chacha20" "github.com/lucas-clemente/quic-go/protocol" ) @@ -22,11 +22,16 @@ func NewAEADChacha20Poly1305(otherKey []byte, myKey []byte, otherIV []byte, myIV if len(myKey) != 32 || len(otherKey) != 32 || len(myIV) != 4 || len(otherIV) != 4 { return nil, errors.New("chacha20poly1305: expected 32-byte keys and 4-byte IVs") } - encrypter, err := chacha20poly1305.New(myKey, 12) + // copy because ChaCha20Poly1305 expects array pointers + var MyKey, OtherKey [32]byte + copy(MyKey[:], myKey) + copy(OtherKey[:], otherKey) + + encrypter, err := chacha20.NewChaCha20Poly1305WithTagSize(&MyKey, 12) if err != nil { return nil, err } - decrypter, err := chacha20poly1305.New(otherKey, 12) + decrypter, err := chacha20.NewChaCha20Poly1305WithTagSize(&OtherKey, 12) if err != nil { return nil, err }