forked from IBM/idemix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bccsp.go
448 lines (414 loc) · 13 KB
/
bccsp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package idemix
import (
"reflect"
"github.com/IBM/idemix/bccsp/schemes/aries"
bccsp "github.com/IBM/idemix/bccsp/types"
math "github.com/IBM/mathlib"
"github.com/ZihuaZhang/idemix/bccsp/handlers"
"github.com/ZihuaZhang/idemix/bccsp/schemes/dlog/bridge"
idemix "github.com/ZihuaZhang/idemix/bccsp/schemes/dlog/crypto"
"github.com/ale-linux/aries-framework-go/component/kmscrypto/crypto/primitive/bbs12381g2pub"
"github.com/pkg/errors"
)
type csp struct {
*CSP
}
func New(keyStore bccsp.KeyStore, curve *math.Curve, translator idemix.Translator, exportable bool) (*csp, error) {
base, err := NewImpl(keyStore)
if err != nil {
return nil, errors.Wrap(err, "failed instantiating base bccsp")
}
csp := &csp{CSP: base}
idmx := &idemix.Idemix{
Curve: curve,
Translator: translator,
}
// key generators
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixIssuerKeyGenOpts{}),
&handlers.IssuerKeyGen{
Exportable: exportable,
Issuer: &bridge.Issuer{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixUserSecretKeyGenOpts{}),
&handlers.UserKeyGen{
Exportable: exportable,
User: &bridge.User{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixRevocationKeyGenOpts{}),
&handlers.RevocationKeyGen{
Exportable: exportable,
Revocation: &bridge.Revocation{
Idemix: idmx, Translator: translator,
},
})
// key derivers
base.AddWrapper(reflect.TypeOf(handlers.NewUserSecretKey(nil, true)),
&handlers.NymKeyDerivation{
Exportable: exportable,
Translator: translator,
User: &bridge.User{
Idemix: idmx, Translator: translator,
},
})
// signers
base.AddWrapper(reflect.TypeOf(handlers.NewUserSecretKey(nil, true)),
&userSecreKeySignerMultiplexer{
signer: &handlers.Signer{
SignatureScheme: &bridge.SignatureScheme{
Idemix: idmx, Translator: translator,
}},
nymSigner: &handlers.NymSigner{
NymSignatureScheme: &bridge.NymSignatureScheme{
Idemix: idmx, Translator: translator,
}},
credentialRequestSigner: &handlers.CredentialRequestSigner{
CredRequest: &bridge.CredRequest{
Idemix: idmx, Translator: translator,
}},
})
base.AddWrapper(reflect.TypeOf(handlers.NewIssuerSecretKey(nil, true)),
&handlers.CredentialSigner{
Credential: &bridge.Credential{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(handlers.NewRevocationSecretKey(nil, true)),
&handlers.CriSigner{
Revocation: &bridge.Revocation{
Idemix: idmx, Translator: translator,
},
})
// verifiers
base.AddWrapper(reflect.TypeOf(handlers.NewIssuerPublicKey(nil)),
&issuerPublicKeyVerifierMultiplexer{
verifier: &handlers.Verifier{
SignatureScheme: &bridge.SignatureScheme{
Idemix: idmx, Translator: translator,
}},
credentialRequestVerifier: &handlers.CredentialRequestVerifier{
CredRequest: &bridge.CredRequest{
Idemix: idmx, Translator: translator,
}},
})
base.AddWrapper(reflect.TypeOf(handlers.NewNymPublicKey(nil, translator)),
&handlers.NymVerifier{
NymSignatureScheme: &bridge.NymSignatureScheme{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(handlers.NewUserSecretKey(nil, true)),
&handlers.CredentialVerifier{
Credential: &bridge.Credential{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(handlers.NewRevocationPublicKey(nil)),
&handlers.CriVerifier{
Revocation: &bridge.Revocation{
Idemix: idmx, Translator: translator,
},
})
// importers
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixUserSecretKeyImportOpts{}),
&handlers.UserKeyImporter{
Exportable: exportable,
User: &bridge.User{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixIssuerPublicKeyImportOpts{}),
&handlers.IssuerPublicKeyImporter{
Issuer: &bridge.Issuer{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixIssuerKeyImportOpts{}),
&handlers.IssuerKeyImporter{
Exportable: exportable,
Issuer: &bridge.Issuer{
Idemix: idmx, Translator: translator,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixNymPublicKeyImportOpts{}),
&handlers.NymPublicKeyImporter{
User: &bridge.User{
Idemix: idmx, Translator: translator,
},
Translator: translator,
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixNymKeyImportOpts{}),
&handlers.NymKeyImporter{
Exportable: exportable,
User: &bridge.User{
Idemix: idmx, Translator: translator,
},
Translator: translator,
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixRevocationPublicKeyImportOpts{}),
&handlers.RevocationPublicKeyImporter{})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixRevocationKeyImportOpts{}),
&handlers.RevocationKeyImporter{
Exportable: exportable,
Revocation: &bridge.Revocation{
Idemix: idmx, Translator: translator,
},
})
return csp, nil
}
func NewAries(keyStore bccsp.KeyStore, curve *math.Curve, _translator idemix.Translator, exportable bool) (*csp, error) {
base, err := NewImpl(keyStore)
if err != nil {
return nil, errors.Wrap(err, "failed instantiating base bccsp")
}
csp := &csp{CSP: base}
rng, err := curve.Rand()
if err != nil {
return nil, errors.Wrap(err, "failed instantiating PRNG")
}
// key generators
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixIssuerKeyGenOpts{}),
&handlers.IssuerKeyGen{
Exportable: exportable,
Issuer: &aries.Issuer{
Curve: curve,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixUserSecretKeyGenOpts{}),
&handlers.UserKeyGen{
Exportable: exportable,
User: &aries.User{
Curve: curve,
Rng: rng,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixRevocationKeyGenOpts{}),
&handlers.RevocationKeyGen{
Exportable: exportable,
Revocation: &aries.RevocationAuthority{
Curve: curve,
Rng: rng,
},
})
// key derivers
base.AddWrapper(reflect.TypeOf(handlers.NewUserSecretKey(nil, true)),
&handlers.NymKeyDerivation{
Exportable: exportable,
Translator: _translator,
User: &aries.User{
Curve: curve,
Rng: rng,
},
})
// signers
base.AddWrapper(reflect.TypeOf(handlers.NewUserSecretKey(nil, true)),
&userSecreKeySignerMultiplexer{
signer: &handlers.Signer{
SignatureScheme: &aries.Signer{
Curve: curve,
Rng: rng,
}},
nymSigner: &handlers.NymSigner{
NymSignatureScheme: &aries.NymSigner{
Curve: curve,
Rng: rng,
}},
blindCredentialRequestSigner: &handlers.BlindCredentialRequestSigner{
CredRequest: &aries.CredRequest{
Curve: curve,
}},
credentialRequestSigner: nil, // aries does not implement this approach
})
base.AddWrapper(reflect.TypeOf(handlers.NewIssuerSecretKey(nil, true)),
&handlers.CredentialSigner{
Credential: &aries.Cred{
Curve: curve,
Bls: bbs12381g2pub.New(curve),
},
})
base.AddWrapper(reflect.TypeOf(handlers.NewRevocationSecretKey(nil, true)),
&handlers.CriSigner{
Revocation: &aries.RevocationAuthority{
Curve: curve,
Rng: rng,
},
})
// verifiers
base.AddWrapper(reflect.TypeOf(handlers.NewIssuerPublicKey(nil)),
&issuerPublicKeyVerifierMultiplexer{
verifier: &handlers.Verifier{
SignatureScheme: &aries.Signer{
Curve: curve,
Rng: rng,
}},
blindcredentialRequestVerifier: &handlers.BlindCredentialRequestVerifier{
CredRequest: &aries.CredRequest{
Curve: curve,
}},
credentialRequestVerifier: nil, // aries does not implement this type of issuance
})
base.AddWrapper(reflect.TypeOf(handlers.NewNymPublicKey(nil, _translator)),
&handlers.NymVerifier{
NymSignatureScheme: &aries.NymSigner{
Curve: curve,
Rng: rng,
},
})
base.AddWrapper(reflect.TypeOf(handlers.NewUserSecretKey(nil, true)),
&handlers.CredentialVerifier{
Credential: &aries.Cred{
Curve: curve,
Bls: bbs12381g2pub.New(curve),
},
})
base.AddWrapper(reflect.TypeOf(handlers.NewRevocationPublicKey(nil)),
&handlers.CriVerifier{
Revocation: &aries.RevocationAuthority{
Curve: curve,
Rng: rng,
},
})
// importers
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixUserSecretKeyImportOpts{}),
&handlers.UserKeyImporter{
Exportable: exportable,
User: &aries.User{
Curve: curve,
Rng: rng,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixIssuerPublicKeyImportOpts{}),
&handlers.IssuerPublicKeyImporter{
Issuer: &aries.Issuer{
Curve: curve,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixIssuerKeyImportOpts{}),
&handlers.IssuerKeyImporter{
Exportable: exportable,
Issuer: &aries.Issuer{
Curve: curve,
},
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixNymPublicKeyImportOpts{}),
&handlers.NymPublicKeyImporter{
User: &aries.User{
Curve: curve,
Rng: rng,
},
Translator: _translator,
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixNymKeyImportOpts{}),
&handlers.NymKeyImporter{
Exportable: exportable,
User: &aries.User{
Curve: curve,
Rng: rng,
},
Translator: _translator,
})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixRevocationPublicKeyImportOpts{}),
&handlers.RevocationPublicKeyImporter{})
base.AddWrapper(reflect.TypeOf(&bccsp.IdemixRevocationKeyImportOpts{}),
&handlers.RevocationKeyImporter{
Exportable: exportable,
Revocation: &aries.RevocationAuthority{
Curve: curve,
Rng: rng,
},
})
return csp, nil
}
// Sign signs digest using key k.
// The opts argument should be appropriate for the primitive used.
//
// Note that when a signature of a hash of a larger message is needed,
// the caller is responsible for hashing the larger message and passing
// the hash (as digest).
// Notice that this is overriding the Sign methods of the sw impl. to avoid the digest check.
func (csp *csp) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
// Validate arguments
if k == nil {
return nil, errors.New("Invalid Key. It must not be nil.")
}
// Do not check for digest
keyType := reflect.TypeOf(k)
signer, found := csp.Signers[keyType]
if !found {
return nil, errors.Errorf("Unsupported 'SignKey' provided [%s]", keyType)
}
signature, err = signer.Sign(k, digest, opts)
if err != nil {
return nil, errors.Wrapf(err, "Failed signing with opts [%v]", opts)
}
return
}
// Verify verifies signature against key k and digest
// Notice that this is overriding the Sign methods of the sw impl. to avoid the digest check.
func (csp *csp) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
// Validate arguments
if k == nil {
return false, errors.New("Invalid Key. It must not be nil.")
}
if len(signature) == 0 {
return false, errors.New("Invalid signature. Cannot be empty.")
}
// Do not check for digest
verifier, found := csp.Verifiers[reflect.TypeOf(k)]
if !found {
return false, errors.Errorf("Unsupported 'VerifyKey' provided [%v]", k)
}
valid, err = verifier.Verify(k, signature, digest, opts)
if err != nil {
return false, errors.Wrapf(err, "Failed verifing with opts [%v]", opts)
}
return
}
type userSecreKeySignerMultiplexer struct {
signer *handlers.Signer
nymSigner *handlers.NymSigner
credentialRequestSigner *handlers.CredentialRequestSigner
blindCredentialRequestSigner *handlers.BlindCredentialRequestSigner
}
func (s *userSecreKeySignerMultiplexer) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
switch opts.(type) {
case *bccsp.IdemixSignerOpts:
return s.signer.Sign(k, digest, opts)
case *bccsp.IdemixNymSignerOpts:
return s.nymSigner.Sign(k, digest, opts)
case *bccsp.IdemixCredentialRequestSignerOpts:
return s.credentialRequestSigner.Sign(k, digest, opts)
case *bccsp.IdemixBlindCredentialRequestSignerOpts:
return s.blindCredentialRequestSigner.Sign(k, digest, opts)
default:
return nil, errors.New("invalid opts, expected *bccsp.IdemixSignerOpt or *bccsp.IdemixNymSignerOpts or *bccsp.IdemixCredentialRequestSignerOpts")
}
}
type issuerPublicKeyVerifierMultiplexer struct {
verifier *handlers.Verifier
credentialRequestVerifier *handlers.CredentialRequestVerifier
blindcredentialRequestVerifier *handlers.BlindCredentialRequestVerifier
}
func (v *issuerPublicKeyVerifierMultiplexer) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
switch opts.(type) {
case *bccsp.EidNymAuditOpts:
return v.verifier.AuditNymEid(k, signature, digest, opts)
case *bccsp.RhNymAuditOpts:
return v.verifier.AuditNymRh(k, signature, digest, opts)
case *bccsp.IdemixSignerOpts:
return v.verifier.Verify(k, signature, digest, opts)
case *bccsp.IdemixCredentialRequestSignerOpts:
return v.credentialRequestVerifier.Verify(k, signature, digest, opts)
case *bccsp.IdemixBlindCredentialRequestSignerOpts:
return v.blindcredentialRequestVerifier.Verify(k, signature, digest, opts)
default:
return false, errors.New("invalid opts, expected *bccsp.IdemixSignerOpts or *bccsp.IdemixCredentialRequestSignerOpts")
}
}