Skip to content

Commit 7abe5c3

Browse files
committed
JCE: Implements HMAC benchmarks with SHA and MD5
1 parent 4b95eae commit 7abe5c3

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

examples/provider/CryptoBenchmark.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import javax.crypto.Cipher;
2+
import javax.crypto.KeyGenerator;
3+
import javax.crypto.Mac;
24
import javax.crypto.SecretKey;
35
import javax.crypto.spec.GCMParameterSpec;
46
import javax.crypto.spec.IvParameterSpec;
@@ -7,10 +9,10 @@
79
import java.security.SecureRandom;
810
import java.security.Security;
911
import java.security.spec.AlgorithmParameterSpec;
10-
import java.util.*;
1112
import java.security.KeyPair;
1213
import java.security.KeyPairGenerator;
1314
import java.security.spec.ECGenParameterSpec;
15+
import java.util.*;
1416

1517
import com.wolfssl.provider.jce.WolfCryptProvider;
1618
import com.wolfssl.wolfcrypt.FeatureDetect;
@@ -403,6 +405,55 @@ private static void runECCBenchmark(String providerName, String curveName) throw
403405
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, "EC");
404406
}
405407

408+
/* HMAC benchmark */
409+
private static void runHmacBenchmark(String algorithm, String providerName) throws Exception {
410+
Mac mac;
411+
byte[] testData;
412+
double dataSizeMiB;
413+
long startTime;
414+
long endTime;
415+
long elapsedTime;
416+
double throughput;
417+
418+
/* Generate test data */
419+
testData = generateTestData(DATA_SIZE);
420+
421+
/* Initialize Mac with specific provider */
422+
mac = Mac.getInstance(algorithm, providerName);
423+
424+
/* Initialize Mac with a random key */
425+
SecureRandom secureRandom = new SecureRandom();
426+
byte[] keyBytes = new byte[64];
427+
secureRandom.nextBytes(keyBytes);
428+
SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm);
429+
mac.init(key);
430+
431+
/* Warm up phase */
432+
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
433+
mac.update(testData);
434+
mac.doFinal();
435+
}
436+
437+
/* Benchmark */
438+
startTime = System.nanoTime();
439+
for (int i = 0; i < TEST_ITERATIONS; i++) {
440+
mac.update(testData);
441+
mac.doFinal();
442+
}
443+
endTime = System.nanoTime();
444+
elapsedTime = (endTime - startTime) / TEST_ITERATIONS;
445+
446+
dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);
447+
throughput = (DATA_SIZE / (elapsedTime / 1000000000.0)) / (1024.0 * 1024.0);
448+
449+
String testName = String.format("%s (%s)", algorithm, providerName);
450+
System.out.printf(" %-40s %8.3f MiB took %.3f seconds, %8.3f MiB/s%n",
451+
testName, dataSizeMiB, elapsedTime / 1_000_000_000.0, throughput);
452+
453+
/* Store result */
454+
results.add(new BenchmarkResult(providerName, algorithm, throughput));
455+
}
456+
406457
public static void main(String[] args) {
407458
try {
408459
/* Check if Bouncy Castle is available */
@@ -494,6 +545,32 @@ public static void main(String[] args) {
494545
Security.removeProvider(provider.getName());
495546
}
496547

548+
System.out.println("\n-----------------------------------------------------------------------------");
549+
System.out.println("HMAC Benchmark Results");
550+
System.out.println("-----------------------------------------------------------------------------");
551+
552+
for (int i = 0; i < providers.length; i++) {
553+
Security.insertProviderAt(providers[i], 1);
554+
555+
if (FeatureDetect.HmacMd5Enabled()) {
556+
runHmacBenchmark("HmacMD5", providerNames[i]);
557+
}
558+
if (FeatureDetect.HmacShaEnabled()) {
559+
runHmacBenchmark("HmacSHA1", providerNames[i]);
560+
}
561+
if (FeatureDetect.HmacSha256Enabled()) {
562+
runHmacBenchmark("HmacSHA256", providerNames[i]);
563+
}
564+
if (FeatureDetect.HmacSha384Enabled()) {
565+
runHmacBenchmark("HmacSHA384", providerNames[i]);
566+
}
567+
if (FeatureDetect.HmacSha512Enabled()) {
568+
runHmacBenchmark("HmacSHA512", providerNames[i]);
569+
}
570+
571+
Security.removeProvider(providers[i].getName());
572+
}
573+
497574
System.out.println("-----------------------------------------------------------------------------\n");
498575

499576
/* Print delta table */

0 commit comments

Comments
 (0)