You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.3 KiB

  1. package xchacha20poly1305
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func toHex(bits []byte) string {
  9. return hex.EncodeToString(bits)
  10. }
  11. func fromHex(bits string) ([]byte, error) {
  12. b, err := hex.DecodeString(bits)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return b, nil
  17. }
  18. func check(t *testing.T, fn func(string) ([]byte, error), hex string) []byte {
  19. t.Helper()
  20. res, err := fn(hex)
  21. require.NoError(t, err)
  22. return res
  23. }
  24. func TestHChaCha20(t *testing.T) {
  25. var hChaCha20Vectors = []struct {
  26. key, nonce, keystream []byte
  27. }{
  28. {
  29. check(t, fromHex, "0000000000000000000000000000000000000000000000000000000000000000"),
  30. check(t, fromHex, "000000000000000000000000000000000000000000000000"),
  31. check(t, fromHex, "1140704c328d1d5d0e30086cdf209dbd6a43b8f41518a11cc387b669b2ee6586"),
  32. },
  33. {
  34. check(t, fromHex, "8000000000000000000000000000000000000000000000000000000000000000"),
  35. check(t, fromHex, "000000000000000000000000000000000000000000000000"),
  36. check(t, fromHex, "7d266a7fd808cae4c02a0a70dcbfbcc250dae65ce3eae7fc210f54cc8f77df86"),
  37. },
  38. {
  39. check(t, fromHex, "0000000000000000000000000000000000000000000000000000000000000001"),
  40. check(t, fromHex, "000000000000000000000000000000000000000000000002"),
  41. check(t, fromHex, "e0c77ff931bb9163a5460c02ac281c2b53d792b1c43fea817e9ad275ae546963"),
  42. },
  43. {
  44. check(t, fromHex, "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"),
  45. check(t, fromHex, "000102030405060708090a0b0c0d0e0f1011121314151617"),
  46. check(t, fromHex, "51e3ff45a895675c4b33b46c64f4a9ace110d34df6a2ceab486372bacbd3eff6"),
  47. },
  48. {
  49. check(t, fromHex, "24f11cce8a1b3d61e441561a696c1c1b7e173d084fd4812425435a8896a013dc"),
  50. check(t, fromHex, "d9660c5900ae19ddad28d6e06e45fe5e"),
  51. check(t, fromHex, "5966b3eec3bff1189f831f06afe4d4e3be97fa9235ec8c20d08acfbbb4e851e3"),
  52. },
  53. }
  54. for i, v := range hChaCha20Vectors {
  55. var key [32]byte
  56. var nonce [16]byte
  57. copy(key[:], v.key)
  58. copy(nonce[:], v.nonce)
  59. HChaCha20(&key, &nonce, &key)
  60. if !bytes.Equal(key[:], v.keystream) {
  61. t.Errorf("test %d: keystream mismatch:\n \t got: %s\n \t want: %s", i, toHex(key[:]), toHex(v.keystream))
  62. }
  63. }
  64. }
  65. func TestVectors(t *testing.T) {
  66. for i, v := range vectors {
  67. if len(v.plaintext) == 0 {
  68. v.plaintext = make([]byte, len(v.ciphertext))
  69. }
  70. var nonce [24]byte
  71. copy(nonce[:], v.nonce)
  72. aead, err := New(v.key)
  73. if err != nil {
  74. t.Error(err)
  75. }
  76. dst := aead.Seal(nil, nonce[:], v.plaintext, v.ad)
  77. if !bytes.Equal(dst, v.ciphertext) {
  78. t.Errorf("test %d: ciphertext mismatch:\n \t got: %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext))
  79. }
  80. open, err := aead.Open(nil, nonce[:], dst, v.ad)
  81. if err != nil {
  82. t.Error(err)
  83. }
  84. if !bytes.Equal(open, v.plaintext) {
  85. t.Errorf("test %d: plaintext mismatch:\n \t got: %s\n \t want: %s", i, string(open), string(v.plaintext))
  86. }
  87. }
  88. }
  89. var vectors = []struct {
  90. key, nonce, ad, plaintext, ciphertext []byte
  91. }{
  92. {
  93. []byte{
  94. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a,
  95. 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
  96. 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  97. },
  98. []byte{0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b},
  99. []byte{0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7},
  100. []byte(
  101. "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.",
  102. ),
  103. []byte{
  104. 0x45, 0x3c, 0x06, 0x93, 0xa7, 0x40, 0x7f, 0x04, 0xff, 0x4c, 0x56,
  105. 0xae, 0xdb, 0x17, 0xa3, 0xc0, 0xa1, 0xaf, 0xff, 0x01, 0x17, 0x49,
  106. 0x30, 0xfc, 0x22, 0x28, 0x7c, 0x33, 0xdb, 0xcf, 0x0a, 0xc8, 0xb8,
  107. 0x9a, 0xd9, 0x29, 0x53, 0x0a, 0x1b, 0xb3, 0xab, 0x5e, 0x69, 0xf2,
  108. 0x4c, 0x7f, 0x60, 0x70, 0xc8, 0xf8, 0x40, 0xc9, 0xab, 0xb4, 0xf6,
  109. 0x9f, 0xbf, 0xc8, 0xa7, 0xff, 0x51, 0x26, 0xfa, 0xee, 0xbb, 0xb5,
  110. 0x58, 0x05, 0xee, 0x9c, 0x1c, 0xf2, 0xce, 0x5a, 0x57, 0x26, 0x32,
  111. 0x87, 0xae, 0xc5, 0x78, 0x0f, 0x04, 0xec, 0x32, 0x4c, 0x35, 0x14,
  112. 0x12, 0x2c, 0xfc, 0x32, 0x31, 0xfc, 0x1a, 0x8b, 0x71, 0x8a, 0x62,
  113. 0x86, 0x37, 0x30, 0xa2, 0x70, 0x2b, 0xb7, 0x63, 0x66, 0x11, 0x6b,
  114. 0xed, 0x09, 0xe0, 0xfd, 0x5c, 0x6d, 0x84, 0xb6, 0xb0, 0xc1, 0xab,
  115. 0xaf, 0x24, 0x9d, 0x5d, 0xd0, 0xf7, 0xf5, 0xa7, 0xea,
  116. },
  117. },
  118. }