-
Notifications
You must be signed in to change notification settings - Fork 420
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
Remove byte[] allocation in VerifyRsa / VerifyECDsa #2589
Merged
+47
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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.
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.
Since you already have the HashAlgorithmName, why not just call VerifyData and let the framework do the hashing for you (which won't create a temporary array)?
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 doesn't appear to be the case. When I step into it, I get:
https://github.com/dotnet/runtime/blob/9926d609dab5929941b7924a04521ea2c2df27f3/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RSA.cs#L661-L662
Which allocates the
byte[]
.Here is my test code (on windows):
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.
Should I submit a similar PR to RSA and ECDsa in dotnet/runtime?
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.
It looks like VerifyData(ReadOnlySpan) uses span-hashing, but VerifyData(byte[]) uses array-hashing; because we never override
public virtual bool VerifyData(byte[], int, int, byte[], HashAlgorithmName, RSASignaturePadding)
. So the change in dotnet/runtime to make everyone happier is probably to override that method on all N of our RSA types. (RSACryptoServiceProvider, RSACng, RSAOpenSsl, RSASecurityTransforms, RSAAndroid, and maybe up to double that from the "RSA.Create()" types). Which is going to involve a lot of copy/paste of the argument validation. Bleh.Here, though, it means
should take your numbers down to 0 (for .NET5+)
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.
RSA is still allocating here:
https://github.com/dotnet/runtime/blob/9926d609dab5929941b7924a04521ea2c2df27f3/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RSA.cs#L322
HashData
returns anew byte[]
.Test Code:
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.
Weird, since it should have hit the
if (this is IRuntimeAlgorithm)
above and bypassed the allocation routine. Must've missed a marker interface somewhere, too.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.
In my case,
this
is anRSABCrypt
, which doesn't implement that interface.https://github.com/dotnet/runtime/blob/acd31754892ab0431ac2c40038f541ffa7168be7/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RSABCrypt.cs#L11
On Linux it looks like we get an
RSAWrapper
, which also doesn't implement it:https://github.com/dotnet/runtime/blob/9926d609dab5929941b7924a04521ea2c2df27f3/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RSAWrapper.cs#L10