summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto/backend
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 /openpgp/src/crypto/backend
parentd673821c1467a0ddc9ff3e1fa755ccb718e36125 (diff)
openpgp: Adjust for SymmetricAlgorithm support diff. across backends
Diffstat (limited to 'openpgp/src/crypto/backend')
-rw-r--r--openpgp/src/crypto/backend/cng/symmetric.rs33
-rw-r--r--openpgp/src/crypto/backend/nettle/symmetric.rs29
2 files changed, 58 insertions, 4 deletions
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> {