AES GCM slow
Michael StJohns
mstjohns at comcast.net
Mon Jan 27 18:16:42 UTC 2014
At 12:17 PM 1/27/2014, Matthew Hall wrote:
>I have often had good luck accelerating such operations using the PKCS #11 provider with a recent copy of libnss to get the native crypto. And the things needed for this are built into Java already.
Yup. https://developer.mozilla.org/en-US/docs/NSS/NSS_3.14.2_release_notes
>--
>Sent from my mobile device.
>
>Michael StJohns <mstjohns at comcast.net> wrote:
>At 09:23 AM 1/27/2014, Mark Christiaens wrote:
>>Silly me, forgot to mention that I'm working on Ubuntu, 64 bit, 13.10.
>>
>>So, AES-CBC seems to be reasonably fast (100 MiB/s) but AES-GCM is slow (5.2 MiB/s). Ã I'm particularly curious about the GCM one because I get the impression that OpenSSL should be able to reach in the GB/s for AES-GCM encryption/authentication.Ã
>>
>>Mark
>
>
>GCM uses a GF2 multiply as part of the integrity calculation. That operation is pretty expensive. My guess is that if the code was profiled, you'd find a lot of time being spent in com.sun.crypto.provider.GHASH.
>
>The more recent intel processors have a set of instructions that substantially improve this performance - http://en.wikipedia.org/wiki/CLMUL_instruction_set - but the code in the standard provider is all pure java and doesn't take advantage of this as far as I can tell. I believe that the more recent versions of OpenSSL *have* been updated to take advantage of the new instructions which explains their performance.
>
>The same processors generally also support an AES instruction set so if someone were to build a native version of this it might be useful to also replace/augment the default AES block cipher implementation.
>
>Also see http://software.intel.com/en-us/articles/intel-aes-ni-performance-testing-on-linuxjava-stack
>
>Mike
>
>
>
>>On Mon, Jan 27, 2014 at 3:19 PM, Xuelei Fan <<mailto:xuelei.fan at oracle.com>xuelei.fan at oracle.com> wrote:
>>What's the platform are you using for the testing? Ã Windows, Linux,
>>Solaris or Mac OS? Ã GCM are now only implemented in SunJCE provider. Ã I
>>want to make sure the crypto provider for AES-CBC, which is different
>>for different platforms by default, is not the major cause of the
>>performance impact.
>>Thanks for the performance measure.
>>Regards,
>>Xuelei
>>On 1/27/2014 5:34 PM, Chris Hegarty wrote:
>>> Cross posting to security-dev, since the question cipher related.
>>>
>>> -Chris.
>>>
>>> On 27/01/14 09:28, Mark Christiaens wrote:
>>>> I wrote à a little test client/server setup that transfers 100 MB of data
>>>> over an SSL socket configured to use TLS 1.2 AES GCM
>>>> (TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256). Ã On my i7-4770 CPU @ 3.40GHz
>>>> with OpenJDK 1.8.0-ea-b124 I get a transfer rate of around 5.2
>>>> MiB/second. Ã I expected a higher speed. Ã Using
>>>> TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 I reach 100 MiB/s. Ã Is this to
>>>> be expected?
>>>>
>>>> For reference, here is my code:
>>>>
>>>> ///// Client.java
>>>>
>>>> package ssl;
>>>>
>>>> import javax.net.ssl.*;
>>>> import java.io.*;
>>>> import java.util.Arrays;
>>>>
>>>> public class Client {
>>>>
>>>> Ã Ã Ã public static void main(String[] arstring) {
>>>> Ã Ã Ã Ã Ã try {
>>>> Ã Ã Ã Ã Ã Ã Ã SSLSocketFactory sslsocketfactory = (SSLSocketFactory)
>>>> SSLSocketFactory.getDefault();
>>>> Ã Ã Ã Ã Ã Ã Ã SSLSocket sslsocket = (SSLSocket)
>>>> sslsocketfactory.createSocket("localhost", 9999);
>>>> Ã Ã Ã Ã Ã Ã Ã Helper.requireAESCipherSuites(sslsocket);
>>>> Ã Ã Ã Ã Ã Ã Ã sslsocket.setEnabledProtocols(new String[]{"TLSv1.2"});
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã try (OutputStream outputstream =
>>>> sslsocket.getOutputStream()) {
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã byte[] buf = new byte[Helper.BUF_SIZE];
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã Arrays.fill(buf, (byte) 1);
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã for (int i = 0; i < Helper.BUF_COUNT; ++i) {
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã Ã Ã outputstream.write(buf);
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã }
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã System.out.println("Using cipher suite: " +
>>>> (sslsocket.getSession()).getCipherSuite());
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã outputstream.flush();
>>>> Ã Ã Ã Ã Ã Ã Ã }
>>>>
>>>> Ã Ã Ã Ã Ã } catch (IOException exception) {
>>>> Ã Ã Ã Ã Ã Ã Ã exception.printStackTrace();
>>>> Ã Ã Ã Ã Ã }
>>>> Ã Ã Ã }
>>>> }
>>>>
>>>> ///// Server.java
>>>>
>>>> package ssl;
>>>>
>>>> import javax.net.ssl.*;
>>>> import java.io.*;
>>>>
>>>> public class Server {
>>>>
>>>> Ã Ã Ã public static void main(String[] arstring) {
>>>> Ã Ã Ã Ã Ã try {
>>>> Ã Ã Ã Ã Ã Ã Ã SSLServerSocketFactory sslserversocketfactory =
>>>> (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
>>>> Ã Ã Ã Ã Ã Ã Ã SSLServerSocket sslserversocket = (SSLServerSocket)
>>>> sslserversocketfactory.createServerSocket(9999);
>>>> Ã Ã Ã Ã Ã Ã Ã SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã InputStream inputstream = sslsocket.getInputStream();
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã byte[] buf = new byte[Helper.BUF_SIZE];
>>>> Ã Ã Ã Ã Ã Ã Ã long bytesToRead = BYTES_TO_READ;
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã long startTime = System.currentTimeMillis();
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã while (bytesToRead > 0) {
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã bytesToRead -= inputstream.read(buf);
>>>> Ã Ã Ã Ã Ã Ã Ã }
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã long stopTime = System.currentTimeMillis();
>>>> Ã Ã Ã Ã Ã Ã Ã long totalTimeMs = stopTime - startTime;
>>>> Ã Ã Ã Ã Ã Ã Ã double mbRead = BYTES_TO_READ / (1024.0 * 1024);
>>>> Ã Ã Ã Ã Ã Ã Ã double totalTimeSeconds = totalTimeMs / 1000.0;
>>>> Ã Ã Ã Ã Ã Ã Ã double mibPerSecond = mbRead / totalTimeSeconds;
>>>>
>>>> Ã Ã Ã Ã Ã Ã Ã System.out.println("Using cipher suite: " +
>>>> (sslsocket.getSession()).getCipherSuite());
>>>> Ã Ã Ã Ã Ã Ã Ã System.out.println("Read " + mbRead + "MiB in " +
>>>> totalTimeSeconds + "s");
>>>> Ã Ã Ã Ã Ã Ã Ã System.out.println("Bandwidth: " + mibPerSecond + "MiB/s");
>>>>
>>>> Ã Ã Ã Ã Ã } catch (IOException exception) {
>>>> Ã Ã Ã Ã Ã Ã Ã exception.printStackTrace();
>>>> Ã Ã Ã Ã Ã }
>>>> Ã Ã Ã }
>>>>
>>>> Ã Ã Ã private static final int BYTES_TO_READ = Helper.BUF_COUNT *
>>>> Helper.BUF_SIZE;
>>>> }
>>>>
>>>> ///// Helper.java
>>>>
>>>> package ssl;
>>>>
>>>> import java.util.*;
>>>> import java.util.regex.*;
>>>> import javax.net.ssl.*;
>>>>
>>>> public class Helper {
>>>>
>>>> Ã Ã Ã static int BUF_SIZE = 1024 * 1024;
>>>> Ã Ã Ã static int BUF_COUNT = 100;
>>>>
>>>> Ã Ã Ã static SSLSocket requireAESCipherSuites(SSLSocket socket) {
>>>> Ã Ã Ã Ã Ã String supportedCipherSuites[] =
>>>> socket.getSupportedCipherSuites();
>>>>
>>>> Ã Ã Ã Ã Ã System.out.println("Supported cipher suite: " +
>>>> Arrays.toString(supportedCipherSuites));
>>>>
>>>> Ã Ã Ã Ã Ã List<String> selectedCipherSuites = new ArrayList<>();
>>>>
>>>> // Ã Ã Ã Ã String patternString = ".*";
>>>> Ã Ã Ã Ã Ã String patternString = "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
>>>> // Ã Ã Ã Ã String patternString =
>>>> "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
>>>>
>>>> Ã Ã Ã Ã Ã Pattern pattern = Pattern.compile(patternString);
>>>>
>>>> Ã Ã Ã Ã Ã for (String cipherSuite : supportedCipherSuites) {
>>>> Ã Ã Ã Ã Ã Ã Ã Matcher matcher = pattern.matcher(cipherSuite);
>>>> Ã Ã Ã Ã Ã Ã Ã if (matcher.find()) {
>>>> Ã Ã Ã Ã Ã Ã Ã Ã Ã selectedCipherSuites.add(cipherSuite);
>>>> Ã Ã Ã Ã Ã Ã Ã }
>>>> Ã Ã Ã Ã Ã }
>>>>
>>>> Ã Ã Ã Ã Ã System.out.println("Selected cipher suites: " +
>>>> selectedCipherSuites);
>>>>
>>>> Ã Ã Ã Ã Ã socket.setEnabledCipherSuites(selectedCipherSuites.toArray(new
>>>> String[0]));
>>>>
>>>> Ã Ã Ã Ã Ã return socket;
>>>> Ã Ã Ã }
>>>> }
>>>>
>>
>>
>>
>>
>>--
>>Mark Christiaens
>>Ganzeplas 23
>>9880 Aalter
>>09 / 325 07 40
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/security-dev/attachments/20140127/04c88c07/attachment.htm>
More information about the security-dev
mailing list