File size: 3,291 Bytes
4bcc2be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | // Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cipher_test
import (
"crypto/aes"
"crypto/cipher"
"strconv"
"testing"
)
func benchmarkAESGCMSeal(b *testing.B, buf []byte, keySize int) {
b.ReportAllocs()
b.SetBytes(int64(len(buf)))
var key = make([]byte, keySize)
var nonce [12]byte
var ad [13]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[:], buf, ad[:])
}
}
func benchmarkAESGCMOpen(b *testing.B, buf []byte, keySize int) {
b.ReportAllocs()
b.SetBytes(int64(len(buf)))
var key = make([]byte, keySize)
var nonce [12]byte
var ad [13]byte
aes, _ := aes.NewCipher(key[:])
aesgcm, _ := cipher.NewGCM(aes)
var out []byte
ct := aesgcm.Seal(nil, nonce[:], buf[:], ad[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
out, _ = aesgcm.Open(out[:0], nonce[:], ct, ad[:])
}
}
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, keySize int) {
b.SetBytes(int64(len(buf)))
key := make([]byte, keySize)
var iv [16]byte
aes, _ := aes.NewCipher(key)
stream := mode(aes, iv[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
stream.XORKeyStream(buf, buf)
}
}
// If we test exactly 1K blocks, we would generate exact multiples of
// the cipher's block size, and the cipher stream fragments would
// always be wordsize aligned, whereas non-aligned is a more typical
// use-case.
const almost1K = 1024 - 5
const almost8K = 8*1024 - 5
func BenchmarkAESCTR(b *testing.B) {
for _, keyBits := range []int{128, 192, 256} {
keySize := keyBits / 8
b.Run(strconv.Itoa(keyBits), func(b *testing.B) {
b.Run("50", func(b *testing.B) {
benchmarkAESStream(b, cipher.NewCTR, make([]byte, 50), keySize)
})
b.Run("1K", func(b *testing.B) {
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K), keySize)
})
b.Run("8K", func(b *testing.B) {
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K), keySize)
})
})
}
}
func BenchmarkAESCBCEncrypt1K(b *testing.B) {
buf := make([]byte, 1024)
b.SetBytes(int64(len(buf)))
var key [16]byte
var iv [16]byte
aes, _ := aes.NewCipher(key[:])
cbc := cipher.NewCBCEncrypter(aes, iv[:])
for i := 0; i < b.N; i++ {
cbc.CryptBlocks(buf, buf)
}
}
func BenchmarkAESCBCDecrypt1K(b *testing.B) {
buf := make([]byte, 1024)
b.SetBytes(int64(len(buf)))
var key [16]byte
var iv [16]byte
aes, _ := aes.NewCipher(key[:])
cbc := cipher.NewCBCDecrypter(aes, iv[:])
for i := 0; i < b.N; i++ {
cbc.CryptBlocks(buf, buf)
}
}
|