Skip to content

Commit a42bd7e

Browse files
tniessenpanva
authored andcommitted
crypto: fix RSA-PSS default saltLength
PR-URL: #39999 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
1 parent 5fd7a72 commit a42bd7e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/crypto/crypto_rsa.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ EVPKeyCtxPointer RsaKeyGenTraits::Setup(RsaKeyPairGenConfig* params) {
7979
return EVPKeyCtxPointer();
8080
}
8181

82-
if (params->params.saltlen >= 0 &&
82+
int saltlen = params->params.saltlen;
83+
if (saltlen < 0 && params->params.md != nullptr) {
84+
saltlen = EVP_MD_size(params->params.md);
85+
}
86+
87+
if (saltlen >= 0 &&
8388
EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(
8489
ctx.get(),
85-
params->params.saltlen) <= 0) {
90+
saltlen) <= 0) {
8691
return EVPKeyCtxPointer();
8792
}
8893
}

test/parallel/test-crypto-keygen.js

+37
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,43 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
391391
}));
392392
}
393393

394+
{
395+
// RFC 8017, A.2.3.: "For a given hashAlgorithm, the default value of
396+
// saltLength is the octet length of the hash value."
397+
398+
generateKeyPair('rsa-pss', {
399+
modulusLength: 512,
400+
hashAlgorithm: 'sha512'
401+
}, common.mustSucceed((publicKey, privateKey) => {
402+
const expectedKeyDetails = {
403+
modulusLength: 512,
404+
publicExponent: 65537n,
405+
hashAlgorithm: 'sha512',
406+
mgf1HashAlgorithm: 'sha512',
407+
saltLength: 64
408+
};
409+
assert.deepStrictEqual(publicKey.asymmetricKeyDetails, expectedKeyDetails);
410+
assert.deepStrictEqual(privateKey.asymmetricKeyDetails, expectedKeyDetails);
411+
}));
412+
413+
// It is still possible to explicitly set saltLength to 0.
414+
generateKeyPair('rsa-pss', {
415+
modulusLength: 512,
416+
hashAlgorithm: 'sha512',
417+
saltLength: 0
418+
}, common.mustSucceed((publicKey, privateKey) => {
419+
const expectedKeyDetails = {
420+
modulusLength: 512,
421+
publicExponent: 65537n,
422+
hashAlgorithm: 'sha512',
423+
mgf1HashAlgorithm: 'sha512',
424+
saltLength: 0
425+
};
426+
assert.deepStrictEqual(publicKey.asymmetricKeyDetails, expectedKeyDetails);
427+
assert.deepStrictEqual(privateKey.asymmetricKeyDetails, expectedKeyDetails);
428+
}));
429+
}
430+
394431
{
395432
const privateKeyEncoding = {
396433
type: 'pkcs8',

0 commit comments

Comments
 (0)