mirror of
https://github.com/golang/go
synced 2024-11-23 23:50:08 -07:00
crypto/cipher: 8K benchmarks for AES stream modes
Some parallelizable cipher modes may achieve peak performance for larger block sizes. For this reason the AES-GCM mode already has an 8K benchmark alongside the 1K version. This change introduces 8K benchmarks for additional AES stream cipher modes. Updates #20967 Change-Id: If97c6fbf31222602dcc200f8f418d95908ec1202 Reviewed-on: https://go-review.googlesource.com/136897 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
699da6bd13
commit
4a0dad211c
@ -81,70 +81,49 @@ func BenchmarkAESGCMOpen8K(b *testing.B) {
|
|||||||
benchmarkAESGCMOpen(b, make([]byte, 8*1024))
|
benchmarkAESGCMOpen(b, make([]byte, 8*1024))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {
|
||||||
|
b.SetBytes(int64(len(buf)))
|
||||||
|
|
||||||
|
var key [16]byte
|
||||||
|
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
|
// If we test exactly 1K blocks, we would generate exact multiples of
|
||||||
// the cipher's block size, and the cipher stream fragments would
|
// the cipher's block size, and the cipher stream fragments would
|
||||||
// always be wordsize aligned, whereas non-aligned is a more typical
|
// always be wordsize aligned, whereas non-aligned is a more typical
|
||||||
// use-case.
|
// use-case.
|
||||||
const almost1K = 1024 - 5
|
const almost1K = 1024 - 5
|
||||||
|
const almost8K = 8*1024 - 5
|
||||||
|
|
||||||
func BenchmarkAESCFBEncrypt1K(b *testing.B) {
|
func BenchmarkAESCFBEncrypt1K(b *testing.B) {
|
||||||
buf := make([]byte, almost1K)
|
benchmarkAESStream(b, cipher.NewCFBEncrypter, make([]byte, almost1K))
|
||||||
b.SetBytes(int64(len(buf)))
|
|
||||||
|
|
||||||
var key [16]byte
|
|
||||||
var iv [16]byte
|
|
||||||
aes, _ := aes.NewCipher(key[:])
|
|
||||||
ctr := cipher.NewCFBEncrypter(aes, iv[:])
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ctr.XORKeyStream(buf, buf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAESCFBDecrypt1K(b *testing.B) {
|
func BenchmarkAESCFBDecrypt1K(b *testing.B) {
|
||||||
buf := make([]byte, almost1K)
|
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost1K))
|
||||||
b.SetBytes(int64(len(buf)))
|
}
|
||||||
|
|
||||||
var key [16]byte
|
func BenchmarkAESCFBDecrypt8K(b *testing.B) {
|
||||||
var iv [16]byte
|
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost8K))
|
||||||
aes, _ := aes.NewCipher(key[:])
|
|
||||||
ctr := cipher.NewCFBDecrypter(aes, iv[:])
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ctr.XORKeyStream(buf, buf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAESOFB1K(b *testing.B) {
|
func BenchmarkAESOFB1K(b *testing.B) {
|
||||||
buf := make([]byte, almost1K)
|
benchmarkAESStream(b, cipher.NewOFB, make([]byte, almost1K))
|
||||||
b.SetBytes(int64(len(buf)))
|
|
||||||
|
|
||||||
var key [16]byte
|
|
||||||
var iv [16]byte
|
|
||||||
aes, _ := aes.NewCipher(key[:])
|
|
||||||
ctr := cipher.NewOFB(aes, iv[:])
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ctr.XORKeyStream(buf, buf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAESCTR1K(b *testing.B) {
|
func BenchmarkAESCTR1K(b *testing.B) {
|
||||||
buf := make([]byte, almost1K)
|
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K))
|
||||||
b.SetBytes(int64(len(buf)))
|
}
|
||||||
|
|
||||||
var key [16]byte
|
func BenchmarkAESCTR8K(b *testing.B) {
|
||||||
var iv [16]byte
|
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K))
|
||||||
aes, _ := aes.NewCipher(key[:])
|
|
||||||
ctr := cipher.NewCTR(aes, iv[:])
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ctr.XORKeyStream(buf, buf)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkAESCBCEncrypt1K(b *testing.B) {
|
func BenchmarkAESCBCEncrypt1K(b *testing.B) {
|
||||||
|
Loading…
Reference in New Issue
Block a user