HOWTO: Fast Diffie-Hellman on Xamarin.Android

On many Xamarin.Android platforms, our built-in managed Diffie-Hellman implementation is not fast enought, which causes an SSH or TLS negotiation with a 2048-bit Diffie-Hellman prime to take several seconds (or more for larger primes).

To work around this problem, we introduced the DiffieHellmanNative class that uses Android's cryptographic API via Xamarin.Android's Java interop, providing a big speed boost.

Usage instructions

Rebex.Common.Native.dll assemblies can be found in "bin/xamarin.android" and "bin/xamarin.ios" subfolders of Rebex libraries install folder. Alternatively, they are distributed as a NuGet package, which also provides a compatible API for other mainstream platforms.

To enable fast Diffie-Hellman implementation for Xamarin.Android, add a reference to Rebex.Common.Native assembly (or the NuGet package) to your project, and enable the DiffieHellmanNative class:

using Rebex.Security.Cryptography;
...

DiffieHellmanNative.Enable();

Once this is done, Rebex libraries will use DiffieHellmanNative for Diffie-Hellman calculations.