| 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. |
| 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, keySize int) { |
| 69 |
b.SetBytes(int64(len(buf))) |
| 70 |
|
| 71 |
key := make([]byte, keySize) |
| 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 |
// If we test exactly 1K blocks, we would generate exact multiples of |
| 83 |
// the cipher's block size, and the cipher stream fragments would |
| 84 |
// always be wordsize aligned, whereas non-aligned is a more typical |
| 85 |
// use-case. |
| 86 |
const almost1K = 1024 - 5 |
| 87 |
const almost8K = 8*1024 - 5 |
| 88 |
|
| 89 |
func BenchmarkAESCTR(b *testing.B) { |
| 90 |
for _, keyBits := range []int{128, 192, 256} { |
| 91 |
keySize := keyBits / 8 |
| 92 |
b.Run(strconv.Itoa(keyBits), func(b *testing.B) { |
| 93 |
b.Run("50", func(b *testing.B) { |
| 94 |
benchmarkAESStream(b, cipher.NewCTR, make([]byte, 50), keySize) |
| 95 |
}) |
| 96 |
b.Run("1K", func(b *testing.B) { |
| 97 |
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K), keySize) |
| 98 |
}) |
| 99 |
b.Run("8K", func(b *testing.B) { |
| 100 |
benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K), keySize) |
| 101 |
}) |
| 102 |
}) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func BenchmarkAESCBCEncrypt1K(b *testing.B) { |
| 107 |
buf := make([]byte, 1024) |
| 108 |
b.SetBytes(int64(len(buf))) |
| 109 |
|
| 110 |
var key [16]byte |
| 111 |
var iv [16]byte |
| 112 |
aes, _ := aes.NewCipher(key[:]) |
| 113 |
cbc := cipher.NewCBCEncrypter(aes, iv[:]) |
| 114 |
for i := 0; i < b.N; i++ { |
| 115 |
cbc.CryptBlocks(buf, buf) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func BenchmarkAESCBCDecrypt1K(b *testing.B) { |
| 120 |
buf := make([]byte, 1024) |
| 121 |
b.SetBytes(int64(len(buf))) |
| 122 |
|
| 123 |
var key [16]byte |
| 124 |
var iv [16]byte |
| 125 |
aes, _ := aes.NewCipher(key[:]) |
| 126 |
cbc := cipher.NewCBCDecrypter(aes, iv[:]) |
| 127 |
for i := 0; i < b.N; i++ { |
| 128 |
cbc.CryptBlocks(buf, buf) |
| 129 |
} |
| 130 |
} |