@@ -39,7 +39,6 @@ import (
39
39
"crypto"
40
40
"crypto/rsa"
41
41
"errors"
42
- "fmt"
43
42
"hash"
44
43
"io"
45
44
"math/big"
@@ -151,20 +150,17 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
151
150
//
152
151
// 2. Let mHash = Hash(M), an octet string of length hLen.
153
152
if hLen != len (mHash ) {
154
- fmt .Println ("here3" , hLen , len (mHash ))
155
153
return ErrVerification
156
154
}
157
155
158
156
// 3. If emLen < hLen + sLen + 2, output "inconsistent" and stop.
159
157
if emLen < hLen + sLen + 2 {
160
- fmt .Println ("here2" )
161
158
return ErrVerification
162
159
}
163
160
164
161
// 4. If the rightmost octet of EM does not have hexadecimal value
165
162
// 0xbc, output "inconsistent" and stop.
166
163
if em [emLen - 1 ] != 0xbc {
167
- fmt .Println ("here" )
168
164
return ErrVerification
169
165
}
170
166
@@ -178,7 +174,6 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
178
174
// stop.
179
175
var bitMask byte = 0xff >> (8 * emLen - emBits )
180
176
if em [0 ] & ^ bitMask != 0 {
181
- fmt .Println ("here4" )
182
177
return ErrVerification
183
178
}
184
179
@@ -195,7 +190,6 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
195
190
if sLen == PSSSaltLengthAuto {
196
191
psLen := bytes .IndexByte (db , 0x01 )
197
192
if psLen < 0 {
198
- fmt .Println ("here5" )
199
193
return ErrVerification
200
194
}
201
195
sLen = len (db ) - psLen - 1
@@ -208,12 +202,10 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
208
202
psLen := emLen - hLen - sLen - 2
209
203
for _ , e := range db [:psLen ] {
210
204
if e != 0x00 {
211
- fmt .Println ("here6" )
212
205
return ErrVerification
213
206
}
214
207
}
215
208
if db [psLen ] != 0x01 {
216
- fmt .Println ("here7" )
217
209
return ErrVerification
218
210
}
219
211
@@ -235,7 +227,6 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
235
227
236
228
// 14. If H = H', output "consistent." Otherwise, output "inconsistent."
237
229
if ! bytes .Equal (h0 , h ) { // TODO: constant time?
238
- fmt .Println ("here8" )
239
230
return ErrVerification
240
231
}
241
232
return nil
@@ -257,8 +248,7 @@ func signPSSWithSalt(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, has
257
248
return nil , err
258
249
}
259
250
s := make ([]byte , priv .Size ())
260
- copyWithLeftPad (s , c .Bytes ())
261
- return s , nil
251
+ return c .FillBytes (s ), nil
262
252
}
263
253
264
254
const (
@@ -308,7 +298,7 @@ func SignPSS(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, digest []by
308
298
saltLength := opts .saltLength ()
309
299
switch saltLength {
310
300
case PSSSaltLengthAuto :
311
- saltLength = priv .Size () - 2 - hash .Size ()
301
+ saltLength = ( priv .N . BitLen () - 1 + 7 ) / 8 - 2 - hash .Size ()
312
302
case PSSSaltLengthEqualsHash :
313
303
saltLength = hash .Size ()
314
304
}
@@ -326,33 +316,17 @@ func SignPSS(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, digest []by
326
316
// result of hashing the input message using the given hash function. The opts
327
317
// argument may be nil, in which case sensible defaults are used. opts.Hash is
328
318
// ignored.
329
- func VerifyPSS (pub * rsa.PublicKey , hash hash .Hash , digest []byte , sig []byte , opts * PSSOptions ) error {
319
+ func VerifyPSS (pub * rsa.PublicKey , hash crypto .Hash , digest []byte , sig []byte , opts * PSSOptions ) error {
330
320
if len (sig ) != pub .Size () {
331
- fmt .Println ("1" )
332
321
return ErrVerification
333
322
}
334
323
s := new (big.Int ).SetBytes (sig )
335
324
m := encrypt (new (big.Int ), pub , s )
336
325
emBits := pub .N .BitLen () - 1
337
326
emLen := (emBits + 7 ) / 8
338
- emBytes := m .Bytes ()
339
327
if m .BitLen () > emLen * 8 {
340
- fmt .Println ("2" )
341
328
return ErrVerification
342
329
}
343
-
344
- em := make ([]byte , emLen )
345
- copyWithLeftPad (em , emBytes )
346
-
347
- return emsaPSSVerify (digest , em , emBits , opts .saltLength (), hash )
348
- }
349
-
350
- // copyWithLeftPad copies src to the end of dest, padding with zero bytes as
351
- // needed.
352
- func copyWithLeftPad (dest , src []byte ) {
353
- numPaddingBytes := len (dest ) - len (src )
354
- for i := 0 ; i < numPaddingBytes ; i ++ {
355
- dest [i ] = 0
356
- }
357
- copy (dest [numPaddingBytes :], src )
330
+ em := m .FillBytes (make ([]byte , emLen ))
331
+ return emsaPSSVerify (digest , em , emBits , opts .saltLength (), hash .New ())
358
332
}
0 commit comments