From 095be6a01f57d5b730a01f131bab4968d3b3b0ba Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 26 Sep 2020 19:20:03 -0700 Subject: [PATCH] crypto: add getCipherInfo method Simple method for retrieving basic information about a cipher (such as block length, expected or default iv length, key length, etc) Signed-off-by: James M Snell Fixes: https://github.com/nodejs/node/issues/22304 PR-URL: https://github.com/nodejs/node/pull/35368 Reviewed-By: Ben Noordhuis --- doc/api/crypto.md | 29 +++++ lib/crypto.js | 4 +- lib/internal/crypto/cipher.js | 31 +++++ src/crypto/crypto_cipher.cc | 141 +++++++++++++++++++++ test/parallel/test-crypto-getcipherinfo.js | 70 ++++++++++ 5 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-crypto-getcipherinfo.js diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 32e90ca030bbb5..2e1744910b9f9f 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2424,6 +2424,35 @@ const ciphers = crypto.getCiphers(); console.log(ciphers); // ['aes-128-cbc', 'aes-128-ccm', ...] ``` +### `crypto.getCipherInfo(nameOrNid[, options])` + + +* `nameOrNid`: {string|number} The name or nid of the cipher to query. +* `options`: {Object} + * `keyLength`: {number} A test key length. + * `ivLength`: {number} A test IV length. +* Returns: {Object} + * `name` {string} The name of the cipher + * `nid` {number} The nid of the cipher + * `blockSize` {number} The block size of the cipher in bytes. This property + is omitted when `mode` is `'stream'`. + * `ivLength` {number} The expected or default initialization vector length in + bytes. This property is omitted if the cipher does not use an initialization + vector. + * `keyLength` {number} The expected or default key length in bytes. + * `mode` {string} The cipher mode. One of `'cbc'`, `'ccm'`, `'cfb'`, `'ctr'`, + `'ecb'`, `'gcm'`, `'ocb'`, `'ofb'`, `'stream'`, `'wrap'`, `'xts'`. + +Returns information about a given cipher. + +Some ciphers accept variable length keys and initialization vectors. By default, +the `crypto.getCipherInfo()` method will return the default values for these +ciphers. To test if a given key length or iv length is acceptable for given +cipher, use the `keyLenth` and `ivLenth` options. If the given values are +unacceptable, `undefined` will be returned. + ### `crypto.getCurves()`