Skip to content

Commit 194f135

Browse files
committed
Update with latest go files.
1 parent b208fe3 commit 194f135

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

blindsign/blindrsa/pss.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"crypto"
4040
"crypto/rsa"
4141
"errors"
42-
"fmt"
4342
"hash"
4443
"io"
4544
"math/big"
@@ -151,20 +150,17 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
151150
//
152151
// 2. Let mHash = Hash(M), an octet string of length hLen.
153152
if hLen != len(mHash) {
154-
fmt.Println("here3", hLen, len(mHash))
155153
return ErrVerification
156154
}
157155

158156
// 3. If emLen < hLen + sLen + 2, output "inconsistent" and stop.
159157
if emLen < hLen+sLen+2 {
160-
fmt.Println("here2")
161158
return ErrVerification
162159
}
163160

164161
// 4. If the rightmost octet of EM does not have hexadecimal value
165162
// 0xbc, output "inconsistent" and stop.
166163
if em[emLen-1] != 0xbc {
167-
fmt.Println("here")
168164
return ErrVerification
169165
}
170166

@@ -178,7 +174,6 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
178174
// stop.
179175
var bitMask byte = 0xff >> (8*emLen - emBits)
180176
if em[0] & ^bitMask != 0 {
181-
fmt.Println("here4")
182177
return ErrVerification
183178
}
184179

@@ -195,7 +190,6 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
195190
if sLen == PSSSaltLengthAuto {
196191
psLen := bytes.IndexByte(db, 0x01)
197192
if psLen < 0 {
198-
fmt.Println("here5")
199193
return ErrVerification
200194
}
201195
sLen = len(db) - psLen - 1
@@ -208,12 +202,10 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
208202
psLen := emLen - hLen - sLen - 2
209203
for _, e := range db[:psLen] {
210204
if e != 0x00 {
211-
fmt.Println("here6")
212205
return ErrVerification
213206
}
214207
}
215208
if db[psLen] != 0x01 {
216-
fmt.Println("here7")
217209
return ErrVerification
218210
}
219211

@@ -235,7 +227,6 @@ func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
235227

236228
// 14. If H = H', output "consistent." Otherwise, output "inconsistent."
237229
if !bytes.Equal(h0, h) { // TODO: constant time?
238-
fmt.Println("here8")
239230
return ErrVerification
240231
}
241232
return nil
@@ -257,8 +248,7 @@ func signPSSWithSalt(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, has
257248
return nil, err
258249
}
259250
s := make([]byte, priv.Size())
260-
copyWithLeftPad(s, c.Bytes())
261-
return s, nil
251+
return c.FillBytes(s), nil
262252
}
263253

264254
const (
@@ -308,7 +298,7 @@ func SignPSS(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, digest []by
308298
saltLength := opts.saltLength()
309299
switch saltLength {
310300
case PSSSaltLengthAuto:
311-
saltLength = priv.Size() - 2 - hash.Size()
301+
saltLength = (priv.N.BitLen()-1+7)/8 - 2 - hash.Size()
312302
case PSSSaltLengthEqualsHash:
313303
saltLength = hash.Size()
314304
}
@@ -326,33 +316,17 @@ func SignPSS(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Hash, digest []by
326316
// result of hashing the input message using the given hash function. The opts
327317
// argument may be nil, in which case sensible defaults are used. opts.Hash is
328318
// 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 {
330320
if len(sig) != pub.Size() {
331-
fmt.Println("1")
332321
return ErrVerification
333322
}
334323
s := new(big.Int).SetBytes(sig)
335324
m := encrypt(new(big.Int), pub, s)
336325
emBits := pub.N.BitLen() - 1
337326
emLen := (emBits + 7) / 8
338-
emBytes := m.Bytes()
339327
if m.BitLen() > emLen*8 {
340-
fmt.Println("2")
341328
return ErrVerification
342329
}
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())
358332
}

blindsign/blindrsa/rsa.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,17 @@ func encrypt(c *big.Int, pub *rsa.PublicKey, m *big.Int) *big.Int {
8787
return c
8888
}
8989

90+
// decrypt performs an RSA decryption, resulting in a plaintext integer. If a
91+
// random source is given, RSA blinding is used.
9092
func decrypt(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, err error) {
9193
// TODO(agl): can we get away with reusing blinds?
9294
if c.Cmp(priv.N) > 0 {
9395
err = rsa.ErrDecryption
9496
return
9597
}
98+
if priv.N.Sign() == 0 {
99+
return nil, rsa.ErrDecryption
100+
}
96101

97102
var ir *big.Int
98103
if random != nil {
@@ -102,7 +107,7 @@ func decrypt(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, er
102107
// by multiplying by the multiplicative inverse of r.
103108

104109
var r *big.Int
105-
110+
ir = new(big.Int)
106111
for {
107112
r, err = rand.Int(random, priv.N)
108113
if err != nil {
@@ -111,13 +116,13 @@ func decrypt(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, er
111116
if r.Cmp(bigZero) == 0 {
112117
r = bigOne
113118
}
114-
ir = new(big.Int).ModInverse(r, priv.N)
115-
if ir != nil {
119+
ok := ir.ModInverse(r, priv.N)
120+
if ok != nil {
116121
break
117122
}
118123
}
119124
bigE := big.NewInt(int64(priv.E))
120-
rpowe := new(big.Int).Exp(r, bigE, priv.N)
125+
rpowe := new(big.Int).Exp(r, bigE, priv.N) // N != 0
121126
cCopy := new(big.Int).Set(c)
122127
cCopy.Mul(cCopy, rpowe)
123128
cCopy.Mod(cCopy, priv.N)
@@ -159,7 +164,7 @@ func decrypt(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, er
159164
m.Mod(m, priv.N)
160165
}
161166

162-
return m, nil
167+
return m, err
163168
}
164169

165170
func decryptAndCheck(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, err error) {

0 commit comments

Comments
 (0)