2015-09-05 23:19:38 +02:00
|
|
|
package benchmark
|
|
|
|
|
|
|
|
// Benchmark go built-int GCM against spacemonkey openssl bindings
|
|
|
|
//
|
|
|
|
// Run benchmark:
|
|
|
|
// go test -bench=.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2015-10-07 22:58:22 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/rfjakob/gocryptfs/cryptfs"
|
2015-10-04 14:36:20 +02:00
|
|
|
"github.com/spacemonkeygo/openssl"
|
2015-10-07 22:58:22 +02:00
|
|
|
"os"
|
2015-10-04 14:36:20 +02:00
|
|
|
"testing"
|
2015-09-05 23:19:38 +02:00
|
|
|
)
|
|
|
|
|
2015-10-04 21:21:32 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
|
2015-10-07 22:05:32 +02:00
|
|
|
fmt.Printf("Benchmarking AES-GCM-%d with 4kB block size\n", cryptfs.KEY_LEN*8)
|
2015-10-04 21:21:32 +02:00
|
|
|
|
|
|
|
r := m.Run()
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGoEnc4K(b *testing.B) {
|
2015-09-05 23:19:38 +02:00
|
|
|
buf := make([]byte, 1024*4)
|
|
|
|
b.SetBytes(int64(len(buf)))
|
|
|
|
|
2015-10-07 22:05:32 +02:00
|
|
|
var key [cryptfs.KEY_LEN]byte
|
2015-09-05 23:19:38 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:21:32 +02:00
|
|
|
func BenchmarkGoDec4K(b *testing.B) {
|
2015-09-05 23:19:38 +02:00
|
|
|
buf := make([]byte, 1024*4)
|
|
|
|
b.SetBytes(int64(len(buf)))
|
|
|
|
|
2015-10-07 22:05:32 +02:00
|
|
|
var key [cryptfs.KEY_LEN]byte
|
2015-09-05 23:19:38 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:21:32 +02:00
|
|
|
func BenchmarkOpensslEnc4K(b *testing.B) {
|
2015-09-05 23:19:38 +02:00
|
|
|
buf := make([]byte, 1024*4)
|
|
|
|
b.SetBytes(int64(len(buf)))
|
|
|
|
|
2015-10-07 22:05:32 +02:00
|
|
|
var key [cryptfs.KEY_LEN]byte
|
2015-09-05 23:19:38 +02:00
|
|
|
var nonce [12]byte
|
|
|
|
|
|
|
|
var ciphertext bytes.Buffer
|
|
|
|
var part []byte
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ciphertext.Reset()
|
2015-10-07 22:05:32 +02:00
|
|
|
ectx, err := openssl.NewGCMEncryptionCipherCtx(cryptfs.KEY_LEN*8, nil, key[:], nonce[:])
|
2015-09-05 23:19:38 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:21:32 +02:00
|
|
|
func BenchmarkOpensslDec4K(b *testing.B) {
|
2015-09-05 23:19:38 +02:00
|
|
|
buf := makeOpensslCiphertext()
|
2015-10-04 14:36:20 +02:00
|
|
|
b.SetBytes(int64(1024 * 4))
|
2015-09-05 23:19:38 +02:00
|
|
|
|
|
|
|
tag := buf[4096:]
|
|
|
|
buf = buf[0:4096]
|
|
|
|
|
2015-10-07 22:05:32 +02:00
|
|
|
var key [cryptfs.KEY_LEN]byte
|
2015-09-05 23:19:38 +02:00
|
|
|
var nonce [12]byte
|
|
|
|
|
|
|
|
var plaintext bytes.Buffer
|
|
|
|
var part []byte
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
plaintext.Reset()
|
2015-10-07 22:05:32 +02:00
|
|
|
dctx, err := openssl.NewGCMDecryptionCipherCtx(cryptfs.KEY_LEN*8, nil, key[:], nonce[:])
|
2015-09-05 23:19:38 +02:00
|
|
|
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)
|
2015-10-07 22:05:32 +02:00
|
|
|
var key [cryptfs.KEY_LEN]byte
|
2015-09-05 23:19:38 +02:00
|
|
|
var nonce [12]byte
|
|
|
|
var ciphertext bytes.Buffer
|
|
|
|
var part []byte
|
|
|
|
|
2015-10-07 22:05:32 +02:00
|
|
|
ectx, _ := openssl.NewGCMEncryptionCipherCtx(cryptfs.KEY_LEN*8, nil, key[:], nonce[:])
|
2015-09-05 23:19:38 +02:00
|
|
|
part, _ = ectx.EncryptUpdate(buf)
|
|
|
|
ciphertext.Write(part)
|
|
|
|
part, _ = ectx.EncryptFinal()
|
|
|
|
ciphertext.Write(part)
|
|
|
|
part, _ = ectx.GetTag()
|
|
|
|
ciphertext.Write(part)
|
|
|
|
|
|
|
|
return ciphertext.Bytes()
|
|
|
|
}
|