runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Performance of RSA.ImportParameters on 5.0 slower than 2.1

Open lenew opened this issue 5 years ago • 7 comments

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.

lenew avatar Jan 26 '21 03:01 lenew

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.

ghost avatar Jan 26 '21 03:01 ghost

Maybe ValidatePrivateRsaParameters in 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

vcsjones avatar Jan 26 '21 14:01 vcsjones

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?

lenew avatar Jan 26 '21 15:01 lenew

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:

area-System.Security, tenet-performance, untriaged

Milestone: -

ghost avatar Jan 26 '21 16:01 ghost

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));
    }
}

bartonjs avatar Jan 26 '21 19:01 bartonjs

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

lenew avatar Jan 27 '21 03:01 lenew

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.

bartonjs avatar Feb 03 '21 17:02 bartonjs