Skip to content

Commit

Permalink
[FAB-3441] bccsp/signer test coverage
Browse files Browse the repository at this point in the history
This change-set improves the test coverage
of bccsp/signer to 100%

Change-Id: I8f0e49ee4e47538b6ce156ca228bad2779c72de0
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Apr 29, 2017
1 parent c5c4c51 commit 9a33854
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 129 deletions.
97 changes: 90 additions & 7 deletions bccsp/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,109 @@ limitations under the License.

package mocks

import "github.com/hyperledger/fabric/bccsp"
import (
"crypto"
"errors"
"hash"
"reflect"

type MockKey struct{}
"github.com/hyperledger/fabric/bccsp"
)

func (*MockKey) Bytes() ([]byte, error) {
type MockBCCSP struct {
SignArgKey bccsp.Key
SignDigestArg []byte
SignOptsArg bccsp.SignerOpts

SignValue []byte
SignErr error

VerifyValue bool
VerifyErr error
}

func (*MockBCCSP) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
panic("implement me")
}

func (*MockKey) SKI() []byte {
func (*MockBCCSP) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
panic("implement me")
}

func (*MockKey) Symmetric() bool {
func (*MockBCCSP) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
panic("implement me")
}

func (*MockKey) Private() bool {
func (*MockBCCSP) GetKey(ski []byte) (k bccsp.Key, err error) {
panic("implement me")
}

func (*MockBCCSP) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error) {
panic("implement me")
}

func (*MockBCCSP) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
panic("implement me")
}

func (b *MockBCCSP) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
if !reflect.DeepEqual(b.SignArgKey, k) {
return nil, errors.New("invalid key")
}
if !reflect.DeepEqual(b.SignDigestArg, digest) {
return nil, errors.New("invalid digest")
}
if !reflect.DeepEqual(b.SignOptsArg, opts) {
return nil, errors.New("invalid opts")
}

return b.SignValue, b.SignErr
}

func (b *MockBCCSP) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
return b.VerifyValue, b.VerifyErr
}

func (*MockBCCSP) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) {
panic("implement me")
}

func (*MockKey) PublicKey() (bccsp.Key, error) {
func (*MockBCCSP) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
panic("implement me")
}

type MockKey struct {
BytesValue []byte
BytesErr error
Symm bool
PK bccsp.Key
PKErr error
}

func (m *MockKey) Bytes() ([]byte, error) {
return m.BytesValue, m.BytesErr
}

func (*MockKey) SKI() []byte {
panic("implement me")
}

func (m *MockKey) Symmetric() bool {
return m.Symm
}

func (*MockKey) Private() bool {
panic("implement me")
}

func (m *MockKey) PublicKey() (bccsp.Key, error) {
return m.PK, m.PKErr
}

type SignerOpts struct {
HashFuncValue crypto.Hash
}

func (o *SignerOpts) HashFunc() crypto.Hash {
return o.HashFuncValue
}
6 changes: 2 additions & 4 deletions bccsp/pkcs11/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,8 +1004,7 @@ func TestKeyImportFromX509ECDSAPublicKey(t *testing.T) {
},
}

cryptoSigner := &signer.CryptoSigner{}
err = cryptoSigner.Init(currentBCCSP, k)
cryptoSigner, err := signer.New(currentBCCSP, k)
if err != nil {
t.Fatalf("Failed initializing CyrptoSigner [%s]", err)
}
Expand Down Expand Up @@ -1815,8 +1814,7 @@ func TestKeyImportFromX509RSAPublicKey(t *testing.T) {
},
}

cryptoSigner := &signer.CryptoSigner{}
err = cryptoSigner.Init(currentBCCSP, k)
cryptoSigner, err := signer.New(currentBCCSP, k)
if err != nil {
t.Fatalf("Failed initializing CyrptoSigner [%s]", err)
}
Expand Down
44 changes: 15 additions & 29 deletions bccsp/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,49 @@ import (
"github.com/hyperledger/fabric/bccsp/utils"
)

// CryptoSigner is the BCCSP-based implementation of a crypto.Signer
type CryptoSigner struct {
// bccspCryptoSigner is the BCCSP-based implementation of a crypto.Signer
type bccspCryptoSigner struct {
csp bccsp.BCCSP
key bccsp.Key
pk interface{}
}

// Init initializes this CryptoSigner.
func (s *CryptoSigner) Init(csp bccsp.BCCSP, key bccsp.Key) error {
// New returns a new BCCSP-based crypto.Signer
// for the given BCCSP instance and key.
func New(csp bccsp.BCCSP, key bccsp.Key) (crypto.Signer, error) {
// Validate arguments
if csp == nil {
return errors.New("Invalid BCCSP. Nil.")
return nil, errors.New("bccsp instance must be different from nil.")
}
if key == nil {
return errors.New("Invalid Key. Nil.")
return nil, errors.New("key must be different from nil.")
}
if key.Symmetric() {
return errors.New("Invalid Key. Symmetric.")
return nil, errors.New("key must be asymmetric.")
}

// Marshall the bccsp public key as a crypto.PublicKey
pub, err := key.PublicKey()
if err != nil {
return fmt.Errorf("Failed getting public key [%s]", err)
return nil, fmt.Errorf("failed getting public key [%s]", err)
}

raw, err := pub.Bytes()
if err != nil {
return fmt.Errorf("Failed marshalling public key [%s]", err)
return nil, fmt.Errorf("failed marshalling public key [%s]", err)
}

pk, err := utils.DERToPublicKey(raw)
if err != nil {
return fmt.Errorf("Failed marshalling public key [%s]", err)
return nil, fmt.Errorf("failed marshalling der to public key [%s]", err)
}

// Init fields
s.csp = csp
s.key = key
s.pk = pk

return nil

return &bccspCryptoSigner{csp, key, pk}, nil
}

// Public returns the public key corresponding to the opaque,
// private key.
func (s *CryptoSigner) Public() crypto.PublicKey {
func (s *bccspCryptoSigner) Public() crypto.PublicKey {
return s.pk
}

Expand All @@ -89,15 +84,6 @@ func (s *CryptoSigner) Public() crypto.PublicKey {
// 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) and the hash function (as opts) to Sign.
func (s *CryptoSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
if opts == nil {
return s.csp.Sign(s.key, digest, nil)
}

so, ok := opts.(bccsp.SignerOpts)
if !ok {
return nil, errors.New("Invalid opts type. Expecting bccsp.SignerOpts")
}

return s.csp.Sign(s.key, digest, so)
func (s *bccspCryptoSigner) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
return s.csp.Sign(s.key, digest, opts)
}
Loading

0 comments on commit 9a33854

Please sign in to comment.