Skip to content

Commit cec4b5c

Browse files
committed
Replace Shake with SHA
This change-set introduces the following: 1. add support to SHA256 in channel config 2. set default hash algorithm to SHA256 in defaultHashingAlgorithm 3. ComputeCryptoHash computes SHA256 4. Gossip crypto uses BCCSP to compute SHA256 5. Removing unsed security section from yaml files This change-set comes in the context of https://jira.hyperledger.org/browse/FAB-2354 Change-Id: I874e823ad8bc93897fd6dfd77723a77597eaff7b Signed-off-by: Angelo De Caro <[email protected]>
1 parent 0a6570f commit cec4b5c

File tree

11 files changed

+44
-109
lines changed

11 files changed

+44
-109
lines changed

common/configvalues/channel/config.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ const (
5959

6060
// Hashing algorithm types
6161
const (
62-
// SHAKE256 is the algorithm type for the sha3 shake256 hashing algorithm with 512 bits of output
63-
SHA3Shake256 = "SHAKE256"
62+
// SHA3256 is SHA3 with fixed size 256-bit hash
63+
SHA3256 = "SHA3256"
64+
65+
// SHA256 is SHA2 with fixed size 256-bit hash
66+
SHA256 = "SHA256"
6467
)
6568

6669
var logger = logging.MustGetLogger("configvalues/channel")
@@ -164,8 +167,10 @@ func (c *Config) ProposeValue(key string, configValue *cb.ConfigValue) error {
164167
return fmt.Errorf("Unmarshaling error for HashingAlgorithm: %s", err)
165168
}
166169
switch hashingAlgorithm.Name {
167-
case SHA3Shake256:
168-
c.pending.hashingAlgorithm = util.ComputeCryptoHash
170+
case SHA256:
171+
c.pending.hashingAlgorithm = util.ComputeSHA256
172+
case SHA3256:
173+
c.pending.hashingAlgorithm = util.ComputeSHA3256
169174
default:
170175
return fmt.Errorf("Unknown hashing algorithm type: %s", hashingAlgorithm.Name)
171176
}

common/configvalues/channel/config_util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/hyperledger/fabric/protos/utils"
2424
)
2525

26-
const defaultHashingAlgorithm = SHA3Shake256
26+
const defaultHashingAlgorithm = SHA256
2727

2828
func configGroup(key string, value []byte) *cb.ConfigGroup {
2929
result := cb.NewConfigGroup()

common/util/utils.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ package util
1818

1919
import (
2020
"crypto/rand"
21-
"crypto/sha256"
2221
"fmt"
2322
"io"
2423
"math/big"
2524
"strings"
2625
"time"
2726

2827
"github.com/golang/protobuf/ptypes/timestamp"
28+
"github.com/hyperledger/fabric/bccsp"
29+
"github.com/hyperledger/fabric/bccsp/factory"
2930
"github.com/hyperledger/fabric/common/metadata"
30-
"golang.org/x/crypto/sha3"
3131
)
3232

3333
type alg struct {
@@ -40,10 +40,21 @@ var availableIDgenAlgs = map[string]alg{
4040
defaultAlg: {GenerateIDfromTxSHAHash},
4141
}
4242

43-
// ComputeCryptoHash should be used in openchain code so that we can change the actual algo used for crypto-hash at one place
44-
func ComputeCryptoHash(data []byte) (hash []byte) {
45-
hash = make([]byte, 64)
46-
sha3.ShakeSum256(hash, data)
43+
// ComputeSHA256 returns SHA2-256 on data
44+
func ComputeSHA256(data []byte) (hash []byte) {
45+
hash, err := factory.GetDefaultOrPanic().Hash(data, &bccsp.SHA256Opts{})
46+
if err != nil {
47+
panic(fmt.Errorf("Failed computing SHA256 on [% x]", data))
48+
}
49+
return
50+
}
51+
52+
// ComputeSHA3256 returns SHA3-256 on data
53+
func ComputeSHA3256(data []byte) (hash []byte) {
54+
hash, err := factory.GetDefaultOrPanic().Hash(data, &bccsp.SHA3_256Opts{})
55+
if err != nil {
56+
panic(fmt.Errorf("Failed computing SHA3_256 on [% x]", data))
57+
}
4758
return
4859
}
4960

@@ -87,12 +98,12 @@ func CreateUtcTimestamp() *timestamp.Timestamp {
8798

8899
//GenerateHashFromSignature returns a hash of the combined parameters
89100
func GenerateHashFromSignature(path string, args []byte) []byte {
90-
return ComputeCryptoHash(args)
101+
return ComputeSHA256(args)
91102
}
92103

93104
// GenerateIDfromTxSHAHash generates SHA256 hash using Tx payload
94105
func GenerateIDfromTxSHAHash(payload []byte) string {
95-
return fmt.Sprintf("%x", sha256.Sum256(payload))
106+
return fmt.Sprintf("%x", ComputeSHA256(payload))
96107
}
97108

98109
// GenerateIDWithAlg generates an ID using a custom algorithm

common/util/utils_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
)
2727

2828
func TestComputeCryptoHash(t *testing.T) {
29-
if bytes.Compare(ComputeCryptoHash([]byte("foobar")), ComputeCryptoHash([]byte("foobar"))) != 0 {
29+
if bytes.Compare(ComputeSHA256([]byte("foobar")), ComputeSHA256([]byte("foobar"))) != 0 {
3030
t.Fatalf("Expected hashes to match, but they did not match")
3131
}
32-
if bytes.Compare(ComputeCryptoHash([]byte("foobar1")), ComputeCryptoHash([]byte("foobar2"))) == 0 {
32+
if bytes.Compare(ComputeSHA256([]byte("foobar1")), ComputeSHA256([]byte("foobar2"))) == 0 {
3333
t.Fatalf("Expected hashes to be different, but they match")
3434
}
3535
}

core/chaincode/chaincodetest.yaml

-39
Original file line numberDiff line numberDiff line change
@@ -446,45 +446,6 @@ ledger:
446446
# 'tire' has no additional configurations exposed as yet
447447

448448

449-
###############################################################################
450-
#
451-
# Security section - Applied to all entities (client, NVP, VP)
452-
#
453-
###############################################################################
454-
security:
455-
# Enable security will force every entity on the network to enroll with obc-ca
456-
# and maintain a valid set of certificates in order to communicate with
457-
# other peers
458-
enabled: false
459-
# To enroll NVP or VP with membersrvc. These parameters are for 1 time use.
460-
# They will not be valid on subsequent times without un-enroll first.
461-
# The values come from off-line registration with obc-ca. For testing, make
462-
# sure the values are in membersrvc/membersrvc.yaml file eca.users
463-
enrollID: vp
464-
enrollSecret: f3489fy98ghf
465-
# To enable privacy of transactions (requires security to be enabled). This
466-
# encrypts the transaction content during transit and at rest. The state
467-
# data is also encrypted
468-
privacy: false
469-
470-
# Can be 256 or 384. If you change here, you have to change also
471-
# the same property in membersrvc.yaml to the same value
472-
level: 256
473-
474-
# Can be SHA2 or SHA3. If you change here, you have to change also
475-
# the same property in membersrvc.yaml to the same value
476-
hashAlgorithm: SHA3
477-
478-
# TCerts related configuration
479-
tcert:
480-
batch:
481-
# The size of the batch of TCerts
482-
size: 200
483-
attributes:
484-
company: IBM
485-
position: "Software Engineer"
486-
487-
488449
################################################################################
489450
#
490451
# SECTION: STATETRANSFER

core/chaincode/platforms/util/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func ComputeHash(contents []byte, hash []byte) []byte {
2626
copy(newSlice[len(contents):], hash[:])
2727

2828
//compute new hash
29-
hash = util.ComputeCryptoHash(newSlice)
29+
hash = util.ComputeSHA256(newSlice)
3030

3131
return hash
3232
}

core/chaincode/platforms/util/utils_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// TestHashContentChange changes a random byte in a content and checks for hash change
1414
func TestHashContentChange(t *testing.T) {
1515
b := []byte("firstcontent")
16-
hash := util.ComputeCryptoHash(b)
16+
hash := util.ComputeSHA256(b)
1717

1818
b2 := []byte("To be, or not to be- that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die- to sleep- No more; and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wish'd.")
1919

@@ -48,7 +48,7 @@ func TestHashContentChange(t *testing.T) {
4848
// TestHashLenChange changes a random length of a content and checks for hash change
4949
func TestHashLenChange(t *testing.T) {
5050
b := []byte("firstcontent")
51-
hash := util.ComputeCryptoHash(b)
51+
hash := util.ComputeSHA256(b)
5252

5353
b2 := []byte("To be, or not to be-")
5454

@@ -70,7 +70,7 @@ func TestHashLenChange(t *testing.T) {
7070
// TestHashOrderChange changes a order of hash computation over a list of lines and checks for hash change
7171
func TestHashOrderChange(t *testing.T) {
7272
b := []byte("firstcontent")
73-
hash := util.ComputeCryptoHash(b)
73+
hash := util.ComputeSHA256(b)
7474

7575
b2 := [][]byte{[]byte("To be, or not to be- that is the question:"),
7676
[]byte("Whether 'tis nobler in the mind to suffer"),
@@ -119,7 +119,7 @@ func TestHashOrderChange(t *testing.T) {
119119
// TestHashOverFiles computes hash over a directory and ensures it matches precomputed, hardcoded, hash
120120
func TestHashOverFiles(t *testing.T) {
121121
b := []byte("firstcontent")
122-
hash := util.ComputeCryptoHash(b)
122+
hash := util.ComputeSHA256(b)
123123

124124
hash, err := HashFilesInDir(".", "hashtestfiles1", hash, nil)
125125

@@ -129,7 +129,7 @@ func TestHashOverFiles(t *testing.T) {
129129
}
130130

131131
//as long as no files under "hashtestfiles1" are changed, hash should always compute to the following
132-
expectedHash := "a4fe18bebf3d7e1c030c042903bdda9019b33829d03d9b95ab1edc8957be70dee6d786ab27b207210d29b5d9f88456ff753b8da5c244458cdcca6eb3c28a17ce"
132+
expectedHash := "0c92180028200dfabd08d606419737f5cdecfcbab403e3f0d79e8d949f4775bc"
133133

134134
computedHash := hex.EncodeToString(hash[:])
135135

@@ -140,7 +140,7 @@ func TestHashOverFiles(t *testing.T) {
140140

141141
func TestHashDiffDir(t *testing.T) {
142142
b := []byte("firstcontent")
143-
hash := util.ComputeCryptoHash(b)
143+
hash := util.ComputeSHA256(b)
144144

145145
hash1, err := HashFilesInDir(".", "hashtestfiles1", hash, nil)
146146
if err != nil {
@@ -157,7 +157,7 @@ func TestHashDiffDir(t *testing.T) {
157157
}
158158
func TestHashSameDir(t *testing.T) {
159159
b := []byte("firstcontent")
160-
hash := util.ComputeCryptoHash(b)
160+
hash := util.ComputeSHA256(b)
161161

162162
hash1, err := HashFilesInDir(".", "hashtestfiles1", hash, nil)
163163
if err != nil {

core/container/ccintf/ccintf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (ccid *CCID) GetName() string {
6767

6868
//this better be chainless system chaincode!
6969
if ccid.ChainID != "" {
70-
hash := util.ComputeCryptoHash([]byte(ccid.ChainID))
70+
hash := util.ComputeSHA256([]byte(ccid.ChainID))
7171
hexstr := hex.EncodeToString(hash[:])
7272
name = name + "-" + hexstr
7373
}

core/endorser/endorser_test.yaml

-40
Original file line numberDiff line numberDiff line change
@@ -467,46 +467,6 @@ ledger:
467467
# configurations for 'trie'
468468
# 'tire' has no additional configurations exposed as yet
469469

470-
471-
###############################################################################
472-
#
473-
# Security section - Applied to all entities (client, NVP, VP)
474-
#
475-
###############################################################################
476-
security:
477-
# Enable security will force every entity on the network to enroll with obc-ca
478-
# and maintain a valid set of certificates in order to communicate with
479-
# other peers
480-
enabled: false
481-
# To enroll NVP or VP with membersrvc. These parameters are for 1 time use.
482-
# They will not be valid on subsequent times without un-enroll first.
483-
# The values come from off-line registration with obc-ca. For testing, make
484-
# sure the values are in membersrvc/membersrvc.yaml file eca.users
485-
enrollID: vp
486-
enrollSecret: f3489fy98ghf
487-
# To enable privacy of transactions (requires security to be enabled). This
488-
# encrypts the transaction content during transit and at rest. The state
489-
# data is also encrypted
490-
privacy: false
491-
492-
# Can be 256 or 384. If you change here, you have to change also
493-
# the same property in membersrvc.yaml to the same value
494-
level: 256
495-
496-
# Can be SHA2 or SHA3. If you change here, you have to change also
497-
# the same property in membersrvc.yaml to the same value
498-
hashAlgorithm: SHA3
499-
500-
# TCerts related configuration
501-
tcert:
502-
batch:
503-
# The size of the batch of TCerts
504-
size: 200
505-
attributes:
506-
company: IBM
507-
position: "Software Engineer"
508-
509-
510470
################################################################################
511471
#
512472
# SECTION: STATETRANSFER

gossip/comm/crypto.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/ecdsa"
2121
"crypto/elliptic"
2222
"crypto/rand"
23-
"crypto/sha256"
2423
"crypto/tls"
2524
"crypto/x509"
2625
"encoding/pem"
@@ -29,6 +28,7 @@ import (
2928
"os"
3029
"time"
3130

31+
"github.com/hyperledger/fabric/common/util"
3232
"golang.org/x/net/context"
3333
"google.golang.org/grpc/credentials"
3434
"google.golang.org/grpc/peer"
@@ -75,9 +75,7 @@ func certHashFromRawCert(rawCert []byte) []byte {
7575
if len(rawCert) == 0 {
7676
return nil
7777
}
78-
hash := sha256.New()
79-
hash.Write(rawCert)
80-
return hash.Sum(nil)
78+
return util.ComputeSHA256(rawCert)
8179
}
8280

8381
// ExtractCertificateHash extracts the hash of the certificate from the stream

protos/common/block.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (b *BlockHeader) Bytes() []byte {
7171
// Hash returns the hash of the block header.
7272
// XXX This method will be removed shortly to allow for confgurable hashing algorithms
7373
func (b *BlockHeader) Hash() []byte {
74-
return util.ComputeCryptoHash(b.Bytes())
74+
return util.ComputeSHA256(b.Bytes())
7575
}
7676

7777
// Bytes returns a deterministically serialized version of the BlockData
@@ -84,5 +84,5 @@ func (b *BlockData) Bytes() []byte {
8484

8585
// Hash returns the hash of the marshaled representation of the block data.
8686
func (b *BlockData) Hash() []byte {
87-
return util.ComputeCryptoHash(b.Bytes())
87+
return util.ComputeSHA256(b.Bytes())
8888
}

0 commit comments

Comments
 (0)