chacha20_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Copryright (C) 2019 Yawning Angel
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as
  5. // published by the Free Software Foundation, either version 3 of the
  6. // License, or (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU Affero General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Affero General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package chacha20
  16. import (
  17. "crypto/rand"
  18. "crypto/sha512"
  19. "math"
  20. "strconv"
  21. "testing"
  22. "github.com/stretchr/testify/require"
  23. "gitlab.com/yawning/chacha20.git/internal/api"
  24. )
  25. // Test vectors taken from:
  26. // https://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-01
  27. var draftTestVectors = []struct {
  28. name string
  29. key []byte
  30. iv []byte
  31. stream []byte
  32. seekOffset uint64
  33. }{
  34. {
  35. name: "IETF Draft: TC1: All zero key and IV.",
  36. key: []byte{
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. },
  42. iv: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  43. stream: []byte{
  44. 0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90,
  45. 0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
  46. 0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a,
  47. 0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
  48. 0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d,
  49. 0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
  50. 0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c,
  51. 0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86,
  52. 0x9f, 0x07, 0xe7, 0xbe, 0x55, 0x51, 0x38, 0x7a,
  53. 0x98, 0xba, 0x97, 0x7c, 0x73, 0x2d, 0x08, 0x0d,
  54. 0xcb, 0x0f, 0x29, 0xa0, 0x48, 0xe3, 0x65, 0x69,
  55. 0x12, 0xc6, 0x53, 0x3e, 0x32, 0xee, 0x7a, 0xed,
  56. 0x29, 0xb7, 0x21, 0x76, 0x9c, 0xe6, 0x4e, 0x43,
  57. 0xd5, 0x71, 0x33, 0xb0, 0x74, 0xd8, 0x39, 0xd5,
  58. 0x31, 0xed, 0x1f, 0x28, 0x51, 0x0a, 0xfb, 0x45,
  59. 0xac, 0xe1, 0x0a, 0x1f, 0x4b, 0x79, 0x4d, 0x6f,
  60. },
  61. },
  62. {
  63. name: "IETF Draft: TC2: Single bit in key set. All zero IV.",
  64. key: []byte{
  65. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. },
  70. iv: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  71. stream: []byte{
  72. 0xc5, 0xd3, 0x0a, 0x7c, 0xe1, 0xec, 0x11, 0x93,
  73. 0x78, 0xc8, 0x4f, 0x48, 0x7d, 0x77, 0x5a, 0x85,
  74. 0x42, 0xf1, 0x3e, 0xce, 0x23, 0x8a, 0x94, 0x55,
  75. 0xe8, 0x22, 0x9e, 0x88, 0x8d, 0xe8, 0x5b, 0xbd,
  76. 0x29, 0xeb, 0x63, 0xd0, 0xa1, 0x7a, 0x5b, 0x99,
  77. 0x9b, 0x52, 0xda, 0x22, 0xbe, 0x40, 0x23, 0xeb,
  78. 0x07, 0x62, 0x0a, 0x54, 0xf6, 0xfa, 0x6a, 0xd8,
  79. 0x73, 0x7b, 0x71, 0xeb, 0x04, 0x64, 0xda, 0xc0,
  80. 0x10, 0xf6, 0x56, 0xe6, 0xd1, 0xfd, 0x55, 0x05,
  81. 0x3e, 0x50, 0xc4, 0x87, 0x5c, 0x99, 0x30, 0xa3,
  82. 0x3f, 0x6d, 0x02, 0x63, 0xbd, 0x14, 0xdf, 0xd6,
  83. 0xab, 0x8c, 0x70, 0x52, 0x1c, 0x19, 0x33, 0x8b,
  84. 0x23, 0x08, 0xb9, 0x5c, 0xf8, 0xd0, 0xbb, 0x7d,
  85. 0x20, 0x2d, 0x21, 0x02, 0x78, 0x0e, 0xa3, 0x52,
  86. 0x8f, 0x1c, 0xb4, 0x85, 0x60, 0xf7, 0x6b, 0x20,
  87. 0xf3, 0x82, 0xb9, 0x42, 0x50, 0x0f, 0xce, 0xac,
  88. },
  89. },
  90. {
  91. name: "IETF Draft: TC3: Single bit in IV set. All zero key.",
  92. key: []byte{
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. },
  98. iv: []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  99. stream: []byte{
  100. 0xef, 0x3f, 0xdf, 0xd6, 0xc6, 0x15, 0x78, 0xfb,
  101. 0xf5, 0xcf, 0x35, 0xbd, 0x3d, 0xd3, 0x3b, 0x80,
  102. 0x09, 0x63, 0x16, 0x34, 0xd2, 0x1e, 0x42, 0xac,
  103. 0x33, 0x96, 0x0b, 0xd1, 0x38, 0xe5, 0x0d, 0x32,
  104. 0x11, 0x1e, 0x4c, 0xaf, 0x23, 0x7e, 0xe5, 0x3c,
  105. 0xa8, 0xad, 0x64, 0x26, 0x19, 0x4a, 0x88, 0x54,
  106. 0x5d, 0xdc, 0x49, 0x7a, 0x0b, 0x46, 0x6e, 0x7d,
  107. 0x6b, 0xbd, 0xb0, 0x04, 0x1b, 0x2f, 0x58, 0x6b,
  108. 0x53, 0x05, 0xe5, 0xe4, 0x4a, 0xff, 0x19, 0xb2,
  109. 0x35, 0x93, 0x61, 0x44, 0x67, 0x5e, 0xfb, 0xe4,
  110. 0x40, 0x9e, 0xb7, 0xe8, 0xe5, 0xf1, 0x43, 0x0f,
  111. 0x5f, 0x58, 0x36, 0xae, 0xb4, 0x9b, 0xb5, 0x32,
  112. 0x8b, 0x01, 0x7c, 0x4b, 0x9d, 0xc1, 0x1f, 0x8a,
  113. 0x03, 0x86, 0x3f, 0xa8, 0x03, 0xdc, 0x71, 0xd5,
  114. 0x72, 0x6b, 0x2b, 0x6b, 0x31, 0xaa, 0x32, 0x70,
  115. 0x8a, 0xfe, 0x5a, 0xf1, 0xd6, 0xb6, 0x90, 0x58,
  116. },
  117. },
  118. {
  119. name: "IETF Draft: TC4: All bits in key and IV are set.",
  120. key: []byte{
  121. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  122. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  123. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  124. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  125. },
  126. iv: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  127. stream: []byte{
  128. 0xd9, 0xbf, 0x3f, 0x6b, 0xce, 0x6e, 0xd0, 0xb5,
  129. 0x42, 0x54, 0x55, 0x77, 0x67, 0xfb, 0x57, 0x44,
  130. 0x3d, 0xd4, 0x77, 0x89, 0x11, 0xb6, 0x06, 0x05,
  131. 0x5c, 0x39, 0xcc, 0x25, 0xe6, 0x74, 0xb8, 0x36,
  132. 0x3f, 0xea, 0xbc, 0x57, 0xfd, 0xe5, 0x4f, 0x79,
  133. 0x0c, 0x52, 0xc8, 0xae, 0x43, 0x24, 0x0b, 0x79,
  134. 0xd4, 0x90, 0x42, 0xb7, 0x77, 0xbf, 0xd6, 0xcb,
  135. 0x80, 0xe9, 0x31, 0x27, 0x0b, 0x7f, 0x50, 0xeb,
  136. 0x5b, 0xac, 0x2a, 0xcd, 0x86, 0xa8, 0x36, 0xc5,
  137. 0xdc, 0x98, 0xc1, 0x16, 0xc1, 0x21, 0x7e, 0xc3,
  138. 0x1d, 0x3a, 0x63, 0xa9, 0x45, 0x13, 0x19, 0xf0,
  139. 0x97, 0xf3, 0xb4, 0xd6, 0xda, 0xb0, 0x77, 0x87,
  140. 0x19, 0x47, 0x7d, 0x24, 0xd2, 0x4b, 0x40, 0x3a,
  141. 0x12, 0x24, 0x1d, 0x7c, 0xca, 0x06, 0x4f, 0x79,
  142. 0x0f, 0x1d, 0x51, 0xcc, 0xaf, 0xf6, 0xb1, 0x66,
  143. 0x7d, 0x4b, 0xbc, 0xa1, 0x95, 0x8c, 0x43, 0x06,
  144. },
  145. },
  146. {
  147. name: "IETF Draft: TC5: Every even bit set in key and IV.",
  148. key: []byte{
  149. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  150. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  151. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  152. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  153. },
  154. iv: []byte{0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55},
  155. stream: []byte{
  156. 0xbe, 0xa9, 0x41, 0x1a, 0xa4, 0x53, 0xc5, 0x43,
  157. 0x4a, 0x5a, 0xe8, 0xc9, 0x28, 0x62, 0xf5, 0x64,
  158. 0x39, 0x68, 0x55, 0xa9, 0xea, 0x6e, 0x22, 0xd6,
  159. 0xd3, 0xb5, 0x0a, 0xe1, 0xb3, 0x66, 0x33, 0x11,
  160. 0xa4, 0xa3, 0x60, 0x6c, 0x67, 0x1d, 0x60, 0x5c,
  161. 0xe1, 0x6c, 0x3a, 0xec, 0xe8, 0xe6, 0x1e, 0xa1,
  162. 0x45, 0xc5, 0x97, 0x75, 0x01, 0x7b, 0xee, 0x2f,
  163. 0xa6, 0xf8, 0x8a, 0xfc, 0x75, 0x80, 0x69, 0xf7,
  164. 0xe0, 0xb8, 0xf6, 0x76, 0xe6, 0x44, 0x21, 0x6f,
  165. 0x4d, 0x2a, 0x34, 0x22, 0xd7, 0xfa, 0x36, 0xc6,
  166. 0xc4, 0x93, 0x1a, 0xca, 0x95, 0x0e, 0x9d, 0xa4,
  167. 0x27, 0x88, 0xe6, 0xd0, 0xb6, 0xd1, 0xcd, 0x83,
  168. 0x8e, 0xf6, 0x52, 0xe9, 0x7b, 0x14, 0x5b, 0x14,
  169. 0x87, 0x1e, 0xae, 0x6c, 0x68, 0x04, 0xc7, 0x00,
  170. 0x4d, 0xb5, 0xac, 0x2f, 0xce, 0x4c, 0x68, 0xc7,
  171. 0x26, 0xd0, 0x04, 0xb1, 0x0f, 0xca, 0xba, 0x86,
  172. },
  173. },
  174. {
  175. name: "IETF Draft: TC6: Every odd bit set in key and IV.",
  176. key: []byte{
  177. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  178. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  179. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  180. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  181. },
  182. iv: []byte{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa},
  183. stream: []byte{
  184. 0x9a, 0xa2, 0xa9, 0xf6, 0x56, 0xef, 0xde, 0x5a,
  185. 0xa7, 0x59, 0x1c, 0x5f, 0xed, 0x4b, 0x35, 0xae,
  186. 0xa2, 0x89, 0x5d, 0xec, 0x7c, 0xb4, 0x54, 0x3b,
  187. 0x9e, 0x9f, 0x21, 0xf5, 0xe7, 0xbc, 0xbc, 0xf3,
  188. 0xc4, 0x3c, 0x74, 0x8a, 0x97, 0x08, 0x88, 0xf8,
  189. 0x24, 0x83, 0x93, 0xa0, 0x9d, 0x43, 0xe0, 0xb7,
  190. 0xe1, 0x64, 0xbc, 0x4d, 0x0b, 0x0f, 0xb2, 0x40,
  191. 0xa2, 0xd7, 0x21, 0x15, 0xc4, 0x80, 0x89, 0x06,
  192. 0x72, 0x18, 0x44, 0x89, 0x44, 0x05, 0x45, 0xd0,
  193. 0x21, 0xd9, 0x7e, 0xf6, 0xb6, 0x93, 0xdf, 0xe5,
  194. 0xb2, 0xc1, 0x32, 0xd4, 0x7e, 0x6f, 0x04, 0x1c,
  195. 0x90, 0x63, 0x65, 0x1f, 0x96, 0xb6, 0x23, 0xe6,
  196. 0x2a, 0x11, 0x99, 0x9a, 0x23, 0xb6, 0xf7, 0xc4,
  197. 0x61, 0xb2, 0x15, 0x30, 0x26, 0xad, 0x5e, 0x86,
  198. 0x6a, 0x2e, 0x59, 0x7e, 0xd0, 0x7b, 0x84, 0x01,
  199. 0xde, 0xc6, 0x3a, 0x09, 0x34, 0xc6, 0xb2, 0xa9,
  200. },
  201. },
  202. {
  203. name: "IETF Draft: TC7: Sequence patterns in key and IV.",
  204. key: []byte{
  205. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  206. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  207. 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
  208. 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
  209. },
  210. iv: []byte{0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78},
  211. stream: []byte{
  212. 0x9f, 0xad, 0xf4, 0x09, 0xc0, 0x08, 0x11, 0xd0,
  213. 0x04, 0x31, 0xd6, 0x7e, 0xfb, 0xd8, 0x8f, 0xba,
  214. 0x59, 0x21, 0x8d, 0x5d, 0x67, 0x08, 0xb1, 0xd6,
  215. 0x85, 0x86, 0x3f, 0xab, 0xbb, 0x0e, 0x96, 0x1e,
  216. 0xea, 0x48, 0x0f, 0xd6, 0xfb, 0x53, 0x2b, 0xfd,
  217. 0x49, 0x4b, 0x21, 0x51, 0x01, 0x50, 0x57, 0x42,
  218. 0x3a, 0xb6, 0x0a, 0x63, 0xfe, 0x4f, 0x55, 0xf7,
  219. 0xa2, 0x12, 0xe2, 0x16, 0x7c, 0xca, 0xb9, 0x31,
  220. 0xfb, 0xfd, 0x29, 0xcf, 0x7b, 0xc1, 0xd2, 0x79,
  221. 0xed, 0xdf, 0x25, 0xdd, 0x31, 0x6b, 0xb8, 0x84,
  222. 0x3d, 0x6e, 0xde, 0xe0, 0xbd, 0x1e, 0xf1, 0x21,
  223. 0xd1, 0x2f, 0xa1, 0x7c, 0xbc, 0x2c, 0x57, 0x4c,
  224. 0xcc, 0xab, 0x5e, 0x27, 0x51, 0x67, 0xb0, 0x8b,
  225. 0xd6, 0x86, 0xf8, 0xa0, 0x9d, 0xf8, 0x7e, 0xc3,
  226. 0xff, 0xb3, 0x53, 0x61, 0xb9, 0x4e, 0xbf, 0xa1,
  227. 0x3f, 0xec, 0x0e, 0x48, 0x89, 0xd1, 0x8d, 0xa5,
  228. },
  229. },
  230. {
  231. name: "IETF Draft: TC8: key: 'All your base are belong to us!, IV: 'IETF2013'",
  232. key: []byte{
  233. 0xc4, 0x6e, 0xc1, 0xb1, 0x8c, 0xe8, 0xa8, 0x78,
  234. 0x72, 0x5a, 0x37, 0xe7, 0x80, 0xdf, 0xb7, 0x35,
  235. 0x1f, 0x68, 0xed, 0x2e, 0x19, 0x4c, 0x79, 0xfb,
  236. 0xc6, 0xae, 0xbe, 0xe1, 0xa6, 0x67, 0x97, 0x5d,
  237. },
  238. iv: []byte{0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21},
  239. stream: []byte{
  240. 0xf6, 0x3a, 0x89, 0xb7, 0x5c, 0x22, 0x71, 0xf9,
  241. 0x36, 0x88, 0x16, 0x54, 0x2b, 0xa5, 0x2f, 0x06,
  242. 0xed, 0x49, 0x24, 0x17, 0x92, 0x30, 0x2b, 0x00,
  243. 0xb5, 0xe8, 0xf8, 0x0a, 0xe9, 0xa4, 0x73, 0xaf,
  244. 0xc2, 0x5b, 0x21, 0x8f, 0x51, 0x9a, 0xf0, 0xfd,
  245. 0xd4, 0x06, 0x36, 0x2e, 0x8d, 0x69, 0xde, 0x7f,
  246. 0x54, 0xc6, 0x04, 0xa6, 0xe0, 0x0f, 0x35, 0x3f,
  247. 0x11, 0x0f, 0x77, 0x1b, 0xdc, 0xa8, 0xab, 0x92,
  248. 0xe5, 0xfb, 0xc3, 0x4e, 0x60, 0xa1, 0xd9, 0xa9,
  249. 0xdb, 0x17, 0x34, 0x5b, 0x0a, 0x40, 0x27, 0x36,
  250. 0x85, 0x3b, 0xf9, 0x10, 0xb0, 0x60, 0xbd, 0xf1,
  251. 0xf8, 0x97, 0xb6, 0x29, 0x0f, 0x01, 0xd1, 0x38,
  252. 0xae, 0x2c, 0x4c, 0x90, 0x22, 0x5b, 0xa9, 0xea,
  253. 0x14, 0xd5, 0x18, 0xf5, 0x59, 0x29, 0xde, 0xa0,
  254. 0x98, 0xca, 0x7a, 0x6c, 0xcf, 0xe6, 0x12, 0x27,
  255. 0x05, 0x3c, 0x84, 0xe4, 0x9a, 0x4a, 0x33, 0x32,
  256. },
  257. },
  258. {
  259. name: "XChaCha20 Test",
  260. key: []byte{
  261. 0x54, 0x68, 0x65, 0x20, 0x70, 0x75, 0x72, 0x70,
  262. 0x6f, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74,
  263. 0x68, 0x65, 0x20, 0x70, 0x69, 0x73, 0x74, 0x6f,
  264. 0x6c, 0x20, 0x69, 0x73, 0x20, 0x74, 0x6f, 0x20,
  265. },
  266. iv: []byte{
  267. 0x73, 0x74, 0x6f, 0x70, 0x20, 0x61, 0x20, 0x66,
  268. 0x69, 0x67, 0x68, 0x74, 0x20, 0x74, 0x68, 0x61,
  269. 0x74, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x62, 0x6f,
  270. },
  271. stream: []byte{
  272. 0xfa, 0x27, 0xe8, 0x54, 0xba, 0x9f, 0x74, 0xf0,
  273. 0x7f, 0xa3, 0x85, 0x03, 0x65, 0x1b, 0xc9, 0xbf,
  274. 0xe8, 0x80, 0xed, 0x2b, 0xf7, 0x20, 0xed, 0xc5,
  275. 0xb4, 0x82, 0xc9, 0xea, 0x95, 0x32, 0x59, 0x20,
  276. 0xb9, 0xf5, 0x86, 0x93, 0xf1, 0xf7, 0xa6, 0x67,
  277. 0x4e, 0xc4, 0xf3, 0xfa, 0x93, 0x14, 0x4d, 0xe2,
  278. 0xe9, 0x6d, 0x0d, 0x4a, 0xe7, 0x70, 0x32, 0xae,
  279. 0x88, 0xb7, 0x58, 0x3b, 0xd5, 0x5f, 0x16, 0xa6,
  280. 0xd5, 0x0e, 0x8d, 0x53, 0x0c, 0x60, 0x8e, 0x41,
  281. 0x5c, 0xda, 0x0c, 0xe9, 0xc7, 0xde, 0xc6, 0x71,
  282. 0x9d, 0x8a, 0xe5, 0xcb, 0xde, 0x45, 0x5b, 0x98,
  283. 0xf6, 0x57, 0xca, 0x9d, 0xd1, 0xa1, 0xe3, 0x5c,
  284. 0x01, 0xe6, 0x29, 0x38, 0x9e, 0xf5, 0x97, 0x38,
  285. 0x75, 0x61, 0x0e, 0x71, 0xfc, 0xd1, 0xa1, 0x11,
  286. 0x29, 0x47, 0x37, 0x6b, 0xac, 0x09, 0xd6, 0xee,
  287. 0xf7, 0x61, 0x62, 0x24, 0x00, 0x58, 0xac, 0x1d,
  288. 0xcf, 0x42, 0xef, 0x50, 0xfa, 0x54, 0x14, 0x49,
  289. 0x31, 0xd2, 0x62,
  290. },
  291. },
  292. {
  293. name: "RFC 7539 Test Vector (96 bit nonce)",
  294. key: []byte{
  295. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  296. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  297. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  298. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  299. },
  300. iv: []byte{
  301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a,
  302. 0x00, 0x00, 0x00, 0x00,
  303. },
  304. stream: []byte{
  305. 0x22, 0x4f, 0x51, 0xf3, 0x40, 0x1b, 0xd9, 0xe1,
  306. 0x2f, 0xde, 0x27, 0x6f, 0xb8, 0x63, 0x1d, 0xed,
  307. 0x8c, 0x13, 0x1f, 0x82, 0x3d, 0x2c, 0x06, 0xe2,
  308. 0x7e, 0x4f, 0xca, 0xec, 0x9e, 0xf3, 0xcf, 0x78,
  309. 0x8a, 0x3b, 0x0a, 0xa3, 0x72, 0x60, 0x0a, 0x92,
  310. 0xb5, 0x79, 0x74, 0xcd, 0xed, 0x2b, 0x93, 0x34,
  311. 0x79, 0x4c, 0xba, 0x40, 0xc6, 0x3e, 0x34, 0xcd,
  312. 0xea, 0x21, 0x2c, 0x4c, 0xf0, 0x7d, 0x41, 0xb7,
  313. 0x69, 0xa6, 0x74, 0x9f, 0x3f, 0x63, 0x0f, 0x41,
  314. 0x22, 0xca, 0xfe, 0x28, 0xec, 0x4d, 0xc4, 0x7e,
  315. 0x26, 0xd4, 0x34, 0x6d, 0x70, 0xb9, 0x8c, 0x73,
  316. 0xf3, 0xe9, 0xc5, 0x3a, 0xc4, 0x0c, 0x59, 0x45,
  317. 0x39, 0x8b, 0x6e, 0xda, 0x1a, 0x83, 0x2c, 0x89,
  318. 0xc1, 0x67, 0xea, 0xcd, 0x90, 0x1d, 0x7e, 0x2b,
  319. 0xf3, 0x63,
  320. },
  321. seekOffset: 1,
  322. },
  323. }
  324. func TestChaCha20(t *testing.T) {
  325. for _, v := range supportedImpls {
  326. testWithImpl := func(t *testing.T, impl api.Implementation) {
  327. oldImpl := activeImpl
  328. defer func() {
  329. activeImpl = oldImpl
  330. }()
  331. activeImpl = impl
  332. t.Run("Basic", doTestBasic)
  333. t.Run("TestVectors", doTestVectors)
  334. }
  335. t.Run(v.Name(), func(t *testing.T) {
  336. testWithImpl(t, v)
  337. })
  338. }
  339. }
  340. func doTestBasic(t *testing.T) {
  341. t.Run("RoundTrip", doTestBasicRoundTrip)
  342. t.Run("Counter", doTestBasicCounter)
  343. t.Run("IETFCounter", doTestBasicIETFCounter)
  344. t.Run("Incremental", doTestBasicIncremental)
  345. }
  346. func doTestBasicRoundTrip(t *testing.T) {
  347. require := require.New(t)
  348. var (
  349. key [KeySize]byte
  350. nonce [NonceSize]byte
  351. )
  352. c, err := New(key[:], nonce[:])
  353. require.NoError(err, "New")
  354. plaintext := []byte("The smallest minority on earth is the individual. Those who deny individual rights cannot claim to be defenders of minorities.")
  355. ciphertext := make([]byte, len(plaintext))
  356. c.XORKeyStream(ciphertext, plaintext)
  357. require.NotEqual(plaintext, ciphertext, "XORKeyStream - output")
  358. err = c.Seek(0)
  359. require.NoError(err, "Seek")
  360. c.XORKeyStream(ciphertext, ciphertext)
  361. require.Equal(plaintext, ciphertext, "XORKeyStream - round trip output")
  362. }
  363. func doTestBasicCounter(t *testing.T) {
  364. require := require.New(t)
  365. var (
  366. key [KeySize]byte
  367. nonce [NonceSize]byte
  368. block, block2 [api.BlockSize]byte
  369. )
  370. c, err := New(key[:], nonce[:])
  371. require.NoError(err, "New")
  372. c.KeyStream(block[:]) // Block: 0
  373. err = c.Seek(math.MaxUint32 + 1)
  374. require.NoError(err, "Seek")
  375. c.KeyStream(block2[:]) // Block: 0x100000000
  376. require.NotEqual(block, block2, "KeyStream - 32 bit counter would wrap")
  377. }
  378. func doTestBasicIETFCounter(t *testing.T) {
  379. require := require.New(t)
  380. var (
  381. key [KeySize]byte
  382. nonce [INonceSize]byte
  383. block [api.BlockSize]byte
  384. )
  385. c, err := New(key[:], nonce[:])
  386. require.NoError(err, "New")
  387. err = c.Seek(math.MaxUint32 - 1)
  388. require.NoError(err, "Seek")
  389. c.KeyStream(block[:])
  390. require.Panics(func() {
  391. c.KeyStream(block[:])
  392. }, "KeyStream - counter would wrap")
  393. }
  394. func doTestBasicIncremental(t *testing.T) {
  395. require := require.New(t)
  396. var (
  397. key [KeySize]byte
  398. nonce [NonceSize]byte
  399. buf [2048]byte
  400. expectedDigest = []byte{
  401. 0xcf, 0xd6, 0xe9, 0x49, 0x22, 0x5b, 0x85, 0x4f,
  402. 0xe0, 0x49, 0x46, 0x49, 0x1e, 0x69, 0x35, 0xff,
  403. 0x05, 0xff, 0x98, 0x3d, 0x15, 0x54, 0xbc, 0x88,
  404. 0x5b, 0xca, 0x0e, 0xc8, 0x08, 0x2d, 0xd5, 0xb8,
  405. }
  406. )
  407. c, err := New(key[:], nonce[:])
  408. require.NoError(err, "New")
  409. h := sha512.New512_256()
  410. for i := 1; i <= 2048; i++ {
  411. c.KeyStream(buf[:i])
  412. _, _ = h.Write(buf[:i])
  413. }
  414. digest := h.Sum(nil)
  415. require.Equal(expectedDigest, digest, "KeyStream digest matches")
  416. }
  417. func doTestVectors(t *testing.T) {
  418. for _, v := range draftTestVectors {
  419. t.Run(v.name, func(t *testing.T) {
  420. require := require.New(t)
  421. c, err := New(v.key, v.iv)
  422. require.NoError(err, "New")
  423. if v.seekOffset != 0 {
  424. err = c.Seek(v.seekOffset)
  425. require.NoErrorf(err, "Seek(%d)", v.seekOffset)
  426. }
  427. out := make([]byte, len(v.stream))
  428. c.XORKeyStream(out, out)
  429. require.EqualValues(v.stream, out, "XORKeyStream")
  430. // Ensure that KeyStream tramples over dst.
  431. _, err = rand.Read(out)
  432. require.NoError(err, "rand.Read")
  433. err = c.Seek(v.seekOffset)
  434. require.NoErrorf(err, "Seek(%d) - resetting", v.seekOffset)
  435. c.KeyStream(out)
  436. require.EqualValues(v.stream, out, "KeyStream")
  437. })
  438. }
  439. }
  440. func BenchmarkChaCha20(b *testing.B) {
  441. for _, v := range supportedImpls {
  442. benchWithImpl := func(b *testing.B, impl api.Implementation) {
  443. oldImpl := activeImpl
  444. defer func() {
  445. activeImpl = oldImpl
  446. }()
  447. activeImpl = impl
  448. for _, vv := range []int{
  449. 1, 8, 32, 64, 576, 1536, 4096, 1024768,
  450. } {
  451. b.Run(strconv.Itoa(vv), func(b *testing.B) {
  452. doBenchN(b, vv)
  453. })
  454. }
  455. }
  456. b.Run(v.Name(), func(b *testing.B) {
  457. benchWithImpl(b, v)
  458. })
  459. }
  460. }
  461. func doBenchN(b *testing.B, n int) {
  462. var (
  463. key [KeySize]byte
  464. nonce [NonceSize]byte
  465. )
  466. s := make([]byte, n)
  467. c, err := New(key[:], nonce[:])
  468. if err != nil {
  469. b.Fatal(err)
  470. }
  471. b.SetBytes(int64(n))
  472. b.ResetTimer()
  473. for i := 0; i < b.N; i++ {
  474. c.XORKeyStream(s, s)
  475. }
  476. }