Skip to content

Commit d6dbc3c

Browse files
committed
crypto: fix RSA-PSS default saltLength
1 parent c6b0ae8 commit d6dbc3c

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/crypto/crypto_rsa.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ EVPKeyCtxPointer RsaKeyGenTraits::Setup(RsaKeyPairGenConfig* params) {
7070
return EVPKeyCtxPointer();
7171
}
7272

73-
if (params->params.saltlen >= 0 &&
73+
int saltlen = params->params.saltlen;
74+
if (saltlen < 0 && params->params.md != nullptr) {
75+
saltlen = EVP_MD_size(params->params.md);
76+
}
77+
78+
if (saltlen >= 0 &&
7479
EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(
7580
ctx.get(),
76-
params->params.saltlen) <= 0) {
81+
saltlen) <= 0) {
7782
return EVPKeyCtxPointer();
7883
}
7984
}

test/parallel/test-crypto-keygen.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,43 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
369369
}));
370370
}
371371

372+
{
373+
// RFC 8017, A.2.3.: "For a given hashAlgorithm, the default value of
374+
// saltLength is the octet length of the hash value."
375+
376+
generateKeyPair('rsa-pss', {
377+
modulusLength: 512,
378+
hashAlgorithm: 'sha512'
379+
}, common.mustSucceed((publicKey, privateKey) => {
380+
const expectedKeyDetails = {
381+
modulusLength: 512,
382+
publicExponent: 65537n,
383+
hashAlgorithm: 'sha512',
384+
mgf1HashAlgorithm: 'sha512',
385+
saltLength: 64
386+
};
387+
assert.deepStrictEqual(publicKey.asymmetricKeyDetails, expectedKeyDetails);
388+
assert.deepStrictEqual(privateKey.asymmetricKeyDetails, expectedKeyDetails);
389+
}));
390+
391+
// It is still possible to explicitly set saltLength to 0.
392+
generateKeyPair('rsa-pss', {
393+
modulusLength: 512,
394+
hashAlgorithm: 'sha512',
395+
saltLength: 0
396+
}, common.mustSucceed((publicKey, privateKey) => {
397+
const expectedKeyDetails = {
398+
modulusLength: 512,
399+
publicExponent: 65537n,
400+
hashAlgorithm: 'sha512',
401+
mgf1HashAlgorithm: 'sha512',
402+
saltLength: 0
403+
};
404+
assert.deepStrictEqual(publicKey.asymmetricKeyDetails, expectedKeyDetails);
405+
assert.deepStrictEqual(privateKey.asymmetricKeyDetails, expectedKeyDetails);
406+
}));
407+
}
408+
372409
{
373410
const privateKeyEncoding = {
374411
type: 'pkcs8',

0 commit comments

Comments
 (0)