-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic crypto benchmark app and script for wolfJSSE
- Loading branch information
Jack Tjaden
committed
Dec 23, 2024
1 parent
d063bb6
commit 11bef90
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import javax.crypto.Cipher; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import java.security.Provider; | ||
import java.security.SecureRandom; | ||
import java.security.Security; | ||
import java.util.Arrays; | ||
|
||
import com.wolfssl.provider.jsse.WolfSSLProvider; | ||
|
||
public class CryptoBenchmark { | ||
private static final int WARMUP_ITERATIONS = 1000; | ||
private static final int TEST_ITERATIONS = 10000; | ||
private static final int DATA_SIZE = 16384; // 16KB of data | ||
|
||
private static byte[] generateRandomData(int size) { | ||
byte[] data = new byte[size]; | ||
new SecureRandom().nextBytes(data); | ||
return data; | ||
} | ||
|
||
private static long benchmarkOperation(String operation, byte[] data, | ||
SecretKey key, IvParameterSpec iv) throws Exception { | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
|
||
long startTime = System.nanoTime(); | ||
|
||
for (int i = 0; i < TEST_ITERATIONS; i++) { | ||
if ("encrypt".equals(operation)) { | ||
cipher.init(Cipher.ENCRYPT_MODE, key, iv); | ||
cipher.doFinal(data); | ||
} else { | ||
cipher.init(Cipher.DECRYPT_MODE, key, iv); | ||
cipher.doFinal(data); | ||
} | ||
} | ||
|
||
long endTime = System.nanoTime(); | ||
return (endTime - startTime) / TEST_ITERATIONS; // Average time per operation | ||
} | ||
|
||
private static void warmup(byte[] data, SecretKey key, | ||
IvParameterSpec iv) throws Exception { | ||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
|
||
for (int i = 0; i < WARMUP_ITERATIONS; i++) { | ||
cipher.init(Cipher.ENCRYPT_MODE, key, iv); | ||
byte[] encrypted = cipher.doFinal(data); | ||
|
||
cipher.init(Cipher.DECRYPT_MODE, key, iv); | ||
cipher.doFinal(encrypted); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
try { | ||
// Register wolfJCE as the default provider | ||
Security.insertProviderAt(new WolfSSLProvider(), 1); | ||
|
||
// Generate a random key and IV | ||
KeyGenerator keyGen = KeyGenerator.getInstance("AES"); | ||
keyGen.init(256); | ||
SecretKey key = keyGen.generateKey(); | ||
|
||
byte[] ivBytes = new byte[16]; | ||
new SecureRandom().nextBytes(ivBytes); | ||
IvParameterSpec iv = new IvParameterSpec(ivBytes); | ||
|
||
// Generate random test data | ||
byte[] testData = generateRandomData(DATA_SIZE); | ||
|
||
// Warm up | ||
System.out.println("Warming up..."); | ||
warmup(testData, key, iv); | ||
|
||
System.out.println("\nBenchmarking AES-CBC (256-bit key) with " + DATA_SIZE + " bytes:"); | ||
System.out.println("Iterations per test: " + TEST_ITERATIONS); | ||
|
||
// Benchmark operations | ||
long encryptTime = benchmarkOperation("encrypt", testData, key, iv); | ||
long decryptTime = benchmarkOperation("decrypt", testData, key, iv); | ||
|
||
// Print results | ||
System.out.println("\nResults (average time per operation):"); | ||
System.out.println(" Encryption: " + encryptTime + " ns (" + | ||
String.format("%.2f", encryptTime/1000000.0) + " ms)"); | ||
System.out.println(" Decryption: " + decryptTime + " ns (" + | ||
String.format("%.2f", decryptTime/1000000.0) + " ms)"); | ||
|
||
// Print throughput | ||
double encryptThroughput = (DATA_SIZE / (encryptTime / 1000000000.0)) / (1024 * 1024); | ||
double decryptThroughput = (DATA_SIZE / (decryptTime / 1000000000.0)) / (1024 * 1024); | ||
|
||
System.out.println("\nThroughput:"); | ||
System.out.println(" Encryption: " + String.format("%.2f", encryptThroughput) + " MB/s"); | ||
System.out.println(" Decryption: " + String.format("%.2f", decryptThroughput) + " MB/s"); | ||
|
||
// Print provider information | ||
Provider provider = Security.getProvider(cipher.getProvider().getName()); | ||
System.out.println("\nProvider Information:"); | ||
System.out.println(" Name: " + provider.getName()); | ||
System.out.println(" Version: " + provider.getVersionStr()); | ||
System.out.println(" Info: " + provider.getInfo()); | ||
|
||
} catch (Exception e) { | ||
System.err.println("Benchmark failed: " + e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
|
||
# Check if JAVA_HOME is set | ||
if [ -z "$JAVA_HOME" ]; then | ||
echo "Error: JAVA_HOME is not set" | ||
exit 1 | ||
fi | ||
|
||
# Required JAR files - update these paths as needed | ||
WOLFJSSE_JAR="wolfssl-jsse-3.0.0.jar" | ||
|
||
# Check if required JAR files exist | ||
if [ ! -f "$WOLFJSSE_JAR" ]; then | ||
echo "Error: wolfJSSE JAR file not found: $WOLFJSSE_JAR" | ||
exit 1 | ||
fi | ||
|
||
# Compile the benchmark | ||
echo "Compiling CryptoBenchmark.java..." | ||
"$JAVA_HOME/bin/javac" -cp ".:$WOLFJSSE_JAR" CryptoBenchmark.java | ||
|
||
if [ $? -ne 0 ]; then | ||
echo "Compilation failed" | ||
exit 1 | ||
fi | ||
|
||
# Run the benchmark | ||
echo "Running benchmark..." | ||
"$JAVA_HOME/bin/java" -cp ".:$WOLFJSSE_JAR" CryptoBenchmark | ||
|
||
# Clean up | ||
echo "Cleaning up..." | ||
rm -f CryptoBenchmark.class |