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/types/mod.rs | |
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/types/mod.rs')
-rw-r--r-- | openpgp/src/types/mod.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index 698b65d1..eca26d92 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -862,6 +862,47 @@ impl SymmetricAlgorithm { pub fn variants() -> impl Iterator<Item=Self> { SYMMETRIC_ALGORITHM_VARIANTS.iter().cloned() } + + /// Length of a key for this algorithm in bytes. + /// + /// Fails if the algorithm isn't known to Sequoia. + pub fn key_size(self) -> Result<usize> { + match self { + SymmetricAlgorithm::IDEA => Ok(16), + SymmetricAlgorithm::TripleDES => Ok(24), + SymmetricAlgorithm::CAST5 => Ok(16), + // RFC4880, Section 9.2: Blowfish (128 bit key, 16 rounds) + SymmetricAlgorithm::Blowfish => Ok(16), + SymmetricAlgorithm::AES128 => Ok(16), + SymmetricAlgorithm::AES192 => Ok(24), + SymmetricAlgorithm::AES256 => Ok(32), + SymmetricAlgorithm::Twofish => Ok(32), + SymmetricAlgorithm::Camellia128 => Ok(16), + SymmetricAlgorithm::Camellia192 => Ok(24), + SymmetricAlgorithm::Camellia256 => Ok(32), + _ => Err(Error::UnsupportedSymmetricAlgorithm(self).into()), + } + } + + /// Length of a block for this algorithm in bytes. + /// + /// Fails if the algorithm isn't known to Sequoia. + pub fn block_size(self) -> Result<usize> { + match self { + SymmetricAlgorithm::IDEA => Ok(8), + SymmetricAlgorithm::TripleDES => Ok(8), + SymmetricAlgorithm::CAST5 => Ok(8), + SymmetricAlgorithm::Blowfish => Ok(8), + SymmetricAlgorithm::AES128 => Ok(16), + SymmetricAlgorithm::AES192 => Ok(16), + SymmetricAlgorithm::AES256 => Ok(16), + SymmetricAlgorithm::Twofish => Ok(16), + SymmetricAlgorithm::Camellia128 => Ok(16), + SymmetricAlgorithm::Camellia192 => Ok(16), + SymmetricAlgorithm::Camellia256 => Ok(16), + _ => Err(Error::UnsupportedSymmetricAlgorithm(self).into()), + } + } } /// The AEAD algorithms as defined in [Section 9.6 of RFC 4880bis]. |