From e8faecac790c504eb5b68d0aacf65d076be11aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 17 Jan 2025 16:56:30 +0100 Subject: [PATCH] Format the dehydration pickling code a bit prettier --- src/olm/account/mod.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/olm/account/mod.rs b/src/olm/account/mod.rs index 36d503f2..1d295326 100644 --- a/src/olm/account/mod.rs +++ b/src/olm/account/mod.rs @@ -516,19 +516,22 @@ impl Account { let cipher = ChaCha20Poly1305::new(key.into()); let ciphertext = base64_decode(ciphertext)?; let nonce = base64_decode(nonce)?; + if nonce.len() != 12 { - return Err(crate::DehydratedDeviceError::InvalidNonce); - } - let mut plaintext = cipher.decrypt(nonce.as_slice().into(), ciphertext.as_slice())?; - let version = - get_pickle_version(&plaintext).ok_or(crate::DehydratedDeviceError::MissingVersion)?; - if version != PICKLE_VERSION { - return Err(crate::DehydratedDeviceError::Version(PICKLE_VERSION, version)); - } + Err(crate::DehydratedDeviceError::InvalidNonce) + } else { + let mut plaintext = cipher.decrypt(nonce.as_slice().into(), ciphertext.as_slice())?; + let version = get_pickle_version(&plaintext) + .ok_or(crate::DehydratedDeviceError::MissingVersion)?; - let pickle = Self::from_decrypted_dehydrated_device(&plaintext); - plaintext.zeroize(); - pickle + if version != PICKLE_VERSION { + Err(crate::DehydratedDeviceError::Version(PICKLE_VERSION, version)) + } else { + let pickle = Self::from_decrypted_dehydrated_device(&plaintext); + plaintext.zeroize(); + pickle + } + } } // This function is public for fuzzing, but should not be used by anything