github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/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 benchmarkAESGCMSeal(b *testing.B, buf []byte) {
    14  	b.SetBytes(int64(len(buf)))
    15  
    16  	var key [16]byte
    17  	var nonce [12]byte
    18  	var ad [13]byte
    19  	aes, _ := aes.NewCipher(key[:])
    20  	aesgcm, _ := cipher.NewGCM(aes)
    21  	var out []byte
    22  
    23  	b.ResetTimer()
    24  	for i := 0; i < b.N; i++ {
    25  		out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
    26  	}
    27  }
    28  
    29  func benchmarkAESGCMOpen(b *testing.B, buf []byte) {
    30  	b.SetBytes(int64(len(buf)))
    31  
    32  	var key [16]byte
    33  	var nonce [12]byte
    34  	var ad [13]byte
    35  	aes, _ := aes.NewCipher(key[:])
    36  	aesgcm, _ := cipher.NewGCM(aes)
    37  	var out []byte
    38  	out = aesgcm.Seal(out[:0], nonce[:], buf, ad[:])
    39  
    40  	b.ResetTimer()
    41  	for i := 0; i < b.N; i++ {
    42  		_, err := aesgcm.Open(buf[:0], nonce[:], out, ad[:])
    43  		if err != nil {
    44  			b.Errorf("Open: %v", err)
    45  		}
    46  	}
    47  }
    48  
    49  func BenchmarkAESGCMSeal1K(b *testing.B) {
    50  	benchmarkAESGCMSeal(b, make([]byte, 1024))
    51  }
    52  
    53  func BenchmarkAESGCMOpen1K(b *testing.B) {
    54  	benchmarkAESGCMOpen(b, make([]byte, 1024))
    55  }
    56  
    57  func BenchmarkAESGCMSeal8K(b *testing.B) {
    58  	benchmarkAESGCMSeal(b, make([]byte, 8*1024))
    59  }
    60  
    61  func BenchmarkAESGCMOpen8K(b *testing.B) {
    62  	benchmarkAESGCMOpen(b, make([]byte, 8*1024))
    63  }
    64  
    65  // If we test exactly 1K blocks, we would generate exact multiples of
    66  // the cipher's block size, and the cipher stream fragments would
    67  // always be wordsize aligned, whereas non-aligned is a more typical
    68  // use-case.
    69  const almost1K = 1024 - 5
    70  
    71  func BenchmarkAESCFBEncrypt1K(b *testing.B) {
    72  	buf := make([]byte, almost1K)
    73  	b.SetBytes(int64(len(buf)))
    74  
    75  	var key [16]byte
    76  	var iv [16]byte
    77  	aes, _ := aes.NewCipher(key[:])
    78  	ctr := cipher.NewCFBEncrypter(aes, iv[:])
    79  
    80  	b.ResetTimer()
    81  	for i := 0; i < b.N; i++ {
    82  		ctr.XORKeyStream(buf, buf)
    83  	}
    84  }
    85  
    86  func BenchmarkAESCFBDecrypt1K(b *testing.B) {
    87  	buf := make([]byte, almost1K)
    88  	b.SetBytes(int64(len(buf)))
    89  
    90  	var key [16]byte
    91  	var iv [16]byte
    92  	aes, _ := aes.NewCipher(key[:])
    93  	ctr := cipher.NewCFBDecrypter(aes, iv[:])
    94  
    95  	b.ResetTimer()
    96  	for i := 0; i < b.N; i++ {
    97  		ctr.XORKeyStream(buf, buf)
    98  	}
    99  }
   100  
   101  func BenchmarkAESOFB1K(b *testing.B) {
   102  	buf := make([]byte, almost1K)
   103  	b.SetBytes(int64(len(buf)))
   104  
   105  	var key [16]byte
   106  	var iv [16]byte
   107  	aes, _ := aes.NewCipher(key[:])
   108  	ctr := cipher.NewOFB(aes, iv[:])
   109  
   110  	b.ResetTimer()
   111  	for i := 0; i < b.N; i++ {
   112  		ctr.XORKeyStream(buf, buf)
   113  	}
   114  }
   115  
   116  func BenchmarkAESCTR1K(b *testing.B) {
   117  	buf := make([]byte, almost1K)
   118  	b.SetBytes(int64(len(buf)))
   119  
   120  	var key [16]byte
   121  	var iv [16]byte
   122  	aes, _ := aes.NewCipher(key[:])
   123  	ctr := cipher.NewCTR(aes, iv[:])
   124  
   125  	b.ResetTimer()
   126  	for i := 0; i < b.N; i++ {
   127  		ctr.XORKeyStream(buf, buf)
   128  	}
   129  }
   130  
   131  func BenchmarkAESCBCEncrypt1K(b *testing.B) {
   132  	buf := make([]byte, 1024)
   133  	b.SetBytes(int64(len(buf)))
   134  
   135  	var key [16]byte
   136  	var iv [16]byte
   137  	aes, _ := aes.NewCipher(key[:])
   138  	cbc := cipher.NewCBCEncrypter(aes, iv[:])
   139  	for i := 0; i < b.N; i++ {
   140  		cbc.CryptBlocks(buf, buf)
   141  	}
   142  }
   143  
   144  func BenchmarkAESCBCDecrypt1K(b *testing.B) {
   145  	buf := make([]byte, 1024)
   146  	b.SetBytes(int64(len(buf)))
   147  
   148  	var key [16]byte
   149  	var iv [16]byte
   150  	aes, _ := aes.NewCipher(key[:])
   151  	cbc := cipher.NewCBCDecrypter(aes, iv[:])
   152  	for i := 0; i < b.N; i++ {
   153  		cbc.CryptBlocks(buf, buf)
   154  	}
   155  }