Skip to content

Commit 2fed83d

Browse files
bnoordhuisdanbev
authored andcommitted
test: test privateEncrypt/publicDecrypt + padding
Verify that RSA_NO_PADDING and RSA_PKCS1_PADDING work as advertised. PR-URL: #27188 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent f6bd3b2 commit 2fed83d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/parallel/test-crypto-rsa-dsa.js

+36
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ const decryptError = {
7474
}, encryptedBuffer);
7575
assert.strictEqual(decryptedBufferWithPassword.toString(), input);
7676

77+
// Now with explicit RSA_PKCS1_PADDING.
78+
encryptedBuffer = crypto.privateEncrypt({
79+
padding: crypto.constants.RSA_PKCS1_PADDING,
80+
key: rsaKeyPemEncrypted,
81+
passphrase: Buffer.from('password')
82+
}, bufferToEncrypt);
83+
84+
decryptedBufferWithPassword = crypto.publicDecrypt({
85+
padding: crypto.constants.RSA_PKCS1_PADDING,
86+
key: rsaKeyPemEncrypted,
87+
passphrase: Buffer.from('password')
88+
}, encryptedBuffer);
89+
assert.strictEqual(decryptedBufferWithPassword.toString(), input);
90+
91+
// Omitting padding should be okay because RSA_PKCS1_PADDING is the default.
92+
decryptedBufferWithPassword = crypto.publicDecrypt({
93+
key: rsaKeyPemEncrypted,
94+
passphrase: Buffer.from('password')
95+
}, encryptedBuffer);
96+
assert.strictEqual(decryptedBufferWithPassword.toString(), input);
97+
98+
// Now with RSA_NO_PADDING. Plaintext needs to match key size.
99+
const plaintext = 'x'.repeat(128);
100+
encryptedBuffer = crypto.privateEncrypt({
101+
padding: crypto.constants.RSA_NO_PADDING,
102+
key: rsaKeyPemEncrypted,
103+
passphrase: Buffer.from('password')
104+
}, Buffer.from(plaintext));
105+
106+
decryptedBufferWithPassword = crypto.publicDecrypt({
107+
padding: crypto.constants.RSA_NO_PADDING,
108+
key: rsaKeyPemEncrypted,
109+
passphrase: Buffer.from('password')
110+
}, encryptedBuffer);
111+
assert.strictEqual(decryptedBufferWithPassword.toString(), plaintext);
112+
77113
encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);
78114

79115
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);

0 commit comments

Comments
 (0)