Skip to content

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

Closed

Description

@bartonjs

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.

Activity

  1. added
    design-discussion
    on Mar 5, 2022
  2. added this to the Future milestone on Mar 5, 2022
  3. ghost added
    untriaged
    on Mar 5, 2022
  4. vcsjones commented on Mar 5, 2022

    @vcsjones
    Member

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

  5. bartonjs commented on Mar 5, 2022

    @bartonjs
    MemberAuthor

    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 😄

  6. vcsjones commented on Mar 5, 2022

    @vcsjones
    Member

    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.

  7. nguerrera commented on Mar 5, 2022

    @nguerrera
    Contributor

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

  8. bartonjs commented on Mar 7, 2022

    @bartonjs
    MemberAuthor

    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.

  9. vcsjones commented on Mar 7, 2022

    @vcsjones
    Member

    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.

  10. GrabYourPitchforks commented on Mar 7, 2022

    @GrabYourPitchforks
    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.

  11. removed
    untriaged
    on Mar 15, 2022
  12. vcsjones commented on Mar 24, 2022

    @vcsjones
    Member

    Since #66349 was merged, closing this.

  13. ghost locked as resolved and limited conversation to collaborators on Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Securitydesign-discussion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions