99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package symm
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
var (
|
|
benchPlain4K = bytes.Repeat([]byte("0123456789abcdef"), 256) // 4 KiB
|
|
benchPlain256K = bytes.Repeat([]byte("0123456789abcdef"), 16384) // 256 KiB
|
|
benchAAD = []byte("benchmark-aad")
|
|
benchAESKey = []byte("0123456789abcdef")
|
|
benchSM4Key = []byte("0123456789abcdef")
|
|
benchXTSKey2 = []byte("fedcba9876543210")
|
|
benchNonce12Byte = []byte("123456789012")
|
|
)
|
|
|
|
func BenchmarkAesGCMEncrypt4K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain4K)))
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := EncryptAesGCM(benchPlain4K, benchAESKey, benchNonce12Byte, benchAAD); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkAesCCMEncrypt4K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain4K)))
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := EncryptAesCCM(benchPlain4K, benchAESKey, benchNonce12Byte, benchAAD); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkAesXTSEncrypt4K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain4K)))
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := EncryptAesXTS(benchPlain4K, benchAESKey, benchXTSKey2, 512); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkSM4GCMEncrypt4K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain4K)))
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := EncryptSM4GCM(benchPlain4K, benchSM4Key, benchNonce12Byte, benchAAD); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkSM4CCMEncrypt4K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain4K)))
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := EncryptSM4CCM(benchPlain4K, benchSM4Key, benchNonce12Byte, benchAAD); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkSM4XTSEncrypt4K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain4K)))
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := EncryptSM4XTS(benchPlain4K, benchSM4Key, benchXTSKey2, 512); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkAesXTSStreamEncrypt256K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain256K)))
|
|
for i := 0; i < b.N; i++ {
|
|
var dst bytes.Buffer
|
|
if err := EncryptAesXTSStream(&dst, bytes.NewReader(benchPlain256K), benchAESKey, benchXTSKey2, 512); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkSM4XTSStreamEncrypt256K(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(len(benchPlain256K)))
|
|
for i := 0; i < b.N; i++ {
|
|
var dst bytes.Buffer
|
|
if err := EncryptSM4XTSStream(&dst, bytes.NewReader(benchPlain256K), benchSM4Key, benchXTSKey2, 512); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|