Skip to content

Commit be0b53d

Browse files
tniessentargos
authored andcommitted
crypto: fix key requirements in asymmetric cipher
PR-URL: #30249 Fixes: #30237 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 671e028 commit be0b53d

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

lib/internal/crypto/cipher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ function rsaFunctionFor(method, defaultPadding, keyType) {
6666
const publicEncrypt = rsaFunctionFor(_publicEncrypt, RSA_PKCS1_OAEP_PADDING,
6767
'public');
6868
const publicDecrypt = rsaFunctionFor(_publicDecrypt, RSA_PKCS1_PADDING,
69-
'private');
69+
'public');
7070
const privateEncrypt = rsaFunctionFor(_privateEncrypt, RSA_PKCS1_PADDING,
7171
'private');
7272
const privateDecrypt = rsaFunctionFor(_privateDecrypt, RSA_PKCS1_OAEP_PADDING,
73-
'public');
73+
'private');
7474

7575
function getDecoder(decoder, encoding) {
7676
encoding = normalizeEncoding(encoding);

test/parallel/test-crypto-key-objects.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const {
1515
createPrivateKey,
1616
KeyObject,
1717
randomBytes,
18+
publicDecrypt,
1819
publicEncrypt,
19-
privateDecrypt
20+
privateDecrypt,
21+
privateEncrypt
2022
} = require('crypto');
2123

2224
const fixtures = require('../common/fixtures');
@@ -156,7 +158,16 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
156158
assert(Buffer.isBuffer(privateDER));
157159

158160
const plaintext = Buffer.from('Hello world', 'utf8');
159-
const ciphertexts = [
161+
const testDecryption = (fn, ciphertexts, decryptionKeys) => {
162+
for (const ciphertext of ciphertexts) {
163+
for (const key of decryptionKeys) {
164+
const deciphered = fn(key, ciphertext);
165+
assert.deepStrictEqual(deciphered, plaintext);
166+
}
167+
}
168+
};
169+
170+
testDecryption(privateDecrypt, [
160171
// Encrypt using the public key.
161172
publicEncrypt(publicKey, plaintext),
162173
publicEncrypt({ key: publicKey }, plaintext),
@@ -173,20 +184,25 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
173184
// DER-encoded data only.
174185
publicEncrypt({ format: 'der', type: 'pkcs1', key: publicDER }, plaintext),
175186
publicEncrypt({ format: 'der', type: 'pkcs1', key: privateDER }, plaintext)
176-
];
177-
178-
const decryptionKeys = [
187+
], [
179188
privateKey,
180189
{ format: 'pem', key: privatePem },
181190
{ format: 'der', type: 'pkcs1', key: privateDER }
182-
];
191+
]);
183192

184-
for (const ciphertext of ciphertexts) {
185-
for (const key of decryptionKeys) {
186-
const deciphered = privateDecrypt(key, ciphertext);
187-
assert(plaintext.equals(deciphered));
188-
}
189-
}
193+
testDecryption(publicDecrypt, [
194+
privateEncrypt(privateKey, plaintext)
195+
], [
196+
// Decrypt using the public key.
197+
publicKey,
198+
{ format: 'pem', key: publicPem },
199+
{ format: 'der', type: 'pkcs1', key: publicDER },
200+
201+
// Decrypt using the private key.
202+
privateKey,
203+
{ format: 'pem', key: privatePem },
204+
{ format: 'der', type: 'pkcs1', key: privateDER }
205+
]);
190206
}
191207

192208
{

0 commit comments

Comments
 (0)