diff options
author | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2023-03-22 09:52:08 +0100 |
---|---|---|
committer | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2023-03-22 13:15:29 +0100 |
commit | 7262b9bc538b7ea0b85076dc9339378cdfadc907 (patch) | |
tree | dda108ccb0930cd1323f54aa482f03f364be737f | |
parent | 17fc37b80785f2564ea7709f30d1a9f3346ab636 (diff) |
openpgp: Drop Aead::digest_size.wiktor/drop-aead-digest-size
- Removes internal interface function `digest_size`.
- Adjusts backends to manage size internally.
- Fixes https://gitlab.com/sequoia-pgp/sequoia/-/issues/937
-rw-r--r-- | openpgp/src/crypto/aead.rs | 5 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/botan/aead.rs | 7 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/cng/aead.rs | 13 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/nettle/aead.rs | 3 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/openssl/aead.rs | 12 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/rust/aead.rs | 14 |
6 files changed, 14 insertions, 40 deletions
diff --git a/openpgp/src/crypto/aead.rs b/openpgp/src/crypto/aead.rs index 700a2bcb..f2e53f73 100644 --- a/openpgp/src/crypto/aead.rs +++ b/openpgp/src/crypto/aead.rs @@ -56,9 +56,6 @@ pub trait Aead : seal::Sealed { /// ciphertext and the digest! fn encrypt_seal(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()>; - /// Length of the digest in bytes. - fn digest_size(&self) -> usize; - /// Decrypt one chunk `src` to `dst` and verify that the digest is /// correct. fn decrypt_verify(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()>; @@ -704,7 +701,7 @@ impl<W: io::Write, S: Schedule> Encryptor<W, S> { // size. The vector has capacity chunk size plus // digest size. debug_assert!(self.buffer.len() < self.chunk_size); - self.scratch.set_len(self.buffer.len() + aead.digest_size()) + self.scratch.set_len(self.buffer.len() + self.digest_size) } aead.encrypt_seal(&mut self.scratch, &self.buffer)?; self.bytes_encrypted += self.buffer.len() as u64; diff --git a/openpgp/src/crypto/backend/botan/aead.rs b/openpgp/src/crypto/backend/botan/aead.rs index c7f4bbf2..5d55aa86 100644 --- a/openpgp/src/crypto/backend/botan/aead.rs +++ b/openpgp/src/crypto/backend/botan/aead.rs @@ -11,19 +11,16 @@ struct Cipher(botan::Cipher, usize); impl seal::Sealed for Cipher {} impl Aead for Cipher { fn encrypt_seal(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len(), src.len() + self.digest_size()); + debug_assert_eq!(dst.len(), src.len() + self.1); self.0.finish_into(src, dst)?; Ok(()) } fn decrypt_verify(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len() + self.digest_size(), src.len()); + debug_assert_eq!(dst.len() + self.1, src.len()); self.0.finish_into(src, dst)?; Ok(()) } - fn digest_size(&self) -> usize { - self.1 - } } impl AEADAlgorithm { diff --git a/openpgp/src/crypto/backend/cng/aead.rs b/openpgp/src/crypto/backend/cng/aead.rs index a3aaefb4..da964cba 100644 --- a/openpgp/src/crypto/backend/cng/aead.rs +++ b/openpgp/src/crypto/backend/cng/aead.rs @@ -114,11 +114,8 @@ macro_rules! impl_aead { ($($type: ty),*) => { $( impl Aead for Eax<$type, Encrypt> { - fn digest_size(&self) -> usize { - <eax::Tag as GenericArrayExt<_, _>>::LEN - } fn encrypt_seal(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len(), src.len() + self.digest_size()); + debug_assert_eq!(dst.len(), src.len() + <eax::Tag as GenericArrayExt<_, _>>::LEN); let len = core::cmp::min(dst.len(), src.len()); dst[..len].copy_from_slice(&src[..len]); Eax::<$type, Encrypt>::encrypt(self, &mut dst[..len]); @@ -135,17 +132,15 @@ macro_rules! impl_aead { )* $( impl Aead for Eax<$type, Decrypt> { - fn digest_size(&self) -> usize { - <eax::Tag as GenericArrayExt<_, _>>::LEN - } fn encrypt_seal(&mut self, _dst: &mut [u8], _src: &[u8]) -> Result<()> { panic!("AEAD encryption called in the decryption context") } fn decrypt_verify(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len() + self.digest_size(), src.len()); + let digest_size = <eax::Tag as GenericArrayExt<_, _>>::LEN; + debug_assert_eq!(dst.len() + digest_size, src.len()); // Split src into ciphertext and digest. - let l = self.digest_size(); + let l = digest_size; let digest = &src[src.len().saturating_sub(l)..]; let src = &src[..src.len().saturating_sub(l)]; diff --git a/openpgp/src/crypto/backend/nettle/aead.rs b/openpgp/src/crypto/backend/nettle/aead.rs index 486269b1..70755f4c 100644 --- a/openpgp/src/crypto/backend/nettle/aead.rs +++ b/openpgp/src/crypto/backend/nettle/aead.rs @@ -50,9 +50,6 @@ impl<T: nettle::aead::Aead> Aead for T { } Ok(()) } - fn digest_size(&self) -> usize { - self.digest_size() - } } impl AEADAlgorithm { diff --git a/openpgp/src/crypto/backend/openssl/aead.rs b/openpgp/src/crypto/backend/openssl/aead.rs index cc8735fb..a5c31ecf 100644 --- a/openpgp/src/crypto/backend/openssl/aead.rs +++ b/openpgp/src/crypto/backend/openssl/aead.rs @@ -14,7 +14,7 @@ struct OpenSslContext { impl Aead for OpenSslContext { fn encrypt_seal(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len(), src.len() + self.digest_size()); + debug_assert_eq!(dst.len(), src.len() + self.ctx.block_size()); // SAFETY: Process completely one full chunk. Since `update` // is not being called again with partial block info and the @@ -26,11 +26,11 @@ impl Aead for OpenSslContext { } fn decrypt_verify(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert!(src.len() >= self.digest_size()); - debug_assert_eq!(dst.len() + self.digest_size(), src.len()); + debug_assert!(src.len() >= self.ctx.block_size()); + debug_assert_eq!(dst.len() + self.ctx.block_size(), src.len()); // Split src into ciphertext and tag. - let l = self.digest_size(); + let l = self.ctx.block_size(); let ciphertext = &src[..src.len().saturating_sub(l)]; let tag = &src[src.len().saturating_sub(l)..]; @@ -44,10 +44,6 @@ impl Aead for OpenSslContext { unsafe { self.ctx.cipher_final_unchecked(&mut dst[size..])? }; Ok(()) } - - fn digest_size(&self) -> usize { - self.ctx.block_size() - } } impl crate::seal::Sealed for OpenSslContext {} diff --git a/openpgp/src/crypto/backend/rust/aead.rs b/openpgp/src/crypto/backend/rust/aead.rs index 90b62a91..c430077d 100644 --- a/openpgp/src/crypto/backend/rust/aead.rs +++ b/openpgp/src/crypto/backend/rust/aead.rs @@ -45,12 +45,8 @@ where Cipher: BlockCipher<BlockSize = U16> + NewBlockCipher + Clone, Cipher::ParBlocks: ArrayLength<Block<Cipher>>, { - fn digest_size(&self) -> usize { - eax::Tag::LEN - } - fn encrypt_seal(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len(), src.len() + self.digest_size()); + debug_assert_eq!(dst.len(), src.len() + eax::Tag::LEN); let len = cmp::min(dst.len(), src.len()); dst[..len].copy_from_slice(&src[..len]); Self::encrypt(self, &mut dst[..len]); @@ -69,19 +65,15 @@ where Cipher: BlockCipher<BlockSize = U16> + NewBlockCipher + Clone, Cipher::ParBlocks: ArrayLength<Block<Cipher>>, { - fn digest_size(&self) -> usize { - eax::Tag::LEN - } - fn encrypt_seal(&mut self, _dst: &mut [u8], _src: &[u8]) -> Result<()> { panic!("AEAD encryption called in the decryption context") } fn decrypt_verify(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { - debug_assert_eq!(dst.len() + self.digest_size(), src.len()); + debug_assert_eq!(dst.len() + eax::Tag::LEN, src.len()); // Split src into ciphertext and digest. - let l = self.digest_size(); + let l = eax::Tag::LEN; let digest = &src[src.len().saturating_sub(l)..]; let src = &src[..src.len().saturating_sub(l)]; |