You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
(Should we) Provide implementations for {RSA,DSA,ECDsa}.HashData(?) #66228
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.
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 😄
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.
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.
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.
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.
Activity
vcsjones commentedon Mar 5, 2022
Seems reasonable. I can throw up a PR to see what it'd look like.
bartonjs commentedon 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 commentedon Mar 5, 2022
Well,
TryHashDatais not virtual + throw, it has an implementation that callsHashData. That is… probably… a breaking change of some kind if we stopped calling a virtual that others were implementing.runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/RSA.cs
Line 100 in 3ae8739
We would want to continue to override
TryHashDatain 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 commentedon Mar 5, 2022
I'm afraid any details I had about this have long escaped my memory. 😂
bartonjs commentedon 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 commentedon Mar 7, 2022
If we make it astatic readonlyfield 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 commentedon Mar 7, 2022
I think you can implement an internal interface
IMySpecialMarkeron all of the sealed types, thenif (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 intoif (true)at trim time.vcsjones commentedon Mar 24, 2022
Since #66349 was merged, closing this.