-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
crypto: support outputLength in XOF hash 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
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aditi-1400 please change the PR title and commit message to crypto: support outputLength in crypto.hash for XOF functions
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #58121 +/- ##
==========================================
+ Coverage 90.20% 90.21% +0.01%
==========================================
Files 635 635
Lines 187616 187635 +19
Branches 36847 36865 +18
==========================================
+ Hits 169234 169276 +42
+ Misses 11150 11147 -3
+ Partials 7232 7212 -20
🚀 New features to boost your workflow:
|
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 comment
The 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?
deps/ncrypto/ncrypto.cc
Outdated
size_t output_length) { | ||
if (md == nullptr) return {}; | ||
|
||
EVP_MD_CTX* ctx = EVP_MD_CTX_new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use EVPMDCtxPointer
@@ -280,6 +280,9 @@ class Digest final { | |||
|
|||
DataPointer hashDigest(const Buffer<const unsigned char>& data, | |||
const EVP_MD* md); | |||
DataPointer xofHashDigest(const Buffer<const unsigned char>& data, | |||
const EVP_MD* md, | |||
size_t length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could use a code comment to describe the differences between hashDigest(...)
and xosHashDigest(...)
doc/api/crypto.md
Outdated
@@ -4187,7 +4187,7 @@ A convenient alias for [`crypto.webcrypto.getRandomValues()`][]. This | |||
implementation is not compliant with the Web Crypto spec, to write | |||
web-compatible code use [`crypto.webcrypto.getRandomValues()`][] instead. | |||
|
|||
### `crypto.hash(algorithm, data[, outputEncoding])` | |||
### `crypto.hash(algorithm, data[, outputEncoding, outputLength])` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for this I'd rather we support an alternative signature that accepts either an encoding for outputEncoding
or an options object... e.g.
// current signature
crypto.hash(algorithm, data[, outputEncoding]);
crypto.hash(algorithm, data[, options]);
// where `options` is:
// { outputEncoding: '...', outputLength: ... }
Makes things more scalable in case additional options need to be added later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if we wanted to support null
, for now I've kept the same behaviour as
Line 318 in 2281a04
function getOptions(options, defaultOptions = kEmptyObject) { |
Also,
crypto.createHash()
just ignores all the invalid options, so let me know which behaviour we should have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not supporting null is fine, it should be either a string or an object (we have many overload like this e.g. in buffer)
src/crypto/crypto_hash.cc
Outdated
int output_length = args[6].As<Uint32>()->Value(); | ||
if (isXOF) { | ||
return ncrypto::xofHashDigest(buf, md, output_length); | ||
} else if (!isXOF && output_length != EVP_MD_get_size(md)) { | ||
ThrowCryptoError(env, ERR_get_error(), "Invalid length or not XOF"); | ||
return {}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int output_length = args[6].As<Uint32>()->Value(); | |
if (isXOF) { | |
return ncrypto::xofHashDigest(buf, md, output_length); | |
} else if (!isXOF && output_length != EVP_MD_get_size(md)) { | |
ThrowCryptoError(env, ERR_get_error(), "Invalid length or not XOF"); | |
return {}; | |
} | |
int output_length = args[6].As<Uint32>()->Value(); | |
if (isXOF) { | |
return ncrypto::xofHashDigest(buf, md, output_length); | |
} | |
if (output_length != EVP_MD_get_size(md)) { | |
Utf8Value method(isolate, args[0]); | |
std::string message = "Output length " + std::to_string(output_length) + " is invalid for "; | |
message += method.ToString() + ", which does not support XOF"; | |
ThrowCryptoError(env, ERR_get_error(), message.c_str()); | |
return {}; | |
} | |
// Does not support XOF but output length matches expected digest size. | |
// Compute digest as usual. |
Support `outputLength` option in crypto.hash() for XOF hash functions to align with the behaviour of crypto.createHash() API
Support
outputLength
option in crypto.hash() for XOF hash functions to align with the behaviour ofcrypto.createHash()
APIFixes: #57312