import (
"crypto/aes"
"crypto/cipher"
+ "strconv"
"testing"
)
-func benchmarkAESGCMSign(b *testing.B, buf []byte) {
+func benchmarkAESGCMSeal(b *testing.B, buf []byte, keySize int) {
+ b.ReportAllocs()
b.SetBytes(int64(len(buf)))
- var key [16]byte
- var nonce [12]byte
- aes, _ := aes.NewCipher(key[:])
- aesgcm, _ := cipher.NewGCM(aes)
- var out []byte
-
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- out = aesgcm.Seal(out[:0], nonce[:], nil, buf)
- }
-}
-
-func benchmarkAESGCMSeal(b *testing.B, buf []byte) {
- b.SetBytes(int64(len(buf)))
-
- var key [16]byte
+ var key = make([]byte, keySize)
var nonce [12]byte
var ad [13]byte
aes, _ := aes.NewCipher(key[:])
}
}
-func benchmarkAESGCMOpen(b *testing.B, buf []byte) {
+func benchmarkAESGCMOpen(b *testing.B, buf []byte, keySize int) {
+ b.ReportAllocs()
b.SetBytes(int64(len(buf)))
- var key [16]byte
+ var key = make([]byte, keySize)
var nonce [12]byte
var ad [13]byte
aes, _ := aes.NewCipher(key[:])
aesgcm, _ := cipher.NewGCM(aes)
var out []byte
- out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
+
+ ct := aesgcm.Seal(nil, nonce[:], buf[:], ad[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
- _, err := aesgcm.Open(buf[:0], nonce[:], out, ad[:])
- if err != nil {
- b.Errorf("Open: %v", err)
- }
+ out, _ = aesgcm.Open(out[:0], nonce[:], ct, ad[:])
}
}
-func BenchmarkAESGCMSeal1K(b *testing.B) {
- benchmarkAESGCMSeal(b, make([]byte, 1024))
-}
-
-func BenchmarkAESGCMOpen1K(b *testing.B) {
- benchmarkAESGCMOpen(b, make([]byte, 1024))
-}
-
-func BenchmarkAESGCMSign8K(b *testing.B) {
- benchmarkAESGCMSign(b, make([]byte, 8*1024))
-}
-
-func BenchmarkAESGCMSeal8K(b *testing.B) {
- benchmarkAESGCMSeal(b, make([]byte, 8*1024))
-}
-
-func BenchmarkAESGCMOpen8K(b *testing.B) {
- benchmarkAESGCMOpen(b, make([]byte, 8*1024))
+func BenchmarkAESGCM(b *testing.B) {
+ for _, length := range []int{64, 1350, 8 * 1024} {
+ b.Run("Open-128-"+strconv.Itoa(length), func(b *testing.B) {
+ benchmarkAESGCMOpen(b, make([]byte, length), 128/8)
+ })
+ b.Run("Seal-128-"+strconv.Itoa(length), func(b *testing.B) {
+ benchmarkAESGCMSeal(b, make([]byte, length), 128/8)
+ })
+
+ b.Run("Open-256-"+strconv.Itoa(length), func(b *testing.B) {
+ benchmarkAESGCMOpen(b, make([]byte, length), 256/8)
+ })
+ b.Run("Seal-256-"+strconv.Itoa(length), func(b *testing.B) {
+ benchmarkAESGCMSeal(b, make([]byte, length), 256/8)
+ })
+ }
}
func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {