Module chacha20poly1305
dep_orion
only.Expand description
AEAD ChaCha20Poly1305 as specified in the RFC 8439.
§Parameters:
secret_key
: The secret key.nonce
: The nonce value.ad
: Additional data to authenticate (this is not encrypted and can beNone
).ciphertext_with_tag
: The encrypted data with the corresponding 16 byte Poly1305 tag appended to it.plaintext
: The data to be encrypted.dst_out
: Destination array that will hold theciphertext_with_tag
/plaintext
after encryption/decryption.
ad
: “A typical use for these data is to authenticate version numbers,
timestamps or monotonically increasing counters in order to discard previous
messages and prevent replay attacks.” See libsodium docs for more information.
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
+POLY1305_OUTSIZE
when callingseal()
. - The length of
dst_out
is less thanciphertext_with_tag
-POLY1305_OUTSIZE
when callingopen()
. - The length of
ciphertext_with_tag
is not at leastPOLY1305_OUTSIZE
. - The received tag does not match the calculated tag when calling
open()
. plaintext.len()
+POLY1305_OUTSIZE
overflows when callingseal()
.- Converting
usize
tou64
would be a lossy conversion. plaintext.len() >
P_MAX
ad.len() >
A_MAX
ciphertext_with_tag.len() >
C_MAX
§Panics:
A panic will occur if:
- More than
2^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.
- Only a nonce for XChaCha20Poly1305 is big enough to be randomly generated using a CSPRNG.
- To securely generate a strong key, use
SecretKey::generate()
. - The length of the
plaintext
is not hidden, only its contents.
§Recommendation:
- It is recommended to use
XChaCha20Poly1305
when possible.
§Example:
use orion::hazardous::aead;
let secret_key = aead::chacha20poly1305::SecretKey::generate();
// WARNING: This nonce is only meant for demonstration and should not
// be repeated. Please read the security section.
let nonce = aead::chacha20poly1305::Nonce::from([0u8; 12]);
let ad = "Additional data".as_bytes();
let message = "Data to protect".as_bytes();
// Length of the above message is 15 and then we accommodate 16 for the Poly1305
// tag.
let mut dst_out_ct = [0u8; 15 + 16];
let mut dst_out_pt = [0u8; 15];
// Encrypt and place ciphertext + tag in dst_out_ct
aead::chacha20poly1305::seal(&secret_key, &nonce, message, Some(&ad), &mut dst_out_ct)?;
// Verify tag, if correct then decrypt and place message in dst_out_pt
aead::chacha20poly1305::open(&secret_key, &nonce, &dst_out_ct, Some(&ad), &mut dst_out_pt)?;
assert_eq!(dst_out_pt.as_ref(), message.as_ref());
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§
- A_MAX
- The maximum size of the associated data (see RFC 8439).
- C_MAX
- The maximum size of the ciphertext (see RFC 8439).
- P_MAX
- The maximum size of the plaintext (see RFC 8439).