25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

189 lines
5.0 KiB

  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/hex"
  7. "fmt"
  8. "math/rand"
  9. "testing"
  10. )
  11. func TestCore(t *testing.T) {
  12. // This is just a smoke test that checks the example from
  13. // https://tools.ietf.org/html/rfc7539#section-2.3.2. The
  14. // chacha20poly1305 package contains much more extensive tests of this
  15. // code.
  16. var key [32]byte
  17. for i := range key {
  18. key[i] = byte(i)
  19. }
  20. var input [16]byte
  21. input[0] = 1
  22. input[7] = 9
  23. input[11] = 0x4a
  24. var out [64]byte
  25. XORKeyStream(out[:], out[:], &input, &key)
  26. const expected = "10f1e7e4d13b5915500fdd1fa32071c4c7d1f4c733c068030422aa9ac3d46c4ed2826446079faa0914c2d705d98b02a2b5129cd1de164eb9cbd083e8a2503c4e"
  27. if result := hex.EncodeToString(out[:]); result != expected {
  28. t.Errorf("wanted %x but got %x", expected, result)
  29. }
  30. }
  31. // Run the test cases with the input and output in different buffers.
  32. func TestNoOverlap(t *testing.T) {
  33. for _, c := range testVectors {
  34. s := New(c.key, c.nonce)
  35. input, err := hex.DecodeString(c.input)
  36. if err != nil {
  37. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  38. }
  39. output := make([]byte, c.length)
  40. s.XORKeyStream(output, input)
  41. got := hex.EncodeToString(output)
  42. if got != c.output {
  43. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  44. }
  45. }
  46. }
  47. // Run the test cases with the input and output overlapping entirely.
  48. func TestOverlap(t *testing.T) {
  49. for _, c := range testVectors {
  50. s := New(c.key, c.nonce)
  51. data, err := hex.DecodeString(c.input)
  52. if err != nil {
  53. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  54. }
  55. s.XORKeyStream(data, data)
  56. got := hex.EncodeToString(data)
  57. if got != c.output {
  58. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  59. }
  60. }
  61. }
  62. // Run the test cases with various source and destination offsets.
  63. func TestUnaligned(t *testing.T) {
  64. const max = 8 // max offset (+1) to test
  65. for _, c := range testVectors {
  66. input := make([]byte, c.length+max)
  67. output := make([]byte, c.length+max)
  68. for i := 0; i < max; i++ { // input offsets
  69. for j := 0; j < max; j++ { // output offsets
  70. s := New(c.key, c.nonce)
  71. input := input[i : i+c.length]
  72. output := output[j : j+c.length]
  73. data, err := hex.DecodeString(c.input)
  74. if err != nil {
  75. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  76. }
  77. copy(input, data)
  78. s.XORKeyStream(output, input)
  79. got := hex.EncodeToString(output)
  80. if got != c.output {
  81. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  82. }
  83. }
  84. }
  85. }
  86. }
  87. // Run the test cases by calling XORKeyStream multiple times.
  88. func TestStep(t *testing.T) {
  89. // wide range of step sizes to try and hit edge cases
  90. steps := [...]int{1, 3, 4, 7, 8, 17, 24, 30, 64, 256}
  91. rnd := rand.New(rand.NewSource(123))
  92. for _, c := range testVectors {
  93. s := New(c.key, c.nonce)
  94. input, err := hex.DecodeString(c.input)
  95. if err != nil {
  96. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  97. }
  98. output := make([]byte, c.length)
  99. // step through the buffers
  100. i, step := 0, steps[rnd.Intn(len(steps))]
  101. for i+step < c.length {
  102. s.XORKeyStream(output[i:i+step], input[i:i+step])
  103. if i+step < c.length && output[i+step] != 0 {
  104. t.Errorf("length=%v, i=%v, step=%v: output overwritten", c.length, i, step)
  105. }
  106. i += step
  107. step = steps[rnd.Intn(len(steps))]
  108. }
  109. // finish the encryption
  110. s.XORKeyStream(output[i:], input[i:])
  111. got := hex.EncodeToString(output)
  112. if got != c.output {
  113. t.Errorf("length=%v: got %#v, want %#v", c.length, got, c.output)
  114. }
  115. }
  116. }
  117. // Test that Advance() discards bytes until a block boundary is hit.
  118. func TestAdvance(t *testing.T) {
  119. for _, c := range testVectors {
  120. for i := 0; i < 63; i++ {
  121. s := New(c.key, c.nonce)
  122. z := New(c.key, c.nonce)
  123. input, err := hex.DecodeString(c.input)
  124. if err != nil {
  125. t.Fatalf("cannot decode input %#v: %v", c.input, err)
  126. }
  127. zeros, discard := make([]byte, 64), make([]byte, 64)
  128. so, zo := make([]byte, c.length), make([]byte, c.length)
  129. for j := 0; j < c.length; j += 64 {
  130. lim := j + i
  131. if lim > c.length {
  132. lim = c.length
  133. }
  134. s.XORKeyStream(so[j:lim], input[j:lim])
  135. // calling s.Advance() multiple times should have no effect
  136. for k := 0; k < i%3+1; k++ {
  137. s.Advance()
  138. }
  139. z.XORKeyStream(zo[j:lim], input[j:lim])
  140. if lim < c.length {
  141. end := 64 - i
  142. if c.length-lim < end {
  143. end = c.length - lim
  144. }
  145. z.XORKeyStream(discard[:], zeros[:end])
  146. }
  147. }
  148. got := hex.EncodeToString(so)
  149. want := hex.EncodeToString(zo)
  150. if got != want {
  151. t.Errorf("length=%v: got %#v, want %#v", c.length, got, want)
  152. }
  153. }
  154. }
  155. }
  156. func BenchmarkChaCha20(b *testing.B) {
  157. sizes := []int{32, 63, 64, 256, 1024, 1350, 65536}
  158. for _, size := range sizes {
  159. s := size
  160. b.Run(fmt.Sprint(s), func(b *testing.B) {
  161. k := [32]byte{}
  162. c := [16]byte{}
  163. src := make([]byte, s)
  164. dst := make([]byte, s)
  165. b.SetBytes(int64(s))
  166. b.ResetTimer()
  167. for i := 0; i < b.N; i++ {
  168. XORKeyStream(dst, src, &c, &k)
  169. }
  170. })
  171. }
  172. }