[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge pull request adoptium#9 from jerrylui803/master
Browse files Browse the repository at this point in the history
Adding/updating crypto test benchmarks
  • Loading branch information
Simon Hirst authored Aug 20, 2018
2 parents 3b036b1 + 6a2c2f7 commit d409bf9
Show file tree
Hide file tree
Showing 9 changed files with 751 additions and 87 deletions.
137 changes: 80 additions & 57 deletions net/adoptopenjdk/bumblebench/crypto/CipherBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,94 +17,117 @@
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Random;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import net.adoptopenjdk.bumblebench.core.MicroBench;

public final class CipherBench extends MicroBench {

static final SecretKeySpec skey = new SecretKeySpec(new byte[] { -80, -103, -1, 68, -29, -94, 61, -52, 93, -59, -128, 105, 110, 88, 44, 105 }, "AES");
static final SecretKeySpec skeydesede = new SecretKeySpec(new byte[] { -80, -103, -1, 68, -29, -94, 61, -52, 93, -59, -128, 105, 110, 88, 44, 105, 29, -94, 61, -52, 93, -59,
-128, 105 }, "DESede");
static final SecretKeySpec skeydes = new SecretKeySpec(new byte[] { 29, -94, 61, -52, 93, -59, -128, 105 }, "DES");

static final SecretKeySpec keyArr[] = { skeydes, skeydesede, skey };

static final byte[] iv = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static final byte[] ivdes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static final byte[][] ivArr = { ivdes, ivdes, iv };
// 128 bits
static final SecretKeySpec skey_128 = new SecretKeySpec(new byte[] { -80, -103, -1, 68, -29, -94, 61, -52, 93, -59, -128, 105, 110, 88, 44, 105 }, "AES");
// 256 bits
static final SecretKeySpec skey_256 = new SecretKeySpec(new byte[] { -80, -103, -1, 68, -29, -94, 61, -52, 93, -59, -128, 105, 110, 88, 44, 105, -80, -103, -1, 68, -29, -94, 61, -52, 93, -59, -128, 105, 110, 88, 44, 105 }, "AES");
static final SecretKeySpec skey;

// 128 bits
static final byte[] iv;
static final int len;
static final String alg;
static final String mode;
static final int algIndex;
static final byte[] data;

static Cipher ciphera;
static Cipher cipherb;
static final byte[] out;
static final int modeInt;
// 128 bit
static Cipher cipher;
static {

//
len = option("payload", 4096);
alg = option("algorithm", "AES");
mode = option("mode", "CBC");
if (alg.equals("DES")) {
algIndex = 0;
} else if (alg.equals("DESede")) {
algIndex = 1;
} else if (alg.equals("AES")) {
algIndex = 2;
} else {
algIndex = -1;
}
String algorithm = option("algorithm", "AES-128-CBC");

data = new byte[len];
out = new byte[len];
iv = new byte[16];
Random r = new Random(10);
r.nextBytes(data);
r.nextBytes(iv);

try {
ciphera = Cipher.getInstance(alg + '/' + mode + "/PKCS5Padding");
cipherb = Cipher.getInstance(alg + '/' + mode + "/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
String mode = option("mode", "encrypt");
if (mode.equals("encrypt")) {
modeInt = 1;
} else if (mode.equals("decrypt")) {
modeInt = 0;
} else {
throw new RuntimeException("Unsupported mode");
}

}
if (algorithm.contains("128")) {
skey = skey_128;
} else if (algorithm.contains("256")) {
skey = skey_256;
} else {
skey = null;
throw new RuntimeException("Unsupported key size");
}

protected long doBatch(long numIterations) throws InterruptedException {
IvParameterSpec iviv = new IvParameterSpec(ivArr[algIndex]);
String cipherMode;
if (algorithm.contains("CBC")) {
cipherMode = "AES/CBC/NoPadding";
} else if (algorithm.contains("CTR")) {
cipherMode = "AES/CTR/NoPadding";
} else {
throw new RuntimeException("Only CBC and CTR cipher modes available");
}

String provider = option("provider_name", "");
try {
ciphera.init(Cipher.ENCRYPT_MODE, keyArr[algIndex], iviv);
cipherb.init(Cipher.DECRYPT_MODE, keyArr[algIndex], iviv);

for (int i = 0; i < numIterations; i++) {
byte[] out11 = ciphera.doFinal(data);
byte[] out2 = cipherb.doFinal(out11);
if (provider.equals("")) {
cipher = Cipher.getInstance(cipherMode);
} else { if (provider.equals("IBMJCEPlus")) {
java.security.Provider java_provider = java.security.Security.getProvider("IBMJCEPlus");
if( java_provider == null ) {
java_provider = (java.security.Provider)Class.forName("com.ibm.crypto.plus.provider.IBMJCEPlus").newInstance();
java.security.Security.insertProviderAt( java_provider, 1 );
}
cipher = Cipher.getInstance(cipherMode, java_provider);

} else {
cipher = Cipher.getInstance(cipherMode, provider);
}
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
}

System.out.println("Using Provider " + cipher.getProvider().getName());
System.out.println("Payload size: "+ data.length + " bytes");
AlgorithmParameterSpec iviv = new IvParameterSpec(iv);
if (modeInt == 0) {
cipher.init(Cipher.DECRYPT_MODE, skey, iviv);
} else {
cipher.init(Cipher.ENCRYPT_MODE, skey, iviv);
}

} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return numIterations;
}

protected long doBatch(long numBytes) throws InterruptedException {
long numIterations = java.lang.Math.round((double)numBytes/data.length)+1;

for (long i = 0; i < numIterations; i++) {
try {
cipher.update(data, 0, data.length, out);
} catch(Exception e) {
System.exit(1);
}
}
return numIterations*data.length;
}
}
56 changes: 28 additions & 28 deletions net/adoptopenjdk/bumblebench/crypto/DigestBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,48 @@

public final class DigestBench extends MicroBench {

static final int payload = option("payload", 4096);
static final String alg = option("algorithm", "SHA");
static final int len = option("payload", 4096);
static final String alg = option("algorithm", "SHA-256");

static final byte[] data = new byte[payload];
static final byte[] data;
static MessageDigest md;
static MessageDigest mdVerify;

static {
data = new byte[len];
Random r = new Random(10);
r.nextBytes(data);

String provider = option("provider_name", "");
try {
md = MessageDigest.getInstance(alg);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
if (provider.equals("")) {
md = MessageDigest.getInstance(alg);
} else {
if (provider.equals("IBMJCEPlus")) {
java.security.Provider java_provider = java.security.Security.getProvider("IBMJCEPlus");
if( java_provider == null ) {
java_provider = (java.security.Provider)Class.forName("com.ibm.crypto.plus.provider.IBMJCEPlus").newInstance();
java.security.Security.insertProviderAt( java_provider, 1 );
}
md = MessageDigest.getInstance(alg, java_provider);
} else {
md = MessageDigest.getInstance(alg, provider);
}
}
System.out.println("Using Provider " + md.getProvider().getName());
System.out.println("Payload size: "+ data.length + " bytes");
} catch (Exception e) {
e.printStackTrace();
}

try {
mdVerify = MessageDigest.getInstance("S"+alg);
} catch (NoSuchAlgorithmException e) {
System.out.println("Could not find verifying algorithm S"+alg);
}
}

protected long doBatch(long numIterations) throws InterruptedException {
for (int inner = 0; inner < numIterations; inner++) {
protected long doBatch(long numBytes) throws InterruptedException {

long numIterations = java.lang.Math.round((double)numBytes/data.length);
for (long i = 0; i < numIterations; i++) {
md.reset();
md.update(data);
byte[] result = md.digest();
if (mdVerify!=null) {
mdVerify.reset();
mdVerify.update(data);
byte[] expected = mdVerify.digest();
if (!Arrays.equals(expected, result)) {
throw new RuntimeException("Wrong SHA\nExpected: " + DatatypeConverter.printHexBinary(expected) + "\nActual: " + DatatypeConverter.printHexBinary(result));
} else {
//System.err.print('.');
}
}
byte[] result = md.digest(data);
}
return numIterations;
return numIterations*data.length;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class EllipticCurveBench extends MicroBench {

protected long doBatch(long numIterations) throws InterruptedException {
try {
for (int i = 0; i < numIterations; i++) {
for (long i = 0; i < numIterations; i++) {
keyPairGeneratora.initialize(new ECGenParameterSpec(curve), null);
keyPairGeneratorb.initialize(new ECGenParameterSpec(curve), null);

Expand Down
Loading

0 comments on commit d409bf9

Please sign in to comment.