diff options
author | Kai Michaelis <kai@sequoia-pgp.org> | 2018-11-21 13:38:04 +0100 |
---|---|---|
committer | Kai Michaelis <kai@sequoia-pgp.org> | 2018-11-21 13:38:38 +0100 |
commit | 7fa35463601e156be0ac177f6f0b71c575f8f24c (patch) | |
tree | 5a2ed53ae9ecf143aa5f194edd1735483a9266bf /openpgp/src/crypto | |
parent | 771fdbd970386ff79bd5f2eca2e1fddcdf3df4f1 (diff) |
openpgp: switch to nettle 2.0
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r-- | openpgp/src/crypto/aead.rs | 14 | ||||
-rw-r--r-- | openpgp/src/crypto/ecdh.rs | 16 | ||||
-rw-r--r-- | openpgp/src/crypto/symmetric.rs | 55 |
3 files changed, 47 insertions, 38 deletions
diff --git a/openpgp/src/crypto/aead.rs b/openpgp/src/crypto/aead.rs index eeec9af2..f53b67e0 100644 --- a/openpgp/src/crypto/aead.rs +++ b/openpgp/src/crypto/aead.rs @@ -47,25 +47,25 @@ impl AEADAlgorithm { AEADAlgorithm::EAX => match cipher { SymmetricAlgorithm::AES128 => Ok(Box::new(aead::Eax::<cipher::Aes128> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), SymmetricAlgorithm::AES192 => Ok(Box::new(aead::Eax::<cipher::Aes192> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), SymmetricAlgorithm::AES256 => Ok(Box::new(aead::Eax::<cipher::Aes256> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), SymmetricAlgorithm::Twofish => Ok(Box::new(aead::Eax::<cipher::Twofish> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), SymmetricAlgorithm::Camellia128 => Ok(Box::new(aead::Eax::<cipher::Camellia128> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), SymmetricAlgorithm::Camellia192 => Ok(Box::new(aead::Eax::<cipher::Camellia192> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), SymmetricAlgorithm::Camellia256 => Ok(Box::new(aead::Eax::<cipher::Camellia256> - ::with_key_and_nonce(key, nonce))), + ::with_key_and_nonce(key, nonce)?)), _ => Err(Error::UnsupportedSymmetricAlgorithm(cipher).into()), }, diff --git a/openpgp/src/crypto/ecdh.rs b/openpgp/src/crypto/ecdh.rs index 129d23b9..9123478b 100644 --- a/openpgp/src/crypto/ecdh.rs +++ b/openpgp/src/crypto/ecdh.rs @@ -280,11 +280,11 @@ pub fn aes_key_wrap(algo: SymmetricAlgorithm, key: &[u8], // CBC, and always use an all-zero IV. let mut cipher: Box<Mode> = match algo { AES128 => Box::new( - mode::Cbc::<cipher::Aes128>::with_encrypt_key(key)), + mode::Cbc::<cipher::Aes128>::with_encrypt_key(key)?), AES192 => Box::new( - mode::Cbc::<cipher::Aes192>::with_encrypt_key(key)), + mode::Cbc::<cipher::Aes192>::with_encrypt_key(key)?), AES256 => Box::new( - mode::Cbc::<cipher::Aes256>::with_encrypt_key(key)), + mode::Cbc::<cipher::Aes256>::with_encrypt_key(key)?), _ => return Err(Error::UnsupportedSymmetricAlgorithm(algo).into()), }; @@ -318,7 +318,7 @@ pub fn aes_key_wrap(algo: SymmetricAlgorithm, key: &[u8], write_be_u64(&mut tmp[..8], a); &mut tmp[8..].copy_from_slice(&r[8 * i..8 * (i + 1)]); let mut iv = vec![0; cipher.block_size()]; // Turn CBC into ECB. - cipher.encrypt(&mut iv, &mut b, &tmp); + cipher.encrypt(&mut iv, &mut b, &tmp)?; // A = MSB(64, B) ^ t where t = (n*j)+i a = read_be_u64(&b[..8]) ^ ((n * j) + i + 1) as u64; @@ -365,11 +365,11 @@ pub fn aes_key_unwrap(algo: SymmetricAlgorithm, key: &[u8], // CBC, and always use an all-zero IV. let mut cipher: Box<Mode> = match algo { AES128 => Box::new( - mode::Cbc::<cipher::Aes128>::with_decrypt_key(key)), + mode::Cbc::<cipher::Aes128>::with_decrypt_key(key)?), AES192 => Box::new( - mode::Cbc::<cipher::Aes192>::with_decrypt_key(key)), + mode::Cbc::<cipher::Aes192>::with_decrypt_key(key)?), AES256 => Box::new( - mode::Cbc::<cipher::Aes256>::with_decrypt_key(key)), + mode::Cbc::<cipher::Aes256>::with_decrypt_key(key)?), _ => return Err(Error::UnsupportedSymmetricAlgorithm(algo).into()), }; @@ -404,7 +404,7 @@ pub fn aes_key_unwrap(algo: SymmetricAlgorithm, key: &[u8], // (Note that our i runs from n-1 to 0 instead of n to // 1, hence the index shift. let mut iv = vec![0; cipher.block_size()]; // Turn CBC into ECB. - cipher.decrypt(&mut iv, &mut b, &tmp); + cipher.decrypt(&mut iv, &mut b, &tmp)?; // A = MSB(64, B) a = read_be_u64(&b[..8]); diff --git a/openpgp/src/crypto/symmetric.rs b/openpgp/src/crypto/symmetric.rs index 8ee454cf..2caaa5a7 100644 --- a/openpgp/src/crypto/symmetric.rs +++ b/openpgp/src/crypto/symmetric.rs @@ -57,31 +57,31 @@ impl SymmetricAlgorithm { match self { SymmetricAlgorithm::TripleDES => Ok(Box::new( - mode::Cfb::<cipher::Des3>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Des3>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::Blowfish => Ok(Box::new( - mode::Cfb::<cipher::Blowfish>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Blowfish>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::AES128 => Ok(Box::new( - mode::Cfb::<cipher::Aes128>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Aes128>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::AES192 => Ok(Box::new( - mode::Cfb::<cipher::Aes192>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Aes192>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::AES256 => Ok(Box::new( - mode::Cfb::<cipher::Aes256>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Aes256>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::Twofish => Ok(Box::new( - mode::Cfb::<cipher::Twofish>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Twofish>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::Camellia128 => Ok(Box::new( - mode::Cfb::<cipher::Camellia128>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Camellia128>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::Camellia192 => Ok(Box::new( - mode::Cfb::<cipher::Camellia192>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Camellia192>::with_encrypt_key(&key[..])?)), SymmetricAlgorithm::Camellia256 => Ok(Box::new( - mode::Cfb::<cipher::Camellia256>::with_encrypt_key(&key[..]))), + mode::Cfb::<cipher::Camellia256>::with_encrypt_key(&key[..])?)), _ => Err(Error::UnsupportedSymmetricAlgorithm(self).into()), } } @@ -92,31 +92,31 @@ impl SymmetricAlgorithm { match self { SymmetricAlgorithm::TripleDES => Ok(Box::new( - mode::Cfb::<cipher::Des3>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Des3>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::Blowfish => Ok(Box::new( - mode::Cfb::<cipher::Blowfish>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Blowfish>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::AES128 => Ok(Box::new( - mode::Cfb::<cipher::Aes128>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Aes128>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::AES192 => Ok(Box::new( - mode::Cfb::<cipher::Aes192>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Aes192>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::AES256 => Ok(Box::new( - mode::Cfb::<cipher::Aes256>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Aes256>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::Twofish => Ok(Box::new( - mode::Cfb::<cipher::Twofish>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Twofish>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::Camellia128 => Ok(Box::new( - mode::Cfb::<cipher::Camellia128>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Camellia128>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::Camellia192 => Ok(Box::new( - mode::Cfb::<cipher::Camellia192>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Camellia192>::with_decrypt_key(&key[..])?)), SymmetricAlgorithm::Camellia256 => Ok(Box::new( - mode::Cfb::<cipher::Camellia256>::with_decrypt_key(&key[..]))), + mode::Cfb::<cipher::Camellia256>::with_decrypt_key(&key[..])?)), _ => Err(Error::UnsupportedSymmetricAlgorithm(self).into()) } } @@ -223,7 +223,10 @@ impl<R: io::Read> io::Read for Decryptor<R> { self.dec.decrypt(&mut self.iv, &mut plaintext[pos..pos + to_copy], - &ciphertext[..]); + &ciphertext[..]) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, + format!("{}", e)))?; + pos += to_copy; if short_read || pos == plaintext.len() { @@ -257,7 +260,9 @@ impl<R: io::Read> io::Read for Decryptor<R> { } self.buffer.truncate(ciphertext.len()); - self.dec.decrypt(&mut self.iv, &mut self.buffer, &ciphertext[..]); + self.dec.decrypt(&mut self.iv, &mut self.buffer, &ciphertext[..]) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, + format!("{}", e)))?; &plaintext[pos..pos + to_copy].copy_from_slice(&self.buffer[..to_copy]); self.buffer.drain(..to_copy); @@ -418,7 +423,7 @@ impl<W: io::Write> Encryptor<W> { if let Some(mut inner) = self.inner.take() { if self.buffer.len() > 0 { unsafe { self.scratch.set_len(self.buffer.len()) } - self.cipher.encrypt(&mut self.iv, &mut self.scratch, &self.buffer); + self.cipher.encrypt(&mut self.iv, &mut self.scratch, &self.buffer)?; self.buffer.clear(); inner.write_all(&self.scratch)?; } @@ -448,7 +453,9 @@ impl<W: io::Write> io::Write for Encryptor<W> { // And possibly encrypt the block. if self.buffer.len() == self.block_size { - self.cipher.encrypt(&mut self.iv, &mut self.scratch, &self.buffer); + self.cipher.encrypt(&mut self.iv, &mut self.scratch, &self.buffer) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, + format!("{}", e)))?; self.buffer.clear(); inner.write_all(&self.scratch)?; } @@ -459,7 +466,9 @@ impl<W: io::Write> io::Write for Encryptor<W> { for block in buf.chunks(self.block_size) { if block.len() == self.block_size { // Complete block. - self.cipher.encrypt(&mut self.iv, &mut self.scratch, block); + self.cipher.encrypt(&mut self.iv, &mut self.scratch, block) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, + format!("{}", e)))?; inner.write_all(&self.scratch)?; } else { // Stash for later. |