chacha20_ref.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // chacha20_ref.go - Reference ChaCha20.
  2. //
  3. // To the extent possible under law, Yawning Angel has waived all copyright
  4. // and related or neighboring rights to chacha20, using the Creative
  5. // Commons "CC0" public domain dedication. See LICENSE or
  6. // <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
  7. package chacha20
  8. import (
  9. "encoding/binary"
  10. "math"
  11. "unsafe"
  12. )
  13. func blocksRef(x *[stateSize]uint32, in []byte, out []byte, nrBlocks int, isIetf bool) {
  14. if isIetf {
  15. var totalBlocks uint64
  16. totalBlocks = uint64(x[8]) + uint64(nrBlocks)
  17. if totalBlocks > math.MaxUint32 {
  18. panic("chacha20: Exceeded keystream per nonce limit")
  19. }
  20. }
  21. // This routine ignores x[0]...x[4] in favor the const values since it's
  22. // ever so slightly faster.
  23. for n := 0; n < nrBlocks; n++ {
  24. x0, x1, x2, x3 := sigma0, sigma1, sigma2, sigma3
  25. x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 := x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11], x[12], x[13], x[14], x[15]
  26. for i := chachaRounds; i > 0; i -= 2 {
  27. // quarterround(x, 0, 4, 8, 12)
  28. x0 += x4
  29. x12 ^= x0
  30. x12 = (x12 << 16) | (x12 >> 16)
  31. x8 += x12
  32. x4 ^= x8
  33. x4 = (x4 << 12) | (x4 >> 20)
  34. x0 += x4
  35. x12 ^= x0
  36. x12 = (x12 << 8) | (x12 >> 24)
  37. x8 += x12
  38. x4 ^= x8
  39. x4 = (x4 << 7) | (x4 >> 25)
  40. // quarterround(x, 1, 5, 9, 13)
  41. x1 += x5
  42. x13 ^= x1
  43. x13 = (x13 << 16) | (x13 >> 16)
  44. x9 += x13
  45. x5 ^= x9
  46. x5 = (x5 << 12) | (x5 >> 20)
  47. x1 += x5
  48. x13 ^= x1
  49. x13 = (x13 << 8) | (x13 >> 24)
  50. x9 += x13
  51. x5 ^= x9
  52. x5 = (x5 << 7) | (x5 >> 25)
  53. // quarterround(x, 2, 6, 10, 14)
  54. x2 += x6
  55. x14 ^= x2
  56. x14 = (x14 << 16) | (x14 >> 16)
  57. x10 += x14
  58. x6 ^= x10
  59. x6 = (x6 << 12) | (x6 >> 20)
  60. x2 += x6
  61. x14 ^= x2
  62. x14 = (x14 << 8) | (x14 >> 24)
  63. x10 += x14
  64. x6 ^= x10
  65. x6 = (x6 << 7) | (x6 >> 25)
  66. // quarterround(x, 3, 7, 11, 15)
  67. x3 += x7
  68. x15 ^= x3
  69. x15 = (x15 << 16) | (x15 >> 16)
  70. x11 += x15
  71. x7 ^= x11
  72. x7 = (x7 << 12) | (x7 >> 20)
  73. x3 += x7
  74. x15 ^= x3
  75. x15 = (x15 << 8) | (x15 >> 24)
  76. x11 += x15
  77. x7 ^= x11
  78. x7 = (x7 << 7) | (x7 >> 25)
  79. // quarterround(x, 0, 5, 10, 15)
  80. x0 += x5
  81. x15 ^= x0
  82. x15 = (x15 << 16) | (x15 >> 16)
  83. x10 += x15
  84. x5 ^= x10
  85. x5 = (x5 << 12) | (x5 >> 20)
  86. x0 += x5
  87. x15 ^= x0
  88. x15 = (x15 << 8) | (x15 >> 24)
  89. x10 += x15
  90. x5 ^= x10
  91. x5 = (x5 << 7) | (x5 >> 25)
  92. // quarterround(x, 1, 6, 11, 12)
  93. x1 += x6
  94. x12 ^= x1
  95. x12 = (x12 << 16) | (x12 >> 16)
  96. x11 += x12
  97. x6 ^= x11
  98. x6 = (x6 << 12) | (x6 >> 20)
  99. x1 += x6
  100. x12 ^= x1
  101. x12 = (x12 << 8) | (x12 >> 24)
  102. x11 += x12
  103. x6 ^= x11
  104. x6 = (x6 << 7) | (x6 >> 25)
  105. // quarterround(x, 2, 7, 8, 13)
  106. x2 += x7
  107. x13 ^= x2
  108. x13 = (x13 << 16) | (x13 >> 16)
  109. x8 += x13
  110. x7 ^= x8
  111. x7 = (x7 << 12) | (x7 >> 20)
  112. x2 += x7
  113. x13 ^= x2
  114. x13 = (x13 << 8) | (x13 >> 24)
  115. x8 += x13
  116. x7 ^= x8
  117. x7 = (x7 << 7) | (x7 >> 25)
  118. // quarterround(x, 3, 4, 9, 14)
  119. x3 += x4
  120. x14 ^= x3
  121. x14 = (x14 << 16) | (x14 >> 16)
  122. x9 += x14
  123. x4 ^= x9
  124. x4 = (x4 << 12) | (x4 >> 20)
  125. x3 += x4
  126. x14 ^= x3
  127. x14 = (x14 << 8) | (x14 >> 24)
  128. x9 += x14
  129. x4 ^= x9
  130. x4 = (x4 << 7) | (x4 >> 25)
  131. }
  132. // On amd64 at least, this is a rather big boost.
  133. if useUnsafe {
  134. if in != nil {
  135. inArr := (*[16]uint32)(unsafe.Pointer(&in[n*BlockSize]))
  136. outArr := (*[16]uint32)(unsafe.Pointer(&out[n*BlockSize]))
  137. outArr[0] = inArr[0] ^ (x0 + sigma0)
  138. outArr[1] = inArr[1] ^ (x1 + sigma1)
  139. outArr[2] = inArr[2] ^ (x2 + sigma2)
  140. outArr[3] = inArr[3] ^ (x3 + sigma3)
  141. outArr[4] = inArr[4] ^ (x4 + x[4])
  142. outArr[5] = inArr[5] ^ (x5 + x[5])
  143. outArr[6] = inArr[6] ^ (x6 + x[6])
  144. outArr[7] = inArr[7] ^ (x7 + x[7])
  145. outArr[8] = inArr[8] ^ (x8 + x[8])
  146. outArr[9] = inArr[9] ^ (x9 + x[9])
  147. outArr[10] = inArr[10] ^ (x10 + x[10])
  148. outArr[11] = inArr[11] ^ (x11 + x[11])
  149. outArr[12] = inArr[12] ^ (x12 + x[12])
  150. outArr[13] = inArr[13] ^ (x13 + x[13])
  151. outArr[14] = inArr[14] ^ (x14 + x[14])
  152. outArr[15] = inArr[15] ^ (x15 + x[15])
  153. } else {
  154. outArr := (*[16]uint32)(unsafe.Pointer(&out[n*BlockSize]))
  155. outArr[0] = x0 + sigma0
  156. outArr[1] = x1 + sigma1
  157. outArr[2] = x2 + sigma2
  158. outArr[3] = x3 + sigma3
  159. outArr[4] = x4 + x[4]
  160. outArr[5] = x5 + x[5]
  161. outArr[6] = x6 + x[6]
  162. outArr[7] = x7 + x[7]
  163. outArr[8] = x8 + x[8]
  164. outArr[9] = x9 + x[9]
  165. outArr[10] = x10 + x[10]
  166. outArr[11] = x11 + x[11]
  167. outArr[12] = x12 + x[12]
  168. outArr[13] = x13 + x[13]
  169. outArr[14] = x14 + x[14]
  170. outArr[15] = x15 + x[15]
  171. }
  172. } else {
  173. // Slow path, either the architecture cares about alignment, or is not litt;e endian.
  174. x0 += sigma0
  175. x1 += sigma1
  176. x2 += sigma2
  177. x3 += sigma3
  178. x4 += x[4]
  179. x5 += x[5]
  180. x6 += x[6]
  181. x7 += x[7]
  182. x8 += x[8]
  183. x9 += x[9]
  184. x10 += x[10]
  185. x11 += x[11]
  186. x12 += x[12]
  187. x13 += x[13]
  188. x14 += x[14]
  189. x15 += x[15]
  190. if in != nil {
  191. binary.LittleEndian.PutUint32(out[0:4], binary.LittleEndian.Uint32(in[0:4])^x0)
  192. binary.LittleEndian.PutUint32(out[4:8], binary.LittleEndian.Uint32(in[4:8])^x1)
  193. binary.LittleEndian.PutUint32(out[8:12], binary.LittleEndian.Uint32(in[8:12])^x2)
  194. binary.LittleEndian.PutUint32(out[12:16], binary.LittleEndian.Uint32(in[12:16])^x3)
  195. binary.LittleEndian.PutUint32(out[16:20], binary.LittleEndian.Uint32(in[16:20])^x4)
  196. binary.LittleEndian.PutUint32(out[20:24], binary.LittleEndian.Uint32(in[20:24])^x5)
  197. binary.LittleEndian.PutUint32(out[24:28], binary.LittleEndian.Uint32(in[24:28])^x6)
  198. binary.LittleEndian.PutUint32(out[28:32], binary.LittleEndian.Uint32(in[28:32])^x7)
  199. binary.LittleEndian.PutUint32(out[32:36], binary.LittleEndian.Uint32(in[32:36])^x8)
  200. binary.LittleEndian.PutUint32(out[36:40], binary.LittleEndian.Uint32(in[36:40])^x9)
  201. binary.LittleEndian.PutUint32(out[40:44], binary.LittleEndian.Uint32(in[40:44])^x10)
  202. binary.LittleEndian.PutUint32(out[44:48], binary.LittleEndian.Uint32(in[44:48])^x11)
  203. binary.LittleEndian.PutUint32(out[48:52], binary.LittleEndian.Uint32(in[48:52])^x12)
  204. binary.LittleEndian.PutUint32(out[52:56], binary.LittleEndian.Uint32(in[52:56])^x13)
  205. binary.LittleEndian.PutUint32(out[56:60], binary.LittleEndian.Uint32(in[56:60])^x14)
  206. binary.LittleEndian.PutUint32(out[60:64], binary.LittleEndian.Uint32(in[60:64])^x15)
  207. in = in[BlockSize:]
  208. } else {
  209. binary.LittleEndian.PutUint32(out[0:4], x0)
  210. binary.LittleEndian.PutUint32(out[4:8], x1)
  211. binary.LittleEndian.PutUint32(out[8:12], x2)
  212. binary.LittleEndian.PutUint32(out[12:16], x3)
  213. binary.LittleEndian.PutUint32(out[16:20], x4)
  214. binary.LittleEndian.PutUint32(out[20:24], x5)
  215. binary.LittleEndian.PutUint32(out[24:28], x6)
  216. binary.LittleEndian.PutUint32(out[28:32], x7)
  217. binary.LittleEndian.PutUint32(out[32:36], x8)
  218. binary.LittleEndian.PutUint32(out[36:40], x9)
  219. binary.LittleEndian.PutUint32(out[40:44], x10)
  220. binary.LittleEndian.PutUint32(out[44:48], x11)
  221. binary.LittleEndian.PutUint32(out[48:52], x12)
  222. binary.LittleEndian.PutUint32(out[52:56], x13)
  223. binary.LittleEndian.PutUint32(out[56:60], x14)
  224. binary.LittleEndian.PutUint32(out[60:64], x15)
  225. }
  226. out = out[BlockSize:]
  227. }
  228. // Stoping at 2^70 bytes per nonce is the user's responsibility.
  229. ctr := uint64(x[13])<<32 | uint64(x[12])
  230. ctr++
  231. x[12] = uint32(ctr)
  232. x[13] = uint32(ctr >> 32)
  233. }
  234. }