| 1 | // Use of this source code is governed by a license |
| 2 | // that can be found in the LICENSE file. |
| 3 | |
| 4 | // Package chacha20 implements the ChaCha stream cipher and |
| 5 | // the ChaCha20Poly1305 AEAD construction described in RFC 7539. |
| 6 | // |
| 7 | // ChaCha20 uses a 32 bit counter and produces 64 byte keystream per |
| 8 | // iteration. Following ChaCha20 can en/decrypt up to 2^32 * 64 byte |
| 9 | // for one key-nonce combination. Notice that one specific key-nonce |
| 10 | // combination must be unique for all time. |
| 11 | package chacha20 |
| 12 | |
| 13 | import ( |
| 14 | "crypto/cipher" |
| 15 | |
| 16 | "github.com/enceve/crypto/chacha20/chacha" |
| 17 | ) |
| 18 | |
| 19 | // The size of the ChaCha20 nonce in bytes. |
| 20 | const NonceSize = 12 |
| 21 | |
| 22 | // XORKeyStream crypts bytes from src to dst using the given key, nonce and counter. Src |
| 23 | // and dst may be the same slice but otherwise should not overlap. If len(dst) < len(src) |
| 24 | // this function panics. |
| 25 | func XORKeyStream(dst, src []byte, nonce *[NonceSize]byte, key *[32]byte, counter uint32) { |
| 26 | chacha.XORKeyStream(dst, src, nonce, key, counter, 20) |
| 27 | } |
| 28 | |
| 29 | // NewCipher returns a new cipher.Stream implementing the ChaCha20 |
| 30 | // stream cipher. The nonce must be unique for one |
| 31 | // key for all time. |
| 32 | func NewCipher(nonce *[NonceSize]byte, key *[32]byte) cipher.Stream { |
| 33 | return chacha.NewCipher(nonce, key, 20) |
| 34 | } |