|
| 1 | +//! # XChaCha20Poly1305 operations |
| 2 | +//! |
| 3 | +//! Contains low level XChaCha20Poly1305 operations used by the rest of the crate. |
| 4 | +//! |
| 5 | +//! In most cases you should use the [EncString][crate::EncString] with |
| 6 | +//! [KeyEncryptable][crate::KeyEncryptable] & [KeyDecryptable][crate::KeyDecryptable] instead. |
| 7 | +//! |
| 8 | +//! Note: |
| 9 | +//! XChaCha20Poly1305 encrypts data, and authenticates both the cipher text and associated |
| 10 | +//! data. This does not provide key-commitment, and assumes there can only be one key. |
| 11 | +//! |
| 12 | +//! If multiple keys are possible, a key-committing cipher such as |
| 13 | +//! XChaCha20Poly1305Blake3CTX should be used: `https://github.com/bitwarden/sdk-internal/pull/41` to prevent invisible-salamander style attacks. |
| 14 | +//! `https://eprint.iacr.org/2019/016.pdf` |
| 15 | +//! `https://soatok.blog/2024/09/10/invisible-salamanders-are-not-what-you-think/` |
| 16 | +
|
| 17 | +use chacha20poly1305::{AeadCore, AeadInPlace, KeyInit, XChaCha20Poly1305}; |
| 18 | +use generic_array::GenericArray; |
| 19 | +use rand::{CryptoRng, RngCore}; |
| 20 | + |
| 21 | +use crate::CryptoError; |
| 22 | + |
| 23 | +#[allow(unused)] |
| 24 | +pub(crate) struct XChaCha20Poly1305Ciphertext { |
| 25 | + nonce: GenericArray<u8, <XChaCha20Poly1305 as AeadCore>::NonceSize>, |
| 26 | + ciphertext: Vec<u8>, |
| 27 | +} |
| 28 | + |
| 29 | +#[allow(unused)] |
| 30 | +fn encrypt_xchacha20_poly1305( |
| 31 | + key: &[u8; 32], |
| 32 | + plaintext_secret_data: &[u8], |
| 33 | + associated_data: &[u8], |
| 34 | +) -> XChaCha20Poly1305Ciphertext { |
| 35 | + let mut rng = rand::thread_rng(); |
| 36 | + encrypt_xchacha20_poly1305_internal(rng, key, plaintext_secret_data, associated_data) |
| 37 | +} |
| 38 | + |
| 39 | +fn encrypt_xchacha20_poly1305_internal( |
| 40 | + rng: impl RngCore + CryptoRng, |
| 41 | + key: &[u8; 32], |
| 42 | + plaintext_secret_data: &[u8], |
| 43 | + associated_data: &[u8], |
| 44 | +) -> XChaCha20Poly1305Ciphertext { |
| 45 | + let nonce = &XChaCha20Poly1305::generate_nonce(rng); |
| 46 | + // This buffer contains the plaintext, that will be encrypted in-place |
| 47 | + let mut buffer = plaintext_secret_data.to_vec(); |
| 48 | + XChaCha20Poly1305::new(GenericArray::from_slice(key)) |
| 49 | + .encrypt_in_place(nonce, associated_data, &mut buffer) |
| 50 | + .expect("encryption failed"); |
| 51 | + |
| 52 | + XChaCha20Poly1305Ciphertext { |
| 53 | + nonce: *nonce, |
| 54 | + ciphertext: buffer, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[allow(unused)] |
| 59 | +pub(crate) fn decrypt_xchacha20_poly1305( |
| 60 | + nonce: &[u8; 24], |
| 61 | + key: &[u8; 32], |
| 62 | + ciphertext: &[u8], |
| 63 | + associated_data: &[u8], |
| 64 | +) -> Result<Vec<u8>, CryptoError> { |
| 65 | + let mut buffer = ciphertext.to_vec(); |
| 66 | + XChaCha20Poly1305::new(GenericArray::from_slice(key)) |
| 67 | + .decrypt_in_place( |
| 68 | + GenericArray::from_slice(nonce), |
| 69 | + associated_data, |
| 70 | + &mut buffer, |
| 71 | + ) |
| 72 | + .map_err(|_| CryptoError::KeyDecrypt)?; |
| 73 | + Ok(buffer) |
| 74 | +} |
| 75 | + |
| 76 | +mod tests { |
| 77 | + #[cfg(test)] |
| 78 | + use crate::xchacha20::*; |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_encrypt_decrypt_xchacha20() { |
| 82 | + let key = [0u8; 32]; |
| 83 | + let plaintext_secret_data = b"My secret data"; |
| 84 | + let authenticated_data = b"My authenticated data"; |
| 85 | + let encrypted = encrypt_xchacha20_poly1305(&key, plaintext_secret_data, authenticated_data); |
| 86 | + let decrypted = decrypt_xchacha20_poly1305( |
| 87 | + &encrypted.nonce.into(), |
| 88 | + &key, |
| 89 | + &encrypted.ciphertext, |
| 90 | + authenticated_data, |
| 91 | + ) |
| 92 | + .unwrap(); |
| 93 | + assert_eq!(plaintext_secret_data, decrypted.as_slice()); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_fails_when_ciphertext_changed() { |
| 98 | + let key = [0u8; 32]; |
| 99 | + let plaintext_secret_data = b"My secret data"; |
| 100 | + let authenticated_data = b"My authenticated data"; |
| 101 | + |
| 102 | + let mut encrypted = |
| 103 | + encrypt_xchacha20_poly1305(&key, plaintext_secret_data, authenticated_data); |
| 104 | + encrypted.ciphertext[0] = encrypted.ciphertext[0].wrapping_add(1); |
| 105 | + let result = decrypt_xchacha20_poly1305( |
| 106 | + &encrypted.nonce.into(), |
| 107 | + &key, |
| 108 | + &encrypted.ciphertext, |
| 109 | + authenticated_data, |
| 110 | + ); |
| 111 | + assert!(result.is_err()); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_fails_when_associated_data_changed() { |
| 116 | + let key = [0u8; 32]; |
| 117 | + let plaintext_secret_data = b"My secret data"; |
| 118 | + let mut authenticated_data = b"My authenticated data".to_vec(); |
| 119 | + |
| 120 | + let encrypted = |
| 121 | + encrypt_xchacha20_poly1305(&key, plaintext_secret_data, authenticated_data.as_slice()); |
| 122 | + authenticated_data[0] = authenticated_data[0].wrapping_add(1); |
| 123 | + let result = decrypt_xchacha20_poly1305( |
| 124 | + &encrypted.nonce.into(), |
| 125 | + &key, |
| 126 | + &encrypted.ciphertext, |
| 127 | + authenticated_data.as_slice(), |
| 128 | + ); |
| 129 | + assert!(result.is_err()); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_fails_when_nonce_changed() { |
| 134 | + let key = [0u8; 32]; |
| 135 | + let plaintext_secret_data = b"My secret data"; |
| 136 | + let authenticated_data = b"My authenticated data"; |
| 137 | + |
| 138 | + let mut encrypted = |
| 139 | + encrypt_xchacha20_poly1305(&key, plaintext_secret_data, authenticated_data); |
| 140 | + encrypted.nonce[0] = encrypted.nonce[0].wrapping_add(1); |
| 141 | + let result = decrypt_xchacha20_poly1305( |
| 142 | + &encrypted.nonce.into(), |
| 143 | + &key, |
| 144 | + &encrypted.ciphertext, |
| 145 | + authenticated_data, |
| 146 | + ); |
| 147 | + assert!(result.is_err()); |
| 148 | + } |
| 149 | +} |
0 commit comments