summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Matuszewski <igor@sequoia-pgp.org>2020-06-15 02:52:31 +0200
committerIgor Matuszewski <igor@sequoia-pgp.org>2020-08-13 15:19:58 +0200
commitfb5fe6b01c0cc200a9264a7085d0714fb06ef0a1 (patch)
tree84f577298af40f6347b8047198df43c3e368b87d
parentd673821c1467a0ddc9ff3e1fa755ccb718e36125 (diff)
openpgp: Adjust for SymmetricAlgorithm support diff. across backends
-rw-r--r--openpgp/src/crypto/aead.rs4
-rw-r--r--openpgp/src/crypto/backend/cng/symmetric.rs33
-rw-r--r--openpgp/src/crypto/backend/nettle/symmetric.rs29
-rw-r--r--openpgp/src/crypto/s2k.rs2
-rw-r--r--openpgp/src/crypto/symmetric.rs4
-rw-r--r--openpgp/src/parse.rs15
-rw-r--r--openpgp/src/parse/hashed_reader.rs2
-rw-r--r--openpgp/src/types/mod.rs29
8 files changed, 81 insertions, 37 deletions
diff --git a/openpgp/src/crypto/aead.rs b/openpgp/src/crypto/aead.rs
index 5858aacb..68595d96 100644
--- a/openpgp/src/crypto/aead.rs
+++ b/openpgp/src/crypto/aead.rs
@@ -775,7 +775,9 @@ mod tests {
SymmetricAlgorithm::Twofish,
SymmetricAlgorithm::Camellia128,
SymmetricAlgorithm::Camellia192,
- SymmetricAlgorithm::Camellia256].iter() {
+ SymmetricAlgorithm::Camellia256]
+ .iter()
+ .filter(|algo| algo.is_supported()) {
for aead in [AEADAlgorithm::EAX].iter() {
let version = 1;
let chunk_size = 64;
diff --git a/openpgp/src/crypto/backend/cng/symmetric.rs b/openpgp/src/crypto/backend/cng/symmetric.rs
index 2d3382a0..a4a140eb 100644
--- a/openpgp/src/crypto/backend/cng/symmetric.rs
+++ b/openpgp/src/crypto/backend/cng/symmetric.rs
@@ -160,8 +160,33 @@ impl TryFrom<SymmetricAlgorithm> for (cng::SymmetricAlgorithmId, usize) {
}
impl SymmetricAlgorithm {
- /// Length of a key for this algorithm in bytes. Fails if Sequoia
- /// does not support this algorithm.
+ /// Returns whether this algorithm is supported by the crypto backend.
+ ///
+ /// All backends support all the AES variants.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use sequoia_openpgp as openpgp;
+ /// use openpgp::types::SymmetricAlgorithm;
+ ///
+ /// assert!(SymmetricAlgorithm::AES256.is_supported());
+ /// assert!(SymmetricAlgorithm::TripleDES.is_supported());
+ ///
+ /// assert!(!SymmetricAlgorithm::IDEA.is_supported());
+ /// assert!(!SymmetricAlgorithm::Unencrypted.is_supported());
+ /// assert!(!SymmetricAlgorithm::Private(101).is_supported());
+ /// ```
+ pub fn is_supported(&self) -> bool {
+ use self::SymmetricAlgorithm::*;
+ match self {
+ AES128 | AES192 | AES256 | TripleDES => true,
+ _ => false,
+ }
+ }
+
+ /// Length of a key for this algorithm in bytes. Fails if the crypto
+ /// backend does not support this algorithm.
pub fn key_size(self) -> Result<usize> {
Ok(match self {
SymmetricAlgorithm::TripleDES => 24,
@@ -172,8 +197,8 @@ impl SymmetricAlgorithm {
})
}
- /// Length of a block for this algorithm in bytes. Fails if
- /// Sequoia does not support this algorithm.
+ /// Length of a block for this algorithm in bytes. Fails if the crypto
+ /// backend does not support this algorithm.
pub fn block_size(self) -> Result<usize> {
Ok(match self {
SymmetricAlgorithm::TripleDES => 8,
diff --git a/openpgp/src/crypto/backend/nettle/symmetric.rs b/openpgp/src/crypto/backend/nettle/symmetric.rs
index 9611701d..4883a5a1 100644
--- a/openpgp/src/crypto/backend/nettle/symmetric.rs
+++ b/openpgp/src/crypto/backend/nettle/symmetric.rs
@@ -33,6 +33,35 @@ impl<T: nettle::mode::Mode> Mode for T {
}
impl SymmetricAlgorithm {
+ /// Returns whether this algorithm is supported by the crypto backend.
+ ///
+ /// All backends support all the AES variants.
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use sequoia_openpgp as openpgp;
+ /// use openpgp::types::SymmetricAlgorithm;
+ ///
+ /// assert!(SymmetricAlgorithm::AES256.is_supported());
+ /// assert!(SymmetricAlgorithm::TripleDES.is_supported());
+ ///
+ /// assert!(!SymmetricAlgorithm::IDEA.is_supported());
+ /// assert!(!SymmetricAlgorithm::Unencrypted.is_supported());
+ /// assert!(!SymmetricAlgorithm::Private(101).is_supported());
+ /// ```
+ pub fn is_supported(&self) -> bool {
+ use self::SymmetricAlgorithm::*;
+ match &self {
+ TripleDES | CAST5 | Blowfish | AES128 | AES192 | AES256 | Twofish
+ | Camellia128 | Camellia192 | Camellia256
+ => true,
+ Unencrypted | IDEA | Private(_) | Unknown(_)
+ => false,
+ __Nonexhaustive => unreachable!(),
+ }
+ }
+
/// Length of a key for this algorithm in bytes. Fails if Sequoia
/// does not support this algorithm.
pub fn key_size(self) -> Result<usize> {
diff --git a/openpgp/src/crypto/s2k.rs b/openpgp/src/crypto/s2k.rs
index 1210aa8b..62e8c9a3 100644
--- a/openpgp/src/crypto/s2k.rs
+++ b/openpgp/src/crypto/s2k.rs
@@ -455,7 +455,7 @@ mod tests {
},
];
- for test in tests.iter() {
+ for test in tests.iter().filter(|t| t.cipher_algo.is_supported()) {
let path = crate::tests::message(&format!("s2k/{}", test.filename));
let pp = PacketParser::from_bytes(path).unwrap().unwrap();
if let Packet::SKESK(SKESK::V4(ref skesk)) = pp.packet {
diff --git a/openpgp/src/crypto/symmetric.rs b/openpgp/src/crypto/symmetric.rs
index 81d4a61d..67d64480 100644
--- a/openpgp/src/crypto/symmetric.rs
+++ b/openpgp/src/crypto/symmetric.rs
@@ -559,7 +559,9 @@ mod tests {
SymmetricAlgorithm::Twofish,
SymmetricAlgorithm::Camellia128,
SymmetricAlgorithm::Camellia192,
- SymmetricAlgorithm::Camellia256].iter() {
+ SymmetricAlgorithm::Camellia256]
+ .iter()
+ .filter(|x| x.is_supported()) {
let mut key = vec![0; algo.key_size().unwrap()];
crate::crypto::random(&mut key);
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs
index 6db6a4d7..9375ffaf 100644
--- a/openpgp/src/parse.rs
+++ b/openpgp/src/parse.rs
@@ -5285,6 +5285,11 @@ mod test {
fn decrypt_test_common(stream: bool) {
for test in DECRYPT_TESTS.iter() {
+ if !test.algo.is_supported() {
+ eprintln!("Algorithm {} unsupported, skipping", test.algo);
+ continue;
+ }
+
eprintln!("Decrypting {}, streaming content: {}",
test.filename, stream);
@@ -5358,6 +5363,11 @@ mod test {
#[test]
fn message_validator() {
for test in DECRYPT_TESTS.iter() {
+ if !test.algo.is_supported() {
+ eprintln!("Algorithm {} unsupported, skipping", test.algo);
+ continue;
+ }
+
let mut ppr = PacketParserBuilder::from_bytes(
crate::tests::message(test.filename)).unwrap()
.build()
@@ -5482,6 +5492,11 @@ mod test {
#[test]
fn path() {
for test in DECRYPT_TESTS.iter() {
+ if !test.algo.is_supported() {
+ eprintln!("Algorithm {} unsupported, skipping", test.algo);
+ continue;
+ }
+
eprintln!("Decrypting {}", test.filename);
let mut ppr = PacketParserBuilder::from_bytes(
diff --git a/openpgp/src/parse/hashed_reader.rs b/openpgp/src/parse/hashed_reader.rs
index 0a26a617..e578293f 100644
--- a/openpgp/src/parse/hashed_reader.rs
+++ b/openpgp/src/parse/hashed_reader.rs
@@ -321,7 +321,7 @@ mod test {
"5bea68c8c696bbed95e152d61c446ad0e05bf68f7df39cbfeae568bee6f6691c840fb1d5dd2599737b08dbb33eed344b"),
(HashAlgorithm::SHA512,
"5fa032487774082af5cc833c2db5f943e31cc75cd2bfaa7d9bbd0ccabf5403b6dbcb484254727a524588f20e9ef336d8ce8533332c5ac1b9d50af3003a0da8d8"),
- ].iter().cloned().collect(),
+ ].iter().filter(|(hash, _)| hash.is_supported()).cloned().collect(),
},
];
diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs
index db62415b..dff23cc3 100644
--- a/openpgp/src/types/mod.rs
+++ b/openpgp/src/types/mod.rs
@@ -589,35 +589,6 @@ impl Default for SymmetricAlgorithm {
}
}
-impl SymmetricAlgorithm {
- /// Returns whether this algorithm is supported.
- ///
- /// # Examples
- ///
- /// ```rust
- /// use sequoia_openpgp as openpgp;
- /// use openpgp::types::SymmetricAlgorithm;
- ///
- /// assert!(SymmetricAlgorithm::AES256.is_supported());
- /// assert!(SymmetricAlgorithm::TripleDES.is_supported());
- ///
- /// assert!(!SymmetricAlgorithm::IDEA.is_supported());
- /// assert!(!SymmetricAlgorithm::Unencrypted.is_supported());
- /// assert!(!SymmetricAlgorithm::Private(101).is_supported());
- /// ```
- pub fn is_supported(&self) -> bool {
- use self::SymmetricAlgorithm::*;
- match &self {
- TripleDES | CAST5 | Blowfish | AES128 | AES192 | AES256 | Twofish
- | Camellia128 | Camellia192 | Camellia256
- => true,
- Unencrypted | IDEA | Private(_) | Unknown(_)
- => false,
- __Nonexhaustive => unreachable!(),
- }
- }
-}
-
impl From<u8> for SymmetricAlgorithm {
fn from(u: u8) -> Self {
match u {