Skip to content

Commit 60fd421

Browse files
committed
Refactored HMAC benchamrks w/ dynamic algorithm search, MD results int overflow fix
1 parent a7b6f82 commit 60fd421

1 file changed

Lines changed: 84 additions & 99 deletions

File tree

examples/provider/CryptoBenchmark.java

Lines changed: 84 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,32 @@ private static int getHmacKeySize(String algorithm) {
9393
return 16;
9494
case "HmacSHA1":
9595
return 20;
96+
case "HmacSHA224":
97+
return 28;
9698
case "HmacSHA256":
9799
return 32;
98100
case "HmacSHA384":
99101
return 48;
100102
case "HmacSHA512":
101103
return 64;
104+
case "HmacSHA3-224":
105+
return 28;
106+
case "HmacSHA3-256":
107+
return 32;
108+
case "HmacSHA3-384":
109+
return 48;
110+
case "HmacSHA3-512":
111+
return 64;
102112
default:
103-
throw new IllegalArgumentException("Unsupported HMAC algorithm: " + algorithm);
113+
if (algorithm.contains("224")) return 28;
114+
if (algorithm.contains("256")) return 32;
115+
if (algorithm.contains("384")) return 48;
116+
if (algorithm.contains("512")) return 64;
117+
if (algorithm.contains("MD5")) return 16;
118+
if (algorithm.contains("SHA1") || algorithm.contains("SHA-1")) return 20;
119+
120+
System.out.println("Warning: Unknown HMAC algorithm " + algorithm + ", using default key size 32");
121+
return 32;
104122
}
105123
}
106124

@@ -502,15 +520,54 @@ private static void runECCBenchmark(String providerName, String curveName) throw
502520
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, "EC");
503521
}
504522

523+
/* Get HMAC algorithms for a specific provider */
524+
private static Set<String> getHmacAlgorithms(String providerName) {
525+
return getAlgorithmsForService(providerName, "Mac");
526+
}
527+
528+
/* Get the baseline HMAC algorithms that wolfJCE supports for comparison */
529+
private static Set<String> getWolfJCEHmacAlgorithms() {
530+
return getWolfJCEAlgorithmsForService("Mac");
531+
}
532+
533+
/* Enhanced method to get HMAC algorithms for special provider cases, filtered by wolfJCE support */
534+
private static Set<String> getHmacAlgorithmsForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
535+
return getAlgorithmsForProvider(providerName, "Mac", wolfJCEAlgorithms);
536+
}
537+
538+
/* Updated HMAC benchmark runner using the universal methods */
539+
private static void runHmacBenchmarksForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
540+
System.out.println("\n" + providerName + ":");
541+
542+
Set<String> supportedAlgorithms;
543+
if (providerName.equals("wolfJCE")) {
544+
supportedAlgorithms = wolfJCEAlgorithms;
545+
} else {
546+
supportedAlgorithms = getHmacAlgorithmsForProvider(providerName, wolfJCEAlgorithms);
547+
}
548+
549+
if (supportedAlgorithms.isEmpty()) {
550+
System.out.println(" No common HMAC algorithms found for provider " + providerName);
551+
return;
552+
}
553+
554+
for (String algorithm : supportedAlgorithms) {
555+
try {
556+
runHmacBenchmark(algorithm, providerName);
557+
} catch (Exception e) {
558+
System.out.printf(" %-40s Error: %s%n",
559+
algorithm + " (" + providerName + ")", e.getMessage());
560+
}
561+
}
562+
}
563+
505564
/* HMAC benchmark */
506565
private static void runHmacBenchmark(String algorithm, String providerName) throws Exception {
507566
Mac mac;
508567
byte[] testData;
509-
double dataSizeMiB;
568+
int ops = 0;
510569
long startTime;
511-
long endTime;
512-
long elapsedTime;
513-
double throughput;
570+
double elapsedTime;
514571

515572
/* Generate test data */
516573
testData = generateTestData(DATA_SIZE);
@@ -532,21 +589,22 @@ private static void runHmacBenchmark(String algorithm, String providerName) thro
532589
mac.doFinal();
533590
}
534591

535-
/* Benchmark */
592+
/* Benchmark phase: run for at least 1 second like other tests */
536593
startTime = System.nanoTime();
537-
for (int i = 0; i < TEST_ITERATIONS; i++) {
594+
elapsedTime = 0;
595+
596+
do {
538597
mac.update(testData);
539598
mac.doFinal();
540-
}
541-
endTime = System.nanoTime();
542-
elapsedTime = (endTime - startTime) / TEST_ITERATIONS;
599+
ops++;
600+
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
601+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
543602

544-
dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);
545-
throughput = (DATA_SIZE / (elapsedTime / 1000000000.0)) / (1024.0 * 1024.0);
603+
double dataSizeMiB = (DATA_SIZE * ops) / (1024.0 * 1024.0);
604+
double throughput = dataSizeMiB / elapsedTime;
546605

547-
String testName = String.format("%s (%s)", algorithm, providerName);
548-
System.out.printf(" %-40s %8.3f MiB took %.3f seconds, %8.3f MiB/s%n",
549-
testName, dataSizeMiB, elapsedTime / 1_000_000_000.0, throughput);
606+
System.out.printf(" %-40s %8.3f MiB took %.3f sec, %8.3f MiB/s%n",
607+
algorithm + " (" + providerName + ")", dataSizeMiB, elapsedTime, throughput);
550608

551609
/* Store result */
552610
results.add(new BenchmarkResult(providerName, algorithm, throughput));
@@ -684,7 +742,7 @@ private static void runPBKDF2Benchmark(String algorithm, String providerName) th
684742
private static void runMessageDigestBenchmark(String algorithm, String providerName) throws Exception {
685743
MessageDigest md = MessageDigest.getInstance(algorithm, providerName);
686744
byte[] testData = generateTestData(DATA_SIZE);
687-
int ops = 0;
745+
long ops = 0;
688746
long startTime = System.nanoTime();
689747
double elapsedTime;
690748

@@ -828,54 +886,17 @@ private static void runSignatureBenchmark(String algorithm, String providerName)
828886

829887
/* Get signature algorithms for a specific provider */
830888
private static Set<String> getSignatureAlgorithms(String providerName) {
831-
Set<String> signatureAlgorithms = new TreeSet<>();
832-
833-
Provider provider = Security.getProvider(providerName);
834-
if (provider == null) {
835-
System.out.println("Provider " + providerName + " not found.");
836-
return signatureAlgorithms;
837-
}
838-
839-
for (Provider.Service service : provider.getServices()) {
840-
if ("Signature".equals(service.getType())) {
841-
signatureAlgorithms.add(service.getAlgorithm());
842-
}
843-
}
844-
845-
return signatureAlgorithms;
889+
return getAlgorithmsForService(providerName, "Signature");
846890
}
847891

848-
/* Get the baseline algorithms that wolfJCE supports for comparison */
892+
/* Get the baseline signature algorithms that wolfJCE supports for comparison */
849893
private static Set<String> getWolfJCESignatureAlgorithms() {
850-
return getSignatureAlgorithms("wolfJCE");
894+
return getWolfJCEAlgorithmsForService("Signature");
851895
}
852896

853897
/* Enhanced method to get signature algorithms for special provider cases, filtered by wolfJCE support */
854898
private static Set<String> getSignatureAlgorithmsForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
855-
Set<String> algorithms = new TreeSet<>();
856-
857-
if (providerName.equals("SunJCE")) {
858-
algorithms.addAll(getSignatureAlgorithms("SunRsaSign"));
859-
algorithms.addAll(getSignatureAlgorithms("SunEC"));
860-
algorithms.addAll(getSignatureAlgorithms("SUN"));
861-
} else {
862-
algorithms.addAll(getSignatureAlgorithms(providerName));
863-
}
864-
865-
if (providerName.equals("BC")) {
866-
Set<String> normalizedAlgorithms = new TreeSet<>();
867-
for (String algorithm : algorithms) {
868-
String normalized = algorithm.replace("WITH", "with");
869-
if (wolfJCEAlgorithms.contains(normalized)) {
870-
normalizedAlgorithms.add(algorithm);
871-
}
872-
}
873-
return normalizedAlgorithms;
874-
} else {
875-
algorithms.retainAll(wolfJCEAlgorithms);
876-
}
877-
878-
return algorithms;
899+
return getAlgorithmsForProvider(providerName, "Signature", wolfJCEAlgorithms);
879900
}
880901

881902
/* Updated signature benchmark runner */
@@ -1007,49 +1028,13 @@ public static void main(String[] args) {
10071028
System.out.println("HMAC Benchmark Results");
10081029
System.out.println("-----------------------------------------------------------------------------");
10091030

1010-
for (int i = 0; i < providers.length; i++) {
1011-
setupProvidersForTest(providers[i]);
1031+
/* First, set up wolfJCE provider to get its algorithm list */
1032+
setupProvidersForTest(providers[0]);
1033+
Set<String> wolfJCEHmacAlgorithms = getWolfJCEHmacAlgorithms();
10121034

1013-
if (FeatureDetect.HmacMd5Enabled()) {
1014-
try {
1015-
runHmacBenchmark("HmacMD5", providerNames[i]);
1016-
} catch (Exception e) {
1017-
System.out.printf("Failed to benchmark HmacMD5 with provider %s: %s%n",
1018-
providerNames[i], e.getMessage());
1019-
}
1020-
}
1021-
if (FeatureDetect.HmacShaEnabled()) {
1022-
try {
1023-
runHmacBenchmark("HmacSHA1", providerNames[i]);
1024-
} catch (Exception e) {
1025-
System.out.printf("Failed to benchmark HmacSHA1 with provider %s: %s%n",
1026-
providerNames[i], e.getMessage());
1027-
}
1028-
}
1029-
if (FeatureDetect.HmacSha256Enabled()) {
1030-
try {
1031-
runHmacBenchmark("HmacSHA256", providerNames[i]);
1032-
} catch (Exception e) {
1033-
System.out.printf("Failed to benchmark HmacSHA256 with provider %s: %s%n",
1034-
providerNames[i], e.getMessage());
1035-
}
1036-
}
1037-
if (FeatureDetect.HmacSha384Enabled()) {
1038-
try {
1039-
runHmacBenchmark("HmacSHA384", providerNames[i]);
1040-
} catch (Exception e) {
1041-
System.out.printf("Failed to benchmark HmacSHA384 with provider %s: %s%n",
1042-
providerNames[i], e.getMessage());
1043-
}
1044-
}
1045-
if (FeatureDetect.HmacSha512Enabled()) {
1046-
try {
1047-
runHmacBenchmark("HmacSHA512", providerNames[i]);
1048-
} catch (Exception e) {
1049-
System.out.printf("Failed to benchmark HmacSHA512 with provider %s: %s%n",
1050-
providerNames[i], e.getMessage());
1051-
}
1052-
}
1035+
for (Provider provider : providers) {
1036+
setupProvidersForTest(provider);
1037+
runHmacBenchmarksForProvider(provider.getName(), wolfJCEHmacAlgorithms);
10531038
}
10541039

10551040
/* Run DH benchmarks with clean provider setup */

0 commit comments

Comments
 (0)