3 Star 13 Fork 4

赵春 / fabric-gm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
revocation_authority.go 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
赵春 提交于 2022-02-22 14:31 . 基础版本作成
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package idemix
import (
// "crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/asn1"
"math/big"
"gitee.com/zhaochuninhefei/gmgo/sm2"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-amcl/amcl"
"github.com/hyperledger/fabric-amcl/amcl/FP256BN"
"github.com/pkg/errors"
)
type RevocationAlgorithm int32
const (
ALG_NO_REVOCATION RevocationAlgorithm = iota
)
var ProofBytes = map[RevocationAlgorithm]int{
ALG_NO_REVOCATION: 0,
}
// GenerateLongTermRevocationKey generates a long term signing key that will be used for revocation
func GenerateLongTermRevocationKey() (*sm2.PrivateKey, error) {
// return ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
return sm2.GenerateKey(rand.Reader) //TODO
}
// CreateCRI creates the Credential Revocation Information for a certain time period (epoch).
// Users can use the CRI to prove that they are not revoked.
// Note that when not using revocation (i.e., alg = ALG_NO_REVOCATION), the entered unrevokedHandles are not used,
// and the resulting CRI can be used by any signer.
func CreateCRI(key *sm2.PrivateKey, unrevokedHandles []*FP256BN.BIG, epoch int, alg RevocationAlgorithm, rng *amcl.RAND) (*CredentialRevocationInformation, error) {
if key == nil || rng == nil {
return nil, errors.Errorf("CreateCRI received nil input")
}
cri := &CredentialRevocationInformation{}
cri.RevocationAlg = int32(alg)
cri.Epoch = int64(epoch)
if alg == ALG_NO_REVOCATION {
// put a dummy PK in the proto
cri.EpochPk = Ecp2ToProto(GenG2)
} else {
// create epoch key
_, epochPk := WBBKeyGen(rng)
cri.EpochPk = Ecp2ToProto(epochPk)
}
// sign epoch + epoch key with long term key
bytesToSign, err := proto.Marshal(cri)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal CRI")
}
digest := sha256.Sum256(bytesToSign)
cri.EpochPkSig, err = key.Sign(rand.Reader, digest[:], nil)
if err != nil {
return nil, err
}
if alg == ALG_NO_REVOCATION {
return cri, nil
} else {
return nil, errors.Errorf("the specified revocation algorithm is not supported.")
}
}
// VerifyEpochPK verifies that the revocation PK for a certain epoch is valid,
// by checking that it was signed with the long term revocation key.
// Note that even if we use no revocation (i.e., alg = ALG_NO_REVOCATION), we need
// to verify the signature to make sure the issuer indeed signed that no revocation
// is used in this epoch.
func VerifyEpochPK(pk *sm2.PublicKey, epochPK *ECP2, epochPkSig []byte, epoch int, alg RevocationAlgorithm) error {
if pk == nil || epochPK == nil {
return errors.Errorf("EpochPK invalid: received nil input")
}
cri := &CredentialRevocationInformation{}
cri.RevocationAlg = int32(alg)
cri.EpochPk = epochPK
cri.Epoch = int64(epoch)
bytesToSign, err := proto.Marshal(cri)
if err != nil {
return err
}
digest := sha256.Sum256(bytesToSign)
var sig struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(epochPkSig, &sig); err != nil {
return errors.Wrap(err, "failed unmashalling signature")
}
if !sm2.Verify(pk, digest[:], sig.R, sig.S) {
return errors.Errorf("EpochPKSig invalid")
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhaochuninhefei/fabric-gm.git
git@gitee.com:zhaochuninhefei/fabric-gm.git
zhaochuninhefei
fabric-gm
fabric-gm
v0.0.1

搜索帮助

344bd9b3 5694891 D2dac590 5694891