Source file
src/crypto/cipher/benchmark_test.go
1
2
3
4
5 package cipher_test
6
7 import (
8 "crypto/aes"
9 "crypto/cipher"
10 "strconv"
11 "testing"
12 )
13
14 func benchmarkAESGCMSeal(b *testing.B, buf []byte, keySize int) {
15 b.ReportAllocs()
16 b.SetBytes(int64(len(buf)))
17
18 var key = make([]byte, keySize)
19 var nonce [12]byte
20 var ad [13]byte
21 aes, _ := aes.NewCipher(key[:])
22 aesgcm, _ := cipher.NewGCM(aes)
23 var out []byte
24
25 b.ResetTimer()
26 for i := 0; i < b.N; i++ {
27 out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
28 }
29 }
30
31 func benchmarkAESGCMOpen(b *testing.B, buf []byte, keySize int) {
32 b.ReportAllocs()
33 b.SetBytes(int64(len(buf)))
34
35 var key = make([]byte, keySize)
36 var nonce [12]byte
37 var ad [13]byte
38 aes, _ := aes.NewCipher(key[:])
39 aesgcm, _ := cipher.NewGCM(aes)
40 var out []byte
41
42 ct := aesgcm.Seal(nil, nonce[:], buf[:], ad[:])
43
44 b.ResetTimer()
45 for i := 0; i < b.N; i++ {
46 out, _ = aesgcm.Open(out[:0], nonce[:], ct, ad[:])
47 }
48 }
49
50 func BenchmarkAESGCM(b *testing.B) {
51 for _, length := range []int{64, 1350, 8 * 1024} {
52 b.Run("Open-128-"+strconv.Itoa(length), func(b *testing.B) {
53 benchmarkAESGCMOpen(b, make([]byte, length), 128/8)
54 })
55 b.Run("Seal-128-"+strconv.Itoa(length), func(b *testing.B) {
56 benchmarkAESGCMSeal(b, make([]byte, length), 128/8)
57 })
58
59 b.Run("Open-256-"+strconv.Itoa(length), func(b *testing.B) {
60 benchmarkAESGCMOpen(b, make([]byte, length), 256/8)
61 })
62 b.Run("Seal-256-"+strconv.Itoa(length), func(b *testing.B) {
63 benchmarkAESGCMSeal(b, make([]byte, length), 256/8)
64 })
65 }
66 }
67
68 func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) {
69 b.SetBytes(int64(len(buf)))
70
71 var key [16]byte
72 var iv [16]byte
73 aes, _ := aes.NewCipher(key[:])
74 stream := mode(aes, iv[:])
75
76 b.ResetTimer()
77 for i := 0; i < b.N; i++ {
78 stream.XORKeyStream(buf, buf)
79 }
80 }
81
82
83
84
85
86 const almost1K = 1024 - 5
87 const almost8K = 8*1024 - 5
88
89 func BenchmarkAESCTR(b *testing.B) {
90 b.Run("50", func(b *testing.B) {
91 benchmarkAESStream(b, cipher.NewCTR, make([]byte, 50))
92 })
93 b.Run("1K", func(b *testing.B) {
94 benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K))
95 })
96 b.Run("8K", func(b *testing.B) {
97 benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K))
98 })
99 }
100
101 func BenchmarkAESCBCEncrypt1K(b *testing.B) {
102 buf := make([]byte, 1024)
103 b.SetBytes(int64(len(buf)))
104
105 var key [16]byte
106 var iv [16]byte
107 aes, _ := aes.NewCipher(key[:])
108 cbc := cipher.NewCBCEncrypter(aes, iv[:])
109 for i := 0; i < b.N; i++ {
110 cbc.CryptBlocks(buf, buf)
111 }
112 }
113
114 func BenchmarkAESCBCDecrypt1K(b *testing.B) {
115 buf := make([]byte, 1024)
116 b.SetBytes(int64(len(buf)))
117
118 var key [16]byte
119 var iv [16]byte
120 aes, _ := aes.NewCipher(key[:])
121 cbc := cipher.NewCBCDecrypter(aes, iv[:])
122 for i := 0; i < b.N; i++ {
123 cbc.CryptBlocks(buf, buf)
124 }
125 }
126
View as plain text