diff options
author | Wei-Wei Wu <wuxx1045@umn.edu> | 2018-03-01 17:13:12 -0800 |
---|---|---|
committer | Tobias Nießen <tniessen@tnie.de> | 2018-03-23 19:56:59 +0100 |
commit | f2e02883e7ef6ff58331e980f91bc31d8076a5c8 (patch) | |
tree | 81055ee1ca2f44f3f127af876584a2c4c7910df2 /test/parallel/test-crypto-ecdh-convert-key.js | |
parent | a0adf56855f59a301d9a1f69b4267ced4fd4cf03 (diff) | |
download | android-node-v8-f2e02883e7ef6ff58331e980f91bc31d8076a5c8.tar.gz android-node-v8-f2e02883e7ef6ff58331e980f91bc31d8076a5c8.tar.bz2 android-node-v8-f2e02883e7ef6ff58331e980f91bc31d8076a5c8.zip |
crypto: add ECDH.convertKey to convert public keys
ECDH.convertKey is used to convert public keys between different
formats.
PR-URL: https://github.com/nodejs/node/pull/19080
Fixes: https://github.com/nodejs/node/issues/18977
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-crypto-ecdh-convert-key.js')
-rw-r--r-- | test/parallel/test-crypto-ecdh-convert-key.js | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/test/parallel/test-crypto-ecdh-convert-key.js b/test/parallel/test-crypto-ecdh-convert-key.js new file mode 100644 index 0000000000..407178a195 --- /dev/null +++ b/test/parallel/test-crypto-ecdh-convert-key.js @@ -0,0 +1,101 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); + +const { ECDH, getCurves } = require('crypto'); + +// A valid private key for the secp256k1 curve. +const cafebabeKey = 'cafebabe'.repeat(8); +// Associated compressed and uncompressed public keys (points). +const cafebabePubPtComp = + '03672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3'; +const cafebabePubPtUnComp = + '04672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3' + + '2e02c7f93d13dc2732b760ca377a5897b9dd41a1c1b29dc0442fdce6d0a04d1d'; + +// Invalid test: key argument is undefined. +common.expectsError( + () => ECDH.convertKey(), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + }); + +// Invalid test: curve argument is undefined. +common.expectsError( + () => ECDH.convertKey(cafebabePubPtComp), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + }); + +// Invalid test: curve argument is invalid. +common.expectsError( + () => ECDH.convertKey(cafebabePubPtComp, 'badcurve'), + { + type: TypeError, + message: 'Invalid ECDH curve name' + }); + +if (getCurves().includes('secp256k1')) { + // Invalid test: format argument is undefined. + common.expectsError( + () => ECDH.convertKey(cafebabePubPtComp, 'secp256k1', 'hex', 'hex', 10), + { + code: 'ERR_CRYPTO_ECDH_INVALID_FORMAT', + type: TypeError, + message: 'Invalid ECDH format: 10' + }); + + // Point formats. + let uncompressed = ECDH.convertKey(cafebabePubPtComp, + 'secp256k1', + 'hex', + 'buffer', + 'uncompressed'); + let compressed = ECDH.convertKey(cafebabePubPtComp, + 'secp256k1', + 'hex', + 'buffer', + 'compressed'); + let hybrid = ECDH.convertKey(cafebabePubPtComp, + 'secp256k1', + 'hex', + 'buffer', + 'hybrid'); + assert.strictEqual(uncompressed[0], 4); + let firstByte = compressed[0]; + assert(firstByte === 2 || firstByte === 3); + firstByte = hybrid[0]; + assert(firstByte === 6 || firstByte === 7); + + // Format conversion from hex to hex + uncompressed = ECDH.convertKey(cafebabePubPtComp, + 'secp256k1', + 'hex', + 'hex', + 'uncompressed'); + compressed = ECDH.convertKey(cafebabePubPtComp, + 'secp256k1', + 'hex', + 'hex', + 'compressed'); + hybrid = ECDH.convertKey(cafebabePubPtComp, + 'secp256k1', + 'hex', + 'hex', + 'hybrid'); + assert.strictEqual(uncompressed, cafebabePubPtUnComp); + assert.strictEqual(compressed, cafebabePubPtComp); + + // Compare to getPublicKey. + const ecdh1 = ECDH('secp256k1'); + ecdh1.generateKeys(); + ecdh1.setPrivateKey(cafebabeKey, 'hex'); + assert.strictEqual(ecdh1.getPublicKey('hex', 'uncompressed'), uncompressed); + assert.strictEqual(ecdh1.getPublicKey('hex', 'compressed'), compressed); + assert.strictEqual(ecdh1.getPublicKey('hex', 'hybrid'), hybrid); +} |