Skip to content

Commit

Permalink
Hash algorithms for signing and txId
Browse files Browse the repository at this point in the history
This fixes the "invalid signature" problem when submitting
proposals to the peer. Turns out the peers are now using SHA3 256
which is a recent change.

In addition, the txID calculation always uses SHA2 256 so the code
around that must be changed to not be determined by the config
setting but instead always use SHA2 256.

Change-Id: I3538e1e388fb7d0a184e508c5792c659ecf91847
Signed-off-by: Jim Zhang <jzhang@us.ibm.com>
  • Loading branch information
jimthematrix committed Feb 26, 2017
1 parent 50b9370 commit 9b9599f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion fabric-client/config/default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request-timeout" : 3000,
"tcert-batch-size" : 10,
"crypto-hash-algo": "SHA2",
"crypto-hash-algo": "SHA3",
"crypto-keysize": 256,
"crypto-hsm": false,
"crypto-suite-software": {
Expand Down
3 changes: 2 additions & 1 deletion fabric-client/lib/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var Orderer = require('./Orderer.js');
var settle = require('promise-settle');
var grpc = require('grpc');
var logger = utils.getLogger('Chain.js');
var hashPrimitives = require('./hash.js');

var _ccProto = grpc.load(__dirname + '/protos/peer/chaincode.proto').protos;
var _transProto = grpc.load(__dirname + '/protos/peer/transaction.proto').protos;
Expand Down Expand Up @@ -1616,7 +1617,7 @@ var Chain = class {
var creator_bytes = userContext.getIdentity().serialize();//same as signatureHeader.Creator
var nonce_bytes = nonce;//nonce is already in bytes
var trans_bytes = Buffer.concat([nonce_bytes, creator_bytes]);
var trans_hash = this.cryptoPrimitives.hash(trans_bytes);
var trans_hash = hashPrimitives.sha2_256(trans_bytes);
var transaction_id = Buffer.from(trans_hash).toString();
logger.debug('buildTransactionID - transaction_id %s',transaction_id);
return transaction_id;
Expand Down
15 changes: 8 additions & 7 deletions test/unit/cryptosuite-ecdsa-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
t.equal(true, (typeof cryptoUtils._ecdsaCurve !== 'undefined' && typeof cryptoUtils._ecdsa !== 'undefined'),
'CryptoSuite_ECDSA_AES function tests: default instance has "_ecdsaCurve" and "_ecdsa" properties');

// test default curve 256 with SHA256
t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA256,
// test default curve 256 with SHA3_256
t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA3_256,
'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 256');

t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA256,
t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA3_256,
'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 256');

// test SHA384 hash
Expand All @@ -288,8 +288,8 @@ test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
t.equal('secp256r1', key.getPublicKey()._key.curveName,
'CryptoSuite_ECDSA_AES function tests: cryptoUtils generated public key curveName == secp256r1');

// test curve 256 with SHA3_256
utils.setConfigSetting('crypto-hash-algo', 'SHA3');
// test curve 256 with SHA2_256
utils.setConfigSetting('crypto-hash-algo', 'SHA2');
utils.setConfigSetting('crypto-keysize', 256);
cryptoUtils = utils.getCryptoSuite();
return cryptoUtils.generateKey();
Expand All @@ -298,13 +298,14 @@ test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
t.equal('secp256r1', key.getPublicKey()._key.curveName,
'CryptoSuite_ECDSA_AES function tests: ccryptoUtils generated public key curveName == secp256r1');

t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA3_256,
t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA256,
'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 256');

t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA3_256,
t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA256,
'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 256');

// test SHA3_384
utils.setConfigSetting('crypto-hash-algo', 'SHA3');
utils.setConfigSetting('crypto-keysize', 384);
cryptoUtils = utils.getCryptoSuite();

Expand Down

0 comments on commit 9b9599f

Please sign in to comment.