@@ -81,70 +81,49 @@ func BenchmarkAESGCMOpen8K(b *testing.B) {
81
81
benchmarkAESGCMOpen (b , make ([]byte , 8 * 1024 ))
82
82
}
83
83
84
- // If we test exactly 1K blocks, we would generate exact multiples of
85
- // the cipher's block size, and the cipher stream fragments would
86
- // always be wordsize aligned, whereas non-aligned is a more typical
87
- // use-case.
88
- const almost1K = 1024 - 5
89
-
90
- func BenchmarkAESCFBEncrypt1K (b * testing.B ) {
91
- buf := make ([]byte , almost1K )
84
+ func benchmarkAESStream (b * testing.B , mode func (cipher.Block , []byte ) cipher.Stream , buf []byte ) {
92
85
b .SetBytes (int64 (len (buf )))
93
86
94
87
var key [16 ]byte
95
88
var iv [16 ]byte
96
89
aes , _ := aes .NewCipher (key [:])
97
- ctr := cipher . NewCFBEncrypter (aes , iv [:])
90
+ stream := mode (aes , iv [:])
98
91
99
92
b .ResetTimer ()
100
93
for i := 0 ; i < b .N ; i ++ {
101
- ctr .XORKeyStream (buf , buf )
94
+ stream .XORKeyStream (buf , buf )
102
95
}
103
96
}
104
97
105
- func BenchmarkAESCFBDecrypt1K (b * testing.B ) {
106
- buf := make ([]byte , almost1K )
107
- b .SetBytes (int64 (len (buf )))
108
-
109
- var key [16 ]byte
110
- var iv [16 ]byte
111
- aes , _ := aes .NewCipher (key [:])
112
- ctr := cipher .NewCFBDecrypter (aes , iv [:])
98
+ // If we test exactly 1K blocks, we would generate exact multiples of
99
+ // the cipher's block size, and the cipher stream fragments would
100
+ // always be wordsize aligned, whereas non-aligned is a more typical
101
+ // use-case.
102
+ const almost1K = 1024 - 5
103
+ const almost8K = 8 * 1024 - 5
113
104
114
- b .ResetTimer ()
115
- for i := 0 ; i < b .N ; i ++ {
116
- ctr .XORKeyStream (buf , buf )
117
- }
105
+ func BenchmarkAESCFBEncrypt1K (b * testing.B ) {
106
+ benchmarkAESStream (b , cipher .NewCFBEncrypter , make ([]byte , almost1K ))
118
107
}
119
108
120
- func BenchmarkAESOFB1K (b * testing.B ) {
121
- buf := make ([]byte , almost1K )
122
- b . SetBytes ( int64 ( len ( buf )))
109
+ func BenchmarkAESCFBDecrypt1K (b * testing.B ) {
110
+ benchmarkAESStream ( b , cipher . NewCFBDecrypter , make ([]byte , almost1K ) )
111
+ }
123
112
124
- var key [16 ]byte
125
- var iv [16 ]byte
126
- aes , _ := aes .NewCipher (key [:])
127
- ctr := cipher .NewOFB (aes , iv [:])
113
+ func BenchmarkAESCFBDecrypt8K (b * testing.B ) {
114
+ benchmarkAESStream (b , cipher .NewCFBDecrypter , make ([]byte , almost8K ))
115
+ }
128
116
129
- b .ResetTimer ()
130
- for i := 0 ; i < b .N ; i ++ {
131
- ctr .XORKeyStream (buf , buf )
132
- }
117
+ func BenchmarkAESOFB1K (b * testing.B ) {
118
+ benchmarkAESStream (b , cipher .NewOFB , make ([]byte , almost1K ))
133
119
}
134
120
135
121
func BenchmarkAESCTR1K (b * testing.B ) {
136
- buf := make ([]byte , almost1K )
137
- b .SetBytes (int64 (len (buf )))
138
-
139
- var key [16 ]byte
140
- var iv [16 ]byte
141
- aes , _ := aes .NewCipher (key [:])
142
- ctr := cipher .NewCTR (aes , iv [:])
122
+ benchmarkAESStream (b , cipher .NewCTR , make ([]byte , almost1K ))
123
+ }
143
124
144
- b .ResetTimer ()
145
- for i := 0 ; i < b .N ; i ++ {
146
- ctr .XORKeyStream (buf , buf )
147
- }
125
+ func BenchmarkAESCTR8K (b * testing.B ) {
126
+ benchmarkAESStream (b , cipher .NewCTR , make ([]byte , almost8K ))
148
127
}
149
128
150
129
func BenchmarkAESCBCEncrypt1K (b * testing.B ) {
0 commit comments