Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
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 <jasnell@gmail.com> Fixes: #22304 PR-URL: #35368 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
Showing
with
274 additions
and 1 deletion.
- +29 −0 doc/api/crypto.md
- +3 −1 lib/crypto.js
- +31 −0 lib/internal/crypto/cipher.js
- +141 −0 src/crypto/crypto_cipher.cc
- +70 −0 test/parallel/test-crypto-getcipherinfo.js
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -0,0 +1,70 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
|
|
||
| const { | ||
| getCiphers, | ||
| getCipherInfo | ||
| } = require('crypto'); | ||
|
|
||
| const assert = require('assert'); | ||
|
|
||
| const ciphers = getCiphers(); | ||
|
|
||
| assert.strictEqual(getCipherInfo(-1), undefined); | ||
| assert.strictEqual(getCipherInfo('cipher that does not exist'), undefined); | ||
|
|
||
| ciphers.forEach((cipher) => { | ||
| const info = getCipherInfo(cipher); | ||
| assert(info); | ||
| const info2 = getCipherInfo(info.nid); | ||
| assert.deepStrictEqual(info, info2); | ||
| }); | ||
|
|
||
| const info = getCipherInfo('aes-128-cbc'); | ||
| assert.strictEqual(info.name, 'aes-128-cbc'); | ||
| assert.strictEqual(info.nid, 419); | ||
| assert.strictEqual(info.blockSize, 16); | ||
| assert.strictEqual(info.ivLength, 16); | ||
| assert.strictEqual(info.keyLength, 16); | ||
| assert.strictEqual(info.mode, 'cbc'); | ||
|
|
||
| [null, undefined, [], {}].forEach((arg) => { | ||
| assert.throws(() => getCipherInfo(arg), { | ||
| code: 'ERR_INVALID_ARG_TYPE' | ||
| }); | ||
| }); | ||
|
|
||
| [null, '', 1, true].forEach((options) => { | ||
| assert.throws( | ||
| () => getCipherInfo('aes-192-cbc', options), { | ||
| code: 'ERR_INVALID_ARG_TYPE' | ||
| }); | ||
| }); | ||
|
|
||
| [null, '', {}, [], true].forEach((len) => { | ||
| assert.throws( | ||
| () => getCipherInfo('aes-192-cbc', { keyLength: len }), { | ||
| code: 'ERR_INVALID_ARG_TYPE' | ||
| }); | ||
| assert.throws( | ||
| () => getCipherInfo('aes-192-cbc', { ivLength: len }), { | ||
| code: 'ERR_INVALID_ARG_TYPE' | ||
| }); | ||
| }); | ||
|
|
||
| assert(!getCipherInfo('aes-128-cbc', { keyLength: 12 })); | ||
| assert(getCipherInfo('aes-128-cbc', { keyLength: 16 })); | ||
| assert(!getCipherInfo('aes-128-cbc', { ivLength: 12 })); | ||
| assert(getCipherInfo('aes-128-cbc', { ivLength: 16 })); | ||
|
|
||
| assert(!getCipherInfo('aes-128-ccm', { ivLength: 1 })); | ||
| assert(!getCipherInfo('aes-128-ccm', { ivLength: 14 })); | ||
| for (let n = 7; n <= 13; n++) | ||
| assert(getCipherInfo('aes-128-ccm', { ivLength: n })); | ||
|
|
||
| assert(!getCipherInfo('aes-128-ocb', { ivLength: 16 })); | ||
| for (let n = 1; n < 16; n++) | ||
| assert(getCipherInfo('aes-128-ocb', { ivLength: n })); |

