github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/crypto/cipher/benchmark_test.go (about) 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 "testing" 11 ) 12 13 func benchmarkAESGCMSign(b *testing.B, buf []byte) { 14 b.SetBytes(int64(len(buf))) 15 16 var key [16]byte 17 var nonce [12]byte 18 aes, _ := aes.NewCipher(key[:]) 19 aesgcm, _ := cipher.NewGCM(aes) 20 var out []byte 21 22 b.ResetTimer() 23 for i := 0; i < b.N; i++ { 24 out = aesgcm.Seal(out[:0], nonce[:], nil, buf) 25 } 26 } 27 28 func benchmarkAESGCMSeal(b *testing.B, buf []byte) { 29 b.SetBytes(int64(len(buf))) 30 31 var key [16]byte 32 var nonce [12]byte 33 var ad [13]byte 34 aes, _ := aes.NewCipher(key[:]) 35 aesgcm, _ := cipher.NewGCM(aes) 36 var out []byte 37 38 b.ResetTimer() 39 for i := 0; i < b.N; i++ { 40 out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:]) 41 } 42 } 43 44 func benchmarkAESGCMOpen(b *testing.B, buf []byte) { 45 b.SetBytes(int64(len(buf))) 46 47 var key [16]byte 48 var nonce [12]byte 49 var ad [13]byte 50 aes, _ := aes.NewCipher(key[:]) 51 aesgcm, _ := cipher.NewGCM(aes) 52 var out []byte 53 out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:]) 54 55 b.ResetTimer() 56 for i := 0; i < b.N; i++ { 57 _, err := aesgcm.Open(buf[:0], nonce[:], out, ad[:]) 58 if err != nil { 59 b.Errorf("Open: %v", err) 60 } 61 } 62 } 63 64 func BenchmarkAESGCMSeal1K(b *testing.B) { 65 benchmarkAESGCMSeal(b, make([]byte, 1024)) 66 } 67 68 func BenchmarkAESGCMOpen1K(b *testing.B) { 69 benchmarkAESGCMOpen(b, make([]byte, 1024)) 70 } 71 72 func BenchmarkAESGCMSign8K(b *testing.B) { 73 benchmarkAESGCMSign(b, make([]byte, 8*1024)) 74 } 75 76 func BenchmarkAESGCMSeal8K(b *testing.B) { 77 benchmarkAESGCMSeal(b, make([]byte, 8*1024)) 78 } 79 80 func BenchmarkAESGCMOpen8K(b *testing.B) { 81 benchmarkAESGCMOpen(b, make([]byte, 8*1024)) 82 } 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) 92 b.SetBytes(int64(len(buf))) 93 94 var key [16]byte 95 var iv [16]byte 96 aes, _ := aes.NewCipher(key[:]) 97 ctr := cipher.NewCFBEncrypter(aes, iv[:]) 98 99 b.ResetTimer() 100 for i := 0; i < b.N; i++ { 101 ctr.XORKeyStream(buf, buf) 102 } 103 } 104 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[:]) 113 114 b.ResetTimer() 115 for i := 0; i < b.N; i++ { 116 ctr.XORKeyStream(buf, buf) 117 } 118 } 119 120 func BenchmarkAESOFB1K(b *testing.B) { 121 buf := make([]byte, almost1K) 122 b.SetBytes(int64(len(buf))) 123 124 var key [16]byte 125 var iv [16]byte 126 aes, _ := aes.NewCipher(key[:]) 127 ctr := cipher.NewOFB(aes, iv[:]) 128 129 b.ResetTimer() 130 for i := 0; i < b.N; i++ { 131 ctr.XORKeyStream(buf, buf) 132 } 133 } 134 135 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[:]) 143 144 b.ResetTimer() 145 for i := 0; i < b.N; i++ { 146 ctr.XORKeyStream(buf, buf) 147 } 148 } 149 150 func BenchmarkAESCBCEncrypt1K(b *testing.B) { 151 buf := make([]byte, 1024) 152 b.SetBytes(int64(len(buf))) 153 154 var key [16]byte 155 var iv [16]byte 156 aes, _ := aes.NewCipher(key[:]) 157 cbc := cipher.NewCBCEncrypter(aes, iv[:]) 158 for i := 0; i < b.N; i++ { 159 cbc.CryptBlocks(buf, buf) 160 } 161 } 162 163 func BenchmarkAESCBCDecrypt1K(b *testing.B) { 164 buf := make([]byte, 1024) 165 b.SetBytes(int64(len(buf))) 166 167 var key [16]byte 168 var iv [16]byte 169 aes, _ := aes.NewCipher(key[:]) 170 cbc := cipher.NewCBCDecrypter(aes, iv[:]) 171 for i := 0; i < b.N; i++ { 172 cbc.CryptBlocks(buf, buf) 173 } 174 }