chacha_generic.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 implements the ChaCha20 and XChaCha20 encryption algorithms
  5. // as specified in RFC 8439 and draft-irtf-cfrg-xchacha-01.
  6. package chacha20
  7. import (
  8. "crypto/cipher"
  9. "encoding/binary"
  10. "errors"
  11. "math/bits"
  12. "golang.org/x/crypto/internal/alias"
  13. )
  14. const (
  15. // KeySize is the size of the key used by this cipher, in bytes.
  16. KeySize = 32
  17. // NonceSize is the size of the nonce used with the standard variant of this
  18. // cipher, in bytes.
  19. //
  20. // Note that this is too short to be safely generated at random if the same
  21. // key is reused more than 2³² times.
  22. NonceSize = 12
  23. // NonceSizeX is the size of the nonce used with the XChaCha20 variant of
  24. // this cipher, in bytes.
  25. NonceSizeX = 24
  26. )
  27. // Cipher is a stateful instance of ChaCha20 or XChaCha20 using a particular key
  28. // and nonce. A *Cipher implements the cipher.Stream interface.
  29. type Cipher struct {
  30. // The ChaCha20 state is 16 words: 4 constant, 8 of key, 1 of counter
  31. // (incremented after each block), and 3 of nonce.
  32. key [8]uint32
  33. counter uint32
  34. nonce [3]uint32
  35. // The last len bytes of buf are leftover key stream bytes from the previous
  36. // XORKeyStream invocation. The size of buf depends on how many blocks are
  37. // computed at a time by xorKeyStreamBlocks.
  38. buf [bufSize]byte
  39. len int
  40. // overflow is set when the counter overflowed, no more blocks can be
  41. // generated, and the next XORKeyStream call should panic.
  42. overflow bool
  43. // The counter-independent results of the first round are cached after they
  44. // are computed the first time.
  45. precompDone bool
  46. p1, p5, p9, p13 uint32
  47. p2, p6, p10, p14 uint32
  48. p3, p7, p11, p15 uint32
  49. }
  50. var _ cipher.Stream = (*Cipher)(nil)
  51. // NewUnauthenticatedCipher creates a new ChaCha20 stream cipher with the given
  52. // 32 bytes key and a 12 or 24 bytes nonce. If a nonce of 24 bytes is provided,
  53. // the XChaCha20 construction will be used. It returns an error if key or nonce
  54. // have any other length.
  55. //
  56. // Note that ChaCha20, like all stream ciphers, is not authenticated and allows
  57. // attackers to silently tamper with the plaintext. For this reason, it is more
  58. // appropriate as a building block than as a standalone encryption mechanism.
  59. // Instead, consider using package golang.org/x/crypto/chacha20poly1305.
  60. func NewUnauthenticatedCipher(key, nonce []byte) (*Cipher, error) {
  61. // This function is split into a wrapper so that the Cipher allocation will
  62. // be inlined, and depending on how the caller uses the return value, won't
  63. // escape to the heap.
  64. c := &Cipher{}
  65. return newUnauthenticatedCipher(c, key, nonce)
  66. }
  67. func newUnauthenticatedCipher(c *Cipher, key, nonce []byte) (*Cipher, error) {
  68. if len(key) != KeySize {
  69. return nil, errors.New("chacha20: wrong key size")
  70. }
  71. if len(nonce) == NonceSizeX {
  72. // XChaCha20 uses the ChaCha20 core to mix 16 bytes of the nonce into a
  73. // derived key, allowing it to operate on a nonce of 24 bytes. See
  74. // draft-irtf-cfrg-xchacha-01, Section 2.3.
  75. key, _ = HChaCha20(key, nonce[0:16])
  76. cNonce := make([]byte, NonceSize)
  77. copy(cNonce[4:12], nonce[16:24])
  78. nonce = cNonce
  79. } else if len(nonce) != NonceSize {
  80. return nil, errors.New("chacha20: wrong nonce size")
  81. }
  82. key, nonce = key[:KeySize], nonce[:NonceSize] // bounds check elimination hint
  83. c.key = [8]uint32{
  84. binary.LittleEndian.Uint32(key[0:4]),
  85. binary.LittleEndian.Uint32(key[4:8]),
  86. binary.LittleEndian.Uint32(key[8:12]),
  87. binary.LittleEndian.Uint32(key[12:16]),
  88. binary.LittleEndian.Uint32(key[16:20]),
  89. binary.LittleEndian.Uint32(key[20:24]),
  90. binary.LittleEndian.Uint32(key[24:28]),
  91. binary.LittleEndian.Uint32(key[28:32]),
  92. }
  93. c.nonce = [3]uint32{
  94. binary.LittleEndian.Uint32(nonce[0:4]),
  95. binary.LittleEndian.Uint32(nonce[4:8]),
  96. binary.LittleEndian.Uint32(nonce[8:12]),
  97. }
  98. return c, nil
  99. }
  100. // The constant first 4 words of the ChaCha20 state.
  101. const (
  102. j0 uint32 = 0x61707865 // expa
  103. j1 uint32 = 0x3320646e // nd 3
  104. j2 uint32 = 0x79622d32 // 2-by
  105. j3 uint32 = 0x6b206574 // te k
  106. )
  107. const blockSize = 64
  108. // quarterRound is the core of ChaCha20. It shuffles the bits of 4 state words.
  109. // It's executed 4 times for each of the 20 ChaCha20 rounds, operating on all 16
  110. // words each round, in columnar or diagonal groups of 4 at a time.
  111. func quarterRound(a, b, c, d uint32) (uint32, uint32, uint32, uint32) {
  112. a += b
  113. d ^= a
  114. d = bits.RotateLeft32(d, 16)
  115. c += d
  116. b ^= c
  117. b = bits.RotateLeft32(b, 12)
  118. a += b
  119. d ^= a
  120. d = bits.RotateLeft32(d, 8)
  121. c += d
  122. b ^= c
  123. b = bits.RotateLeft32(b, 7)
  124. return a, b, c, d
  125. }
  126. // SetCounter sets the Cipher counter. The next invocation of XORKeyStream will
  127. // behave as if (64 * counter) bytes had been encrypted so far.
  128. //
  129. // To prevent accidental counter reuse, SetCounter panics if counter is less
  130. // than the current value.
  131. //
  132. // Note that the execution time of XORKeyStream is not independent of the
  133. // counter value.
  134. func (s *Cipher) SetCounter(counter uint32) {
  135. // Internally, s may buffer multiple blocks, which complicates this
  136. // implementation slightly. When checking whether the counter has rolled
  137. // back, we must use both s.counter and s.len to determine how many blocks
  138. // we have already output.
  139. outputCounter := s.counter - uint32(s.len)/blockSize
  140. if s.overflow || counter < outputCounter {
  141. panic("chacha20: SetCounter attempted to rollback counter")
  142. }
  143. // In the general case, we set the new counter value and reset s.len to 0,
  144. // causing the next call to XORKeyStream to refill the buffer. However, if
  145. // we're advancing within the existing buffer, we can save work by simply
  146. // setting s.len.
  147. if counter < s.counter {
  148. s.len = int(s.counter-counter) * blockSize
  149. } else {
  150. s.counter = counter
  151. s.len = 0
  152. }
  153. }
  154. // XORKeyStream XORs each byte in the given slice with a byte from the
  155. // cipher's key stream. Dst and src must overlap entirely or not at all.
  156. //
  157. // If len(dst) < len(src), XORKeyStream will panic. It is acceptable
  158. // to pass a dst bigger than src, and in that case, XORKeyStream will
  159. // only update dst[:len(src)] and will not touch the rest of dst.
  160. //
  161. // Multiple calls to XORKeyStream behave as if the concatenation of
  162. // the src buffers was passed in a single run. That is, Cipher
  163. // maintains state and does not reset at each XORKeyStream call.
  164. func (s *Cipher) XORKeyStream(dst, src []byte) {
  165. if len(src) == 0 {
  166. return
  167. }
  168. if len(dst) < len(src) {
  169. panic("chacha20: output smaller than input")
  170. }
  171. dst = dst[:len(src)]
  172. if alias.InexactOverlap(dst, src) {
  173. panic("chacha20: invalid buffer overlap")
  174. }
  175. // First, drain any remaining key stream from a previous XORKeyStream.
  176. if s.len != 0 {
  177. keyStream := s.buf[bufSize-s.len:]
  178. if len(src) < len(keyStream) {
  179. keyStream = keyStream[:len(src)]
  180. }
  181. _ = src[len(keyStream)-1] // bounds check elimination hint
  182. for i, b := range keyStream {
  183. dst[i] = src[i] ^ b
  184. }
  185. s.len -= len(keyStream)
  186. dst, src = dst[len(keyStream):], src[len(keyStream):]
  187. }
  188. if len(src) == 0 {
  189. return
  190. }
  191. // If we'd need to let the counter overflow and keep generating output,
  192. // panic immediately. If instead we'd only reach the last block, remember
  193. // not to generate any more output after the buffer is drained.
  194. numBlocks := (uint64(len(src)) + blockSize - 1) / blockSize
  195. if s.overflow || uint64(s.counter)+numBlocks > 1<<32 {
  196. panic("chacha20: counter overflow")
  197. } else if uint64(s.counter)+numBlocks == 1<<32 {
  198. s.overflow = true
  199. }
  200. // xorKeyStreamBlocks implementations expect input lengths that are a
  201. // multiple of bufSize. Platform-specific ones process multiple blocks at a
  202. // time, so have bufSizes that are a multiple of blockSize.
  203. full := len(src) - len(src)%bufSize
  204. if full > 0 {
  205. s.xorKeyStreamBlocks(dst[:full], src[:full])
  206. }
  207. dst, src = dst[full:], src[full:]
  208. // If using a multi-block xorKeyStreamBlocks would overflow, use the generic
  209. // one that does one block at a time.
  210. const blocksPerBuf = bufSize / blockSize
  211. if uint64(s.counter)+blocksPerBuf > 1<<32 {
  212. s.buf = [bufSize]byte{}
  213. numBlocks := (len(src) + blockSize - 1) / blockSize
  214. buf := s.buf[bufSize-numBlocks*blockSize:]
  215. copy(buf, src)
  216. s.xorKeyStreamBlocksGeneric(buf, buf)
  217. s.len = len(buf) - copy(dst, buf)
  218. return
  219. }
  220. // If we have a partial (multi-)block, pad it for xorKeyStreamBlocks, and
  221. // keep the leftover keystream for the next XORKeyStream invocation.
  222. if len(src) > 0 {
  223. s.buf = [bufSize]byte{}
  224. copy(s.buf[:], src)
  225. s.xorKeyStreamBlocks(s.buf[:], s.buf[:])
  226. s.len = bufSize - copy(dst, s.buf[:])
  227. }
  228. }
  229. func (s *Cipher) xorKeyStreamBlocksGeneric(dst, src []byte) {
  230. if len(dst) != len(src) || len(dst)%blockSize != 0 {
  231. panic("chacha20: internal error: wrong dst and/or src length")
  232. }
  233. // To generate each block of key stream, the initial cipher state
  234. // (represented below) is passed through 20 rounds of shuffling,
  235. // alternatively applying quarterRounds by columns (like 1, 5, 9, 13)
  236. // or by diagonals (like 1, 6, 11, 12).
  237. //
  238. // 0:cccccccc 1:cccccccc 2:cccccccc 3:cccccccc
  239. // 4:kkkkkkkk 5:kkkkkkkk 6:kkkkkkkk 7:kkkkkkkk
  240. // 8:kkkkkkkk 9:kkkkkkkk 10:kkkkkkkk 11:kkkkkkkk
  241. // 12:bbbbbbbb 13:nnnnnnnn 14:nnnnnnnn 15:nnnnnnnn
  242. //
  243. // c=constant k=key b=blockcount n=nonce
  244. var (
  245. c0, c1, c2, c3 = j0, j1, j2, j3
  246. c4, c5, c6, c7 = s.key[0], s.key[1], s.key[2], s.key[3]
  247. c8, c9, c10, c11 = s.key[4], s.key[5], s.key[6], s.key[7]
  248. _, c13, c14, c15 = s.counter, s.nonce[0], s.nonce[1], s.nonce[2]
  249. )
  250. // Three quarters of the first round don't depend on the counter, so we can
  251. // calculate them here, and reuse them for multiple blocks in the loop, and
  252. // for future XORKeyStream invocations.
  253. if !s.precompDone {
  254. s.p1, s.p5, s.p9, s.p13 = quarterRound(c1, c5, c9, c13)
  255. s.p2, s.p6, s.p10, s.p14 = quarterRound(c2, c6, c10, c14)
  256. s.p3, s.p7, s.p11, s.p15 = quarterRound(c3, c7, c11, c15)
  257. s.precompDone = true
  258. }
  259. // A condition of len(src) > 0 would be sufficient, but this also
  260. // acts as a bounds check elimination hint.
  261. for len(src) >= 64 && len(dst) >= 64 {
  262. // The remainder of the first column round.
  263. fcr0, fcr4, fcr8, fcr12 := quarterRound(c0, c4, c8, s.counter)
  264. // The second diagonal round.
  265. x0, x5, x10, x15 := quarterRound(fcr0, s.p5, s.p10, s.p15)
  266. x1, x6, x11, x12 := quarterRound(s.p1, s.p6, s.p11, fcr12)
  267. x2, x7, x8, x13 := quarterRound(s.p2, s.p7, fcr8, s.p13)
  268. x3, x4, x9, x14 := quarterRound(s.p3, fcr4, s.p9, s.p14)
  269. // The remaining 18 rounds.
  270. for i := 0; i < 9; i++ {
  271. // Column round.
  272. x0, x4, x8, x12 = quarterRound(x0, x4, x8, x12)
  273. x1, x5, x9, x13 = quarterRound(x1, x5, x9, x13)
  274. x2, x6, x10, x14 = quarterRound(x2, x6, x10, x14)
  275. x3, x7, x11, x15 = quarterRound(x3, x7, x11, x15)
  276. // Diagonal round.
  277. x0, x5, x10, x15 = quarterRound(x0, x5, x10, x15)
  278. x1, x6, x11, x12 = quarterRound(x1, x6, x11, x12)
  279. x2, x7, x8, x13 = quarterRound(x2, x7, x8, x13)
  280. x3, x4, x9, x14 = quarterRound(x3, x4, x9, x14)
  281. }
  282. // Add back the initial state to generate the key stream, then
  283. // XOR the key stream with the source and write out the result.
  284. addXor(dst[0:4], src[0:4], x0, c0)
  285. addXor(dst[4:8], src[4:8], x1, c1)
  286. addXor(dst[8:12], src[8:12], x2, c2)
  287. addXor(dst[12:16], src[12:16], x3, c3)
  288. addXor(dst[16:20], src[16:20], x4, c4)
  289. addXor(dst[20:24], src[20:24], x5, c5)
  290. addXor(dst[24:28], src[24:28], x6, c6)
  291. addXor(dst[28:32], src[28:32], x7, c7)
  292. addXor(dst[32:36], src[32:36], x8, c8)
  293. addXor(dst[36:40], src[36:40], x9, c9)
  294. addXor(dst[40:44], src[40:44], x10, c10)
  295. addXor(dst[44:48], src[44:48], x11, c11)
  296. addXor(dst[48:52], src[48:52], x12, s.counter)
  297. addXor(dst[52:56], src[52:56], x13, c13)
  298. addXor(dst[56:60], src[56:60], x14, c14)
  299. addXor(dst[60:64], src[60:64], x15, c15)
  300. s.counter += 1
  301. src, dst = src[blockSize:], dst[blockSize:]
  302. }
  303. }
  304. // HChaCha20 uses the ChaCha20 core to generate a derived key from a 32 bytes
  305. // key and a 16 bytes nonce. It returns an error if key or nonce have any other
  306. // length. It is used as part of the XChaCha20 construction.
  307. func HChaCha20(key, nonce []byte) ([]byte, error) {
  308. // This function is split into a wrapper so that the slice allocation will
  309. // be inlined, and depending on how the caller uses the return value, won't
  310. // escape to the heap.
  311. out := make([]byte, 32)
  312. return hChaCha20(out, key, nonce)
  313. }
  314. func hChaCha20(out, key, nonce []byte) ([]byte, error) {
  315. if len(key) != KeySize {
  316. return nil, errors.New("chacha20: wrong HChaCha20 key size")
  317. }
  318. if len(nonce) != 16 {
  319. return nil, errors.New("chacha20: wrong HChaCha20 nonce size")
  320. }
  321. x0, x1, x2, x3 := j0, j1, j2, j3
  322. x4 := binary.LittleEndian.Uint32(key[0:4])
  323. x5 := binary.LittleEndian.Uint32(key[4:8])
  324. x6 := binary.LittleEndian.Uint32(key[8:12])
  325. x7 := binary.LittleEndian.Uint32(key[12:16])
  326. x8 := binary.LittleEndian.Uint32(key[16:20])
  327. x9 := binary.LittleEndian.Uint32(key[20:24])
  328. x10 := binary.LittleEndian.Uint32(key[24:28])
  329. x11 := binary.LittleEndian.Uint32(key[28:32])
  330. x12 := binary.LittleEndian.Uint32(nonce[0:4])
  331. x13 := binary.LittleEndian.Uint32(nonce[4:8])
  332. x14 := binary.LittleEndian.Uint32(nonce[8:12])
  333. x15 := binary.LittleEndian.Uint32(nonce[12:16])
  334. for i := 0; i < 10; i++ {
  335. // Diagonal round.
  336. x0, x4, x8, x12 = quarterRound(x0, x4, x8, x12)
  337. x1, x5, x9, x13 = quarterRound(x1, x5, x9, x13)
  338. x2, x6, x10, x14 = quarterRound(x2, x6, x10, x14)
  339. x3, x7, x11, x15 = quarterRound(x3, x7, x11, x15)
  340. // Column round.
  341. x0, x5, x10, x15 = quarterRound(x0, x5, x10, x15)
  342. x1, x6, x11, x12 = quarterRound(x1, x6, x11, x12)
  343. x2, x7, x8, x13 = quarterRound(x2, x7, x8, x13)
  344. x3, x4, x9, x14 = quarterRound(x3, x4, x9, x14)
  345. }
  346. _ = out[31] // bounds check elimination hint
  347. binary.LittleEndian.PutUint32(out[0:4], x0)
  348. binary.LittleEndian.PutUint32(out[4:8], x1)
  349. binary.LittleEndian.PutUint32(out[8:12], x2)
  350. binary.LittleEndian.PutUint32(out[12:16], x3)
  351. binary.LittleEndian.PutUint32(out[16:20], x12)
  352. binary.LittleEndian.PutUint32(out[20:24], x13)
  353. binary.LittleEndian.PutUint32(out[24:28], x14)
  354. binary.LittleEndian.PutUint32(out[28:32], x15)
  355. return out, nil
  356. }