@@ -31,7 +31,11 @@ module.exports.makeScheme = function (key, options) {
31
31
}
32
32
33
33
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 ) ;
35
39
var res = this . key . $doPrivate ( new BigInteger ( encoded ) ) . toBuffer ( this . key . encryptedDataLength ) ;
36
40
return res ;
37
41
} ;
@@ -45,17 +49,21 @@ module.exports.makeScheme = function (key, options) {
45
49
var emLen = Math . ceil ( ( this . key . keySize - 1 ) / 8 ) ;
46
50
var m = this . key . $doPublic ( signature ) . toBuffer ( emLen ) ;
47
51
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 ) ;
49
57
} ;
50
58
51
59
/*
52
60
* https://tools.ietf.org/html/rfc3447#section-9.1.1
53
61
*
54
- * M [Buffer] Message to encode
62
+ * mHash [Buffer] Hashed message to encode
55
63
* 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)
56
64
* @returns {Buffer } The encoded message
57
65
*/
58
- Scheme . prototype . emsa_pss_encode = function ( M , emBits ) {
66
+ Scheme . prototype . emsa_pss_encode = function ( mHash , emBits ) {
59
67
var hash = this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ;
60
68
var mgf = this . options . signingSchemeOptions . mgf || OAEP . eme_oaep_mgf1 ;
61
69
var sLen = this . options . signingSchemeOptions . saltLength || DEFAULT_SALT_LENGTH ;
@@ -70,10 +78,6 @@ module.exports.makeScheme = function (key, options) {
70
78
) ;
71
79
}
72
80
73
- var mHash = crypt . createHash ( hash ) ;
74
- mHash . update ( M ) ;
75
- mHash = mHash . digest ( ) ;
76
-
77
81
var salt = crypt . randomBytes ( sLen ) ;
78
82
79
83
var Mapostrophe = new Buffer ( 8 + hLen + sLen ) ;
@@ -116,12 +120,12 @@ module.exports.makeScheme = function (key, options) {
116
120
/*
117
121
* https://tools.ietf.org/html/rfc3447#section-9.1.2
118
122
*
119
- * M [Buffer] Message
123
+ * mHash [Buffer] Hashed message
120
124
* EM [Buffer] Signature
121
125
* 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)
122
126
* @returns {Boolean } True if signature(EM) matches message(M)
123
127
*/
124
- Scheme . prototype . emsa_pss_verify = function ( M , EM , emBits ) {
128
+ Scheme . prototype . emsa_pss_verify = function ( mHash , EM , emBits ) {
125
129
var hash = this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ;
126
130
var mgf = this . options . signingSchemeOptions . mgf || OAEP . eme_oaep_mgf1 ;
127
131
var sLen = this . options . signingSchemeOptions . saltLength || DEFAULT_SALT_LENGTH ;
@@ -172,10 +176,6 @@ module.exports.makeScheme = function (key, options) {
172
176
173
177
var salt = DB . slice ( DB . length - sLen ) ;
174
178
175
- var mHash = crypt . createHash ( hash ) ;
176
- mHash . update ( M ) ;
177
- mHash = mHash . digest ( ) ;
178
-
179
179
var Mapostrophe = new Buffer ( 8 + hLen + sLen ) ;
180
180
Mapostrophe . fill ( 0 , 0 , 8 ) ;
181
181
mHash . copy ( Mapostrophe , 8 ) ;
@@ -189,4 +189,4 @@ module.exports.makeScheme = function (key, options) {
189
189
} ;
190
190
191
191
return new Scheme ( key , options ) ;
192
- } ;
192
+ } ;
0 commit comments