Unable to decrypt RSA-OAEP message encrypted with PyCryptodome using SubtleCrypto Web Crypto API

On the server side I am using PyCryptodome to encrypt a message with RSA-OAEP (with SHA-256).
I'm trying to decrypt the message using SubtleCrypto Web Crypto API on the client side, but it give me a DOMException error without no further details.
On SubtleCrypto I can import the private key generated in PyCryptodome without problems, but it gives me the error when I'm trying to decrypt the message.

I have also tried to import the public key generated on PyCryptodome on client side to encrypt the same message with SubtleCrypto. In that case I can decrypt it without problems, using the same flow as before.

Are the RSA-OAEP algorithms between these two libraries incompatible? I noticed that PyCryptodome references RFC 8017(v2.2) and SubtleCrypto RFC 3447(v2.1) in their respective documentation.

Edit:

Upvotes: 3

Views: 1675

Answers (1)

Topaco
Topaco

Reputation: 49121

PyCryptodome does not apply SHA-256 as default digest for OAEP, but SHA-1, here. Accordingly SHA-1 must be used on the WebCrypto side:

let algorithmParams: RsaHashedImportParams = {
    name: "RSA-OAEP",
    hash: "SHA-1"
};

Of course you can also apply SHA-256 on the PyCryptodome side, then no changes are necessary on the WebCrypto side.

from Crypto.Hash import SHA256
...
rsa_encryption_cipher = PKCS1_OAEP.new(key, SHA256) # default: Crypto.Hash.SHA1

With consistent digests on both sides I can successfully decrypt a ciphertext with your WebCrypto code, which I have previously generated with your PyCryptodome code (using my own keys).

Upvotes: 7

Related Questions