[−][src]Crate chacha20
The ChaCha20 stream cipher (RFC 7539)
ChaCha20 is a lightweight stream cipher which is amenable to fast, constant-time implementations in software. It improves upon the previous Salsa20 stream cipher, providing increased per-round diffusion with no cost to performance.
Cipher functionality is accessed using traits from re-exported
stream-cipher
crate.
This crate contains three variants of ChaCha20:
ChaCha20
: standard IETF variant with 96-bit nonceChaCha20Legacy
: (gated under thelegacy
feature) "djb" variant with 64-bit nonceXChaCha20
: (gated under thexchacha20
feature) 192-bit extended nonce variant
Security Warning
This crate does not ensure ciphertexts are authentic, which can lead to serious vulnerabilities if used incorrectly!
USE AT YOUR OWN RISK!
Usage
use chacha20::ChaCha20; use chacha20::stream_cipher::generic_array::GenericArray; use chacha20::stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; let mut data = [1, 2, 3, 4, 5, 6, 7]; let key = GenericArray::from_slice(b"an example very very secret key."); let nonce = GenericArray::from_slice(b"secret nonce"); // create cipher instance let mut cipher = ChaCha20::new(&key, &nonce); // apply keystream (encrypt) cipher.apply_keystream(&mut data); assert_eq!(data, [73, 98, 234, 202, 73, 143, 0]); // seek to the keystream beginning and apply it again to the `data` (decrypt) cipher.seek(0); cipher.apply_keystream(&mut data); assert_eq!(data, [1, 2, 3, 4, 5, 6, 7]);
Re-exports
pub extern crate stream_cipher; |
Structs
ChaCha20Legacy | The ChaCha20 stream cipher (legacy "djb" construction with 64-bit nonce). |
ChaCha20 | The ChaCha20 stream cipher (RFC 7539 version with 96-bit nonce) |
XChaCha20 | XChaCha20 is a ChaCha20 variant with an extended 192-bit (24-byte) nonce. |
Constants
BLOCK_SIZE | Size of a ChaCha20 block in bytes |
MAX_BLOCKS | Maximum number of blocks that can be encrypted with ChaCha20 before the counter overflows. |