github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/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 func benchmarkAESStream(b *testing.B, mode func(cipher.Block, []byte) cipher.Stream, buf []byte) { 85 b.SetBytes(int64(len(buf))) 86 87 var key [16]byte 88 var iv [16]byte 89 aes, _ := aes.NewCipher(key[:]) 90 stream := mode(aes, iv[:]) 91 92 b.ResetTimer() 93 for i := 0; i < b.N; i++ { 94 stream.XORKeyStream(buf, buf) 95 } 96 } 97 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 104 105 func BenchmarkAESCFBEncrypt1K(b *testing.B) { 106 benchmarkAESStream(b, cipher.NewCFBEncrypter, make([]byte, almost1K)) 107 } 108 109 func BenchmarkAESCFBDecrypt1K(b *testing.B) { 110 benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost1K)) 111 } 112 113 func BenchmarkAESCFBDecrypt8K(b *testing.B) { 114 benchmarkAESStream(b, cipher.NewCFBDecrypter, make([]byte, almost8K)) 115 } 116 117 func BenchmarkAESOFB1K(b *testing.B) { 118 benchmarkAESStream(b, cipher.NewOFB, make([]byte, almost1K)) 119 } 120 121 func BenchmarkAESCTR1K(b *testing.B) { 122 benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost1K)) 123 } 124 125 func BenchmarkAESCTR8K(b *testing.B) { 126 benchmarkAESStream(b, cipher.NewCTR, make([]byte, almost8K)) 127 } 128 129 func BenchmarkAESCBCEncrypt1K(b *testing.B) { 130 buf := make([]byte, 1024) 131 b.SetBytes(int64(len(buf))) 132 133 var key [16]byte 134 var iv [16]byte 135 aes, _ := aes.NewCipher(key[:]) 136 cbc := cipher.NewCBCEncrypter(aes, iv[:]) 137 for i := 0; i < b.N; i++ { 138 cbc.CryptBlocks(buf, buf) 139 } 140 } 141 142 func BenchmarkAESCBCDecrypt1K(b *testing.B) { 143 buf := make([]byte, 1024) 144 b.SetBytes(int64(len(buf))) 145 146 var key [16]byte 147 var iv [16]byte 148 aes, _ := aes.NewCipher(key[:]) 149 cbc := cipher.NewCBCDecrypter(aes, iv[:]) 150 for i := 0; i < b.N; i++ { 151 cbc.CryptBlocks(buf, buf) 152 } 153 }