2015-08-28 15:33:40 +00:00
|
|
|
// 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"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
func benchmarkAESGCMSign(b *testing.B, buf []byte) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func benchmarkAESGCMSeal(b *testing.B, buf []byte) {
|
2015-08-28 15:33:40 +00:00
|
|
|
b.SetBytes(int64(len(buf)))
|
|
|
|
|
|
|
|
var key [16]byte
|
|
|
|
var nonce [12]byte
|
2017-04-10 11:32:00 +00:00
|
|
|
var ad [13]byte
|
2015-08-28 15:33:40 +00:00
|
|
|
aes, _ := aes.NewCipher(key[:])
|
|
|
|
aesgcm, _ := cipher.NewGCM(aes)
|
|
|
|
var out []byte
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-04-10 11:32:00 +00:00
|
|
|
out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func benchmarkAESGCMOpen(b *testing.B, buf []byte) {
|
2015-08-28 15:33:40 +00:00
|
|
|
b.SetBytes(int64(len(buf)))
|
|
|
|
|
|
|
|
var key [16]byte
|
|
|
|
var nonce [12]byte
|
2017-04-10 11:32:00 +00:00
|
|
|
var ad [13]byte
|
2015-08-28 15:33:40 +00:00
|
|
|
aes, _ := aes.NewCipher(key[:])
|
|
|
|
aesgcm, _ := cipher.NewGCM(aes)
|
|
|
|
var out []byte
|
2017-04-10 11:32:00 +00:00
|
|
|
out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
|
2015-08-28 15:33:40 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-04-10 11:32:00 +00:00
|
|
|
_, err := aesgcm.Open(buf[:0], nonce[:], out, ad[:])
|
2015-08-28 15:33:40 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Errorf("Open: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func BenchmarkAESGCMSeal1K(b *testing.B) {
|
|
|
|
benchmarkAESGCMSeal(b, make([]byte, 1024))
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAESGCMOpen1K(b *testing.B) {
|
|
|
|
benchmarkAESGCMOpen(b, make([]byte, 1024))
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
func BenchmarkAESGCMSign8K(b *testing.B) {
|
|
|
|
benchmarkAESGCMSign(b, make([]byte, 8*1024))
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func BenchmarkAESGCMSeal8K(b *testing.B) {
|
|
|
|
benchmarkAESGCMSeal(b, make([]byte, 8*1024))
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAESGCMOpen8K(b *testing.B) {
|
|
|
|
benchmarkAESGCMOpen(b, make([]byte, 8*1024))
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {
|
2015-08-28 15:33:40 +00:00
|
|
|
b.SetBytes(int64(len(buf)))
|
|
|
|
|
|
|
|
var key [16]byte
|
|
|
|
var iv [16]byte
|
|
|
|
aes, _ := aes.NewCipher(key[:])
|
2019-06-02 15:48:37 +00:00
|
|
|
stream := mode(aes, iv[:])
|
2015-08-28 15:33:40 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-06-02 15:48:37 +00:00
|
|
|
stream.XORKeyStream(buf, buf)
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
// 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
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func BenchmarkAESCFBEncrypt1K(b *testing.B) {
|
|
|
|
benchmarkAESStream(b, cipher.NewCFBEncrypter, make([]byte, almost1K))
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func BenchmarkAESCFBDecrypt1K(b *testing.B) {
|
|
|
|
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost1K))
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func BenchmarkAESCFBDecrypt8K(b *testing.B) {
|
|
|
|
benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost8K))
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func BenchmarkAESOFB1K(b *testing.B) {
|
|
|
|
benchmarkAESStream(b, cipher.NewOFB, make([]byte, almost1K))
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAESCTR1K(b *testing.B) {
|
2019-06-02 15:48:37 +00:00
|
|
|
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K))
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func BenchmarkAESCTR8K(b *testing.B) {
|
|
|
|
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K))
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|