diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-05-08 10:03:25 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-05-22 11:59:46 +0200 |
commit | 20a0b8ecf8f9a89e9c448299d8e7162ed55618f6 (patch) | |
tree | 404153e7d761d96b135be38d47b38aae2bc47010 /openpgp | |
parent | 4bcaebc7515ed15bb403a312532e8870a781fb3a (diff) |
openpgp: Introduce a trait for the crypto backends.
- This trait will not be public, at least for now, and we will still
stick to the compile-time backend selection, at least for now.
Therefore, we can still enjoy static dispatch.
- The trait will formalize the interface. It should only abstract
over the underlying primitives. Notably, we want to be able to
implement all operations on packets using this interface, so that
no user-facing functions are implemented in the crypto backends.
This will lead to a more consistent experience across all backends.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/crypto/backend.rs | 20 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/botan.rs | 9 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/cng.rs | 11 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/interface.rs | 11 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/nettle.rs | 21 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/openssl.rs | 9 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/rust.rs | 15 | ||||
-rw-r--r-- | openpgp/src/crypto/mod.rs | 3 |
8 files changed, 73 insertions, 26 deletions
diff --git a/openpgp/src/crypto/backend.rs b/openpgp/src/crypto/backend.rs index 2aecfb69..03c142ea 100644 --- a/openpgp/src/crypto/backend.rs +++ b/openpgp/src/crypto/backend.rs @@ -1,6 +1,7 @@ //! Concrete implementation of the crypto primitives used by the rest of the //! crypto API. +pub(crate) mod interface; pub(crate) mod sha1cd; // Nettle is the default backend, but on Windows targets we instead @@ -25,6 +26,12 @@ mod nettle; feature = "crypto-botan2", feature = "crypto-rust")))))] pub use self::nettle::*; +#[cfg(all(feature = "crypto-nettle", + not(all(feature = "__implicit-crypto-backend-for-tests", + any(feature = "crypto-openssl", + feature = "crypto-botan", + feature = "crypto-rust")))))] +pub use self::nettle::Backend; // Nettle is the default backend, but on Windows targets we instead // enable CNG for running the tests in non-leaf crates that depend on @@ -50,18 +57,31 @@ mod cng; feature = "crypto-botan2", feature = "crypto-rust")))))] pub use self::cng::*; +#[cfg(all(feature = "crypto-cng", + not(all(feature = "__implicit-crypto-backend-for-tests", + any(feature = "crypto-nettle", + feature = "crypto-openssl", + feature = "crypto-botan", + feature = "crypto-rust")))))] +pub use self::cng::Backend; #[cfg(feature = "crypto-rust")] mod rust; #[cfg(feature = "crypto-rust")] pub use self::rust::*; +#[cfg(feature = "crypto-rust")] +pub use self::rust::Backend; #[cfg(feature = "crypto-openssl")] mod openssl; #[cfg(feature = "crypto-openssl")] pub use self::openssl::*; +#[cfg(feature = "crypto-openssl")] +pub use self::openssl::Backend; #[cfg(any(feature = "crypto-botan", feature = "crypto-botan2"))] mod botan; #[cfg(any(feature = "crypto-botan", feature = "crypto-botan2"))] pub use self::botan::*; +#[cfg(feature = "crypto-botan")] +pub use self::botan::Backend; diff --git a/openpgp/src/crypto/backend/botan.rs b/openpgp/src/crypto/backend/botan.rs index 975d6196..04951225 100644 --- a/openpgp/src/crypto/backend/botan.rs +++ b/openpgp/src/crypto/backend/botan.rs @@ -8,9 +8,12 @@ pub mod ecdh; pub mod hash; pub mod symmetric; -/// Returns a short, human-readable description of the backend. -pub fn backend() -> String { - "Botan".to_string() +pub struct Backend(()); + +impl super::interface::Backend for Backend { + fn backend() -> String { + "Botan".to_string() + } } /// Fills the given buffer with random data. diff --git a/openpgp/src/crypto/backend/cng.rs b/openpgp/src/crypto/backend/cng.rs index 3ded3f5b..96a4c2c0 100644 --- a/openpgp/src/crypto/backend/cng.rs +++ b/openpgp/src/crypto/backend/cng.rs @@ -10,10 +10,13 @@ pub mod ecdh; pub mod hash; pub mod symmetric; -/// Returns a short, human-readable description of the backend. -pub fn backend() -> String { - // XXX: can we include features and the version? - "Windows CNG".to_string() +pub struct Backend(()); + +impl super::interface::Backend for Backend { + fn backend() -> String { + // XXX: can we include features and the version? + "Windows CNG".to_string() + } } /// Fills the given buffer with random data. diff --git a/openpgp/src/crypto/backend/interface.rs b/openpgp/src/crypto/backend/interface.rs new file mode 100644 index 00000000..d0c8ffff --- /dev/null +++ b/openpgp/src/crypto/backend/interface.rs @@ -0,0 +1,11 @@ +//! The crypto-backend abstraction. + +/// Abstracts over the cryptographic backends. +pub trait Backend { + /// Returns a short, human-readable description of the backend. + /// + /// This starts with the name of the backend, possibly a version, + /// and any optional features that are available. This is meant + /// for inclusion in version strings to improve bug reports. + fn backend() -> String; +} diff --git a/openpgp/src/crypto/backend/nettle.rs b/openpgp/src/crypto/backend/nettle.rs index 7109fa3a..0800540f 100644 --- a/openpgp/src/crypto/backend/nettle.rs +++ b/openpgp/src/crypto/backend/nettle.rs @@ -10,15 +10,18 @@ pub mod ecdh; pub mod hash; pub mod symmetric; -/// Returns a short, human-readable description of the backend. -pub fn backend() -> String { - let (major, minor) = nettle::version(); - format!( - "Nettle {}.{} (Cv448: {:?}, OCB: {:?})", - major, minor, - nettle::curve448::IS_SUPPORTED, - nettle::aead::OCB_IS_SUPPORTED, - ) +pub struct Backend(()); + +impl super::interface::Backend for Backend { + fn backend() -> String { + let (major, minor) = nettle::version(); + format!( + "Nettle {}.{} (Cv448: {:?}, OCB: {:?})", + major, minor, + nettle::curve448::IS_SUPPORTED, + nettle::aead::OCB_IS_SUPPORTED, + ) + } } /// Fills the given buffer with random data. diff --git a/openpgp/src/crypto/backend/openssl.rs b/openpgp/src/crypto/backend/openssl.rs index 1c077cf2..e4f73418 100644 --- a/openpgp/src/crypto/backend/openssl.rs +++ b/openpgp/src/crypto/backend/openssl.rs @@ -9,9 +9,12 @@ pub mod ecdh; pub mod hash; pub mod symmetric; -/// Returns a short, human-readable description of the backend. -pub fn backend() -> String { - "OpenSSL".to_string() +pub struct Backend(()); + +impl super::interface::Backend for Backend { + fn backend() -> String { + "OpenSSL".to_string() + } } /// Fills the given buffer with random data. diff --git a/openpgp/src/crypto/backend/rust.rs b/openpgp/src/crypto/backend/rust.rs index 8dd85559..2a057569 100644 --- a/openpgp/src/crypto/backend/rust.rs +++ b/openpgp/src/crypto/backend/rust.rs @@ -12,6 +12,15 @@ pub mod ecdh; pub mod hash; pub mod symmetric; +pub struct Backend(()); + +impl super::interface::Backend for Backend { + fn backend() -> String { + // XXX: can we include features and the version? + "RustCrypto".to_string() + } +} + trait GenericArrayExt<T, N: ArrayLength<T>> { const LEN: usize; @@ -44,12 +53,6 @@ impl<T, N: ArrayLength<T>> GenericArrayExt<T, N> for GenericArray<T, N> { const LEN: usize = N::USIZE; } -/// Returns a short, human-readable description of the backend. -pub fn backend() -> String { - // XXX: can we include features and the version? - "RustCrypto".to_string() -} - /// Fills the given buffer with random data. pub fn random(buf: &mut [u8]) { use rand07::rngs::OsRng; diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs index 6dc2002d..12527b70 100644 --- a/openpgp/src/crypto/mod.rs +++ b/openpgp/src/crypto/mod.rs @@ -49,7 +49,8 @@ mod tests; /// any optional features that are available. This is meant for /// inclusion in version strings to improve bug reports. pub fn backend() -> String { - backend::backend() + use backend::interface::Backend; + backend::Backend::backend() } /// Fills the given buffer with random data. |