From 2fed83dee884c3bddafa67bb53abf507db1a8ba3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 11 Apr 2019 09:06:47 +0200 Subject: [PATCH] test: test privateEncrypt/publicDecrypt + padding Verify that RSA_NO_PADDING and RSA_PKCS1_PADDING work as advertised. PR-URL: https://github.com/nodejs/node/pull/27188 Reviewed-By: Colin Ihrig Reviewed-By: Sam Roberts Reviewed-By: Daniel Bevenius --- test/parallel/test-crypto-rsa-dsa.js | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/parallel/test-crypto-rsa-dsa.js b/test/parallel/test-crypto-rsa-dsa.js index 143b7c2c41276a..35839834087905 100644 --- a/test/parallel/test-crypto-rsa-dsa.js +++ b/test/parallel/test-crypto-rsa-dsa.js @@ -74,6 +74,42 @@ const decryptError = { }, encryptedBuffer); assert.strictEqual(decryptedBufferWithPassword.toString(), input); + // Now with explicit RSA_PKCS1_PADDING. + encryptedBuffer = crypto.privateEncrypt({ + padding: crypto.constants.RSA_PKCS1_PADDING, + key: rsaKeyPemEncrypted, + passphrase: Buffer.from('password') + }, bufferToEncrypt); + + decryptedBufferWithPassword = crypto.publicDecrypt({ + padding: crypto.constants.RSA_PKCS1_PADDING, + key: rsaKeyPemEncrypted, + passphrase: Buffer.from('password') + }, encryptedBuffer); + assert.strictEqual(decryptedBufferWithPassword.toString(), input); + + // Omitting padding should be okay because RSA_PKCS1_PADDING is the default. + decryptedBufferWithPassword = crypto.publicDecrypt({ + key: rsaKeyPemEncrypted, + passphrase: Buffer.from('password') + }, encryptedBuffer); + assert.strictEqual(decryptedBufferWithPassword.toString(), input); + + // Now with RSA_NO_PADDING. Plaintext needs to match key size. + const plaintext = 'x'.repeat(128); + encryptedBuffer = crypto.privateEncrypt({ + padding: crypto.constants.RSA_NO_PADDING, + key: rsaKeyPemEncrypted, + passphrase: Buffer.from('password') + }, Buffer.from(plaintext)); + + decryptedBufferWithPassword = crypto.publicDecrypt({ + padding: crypto.constants.RSA_NO_PADDING, + key: rsaKeyPemEncrypted, + passphrase: Buffer.from('password') + }, encryptedBuffer); + assert.strictEqual(decryptedBufferWithPassword.toString(), plaintext); + encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt); decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);