RustCrypto / RSA

RSA implementation in pure Rust
Apache License 2.0
557 stars 154 forks source link

How to implement RSA_PKCS1_OAEP_PADDING #435

Closed sn99 closed 5 months ago

sn99 commented 5 months ago

It is defined as :

RSA_PKCS1_OAEP_PADDING

    EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode is recommended for all new applications.

I am trying to copy node.js behavior defined here: https://nodejs.org/api/crypto.html#cryptopublicencryptkey-buffer. For something like: crypto.publicEncrypt(this.publicKey(), Buffer.from(data)).

Would this actually be correct Rust implementation:

let public_key = RsaPublicKey::from_public_key_pem(&public_key)?;
let padding = Oaep::new_with_mgf_hash::<Sha256, Sha1>();

let encrypted_data = public_key.encrypt(&mut rng, padding, data.as_bytes())?;
tarcieri commented 5 months ago

Yes, that looks about right. You can also use the types in the rsa::oaep module like EncryptingKey.