speed: add decryption benchmarks

gocryptfs/internal/speed$ go test -bench .
goos: linux
goarch: amd64
pkg: github.com/rfjakob/gocryptfs/v2/internal/speed
cpu: Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
BenchmarkStupidGCM-4              	  263742	      4523 ns/op	 905.61 MB/s
BenchmarkStupidGCMDecrypt-4       	  204858	      5779 ns/op	 708.76 MB/s
BenchmarkGoGCM-4                  	  291259	      4095 ns/op	1000.25 MB/s
BenchmarkGoGCMDecrypt-4           	  293886	      4061 ns/op	1008.53 MB/s
BenchmarkAESSIV-4                 	   46537	     25538 ns/op	 160.39 MB/s
BenchmarkAESSIVDecrypt-4          	   46770	     25627 ns/op	 159.83 MB/s
BenchmarkXchacha-4                	  243619	      4893 ns/op	 837.03 MB/s
BenchmarkXchachaDecrypt-4         	  248857	      4793 ns/op	 854.51 MB/s
BenchmarkStupidXchacha-4          	  213717	      5558 ns/op	 736.99 MB/s
BenchmarkStupidXchachaDecrypt-4   	  176635	      6782 ns/op	 603.96 MB/s
PASS
ok  	github.com/rfjakob/gocryptfs/v2/internal/speed	12.871s
This commit is contained in:
Jakob Unterwurzacher 2021-09-03 18:30:42 +02:00
parent 5046962634
commit 3e27acb989
2 changed files with 62 additions and 4 deletions

View File

@ -92,6 +92,25 @@ func bEncrypt(b *testing.B, c cipher.AEAD) {
} }
func bDecrypt(b *testing.B, c cipher.AEAD) {
authData := randBytes(adLen)
iv := randBytes(c.NonceSize())
plain := randBytes(blockSize)
ciphertext := c.Seal(iv, iv, plain, authData)
b.SetBytes(int64(len(plain)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Reset plain buffer
plain = plain[:0]
// Decrypt
_, err := c.Open(plain, iv, ciphertext[c.NonceSize():], authData)
if err != nil {
b.Fatal(err)
}
}
}
// bStupidGCM benchmarks stupidgcm's openssl GCM // bStupidGCM benchmarks stupidgcm's openssl GCM
func bStupidGCM(b *testing.B) { func bStupidGCM(b *testing.B) {
if stupidgcm.BuiltWithoutOpenssl { if stupidgcm.BuiltWithoutOpenssl {

View File

@ -1,5 +1,16 @@
package speed package speed
import (
"crypto/aes"
"crypto/cipher"
"testing"
"golang.org/x/crypto/chacha20poly1305"
"github.com/rfjakob/gocryptfs/v2/internal/siv_aead"
"github.com/rfjakob/gocryptfs/v2/internal/stupidgcm"
)
/* /*
Make the "-speed" benchmarks also accessible to the standard test system. Make the "-speed" benchmarks also accessible to the standard test system.
Example run: Example run:
@ -12,26 +23,54 @@ PASS
ok github.com/rfjakob/gocryptfs/v2/internal/speed 6.022s ok github.com/rfjakob/gocryptfs/v2/internal/speed 6.022s
*/ */
import (
"testing"
)
func BenchmarkStupidGCM(b *testing.B) { func BenchmarkStupidGCM(b *testing.B) {
bStupidGCM(b) bStupidGCM(b)
} }
func BenchmarkStupidGCMDecrypt(b *testing.B) {
if stupidgcm.BuiltWithoutOpenssl {
b.Skip("openssl has been disabled at compile-time")
}
bDecrypt(b, stupidgcm.New(randBytes(32), false))
}
func BenchmarkGoGCM(b *testing.B) { func BenchmarkGoGCM(b *testing.B) {
bGoGCM(b) bGoGCM(b)
} }
func BenchmarkGoGCMDecrypt(b *testing.B) {
gAES, err := aes.NewCipher(randBytes(32))
if err != nil {
b.Fatal(err)
}
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
if err != nil {
b.Fatal(err)
}
bDecrypt(b, gGCM)
}
func BenchmarkAESSIV(b *testing.B) { func BenchmarkAESSIV(b *testing.B) {
bAESSIV(b) bAESSIV(b)
} }
func BenchmarkAESSIVDecrypt(b *testing.B) {
bEncrypt(b, siv_aead.New(randBytes(64)))
}
func BenchmarkXchacha(b *testing.B) { func BenchmarkXchacha(b *testing.B) {
bXchacha20poly1305(b) bXchacha20poly1305(b)
} }
func BenchmarkXchachaDecrypt(b *testing.B) {
c, _ := chacha20poly1305.NewX(randBytes(32))
bDecrypt(b, c)
}
func BenchmarkStupidXchacha(b *testing.B) { func BenchmarkStupidXchacha(b *testing.B) {
bStupidXchacha(b) bStupidXchacha(b)
} }
func BenchmarkStupidXchachaDecrypt(b *testing.B) {
bDecrypt(b, stupidgcm.NewXchacha20poly1305(randBytes(32)))
}