2017-02-22 23:55:43 +01:00
|
|
|
// Package speed implements the "-speed" command-line option,
|
|
|
|
// similar to "openssl speed".
|
|
|
|
// It benchmarks the crypto algorithms and libraries used by
|
|
|
|
// gocryptfs.
|
|
|
|
package speed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-29 21:27:05 +01:00
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
|
2021-08-23 22:13:49 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/siv_aead"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/stupidgcm"
|
2017-02-22 23:55:43 +01:00
|
|
|
)
|
|
|
|
|
2020-02-29 21:26:28 +01:00
|
|
|
// 128-bit file ID + 64 bit block number = 192 bits = 24 bytes
|
|
|
|
const adLen = 24
|
|
|
|
|
|
|
|
// gocryptfs uses fixed-size 4 kiB blocks
|
|
|
|
const blockSize = 4096
|
|
|
|
|
2017-04-29 14:50:58 +02:00
|
|
|
// Run - run the speed the test and print the results.
|
2017-02-22 23:55:43 +01:00
|
|
|
func Run() {
|
2021-09-14 18:47:41 +02:00
|
|
|
cpu := cpuModelName()
|
|
|
|
if cpu == "" {
|
|
|
|
cpu = "unknown"
|
|
|
|
}
|
|
|
|
aes := "; no AES acceleration"
|
|
|
|
if stupidgcm.CpuHasAES() {
|
|
|
|
aes = "; with AES acceleration"
|
|
|
|
}
|
|
|
|
fmt.Printf("cpu: %s%s\n", cpu, aes)
|
|
|
|
|
2017-02-22 23:55:43 +01:00
|
|
|
bTable := []struct {
|
|
|
|
name string
|
|
|
|
f func(*testing.B)
|
|
|
|
preferred bool
|
|
|
|
}{
|
2021-09-28 17:45:26 +02:00
|
|
|
{name: cryptocore.BackendOpenSSL.String(), f: bStupidGCM, preferred: stupidgcm.PreferOpenSSLAES256GCM()},
|
|
|
|
{name: cryptocore.BackendGoGCM.String(), f: bGoGCM, preferred: !stupidgcm.PreferOpenSSLAES256GCM()},
|
|
|
|
{name: cryptocore.BackendAESSIV.String(), f: bAESSIV, preferred: false},
|
|
|
|
{name: cryptocore.BackendXChaCha20Poly1305OpenSSL.String(), f: bStupidXchacha, preferred: stupidgcm.PreferOpenSSLXchacha20poly1305()},
|
|
|
|
{name: cryptocore.BackendXChaCha20Poly1305.String(), f: bXchacha20poly1305, preferred: !stupidgcm.PreferOpenSSLXchacha20poly1305()},
|
2017-02-22 23:55:43 +01:00
|
|
|
}
|
|
|
|
for _, b := range bTable {
|
2021-09-02 11:33:06 +02:00
|
|
|
fmt.Printf("%-26s\t", b.name)
|
2017-02-22 23:55:43 +01:00
|
|
|
mbs := mbPerSec(testing.Benchmark(b.f))
|
|
|
|
if mbs > 0 {
|
|
|
|
fmt.Printf("%7.2f MB/s", mbs)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" N/A")
|
|
|
|
}
|
|
|
|
if b.preferred {
|
|
|
|
fmt.Printf("\t(selected in auto mode)\n")
|
|
|
|
} else {
|
2021-09-14 10:15:18 +02:00
|
|
|
fmt.Printf("\n")
|
2017-02-22 23:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mbPerSec(r testing.BenchmarkResult) float64 {
|
|
|
|
if r.Bytes <= 0 || r.T <= 0 || r.N <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get "n" random bytes from /dev/urandom or panic
|
|
|
|
func randBytes(n int) []byte {
|
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("Failed to read random bytes: " + err.Error())
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-09-03 17:40:29 +02:00
|
|
|
// bEncrypt benchmarks the encryption speed of cipher "c"
|
|
|
|
func bEncrypt(b *testing.B, c cipher.AEAD) {
|
2020-02-29 21:26:28 +01:00
|
|
|
authData := randBytes(adLen)
|
2021-09-03 17:40:29 +02:00
|
|
|
iv := randBytes(c.NonceSize())
|
2017-02-22 23:55:43 +01:00
|
|
|
in := make([]byte, blockSize)
|
2021-09-03 17:40:29 +02:00
|
|
|
dst := make([]byte, len(in)+len(iv)+c.Overhead())
|
|
|
|
copy(dst, iv)
|
2017-02-22 23:55:43 +01:00
|
|
|
|
2021-09-03 17:40:29 +02:00
|
|
|
b.SetBytes(int64(len(in)))
|
2017-02-22 23:55:43 +01:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-09-03 17:40:29 +02:00
|
|
|
// Reset dst buffer
|
|
|
|
dst = dst[:len(iv)]
|
2017-02-22 23:55:43 +01:00
|
|
|
// Encrypt and append to nonce
|
2021-09-03 17:40:29 +02:00
|
|
|
c.Seal(dst, iv, in, authData)
|
2017-02-22 23:55:43 +01:00
|
|
|
}
|
2021-09-03 17:40:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:30:42 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 17:40:29 +02:00
|
|
|
// bStupidGCM benchmarks stupidgcm's openssl GCM
|
|
|
|
func bStupidGCM(b *testing.B) {
|
|
|
|
if stupidgcm.BuiltWithoutOpenssl {
|
|
|
|
b.Skip("openssl has been disabled at compile-time")
|
|
|
|
}
|
2021-09-10 12:14:19 +02:00
|
|
|
bEncrypt(b, stupidgcm.NewAES256GCM(randBytes(32)))
|
2017-02-22 23:55:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 21:26:28 +01:00
|
|
|
// bGoGCM benchmarks Go stdlib GCM
|
2017-02-22 23:55:43 +01:00
|
|
|
func bGoGCM(b *testing.B) {
|
2021-09-03 17:40:29 +02:00
|
|
|
gAES, err := aes.NewCipher(randBytes(32))
|
2017-02-22 23:55:43 +01:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2021-09-03 17:40:29 +02:00
|
|
|
bEncrypt(b, gGCM)
|
2017-02-22 23:55:43 +01:00
|
|
|
}
|
|
|
|
|
2022-06-25 23:57:38 +02:00
|
|
|
// bAESSIV benchmarks AES-SIV from github.com/aperturerobotics/jacobsa-crypto/siv
|
2017-02-22 23:55:43 +01:00
|
|
|
func bAESSIV(b *testing.B) {
|
2021-09-03 17:40:29 +02:00
|
|
|
c := siv_aead.New(randBytes(64))
|
|
|
|
bEncrypt(b, c)
|
2017-02-22 23:55:43 +01:00
|
|
|
}
|
2020-02-29 21:27:05 +01:00
|
|
|
|
2021-09-02 11:33:06 +02:00
|
|
|
// bXchacha20poly1305 benchmarks XChaCha20 from golang.org/x/crypto/chacha20poly1305
|
|
|
|
func bXchacha20poly1305(b *testing.B) {
|
2021-09-03 17:40:29 +02:00
|
|
|
c, _ := chacha20poly1305.NewX(randBytes(32))
|
|
|
|
bEncrypt(b, c)
|
2020-02-29 21:27:05 +01:00
|
|
|
}
|
2021-09-02 11:33:06 +02:00
|
|
|
|
|
|
|
// bStupidXchacha benchmarks OpenSSL XChaCha20
|
|
|
|
func bStupidXchacha(b *testing.B) {
|
|
|
|
if stupidgcm.BuiltWithoutOpenssl {
|
|
|
|
b.Skip("openssl has been disabled at compile-time")
|
|
|
|
}
|
2021-09-03 17:40:29 +02:00
|
|
|
bEncrypt(b, stupidgcm.NewXchacha20poly1305(randBytes(32)))
|
2021-09-02 11:33:06 +02:00
|
|
|
}
|