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.
 
 
 
 
 
 

100 lines
3.0 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. )
  7. // See draft-irtf-cfrg-chacha20-poly1305-10, section 2.1.1.
  8. func TestChaChaQuarterRound(t *testing.T) {
  9. state := [16]uint32{0x11111111, 0x01020304, 0x9b8d6f43, 0x01234567}
  10. chaChaQuarterRound(&state, 0, 1, 2, 3)
  11. a, b, c, d := state[0], state[1], state[2], state[3]
  12. if a != 0xea2a92f4 || b != 0xcb1cf8ce || c != 0x4581472e || d != 0x5881c4bb {
  13. t.Errorf("Incorrect results: %x", state)
  14. }
  15. }
  16. // See draft-irtf-cfrg-chacha20-poly1305-10, section 2.2.1.
  17. func TestChaChaQuarterRoundState(t *testing.T) {
  18. state := [16]uint32{
  19. 0x879531e0, 0xc5ecf37d, 0x516461b1, 0xc9a62f8a,
  20. 0x44c20ef3, 0x3390af7f, 0xd9fc690b, 0x2a5f714c,
  21. 0x53372767, 0xb00a5631, 0x974c541a, 0x359e9963,
  22. 0x5c971061, 0x3d631689, 0x2098d9d6, 0x91dbd320,
  23. }
  24. chaChaQuarterRound(&state, 2, 7, 8, 13)
  25. expected := [16]uint32{
  26. 0x879531e0, 0xc5ecf37d, 0xbdb886dc, 0xc9a62f8a,
  27. 0x44c20ef3, 0x3390af7f, 0xd9fc690b, 0xcfacafd2,
  28. 0xe46bea80, 0xb00a5631, 0x974c541a, 0x359e9963,
  29. 0x5c971061, 0xccc07c79, 0x2098d9d6, 0x91dbd320,
  30. }
  31. for i := range state {
  32. if state[i] != expected[i] {
  33. t.Errorf("Mismatch at %d: %x vs %x", i, state, expected)
  34. }
  35. }
  36. }
  37. // See draft-irtf-cfrg-chacha20-poly1305-10, section 2.3.2.
  38. func TestChaCha20Block(t *testing.T) {
  39. state := [16]uint32{
  40. 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574,
  41. 0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
  42. 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c,
  43. 0x00000001, 0x09000000, 0x4a000000, 0x00000000,
  44. }
  45. out := make([]byte, 64)
  46. chaCha20Block(&state, out)
  47. expected := []byte{
  48. 0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15,
  49. 0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4,
  50. 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03,
  51. 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e,
  52. 0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09,
  53. 0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2,
  54. 0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9,
  55. 0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e,
  56. }
  57. if !bytes.Equal(out, expected) {
  58. t.Errorf("Got %x, wanted %x", out, expected)
  59. }
  60. }
  61. // See draft-agl-tls-chacha20poly1305-04, section 7.
  62. func TestChaCha20Poly1305(t *testing.T) {
  63. key, _ := hex.DecodeString("4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
  64. input, _ := hex.DecodeString("86d09974840bded2a5ca")
  65. nonce, _ := hex.DecodeString("cd7cf67be39c794a")
  66. ad, _ := hex.DecodeString("87e229d4500845a079c0")
  67. output, _ := hex.DecodeString("e3e446f7ede9a19b62a4677dabf4e3d24b876bb284753896e1d6")
  68. aead, err := newChaCha20Poly1305(key)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. out, err := aead.Open(nil, nonce, output, ad)
  73. if err != nil {
  74. t.Errorf("Open failed: %s", err)
  75. } else if !bytes.Equal(out, input) {
  76. t.Errorf("Open gave %x, wanted %x", out, input)
  77. }
  78. out = aead.Seal(nil, nonce, input, ad)
  79. if !bytes.Equal(out, output) {
  80. t.Errorf("Open gave %x, wanted %x", out, output)
  81. }
  82. out[0]++
  83. _, err = aead.Open(nil, nonce, out, ad)
  84. if err == nil {
  85. t.Errorf("Open on malformed data unexpectedly succeeded")
  86. }
  87. }