summaryrefslogtreecommitdiff
path: root/internal/cryptocore/openssl_test.go
blob: 94b696ae3464291a02b45a04c184901d19d2d818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cryptocore

// 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"
	"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 := goGCMWrapper(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)
}