Skip to content
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

Huge amount of time (15 seconds and more) spent to Org.BoumcyCastle.Security.DotNetUtilities.GetRsaPublicKey(...) . #3

Closed
Alex-DE-74 opened this issue Dec 25, 2017 · 5 comments

Comments

@Alex-DE-74
Copy link

Hello and thanks for great project!

This issue may be a part of BouncyCastle PSL only, but it has a cross touch to Mono.Security also.

Steps to Reproduce

        RSA issuerKey = new RSACryptoServiceProvider(512); // or 2048
        **var RsaPublicKey = DotNetUtilities.GetRsaPublicKey(issuerKey);**
              <---- TAKES A HUGE AMOUNT OF TIME

Current Behavior

I test on fast (with Intel HAX) Android Emulator running Oreo
The bold-marked line takes about 15 seconds.
All other parts are ok :)

Expected Behavior

On plain C# Programm it takes milliseconds, or lower, it's simply step over as expected.

On which platforms did you notice this

[ ] macOS
[ ] Linux
[ ] Windows
[ x ] Andoid Oreo 8.0 fast Emulator.

Version Used:

You can use mono --version or About dialog to obtain this information.

-- Alex -

@clairernovotny
Copy link
Collaborator

clairernovotny commented Dec 26, 2017

If this is an issue that only occurs on a specific platform, then it's going to have to be something the platform fixes. I would file a bug with Xamarin on this. If you file an issue with Xamarin, can you please post a link back here for tracking?

Otherwise, if there's something that occurs on all platforms, I'd recommend a bug on the main bc-csharp repo as this portable version is tracking that as closely as possible. The only changes here are in support of multi-targeting.

@Alex-DE-74
Copy link
Author

Hello Oren,

glad to see you comment!

I checked it again and see that problem occurs only on Android (can't check on iOS because I don't have one) plattform.

On Windows it takes - I do more randomly runs, not simply multiple runs for single line - apprx. 471 milliseconds.

The same game again on call to Mono.Security.X509.X509CertificateBuilder.Sign(RSA key).
This call takes also exceptionally too long on Android, but is very fast on Windows.

One remark - Windows programm is also running in x86 mode, because someone noted somewhere x64 should be much faster. So i use x86 to get comparative results.

Because I'm very new on mobile development and don't really sure, where to fill the bug report, it would be very nice if you can do this.

Many thanks for your help!

-- Alex --

@jariq
Copy link

jariq commented Dec 26, 2017

@Alex-DE-74 I don't think it is a bug. Your code is generating asymmetric RSA key pair and that is nondeterministic operation which might take not only seconds but minutes to complete. Its speed might also depend on the speed of RNG available in the target system which might be slow even in "fast emulator" depending on its uptime and available entropy sources.

You might want to take a look at comments in mono source code:

public RSACryptoServiceProvider ()
	: this (1024)
{
	// Here it's not clear if we need to generate a keypair
	// (note: MS implementation generates a keypair in this case).
	// However we:
	// (a) often use this constructor to import an existing keypair.
	// (b) take a LOT of time to generate the RSA keypair
	// So we'll generate the keypair only when (and if) it's being
	// used (or exported). This should save us a lot of time (at 
	// least in the unit tests).
}

@Alex-DE-74
Copy link
Author

Alex-DE-74 commented Dec 26, 2017

@jariq Maybe.

Of course you a right, the cryptosystem needs to gather "a lot of random noise" in order to generate strong secure key, etc...

But why it is determimistic slow on Android (fast Emulator on Windows) and determimistic fast on Windows on the same machine?

The next question - is it possible, especially for Mono.Security.X509.X509CertificateBuilder.Sign(RSA key) to speed it up?

What me wonder, if use ssl-over-http (https) with a browser client must have, on mutually authentificated new connection, the same impact. So it would take very long time to make a connection. But that is not true.

If nothing would help - it is not acceptable the user would wait a minute for connection - the only possibility is to build single-side authenticated (but not mutually authentificated) SSL-Connection. It is because Android device should not store and reuse same certificate for the next connection. What you think, it is ok in terms of connection security?

@jariq
Copy link

jariq commented Dec 27, 2017

But why it is determimistic slow on Android (fast Emulator on Windows) and determimistic fast on Windows on the same machine?

Whether we look at it from the software point of view (different OSes) or from hardware point of view (emulator surely does not have all resources of host system) I don't think term "the same machine" is applicable to your emulator and your host system.

What me wonder, if use ssl-over-http (https) with a browser client must have, on mutually authentificated new connection, the same impact. So it would take very long time to make a connection. But that is not true.

I don't think your browser generates new SSL client certificate/key for each connection. As far as I know mutually authenticated SSL connection usually uses pregenerated certificate/key on both server and client side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants