Skip to content

Rfc2898DeriveBytes should reuse arrays during chained HMAC invocations. #20544

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

Closed
bartonjs opened this issue Mar 9, 2017 · 3 comments
Closed
Labels
area-System.Security enhancement Product code improvement that does NOT require public API changes/additions tenet-performance Performance related issue
Milestone

Comments

@bartonjs
Copy link
Member

bartonjs commented Mar 9, 2017

Rfc2898DeriveBytes provides an implementation for the PBKDF2 algorithm.

This algorithm does quite a lot of HMAC.ComputeHash calls, each of which marshals back and forth to native code and creates a byte[] to emit the answer into.

@bartonjs bartonjs self-assigned this Apr 18, 2017
@bartonjs
Copy link
Member Author

A minor complication here is that Rfc2898DeriveBytes lets you keep calling GetBytes as if it were a stream (GetBytes(16).Concat(GetBytes(16)) is the same as GetBytes(32)); and all of the native implementations are one-shot.

There certainly exist buffered workarounds, but I'm wondering if it's going to introduce more complexity than it's worth...

  • Always ask the native implementation, do local offsetting
    • N^2, where N is the number of calls to GetBytes
      • Assume all calls are asking for full blocks:
        • First call asks for one block, quick.
        • Second call asks for one block, two blocks computed (repeating work from step 1), first block skipped.
        • Repeat.
  • Use native unless (some criteria), then switch to managed
    • At some point the perf changes significantly, but probably not for most callers
  • Leave it all as managed
    • Predictable
  • Leave it all as managed, and at some point add a ComputeHash overload to HashAlgorithm which provides an array (span?) to write into, avoiding the blocks*iterations array allocations, and update this to make use of it
    • Potential win for everyone?

@bartonjs
Copy link
Member Author

Having thought about it a bit more, plan 4 seems the best (reduce the allocations) in terms of supportability and compatibility.

Since we're going to make this as an incremental improvement and not a Hail-Mary it no longer feels like we need to rush to get it in for 2.0.

@bartonjs bartonjs changed the title Rfc2898DeriveBytes should use native PBKDF2 when possible Rfc2898DeriveBytes should reuse arrays during chained HMAC invocations. Apr 24, 2017
@bartonjs bartonjs removed their assignment May 2, 2017
@bartonjs
Copy link
Member Author

Already fixed, just wasn't tagged.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Security enhancement Product code improvement that does NOT require public API changes/additions tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

2 participants