Skip to content
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
merged 2 commits into from
May 13, 2024

Conversation

eerhardt
Copy link
Contributor

@eerhardt eerhardt commented May 9, 2024

Calling HashAlgorithm.ComputeHash will always allocate and return a new byte array. On .NET 6+ we can instead use a temporary buffer (either on the stack for normal hashing, or use the array pool for hash algorithms that need more than 2k bits) to reduce the intermediate allocation.

cc @bartonjs - in case you want to take a look.

Calling HashAlgorithm.ComputeHash will always allocate and return a new byte array. On .NET 6+ we can instead use a temporary buffer (either on the stack for normal hashing, or use the array pool for hash algorithms that need more than 2k bits) to reduce the intermediate allocation.
@eerhardt eerhardt requested a review from a team as a code owner May 9, 2024 19:23
@jennyf19 jennyf19 added this to the 7.5.2 milestone May 9, 2024
}

private bool VerifyUsingOffsetRsa(byte[] bytes, int offset, int count, byte[] signature)
{
#if NET6_0_OR_GREATER
return VerifyUsingSpan(isRSA: true, bytes.AsSpan(offset, count), signature);
#else
return RSA.VerifyHash(HashAlgorithm.ComputeHash(bytes, offset, count), signature, HashAlgorithmName, RSASignaturePadding);
Copy link

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)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and let the framework do the hashing for you (which won't create a temporary array)?

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):

using System.Security.Cryptography;

RSA rSA = RSA.Create();

byte[] data = [0x1, 0x2, 0x3, 0x4];

byte[] signature = rSA.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

for (int i = 0; i < 1_000; i++)
{
    rSA.VerifyData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}

image

Copy link
Contributor Author

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?

Copy link

@bartonjs bartonjs May 9, 2024

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

return rsa.VerifyData(
#if NET5_OR_GREATER
    bytes.AsSpan(offset, count),
#else
    bytes, offset, count,
#endif
    signature,
    HashAlgorithmName,
    RSASignaturePadding);

should take your numbers down to 0 (for .NET5+)

Copy link
Contributor Author

@eerhardt eerhardt May 9, 2024

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

image

HashData returns a new byte[].

Test Code:

using System.Security.Cryptography;

RSA algo = RSA.Create();

byte[] data = [0x1, 0x2, 0x3, 0x4];

byte[] signature = algo.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

for (int i = 0; i < 1_000; i++)
{
    algo.VerifyData(data.AsSpan(), signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}

Copy link

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.

Copy link
Contributor Author

@eerhardt eerhardt May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@keegan-caruso keegan-caruso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jennyf19 jennyf19 merged commit ea42b6b into dev May 13, 2024
5 checks passed
@eerhardt eerhardt deleted the eerhardt/FixAllocationsInAsymmetricAdapter branch May 13, 2024 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants