-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Move KeyDerivation to use the built in Rfc2898 SHA2* support in .NET Core 2.0 #2508
Comments
From @natemcmaster on Friday, September 1, 2017 10:41:44 AM Looks like a good opportunity to delete code (a favorite pastime). Before we do, however, we should ensure we don't regress behavior by checking our test coverage. |
From @GrabYourPitchforks on Thursday, September 28, 2017 12:22:02 PM Use caution that you don't cause a perf regression when you do this. The code as currently written determines which platform it is running on and calls into the fastest implementation for the platform. This is especially important when we're talking about password hashing, which defaults to 10,000 iterations. You probably don't want to take the CPU cost of 10,000 p/invoke calls in a tight loop if you can get away with just one. (Of course, this all depends on how the Rfc2898DeriveBytes class is written.) |
From @blowdart on Friday, October 13, 2017 12:03:43 PM @GrabYourPitchforks do you think we should do this the other way around, and move the data protection version to core? |
From @GrabYourPitchforks on Friday, October 13, 2017 1:46:41 PM @blowdart that would be ideal since you could bring the perf gains to the core class. It looks like |
From @Tornhoof on Thursday, October 19, 2017 3:26:59 AM Atleast on my Windows machine, the Dataprotection version is still ~25% faster than the pbkdf2 version from .NET Core: BenchmarkDotNet=v0.10.9, OS=Windows 10 Redstone 1 (10.0.14393)
Processor=Intel Xeon CPU E5-1620 0 3.60GHz, ProcessorCount=8
Frequency=3507177 Hz, Resolution=285.1296 ns, Timer=TSC
.NET Core SDK=2.0.2
[Host] : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT
DefaultJob : .NET Core 2.0.0 (Framework 4.6.00001.0), 64bit RyuJIT
Apparently there are quite a few performance improvements in there, last time I checked against the vanilla implementation (modified to support SHA512) on netfx, the dataprotection version was pretty much twice as fast. As for source: using System.Security.Cryptography;
using System.Text;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
namespace PBKDF2Benchmark
{
public class HashBenchmark
{
[Params(1000, 10000, 100000)]
public int Iterations { get; set; }
private static readonly byte[] _password = Encoding.UTF8.GetBytes("Hello World");
private static readonly byte[] _salt = GenerateSalt();
private static byte[] GenerateSalt()
{
using (var random = new RNGCryptoServiceProvider())
{
var result = new byte[64];
random.GetBytes(result);
return result;
}
}
[Benchmark]
public byte[] Rfc2898DeriveBytesSHA512()
{
var rfc = new Rfc2898DeriveBytes(_password, _salt, Iterations, HashAlgorithmName.SHA512);
return rfc.GetBytes(64);
}
[Benchmark]
public byte[] KeyDerivationPBKDF2SHA512()
{
var key = KeyDerivation.Pbkdf2("Hello World", _salt, KeyDerivationPrf.HMACSHA512, Iterations, 512 / 8);
return key;
}
}
} |
From @natemcmaster on Friday, October 20, 2017 9:24:59 AM Thanks for the numbers. I'll take a look at these more closely before we change implementations. |
OK this does need to be the other way around. @GrabYourPitchforks do you want to create an issue in .NET Core and drive this? |
I opened https://github.com/dotnet/corefx/issues/26796 to track improving perf of Rfc2898DeriveBytes. No guarantees at the moment because I'm not sure what constraints will be placed on us when making changes or exposing Span in public API surface. |
I'm seeing something similar in perf on Windows. On macOS/Linux, it goes the other way though. @blowdart what do you think about starting with switching the Linux/macOS implementation first? Windows is still faster and allocates less. Windows x64:
macOS 10.13
Debian 9
|
That'd be fine by me |
Interesting, looks like we may not be able to do this. It would break apps that use a salt with less than 8 bytes.
|
From @blowdart on Monday, August 28, 2017 10:55:59 AM
https://docs.microsoft.com/dotnet/api/system.security.cryptography.rfc2898derivebytes now supports SHA256, SHA384 and SHA512. We could remove the code from data protection and just use the framework support.
Copied from original issue: aspnet/DataProtection#272
The text was updated successfully, but these errors were encountered: