Skip to content

Commit

Permalink
test: test privateEncrypt/publicDecrypt + padding
Browse files Browse the repository at this point in the history
Verify that RSA_NO_PADDING and RSA_PKCS1_PADDING work as advertised.

PR-URL: #27188
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
  • Loading branch information
bnoordhuis authored and danbev committed Apr 15, 2019
1 parent f6bd3b2 commit 2fed83d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/parallel/test-crypto-rsa-dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2fed83d

Please sign in to comment.