1use core::fmt;
5
6use crate::{alloc::Cow, Algorithm};
7
8mod generic;
9mod hmacs;
10#[cfg(feature = "secp256k1")]
12mod es256k;
13#[cfg(feature = "k256")]
14mod k256;
15#[cfg(feature = "ed25519-compact")]
17mod eddsa_compact;
18#[cfg(feature = "ed25519-dalek")]
19mod eddsa_dalek;
20#[cfg(feature = "exonum-crypto")]
21mod eddsa_sodium;
22#[cfg(feature = "p256")]
24mod p256;
25#[cfg(feature = "rsa")]
27mod rsa;
28
29#[cfg(feature = "ed25519-compact")]
30pub use self::eddsa_compact::*;
31#[cfg(feature = "ed25519-dalek")]
32pub use self::eddsa_dalek::Ed25519;
33#[cfg(feature = "exonum-crypto")]
34pub use self::eddsa_sodium::Ed25519;
35#[cfg(feature = "es256k")]
36pub use self::es256k::Es256k;
37#[cfg(feature = "k256")]
38pub use self::k256::Es256k;
39#[cfg(feature = "p256")]
40pub use self::p256::Es256;
41#[cfg(feature = "rsa")]
42#[cfg_attr(docsrs, doc(cfg(feature = "rsa")))]
43pub use self::rsa::{
44 ModulusBits, ModulusBitsError, Rsa, RsaError, RsaParseError, RsaPrivateKey, RsaPublicKey, RsaSignature,
45};
46pub use self::{
47 generic::{SecretBytes, SigningKey, VerifyingKey},
48 hmacs::*,
49};
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub struct StrongKey<T>(T);
72
73impl<T> StrongKey<T> {
74 pub fn into_inner(self) -> T {
76 self.0
77 }
78}
79
80impl<T> AsRef<T> for StrongKey<T> {
81 fn as_ref(&self) -> &T {
82 &self.0
83 }
84}
85
86#[derive(Debug)]
90pub struct WeakKeyError<T>(pub T);
91
92impl<T> fmt::Display for WeakKeyError<T> {
93 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
94 formatter.write_str("Weak cryptographic key")
95 }
96}
97
98#[cfg(feature = "std")]
99impl<T: fmt::Debug + 'static> std::error::Error for WeakKeyError<T> {}
100
101#[derive(Debug, Clone, Copy, Default)]
125pub struct StrongAlg<T>(pub T);
126
127#[allow(clippy::trait_duplication_in_bounds)] impl<T: Algorithm> Algorithm for StrongAlg<T>
129where
130 StrongKey<T::SigningKey>: TryFrom<T::SigningKey>,
131 StrongKey<T::VerifyingKey>: TryFrom<T::VerifyingKey>,
132{
133 type Signature = T::Signature;
134 type SigningKey = StrongKey<T::SigningKey>;
135 type VerifyingKey = StrongKey<T::VerifyingKey>;
136
137 fn name(&self) -> Cow<'static, str> {
138 self.0.name()
139 }
140
141 fn sign(&self, signing_key: &Self::SigningKey, message: &[u8]) -> Self::Signature {
142 self.0.sign(&signing_key.0, message)
143 }
144
145 fn verify_signature(
146 &self,
147 signature: &Self::Signature,
148 verifying_key: &Self::VerifyingKey,
149 message: &[u8],
150 ) -> bool {
151 self.0.verify_signature(signature, &verifying_key.0, message)
152 }
153}