From f410895930c699fef147d9542bd9e1e76fb341c1 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Fri, 19 Aug 2016 14:45:20 -0700 Subject: [PATCH 1/3] test: crypto createClass instanceof Class The crypto classes are also exposed as createClass for each class. This tests that each of them returns an instance of the class in question. PR-URL: https://github.com/nodejs/node/pull/8188 Reviewed-By: Ben Noordhuis Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Rich Trott --- test/parallel/test-crypto-classes.js | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/parallel/test-crypto-classes.js diff --git a/test/parallel/test-crypto-classes.js b/test/parallel/test-crypto-classes.js new file mode 100644 index 00000000000000..ed6bfd76c1e012 --- /dev/null +++ b/test/parallel/test-crypto-classes.js @@ -0,0 +1,31 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); + +if (!common.hasCrypto) { + common.skip('missing crypto'); + return; +} +const crypto = require('crypto'); + +// 'ClassName' : ['args', 'for', 'constructor'] +const TEST_CASES = { + 'Hash': ['sha1'], + 'Hmac': ['sha1', 'Node'], + 'Cipheriv': ['des-ede3-cbc', '0123456789abcd0123456789', '12345678'], + 'Decipheriv': ['des-ede3-cbc', '0123456789abcd0123456789', '12345678'], + 'Sign': ['RSA-SHA1'], + 'Verify': ['RSA-SHA1'], + 'DiffieHellman': [1024], + 'DiffieHellmanGroup': ['modp5'], + 'Credentials': [] +}; + +if (!common.hasFipsCrypto) { + TEST_CASES.Cipher = ['aes192', 'secret']; + TEST_CASES.Decipher = ['aes192', 'secret']; +} + +for (const [clazz, args] of Object.entries(TEST_CASES)) { + assert(crypto[`create${clazz}`](...args) instanceof crypto[clazz]); +} From 68f4e248ebd0ba2ac929a7a2eb4b1dc76b286706 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Fri, 19 Aug 2016 13:56:58 -0700 Subject: [PATCH 2/3] crypto: expose ECDH class For consistency with the rest of the crypto classes, exposes the ECDH class. Originally, only the createECDH function was exposed, and there was no real reason to hide the class. PR-URL: https://github.com/nodejs/node/pull/8188 Reviewed-By: Ben Noordhuis Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/crypto.js | 8 +++----- test/parallel/test-crypto-classes.js | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index 56795e23f24af9..8b5690a3b88570 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -67,7 +67,6 @@ function toBuf(str, encoding) { } exports._toBuf = toBuf; - const assert = require('assert'); const StringDecoder = require('string_decoder').StringDecoder; @@ -559,17 +558,16 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) { }; +exports.createECDH = exports.ECDH = ECDH; function ECDH(curve) { + if (!(this instanceof ECDH)) + return new ECDH(curve); if (typeof curve !== 'string') throw new TypeError('"curve" argument should be a string'); this._handle = new binding.ECDH(curve); } -exports.createECDH = function createECDH(curve) { - return new ECDH(curve); -}; - ECDH.prototype.computeSecret = DiffieHellman.prototype.computeSecret; ECDH.prototype.setPrivateKey = DiffieHellman.prototype.setPrivateKey; ECDH.prototype.setPublicKey = DiffieHellman.prototype.setPublicKey; diff --git a/test/parallel/test-crypto-classes.js b/test/parallel/test-crypto-classes.js index ed6bfd76c1e012..3923cb0dc7cfa8 100644 --- a/test/parallel/test-crypto-classes.js +++ b/test/parallel/test-crypto-classes.js @@ -18,6 +18,7 @@ const TEST_CASES = { 'Verify': ['RSA-SHA1'], 'DiffieHellman': [1024], 'DiffieHellmanGroup': ['modp5'], + 'ECDH': ['prime256v1'], 'Credentials': [] }; From 367116ec1b769b27f670776c0c3778e4b7249c77 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Thu, 28 Sep 2017 11:57:36 -0700 Subject: [PATCH 3/3] test: fix flaky test-crypto-classes.js On non-FIPS, we can instantiate DiffieHellman with 256 instead of 1024. This should be quite a bit faster, and therefore prevent the timeouts. PR-URL: https://github.com/nodejs/node/pull/15662 Fixes: https://github.com/nodejs/node/issues/15655 Reviewed-By: Refael Ackermann Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Rich Trott --- test/parallel/test-crypto-classes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-crypto-classes.js b/test/parallel/test-crypto-classes.js index 3923cb0dc7cfa8..78d248c2b2850c 100644 --- a/test/parallel/test-crypto-classes.js +++ b/test/parallel/test-crypto-classes.js @@ -25,6 +25,7 @@ const TEST_CASES = { if (!common.hasFipsCrypto) { TEST_CASES.Cipher = ['aes192', 'secret']; TEST_CASES.Decipher = ['aes192', 'secret']; + TEST_CASES.DiffieHellman = [256]; } for (const [clazz, args] of Object.entries(TEST_CASES)) {