Skip to content

Commit

Permalink
added static key and iv size
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Tjaden committed Jan 4, 2025
1 parent 2db9e31 commit a59ab1b
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions examples/provider/CryptoBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
Expand All @@ -13,20 +14,38 @@

public class CryptoBenchmark {
/* Constants for benchmark configuration */
private static final int WARMUP_ITERATIONS = 1000;
private static final int WARMUP_ITERATIONS = 5;
private static final int TEST_ITERATIONS = 5; /* Number of iterations */
private static final int DATA_SIZE = 1024 * 1024;
private static final int AES_BLOCK_SIZE = 16;
private static final int GCM_TAG_LENGTH = 128; /* GCM auth tag length in bits */
private static final int AES_KEY_SIZE = 256;

/* Static key and IV buffers */
private static final byte[] STATIC_KEY = new byte[] {
(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef,
(byte)0xfe, (byte)0xde, (byte)0xba, (byte)0x98,
(byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10,
(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef,
(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
(byte)0xf0, (byte)0xf1, (byte)0xf2, (byte)0xf3,
(byte)0xf4, (byte)0xf5, (byte)0xf6, (byte)0xf7
};

private static final byte[] STATIC_IV = new byte[] {
(byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
(byte)0x90, (byte)0xab, (byte)0xcd, (byte)0xef,
(byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01,
(byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01
};

private static byte[] generateTestData(int size) {
return new byte[size]; /* Creates array initialized with zeros */
}

private static void runBenchmark(String algorithm, String mode) throws Exception {
/* Key generation variables */
KeyGenerator keyGen;
SecretKey key;

/* IV/Nonce generation variables */
Expand All @@ -51,14 +70,11 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
double encryptTimeMS;
double decryptTimeMS;

/* Generate a random key */
keyGen = KeyGenerator.getInstance("AES");
keyGen.init(AES_KEY_SIZE);
key = keyGen.generateKey();
/* Use static pre-made key */
key = new SecretKeySpec(STATIC_KEY, "AES");

/* Generate IV/Nonce and parameters based on mode */
ivBytes = new byte[AES_BLOCK_SIZE];
new SecureRandom().nextBytes(ivBytes);
/* Use static pre-made IV */
ivBytes = STATIC_IV;
if (mode.equals("GCM")) {
params = new GCMParameterSpec(GCM_TAG_LENGTH, ivBytes);
} else {
Expand Down Expand Up @@ -89,6 +105,20 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
endTime = System.nanoTime();
encryptTime = (endTime - startTime) / TEST_ITERATIONS;

/* Calculate data size in MiB */
dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);

/* Calculate time in milliseconds */
encryptTimeMS = encryptTime / 1000000.0;

/* Calculate throughput using seconds for MiB/s */
encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024.0 * 1024.0);

/* Print encryption results immediately */
String testName = "AES-256-" + mode;
System.out.printf("%s-enc %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
testName, dataSizeMiB, encryptTimeMS, encryptThroughput);

/* Benchmark decryption using the encrypted data from encryption benchmark */
startTime = System.nanoTime();
for (int i = 0; i < TEST_ITERATIONS; i++) {
Expand All @@ -98,21 +128,13 @@ private static void runBenchmark(String algorithm, String mode) throws Exception
endTime = System.nanoTime();
decryptTime = (endTime - startTime) / TEST_ITERATIONS;

/* Calculate data size in MiB */
dataSizeMiB = DATA_SIZE / (1024.0 * 1024.0);

/* Calculate time in milliseconds */
encryptTimeMS = encryptTime / 1000000.0;
decryptTimeMS = decryptTime / 1000000.0;

/* Calculate throughput using seconds for MiB/s */
encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024.0 * 1024.0);
decryptThroughput = (DATA_SIZE / (decryptTime / 1000000000.0)) / (1024.0 * 1024.0);

/* Print results */
String testName = "AES-256-" + mode;
System.out.printf("%s-enc %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
testName, dataSizeMiB, encryptTimeMS, encryptThroughput);
/* Print decryption results immediately */
System.out.printf("%s-dec %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
testName, dataSizeMiB, decryptTimeMS, decryptThroughput);
}
Expand Down

0 comments on commit a59ab1b

Please sign in to comment.