cipher: add more xts benchmark test

This commit is contained in:
Sun Yimin 2023-08-22 17:39:51 +08:00 committed by GitHub
parent 76635c4cf3
commit 099ebd7d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -384,13 +384,23 @@ func benchmarkSM4CCMOpen(b *testing.B, buf []byte) {
benchmarkGCMOpen(b, sm4gcm, buf)
}
func benchmarkXTS(b *testing.B, cipherFunc func([]byte) (cipher.Block, error), length, keylen int64) {
c, err := smcipher.NewXTSEncrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0)
func benchmarkXTS(b *testing.B, isGB bool, cipherFunc func([]byte) (cipher.Block, error), length, keylen int64) {
plaintext := make([]byte, length)
encrypted := make([]byte, length)
var c cipher.BlockMode
var err error
if !isGB {
c, err = smcipher.NewXTSEncrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0)
if err != nil {
b.Fatalf("NewCipher failed: %s", err)
}
plaintext := make([]byte, length)
encrypted := make([]byte, length)
} else {
c, err = smcipher.NewGBXTSEncrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0)
if err != nil {
b.Fatalf("NewCipher failed: %s", err)
}
}
//decrypted := make([]byte, length)
b.SetBytes(int64(len(plaintext)))
b.ResetTimer()
@ -401,37 +411,121 @@ func benchmarkXTS(b *testing.B, cipherFunc func([]byte) (cipher.Block, error), l
}
func BenchmarkAES128XTSEncrypt512(b *testing.B) {
benchmarkXTS(b, aes.NewCipher, 512, 16)
benchmarkXTS(b, false, aes.NewCipher, 512, 16)
}
func BenchmarkAES128XTSEncrypt1K(b *testing.B) {
benchmarkXTS(b, aes.NewCipher, 1024, 16)
benchmarkXTS(b, false, aes.NewCipher, 1024, 16)
}
func BenchmarkAES128XTSEncrypt4K(b *testing.B) {
benchmarkXTS(b, aes.NewCipher, 4096, 16)
benchmarkXTS(b, false, aes.NewCipher, 4096, 16)
}
func BenchmarkAES256XTSEncrypt512(b *testing.B) {
benchmarkXTS(b, aes.NewCipher, 512, 32)
benchmarkXTS(b, false, aes.NewCipher, 512, 32)
}
func BenchmarkAES256XTSEncrypt1K(b *testing.B) {
benchmarkXTS(b, aes.NewCipher, 1024, 32)
benchmarkXTS(b, false, aes.NewCipher, 1024, 32)
}
func BenchmarkAES256XTSEncrypt4K(b *testing.B) {
benchmarkXTS(b, aes.NewCipher, 4096, 32)
benchmarkXTS(b, false, aes.NewCipher, 4096, 32)
}
func BenchmarkSM4XTSEncrypt512(b *testing.B) {
benchmarkXTS(b, sm4.NewCipher, 512, 16)
benchmarkXTS(b, false, sm4.NewCipher, 512, 16)
}
func BenchmarkSM4XTSEncrypt1K(b *testing.B) {
benchmarkXTS(b, sm4.NewCipher, 1024, 16)
benchmarkXTS(b, false, sm4.NewCipher, 1024, 16)
}
func BenchmarkSM4XTSEncrypt4K(b *testing.B) {
benchmarkXTS(b, sm4.NewCipher, 4096, 16)
benchmarkXTS(b, false, sm4.NewCipher, 4096, 16)
}
func BenchmarkSM4XTSEncrypt512_GB(b *testing.B) {
benchmarkXTS(b, true, sm4.NewCipher, 512, 16)
}
func BenchmarkSM4XTSEncrypt1K_GB(b *testing.B) {
benchmarkXTS(b, true, sm4.NewCipher, 1024, 16)
}
func BenchmarkSM4XTSEncrypt4K_GB(b *testing.B) {
benchmarkXTS(b, true, sm4.NewCipher, 4096, 16)
}
func benchmarkXTS_Decrypt(b *testing.B, isGB bool, cipherFunc func([]byte) (cipher.Block, error), length, keylen int64) {
plaintext := make([]byte, length)
encrypted := make([]byte, length)
var c cipher.BlockMode
var err error
if !isGB {
c, err = smcipher.NewXTSDecrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0)
if err != nil {
b.Fatalf("NewCipher failed: %s", err)
}
} else {
c, err = smcipher.NewGBXTSDecrypterWithSector(cipherFunc, make([]byte, keylen), make([]byte, keylen), 0)
if err != nil {
b.Fatalf("NewCipher failed: %s", err)
}
}
b.SetBytes(int64(len(plaintext)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.CryptBlocks(plaintext, encrypted)
}
}
func BenchmarkAES128XTSDecrypt512(b *testing.B) {
benchmarkXTS_Decrypt(b, false, aes.NewCipher, 512, 16)
}
func BenchmarkAES128XTSDecrypt1K(b *testing.B) {
benchmarkXTS_Decrypt(b, false, aes.NewCipher, 1024, 16)
}
func BenchmarkAES128XTSDecrypt4K(b *testing.B) {
benchmarkXTS_Decrypt(b, false, aes.NewCipher, 4096, 16)
}
func BenchmarkAES256XTSDecrypt512(b *testing.B) {
benchmarkXTS_Decrypt(b, false, aes.NewCipher, 512, 32)
}
func BenchmarkAES256XTSDecrypt1K(b *testing.B) {
benchmarkXTS_Decrypt(b, false, aes.NewCipher, 1024, 32)
}
func BenchmarkAES256XTSDecrypt4K(b *testing.B) {
benchmarkXTS_Decrypt(b, false, aes.NewCipher, 4096, 32)
}
func BenchmarkSM4XTSDecrypt512(b *testing.B) {
benchmarkXTS_Decrypt(b, false, sm4.NewCipher, 512, 16)
}
func BenchmarkSM4XTSDecrypt1K(b *testing.B) {
benchmarkXTS_Decrypt(b, false, sm4.NewCipher, 1024, 16)
}
func BenchmarkSM4XTSDecrypt4K(b *testing.B) {
benchmarkXTS_Decrypt(b, false, sm4.NewCipher, 4096, 16)
}
func BenchmarkSM4XTSDecrypt512_GB(b *testing.B) {
benchmarkXTS_Decrypt(b, true, sm4.NewCipher, 512, 16)
}
func BenchmarkSM4XTSDecrypt1K_GB(b *testing.B) {
benchmarkXTS_Decrypt(b, true, sm4.NewCipher, 1024, 16)
}
func BenchmarkSM4XTSDecrypt4K_GB(b *testing.B) {
benchmarkXTS_Decrypt(b, true, sm4.NewCipher, 4096, 16)
}