Skip to content

Commit

Permalink
[FAB-1756] Add support for SHA384 hash
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1756

Adds basic support for using SHA384 hash algorithm,
other known as SHA2 384.

Note:  this does not add support for using SHA384
as part of key derivation via HMAC.  That will be
added in the future as needed.

Fixes FAB-1756

Change-Id: I9b5c108db0757dcf7c46c932f252c3664d74fb15
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Jan 19, 2017
1 parent 7eef633 commit f61aad3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
5 changes: 5 additions & 0 deletions hfc/lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var jsSHA3 = require('js-sha3');
var sha3_256 = jsSHA3.sha3_256;
var sha3_384 = jsSHA3.sha3_384;
var shake_256 = jsSHA3.shake_256;
var crypto = require('crypto');

hash_sha2_256 = function (hash) {

Expand Down Expand Up @@ -166,5 +167,9 @@ exports.sha2_256 = function (data) {
return sjcl_codec.hex.fromBits(new sjcl.hash.sha256().update(bytesToBits(Buffer.from(data, 'utf8'))).finalize());
};
exports.sha3_256 = sha3_256;
exports.sha2_384 = function (data){
var sha384 = crypto.createHash('sha384');
return sha384.update(data).digest('hex');
};
exports.sha3_384 = sha3_384;
exports.shake_256 = shake_256;
4 changes: 4 additions & 0 deletions hfc/lib/impl/CryptoSuite_ECDSA_AES.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ var CryptoSuite_ECDSA_AES = class extends api.CryptoSuite {
this._hashFunction = hashPrimitives.sha2_256;
this._hashFunctionKeyDerivation = hashPrimitives.hash_sha2_256;
break;
case 'sha2-384':
this._hashFunction = hashPrimitives.sha2_384;
//TODO: this._hashFunctionKeyDerivation = xxxxxxx;
break;
default:
throw Error(util.format('Unsupported hash algorithm and key size pair: %s-%s', hashAlgo, this._keySize));
}
Expand Down
29 changes: 16 additions & 13 deletions test/unit/headless-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,8 @@ var TEST_LONG_MSG = 'The Hyperledger project is an open source collaborative eff
'ensure the transparency, longevity, interoperability and support required to bring blockchain technologies forward to mainstream commercial adoption. That ' +
'is what Hyperledger is about – communities of software developers building blockchain frameworks and platforms.';

var HASH_MSG_SHA384 = '6247065855a812ecd182476576c02d46a675845ef4b0056e973ca42dcf8191d3adabc8c6c4b909f20f96136032ab723a';
var HASH_LONG_MSG_SHA384 = 'e647ea97fec64412a34f522b5d80cbba9a293f89d4dc63802c79bf485078ecbaed59a0d53cd7ab08a9ae983e64f886a6';
var HASH_MSG_SHA3_384 = '9e9c2e5edf6cbc0b512807a8efa2917daff71b83e04dee28fcc00b1a1dd935fb5afc5eafa06bf55bd64792a597e2a8f3';
var HASH_LONG_MSG_SHA3_384 = '47a90d6721523682e09b81da0a60e6ee1faf839f0503252316638daf038cf682c0a842edaf310eb0f480a2e181a07af0';
var HASH_MSG_SHA256 = '4e4aa09b6d80efbd684e80f54a70c1d8605625c3380f4cb012b32644a002b5be';
Expand Down Expand Up @@ -1428,13 +1430,25 @@ test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA256,
'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 256');

// test SHA384 hash
utils.setConfigSetting('crypto-hash-algo', 'sha2');
utils.setConfigSetting('crypto-keysize', 384);
cryptoUtils = utils.getCryptoSuite();
t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA384,
'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 384');

//reset to default key size
utils.setConfigSetting('crypto-keysize', 256);
cryptoUtils = utils.getCryptoSuite();

cryptoUtils.generateKey()
.then(function (key) {
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');
utils.setConfigSetting('crypto-keysize', 256);
cryptoUtils = utils.getCryptoSuite();
return cryptoUtils.generateKey();
})
Expand All @@ -1453,10 +1467,10 @@ test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
cryptoUtils = utils.getCryptoSuite();

t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA3_384,
'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with key size 384');
'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 384');

t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA3_384,
'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with key size 384');
'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 384');

return cryptoUtils.generateKey();
})
Expand All @@ -1483,17 +1497,6 @@ test('\n\n ** CryptoSuite_ECDSA_AES - function tests **\n\n', function (t) {
else
t.fail('CryptoSuite_ECDSA_AES function tests: verify generateKey ephemeral=true return object');

t.throws(
function () {
utils.setConfigSetting('crypto-hash-algo', 'sha2');
utils.setConfigSetting('crypto-keysize', 384);
cryptoUtils = utils.getCryptoSuite();
},
/^Error: Unsupported/,
'CryptoSuite_ECDSA_AES function tests: SHA2 and 384 should throw ' +
'Error: Unsupported hash algorithm and security level pair sha2-384'
);

t.throws(
function () {
utils.setConfigSetting('crypto-keysize', 123);
Expand Down

0 comments on commit f61aad3

Please sign in to comment.