Skip to content

Commit cd74ed4

Browse files
committed
Add partially blind RSA implementation
See the specification for more information: https://datatracker.ietf.org/doc/html/draft-amjad-cfrg-partially-blind-rsa-00
1 parent 769d73f commit cd74ed4

File tree

9 files changed

+1115
-237
lines changed

9 files changed

+1115
-237
lines changed

blindsign/blindrsa/blindrsa.go renamed to blindsign/blindrsa/brsa.go

+102-74
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@ package blindrsa
22

33
// This package implements the blind RSA protocol based on the CFRG specification:
44
// 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.
511

612
import (
713
"crypto"
814
"crypto/rand"
915
"crypto/rsa"
1016
"crypto/sha256"
1117
"crypto/sha512"
12-
"crypto/subtle"
1318
"errors"
19+
"fmt"
1420
"hash"
1521
"io"
1622
"math/big"
17-
18-
"github.com/cloudflare/circl/blindsign"
1923
)
2024

2125
var errUnsupportedHashFunction = errors.New("unsupported hash function")
2226

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 {
2631
// Public key of the Signer
2732
pk *rsa.PublicKey
2833

@@ -33,8 +38,8 @@ type RSAVerifier struct {
3338
hash hash.Hash
3439
}
3540

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 {
3843
// Public key of the Signer
3944
pk *rsa.PublicKey
4045

@@ -45,6 +50,14 @@ type DeterminsiticRSAVerifier struct {
4550
hash hash.Hash
4651
}
4752

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+
4861
func convertHashFunction(hash crypto.Hash) hash.Hash {
4962
switch hash {
5063
case crypto.SHA256:
@@ -58,61 +71,78 @@ func convertHashFunction(hash crypto.Hash) hash.Hash {
5871
}
5972
}
6073

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 {
6376
h := convertHashFunction(hash)
64-
return DeterminsiticRSAVerifier{
77+
return DeterminsiticBRSAVerifier{
6578
pk: pk,
6679
cryptoHash: hash,
6780
hash: h,
6881
}
6982
}
7083

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 {
7394
h := convertHashFunction(hash)
74-
return RSAVerifier{
95+
return RandomBRSAVerifier{
7596
pk: pk,
7697
cryptoHash: hash,
7798
hash: h,
7899
}
79100
}
80101

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) {
82111
hash.Reset() // Ensure the hash state is cleared
83112
hash.Write(message)
84113
digest := hash.Sum(nil)
85114
hash.Reset()
86-
emBits := key.N.BitLen() - 1
115+
emBits := N.BitLen() - 1
87116
encodedMsg, err := emsaPSSEncode(digest[:], emBits, salt, hash)
88117
return encodedMsg, err
89118
}
90119

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) {
92121
randReader := random
93122
if randReader == nil {
94123
randReader = rand.Reader
95124
}
96-
r, err := rand.Int(randReader, key.N)
125+
r, err := rand.Int(randReader, N)
97126
if err != nil {
98127
return nil, nil, err
99128
}
100129

101130
if r.Sign() == 0 {
102131
r = bigOne
103132
}
104-
rInv := new(big.Int).ModInverse(r, key.N)
133+
rInv := new(big.Int).ModInverse(r, N)
105134
if rInv == nil {
135+
fmt.Println(r, N)
106136
return nil, nil, ErrInvalidBlind
107137
}
108138

109139
return r, rInv, nil
110140
}
111141

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)
114144
if err != nil {
115-
return nil, nil, err
145+
return nil, BRSAVerifierState{}, err
116146
}
117147

118148
m := new(big.Int).SetBytes(encodedMsg)
@@ -127,7 +157,7 @@ func fixedBlind(message, salt []byte, r, rInv *big.Int, pk *rsa.PublicKey, hash
127157
blindedMsg := make([]byte, kLen)
128158
z.FillBytes(blindedMsg)
129159

130-
return blindedMsg, RSAVerifierState{
160+
return blindedMsg, BRSAVerifierState{
131161
encodedMsg: encodedMsg,
132162
pk: pk,
133163
hash: hash,
@@ -141,34 +171,44 @@ func fixedBlind(message, salt []byte, r, rInv *big.Int, pk *rsa.PublicKey, hash
141171
//
142172
// See the specification for more details:
143173
// 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) {
145175
if random == nil {
146-
return nil, nil, ErrInvalidRandomness
176+
return nil, BRSAVerifierState{}, ErrInvalidRandomness
147177
}
148178

149-
r, rInv, err := generateBlindingFactor(random, v.pk)
179+
r, rInv, err := generateBlindingFactor(random, v.pk.N)
150180
if err != nil {
151-
return nil, nil, err
181+
return nil, BRSAVerifierState{}, err
152182
}
153183

154184
return fixedBlind(message, nil, r, rInv, v.pk, v.hash)
155185
}
156186

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)
167207
}
168208

169209
// 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)
172212
}
173213

174214
// 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 {
177217
//
178218
// See the specification for more details:
179219
// 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) {
181221
if random == nil {
182-
return nil, nil, ErrInvalidRandomness
222+
return nil, BRSAVerifierState{}, ErrInvalidRandomness
183223
}
184224

185225
salt := make([]byte, v.hash.Size())
186226
_, err := random.Read(salt)
187227
if err != nil {
188-
return nil, nil, err
228+
return nil, BRSAVerifierState{}, err
189229
}
190230

191-
r, rInv, err := generateBlindingFactor(random, v.pk)
231+
r, rInv, err := generateBlindingFactor(random, v.pk.N)
192232
if err != nil {
193-
return nil, nil, err
233+
return nil, BRSAVerifierState{}, err
194234
}
195235

196236
return fixedBlind(message, salt, r, rInv, v.pk, v.hash)
197237
}
198238

199239
// 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) {
201241
if blind == nil {
202-
return nil, nil, ErrInvalidRandomness
242+
return nil, BRSAVerifierState{}, ErrInvalidRandomness
203243
}
204244

205245
r := new(big.Int).SetBytes(blind)
206246
rInv := new(big.Int).ModInverse(r, v.pk.N)
207247
if rInv == nil {
208-
return nil, nil, ErrInvalidBlind
248+
return nil, BRSAVerifierState{}, ErrInvalidBlind
209249
}
210250

211251
return fixedBlind(message, salt, r, rInv, v.pk, v.hash)
212252
}
213253

214254
// 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)
217257
}
218258

219-
// An RSAVerifierState carries state needed to complete the blind signature protocol
259+
// An BRSAVerifierState carries state needed to complete the blind signature protocol
220260
// as a verifier.
221-
type RSAVerifierState struct {
261+
type BRSAVerifierState struct {
222262
// Public key of the Signer
223263
pk *rsa.PublicKey
224264

@@ -235,23 +275,11 @@ type RSAVerifierState struct {
235275
rInv *big.Int
236276
}
237277

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-
250278
// Finalize computes and outputs the final signature, if it's valid. Otherwise, it returns an error.
251279
//
252280
// See the specification for more details:
253281
// 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) {
255283
kLen := (state.pk.N.BitLen() + 7) / 8
256284
if len(data) != kLen {
257285
return nil, ErrUnexpectedSize
@@ -265,7 +293,7 @@ func (state RSAVerifierState) Finalize(data []byte) ([]byte, error) {
265293
sig := make([]byte, kLen)
266294
s.FillBytes(sig)
267295

268-
err := verifyBlindSignature(state.pk, state.encodedMsg, sig)
296+
err := verifyBlindSignature(convertToCustomPublicKey(state.pk), state.encodedMsg, sig)
269297
if err != nil {
270298
return nil, err
271299
}
@@ -274,28 +302,28 @@ func (state RSAVerifierState) Finalize(data []byte) ([]byte, error) {
274302
}
275303

276304
// CopyBlind returns an encoding of the blind value used in the protocol.
277-
func (state RSAVerifierState) CopyBlind() []byte {
305+
func (state BRSAVerifierState) CopyBlind() []byte {
278306
r := new(big.Int).ModInverse(state.rInv, state.pk.N)
279307
return r.Bytes()
280308
}
281309

282310
// 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 {
284312
salt := make([]byte, len(state.salt))
285313
copy(salt, state.salt)
286314
return salt
287315
}
288316

289-
// An RSASigner represents the Signer in the blind RSA protocol.
317+
// An BRSASigner represents the Signer in the blind RSA protocol.
290318
// It carries the raw RSA private key used for signing blinded messages.
291-
type RSASigner struct {
319+
type BRSASigner struct {
292320
// An RSA private key
293321
sk *rsa.PrivateKey
294322
}
295323

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{
299327
sk: sk,
300328
}
301329
}
@@ -305,7 +333,7 @@ func NewRSASigner(sk *rsa.PrivateKey) RSASigner {
305333
//
306334
// See the specification for more details:
307335
// 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) {
309337
kLen := (signer.sk.N.BitLen() + 7) / 8
310338
if len(data) != kLen {
311339
return nil, ErrUnexpectedSize
@@ -316,7 +344,7 @@ func (signer RSASigner) BlindSign(data []byte) ([]byte, error) {
316344
return nil, ErrInvalidMessageLength
317345
}
318346

319-
s, err := decryptAndCheck(rand.Reader, signer.sk, m)
347+
s, err := decryptAndCheck(rand.Reader, convertToCustomPrivateKey(signer.sk), m)
320348
if err != nil {
321349
return nil, err
322350
}

0 commit comments

Comments
 (0)