-
Notifications
You must be signed in to change notification settings - Fork 10
/
revocation.go
71 lines (60 loc) · 1.95 KB
/
revocation.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package bridge
import (
// "crypto/ecdsa"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-amcl/amcl/FP256BN"
"github.com/hyperledger/fabric/bccsp"
cryptolib "github.com/hyperledger/fabric/idemix"
"github.com/pkg/errors"
"github.com/tjfoc/gmsm/sm2"
)
// Revocation encapsulates the idemix algorithms for revocation
type Revocation struct {
}
// NewKey generate a new revocation key-pair.
func (*Revocation) NewKey() (*sm2.PrivateKey, error) {
return cryptolib.GenerateLongTermRevocationKey()
}
// Sign generates a new CRI with the respect to the passed unrevoked handles, epoch, and revocation algorithm.
func (*Revocation) Sign(key *sm2.PrivateKey, unrevokedHandles [][]byte, epoch int, alg bccsp.RevocationAlgorithm) (res []byte, err error) {
defer func() {
if r := recover(); r != nil {
res = nil
err = errors.Errorf("failure [%s]", r)
}
}()
handles := make([]*FP256BN.BIG, len(unrevokedHandles))
for i := 0; i < len(unrevokedHandles); i++ {
handles[i] = FP256BN.FromBytes(unrevokedHandles[i])
}
cri, err := cryptolib.CreateCRI(key, handles, epoch, cryptolib.RevocationAlgorithm(alg), NewRandOrPanic())
if err != nil {
return nil, errors.WithMessage(err, "failed creating CRI")
}
return proto.Marshal(cri)
}
// Verify checks that the passed serialised CRI (criRaw) is valid with the respect to the passed revocation public key,
// epoch, and revocation algorithm.
func (*Revocation) Verify(pk *sm2.PublicKey, criRaw []byte, epoch int, alg bccsp.RevocationAlgorithm) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.Errorf("failure [%s]", r)
}
}()
cri := &cryptolib.CredentialRevocationInformation{}
err = proto.Unmarshal(criRaw, cri)
if err != nil {
return err
}
return cryptolib.VerifyEpochPK(
pk,
cri.EpochPk,
cri.EpochPkSig,
int(cri.Epoch),
cryptolib.RevocationAlgorithm(cri.RevocationAlg),
)
}