| 1 | // Use of this source code is governed by a license |
| 2 | // that can be found in the LICENSE file. |
| 3 | |
| 4 | // Package chacha implements some low level functions of the |
| 5 | // ChaCha cipher family. |
| 6 | package chacha |
| 7 | |
| 8 | var constants = [16]byte{ |
| 9 | 0x65, 0x78, 0x70, 0x61, |
| 10 | 0x6e, 0x64, 0x20, 0x33, |
| 11 | 0x32, 0x2d, 0x62, 0x79, |
| 12 | 0x74, 0x65, 0x20, 0x6b, |
| 13 | } |
| 14 | |
| 15 | // Cipher is the ChaCha/X struct. |
| 16 | // X is the number of rounds (e.g. ChaCha20 for 20 rounds) |
| 17 | type Cipher struct { |
| 18 | state, block [64]byte |
| 19 | off int |
| 20 | rounds int |
| 21 | } |
| 22 | |
| 23 | // Sets the counter of the cipher. |
| 24 | // Notice that this function skips the unused |
| 25 | // keystream of the current 64 byte block. |
| 26 | func (c *Cipher) SetCounter(ctr uint32) { |
| 27 | c.state[48] = byte(ctr) |
| 28 | c.state[49] = byte(ctr >> 8) |
| 29 | c.state[50] = byte(ctr >> 16) |
| 30 | c.state[51] = byte(ctr >> 24) |
| 31 | c.off = 0 |
| 32 | } |