| 1 |
# RustCrypto: ChaCha20 |
| 2 |
|
| 3 |
[![Crate][crate-image]][crate-link] |
| 4 |
[![Docs][docs-image]][docs-link] |
| 5 |
[![Build Status][build-image]][build-link] |
| 6 |
![Apache2/MIT licensed][license-image] |
| 7 |
![Rust Version][rustc-image] |
| 8 |
[![Project Chat][chat-image]][chat-link] |
| 9 |
[![HAZMAT][hazmat-image]][hazmat-link] |
| 10 |
|
| 11 |
Implementation of the [ChaCha] family of stream ciphers. |
| 12 |
|
| 13 |
ChaCha improves upon the previous [Salsa] family of stream ciphers |
| 14 |
with increased per-round diffusion at no cost to performance. |
| 15 |
|
| 16 |
This crate also contains an implementation of the [XChaCha] family of stream ciphers |
| 17 |
with an extended 192-bit (24-byte) nonce, gated under the `xchacha` Cargo feature, |
| 18 |
and "legacy" (a.k.a "djb") variant with 64-bit nonce, gated under the `legacy` crate feature. |
| 19 |
|
| 20 |
**WARNING:** This implementation internally uses 32-bit counter, |
| 21 |
while the original "legacy" variant implementation uses 64-bit counter. |
| 22 |
In other words, it does not allow encryption of more than 256 GiB of data. |
| 23 |
|
| 24 |
## Security |
| 25 |
|
| 26 |
### ⚠️ Warning: [Hazmat!][hazmat-link] |
| 27 |
|
| 28 |
This crate does not ensure ciphertexts are authentic (i.e. by using a MAC to |
| 29 |
verify ciphertext integrity), which can lead to serious vulnerabilities |
| 30 |
if used incorrectly! |
| 31 |
|
| 32 |
To avoid this, use an [AEAD] mode based on ChaCha20, e.g. [`chacha20poly1305`]. |
| 33 |
See the [RustCrypto/AEADs] repository for more information. |
| 34 |
|
| 35 |
USE AT YOUR OWN RISK! |
| 36 |
|
| 37 |
### Notes |
| 38 |
|
| 39 |
This crate has received one [security audit by NCC Group][NCC-AUDIT], with no significant |
| 40 |
findings. We would like to thank [MobileCoin] for funding the audit. |
| 41 |
|
| 42 |
All implementations contained in the crate (along with the underlying ChaCha20 |
| 43 |
stream cipher itself) are designed to execute in constant time. |
| 44 |
|
| 45 |
## Examples |
| 46 |
|
| 47 |
```rust |
| 48 |
// This example requires `cipher` crate feature |
| 49 |
#[cfg(feature = "cipher")] { |
| 50 |
|
| 51 |
use chacha20::ChaCha20; |
| 52 |
use chacha20::cipher::{KeyIvInit, StreamCipher, StreamCipherSeek}; |
| 53 |
use hex_literal::hex; |
| 54 |
|
| 55 |
let key = [0x42; 32]; |
| 56 |
let nonce = [0x24; 12]; |
| 57 |
let plaintext = hex!("000102030405060708090A0B0C0D0E0F"); |
| 58 |
let ciphertext = hex!("e405626e4f1236b3670ee428332ea20e"); |
| 59 |
|
| 60 |
// Key and IV must be references to the `Array` type. |
| 61 |
// Here we use the `Into` trait to convert arrays into it. |
| 62 |
let mut cipher = ChaCha20::new(&key.into(), &nonce.into()); |
| 63 |
|
| 64 |
let mut buffer = plaintext; |
| 65 |
|
| 66 |
// apply keystream (encrypt) |
| 67 |
cipher.apply_keystream(&mut buffer); |
| 68 |
assert_eq!(buffer, ciphertext); |
| 69 |
|
| 70 |
let ciphertext = buffer; |
| 71 |
|
| 72 |
// ChaCha ciphers support seeking |
| 73 |
cipher.seek(0u32); |
| 74 |
|
| 75 |
// decrypt ciphertext by applying keystream again |
| 76 |
cipher.apply_keystream(&mut buffer); |
| 77 |
assert_eq!(buffer, plaintext); |
| 78 |
|
| 79 |
// stream ciphers can be used with streaming messages |
| 80 |
cipher.seek(0u32); |
| 81 |
for chunk in buffer.chunks_mut(3) { |
| 82 |
cipher.apply_keystream(chunk); |
| 83 |
} |
| 84 |
assert_eq!(buffer, ciphertext); |
| 85 |
} |
| 86 |
``` |
| 87 |
|
| 88 |
## Configuration Flags |
| 89 |
|
| 90 |
You can modify crate using the following configuration flags: |
| 91 |
|
| 92 |
- `chacha20_backend="avx2"`: force AVX2 backend on x86/x86_64 targets. |
| 93 |
Requires enabled AVX2 target feature. Ignored on non-x86(_64) targets. |
| 94 |
- `chacha20_backend="avx512"`: force AVX-512 backend on x86/x86_64 targets. |
| 95 |
Requires enabled AVX-512 target feature (MSRV 1.89). Ignored on non-x86(_64) targets. |
| 96 |
- `chacha20_backend="soft"`: force software backend. |
| 97 |
- `chacha20_backend="sse2"`: force SSE2 backend on x86/x86_64 targets. |
| 98 |
Requires enabled SSE2 target feature. Ignored on non-x86(-64) targets. |
| 99 |
|
| 100 |
To use the MSRV 1.89 AVX-512 support with autodetection, you must enable it using |
| 101 |
`chacha20_avx512` configuration flag. |
| 102 |
|
| 103 |
The flags can be enabled using `RUSTFLAGS` environmental variable |
| 104 |
(e.g. `RUSTFLAGS='--cfg chacha20_backend="avx2"'`) or by modifying `.cargo/config.toml`. |
| 105 |
|
| 106 |
## License |
| 107 |
|
| 108 |
Licensed under either of: |
| 109 |
|
| 110 |
- [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) |
| 111 |
- [MIT license](http://opensource.org/licenses/MIT) |
| 112 |
|
| 113 |
at your option. |
| 114 |
|
| 115 |
### Contribution |
| 116 |
|
| 117 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 118 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 119 |
dual licensed as above, without any additional terms or conditions. |
| 120 |
|
| 121 |
[//]: # (badges) |
| 122 |
|
| 123 |
[crate-image]: https://img.shields.io/crates/v/chacha20.svg |
| 124 |
[crate-link]: https://crates.io/crates/chacha20 |
| 125 |
[docs-image]: https://docs.rs/chacha20/badge.svg |
| 126 |
[docs-link]: https://docs.rs/chacha20/ |
| 127 |
[build-image]: https://github.com/RustCrypto/stream-ciphers/actions/workflows/chacha20.yml/badge.svg |
| 128 |
[build-link]: https://github.com/RustCrypto/stream-ciphers/actions/workflows/chacha20.yml |
| 129 |
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg |
| 130 |
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg |
| 131 |
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg |
| 132 |
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260049-stream-ciphers |
| 133 |
[hazmat-image]: https://img.shields.io/badge/crypto-hazmat%E2%9A%A0-red.svg |
| 134 |
[hazmat-link]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md |
| 135 |
|
| 136 |
[//]: # (footnotes) |
| 137 |
|
| 138 |
[ChaCha]: https://en.wikipedia.org/wiki/Salsa20#ChaCha_variant |
| 139 |
[Salsa]: https://en.wikipedia.org/wiki/Salsa20 |
| 140 |
[XChaCha]: https://tools.ietf.org/html/draft-arciszewski-xchacha-02 |
| 141 |
[AEAD]: https://en.wikipedia.org/wiki/Authenticated_encryption |
| 142 |
[`chacha20poly1305`]: https://docs.rs/chacha20poly1305 |
| 143 |
[RustCrypto/AEADs]: https://github.com/RustCrypto/AEADs |
| 144 |
[NCC-AUDIT]: https://web.archive.org/web/20240108154854/https://research.nccgroup.com/wp-content/uploads/2020/02/NCC_Group_MobileCoin_RustCrypto_AESGCM_ChaCha20Poly1305_Implementation_Review_2020-02-12_v1.0.pdf |
| 145 |
[MobileCoin]: https://www.mobilecoin.com/ |