-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
crypto: support outputLength option in crypto.hash for XOF functions #58121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4191,6 +4191,22 @@ DataPointer hashDigest(const Buffer<const unsigned char>& buf, | |
| return data.resize(result_size); | ||
| } | ||
|
|
||
| DataPointer xofHashDigest(const Buffer<const unsigned char>& buf, | ||
| const EVP_MD* md, | ||
| size_t output_length) { | ||
| if (md == nullptr) return {}; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @codebytere ... can I ask you to take a look at this from a Whether-This-Will-Work-In-BoringSSL perspective? |
||
|
|
||
| EVPMDCtxPointer ctx = EVPMDCtxPointer::New(); | ||
| if (!ctx) return {}; | ||
| if (ctx.digestInit(md) != 1) { | ||
| return {}; | ||
| } | ||
| if (ctx.digestUpdate(reinterpret_cast<const Buffer<const void>&>(buf)) != 1) { | ||
| return {}; | ||
| } | ||
| return ctx.digestFinal(output_length); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
|
|
||
| X509Name::X509Name() : name_(nullptr), total_(0) {} | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
test/parallel/test-crypto-default-shake-lengths-oneshot.js
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Flags: --pending-deprecation | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
|
|
||
| const { hash } = require('crypto'); | ||
|
|
||
| common.expectWarning({ | ||
| DeprecationWarning: { | ||
| DEP0198: 'Creating SHAKE128/256 digests without an explicit options.outputLength is deprecated.', | ||
| } | ||
| }); | ||
|
|
||
| { | ||
| hash('shake128', 'test'); | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| 'use strict'; | ||
| // This tests crypto.hash() works. | ||
| const common = require('../common'); | ||
|
|
||
| if (!common.hasCrypto) common.skip('missing crypto'); | ||
|
|
||
| const assert = require('assert'); | ||
| const crypto = require('crypto'); | ||
|
|
||
| // Test XOF hash functions and the outputLength option. | ||
| { | ||
| // Default outputLengths. | ||
| assert.strictEqual( | ||
| crypto.hash('shake128', ''), | ||
| '7f9c2ba4e88f827d616045507605853e' | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| crypto.hash('shake256', ''), | ||
| '46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f' | ||
| ); | ||
|
|
||
| // outputEncoding as an option. | ||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputEncoding: 'base64url' }), | ||
| 'f5wrpOiPgn1hYEVQdgWFPg' | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| crypto.hash('shake256', '', { outputEncoding: 'base64url' }), | ||
| 'RrndKwuojRMjOz_rdD7rJD_NUupiuBuCtQwnZG7Vdi8' | ||
| ); | ||
|
|
||
| assert.deepStrictEqual( | ||
| crypto.hash('shake128', '', { outputEncoding: 'buffer' }), | ||
| Buffer.from('f5wrpOiPgn1hYEVQdgWFPg', 'base64url') | ||
| ); | ||
|
|
||
| assert.deepStrictEqual( | ||
| crypto.hash('shake256', '', { outputEncoding: 'buffer' }), | ||
| Buffer.from('RrndKwuojRMjOz_rdD7rJD_NUupiuBuCtQwnZG7Vdi8', 'base64url') | ||
| ); | ||
|
|
||
| // Short outputLengths. | ||
| assert.strictEqual(crypto.hash('shake128', '', { outputLength: 0 }), ''); | ||
| assert.deepStrictEqual(crypto.hash('shake128', '', { outputEncoding: 'buffer', outputLength: 0 }), | ||
| Buffer.alloc(0)); | ||
|
|
||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputLength: 5 }), | ||
| crypto.createHash('shake128', { outputLength: 5 }).update('').digest('hex') | ||
| ); | ||
| // Check length | ||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputLength: 5 }).length, | ||
| crypto.createHash('shake128', { outputLength: 5 }).update('').digest('hex') | ||
| .length | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputLength: 15 }), | ||
| crypto.createHash('shake128', { outputLength: 15 }).update('').digest('hex') | ||
| ); | ||
| // Check length | ||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputLength: 15 }).length, | ||
| crypto.createHash('shake128', { outputLength: 15 }).update('').digest('hex') | ||
| .length | ||
| ); | ||
|
|
||
| assert.strictEqual( | ||
| crypto.hash('shake256', '', { outputLength: 16 }), | ||
| crypto.createHash('shake256', { outputLength: 16 }).update('').digest('hex') | ||
| ); | ||
| // Check length | ||
| assert.strictEqual( | ||
| crypto.hash('shake256', '', { outputLength: 16 }).length, | ||
| crypto.createHash('shake256', { outputLength: 16 }).update('').digest('hex') | ||
| .length | ||
| ); | ||
|
|
||
| // Large outputLengths. | ||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputLength: 128 }), | ||
| crypto | ||
| .createHash('shake128', { outputLength: 128 }).update('') | ||
| .digest('hex') | ||
| ); | ||
| // Check length without encoding | ||
| assert.strictEqual( | ||
| crypto.hash('shake128', '', { outputLength: 128 }).length, | ||
| crypto | ||
| .createHash('shake128', { outputLength: 128 }).update('') | ||
| .digest('hex').length | ||
| ); | ||
| assert.strictEqual( | ||
| crypto.hash('shake256', '', { outputLength: 128 }), | ||
| crypto | ||
| .createHash('shake256', { outputLength: 128 }).update('') | ||
| .digest('hex') | ||
| ); | ||
|
|
||
| const actual = crypto.hash('shake256', 'The message is shorter than the hash!', { outputLength: 1024 * 1024 }); | ||
| const expected = crypto | ||
| .createHash('shake256', { | ||
| outputLength: 1024 * 1024, | ||
| }) | ||
| .update('The message is shorter than the hash!') | ||
| .digest('hex'); | ||
| assert.strictEqual(actual, expected); | ||
|
|
||
| // Non-XOF hash functions should accept valid outputLength options as well. | ||
| assert.strictEqual(crypto.hash('sha224', '', { outputLength: 28 }), | ||
| 'd14a028c2a3a2bc9476102bb288234c4' + | ||
| '15a2b01f828ea62ac5b3e42f'); | ||
|
|
||
| // Non-XOF hash functions should fail when outputLength isn't their actual outputLength | ||
| assert.throws(() => crypto.hash('sha224', '', { outputLength: 32 }), | ||
| { message: 'Output length 32 is invalid for sha224, which does not support XOF' }); | ||
| assert.throws(() => crypto.hash('sha224', '', { outputLength: 0 }), | ||
| { message: 'Output length 0 is invalid for sha224, which does not support XOF' }); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.