use super::{AuthProtocol, PrivProtocol};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CryptoError {
UnsupportedAlgorithm(&'static str),
InvalidKeyLength,
CipherError,
RandomSource,
}
impl std::fmt::Display for CryptoError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnsupportedAlgorithm(name) => {
write!(f, "unsupported algorithm: {}", name)
}
Self::InvalidKeyLength => write!(f, "invalid key length"),
Self::CipherError => write!(f, "cipher operation failed"),
Self::RandomSource => write!(f, "OS random source unavailable"),
}
}
}
impl std::error::Error for CryptoError {}
pub type CryptoResult<T> = Result<T, CryptoError>;
#[cfg(all(feature = "crypto-rustcrypto", feature = "crypto-fips"))]
compile_error!(
"Features \"crypto-rustcrypto\" and \"crypto-fips\" are mutually exclusive. If you used --all-features, specify features explicitly instead."
);
#[cfg(not(any(feature = "crypto-rustcrypto", feature = "crypto-fips")))]
compile_error!(
"A crypto backend is required. Enable either \"crypto-rustcrypto\" (default) or \"crypto-fips\"."
);
#[cfg(feature = "crypto-rustcrypto")]
mod rustcrypto;
#[cfg(feature = "crypto-rustcrypto")]
pub use rustcrypto::RustCryptoProvider;
#[cfg(feature = "crypto-fips")]
mod fips;
#[cfg(feature = "crypto-fips")]
pub use fips::AwsLcFipsProvider;
pub trait CryptoProvider: Send + Sync + 'static {
fn password_to_key(&self, protocol: AuthProtocol, password: &[u8]) -> CryptoResult<Vec<u8>>;
fn localize_key(
&self,
protocol: AuthProtocol,
master_key: &[u8],
engine_id: &[u8],
) -> CryptoResult<Vec<u8>>;
fn compute_hmac(
&self,
protocol: AuthProtocol,
key: &[u8],
slices: &[&[u8]],
truncate_len: usize,
) -> CryptoResult<Vec<u8>>;
fn encrypt(
&self,
protocol: PrivProtocol,
key: &[u8],
iv: &[u8],
data: &mut [u8],
) -> CryptoResult<()>;
fn hash(&self, protocol: AuthProtocol, data: &[u8]) -> CryptoResult<Vec<u8>>;
fn decrypt(
&self,
protocol: PrivProtocol,
key: &[u8],
iv: &[u8],
data: &mut [u8],
) -> CryptoResult<()>;
}
#[cfg(feature = "crypto-rustcrypto")]
pub(crate) fn provider() -> &'static RustCryptoProvider {
&RustCryptoProvider
}
#[cfg(feature = "crypto-fips")]
pub(crate) fn provider() -> &'static AwsLcFipsProvider {
&AwsLcFipsProvider
}