aboutsummaryrefslogtreecommitdiff
path: root/benchmark/openssl_test.go
blob: f0b7bc542bad979e965a463b4b4b37ca819e448f (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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()
}