1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func BenchmarkAESGCMSeal1K(b *testing.B) {
14 buf := make([]byte, 1024)
15 b.SetBytes(int64(len(buf)))
19 aes, _ := aes.NewCipher(key[:])
20 aesgcm, _ := cipher.NewGCM(aes)
24 for i := 0; i < b.N; i++ {
25 out = aesgcm.Seal(out[:0], nonce[:], buf, nonce[:])
29 func BenchmarkAESGCMOpen1K(b *testing.B) {
30 buf := make([]byte, 1024)
31 b.SetBytes(int64(len(buf)))
35 aes, _ := aes.NewCipher(key[:])
36 aesgcm, _ := cipher.NewGCM(aes)
38 out = aesgcm.Seal(out[:0], nonce[:], buf, nonce[:])
41 for i := 0; i < b.N; i++ {
42 _, err := aesgcm.Open(buf[:0], nonce[:], out, nonce[:])
44 b.Errorf("Open: %v", err)
49 // If we test exactly 1K blocks, we would generate exact multiples of
50 // the cipher's block size, and and the cipher stream fragments would
51 // always be wordsize aligned, whereas non-aligned is a more typical
53 const almost1K = 1024 - 5
55 func BenchmarkAESCFBEncrypt1K(b *testing.B) {
56 buf := make([]byte, almost1K)
57 b.SetBytes(int64(len(buf)))
61 aes, _ := aes.NewCipher(key[:])
62 ctr := cipher.NewCFBEncrypter(aes, iv[:])
65 for i := 0; i < b.N; i++ {
66 ctr.XORKeyStream(buf, buf)
70 func BenchmarkAESCFBDecrypt1K(b *testing.B) {
71 buf := make([]byte, almost1K)
72 b.SetBytes(int64(len(buf)))
76 aes, _ := aes.NewCipher(key[:])
77 ctr := cipher.NewCFBDecrypter(aes, iv[:])
80 for i := 0; i < b.N; i++ {
81 ctr.XORKeyStream(buf, buf)
85 func BenchmarkAESOFB1K(b *testing.B) {
86 buf := make([]byte, almost1K)
87 b.SetBytes(int64(len(buf)))
91 aes, _ := aes.NewCipher(key[:])
92 ctr := cipher.NewOFB(aes, iv[:])
95 for i := 0; i < b.N; i++ {
96 ctr.XORKeyStream(buf, buf)
100 func BenchmarkAESCTR1K(b *testing.B) {
101 buf := make([]byte, almost1K)
102 b.SetBytes(int64(len(buf)))
106 aes, _ := aes.NewCipher(key[:])
107 ctr := cipher.NewCTR(aes, iv[:])
110 for i := 0; i < b.N; i++ {
111 ctr.XORKeyStream(buf, buf)
115 func BenchmarkAESCBCEncrypt1K(b *testing.B) {
116 buf := make([]byte, 1024)
117 b.SetBytes(int64(len(buf)))
121 aes, _ := aes.NewCipher(key[:])
122 cbc := cipher.NewCBCEncrypter(aes, iv[:])
123 for i := 0; i < b.N; i++ {
124 cbc.CryptBlocks(buf, buf)
128 func BenchmarkAESCBCDecrypt1K(b *testing.B) {
129 buf := make([]byte, 1024)
130 b.SetBytes(int64(len(buf)))
134 aes, _ := aes.NewCipher(key[:])
135 cbc := cipher.NewCBCDecrypter(aes, iv[:])
136 for i := 0; i < b.N; i++ {
137 cbc.CryptBlocks(buf, buf)