Skip to content

Commit 6ffb703

Browse files
authored
Merge pull request #89 from jackctj117/wolfJSSE_Benchmark
Adds AES-GCM to benchmark
2 parents d34bea0 + 8ddb01f commit 6ffb703

File tree

1 file changed

+86
-70
lines changed

1 file changed

+86
-70
lines changed

examples/provider/CryptoBenchmark.java

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,61 @@
11
import javax.crypto.Cipher;
22
import javax.crypto.KeyGenerator;
33
import javax.crypto.SecretKey;
4+
import javax.crypto.spec.GCMParameterSpec;
45
import javax.crypto.spec.IvParameterSpec;
6+
import javax.crypto.spec.SecretKeySpec;
57
import java.security.Provider;
68
import java.security.SecureRandom;
79
import java.security.Security;
10+
import java.security.spec.AlgorithmParameterSpec;
811
import java.util.Arrays;
912

1013
import com.wolfssl.provider.jce.WolfCryptProvider;
1114

1215
public class CryptoBenchmark {
1316
/* Constants for benchmark configuration */
14-
private static final int WARMUP_ITERATIONS = 1000;
15-
private static final int TEST_ITERATIONS = 10000;
16-
private static final int DATA_SIZE = 16384; /* 16KB of data */
17+
private static final int WARMUP_ITERATIONS = 5;
18+
private static final int TEST_ITERATIONS = 5; /* Number of iterations */
19+
private static final int DATA_SIZE = 1024 * 1024;
1720
private static final int AES_BLOCK_SIZE = 16;
21+
private static final int GCM_TAG_LENGTH = 128; /* GCM auth tag length in bits */
1822
private static final int AES_KEY_SIZE = 256;
1923

20-
private static byte[] generateRandomData(int size) {
21-
byte[] data = new byte[size];
22-
new SecureRandom().nextBytes(data);
23-
return data;
24+
/* Static key and IV buffers */
25+
private static final byte[] STATIC_KEY = new byte[] {
26+
(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
27+
(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef,
28+
(byte)0xfe, (byte)0xde, (byte)0xba, (byte)0x98,
29+
(byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10,
30+
(byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef,
31+
(byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
32+
(byte)0xf0, (byte)0xf1, (byte)0xf2, (byte)0xf3,
33+
(byte)0xf4, (byte)0xf5, (byte)0xf6, (byte)0xf7
34+
};
35+
36+
private static final byte[] STATIC_IV = new byte[] {
37+
(byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
38+
(byte)0x90, (byte)0xab, (byte)0xcd, (byte)0xef,
39+
(byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01,
40+
(byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01
41+
};
42+
43+
private static byte[] generateTestData(int size) {
44+
return new byte[size]; /* Creates array initialized with zeros */
2445
}
2546

26-
/*
27-
* Benchmarks Cipher class operations using AES-CBC
28-
* Returns array containing encrypt and decrypt times in nanoseconds
29-
*/
30-
private static void benchmarkCipher() throws Exception {
47+
private static void runBenchmark(String algorithm, String mode) throws Exception {
3148
/* Key generation variables */
32-
KeyGenerator keyGen;
3349
SecretKey key;
3450

35-
/* IV generation variables */
51+
/* IV/Nonce generation variables */
3652
byte[] ivBytes;
37-
IvParameterSpec iv;
38-
53+
/* Using specific type instead of Object */
54+
AlgorithmParameterSpec params;
3955
/* Test data variables */
4056
byte[] testData;
41-
byte[] encryptedData;
42-
byte[] encrypted;
57+
byte[] encryptedData = null;
58+
double dataSizeMiB;
4359

4460
/* Cipher variables */
4561
Cipher cipher;
@@ -51,90 +67,90 @@ private static void benchmarkCipher() throws Exception {
5167
long decryptTime;
5268
double encryptThroughput;
5369
double decryptThroughput;
54-
55-
/* Provider info */
56-
Provider provider;
70+
double encryptTimeMS;
71+
double decryptTimeMS;
5772

58-
/* Generate a random key and IV */
59-
keyGen = KeyGenerator.getInstance("AES");
60-
keyGen.init(AES_KEY_SIZE);
61-
key = keyGen.generateKey();
73+
/* Use static pre-made key */
74+
key = new SecretKeySpec(STATIC_KEY, "AES");
6275

63-
ivBytes = new byte[AES_BLOCK_SIZE];
64-
new SecureRandom().nextBytes(ivBytes);
65-
iv = new IvParameterSpec(ivBytes);
76+
/* Use static pre-made IV */
77+
ivBytes = STATIC_IV;
78+
if (mode.equals("GCM")) {
79+
params = new GCMParameterSpec(GCM_TAG_LENGTH, ivBytes);
80+
} else {
81+
params = new IvParameterSpec(ivBytes);
82+
}
6683

67-
/* Generate random test data */
68-
testData = generateRandomData(DATA_SIZE);
84+
/* Generate test data filled with zeros */
85+
testData = generateTestData(DATA_SIZE);
6986

70-
/* Initialize cipher for warmup */
71-
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
87+
/* Initialize cipher */
88+
cipher = Cipher.getInstance(algorithm);
7289

7390
/* Warm up phase */
74-
System.out.println("Warming up...");
7591
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
76-
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
77-
encrypted = cipher.doFinal(testData);
92+
cipher.init(Cipher.ENCRYPT_MODE, key, params);
93+
encryptedData = cipher.doFinal(testData);
7894

79-
cipher.init(Cipher.DECRYPT_MODE, key, iv);
80-
cipher.doFinal(encrypted);
95+
cipher.init(Cipher.DECRYPT_MODE, key, params);
96+
cipher.doFinal(encryptedData);
8197
}
8298

83-
System.out.println("\nBenchmarking AES-CBC (" + AES_KEY_SIZE + "-bit key) with " +
84-
DATA_SIZE + " bytes:");
85-
System.out.println("Iterations per test: " + TEST_ITERATIONS);
86-
8799
/* Benchmark encryption */
88100
startTime = System.nanoTime();
89101
for (int i = 0; i < TEST_ITERATIONS; i++) {
90-
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
91-
cipher.doFinal(testData);
102+
cipher.init(Cipher.ENCRYPT_MODE, key, params);
103+
encryptedData = cipher.doFinal(testData);
92104
}
93105
endTime = System.nanoTime();
94106
encryptTime = (endTime - startTime) / TEST_ITERATIONS;
95107

96-
/* Benchmark decryption */
97-
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
98-
encryptedData = cipher.doFinal(testData);
99-
108+
/* Calculate data size in MiB */
109+
dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);
110+
111+
/* Calculate time in milliseconds */
112+
encryptTimeMS = encryptTime / 1000000.0;
113+
114+
/* Calculate throughput using seconds for MiB/s */
115+
encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024.0 * 1024.0);
116+
117+
/* Print encryption results immediately */
118+
String testName = "AES-256-" + mode;
119+
System.out.printf("%s-enc %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
120+
testName, dataSizeMiB, encryptTimeMS, encryptThroughput);
121+
122+
/* Benchmark decryption using the encrypted data from encryption benchmark */
100123
startTime = System.nanoTime();
101124
for (int i = 0; i < TEST_ITERATIONS; i++) {
102-
cipher.init(Cipher.DECRYPT_MODE, key, iv);
125+
cipher.init(Cipher.DECRYPT_MODE, key, params);
103126
cipher.doFinal(encryptedData);
104127
}
105128
endTime = System.nanoTime();
106129
decryptTime = (endTime - startTime) / TEST_ITERATIONS;
107130

108-
/* Print results */
109-
System.out.println("\nResults (average time per operation):");
110-
System.out.println(" Encryption: " + encryptTime + " ns (" +
111-
String.format("%.2f", encryptTime/1000000.0) + " ms)");
112-
System.out.println(" Decryption: " + decryptTime + " ns (" +
113-
String.format("%.2f", decryptTime/1000000.0) + " ms)");
131+
/* Calculate time in milliseconds */
132+
decryptTimeMS = decryptTime / 1000000.0;
114133

115-
/* Calculate and print throughput */
116-
encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024 * 1024);
117-
decryptThroughput = (DATA_SIZE / (decryptTime / 1000000000.0)) / (1024 * 1024);
118-
119-
System.out.println("\nThroughput:");
120-
System.out.println(" Encryption: " + String.format("%.2f", encryptThroughput) + " MB/s");
121-
System.out.println(" Decryption: " + String.format("%.2f", decryptThroughput) + " MB/s");
122-
123-
/* Print provider information */
124-
provider = Security.getProvider(cipher.getProvider().getName());
125-
System.out.println("\nProvider Information:");
126-
System.out.println(" Name: " + provider.getName());
127-
System.out.println(" Version: " + provider.getVersion());
128-
System.out.println(" Info: " + provider.getInfo());
134+
/* Calculate throughput using seconds for MiB/s */
135+
decryptThroughput = (DATA_SIZE / (decryptTime / 1000000000.0)) / (1024.0 * 1024.0);
136+
137+
/* Print decryption results immediately */
138+
System.out.printf("%s-dec %4.2f MiB took %1.3f ms, %8.3f MiB/s%n",
139+
testName, dataSizeMiB, decryptTimeMS, decryptThroughput);
129140
}
130141

131142
public static void main(String[] args) {
132143
try {
133144
/* Register wolfJCE as the default provider */
134145
Security.insertProviderAt(new WolfCryptProvider(), 1);
135146

136-
/* Run Cipher benchmark */
137-
benchmarkCipher();
147+
System.out.println("------------------------------------------------------------------------------");
148+
System.out.println(" JCE Crypto Benchmark");
149+
System.out.println("------------------------------------------------------------------------------");
150+
151+
/* Run benchmarks for different algorithms */
152+
runBenchmark("AES/CBC/PKCS5Padding", "CBC");
153+
runBenchmark("AES/GCM/NoPadding", "GCM");
138154

139155
} catch (Exception e) {
140156
System.err.println("Benchmark failed: " + e.getMessage());

0 commit comments

Comments
 (0)