github.com/lzy4123/fabric@v2.1.1+incompatible/bccsp/idemix/handlers/signer.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package handlers
     7  
     8  import (
     9  	"github.com/hyperledger/fabric/bccsp"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type Signer struct {
    14  	SignatureScheme SignatureScheme
    15  }
    16  
    17  func (s *Signer) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) ([]byte, error) {
    18  	userSecretKey, ok := k.(*userSecretKey)
    19  	if !ok {
    20  		return nil, errors.New("invalid key, expected *userSecretKey")
    21  	}
    22  
    23  	signerOpts, ok := opts.(*bccsp.IdemixSignerOpts)
    24  	if !ok {
    25  		return nil, errors.New("invalid options, expected *IdemixSignerOpts")
    26  	}
    27  
    28  	// Issuer public key
    29  	if signerOpts.IssuerPK == nil {
    30  		return nil, errors.New("invalid options, missing issuer public key")
    31  	}
    32  	ipk, ok := signerOpts.IssuerPK.(*issuerPublicKey)
    33  	if !ok {
    34  		return nil, errors.New("invalid issuer public key, expected *issuerPublicKey")
    35  	}
    36  
    37  	// Nym
    38  	if signerOpts.Nym == nil {
    39  		return nil, errors.New("invalid options, missing nym key")
    40  	}
    41  	nymSk, ok := signerOpts.Nym.(*nymSecretKey)
    42  	if !ok {
    43  		return nil, errors.New("invalid nym key, expected *nymSecretKey")
    44  	}
    45  
    46  	sigma, err := s.SignatureScheme.Sign(
    47  		signerOpts.Credential,
    48  		userSecretKey.sk,
    49  		nymSk.pk, nymSk.sk,
    50  		ipk.pk,
    51  		signerOpts.Attributes,
    52  		digest,
    53  		signerOpts.RhIndex,
    54  		signerOpts.CRI,
    55  	)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return sigma, nil
    61  }
    62  
    63  type Verifier struct {
    64  	SignatureScheme SignatureScheme
    65  }
    66  
    67  func (v *Verifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (bool, error) {
    68  	issuerPublicKey, ok := k.(*issuerPublicKey)
    69  	if !ok {
    70  		return false, errors.New("invalid key, expected *issuerPublicKey")
    71  	}
    72  
    73  	signerOpts, ok := opts.(*bccsp.IdemixSignerOpts)
    74  	if !ok {
    75  		return false, errors.New("invalid options, expected *IdemixSignerOpts")
    76  	}
    77  
    78  	rpk, ok := signerOpts.RevocationPublicKey.(*revocationPublicKey)
    79  	if !ok {
    80  		return false, errors.New("invalid options, expected *revocationPublicKey")
    81  	}
    82  
    83  	if len(signature) == 0 {
    84  		return false, errors.New("invalid signature, it must not be empty")
    85  	}
    86  
    87  	err := v.SignatureScheme.Verify(
    88  		issuerPublicKey.pk,
    89  		signature,
    90  		digest,
    91  		signerOpts.Attributes,
    92  		signerOpts.RhIndex,
    93  		rpk.pubKey,
    94  		signerOpts.Epoch,
    95  	)
    96  	if err != nil {
    97  		return false, err
    98  	}
    99  
   100  	return true, nil
   101  }