rsa/traits/
padding.rs

1//! Supported padding schemes.
2
3use alloc::vec::Vec;
4
5use rand_core::CryptoRngCore;
6
7use crate::errors::Result;
8use crate::key::{RsaPrivateKey, RsaPublicKey};
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
15    /// side-channel attacks.
16    fn decrypt<Rng: CryptoRngCore>(
17        self,
18        rng: Option<&mut Rng>,
19        priv_key: &RsaPrivateKey,
20        ciphertext: &[u8],
21    ) -> Result<Vec<u8>>;
22
23    /// Encrypt the given message using the given public key.
24    fn encrypt<Rng: CryptoRngCore>(
25        self,
26        rng: &mut Rng,
27        pub_key: &RsaPublicKey,
28        msg: &[u8],
29    ) -> Result<Vec<u8>>;
30}
31
32/// Digital signature scheme.
33pub trait SignatureScheme {
34    /// Sign the given digest.
35    fn sign<Rng: CryptoRngCore>(
36        self,
37        rng: Option<&mut Rng>,
38        priv_key: &RsaPrivateKey,
39        hashed: &[u8],
40    ) -> Result<Vec<u8>>;
41
42    /// Verify a signed message.
43    ///
44    /// `hashed` must be the result of hashing the input using the hashing function
45    /// passed in through `hash`.
46    ///
47    /// If the message is valid `Ok(())` is returned, otherwise an `Err` indicating failure.
48    fn verify(self, pub_key: &RsaPublicKey, hashed: &[u8], sig: &[u8]) -> Result<()>;
49}