-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
crypto: reduce memory usage of SignFinal #23427
Changes from 1 commit
4b4b71b
e5bd9d0
202d506
27f5947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -3517,36 +3517,38 @@ void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) { | |||
sign->CheckThrow(err); | ||||
} | ||||
|
||||
static int Node_SignFinal(EVPMDPointer&& mdctx, unsigned char* md, | ||||
unsigned int* sig_len, | ||||
const EVPKeyPointer& pkey, int padding, | ||||
int pss_salt_len) { | ||||
static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx, | ||||
const EVPKeyPointer& pkey, | ||||
int padding, | ||||
int pss_salt_len) { | ||||
unsigned char m[EVP_MAX_MD_SIZE]; | ||||
unsigned int m_len; | ||||
|
||||
*sig_len = 0; | ||||
if (!EVP_DigestFinal_ex(mdctx.get(), m, &m_len)) | ||||
return 0; | ||||
return MallocedBuffer<unsigned char>(); | ||||
|
||||
int signed_sig_len = EVP_PKEY_size(pkey.get()); | ||||
CHECK_GE(signed_sig_len, 0); | ||||
size_t sig_len = static_cast<size_t>(signed_sig_len); | ||||
MallocedBuffer<unsigned char> sig(sig_len); | ||||
|
||||
size_t sltmp = static_cast<size_t>(EVP_PKEY_size(pkey.get())); | ||||
EVPKeyCtxPointer pkctx(EVP_PKEY_CTX_new(pkey.get(), nullptr)); | ||||
if (pkctx && | ||||
EVP_PKEY_sign_init(pkctx.get()) > 0 && | ||||
ApplyRSAOptions(pkey, pkctx.get(), padding, pss_salt_len) && | ||||
EVP_PKEY_CTX_set_signature_md(pkctx.get(), | ||||
EVP_MD_CTX_md(mdctx.get())) > 0 && | ||||
EVP_PKEY_sign(pkctx.get(), md, &sltmp, m, m_len) > 0) { | ||||
*sig_len = sltmp; | ||||
return 1; | ||||
EVP_PKEY_sign(pkctx.get(), sig.data, &sig_len, m, m_len) > 0) { | ||||
return MallocedBuffer<unsigned char>(sig.release(), sig_len); | ||||
} | ||||
return 0; | ||||
|
||||
return MallocedBuffer<unsigned char>(); | ||||
} | ||||
|
||||
SignBase::Error Sign::SignFinal(const char* key_pem, | ||||
int key_pem_len, | ||||
const char* passphrase, | ||||
unsigned char* sig, | ||||
unsigned int* sig_len, | ||||
MallocedBuffer<unsigned char>* buffer, | ||||
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. Could you return an |
||||
int padding, | ||||
int salt_len) { | ||||
if (!mdctx_) | ||||
|
@@ -3591,10 +3593,8 @@ SignBase::Error Sign::SignFinal(const char* key_pem, | |||
} | ||||
#endif // NODE_FIPS_MODE | ||||
|
||||
if (Node_SignFinal(std::move(mdctx), sig, sig_len, pkey, padding, salt_len)) | ||||
return kSignOk; | ||||
else | ||||
return kSignPrivateKey; | ||||
*buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len); | ||||
return buffer->is_empty() ? kSignPrivateKey : kSignOk; | ||||
} | ||||
|
||||
|
||||
|
@@ -3618,22 +3618,20 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) { | |||
int salt_len = args[3].As<Int32>()->Value(); | ||||
|
||||
ClearErrorOnReturn clear_error_on_return; | ||||
unsigned char md_value[8192]; | ||||
unsigned int md_len = sizeof(md_value); | ||||
MallocedBuffer<unsigned char> sig; | ||||
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. If the 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. @refack That would undo the purpose of this change, which is to avoid extra allocations 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.
And we have several guidelines that this breaks:
C++CG: 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.
I don’t see that being broken here.
That would go away if we implemented a
I don’t see that being broken here.
I don’t see that being broken here. Again, the rule explicitly lists it as a valid exception if the object is about to be initialized.
(same as above, 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. Yes, if we use a As for whether IMHO it breaks since we need to resize in: Line 3542 in e5bd9d0
And a resizable contiguous container is literally the definition of std::vector . AFAIU since it's a part of the language compilers can minimize heap allocations
|
||||
|
||||
Error err = sign->SignFinal( | ||||
buf, | ||||
buf_len, | ||||
len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr, | ||||
md_value, | ||||
&md_len, | ||||
&sig, | ||||
padding, | ||||
salt_len); | ||||
if (err != kSignOk) | ||||
return sign->CheckThrow(err); | ||||
|
||||
Local<Object> rc = | ||||
Buffer::Copy(env, reinterpret_cast<char*>(md_value), md_len) | ||||
Buffer::New(env, reinterpret_cast<char*>(sig.data), sig.size) | ||||
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.
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. Ohhhhh right thank you so much!!! 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. This is a very important point... I've been thinking about how to minimize that. |
||||
.ToLocalChecked(); | ||||
args.GetReturnValue().Set(rc); | ||||
} | ||||
|
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.
why are you constructing a new
MallocedBuffer
?