@@ -2,27 +2,32 @@ package blindrsa
2
2
3
3
// This package implements the blind RSA protocol based on the CFRG specification:
4
4
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-blind-signatures-02
5
+ //
6
+ // Blind RSA is an example of a blind signature protocol is a two-party protocol
7
+ // for computing a digital signature. One party (the server) holds the signing
8
+ // key, and the other (the client) holds the message input. Blindness
9
+ // ensures that the server does not learn anything about the client's
10
+ // input during the BlindSign step.
5
11
6
12
import (
7
13
"crypto"
8
14
"crypto/rand"
9
15
"crypto/rsa"
10
16
"crypto/sha256"
11
17
"crypto/sha512"
12
- "crypto/subtle"
13
18
"errors"
19
+ "fmt"
14
20
"hash"
15
21
"io"
16
22
"math/big"
17
-
18
- "github.com/cloudflare/circl/blindsign"
19
23
)
20
24
21
25
var errUnsupportedHashFunction = errors .New ("unsupported hash function" )
22
26
23
- // An RSAVerifier represents a Verifier in the RSA blind signature protocol.
24
- // It carries state needed to produce and validate an RSA blind signature.
25
- type RSAVerifier struct {
27
+ // An RandomBRSAVerifier represents a Verifier in the RSA blind signature protocol.
28
+ // It carries state needed to produce and validate an RSA signature produced
29
+ // using the blind RSA protocol.
30
+ type RandomBRSAVerifier struct {
26
31
// Public key of the Signer
27
32
pk * rsa.PublicKey
28
33
@@ -33,8 +38,8 @@ type RSAVerifier struct {
33
38
hash hash.Hash
34
39
}
35
40
36
- // A DeterminsiticRSAVerifier is an RSAVerifier that supports deterministic signatures.
37
- type DeterminsiticRSAVerifier struct {
41
+ // A DeterminsiticBRSAVerifier is a BRSAVerifier that supports deterministic signatures.
42
+ type DeterminsiticBRSAVerifier struct {
38
43
// Public key of the Signer
39
44
pk * rsa.PublicKey
40
45
@@ -45,6 +50,14 @@ type DeterminsiticRSAVerifier struct {
45
50
hash hash.Hash
46
51
}
47
52
53
+ type BRSAVerifier interface {
54
+ Blind (random io.Reader , message []byte ) ([]byte , BRSAVerifierState , error )
55
+ FixedBlind (message , blind , salt []byte ) ([]byte , BRSAVerifierState , error )
56
+ Verify (message , signature []byte ) error
57
+ Hash () hash.Hash
58
+ PublicKey () * rsa.PublicKey
59
+ }
60
+
48
61
func convertHashFunction (hash crypto.Hash ) hash.Hash {
49
62
switch hash {
50
63
case crypto .SHA256 :
@@ -58,61 +71,78 @@ func convertHashFunction(hash crypto.Hash) hash.Hash {
58
71
}
59
72
}
60
73
61
- // NewDeterministicRSAVerifier creates a new RSAVerifier using the corresponding Signer parameters.
62
- func NewDeterministicRSAVerifier (pk * rsa.PublicKey , hash crypto.Hash ) DeterminsiticRSAVerifier {
74
+ // NewDeterministicBRSAVerifier creates a new DeterminsiticBRSAVerifier using the corresponding Signer parameters.
75
+ func NewDeterministicBRSAVerifier (pk * rsa.PublicKey , hash crypto.Hash ) BRSAVerifier {
63
76
h := convertHashFunction (hash )
64
- return DeterminsiticRSAVerifier {
77
+ return DeterminsiticBRSAVerifier {
65
78
pk : pk ,
66
79
cryptoHash : hash ,
67
80
hash : h ,
68
81
}
69
82
}
70
83
71
- // NewRSAVerifier creates a new RSAVerifier using the corresponding Signer parameters.
72
- func NewRSAVerifier (pk * rsa.PublicKey , hash crypto.Hash ) RSAVerifier {
84
+ func (v DeterminsiticBRSAVerifier ) Hash () hash.Hash {
85
+ return v .hash
86
+ }
87
+
88
+ func (v DeterminsiticBRSAVerifier ) PublicKey () * rsa.PublicKey {
89
+ return v .pk
90
+ }
91
+
92
+ // NewBRSAVerifier creates a new BRSAVerifier using the corresponding Signer parameters.
93
+ func NewBRSAVerifier (pk * rsa.PublicKey , hash crypto.Hash ) BRSAVerifier {
73
94
h := convertHashFunction (hash )
74
- return RSAVerifier {
95
+ return RandomBRSAVerifier {
75
96
pk : pk ,
76
97
cryptoHash : hash ,
77
98
hash : h ,
78
99
}
79
100
}
80
101
81
- func encodeMessageEMSAPSS (message []byte , key * rsa.PublicKey , hash hash.Hash , salt []byte ) ([]byte , error ) {
102
+ func (v RandomBRSAVerifier ) Hash () hash.Hash {
103
+ return v .hash
104
+ }
105
+
106
+ func (v RandomBRSAVerifier ) PublicKey () * rsa.PublicKey {
107
+ return v .pk
108
+ }
109
+
110
+ func encodeMessageEMSAPSS (message []byte , N * big.Int , hash hash.Hash , salt []byte ) ([]byte , error ) {
82
111
hash .Reset () // Ensure the hash state is cleared
83
112
hash .Write (message )
84
113
digest := hash .Sum (nil )
85
114
hash .Reset ()
86
- emBits := key . N .BitLen () - 1
115
+ emBits := N .BitLen () - 1
87
116
encodedMsg , err := emsaPSSEncode (digest [:], emBits , salt , hash )
88
117
return encodedMsg , err
89
118
}
90
119
91
- func generateBlindingFactor (random io.Reader , key * rsa. PublicKey ) (* big.Int , * big.Int , error ) {
120
+ func generateBlindingFactor (random io.Reader , N * big. Int ) (* big.Int , * big.Int , error ) {
92
121
randReader := random
93
122
if randReader == nil {
94
123
randReader = rand .Reader
95
124
}
96
- r , err := rand .Int (randReader , key . N )
125
+ r , err := rand .Int (randReader , N )
97
126
if err != nil {
98
127
return nil , nil , err
99
128
}
100
129
101
130
if r .Sign () == 0 {
102
131
r = bigOne
103
132
}
104
- rInv := new (big.Int ).ModInverse (r , key . N )
133
+ rInv := new (big.Int ).ModInverse (r , N )
105
134
if rInv == nil {
135
+ fmt .Println (r , N )
106
136
return nil , nil , ErrInvalidBlind
107
137
}
108
138
109
139
return r , rInv , nil
110
140
}
111
141
112
- func fixedBlind (message , salt []byte , r , rInv * big.Int , pk * rsa.PublicKey , hash hash.Hash ) ([]byte , blindsign. VerifierState , error ) {
113
- encodedMsg , err := encodeMessageEMSAPSS (message , pk , hash , salt )
142
+ func fixedBlind (message , salt []byte , r , rInv * big.Int , pk * rsa.PublicKey , hash hash.Hash ) ([]byte , BRSAVerifierState , error ) {
143
+ encodedMsg , err := encodeMessageEMSAPSS (message , pk . N , hash , salt )
114
144
if err != nil {
115
- return nil , nil , err
145
+ return nil , BRSAVerifierState {} , err
116
146
}
117
147
118
148
m := new (big.Int ).SetBytes (encodedMsg )
@@ -127,7 +157,7 @@ func fixedBlind(message, salt []byte, r, rInv *big.Int, pk *rsa.PublicKey, hash
127
157
blindedMsg := make ([]byte , kLen )
128
158
z .FillBytes (blindedMsg )
129
159
130
- return blindedMsg , RSAVerifierState {
160
+ return blindedMsg , BRSAVerifierState {
131
161
encodedMsg : encodedMsg ,
132
162
pk : pk ,
133
163
hash : hash ,
@@ -141,34 +171,44 @@ func fixedBlind(message, salt []byte, r, rInv *big.Int, pk *rsa.PublicKey, hash
141
171
//
142
172
// See the specification for more details:
143
173
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-blind-signatures-02#section-5.1.1
144
- func (v DeterminsiticRSAVerifier ) Blind (random io.Reader , message []byte ) ([]byte , blindsign. VerifierState , error ) {
174
+ func (v DeterminsiticBRSAVerifier ) Blind (random io.Reader , message []byte ) ([]byte , BRSAVerifierState , error ) {
145
175
if random == nil {
146
- return nil , nil , ErrInvalidRandomness
176
+ return nil , BRSAVerifierState {} , ErrInvalidRandomness
147
177
}
148
178
149
- r , rInv , err := generateBlindingFactor (random , v .pk )
179
+ r , rInv , err := generateBlindingFactor (random , v .pk . N )
150
180
if err != nil {
151
- return nil , nil , err
181
+ return nil , BRSAVerifierState {} , err
152
182
}
153
183
154
184
return fixedBlind (message , nil , r , rInv , v .pk , v .hash )
155
185
}
156
186
157
- func verifyMessageSignature (message , signature []byte , saltLength int , pk * rsa.PublicKey , hash crypto.Hash ) error {
158
- h := convertHashFunction (hash )
159
- h .Write (message )
160
- digest := h .Sum (nil )
161
-
162
- err := rsa .VerifyPSS (pk , hash , digest , signature , & rsa.PSSOptions {
163
- Hash : hash ,
164
- SaltLength : saltLength ,
165
- })
166
- return err
187
+ func saltLength (opts * rsa.PSSOptions ) int {
188
+ if opts == nil {
189
+ return rsa .PSSSaltLengthAuto
190
+ }
191
+ return opts .SaltLength
192
+ }
193
+
194
+ // FixedBlind runs the Blind function with fixed blind and salt inputs.
195
+ func (v DeterminsiticBRSAVerifier ) FixedBlind (message , blind , salt []byte ) ([]byte , BRSAVerifierState , error ) {
196
+ if blind == nil {
197
+ return nil , BRSAVerifierState {}, ErrInvalidRandomness
198
+ }
199
+
200
+ r := new (big.Int ).SetBytes (blind )
201
+ rInv := new (big.Int ).ModInverse (r , v .pk .N )
202
+ if rInv == nil {
203
+ return nil , BRSAVerifierState {}, ErrInvalidBlind
204
+ }
205
+
206
+ return fixedBlind (message , salt , r , rInv , v .pk , v .hash )
167
207
}
168
208
169
209
// Verify verifies the input (message, signature) pair and produces an error upon failure.
170
- func (v DeterminsiticRSAVerifier ) Verify (message , signature []byte ) error {
171
- return verifyMessageSignature (message , signature , 0 , v .pk , v .cryptoHash )
210
+ func (v DeterminsiticBRSAVerifier ) Verify (message , signature []byte ) error {
211
+ return verifyMessageSignature (message , signature , 0 , convertToCustomPublicKey ( v .pk ) , v .cryptoHash )
172
212
}
173
213
174
214
// Blind initializes the blind RSA protocol using an input message and source of randomness. The
@@ -177,48 +217,48 @@ func (v DeterminsiticRSAVerifier) Verify(message, signature []byte) error {
177
217
//
178
218
// See the specification for more details:
179
219
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-blind-signatures-02#section-5.1.1
180
- func (v RSAVerifier ) Blind (random io.Reader , message []byte ) ([]byte , blindsign. VerifierState , error ) {
220
+ func (v RandomBRSAVerifier ) Blind (random io.Reader , message []byte ) ([]byte , BRSAVerifierState , error ) {
181
221
if random == nil {
182
- return nil , nil , ErrInvalidRandomness
222
+ return nil , BRSAVerifierState {} , ErrInvalidRandomness
183
223
}
184
224
185
225
salt := make ([]byte , v .hash .Size ())
186
226
_ , err := io .ReadFull (random , salt )
187
227
if err != nil {
188
- return nil , nil , err
228
+ return nil , BRSAVerifierState {} , err
189
229
}
190
230
191
- r , rInv , err := generateBlindingFactor (random , v .pk )
231
+ r , rInv , err := generateBlindingFactor (random , v .pk . N )
192
232
if err != nil {
193
- return nil , nil , err
233
+ return nil , BRSAVerifierState {} , err
194
234
}
195
235
196
236
return fixedBlind (message , salt , r , rInv , v .pk , v .hash )
197
237
}
198
238
199
239
// FixedBlind runs the Blind function with fixed blind and salt inputs.
200
- func (v RSAVerifier ) FixedBlind (message , blind , salt []byte ) ([]byte , blindsign. VerifierState , error ) {
240
+ func (v RandomBRSAVerifier ) FixedBlind (message , blind , salt []byte ) ([]byte , BRSAVerifierState , error ) {
201
241
if blind == nil {
202
- return nil , nil , ErrInvalidRandomness
242
+ return nil , BRSAVerifierState {} , ErrInvalidRandomness
203
243
}
204
244
205
245
r := new (big.Int ).SetBytes (blind )
206
246
rInv := new (big.Int ).ModInverse (r , v .pk .N )
207
247
if rInv == nil {
208
- return nil , nil , ErrInvalidBlind
248
+ return nil , BRSAVerifierState {} , ErrInvalidBlind
209
249
}
210
250
211
251
return fixedBlind (message , salt , r , rInv , v .pk , v .hash )
212
252
}
213
253
214
254
// Verify verifies the input (message, signature) pair and produces an error upon failure.
215
- func (v RSAVerifier ) Verify (message , signature []byte ) error {
216
- return verifyMessageSignature (message , signature , v .hash .Size (), v .pk , v .cryptoHash )
255
+ func (v RandomBRSAVerifier ) Verify (message , signature []byte ) error {
256
+ return verifyMessageSignature (message , signature , v .hash .Size (), convertToCustomPublicKey ( v .pk ) , v .cryptoHash )
217
257
}
218
258
219
- // An RSAVerifierState carries state needed to complete the blind signature protocol
259
+ // An BRSAVerifierState carries state needed to complete the blind signature protocol
220
260
// as a verifier.
221
- type RSAVerifierState struct {
261
+ type BRSAVerifierState struct {
222
262
// Public key of the Signer
223
263
pk * rsa.PublicKey
224
264
@@ -235,23 +275,11 @@ type RSAVerifierState struct {
235
275
rInv * big.Int
236
276
}
237
277
238
- func verifyBlindSignature (pub * rsa.PublicKey , hashed , sig []byte ) error {
239
- m := new (big.Int ).SetBytes (hashed )
240
- bigSig := new (big.Int ).SetBytes (sig )
241
-
242
- c := encrypt (new (big.Int ), pub , bigSig )
243
- if subtle .ConstantTimeCompare (m .Bytes (), c .Bytes ()) == 1 {
244
- return nil
245
- } else {
246
- return rsa .ErrVerification
247
- }
248
- }
249
-
250
278
// Finalize computes and outputs the final signature, if it's valid. Otherwise, it returns an error.
251
279
//
252
280
// See the specification for more details:
253
281
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-blind-signatures-02#section-5.1.3
254
- func (state RSAVerifierState ) Finalize (data []byte ) ([]byte , error ) {
282
+ func (state BRSAVerifierState ) Finalize (data []byte ) ([]byte , error ) {
255
283
kLen := (state .pk .N .BitLen () + 7 ) / 8
256
284
if len (data ) != kLen {
257
285
return nil , ErrUnexpectedSize
@@ -265,7 +293,7 @@ func (state RSAVerifierState) Finalize(data []byte) ([]byte, error) {
265
293
sig := make ([]byte , kLen )
266
294
s .FillBytes (sig )
267
295
268
- err := verifyBlindSignature (state .pk , state .encodedMsg , sig )
296
+ err := verifyBlindSignature (convertToCustomPublicKey ( state .pk ) , state .encodedMsg , sig )
269
297
if err != nil {
270
298
return nil , err
271
299
}
@@ -274,28 +302,28 @@ func (state RSAVerifierState) Finalize(data []byte) ([]byte, error) {
274
302
}
275
303
276
304
// CopyBlind returns an encoding of the blind value used in the protocol.
277
- func (state RSAVerifierState ) CopyBlind () []byte {
305
+ func (state BRSAVerifierState ) CopyBlind () []byte {
278
306
r := new (big.Int ).ModInverse (state .rInv , state .pk .N )
279
307
return r .Bytes ()
280
308
}
281
309
282
310
// CopySalt returns an encoding of the per-message salt used in the protocol.
283
- func (state RSAVerifierState ) CopySalt () []byte {
311
+ func (state BRSAVerifierState ) CopySalt () []byte {
284
312
salt := make ([]byte , len (state .salt ))
285
313
copy (salt , state .salt )
286
314
return salt
287
315
}
288
316
289
- // An RSASigner represents the Signer in the blind RSA protocol.
317
+ // An BRSASigner represents the Signer in the blind RSA protocol.
290
318
// It carries the raw RSA private key used for signing blinded messages.
291
- type RSASigner struct {
319
+ type BRSASigner struct {
292
320
// An RSA private key
293
321
sk * rsa.PrivateKey
294
322
}
295
323
296
- // NewRSASigner creates a new Signer for the blind RSA protocol using an RSA private key.
297
- func NewRSASigner (sk * rsa.PrivateKey ) RSASigner {
298
- return RSASigner {
324
+ // NewBRSASigner creates a new Signer for the blind RSA protocol using an RSA private key.
325
+ func NewBRSASigner (sk * rsa.PrivateKey ) BRSASigner {
326
+ return BRSASigner {
299
327
sk : sk ,
300
328
}
301
329
}
@@ -305,7 +333,7 @@ func NewRSASigner(sk *rsa.PrivateKey) RSASigner {
305
333
//
306
334
// See the specification for more details:
307
335
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-blind-signatures-02#section-5.1.2
308
- func (signer RSASigner ) BlindSign (data []byte ) ([]byte , error ) {
336
+ func (signer BRSASigner ) BlindSign (data []byte ) ([]byte , error ) {
309
337
kLen := (signer .sk .N .BitLen () + 7 ) / 8
310
338
if len (data ) != kLen {
311
339
return nil , ErrUnexpectedSize
@@ -316,7 +344,7 @@ func (signer RSASigner) BlindSign(data []byte) ([]byte, error) {
316
344
return nil , ErrInvalidMessageLength
317
345
}
318
346
319
- s , err := decryptAndCheck (rand .Reader , signer .sk , m )
347
+ s , err := decryptAndCheck (rand .Reader , convertToCustomPrivateKey ( signer .sk ) , m )
320
348
if err != nil {
321
349
return nil , err
322
350
}
0 commit comments