Skip to content

Commit be0b53d

Browse files
tniessentargos
authored andcommitted
crypto: fix key requirements in asymmetric cipher
PR-URL: #30249Fixes: #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

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎lib/internal/crypto/cipher.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ function rsaFunctionFor(method, defaultPadding, keyType) {
6666
constpublicEncrypt=rsaFunctionFor(_publicEncrypt,RSA_PKCS1_OAEP_PADDING,
6767
'public');
6868
constpublicDecrypt=rsaFunctionFor(_publicDecrypt,RSA_PKCS1_PADDING,
69-
'private');
69+
'public');
7070
constprivateEncrypt=rsaFunctionFor(_privateEncrypt,RSA_PKCS1_PADDING,
7171
'private');
7272
constprivateDecrypt=rsaFunctionFor(_privateDecrypt,RSA_PKCS1_OAEP_PADDING,
73-
'public');
73+
'private');
7474

7575
functiongetDecoder(decoder,encoding){
7676
encoding=normalizeEncoding(encoding);

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

Lines changed: 28 additions & 12 deletions
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
constfixtures=require('../common/fixtures');
@@ -156,7 +158,16 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
156158
assert(Buffer.isBuffer(privateDER));
157159

158160
constplaintext=Buffer.from('Hello world','utf8');
159-
constciphertexts=[
161+
consttestDecryption=(fn,ciphertexts,decryptionKeys)=>{
162+
for(constciphertextofciphertexts){
163+
for(constkeyofdecryptionKeys){
164+
constdeciphered=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-
constdecryptionKeys=[
187+
],[
179188
privateKey,
180189
{format: 'pem',key: privatePem},
181190
{format: 'der',type: 'pkcs1',key: privateDER}
182-
];
191+
]);
183192

184-
for(constciphertextofciphertexts){
185-
for(constkeyofdecryptionKeys){
186-
constdeciphered=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)