0

I am playing around with RSA encryption and I have just been using the standard PCKS1 padding which works fine, but I would like to use the more advanced OAEP or PSS Padding schemes. But for some reason when I switch the constant from PCKS1 to PKCS1_OAEP, it compiles but I get a run-time error. Which implies to me that the functionality is there but I am doing something wrong. Here's my code

use openssl::{rsa::{Rsa, Padding}, symm::Cipher};
use bincode;

fn main() {
    let rsa = Rsa::generate(512).unwrap();
    let password = "password";
    let source = "hello paul".to_string();
    let data = bincode::serialize(&source).unwrap();
    let private = rsa.private_key_to_pem_passphrase(Cipher::aes_128_cbc(), password.as_bytes()).unwrap();

    //encrypt
    let private_key = Rsa::private_key_from_pem_passphrase(&private, password.as_bytes()).unwrap();

    let mut enc_data = vec![0; private_key.size() as usize];
    match private_key.private_encrypt(&data, &mut enc_data, Padding::PKCS1_OAEP) {
        Ok(_) => {},
        Err(e) => {
            println!("{e}");
            return;
        }
    }
}

and my Cargo.toml dependencies

[dependencies]
openssl-sys = "0.9.79"
openssl = "0.10.44"
chrono = "0.4"
bincode = "1.0"
serde = { version = "1.0", features = ["derive"] }

and here is the error I am getting.

error:04066076:rsa routines:rsa_ossl_private_encrypt:unknown padding type:crypto/rsa/rsa_ossl.c:273:

So I am getting this from this resource(https://docs.rs/openssl/latest/openssl/rsa/struct.Padding.html#associatedconstant.PKCS1_OAEP) I ahve also tried it with PKCS1_PSS and get the same error. Does anyone know whats up, maybe they never actually finished OAEP or PSS, or is there something wrong on my end? Maybe I need to use a different Cipher than aes_128_cbc? Thanks for reading any help is appreciated.

  • 1
    `private_encrypt` is really for signing, so only PKCS1_PSS *might* make sense there. You might try the `public_encrypt` method (assuming there is one, I know nothing about Rust) with PKCS1_OAEP. I don't which method lets you use PKCS1_PSS for signing. – President James K. Polk Dec 07 '22 at 21:32
  • You should also increase the key size. 512 bits is too small, not only for security reasons, but also with regard to the max. message size or the compatible digest output size. Nowadays the RSA key size should be at least 2048 bits. – Topaco Dec 07 '22 at 23:38

0 Answers0