Skip to main content

rsa/traits/
padding.rs

1//! Supported padding schemes.
2
3#[cfg(feature = "alloc")]
4use alloc::vec::Vec;
5
6use rand_core::TryCryptoRng;
7
8use crate::errors::Result;
9#[cfg(feature = "private-key")]
10use crate::key::RsaPrivateKey;
11use crate::traits::{PublicKeyParts, UnsignedModularInt};
12
13/// Padding scheme used for encryption.
14pub trait PaddingScheme {
15    /// Decrypt the given message using the given private key.
16    ///
17    /// If an `rng` is passed, it uses RSA blinding to help mitigate timing
18    /// side-channel attacks.
19    #[cfg(feature = "private-key")]
20    fn decrypt<Rng: TryCryptoRng + ?Sized>(
21        self,
22        rng: Option<&mut Rng>,
23        priv_key: &RsaPrivateKey,
24        ciphertext: &[u8],
25    ) -> Result<Vec<u8>>;
26
27    /// Encrypt the given message using the given public key.
28    #[cfg(feature = "alloc")]
29    fn encrypt<Rng, K, T>(self, rng: &mut Rng, pub_key: &K, msg: &[u8]) -> Result<Vec<u8>>
30    where
31        Rng: TryCryptoRng + ?Sized,
32        T: UnsignedModularInt,
33        K: PublicKeyParts<T>;
34}
35
36/// Digital signature scheme.
37pub trait SignatureScheme {
38    /// Sign the given digest.
39    #[cfg(feature = "private-key")]
40    fn sign<Rng: TryCryptoRng + ?Sized>(
41        self,
42        rng: Option<&mut Rng>,
43        priv_key: &RsaPrivateKey,
44        hashed: &[u8],
45    ) -> Result<Vec<u8>>;
46
47    /// Verify a signed message.
48    ///
49    /// `hashed` must be the result of hashing the input using the hashing function
50    /// passed in through `hash`.
51    ///
52    /// If the message is valid `Ok(())` is returned, otherwise an `Err` indicating failure.
53    fn verify<K, T>(self, pub_key: &K, hashed: &[u8], sig: &[u8]) -> Result<()>
54    where
55        T: UnsignedModularInt,
56        K: PublicKeyParts<T>;
57}