Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const {
isArrayBufferView,
} = require('internal/util/types');

const constants = internalBinding('constants').crypto;

function Sign(algorithm, options) {
if (!(this instanceof Sign))
return new Sign(algorithm, options);
Expand Down Expand Up @@ -85,7 +87,11 @@ function getPadding(options) {
}

function getSaltLength(options) {
return getIntOption('saltLength', options);
let saltLength = getIntOption('saltLength', options);
if (options.padding === constants.RSA_PKCS1_PSS_PADDING && saltLength === undefined) {
saltLength = constants.RSA_PSS_SALTLEN_MAX_SIGN;
}
return saltLength;
}

function getDSASignatureEncoding(options) {
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-crypto-rsa-pss-default-salt-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto');

const fixtures = require('../common/fixtures');

const privateKey = crypto.createPrivateKey(fixtures.readKey('rsa_private.pem', 'ascii'));
const publicKey = crypto.createPublicKey(fixtures.readKey('rsa_public.pem', 'ascii'));

const data = crypto.randomBytes(32);

for (const digest of ['sha256', 'sha384', 'sha512']) {
const hLen = crypto.hash(digest, data, 'buffer').byteLength;
const maxSaltLength =
privateKey.asymmetricKeyDetails.modulusLength / 8 - hLen - 2;

const sig = crypto.sign(digest, data, {
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
// No "saltLength" provided, documented default RSA_PSS_SALTLEN_MAX_SIGN expected
});

assert.strictEqual(crypto.verify(
digest,
data,
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: maxSaltLength,
},
sig
), true);
}
Loading