Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test privateEncrypt/publicDecrypt + padding #27188

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test: test privateEncrypt/publicDecrypt + padding
Verify that RSA_NO_PADDING and RSA_PKCS1_PADDING work as advertised.
  • Loading branch information
bnoordhuis committed Apr 11, 2019
commit 3980d0547909eac93d1f92c10d169931f7b8c125
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