diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-03-23 17:37:08 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-03-24 10:08:31 +0100 |
commit | 301ad2858cf43b06f398214d87c8b5bf24dffa79 (patch) | |
tree | bc13d6713451b82ed046a818be0990e6bc021ca7 /openpgp/src/crypto/backend/nettle | |
parent | 6f801ea5e5b884711945d49bc8e6589e150983d2 (diff) |
openpgp: Hardcode symmetric algorithm key and block sizes.
- Previously, every crypto backend had to implement these methods.
Instead, implement them just once and hard code the lengths.
Anchor them using the values from the crypto backends, if
available.
- Fixes #966.
Diffstat (limited to 'openpgp/src/crypto/backend/nettle')
-rw-r--r-- | openpgp/src/crypto/backend/nettle/symmetric.rs | 98 |
1 files changed, 59 insertions, 39 deletions
diff --git a/openpgp/src/crypto/backend/nettle/symmetric.rs b/openpgp/src/crypto/backend/nettle/symmetric.rs index b9dd2703..2b3c9f1b 100644 --- a/openpgp/src/crypto/backend/nettle/symmetric.rs +++ b/openpgp/src/crypto/backend/nettle/symmetric.rs @@ -109,45 +109,6 @@ impl SymmetricAlgorithm { } } - /// Length of a key for this algorithm in bytes. - /// - /// Fails if Sequoia does not support this algorithm. - pub fn key_size(self) -> Result<usize> { - match self { - SymmetricAlgorithm::TripleDES => Ok(cipher::Des3::KEY_SIZE), - SymmetricAlgorithm::CAST5 => Ok(cipher::Cast128::KEY_SIZE), - // RFC4880, Section 9.2: Blowfish (128 bit key, 16 rounds) - SymmetricAlgorithm::Blowfish => Ok(16), - SymmetricAlgorithm::AES128 => Ok(cipher::Aes128::KEY_SIZE), - SymmetricAlgorithm::AES192 => Ok(cipher::Aes192::KEY_SIZE), - SymmetricAlgorithm::AES256 => Ok(cipher::Aes256::KEY_SIZE), - SymmetricAlgorithm::Twofish => Ok(cipher::Twofish::KEY_SIZE), - SymmetricAlgorithm::Camellia128 => Ok(cipher::Camellia128::KEY_SIZE), - SymmetricAlgorithm::Camellia192 => Ok(cipher::Camellia192::KEY_SIZE), - SymmetricAlgorithm::Camellia256 => Ok(cipher::Camellia256::KEY_SIZE), - _ => Err(Error::UnsupportedSymmetricAlgorithm(self).into()), - } - } - - /// Length of a block for this algorithm in bytes. - /// - /// Fails if Sequoia does not support this algorithm. - pub fn block_size(self) -> Result<usize> { - match self { - SymmetricAlgorithm::TripleDES => Ok(cipher::Des3::BLOCK_SIZE), - SymmetricAlgorithm::CAST5 => Ok(cipher::Cast128::BLOCK_SIZE), - SymmetricAlgorithm::Blowfish => Ok(cipher::Blowfish::BLOCK_SIZE), - SymmetricAlgorithm::AES128 => Ok(cipher::Aes128::BLOCK_SIZE), - SymmetricAlgorithm::AES192 => Ok(cipher::Aes192::BLOCK_SIZE), - SymmetricAlgorithm::AES256 => Ok(cipher::Aes256::BLOCK_SIZE), - SymmetricAlgorithm::Twofish => Ok(cipher::Twofish::BLOCK_SIZE), - SymmetricAlgorithm::Camellia128 => Ok(cipher::Camellia128::BLOCK_SIZE), - SymmetricAlgorithm::Camellia192 => Ok(cipher::Camellia192::BLOCK_SIZE), - SymmetricAlgorithm::Camellia256 => Ok(cipher::Camellia256::BLOCK_SIZE), - _ => Err(Error::UnsupportedSymmetricAlgorithm(self).into()), - } - } - /// Creates a Nettle context for encrypting in CFB mode. pub(crate) fn make_encrypt_cfb(self, key: &[u8], iv: Vec<u8>) -> Result<Box<dyn Mode>> { match self { @@ -256,3 +217,62 @@ impl SymmetricAlgorithm { } } } + +#[cfg(test)] +mod tests { + use super::*; + + /// Anchors the constants used in Sequoia with the ones from + /// Nettle. + #[test] + fn key_size() -> Result<()> { + assert_eq!(SymmetricAlgorithm::TripleDES.key_size()?, + cipher::Des3::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::CAST5.key_size()?, + cipher::Cast128::KEY_SIZE); + // RFC4880, Section 9.2: Blowfish (128 bit key, 16 rounds) + assert_eq!(SymmetricAlgorithm::Blowfish.key_size()?, 16); + assert_eq!(SymmetricAlgorithm::AES128.key_size()?, + cipher::Aes128::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::AES192.key_size()?, + cipher::Aes192::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::AES256.key_size()?, + cipher::Aes256::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::Twofish.key_size()?, + cipher::Twofish::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::Camellia128.key_size()?, + cipher::Camellia128::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::Camellia192.key_size()?, + cipher::Camellia192::KEY_SIZE); + assert_eq!(SymmetricAlgorithm::Camellia256.key_size()?, + cipher::Camellia256::KEY_SIZE); + Ok(()) + } + + /// Anchors the constants used in Sequoia with the ones from + /// Nettle. + #[test] + fn block_size() -> Result<()> { + assert_eq!(SymmetricAlgorithm::TripleDES.block_size()?, + cipher::Des3::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::CAST5.block_size()?, + cipher::Cast128::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::Blowfish.block_size()?, + cipher::Blowfish::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::AES128.block_size()?, + cipher::Aes128::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::AES192.block_size()?, + cipher::Aes192::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::AES256.block_size()?, + cipher::Aes256::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::Twofish.block_size()?, + cipher::Twofish::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::Camellia128.block_size()?, + cipher::Camellia128::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::Camellia192.block_size()?, + cipher::Camellia192::BLOCK_SIZE); + assert_eq!(SymmetricAlgorithm::Camellia256.block_size()?, + cipher::Camellia256::BLOCK_SIZE); + Ok(()) + } +} |