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

(Should we) Provide implementations for {RSA,DSA,ECDsa}.HashData(?) #66228

Closed
bartonjs opened this issue Mar 5, 2022 · 9 comments
Closed

(Should we) Provide implementations for {RSA,DSA,ECDsa}.HashData(?) #66228

bartonjs opened this issue Mar 5, 2022 · 9 comments
Labels
area-System.Security design-discussion Ongoing discussion about design without consensus
Milestone

Comments

@bartonjs
Copy link
Member

bartonjs commented Mar 5, 2022

All of our derived types for these algorithms now call the exact same helpers (except maybe DSACryptoServiceProvider, which fails if you ask for something other than SHA-1, because of FIPS 186-2 limitations).

Perhaps instead of re-implementing these methods identically 14 times ({CNG,CSP,OpenSSL,Android,Apple} * {RSA,DSA,ECDSA} - DSACSP)) we should define them just the 3 times.

@bartonjs bartonjs added design-discussion Ongoing discussion about design without consensus area-System.Security labels Mar 5, 2022
@bartonjs bartonjs added this to the Future milestone Mar 5, 2022
@ghost
Copy link

ghost commented Mar 5, 2022

Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

All of our derived types for these algorithms now call the exact same helpers (except maybe DSACryptoServiceProvider, which fails if you ask for something other than SHA-1, because of FIPS 186-2 limitations).

Perhaps instead of re-implementing these methods identically 14 times ({CNG,CSP,OpenSSL,Android,Apple} * {RSA,DSA,ECDSA} - DSACSP)) we should define them just the 3 times.

Author: bartonjs
Assignees: -
Labels:

design-discussion, area-System.Security

Milestone: Future

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 5, 2022
@vcsjones
Copy link
Member

vcsjones commented Mar 5, 2022

Seems reasonable. I can throw up a PR to see what it'd look like.

@bartonjs
Copy link
Member Author

bartonjs commented Mar 5, 2022

Really this is a bit "I have no idea why we made these methods as virtual+throw" (except where they're, maybe, abstract). Possibly it was just that we didn't have access to the dispatchers we wanted to use on .NET Framework (System.dll vs System.Core.dll).

Unless someone can remember a good reason why they're virtual+throw then virtual+work seems good to me.

@nguerrera any chance you have the answer in the back of your head? I feel like some of these popped up in 4.6.0 😄

@vcsjones
Copy link
Member

vcsjones commented Mar 5, 2022

Well, TryHashData is not virtual + throw, it has an implementation that calls HashData. That is… probably… a breaking change of some kind if we stopped calling a virtual that others were implementing.

protected virtual bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)

We would want to continue to override TryHashData in our implementations since we can provide a better implementation than the one that is provided.

Or, we stop calling the virtual and take it as a breaking change, if we want.

@nguerrera
Copy link
Contributor

I'm afraid any details I had about this have long escaped my memory. 😂

@bartonjs
Copy link
Member Author

bartonjs commented Mar 7, 2022

I was going to suggest something like using reflection to ask if the current type was defined in the same assembly as typeof(RSA), and wonder if the JIT would drop the body for our implementations (since they're sealed)... and decided it's probably just simpler to keep the overrides.

Though, if the JIT does do a good job there (or has a similar pattern for it) then it would let us delete more trivia.

So... less a suggestion than an area to maybe investigate.

@vcsjones
Copy link
Member

vcsjones commented Mar 7, 2022

I was going to suggest something like using reflection to ask if the current type was defined in the same assembly

If we make it a static readonly field doesn’t the JIT do a decent job of burning those in? I think we did that for “Is this Windows 10 and have pseudo handles” where we determined that works.

Ohhh I misunderstood. It can’t be a static readonly.

@GrabYourPitchforks
Copy link
Member

I think you can implement an internal interface IMySpecialMarker on all of the sealed types, then if (this is IMySpecialMarker) { /* fast-path */ } within the base class method body. In theory, the trimmer / linker should be able to deduce that there are no concrete RSA/DSA/etc.-derived types other than the sealed inbox ones that implement this special interface, so it will turn this into if (true) at trim time.

@jeffhandley jeffhandley removed the untriaged New issue has not been triaged by the area owner label Mar 15, 2022
@vcsjones
Copy link
Member

Since #66349 was merged, closing this.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Security design-discussion Ongoing discussion about design without consensus
Projects
None yet
Development

No branches or pull requests

5 participants