Guest User

Untitled

a guest
Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "fmt"
  8. "io"
  9. "testing"
  10.  
  11. "github.com/alecthomas/units"
  12. )
  13.  
  14. func newCipher(keySize int64) cipher.AEAD {
  15. key := make([]byte, keySize)
  16. io.ReadFull(rand.Reader, key)
  17.  
  18. block, err := aes.NewCipher(key)
  19. if err != nil {
  20. panic(err)
  21. }
  22.  
  23. aead, err := cipher.NewGCM(block)
  24. if err != nil {
  25. panic(err)
  26. }
  27.  
  28. return aead
  29. }
  30.  
  31. func benchmarkSeal(b *testing.B, chunkSize units.Base2Bytes, aead cipher.AEAD) {
  32. plaintext := make([]byte, chunkSize)
  33. nonce := make([]byte, 12)
  34.  
  35. b.SetBytes(int64(chunkSize))
  36. b.ResetTimer()
  37.  
  38. for i := 0; i < b.N; i++ {
  39. aead.Seal(nil, nonce, plaintext, nil)
  40. }
  41. }
  42.  
  43. func benchmarkOpen(b *testing.B, chunkSize units.Base2Bytes, aead cipher.AEAD) {
  44. nonce := make([]byte, 12)
  45. ciphertext := aead.Seal(nil, nonce, make([]byte, chunkSize), nil)
  46.  
  47. b.SetBytes(int64(chunkSize))
  48. b.ResetTimer()
  49.  
  50. for i := 0; i < b.N; i++ {
  51. aead.Open(nil, nonce, ciphertext, nil)
  52. }
  53. }
  54.  
  55. func BenchmarkGCM(b *testing.B) {
  56. for _, keySize := range []int64{16, 32} {
  57. for _, chunkSize := range []units.Base2Bytes{units.KiB, units.MiB, units.GiB} {
  58. aead := newCipher(keySize)
  59.  
  60. seal := fmt.Sprintf("AES-%d-GCM-Seal-%s", int64(keySize)*8, chunkSize.String())
  61. open := fmt.Sprintf("AES-%d-GCM-Open-%s", int64(keySize)*8, chunkSize.String())
  62.  
  63. b.Run(seal, func(b *testing.B) { benchmarkSeal(b, chunkSize, aead) })
  64. b.Run(open, func(b *testing.B) { benchmarkOpen(b, chunkSize, aead) })
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment