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