From 747163f433649bff218d280e354e970ea706059e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 24 Mar 2024 12:17:29 +0100 Subject: [PATCH] Check the size of the nonce --- src/scheme/xchacha20poly1305.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/scheme/xchacha20poly1305.rs b/src/scheme/xchacha20poly1305.rs index aa4f56d..daa38a3 100644 --- a/src/scheme/xchacha20poly1305.rs +++ b/src/scheme/xchacha20poly1305.rs @@ -1,11 +1,13 @@ use crate::encrypted_data::EncryptedData; -use crate::error::Result; +use crate::error::{Error, Result}; use chacha20poly1305::aead::{Aead, KeyInit, Payload}; use chacha20poly1305::{Key, XChaCha20Poly1305, XNonce}; +// X-variant: the nonce's size is 192 bits (24 bytes) +const NONCE_SIZE: usize = 24; + pub(crate) fn xchacha20poly1305_gen_nonce() -> Result> { - // X-variant: the nonce's size is 192 bits (24 bytes) - let mut nonce: [u8; 24] = [0; 24]; + let mut nonce: [u8; NONCE_SIZE] = [0; NONCE_SIZE]; getrandom::getrandom(&mut nonce)?; Ok(nonce.to_vec()) } @@ -44,6 +46,12 @@ pub(crate) fn xchacha20poly1305_decrypt( ) -> Result> { // Adapt the key and nonce let key = Key::from_slice(key); + if encrypted_data.nonce.len() != NONCE_SIZE { + return Err(Error::InvalidNonceSize( + NONCE_SIZE, + encrypted_data.nonce.len(), + )); + } let nonce = XNonce::from_slice(&encrypted_data.nonce); // Prepare the payload