* Crypto: JVM AES only faster for larger data size

This commit is contained in:
zzz
2014-05-09 19:13:28 +00:00
parent 8d9790fd77
commit d2e3547a2e

View File

@@ -42,6 +42,8 @@ public class CryptixAESEngine extends AESEngine {
// keys are now cached in the SessionKey objects // keys are now cached in the SessionKey objects
//private CryptixAESKeyCache _cache; //private CryptixAESKeyCache _cache;
/** see test results below */
private static final int MIN_SYSTEM_AES_LENGTH = 704;
private static final boolean USE_SYSTEM_AES; private static final boolean USE_SYSTEM_AES;
static { static {
boolean systemOK = false; boolean systemOK = false;
@@ -128,7 +130,7 @@ public class CryptixAESEngine extends AESEngine {
return; return;
} }
if (USE_SYSTEM_AES) { if (USE_SYSTEM_AES && length >= MIN_SYSTEM_AES_LENGTH) {
try { try {
SecretKeySpec key = new SecretKeySpec(sessionKey.getData(), "AES"); SecretKeySpec key = new SecretKeySpec(sessionKey.getData(), "AES");
IvParameterSpec ivps = new IvParameterSpec(iv, ivOffset, 16); IvParameterSpec ivps = new IvParameterSpec(iv, ivOffset, 16);
@@ -181,7 +183,7 @@ public class CryptixAESEngine extends AESEngine {
return ; return ;
} }
if (USE_SYSTEM_AES) { if (USE_SYSTEM_AES && length >= MIN_SYSTEM_AES_LENGTH) {
try { try {
SecretKeySpec key = new SecretKeySpec(sessionKey.getData(), "AES"); SecretKeySpec key = new SecretKeySpec(sessionKey.getData(), "AES");
IvParameterSpec ivps = new IvParameterSpec(iv, ivOffset, 16); IvParameterSpec ivps = new IvParameterSpec(iv, ivOffset, 16);
@@ -292,6 +294,8 @@ public class CryptixAESEngine extends AESEngine {
* And we can't get rid of Cryptix because AES-256 is unavailable * And we can't get rid of Cryptix because AES-256 is unavailable
* in several JVMs. * in several JVMs.
* Make USE_SYSTEM_AES above non-final to run this. * Make USE_SYSTEM_AES above non-final to run this.
* You also must comment out the length check in encrypt() and decrypt() above.
*
*<pre> *<pre>
* JVM Cryptix (ms) System (ms) * JVM Cryptix (ms) System (ms)
* Sun 8662 n/a * Sun 8662 n/a
@@ -303,14 +307,14 @@ public class CryptixAESEngine extends AESEngine {
*</pre> *</pre>
* *
* Speed ups with AES-NI: * Speed ups with AES-NI:
* May 2014 AMD Hexcore 100K runs: * May 2014 AMD Hexcore 100K runs (1024 bytes):
*<pre> *<pre>
* JVM Cryptix (ms) System (ms) * JVM Cryptix (ms) System (ms)
* OpenJDK 6 3314 5030 * OpenJDK 6 3314 5030
* OpenJDK 7 3285 2476 * OpenJDK 7 3285 2476
*</pre> *</pre>
* *
* * Cryptix is faster for data smaller than 704 bytes.
*/ */
/******* /*******
public static void main(String args[]) { public static void main(String args[]) {
@@ -325,23 +329,31 @@ public class CryptixAESEngine extends AESEngine {
if (canTestSystem) { if (canTestSystem) {
System.out.println("Start Cryptix vs. System verification run of " + MATCH_RUNS); System.out.println("Start Cryptix vs. System verification run of " + MATCH_RUNS);
for (int i = 0; i < MATCH_RUNS; i++) { for (int i = 0; i < MATCH_RUNS; i++) {
testED(ctx, false, true); testED(ctx, false, true, 1024);
testED(ctx, true, false); testED(ctx, true, false, 1024);
} }
} }
System.out.println("Start Cryptix run of " + TIMING_RUNS); for (int len = 512; len <= 1024; len += 32) {
long start = System.currentTimeMillis(); System.out.println("Start Cryptix run of " + TIMING_RUNS + " length " + len);
for (int i = 0; i < TIMING_RUNS; i++) { long start = System.currentTimeMillis();
testED(ctx, false, false);
}
System.out.println("Cryptix took " + (System.currentTimeMillis() - start));
if (canTestSystem) {
System.out.println("Start System run of " + TIMING_RUNS);
start = System.currentTimeMillis();
for (int i = 0; i < TIMING_RUNS; i++) { for (int i = 0; i < TIMING_RUNS; i++) {
testED(ctx, true, true); testED(ctx, false, false, len);
}
long cryptix = System.currentTimeMillis() - start;
System.out.println("Cryptix took " + cryptix);
if (canTestSystem) {
System.out.println("Start System run of " + TIMING_RUNS + " length " + len);
start = System.currentTimeMillis();
for (int i = 0; i < TIMING_RUNS; i++) {
testED(ctx, true, true, len);
}
long system = System.currentTimeMillis() - start;
System.out.println("System took " + system);
if (system < cryptix)
System.out.println("***System is " + (100 - (system * 100 / cryptix)) + "% better");
else
System.out.println("***System is " + ((system * 100 / cryptix) - 100) + "% worse");
} }
System.out.println("System took " + (System.currentTimeMillis() - start));
} }
//testFake(ctx); //testFake(ctx);
//testNull(ctx); //testNull(ctx);
@@ -356,16 +368,16 @@ public class CryptixAESEngine extends AESEngine {
private static byte[] _encrypted = new byte[1024]; private static byte[] _encrypted = new byte[1024];
private static byte[] _decrypted = new byte[1024]; private static byte[] _decrypted = new byte[1024];
private static void testED(I2PAppContext ctx, boolean systemEnc, boolean systemDec) { private static void testED(I2PAppContext ctx, boolean systemEnc, boolean systemDec, int len) {
SessionKey key = ctx.keyGenerator().generateSessionKey(); SessionKey key = ctx.keyGenerator().generateSessionKey();
ctx.random().nextBytes(_iv); ctx.random().nextBytes(_iv);
ctx.random().nextBytes(_orig); ctx.random().nextBytes(_orig);
CryptixAESEngine aes = new CryptixAESEngine(ctx); CryptixAESEngine aes = new CryptixAESEngine(ctx);
USE_SYSTEM_AES = systemEnc; USE_SYSTEM_AES = systemEnc;
aes.encrypt(_orig, 0, _encrypted, 0, key, _iv, _orig.length); aes.encrypt(_orig, 0, _encrypted, 0, key, _iv, len);
USE_SYSTEM_AES = systemDec; USE_SYSTEM_AES = systemDec;
aes.decrypt(_encrypted, 0, _decrypted, 0, key, _iv, _encrypted.length); aes.decrypt(_encrypted, 0, _decrypted, 0, key, _iv, len);
if (!DataHelper.eq(_decrypted,_orig)) if (!DataHelper.eq(_decrypted, 0, _orig, 0, len))
throw new RuntimeException("full D(E(orig)) != orig"); throw new RuntimeException("full D(E(orig)) != orig");
//else //else
// System.out.println("full D(E(orig)) == orig"); // System.out.println("full D(E(orig)) == orig");