chacha_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2016 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 chacha20
  5. import (
  6. "encoding/binary"
  7. "encoding/hex"
  8. "fmt"
  9. "math/rand"
  10. "testing"
  11. )
  12. func TestCore(t *testing.T) {
  13. // This is just a smoke test that checks the example from
  14. // https://tools.ietf.org/html/rfc7539#section-2.3.2. The
  15. // chacha20poly1305 package contains much more extensive tests of this
  16. // code.
  17. var key [32]byte
  18. for i := range key {
  19. key[i] = byte(i)
  20. }
  21. var input [16]byte
  22. input[0] = 1
  23. input[7] = 9
  24. input[11] = 0x4a
  25. var out [64]byte
  26. XORKeyStream(out[:], out[:], &input, &key)
  27. const expected = "10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e"
  28. if result := hex.EncodeToString(out[:]); result != expected {
  29. t.Errorf("wanted %x but got %x", expected, result)
  30. }
  31. }
  32. // Run the test cases with the input and output in different buffers.
  33. func TestNoOverlap(t *testing.T) {
  34. for _, c := range testVectors {
  35. s := New(c.key, c.nonce)
  36. input, err := hex.DecodeString(c.input)
  37. if err != nil {
  38. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  39. }
  40. output := make([]byte, c.length)
  41. s.XORKeyStream(output, input)
  42. got := hex.EncodeToString(output)
  43. if got != c.output {
  44. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  45. }
  46. }
  47. }
  48. // Run the test cases with the input and output overlapping entirely.
  49. func TestOverlap(t *testing.T) {
  50. for _, c := range testVectors {
  51. s := New(c.key, c.nonce)
  52. data, err := hex.DecodeString(c.input)
  53. if err != nil {
  54. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  55. }
  56. s.XORKeyStream(data, data)
  57. got := hex.EncodeToString(data)
  58. if got != c.output {
  59. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  60. }
  61. }
  62. }
  63. // Run the test cases with various source and destination offsets.
  64. func TestUnaligned(t *testing.T) {
  65. const max = 8 // max offset (+1) to test
  66. for _, c := range testVectors {
  67. input := make([]byte, c.length+max)
  68. output := make([]byte, c.length+max)
  69. for i := 0; i < max; i++ { // input offsets
  70. for j := 0; j < max; j++ { // output offsets
  71. s := New(c.key, c.nonce)
  72. input := input[i : i+c.length]
  73. output := output[j : j+c.length]
  74. data, err := hex.DecodeString(c.input)
  75. if err != nil {
  76. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  77. }
  78. copy(input, data)
  79. s.XORKeyStream(output, input)
  80. got := hex.EncodeToString(output)
  81. if got != c.output {
  82. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  83. }
  84. }
  85. }
  86. }
  87. }
  88. // Run the test cases by calling XORKeyStream multiple times.
  89. func TestStep(t *testing.T) {
  90. // wide range of step sizes to try and hit edge cases
  91. steps := [...]int{1, 3, 4, 7, 8, 17, 24, 30, 64, 256}
  92. rnd := rand.New(rand.NewSource(123))
  93. for _, c := range testVectors {
  94. s := New(c.key, c.nonce)
  95. input, err := hex.DecodeString(c.input)
  96. if err != nil {
  97. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  98. }
  99. output := make([]byte, c.length)
  100. // step through the buffers
  101. i, step := 0, steps[rnd.Intn(len(steps))]
  102. for i+step < c.length {
  103. s.XORKeyStream(output[i:i+step], input[i:i+step])
  104. if i+step < c.length && output[i+step] != 0 {
  105. t.Errorf("length=%v, i=%v, step=%v: output overwritten", c.length, i, step)
  106. }
  107. i += step
  108. step = steps[rnd.Intn(len(steps))]
  109. }
  110. // finish the encryption
  111. s.XORKeyStream(output[i:], input[i:])
  112. got := hex.EncodeToString(output)
  113. if got != c.output {
  114. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  115. }
  116. }
  117. }
  118. // Test that Advance() discards bytes until a block boundary is hit.
  119. func TestAdvance(t *testing.T) {
  120. for _, c := range testVectors {
  121. for i := 0; i < 63; i++ {
  122. s := New(c.key, c.nonce)
  123. z := New(c.key, c.nonce)
  124. input, err := hex.DecodeString(c.input)
  125. if err != nil {
  126. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  127. }
  128. zeros, discard := make([]byte, 64), make([]byte, 64)
  129. so, zo := make([]byte, c.length), make([]byte, c.length)
  130. for j := 0; j < c.length; j += 64 {
  131. lim := j + i
  132. if lim > c.length {
  133. lim = c.length
  134. }
  135. s.XORKeyStream(so[j:lim], input[j:lim])
  136. // calling s.Advance() multiple times should have no effect
  137. for k := 0; k < i%3+1; k++ {
  138. s.Advance()
  139. }
  140. z.XORKeyStream(zo[j:lim], input[j:lim])
  141. if lim < c.length {
  142. end := 64 - i
  143. if c.length-lim < end {
  144. end = c.length - lim
  145. }
  146. z.XORKeyStream(discard[:], zeros[:end])
  147. }
  148. }
  149. got := hex.EncodeToString(so)
  150. want := hex.EncodeToString(zo)
  151. if got != want {
  152. t.Errorf("length=%v: got %#v, want %#v", c.length, got, want)
  153. }
  154. }
  155. }
  156. }
  157. func BenchmarkChaCha20(b *testing.B) {
  158. sizes := []int{32, 63, 64, 256, 1024, 1350, 65536}
  159. for _, size := range sizes {
  160. s := size
  161. b.Run(fmt.Sprint(s), func(b *testing.B) {
  162. k := [32]byte{}
  163. c := [16]byte{}
  164. src := make([]byte, s)
  165. dst := make([]byte, s)
  166. b.SetBytes(int64(s))
  167. b.ResetTimer()
  168. for i := 0; i < b.N; i++ {
  169. XORKeyStream(dst, src, &c, &k)
  170. }
  171. })
  172. }
  173. }
  174. func TestHChaCha20(t *testing.T) {
  175. // See draft-paragon-paseto-rfc-00 §7.2.1.
  176. key := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  177. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  178. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  179. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}
  180. nonce := []byte{0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a,
  181. 0x00, 0x00, 0x00, 0x00, 0x31, 0x41, 0x59, 0x27}
  182. expected := []byte{0x82, 0x41, 0x3b, 0x42, 0x27, 0xb2, 0x7b, 0xfe,
  183. 0xd3, 0x0e, 0x42, 0x50, 0x8a, 0x87, 0x7d, 0x73,
  184. 0xa0, 0xf9, 0xe4, 0xd5, 0x8a, 0x74, 0xa8, 0x53,
  185. 0xc1, 0x2e, 0xc4, 0x13, 0x26, 0xd3, 0xec, 0xdc,
  186. }
  187. result := HChaCha20(&[8]uint32{
  188. binary.LittleEndian.Uint32(key[0:4]),
  189. binary.LittleEndian.Uint32(key[4:8]),
  190. binary.LittleEndian.Uint32(key[8:12]),
  191. binary.LittleEndian.Uint32(key[12:16]),
  192. binary.LittleEndian.Uint32(key[16:20]),
  193. binary.LittleEndian.Uint32(key[20:24]),
  194. binary.LittleEndian.Uint32(key[24:28]),
  195. binary.LittleEndian.Uint32(key[28:32]),
  196. }, &[4]uint32{
  197. binary.LittleEndian.Uint32(nonce[0:4]),
  198. binary.LittleEndian.Uint32(nonce[4:8]),
  199. binary.LittleEndian.Uint32(nonce[8:12]),
  200. binary.LittleEndian.Uint32(nonce[12:16]),
  201. })
  202. for i := 0; i < 8; i++ {
  203. want := binary.LittleEndian.Uint32(expected[i*4 : (i+1)*4])
  204. if got := result[i]; got != want {
  205. t.Errorf("word %d incorrect: want 0x%x, got 0x%x", i, want, got)
  206. }
  207. }
  208. }