aboutsummaryrefslogtreecommitdiff
path: root/benchmark/openssl_test.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-09-05 23:19:38 +0200
committerJakob Unterwurzacher2015-09-05 23:19:38 +0200
commitbaa837b7881a87f342c67096013bdf078d11af99 (patch)
tree465c99d8df8f8dac19187a004e801a2989897988 /benchmark/openssl_test.go
parentd0524ded993f870c2dd8a559efed0d612e85d588 (diff)
Add openssl benchmark
Results: BenchmarkAESGCMSeal4K 20000 98638 ns/op 41.53 MB/s BenchmarkAESGCMOpen4K 20000 98781 ns/op 41.47 MB/s BenchmarkOpensslGCMenc4K 50000 27563 ns/op 148.60 MB/s BenchmarkOpensslGCMdec4K 50000 27563 ns/op 148.60 MB/s openssl is 3x faster.
Diffstat (limited to 'benchmark/openssl_test.go')
-rw-r--r--benchmark/openssl_test.go140
1 files changed, 140 insertions, 0 deletions
diff --git a/benchmark/openssl_test.go b/benchmark/openssl_test.go
new file mode 100644
index 0000000..f0b7bc5
--- /dev/null
+++ b/benchmark/openssl_test.go
@@ -0,0 +1,140 @@
+package benchmark
+
+// Benchmark go built-int GCM against spacemonkey openssl bindings
+//
+// Run benchmark:
+// go test -bench=.
+
+import (
+ "bytes"
+ "testing"
+ "github.com/spacemonkeygo/openssl"
+ "crypto/aes"
+ "crypto/cipher"
+)
+
+func BenchmarkAESGCMSeal4K(b *testing.B) {
+ buf := make([]byte, 1024*4)
+ b.SetBytes(int64(len(buf)))
+
+ var key [16]byte
+ var nonce [12]byte
+ aes, _ := aes.NewCipher(key[:])
+ aesgcm, _ := cipher.NewGCM(aes)
+ var out []byte
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ out = aesgcm.Seal(out[:0], nonce[:], buf, nil)
+ }
+}
+
+func BenchmarkAESGCMOpen4K(b *testing.B) {
+ buf := make([]byte, 1024*4)
+ b.SetBytes(int64(len(buf)))
+
+ var key [16]byte
+ var nonce [12]byte
+ aes, _ := aes.NewCipher(key[:])
+ aesgcm, _ := cipher.NewGCM(aes)
+ var out []byte
+ out = aesgcm.Seal(out[:0], nonce[:], buf, nil)
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ _, err := aesgcm.Open(buf[:0], nonce[:], out, nil)
+ if err != nil {
+ b.Errorf("Open: %v", err)
+ }
+ }
+}
+
+func BenchmarkOpensslGCMenc4K(b *testing.B) {
+ buf := make([]byte, 1024*4)
+ b.SetBytes(int64(len(buf)))
+
+ var key [16]byte
+ var nonce [12]byte
+
+ var ciphertext bytes.Buffer
+ var part []byte
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ ciphertext.Reset()
+ ectx, err := openssl.NewGCMEncryptionCipherCtx(128, nil, key[:], nonce[:])
+ if err != nil {
+ b.FailNow()
+ }
+ part, err = ectx.EncryptUpdate(buf)
+ if err != nil {
+ b.FailNow()
+ }
+ ciphertext.Write(part)
+ part, err = ectx.EncryptFinal()
+ if err != nil {
+ b.FailNow()
+ }
+ ciphertext.Write(part)
+ part, err = ectx.GetTag()
+ if err != nil {
+ b.FailNow()
+ }
+ ciphertext.Write(part)
+ }
+}
+
+func BenchmarkOpensslGCMdec4K(b *testing.B) {
+ buf := makeOpensslCiphertext()
+ b.SetBytes(int64(1024*4))
+
+ tag := buf[4096:]
+ buf = buf[0:4096]
+
+ var key [16]byte
+ var nonce [12]byte
+
+ var plaintext bytes.Buffer
+ var part []byte
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ plaintext.Reset()
+ dctx, err := openssl.NewGCMDecryptionCipherCtx(128, nil, key[:], nonce[:])
+ if err != nil {
+ b.FailNow()
+ }
+ part, err = dctx.DecryptUpdate(buf)
+ if err != nil {
+ b.FailNow()
+ }
+ plaintext.Write(part)
+ err = dctx.SetTag(tag)
+ if err != nil {
+ b.FailNow()
+ }
+ part, err = dctx.DecryptFinal()
+ if err != nil {
+ b.FailNow()
+ }
+ plaintext.Write(part)
+ }
+}
+
+func makeOpensslCiphertext() []byte {
+ buf := make([]byte, 1024*4)
+ var key [16]byte
+ var nonce [12]byte
+ var ciphertext bytes.Buffer
+ var part []byte
+
+ ectx, _ := openssl.NewGCMEncryptionCipherCtx(128, nil, key[:], nonce[:])
+ part, _ = ectx.EncryptUpdate(buf)
+ ciphertext.Write(part)
+ part, _ = ectx.EncryptFinal()
+ ciphertext.Write(part)
+ part, _ = ectx.GetTag()
+ ciphertext.Write(part)
+
+ return ciphertext.Bytes()
+}