Skip to main content

rsa/traits/
padding.rs

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