summaryrefslogtreecommitdiff
path: root/cryptfs/openssl_test.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-12-08 13:19:19 +0100
committerJakob Unterwurzacher2015-12-08 13:19:19 +0100
commitc6a6641b58d4bd8fd9e64ad7a653eff9886ee6e2 (patch)
tree41f69679a578323bc0f247b2e4abfbec191a5b85 /cryptfs/openssl_test.go
parent21abf57abbfc6ad818df623f007cac1aa1945682 (diff)
tests: add encryption benchmarks to cryptfs
Diffstat (limited to 'cryptfs/openssl_test.go')
-rw-r--r--cryptfs/openssl_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/cryptfs/openssl_test.go b/cryptfs/openssl_test.go
new file mode 100644
index 0000000..85a97d9
--- /dev/null
+++ b/cryptfs/openssl_test.go
@@ -0,0 +1,76 @@
+package cryptfs
+
+// Benchmark go built-int GCM against spacemonkey openssl bindings
+//
+// Note: The benchmarks in this file supersede the ones in the openssl_benchmark
+// directory as they use the same code paths that gocryptfs actually uses.
+//
+// Run benchmark:
+// go test -bench Enc
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "testing"
+)
+
+func benchmarkGoEnc(b *testing.B, plaintext []byte, key []byte, nonce []byte) (ciphertext []byte) {
+ b.SetBytes(int64(len(plaintext)))
+ aes, err := aes.NewCipher(key[:])
+ if err != nil {
+ b.Fatal(err)
+ }
+ aesgcm, err := cipher.NewGCMWithNonceSize(aes, len(nonce))
+ if err != nil {
+ b.Fatal(err)
+ }
+ // This would be fileID + blockNo
+ aData := make([]byte, 24)
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ // Encrypt plaintext and append to nonce
+ ciphertext = aesgcm.Seal(nonce, nonce, plaintext, aData)
+ }
+ return ciphertext
+}
+
+func benchmarkOpensslEnc(b *testing.B, plaintext []byte, key []byte, nonce []byte) (ciphertext []byte) {
+ b.SetBytes(int64(len(plaintext)))
+ var aesgcm opensslGCM
+ aesgcm.key = key
+ // This would be fileID + blockNo
+ aData := make([]byte, 24)
+ for i := 0; i < b.N; i++ {
+ // Encrypt plaintext and append to nonce
+ ciphertext = aesgcm.Seal(nonce, nonce, plaintext, aData)
+ }
+ return ciphertext
+}
+
+func BenchmarkEnc_Go_4k_AES256_nonce96(b *testing.B) {
+ plaintext := make([]byte, 4048)
+ key := make([]byte, 256/8)
+ nonce := make([]byte, 96/8)
+ benchmarkGoEnc(b, plaintext, key, nonce)
+}
+
+func BenchmarkEnc_Go_4k_AES256_nonce128(b *testing.B) {
+ plaintext := make([]byte, 4048)
+ key := make([]byte, 256/8)
+ nonce := make([]byte, 128/8)
+ benchmarkGoEnc(b, plaintext, key, nonce)
+}
+
+func BenchmarkEnc_OpenSSL_4k_AES256_nonce96(b *testing.B) {
+ plaintext := make([]byte, 4048)
+ key := make([]byte, 256/8)
+ nonce := make([]byte, 96/8)
+ benchmarkOpensslEnc(b, plaintext, key, nonce)
+}
+
+func BenchmarkEnc_OpenSSL_4k_AES256_nonce128(b *testing.B) {
+ plaintext := make([]byte, 4048)
+ key := make([]byte, 256/8)
+ nonce := make([]byte, 96/8)
+ benchmarkOpensslEnc(b, plaintext, key, nonce)
+}