Module chacha20
dep_orion
only.Expand description
IETF ChaCha20 as specified in the RFC 8439.
§Parameters:
secret_key
: The secret key.nonce
: The nonce value.initial_counter
: The initial counter value. In most cases, this is0
.ciphertext
: The encrypted data.plaintext
: The data to be encrypted.dst_out
: Destination array that will hold the ciphertext/plaintext after encryption/decryption.
nonce
: “Counters and LFSRs are both acceptable ways of generating unique
nonces, as is encrypting a counter using a block cipher with a 64-bit block
size such as DES. Note that it is not acceptable to use a truncation of a
counter encrypted with block ciphers with 128-bit or 256-bit blocks,
because such a truncation may repeat after a short time.” See RFC
for more information.
dst_out
: The output buffer may have a capacity greater than the input. If this is the case,
only the first input length amount of bytes in dst_out
are modified, while the rest remain untouched.
§Errors:
An error will be returned if:
- The length of
dst_out
is less thanplaintext
orciphertext
. plaintext
orciphertext
is empty.- The
initial_counter
is high enough to cause a potential overflow.
Even though dst_out
is allowed to be of greater length than plaintext
,
the ciphertext
produced by chacha20
/xchacha20
will always be of the
same length as the plaintext
.
§Panics:
A panic will occur if:
- More than
2^32-1
keystream blocks are processed or more than2^32-1 * 64
bytes of data are processed.
§Security:
- It is critical for security that a given nonce is not re-used with a given key. Should this happen, the security of all data that has been encrypted with that given key is compromised.
- Functions herein do not provide any data integrity. If you need
data integrity, which is nearly always the case, you should use an
AEAD construction instead. See the
aead
module for this. - Only a nonce for XChaCha20 is big enough to be randomly generated using a CSPRNG.
- To securely generate a strong key, use
SecretKey::generate()
.
§Recommendation:
- It is recommended to use
XChaCha20Poly1305
when possible.
§Example:
use orion::hazardous::stream::chacha20;
let secret_key = chacha20::SecretKey::generate();
// WARNING: This nonce is only meant for demonstration and should not
// be repeated. Please read the security section.
let nonce = chacha20::Nonce::from([0u8; 12]);
let message = "Data to protect".as_bytes();
// The length of this message is 15.
let mut dst_out_pt = [0u8; 15];
let mut dst_out_ct = [0u8; 15];
chacha20::encrypt(&secret_key, &nonce, 0, message, &mut dst_out_ct)?;
chacha20::decrypt(&secret_key, &nonce, 0, &dst_out_ct, &mut dst_out_pt)?;
assert_eq!(dst_out_pt, message);
Structs§
- Nonce
- A type that represents a
Nonce
that ChaCha20 and ChaCha20-Poly1305 use. - Secret
Key - A type to represent the
SecretKey
that Chacha20, XChaCha20, ChaCha20-Poly1305 and XChaCha20-Poly1305 use.
Constants§
- CHACHA_
KEYSIZE - The key size for ChaCha20.
- IETF_
CHACHA_ NONCESIZE - The nonce size for IETF ChaCha20.