Skip to content

Commit debdcee

Browse files
committed
Make pss_encode and pss_verify take already hashed buffers
1 parent 9ce020a commit debdcee

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/schemes/pss.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ module.exports.makeScheme = function (key, options) {
3131
}
3232

3333
Scheme.prototype.sign = function (buffer) {
34-
var encoded = this.emsa_pss_encode(buffer, this.key.keySize - 1);
34+
var mHash = crypt.createHash(this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION);
35+
mHash.update(buffer);
36+
mHash = mHash.digest();
37+
38+
var encoded = this.emsa_pss_encode(mHash, this.key.keySize - 1);
3539
var res = this.key.$doPrivate(new BigInteger(encoded)).toBuffer(this.key.encryptedDataLength);
3640
return res;
3741
};
@@ -45,17 +49,21 @@ module.exports.makeScheme = function (key, options) {
4549
var emLen = Math.ceil((this.key.keySize - 1) / 8);
4650
var m = this.key.$doPublic(signature).toBuffer(emLen);
4751

48-
return this.emsa_pss_verify(buffer, m, this.key.keySize - 1);
52+
var mHash = crypt.createHash(this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION);
53+
mHash.update(buffer);
54+
mHash = mHash.digest();
55+
56+
return this.emsa_pss_verify(mHash.digest(), m, this.key.keySize - 1);
4957
};
5058

5159
/*
5260
* https://tools.ietf.org/html/rfc3447#section-9.1.1
5361
*
54-
* M [Buffer] Message to encode
62+
* mHash [Buffer] Hashed message to encode
5563
* emBits [uint] Maximum length of output in bits. Must be at least 8hLen + 8sLen + 9 (hLen = Hash digest length in bytes | sLen = length of salt in bytes)
5664
* @returns {Buffer} The encoded message
5765
*/
58-
Scheme.prototype.emsa_pss_encode = function (M, emBits) {
66+
Scheme.prototype.emsa_pss_encode = function (mHash, emBits) {
5967
var hash = this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
6068
var mgf = this.options.signingSchemeOptions.mgf || OAEP.eme_oaep_mgf1;
6169
var sLen = this.options.signingSchemeOptions.saltLength || DEFAULT_SALT_LENGTH;
@@ -70,10 +78,6 @@ module.exports.makeScheme = function (key, options) {
7078
);
7179
}
7280

73-
var mHash = crypt.createHash(hash);
74-
mHash.update(M);
75-
mHash = mHash.digest();
76-
7781
var salt = crypt.randomBytes(sLen);
7882

7983
var Mapostrophe = new Buffer(8 + hLen + sLen);
@@ -116,12 +120,12 @@ module.exports.makeScheme = function (key, options) {
116120
/*
117121
* https://tools.ietf.org/html/rfc3447#section-9.1.2
118122
*
119-
* M [Buffer] Message
123+
* mHash [Buffer] Hashed message
120124
* EM [Buffer] Signature
121125
* emBits [uint] Length of EM in bits. Must be at least 8hLen + 8sLen + 9 to be a valid signature. (hLen = Hash digest length in bytes | sLen = length of salt in bytes)
122126
* @returns {Boolean} True if signature(EM) matches message(M)
123127
*/
124-
Scheme.prototype.emsa_pss_verify = function (M, EM, emBits) {
128+
Scheme.prototype.emsa_pss_verify = function (mHash, EM, emBits) {
125129
var hash = this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
126130
var mgf = this.options.signingSchemeOptions.mgf || OAEP.eme_oaep_mgf1;
127131
var sLen = this.options.signingSchemeOptions.saltLength || DEFAULT_SALT_LENGTH;
@@ -172,10 +176,6 @@ module.exports.makeScheme = function (key, options) {
172176

173177
var salt = DB.slice(DB.length - sLen);
174178

175-
var mHash = crypt.createHash(hash);
176-
mHash.update(M);
177-
mHash = mHash.digest();
178-
179179
var Mapostrophe = new Buffer(8 + hLen + sLen);
180180
Mapostrophe.fill(0, 0, 8);
181181
mHash.copy(Mapostrophe, 8);
@@ -189,4 +189,4 @@ module.exports.makeScheme = function (key, options) {
189189
};
190190

191191
return new Scheme(key, options);
192-
};
192+
};

0 commit comments

Comments
 (0)