Skip to content

Commit 52d1810

Browse files
authored
Merge pull request #113 from jackctj117/MAC_bench
Added Sha3 ciphers to HMAC benchmark
2 parents a7b6f82 + 81f3ba5 commit 52d1810

File tree

1 file changed

+183
-99
lines changed

1 file changed

+183
-99
lines changed

examples/provider/CryptoBenchmark.java

Lines changed: 183 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

@@ -165,6 +183,103 @@ private static KeyPairGenerator initializeKeyGenerator(String keyType, String ke
165183
return keyGen;
166184
}
167185

186+
/* Universal method to get algorithms for a specific provider and service type */
187+
private static Set<String> getAlgorithmsForProvider(String providerName, String serviceType, Set<String> wolfJCEAlgorithms) {
188+
Set<String> algorithms = new TreeSet<>();
189+
190+
if (providerName.equals("SunJCE") && serviceType.equals("Signature")) {
191+
algorithms.addAll(getAlgorithmsForService("SunRsaSign", serviceType));
192+
algorithms.addAll(getAlgorithmsForService("SunEC", serviceType));
193+
algorithms.addAll(getAlgorithmsForService("SUN", serviceType));
194+
} else {
195+
algorithms.addAll(getAlgorithmsForService(providerName, serviceType));
196+
}
197+
198+
if (providerName.equals("BC")) {
199+
Set<String> normalizedAlgorithms = new TreeSet<>();
200+
for (String algorithm : algorithms) {
201+
if (serviceType.equals("Signature")) {
202+
String normalized = algorithm.replace("WITH", "with");
203+
if (wolfJCEAlgorithms.contains(normalized)) {
204+
normalizedAlgorithms.add(algorithm);
205+
}
206+
} else if (serviceType.equals("Mac")) {
207+
String normalized = algorithm;
208+
209+
if (wolfJCEAlgorithms.contains(normalized)) {
210+
normalizedAlgorithms.add(algorithm);
211+
continue;
212+
}
213+
214+
for (String wolfAlg : wolfJCEAlgorithms) {
215+
if (wolfAlg.equalsIgnoreCase(normalized)) {
216+
normalizedAlgorithms.add(algorithm);
217+
break;
218+
}
219+
220+
String bcNormalized = normalized.replace("-", "");
221+
String wolfNormalized = wolfAlg.replace("-", "");
222+
if (bcNormalized.equalsIgnoreCase(wolfNormalized)) {
223+
normalizedAlgorithms.add(algorithm);
224+
break;
225+
}
226+
}
227+
} else {
228+
if (wolfJCEAlgorithms.contains(algorithm)) {
229+
normalizedAlgorithms.add(algorithm);
230+
}
231+
}
232+
}
233+
return normalizedAlgorithms;
234+
} else {
235+
algorithms.retainAll(wolfJCEAlgorithms);
236+
}
237+
238+
return algorithms;
239+
}
240+
241+
/* Get the baseline algorithms that wolfJCE supports for comparison */
242+
private static Set<String> getWolfJCEAlgorithmsForService(String serviceType) {
243+
return getAlgorithmsForService("wolfJCE", serviceType);
244+
}
245+
246+
/* Debug method to see what algorithms each provider actually supports */
247+
private static void debugPrintAlgorithms(String providerName, String serviceType) {
248+
System.out.println("Debug: " + providerName + " " + serviceType + " algorithms:");
249+
Set<String> algorithms = getAlgorithmsForService(providerName, serviceType);
250+
for (String alg : algorithms) {
251+
System.out.println(" " + alg);
252+
}
253+
System.out.println();
254+
}
255+
256+
/* Universal method to get algorithms for a specific provider and service type */
257+
private static Set<String> getAlgorithmsForService(String providerName, String serviceType) {
258+
Set<String> algorithms = new TreeSet<>();
259+
260+
Provider provider = Security.getProvider(providerName);
261+
if (provider == null) {
262+
System.out.println("Provider " + providerName + " not found.");
263+
return algorithms;
264+
}
265+
266+
for (Provider.Service service : provider.getServices()) {
267+
if (serviceType.equals(service.getType())) {
268+
String algorithm = service.getAlgorithm();
269+
270+
if (serviceType.equals("Mac")) {
271+
if (algorithm.startsWith("Hmac") || algorithm.startsWith("HMAC")) {
272+
algorithms.add(algorithm);
273+
}
274+
} else {
275+
algorithms.add(algorithm);
276+
}
277+
}
278+
}
279+
280+
return algorithms;
281+
}
282+
168283
private static void printDeltaTable() {
169284
/* Variables for table generation */
170285
Map<String, Map<String, Double>> groupedResults;
@@ -502,15 +617,54 @@ private static void runECCBenchmark(String providerName, String curveName) throw
502617
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, "EC");
503618
}
504619

620+
/* Get HMAC algorithms for a specific provider */
621+
private static Set<String> getHmacAlgorithms(String providerName) {
622+
return getAlgorithmsForService(providerName, "Mac");
623+
}
624+
625+
/* Get the baseline HMAC algorithms that wolfJCE supports for comparison */
626+
private static Set<String> getWolfJCEHmacAlgorithms() {
627+
return getWolfJCEAlgorithmsForService("Mac");
628+
}
629+
630+
/* Enhanced method to get HMAC algorithms for special provider cases, filtered by wolfJCE support */
631+
private static Set<String> getHmacAlgorithmsForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
632+
return getAlgorithmsForProvider(providerName, "Mac", wolfJCEAlgorithms);
633+
}
634+
635+
/* Updated HMAC benchmark runner using the universal methods */
636+
private static void runHmacBenchmarksForProvider(String providerName, Set<String> wolfJCEAlgorithms) {
637+
System.out.println("\n" + providerName + ":");
638+
639+
Set<String> supportedAlgorithms;
640+
if (providerName.equals("wolfJCE")) {
641+
supportedAlgorithms = wolfJCEAlgorithms;
642+
} else {
643+
supportedAlgorithms = getHmacAlgorithmsForProvider(providerName, wolfJCEAlgorithms);
644+
}
645+
646+
if (supportedAlgorithms.isEmpty()) {
647+
System.out.println(" No common HMAC algorithms found for provider " + providerName);
648+
return;
649+
}
650+
651+
for (String algorithm : supportedAlgorithms) {
652+
try {
653+
runHmacBenchmark(algorithm, providerName);
654+
} catch (Exception e) {
655+
System.out.printf(" %-40s Error: %s%n",
656+
algorithm + " (" + providerName + ")", e.getMessage());
657+
}
658+
}
659+
}
660+
505661
/* HMAC benchmark */
506662
private static void runHmacBenchmark(String algorithm, String providerName) throws Exception {
507663
Mac mac;
508664
byte[] testData;
509-
double dataSizeMiB;
665+
int ops = 0;
510666
long startTime;
511-
long endTime;
512-
long elapsedTime;
513-
double throughput;
667+
double elapsedTime;
514668

515669
/* Generate test data */
516670
testData = generateTestData(DATA_SIZE);
@@ -532,21 +686,22 @@ private static void runHmacBenchmark(String algorithm, String providerName) thro
532686
mac.doFinal();
533687
}
534688

535-
/* Benchmark */
689+
/* Benchmark phase: run for at least 1 second like other tests */
536690
startTime = System.nanoTime();
537-
for (int i = 0; i < TEST_ITERATIONS; i++) {
691+
elapsedTime = 0;
692+
693+
do {
538694
mac.update(testData);
539695
mac.doFinal();
540-
}
541-
endTime = System.nanoTime();
542-
elapsedTime = (endTime - startTime) / TEST_ITERATIONS;
696+
ops++;
697+
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
698+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
543699

544-
dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);
545-
throughput = (DATA_SIZE / (elapsedTime / 1000000000.0)) / (1024.0 * 1024.0);
700+
double dataSizeMiB = (DATA_SIZE * ops) / (1024.0 * 1024.0);
701+
double throughput = dataSizeMiB / elapsedTime;
546702

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);
703+
System.out.printf(" %-40s %8.3f MiB took %.3f sec, %8.3f MiB/s%n",
704+
algorithm + " (" + providerName + ")", dataSizeMiB, elapsedTime, throughput);
550705

551706
/* Store result */
552707
results.add(new BenchmarkResult(providerName, algorithm, throughput));
@@ -684,7 +839,7 @@ private static void runPBKDF2Benchmark(String algorithm, String providerName) th
684839
private static void runMessageDigestBenchmark(String algorithm, String providerName) throws Exception {
685840
MessageDigest md = MessageDigest.getInstance(algorithm, providerName);
686841
byte[] testData = generateTestData(DATA_SIZE);
687-
int ops = 0;
842+
long ops = 0;
688843
long startTime = System.nanoTime();
689844
double elapsedTime;
690845

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

829984
/* Get signature algorithms for a specific provider */
830985
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;
986+
return getAlgorithmsForService(providerName, "Signature");
846987
}
847988

848-
/* Get the baseline algorithms that wolfJCE supports for comparison */
989+
/* Get the baseline signature algorithms that wolfJCE supports for comparison */
849990
private static Set<String> getWolfJCESignatureAlgorithms() {
850-
return getSignatureAlgorithms("wolfJCE");
991+
return getWolfJCEAlgorithmsForService("Signature");
851992
}
852993

853994
/* Enhanced method to get signature algorithms for special provider cases, filtered by wolfJCE support */
854995
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;
996+
return getAlgorithmsForProvider(providerName, "Signature", wolfJCEAlgorithms);
879997
}
880998

881999
/* Updated signature benchmark runner */
@@ -1007,49 +1125,15 @@ public static void main(String[] args) {
10071125
System.out.println("HMAC Benchmark Results");
10081126
System.out.println("-----------------------------------------------------------------------------");
10091127

1010-
for (int i = 0; i < providers.length; i++) {
1011-
setupProvidersForTest(providers[i]);
10121128

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-
}
1129+
/* First, set up wolfJCE provider to get its algorithm list */
1130+
setupProvidersForTest(providers[0]);
1131+
Set<String> wolfJCEHmacAlgorithms = getWolfJCEHmacAlgorithms();
1132+
1133+
for (Provider provider : providers) {
1134+
setupProvidersForTest(provider);
1135+
runHmacBenchmarksForProvider(provider.getName(), wolfJCEHmacAlgorithms);
1136+
10531137
}
10541138

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

0 commit comments

Comments
 (0)