Performance of RSA.ImportParameters on 5.0 slower than 2.1
Description
RSA.ImportParameters is about 100x slower on dotnet 5.0.2 compares to dotnet 2.1.
Code
var i = 1000;
while (i-- > 0)
{
var rsa = RSA.Create();
rsa.ImportParameters(rsaparameters);
}
Result: dotnet 2.1 18ms dotnet 5.0.2 2401ms
Configuration
AWS EC2 C5.LARGE(Amazon Linux 2) OpenSSL 1.0.2k-fips 26 Jan 2017
CPU Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz RAM 4G
.NET Core SDK (reflecting any global.json): Version: 2.1.809 Commit: d660ed81be
.NET SDK (reflecting any global.json): Version: 5.0.102 Commit: 71365b4d42
Analysis
Maybe ValidatePrivateRsaParameters in pal_rsa.c contribute to this issue. Because it is the only in CryptoNative_SetRsaParameters difference between 5.0 and 2.1.
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Maybe
ValidatePrivateRsaParametersin pal_rsa.c contribute to this issue.
Seems likely that could at least contribute to the issue. I don't know what could be done to improve the performance. The key validation was implemented to have Linux-based RSA do key checking similarly to how Windows does it.
If I may ask, what is your use case where importing RSA keys with ImportParameters is performance sensitive?
/cc @bartonjs
I'm using RSA to sign every response message to my client. RSA is not thread safe, so currently my workaround is using ObjectPool<RSA>. Is it possible to provide a method to clone RSA object or parameters to reduce ValidatePrivateRsaParameters calls in future?
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq See info in area-owners.md if you want to be subscribed.
Issue Details
Description
RSA.ImportParameters is about 100x slower on dotnet 5.0.2 compares to dotnet 2.1.
Code
var i = 1000;
while (i-- > 0)
{
var rsa = RSA.Create();
rsa.ImportParameters(rsaparameters);
}
Result: dotnet 2.1 18ms dotnet 5.0.2 2401ms
Configuration
AWS EC2 C5.LARGE(Amazon Linux 2) OpenSSL 1.0.2k-fips 26 Jan 2017
CPU Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz RAM 4G
.NET Core SDK (reflecting any global.json): Version: 2.1.809 Commit: d660ed81be
.NET SDK (reflecting any global.json): Version: 5.0.102 Commit: 71365b4d42
Analysis
Maybe ValidatePrivateRsaParameters in pal_rsa.c contribute to this issue. Because it is the only in CryptoNative_SetRsaParameters difference between 5.0 and 2.1.
| Author: | lenew |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Is it possible to provide a method to clone RSA object or parameters to reduce ValidatePrivateRsaParameters calls in future?
We could probably add a T Clone() pattern on each of the types immediately derived from AsymmetricAlgorithm, if that seems like it would help your scenario.
In the meantime, if you know you're only using Linux, you could explicitly instantiate RSAOpenSsl and just use key-referencing:
RSAOpenSsl seedKey = new RSAOpenSsl(rsaParameters);
using (SafeEvpPKeyHandle pkey = seedKey.DuplicateKeyHandle())
{
for (int i = 0; i < 1000; i++)
{
pool.Add(new RSAOpenSsl(pkey));
}
}
RSAOpenSsl seedKey = new RSAOpenSsl(rsaParameters); using (SafeEvpPKeyHandle pkey = seedKey.DuplicateKeyHandle()) { for (int i = 0; i < 1000; i++) { pool.Add(new RSAOpenSsl(pkey)); } }
It performs well in my scenario.
@bartonjs Thank you
Since the performance change is the cost of the key self-check that we intentionally added, and a scenario workaround has already been provided, I'm going to close the issue.
If there's still a compelling reason for something like RSA.Clone, please open a new issue for the API proposal/feature request.