Skip to content

Commit 4464f6c

Browse files
committed
MSP crytpo and HASH cleanup
This change-sets removes all dependencies to the crytpo/primitives package from the msp package. The MSP uses now only the BCCSP for its cryptographic operations. This change-sets also removes all dependencies to the primitives.Hash function. The BCCSP is used to replace thos calls. Change-Id: Ia1432fac29745bb4f42b9acb8334caf69f115b3d Signed-off-by: Angelo De Caro <[email protected]>
1 parent e8daa0e commit 4464f6c

File tree

8 files changed

+92
-43
lines changed

8 files changed

+92
-43
lines changed

core/crypto/bccsp/factory/factory.go

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ func GetDefault() (bccsp.BCCSP, error) {
7070
return defaultBCCSP, nil
7171
}
7272

73+
// GetDefaultOrPanic returns a non-ephemeral (long-term) BCCSP or panic if an error occurs.
74+
func GetDefaultOrPanic() bccsp.BCCSP {
75+
if err := initFactories(); err != nil {
76+
panic(err)
77+
}
78+
79+
return defaultBCCSP
80+
}
81+
7382
// GetBCCSP returns a BCCSP created according to the options passed in input.
7483
func GetBCCSP(opts Opts) (bccsp.BCCSP, error) {
7584
if err := initFactories(); err != nil {

core/crypto/bccsp/sw/dummyks.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sw
2+
3+
import (
4+
"errors"
5+
6+
"github.com/hyperledger/fabric/core/crypto/bccsp"
7+
)
8+
9+
// DummyKeyStore is a read-only KeyStore that neither loads nor stores keys.
10+
type DummyKeyStore struct {
11+
}
12+
13+
// ReadOnly returns true if this KeyStore is read only, false otherwise.
14+
// If ReadOnly is true then StoreKey will fail.
15+
func (ks *DummyKeyStore) ReadOnly() bool {
16+
return true
17+
}
18+
19+
// GetKey returns a key object whose SKI is the one passed.
20+
func (ks *DummyKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
21+
return nil, errors.New("Key not found. This is a dummy KeyStore")
22+
}
23+
24+
// StoreKey stores the key k in this KeyStore.
25+
// If this KeyStore is read only then the method will fail.
26+
func (ks *DummyKeyStore) StoreKey(k bccsp.Key) (err error) {
27+
return errors.New("Cannot store key. This is a dummy read-only KeyStore")
28+
}

core/crypto/bccsp/sw/impl.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
)
4747

4848
// NewDefaultSecurityLevel returns a new instance of the software-based BCCSP
49-
// at security level 256, hash family SHA2 and using FolderBasedKeyStore as keystore.
49+
// at security level 256, hash family SHA2 and using FolderBasedKeyStore as KeyStore.
5050
func NewDefaultSecurityLevel(keyStorePath string) (bccsp.BCCSP, error) {
5151
ks := &FileBasedKeyStore{}
5252
if err := ks.Init(nil, keyStorePath, false); err != nil {
@@ -56,8 +56,14 @@ func NewDefaultSecurityLevel(keyStorePath string) (bccsp.BCCSP, error) {
5656
return New(256, "SHA2", ks)
5757
}
5858

59+
// NewDefaultSecurityLevel returns a new instance of the software-based BCCSP
60+
// at security level 256, hash family SHA2 and using the passed KeyStore.
61+
func NewDefaultSecurityLevelWithKeystore(keyStore bccsp.KeyStore) (bccsp.BCCSP, error) {
62+
return New(256, "SHA2", keyStore)
63+
}
64+
5965
// New returns a new instance of the software-based BCCSP
60-
// set at the passed security level, hash family and keystore.
66+
// set at the passed security level, hash family and KeyStore.
6167
func New(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (bccsp.BCCSP, error) {
6268
// Init config
6369
conf := &config{}
@@ -66,7 +72,7 @@ func New(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (bccsp.B
6672
return nil, fmt.Errorf("Failed initializing configuration [%s]", err)
6773
}
6874

69-
// Check keystore
75+
// Check KeyStore
7076
if keyStore == nil {
7177
return nil, errors.New("Invalid bccsp.KeyStore instance. It must be different from nil.")
7278
}

msp/identities.go

+26-17
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import (
2525

2626
"encoding/asn1"
2727

28+
"errors"
29+
2830
"github.com/hyperledger/fabric/core/crypto/bccsp"
29-
"github.com/hyperledger/fabric/core/crypto/bccsp/factory"
3031
"github.com/hyperledger/fabric/core/crypto/bccsp/signer"
31-
"github.com/hyperledger/fabric/core/crypto/primitives"
3232
)
3333

3434
type identity struct {
@@ -42,12 +42,12 @@ type identity struct {
4242
pk bccsp.Key
4343

4444
// reference to the MSP that "owns" this identity
45-
myMsp MSP
45+
msp *bccspmsp
4646
}
4747

48-
func newIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, myMsp MSP) Identity {
48+
func newIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, msp *bccspmsp) Identity {
4949
mspLogger.Infof("Creating identity instance for ID %s", id)
50-
return &identity{id: id, cert: cert, pk: pk, myMsp: myMsp}
50+
return &identity{id: id, cert: cert, pk: pk, msp: msp}
5151
}
5252

5353
// GetIdentifier returns the identifier (MSPID/IDID) for this instance
@@ -62,7 +62,7 @@ func (id *identity) GetMSPIdentifier() string {
6262

6363
// IsValid returns nil if this instance is a valid identity or an error otherwise
6464
func (id *identity) IsValid() error {
65-
return id.myMsp.Validate(id)
65+
return id.msp.Validate(id)
6666
}
6767

6868
// GetOrganizationUnits returns the OU for this instance
@@ -76,21 +76,22 @@ func (id *identity) GetOrganizationUnits() string {
7676
// signature; it returns nil if so or an error otherwise
7777
func (id *identity) Verify(msg []byte, sig []byte) error {
7878
mspLogger.Infof("Verifying signature")
79-
bccsp, err := factory.GetDefault()
79+
80+
// Compute Hash
81+
digest, err := id.msp.bccsp.Hash(msg, &bccsp.SHAOpts{})
8082
if err != nil {
81-
return fmt.Errorf("Failed getting default BCCSP [%s]", err)
82-
} else if bccsp == nil {
83-
return fmt.Errorf("Failed getting default BCCSP. Nil instance.")
83+
return fmt.Errorf("Failed computing digest [%s]", err)
8484
}
8585

86-
valid, err := bccsp.Verify(id.pk, sig, primitives.Hash(msg), nil)
86+
// Verify signature
87+
valid, err := id.msp.bccsp.Verify(id.pk, sig, digest, nil)
8788
if err != nil {
8889
return fmt.Errorf("Could not determine the validity of the signature, err %s", err)
8990
} else if !valid {
90-
return fmt.Errorf("The signature is invalid")
91-
} else {
92-
return nil
91+
return errors.New("The signature is invalid")
9392
}
93+
94+
return nil
9495
}
9596

9697
func (id *identity) VerifyOpts(msg []byte, sig []byte, opts SignatureOpts) error {
@@ -131,15 +132,23 @@ type signingidentity struct {
131132
signer *signer.CryptoSigner
132133
}
133134

134-
func newSigningIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, signer *signer.CryptoSigner, myMsp MSP) SigningIdentity {
135+
func newSigningIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, signer *signer.CryptoSigner, msp *bccspmsp) SigningIdentity {
135136
mspLogger.Infof("Creating signing identity instance for ID %s", id)
136-
return &signingidentity{identity{id: id, cert: cert, pk: pk, myMsp: myMsp}, signer}
137+
return &signingidentity{identity{id: id, cert: cert, pk: pk, msp: msp}, signer}
137138
}
138139

139140
// Sign produces a signature over msg, signed by this instance
140141
func (id *signingidentity) Sign(msg []byte) ([]byte, error) {
141142
mspLogger.Infof("Signing message")
142-
return id.signer.Sign(rand.Reader, primitives.Hash(msg), nil)
143+
144+
// Compute Hash
145+
digest, err := id.msp.bccsp.Hash(msg, &bccsp.SHAOpts{})
146+
if err != nil {
147+
return nil, fmt.Errorf("Failed computing digest [%s]", err)
148+
}
149+
150+
// Sign
151+
return id.signer.Sign(rand.Reader, digest, nil)
143152
}
144153

145154
func (id *signingidentity) SignOpts(msg []byte, opts SignatureOpts) ([]byte, error) {

msp/msp_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"os"
55
"reflect"
66
"testing"
7-
8-
"github.com/hyperledger/fabric/core/crypto/primitives"
97
)
108

119
var localMsp MSP
@@ -158,8 +156,6 @@ func TestSignAndVerify(t *testing.T) {
158156
}
159157

160158
func TestMain(m *testing.M) {
161-
primitives.SetSecurityLevel("SHA2", 256)
162-
163159
retVal := m.Run()
164160
os.Exit(retVal)
165161
}

msp/mspimpl.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"encoding/asn1"
2929

3030
"github.com/hyperledger/fabric/core/crypto/bccsp"
31-
"github.com/hyperledger/fabric/core/crypto/bccsp/factory"
3231
"github.com/hyperledger/fabric/core/crypto/bccsp/signer"
32+
"github.com/hyperledger/fabric/core/crypto/bccsp/sw"
3333
)
3434

3535
// This is an instantiation of an MSP that
@@ -58,12 +58,11 @@ type bccspmsp struct {
5858
func NewBccspMsp() (MSP, error) {
5959
mspLogger.Infof("Creating BCCSP-based MSP instance")
6060

61-
/* TODO: is the default BCCSP okay here?*/
62-
bccsp, err := factory.GetDefault()
61+
// TODO: security level, hash family and keystore should
62+
// be probably set in the appropriate way.
63+
bccsp, err := sw.NewDefaultSecurityLevelWithKeystore(&sw.DummyKeyStore{})
6364
if err != nil {
64-
return nil, fmt.Errorf("Failed getting default BCCSP [%s]", err)
65-
} else if bccsp == nil {
66-
return nil, fmt.Errorf("Failed getting default BCCSP. Nil instance.")
65+
return nil, fmt.Errorf("Failed initiliazing BCCSP [%s]", err)
6766
}
6867

6968
theMsp := &bccspmsp{}

peer/chaincode/upgrade_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/hyperledger/fabric/peer/common"
2626
pb "github.com/hyperledger/fabric/protos/peer"
2727
// "github.com/hyperledger/fabric/protos/utils"
28-
"github.com/hyperledger/fabric/core/crypto/primitives"
2928
"github.com/hyperledger/fabric/core/peer/msp"
3029
)
3130

@@ -55,7 +54,6 @@ func initMSP() {
5554
}
5655

5756
func TestUpgradeCmd(t *testing.T) {
58-
primitives.SetSecurityLevel("SHA2", 256)
5957
InitMSP()
6058

6159
signer, err := common.GetDefaultSigner()
@@ -90,7 +88,6 @@ func TestUpgradeCmd(t *testing.T) {
9088
}
9189

9290
func TestUpgradeCmdEndorseFail(t *testing.T) {
93-
primitives.SetSecurityLevel("SHA2", 256)
9491
InitMSP()
9592

9693
signer, err := common.GetDefaultSigner()
@@ -129,7 +126,6 @@ func TestUpgradeCmdEndorseFail(t *testing.T) {
129126
}
130127

131128
func TestUpgradeCmdSendTXFail(t *testing.T) {
132-
primitives.SetSecurityLevel("SHA2", 256)
133129
InitMSP()
134130

135131
signer, err := common.GetDefaultSigner()

protos/utils/txutils.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"bytes"
2424

2525
"github.com/golang/protobuf/proto"
26-
"github.com/hyperledger/fabric/core/crypto/primitives"
26+
"github.com/hyperledger/fabric/core/crypto/bccsp"
27+
"github.com/hyperledger/fabric/core/crypto/bccsp/factory"
2728
"github.com/hyperledger/fabric/msp"
2829
"github.com/hyperledger/fabric/protos/common"
2930
"github.com/hyperledger/fabric/protos/peer"
@@ -255,11 +256,11 @@ func GetBytesProposalPayloadForTx(payload *peer.ChaincodeProposalPayload, visibi
255256
// here, as an example, I'll code the visibility policy that allows the
256257
// full header but only the hash of the payload
257258

258-
// TODO: use bccsp interfaces and providers as soon as they are ready!
259-
hash := primitives.GetDefaultHash()()
260-
hash.Write(cppBytes) // hash the serialized ChaincodeProposalPayload object (stripped of the transient bytes)
261-
262-
return hash.Sum(nil), nil
259+
digest, err := factory.GetDefaultOrPanic().Hash(cppBytes, &bccsp.SHAOpts{})
260+
if err != nil {
261+
return nil, fmt.Errorf("Failed computing digest [%s]", err)
262+
}
263+
return digest, nil
263264
}
264265

265266
// GetProposalHash2 gets the proposal hash - this version
@@ -272,8 +273,10 @@ func GetProposalHash2(header []byte, ccPropPayl []byte) ([]byte, error) {
272273
return nil, fmt.Errorf("Nil arguments")
273274
}
274275

275-
// TODO: use bccsp interfaces and providers as soon as they are ready!
276-
hash := primitives.GetDefaultHash()()
276+
hash, err := factory.GetDefaultOrPanic().GetHash(&bccsp.SHAOpts{})
277+
if err != nil {
278+
return nil, fmt.Errorf("Failed instantiating hash function [%s]", err)
279+
}
277280
hash.Write(header) // hash the serialized Header object
278281
hash.Write(ccPropPayl) // hash the bytes of the chaincode proposal payload that we are given
279282

@@ -301,7 +304,10 @@ func GetProposalHash1(header []byte, ccPropPayl []byte, visibility []byte) ([]by
301304
}
302305

303306
// TODO: use bccsp interfaces and providers as soon as they are ready!
304-
hash2 := primitives.GetDefaultHash()()
307+
hash2, err := factory.GetDefaultOrPanic().GetHash(&bccsp.SHAOpts{})
308+
if err != nil {
309+
return nil, fmt.Errorf("Failed instantiating hash function [%s]", err)
310+
}
305311
hash2.Write(header) // hash the serialized Header object
306312
hash2.Write(ppBytes) // hash of the part of the chaincode proposal payload that will go to the tx
307313

0 commit comments

Comments
 (0)