diff options
author | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-04-10 00:53:35 +0200 |
---|---|---|
committer | Igor Matuszewski <igor@sequoia-pgp.org> | 2020-06-22 11:57:07 +0200 |
commit | c8c207e931f204e5746eaff7dd57382048d26492 (patch) | |
tree | 1482b26870f6f2cd54395255f5443ce1c25dc5a5 /openpgp/src/crypto/backend/nettle/aead.rs | |
parent | f30cc7f367084fa295bd542cf9eb9a3398657fce (diff) |
openpgp: Move Nettle AEAD implementation to the backend module
Diffstat (limited to 'openpgp/src/crypto/backend/nettle/aead.rs')
-rw-r--r-- | openpgp/src/crypto/backend/nettle/aead.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/openpgp/src/crypto/backend/nettle/aead.rs b/openpgp/src/crypto/backend/nettle/aead.rs new file mode 100644 index 00000000..1614a20a --- /dev/null +++ b/openpgp/src/crypto/backend/nettle/aead.rs @@ -0,0 +1,62 @@ +//! Implementation of AEAD using Nettle cryptographic library. +use nettle::{aead, cipher}; + +use crate::{Error, Result}; + +use crate::crypto::aead::Aead; +use crate::types::{AEADAlgorithm, SymmetricAlgorithm}; + +impl<T: nettle::aead::Aead> Aead for T { + fn update(&mut self, ad: &[u8]) { + self.update(ad) + } + fn encrypt(&mut self, dst: &mut [u8], src: &[u8]) { + self.encrypt(dst, src) + } + fn decrypt(&mut self, dst: &mut [u8], src: &[u8]) { + self.decrypt(dst, src) + } + fn digest(&mut self, digest: &mut [u8]) { + self.digest(digest) + } + fn digest_size(&self) -> usize { + self.digest_size() + } +} + +impl AEADAlgorithm { + pub(crate) fn context( + &self, + sym_algo: SymmetricAlgorithm, + key: &[u8], + nonce: &[u8], + ) -> Result<Box<dyn Aead>> { + match self { + AEADAlgorithm::EAX => match sym_algo { + SymmetricAlgorithm::AES128 => Ok(Box::new( + aead::Eax::<cipher::Aes128>::with_key_and_nonce(key, nonce)?, + )), + SymmetricAlgorithm::AES192 => Ok(Box::new( + aead::Eax::<cipher::Aes192>::with_key_and_nonce(key, nonce)?, + )), + SymmetricAlgorithm::AES256 => Ok(Box::new( + aead::Eax::<cipher::Aes256>::with_key_and_nonce(key, nonce)?, + )), + SymmetricAlgorithm::Twofish => Ok(Box::new( + aead::Eax::<cipher::Twofish>::with_key_and_nonce(key, nonce)?, + )), + SymmetricAlgorithm::Camellia128 => Ok(Box::new( + aead::Eax::<cipher::Camellia128>::with_key_and_nonce(key, nonce)?, + )), + SymmetricAlgorithm::Camellia192 => Ok(Box::new( + aead::Eax::<cipher::Camellia192>::with_key_and_nonce(key, nonce)?, + )), + SymmetricAlgorithm::Camellia256 => Ok(Box::new( + aead::Eax::<cipher::Camellia256>::with_key_and_nonce(key, nonce)?, + )), + _ => Err(Error::UnsupportedSymmetricAlgorithm(sym_algo).into()), + }, + _ => Err(Error::UnsupportedAEADAlgorithm(self.clone()).into()), + } + } +} |