use core::{convert::TryFrom, fmt};
use crate::{alloc::Cow, Algorithm};
mod generic;
mod hmacs;
#[cfg(feature = "secp256k1")]
mod es256k;
#[cfg(feature = "k256")]
mod k256;
#[cfg(any(feature = "digest-legacy", feature = "k256"))]
mod digest_compat;
#[cfg(feature = "ed25519-compact")]
mod eddsa_compact;
#[cfg(feature = "ed25519-dalek")]
mod eddsa_dalek;
#[cfg(feature = "exonum-crypto")]
mod eddsa_sodium;
#[cfg(feature = "with_rsa")]
mod rsa;
#[cfg(feature = "ed25519-compact")]
pub use self::eddsa_compact::*;
#[cfg(feature = "ed25519-dalek")]
pub use self::eddsa_dalek::Ed25519;
#[cfg(feature = "exonum-crypto")]
pub use self::eddsa_sodium::Ed25519;
#[cfg(feature = "es256k")]
pub use self::es256k::Es256k;
pub use self::generic::{SecretBytes, SigningKey, VerifyingKey};
pub use self::hmacs::*;
#[cfg(feature = "k256")]
pub use self::k256::Es256k;
#[cfg(feature = "with_rsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "with_rsa")))]
pub use self::rsa::{
ModulusBits, ModulusBitsError, Rsa, RsaParseError, RsaPrivateKey, RsaPublicKey, RsaSignature,
};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StrongKey<T>(T);
impl<T> StrongKey<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> AsRef<T> for StrongKey<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
#[derive(Debug)]
pub struct WeakKeyError<T>(pub T);
impl<T> fmt::Display for WeakKeyError<T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("Weak cryptographic key")
}
}
#[cfg(feature = "std")]
impl<T: fmt::Debug + 'static> std::error::Error for WeakKeyError<T> {}
#[derive(Debug, Clone, Copy, Default)]
pub struct StrongAlg<T>(pub T);
impl<T: Algorithm> Algorithm for StrongAlg<T>
where
StrongKey<T::SigningKey>: TryFrom<T::SigningKey>,
StrongKey<T::VerifyingKey>: TryFrom<T::VerifyingKey>,
{
type SigningKey = StrongKey<T::SigningKey>;
type VerifyingKey = StrongKey<T::VerifyingKey>;
type Signature = T::Signature;
fn name(&self) -> Cow<'static, str> {
self.0.name()
}
fn sign(&self, signing_key: &Self::SigningKey, message: &[u8]) -> Self::Signature {
self.0.sign(&signing_key.0, message)
}
fn verify_signature(
&self,
signature: &Self::Signature,
verifying_key: &Self::VerifyingKey,
message: &[u8],
) -> bool {
self.0
.verify_signature(signature, &verifying_key.0, message)
}
}