JCE: Implements KeyGen Benchamrk

pull/115/head
Jack Tjaden 2025-06-10 10:31:33 -06:00
parent d6eb404f7c
commit 2039cbb9d1
1 changed files with 200 additions and 97 deletions

View File

@ -203,7 +203,7 @@ public class CryptoBenchmark {
if (wolfJCEAlgorithms.contains(normalized)) {
normalizedAlgorithms.add(algorithm);
}
} else if (serviceType.equals("Mac")) {
} else if (serviceType.equals("Mac") || serviceType.equals("KeyGenerator")) {
String normalized = algorithm;
if (wolfJCEAlgorithms.contains(normalized)) {
@ -1287,6 +1287,95 @@ public class CryptoBenchmark {
return getWolfJCEAlgorithmsForService("Signature");
}
/* KeyGenerator benchmark */
/* KeyGenerator benchmark */
private static void runKeyGeneratorBenchmark(String algorithm, String providerName) throws Exception {
KeyGenerator keyGen;
int ops = 0;
long startTime;
double elapsedTime;
/* Initialize KeyGenerator with specific provider */
keyGen = KeyGenerator.getInstance(algorithm, providerName);
/* Set appropriate key size based on algorithm */
if (algorithm.equals("AES")) {
keyGen.init(256);
} else if (algorithm.equals("DES")) {
keyGen.init(56);
} else if (algorithm.equals("DESede")) {
keyGen.init(168);
} else if (algorithm.equals("RSA")) {
keyGen.init(2048);
} else if (algorithm.startsWith("Hmac") || algorithm.startsWith("HMAC")) {
try {
int keySize = getHmacKeySize(algorithm) * 8;
keyGen.init(keySize);
} catch (Exception e) {
}
} else {
try {
keyGen.generateKey();
keyGen = KeyGenerator.getInstance(algorithm, providerName);
} catch (Exception e) {
throw new IllegalArgumentException("Unsupported algorithm or unable to determine key size: " + algorithm);
}
}
/* Warm up phase */
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
keyGen.generateKey();
}
/* Benchmark phase */
startTime = System.nanoTime();
elapsedTime = 0;
do {
keyGen.generateKey();
ops++;
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
double opsPerSec = ops / elapsedTime;
System.out.printf(" %-40s %8d ops took %.3f sec, %8.3f ops/sec%n",
algorithm + " (" + providerName + ")", ops, elapsedTime, opsPerSec);
/* Store result */
results.add(new BenchmarkResult(providerName, algorithm + " keygen", opsPerSec));
}
/* Get KeyGenerator algorithms for a specific provider */
private static Set<String> getKeyGeneratorAlgorithmsForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
return getAlgorithmsForProvider(providerName, "KeyGenerator", wolfJCEAlgorithms);
}
/* KeyGenerator benchmark runner */
private static void runKeyGeneratorBenchmarksForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
System.out.println("\n" + providerName + ":");
Set<String> supportedAlgorithms;
if (providerName.equals("wolfJCE")) {
supportedAlgorithms = wolfJCEAlgorithms;
} else {
supportedAlgorithms = getKeyGeneratorAlgorithmsForProvider(providerName, wolfJCEAlgorithms);
}
if (supportedAlgorithms.isEmpty()) {
System.out.println(" No common KeyGenerator algorithms found for provider " + providerName);
return;
}
for (String algorithm : supportedAlgorithms) {
try {
runKeyGeneratorBenchmark(algorithm, providerName);
} catch (Exception e) {
System.out.printf(" %-40s Error: %s%n",
algorithm + " (" + providerName + ")", e.getMessage());
}
}
}
public static void main(String[] args) {
try {
/* Check if Bouncy Castle is available */
@ -1482,6 +1571,20 @@ public class CryptoBenchmark {
runSignatureBenchmarksForProvider(provider.getName(), wolfJCEAlgorithms);
}
/* Run KeyGenerator benchmarks with clean provider setup */
System.out.println("\n-----------------------------------------------------------------------------");
System.out.println("KeyGenerator Benchmark Results");
System.out.println("-----------------------------------------------------------------------------");
/* First, set up wolfJCE provider to get its algorithm list */
setupProvidersForTest(providers[0]);
Set<String> wolfJCEKeyGenAlgorithms = getWolfJCEAlgorithmsForService("KeyGenerator");
for (Provider provider : providers) {
setupProvidersForTest(provider);
runKeyGeneratorBenchmarksForProvider(provider.getName(), wolfJCEKeyGenAlgorithms);
}
System.out.println("-----------------------------------------------------------------------------\n");
/* Print delta table */