benchmark_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. package cipher_test
  5. import (
  6. "crypto/aes"
  7. "crypto/cipher"
  8. "testing"
  9. )
  10. func BenchmarkAESGCMSeal1K(b *testing.B) {
  11. buf := make([]byte, 1024)
  12. b.SetBytes(int64(len(buf)))
  13. var key [16]byte
  14. var nonce [12]byte
  15. aes, _ := aes.NewCipher(key[:])
  16. aesgcm, _ := cipher.NewGCM(aes)
  17. var out []byte
  18. b.ResetTimer()
  19. for i := 0; i < b.N; i++ {
  20. out = aesgcm.Seal(out[:0], nonce[:], buf, nonce[:])
  21. }
  22. }
  23. func BenchmarkAESGCMOpen1K(b *testing.B) {
  24. buf := make([]byte, 1024)
  25. b.SetBytes(int64(len(buf)))
  26. var key [16]byte
  27. var nonce [12]byte
  28. aes, _ := aes.NewCipher(key[:])
  29. aesgcm, _ := cipher.NewGCM(aes)
  30. var out []byte
  31. out = aesgcm.Seal(out[:0], nonce[:], buf, nonce[:])
  32. b.ResetTimer()
  33. for i := 0; i < b.N; i++ {
  34. _, err := aesgcm.Open(buf[:0], nonce[:], out, nonce[:])
  35. if err != nil {
  36. b.Errorf("Open: %v", err)
  37. }
  38. }
  39. }
  40. // If we test exactly 1K blocks, we would generate exact multiples of
  41. // the cipher's block size, and the cipher stream fragments would
  42. // always be wordsize aligned, whereas non-aligned is a more typical
  43. // use-case.
  44. const almost1K = 1024 - 5
  45. func BenchmarkAESCFBEncrypt1K(b *testing.B) {
  46. buf := make([]byte, almost1K)
  47. b.SetBytes(int64(len(buf)))
  48. var key [16]byte
  49. var iv [16]byte
  50. aes, _ := aes.NewCipher(key[:])
  51. ctr := cipher.NewCFBEncrypter(aes, iv[:])
  52. b.ResetTimer()
  53. for i := 0; i < b.N; i++ {
  54. ctr.XORKeyStream(buf, buf)
  55. }
  56. }
  57. func BenchmarkAESCFBDecrypt1K(b *testing.B) {
  58. buf := make([]byte, almost1K)
  59. b.SetBytes(int64(len(buf)))
  60. var key [16]byte
  61. var iv [16]byte
  62. aes, _ := aes.NewCipher(key[:])
  63. ctr := cipher.NewCFBDecrypter(aes, iv[:])
  64. b.ResetTimer()
  65. for i := 0; i < b.N; i++ {
  66. ctr.XORKeyStream(buf, buf)
  67. }
  68. }
  69. func BenchmarkAESOFB1K(b *testing.B) {
  70. buf := make([]byte, almost1K)
  71. b.SetBytes(int64(len(buf)))
  72. var key [16]byte
  73. var iv [16]byte
  74. aes, _ := aes.NewCipher(key[:])
  75. ctr := cipher.NewOFB(aes, iv[:])
  76. b.ResetTimer()
  77. for i := 0; i < b.N; i++ {
  78. ctr.XORKeyStream(buf, buf)
  79. }
  80. }
  81. func BenchmarkAESCTR1K(b *testing.B) {
  82. buf := make([]byte, almost1K)
  83. b.SetBytes(int64(len(buf)))
  84. var key [16]byte
  85. var iv [16]byte
  86. aes, _ := aes.NewCipher(key[:])
  87. ctr := cipher.NewCTR(aes, iv[:])
  88. b.ResetTimer()
  89. for i := 0; i < b.N; i++ {
  90. ctr.XORKeyStream(buf, buf)
  91. }
  92. }
  93. func BenchmarkAESCBCEncrypt1K(b *testing.B) {
  94. buf := make([]byte, 1024)
  95. b.SetBytes(int64(len(buf)))
  96. var key [16]byte
  97. var iv [16]byte
  98. aes, _ := aes.NewCipher(key[:])
  99. cbc := cipher.NewCBCEncrypter(aes, iv[:])
  100. for i := 0; i < b.N; i++ {
  101. cbc.CryptBlocks(buf, buf)
  102. }
  103. }
  104. func BenchmarkAESCBCDecrypt1K(b *testing.B) {
  105. buf := make([]byte, 1024)
  106. b.SetBytes(int64(len(buf)))
  107. var key [16]byte
  108. var iv [16]byte
  109. aes, _ := aes.NewCipher(key[:])
  110. cbc := cipher.NewCBCDecrypter(aes, iv[:])
  111. for i := 0; i < b.N; i++ {
  112. cbc.CryptBlocks(buf, buf)
  113. }
  114. }