diff options
Diffstat (limited to 'openpgp/src/crypto/backend/nettle/aead.rs')
-rw-r--r-- | openpgp/src/crypto/backend/nettle/aead.rs | 70 |
1 files changed, 44 insertions, 26 deletions
diff --git a/openpgp/src/crypto/backend/nettle/aead.rs b/openpgp/src/crypto/backend/nettle/aead.rs index df2f0f64..ad6014d7 100644 --- a/openpgp/src/crypto/backend/nettle/aead.rs +++ b/openpgp/src/crypto/backend/nettle/aead.rs @@ -1,7 +1,7 @@ //! Implementation of AEAD using Nettle cryptographic library. use std::cmp::Ordering; -use nettle::{aead, cipher}; +use nettle::{aead::{self, Aead as _}, cipher}; use crate::{Error, Result}; @@ -18,10 +18,6 @@ const DANGER_DISABLE_AUTHENTICATION: bool = false; impl<T: nettle::aead::Aead> seal::Sealed for T {} impl<T: nettle::aead::Aead> Aead for T { - fn update(&mut self, ad: &[u8]) -> Result<()> { - self.update(ad); - Ok(()) - } fn encrypt_seal(&mut self, dst: &mut [u8], src: &[u8]) -> Result<()> { debug_assert_eq!(dst.len(), src.len() + self.digest_size()); self.encrypt(dst, src); @@ -50,32 +46,54 @@ impl AEADAlgorithm { &self, sym_algo: SymmetricAlgorithm, key: &[u8], + aad: &[u8], nonce: &[u8], _op: CipherOp, ) -> 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)?, - )), + SymmetricAlgorithm::AES128 => { + let mut ctx = + aead::Eax::<cipher::Aes128>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, + SymmetricAlgorithm::AES192 => { + let mut ctx = + aead::Eax::<cipher::Aes192>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, + SymmetricAlgorithm::AES256 => { + let mut ctx = + aead::Eax::<cipher::Aes256>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, + SymmetricAlgorithm::Twofish => { + let mut ctx = + aead::Eax::<cipher::Twofish>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, + SymmetricAlgorithm::Camellia128 => { + let mut ctx = + aead::Eax::<cipher::Camellia128>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, + SymmetricAlgorithm::Camellia192 => { + let mut ctx = + aead::Eax::<cipher::Camellia192>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, + SymmetricAlgorithm::Camellia256 => { + let mut ctx = + aead::Eax::<cipher::Camellia256>::with_key_and_nonce(key, nonce)?; + ctx.update(aad); + Ok(Box::new(ctx)) + }, _ => Err(Error::UnsupportedSymmetricAlgorithm(sym_algo).into()), }, _ => Err(Error::UnsupportedAEADAlgorithm(*self).into()), |