aead_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // aead_test.go - AEAD_CHACHA20_POLY1305 implementation tests.
  2. //
  3. // To the extent possible under law, Yawning Angel waived all copyright
  4. // and related or neighboring rights to chacha20poly1305, using the Creative
  5. // Commons "CC0" public domain dedication. See LICENSE or
  6. // <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
  7. package chacha20poly1305
  8. import (
  9. "bytes"
  10. "testing"
  11. )
  12. func TestChaCha20Poly1305(t *testing.T) {
  13. // Test vector taken from RFC 7539.
  14. key := []byte{
  15. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  16. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  17. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  18. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  19. }
  20. nonce := []byte{
  21. // "32-bit fixed common part"
  22. 0x07, 0x00, 0x00, 0x00,
  23. // "IV"
  24. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  25. }
  26. aad := []byte{
  27. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  28. 0xc4, 0xc5, 0xc6, 0xc7,
  29. }
  30. vecPlaintext := []byte{
  31. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  32. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  33. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  34. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  35. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  36. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  37. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  38. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  39. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  40. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  41. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  42. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  43. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  44. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  45. 0x74, 0x2e,
  46. }
  47. vecCiphertext := []byte{
  48. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  49. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  50. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  51. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  52. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  53. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  54. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  55. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  56. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  57. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  58. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  59. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  60. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  61. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  62. 0x61, 0x16,
  63. }
  64. vecTag := []byte{
  65. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  66. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91,
  67. }
  68. a, err := New(key)
  69. if err != nil {
  70. t.Fatalf("Failed to instantiate AEAD instance: %v", err)
  71. }
  72. ciphertext := a.Seal(nil, nonce, vecPlaintext, aad)
  73. if !bytes.Equal(ciphertext[len(vecCiphertext):], vecTag) {
  74. t.Fatalf("Seal: Tag mismatch")
  75. }
  76. if !bytes.Equal(ciphertext[:len(vecCiphertext)], vecCiphertext) {
  77. t.Fatalf("Seal: Ciphertext mismatch")
  78. }
  79. plaintext, err := a.Open(nil, nonce, ciphertext, aad)
  80. if err != nil {
  81. t.Fatalf("Open: Failed")
  82. }
  83. if !bytes.Equal(plaintext, vecPlaintext) {
  84. t.Fatalf("Open: Plaintext mismatch")
  85. }
  86. }
  87. func benchmarkChaCha20Poly1305Seal(b *testing.B, n int) {
  88. var key [KeySize]byte
  89. var nonce [NonceSize]byte
  90. a, err := New(key[:])
  91. if err != nil {
  92. b.Fatalf("Failed to instantiate AEAD instance: %v", err)
  93. }
  94. var ad [13]byte // The crypto/cipher benchmarks use this size.
  95. plaintext := make([]byte, n)
  96. b.SetBytes(int64(n))
  97. b.ResetTimer()
  98. for i := 0; i < b.N; i++ {
  99. _ = a.Seal(nil, nonce[:], plaintext, ad[:])
  100. }
  101. }
  102. func benchmarkChaCha20Poly1305Open(b *testing.B, n int) {
  103. var key [KeySize]byte
  104. var nonce [NonceSize]byte
  105. a, err := New(key[:])
  106. if err != nil {
  107. b.Fatalf("Failed to instantiate AEAD instance: %v", err)
  108. }
  109. plaintext := make([]byte, n)
  110. var ad [13]byte // The crypto/cipher benchmarks use this size.
  111. ciphertext := a.Seal(nil, nonce[:], plaintext, ad[:])
  112. b.SetBytes(int64(n))
  113. b.ResetTimer()
  114. b.SetBytes(int64(n))
  115. b.ResetTimer()
  116. for i := 0; i < b.N; i++ {
  117. _, err := a.Open(plaintext[:0], nonce[:], ciphertext, ad[:])
  118. if err != nil {
  119. b.Fatalf("Failed to open ciphertext: %v", err)
  120. }
  121. }
  122. }
  123. func BenchmarkChaCha20Poly1305Seal1K(b *testing.B) {
  124. benchmarkChaCha20Poly1305Seal(b, 1024)
  125. }
  126. func BenchmarkChaCha20Poly1305Open1K(b *testing.B) {
  127. benchmarkChaCha20Poly1305Open(b, 1024)
  128. }
  129. func BenchmarkChaCha20Poly1305Seal4K(b *testing.B) {
  130. benchmarkChaCha20Poly1305Seal(b, 4096)
  131. }
  132. func BenchmarkChaCha20Poly1305Open4K(b *testing.B) {
  133. benchmarkChaCha20Poly1305Open(b, 4096)
  134. }
  135. func BenchmarkChaCha20Poly1305Seal8K(b *testing.B) {
  136. benchmarkChaCha20Poly1305Seal(b, 8192)
  137. }
  138. func BenchmarkChaCha20Poly1305Open8K(b *testing.B) {
  139. benchmarkChaCha20Poly1305Open(b, 8192)
  140. }