Skip to content

Commit 41d9fa2

Browse files
authored
Merge pull request #100 from jackctj117/wolfJSSE_Benchmark
JCE: Implements HMAC benchmarks with SHA and MD5
2 parents 818807b + 98e85c9 commit 41d9fa2

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

examples/provider/CryptoBenchmark.java

+98-2
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;
@@ -72,6 +74,24 @@ private static byte[] generateTestData(int size) {
7274
return new byte[size];
7375
}
7476

77+
/* Bytes sizes from WC_*_DIGEST_SIZE for corresponding algorithm in text.c */
78+
private static int getHmacKeySize(String algorithm) {
79+
switch (algorithm) {
80+
case "HmacMD5":
81+
return 16;
82+
case "HmacSHA1":
83+
return 20;
84+
case "HmacSHA256":
85+
return 32;
86+
case "HmacSHA384":
87+
return 48;
88+
case "HmacSHA512":
89+
return 64;
90+
default:
91+
throw new IllegalArgumentException("Unsupported HMAC algorithm: " + algorithm);
92+
}
93+
}
94+
7595
private static void printProviderInfo(Provider provider) {
7696
System.out.printf("%s version: %.1f%n", provider.getName(), provider.getVersion());
7797
}
@@ -156,7 +176,7 @@ private static void printDeltaTable() {
156176
}
157177
}
158178
System.out.println("--------------------------------------------------------------------------------");
159-
System.out.println("* Delta Value: MiB/s for symmetric ciphers, operations/second for RSA");
179+
System.out.println("* Delta Value: MiB/s for symmetric ciphers, operations/second for RSA and ECC");
160180
}
161181

162182
/* Run symmetric encryption/decryption benchmarks */
@@ -403,6 +423,56 @@ private static void runECCBenchmark(String providerName, String curveName) throw
403423
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, "EC");
404424
}
405425

426+
/* HMAC benchmark */
427+
private static void runHmacBenchmark(String algorithm, String providerName) throws Exception {
428+
Mac mac;
429+
byte[] testData;
430+
double dataSizeMiB;
431+
long startTime;
432+
long endTime;
433+
long elapsedTime;
434+
double throughput;
435+
436+
/* Generate test data */
437+
testData = generateTestData(DATA_SIZE);
438+
439+
/* Initialize Mac with specific provider */
440+
mac = Mac.getInstance(algorithm, providerName);
441+
442+
/* Initialize Mac with a random key of appropriate length */
443+
SecureRandom secureRandom = new SecureRandom();
444+
int keySize = getHmacKeySize(algorithm);
445+
byte[] keyBytes = new byte[keySize];
446+
secureRandom.nextBytes(keyBytes);
447+
SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm);
448+
mac.init(key);
449+
450+
/* Warm up phase */
451+
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
452+
mac.update(testData);
453+
mac.doFinal();
454+
}
455+
456+
/* Benchmark */
457+
startTime = System.nanoTime();
458+
for (int i = 0; i < TEST_ITERATIONS; i++) {
459+
mac.update(testData);
460+
mac.doFinal();
461+
}
462+
endTime = System.nanoTime();
463+
elapsedTime = (endTime - startTime) / TEST_ITERATIONS;
464+
465+
dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);
466+
throughput = (DATA_SIZE / (elapsedTime / 1000000000.0)) / (1024.0 * 1024.0);
467+
468+
String testName = String.format("%s (%s)", algorithm, providerName);
469+
System.out.printf(" %-40s %8.3f MiB took %.3f seconds, %8.3f MiB/s%n",
470+
testName, dataSizeMiB, elapsedTime / 1_000_000_000.0, throughput);
471+
472+
/* Store result */
473+
results.add(new BenchmarkResult(providerName, algorithm, throughput));
474+
}
475+
406476
public static void main(String[] args) {
407477
try {
408478
/* Check if Bouncy Castle is available */
@@ -494,6 +564,32 @@ public static void main(String[] args) {
494564
Security.removeProvider(provider.getName());
495565
}
496566

567+
System.out.println("\n-----------------------------------------------------------------------------");
568+
System.out.println("HMAC Benchmark Results");
569+
System.out.println("-----------------------------------------------------------------------------");
570+
571+
for (int i = 0; i < providers.length; i++) {
572+
Security.insertProviderAt(providers[i], 1);
573+
574+
if (FeatureDetect.HmacMd5Enabled()) {
575+
runHmacBenchmark("HmacMD5", providerNames[i]);
576+
}
577+
if (FeatureDetect.HmacShaEnabled()) {
578+
runHmacBenchmark("HmacSHA1", providerNames[i]);
579+
}
580+
if (FeatureDetect.HmacSha256Enabled()) {
581+
runHmacBenchmark("HmacSHA256", providerNames[i]);
582+
}
583+
if (FeatureDetect.HmacSha384Enabled()) {
584+
runHmacBenchmark("HmacSHA384", providerNames[i]);
585+
}
586+
if (FeatureDetect.HmacSha512Enabled()) {
587+
runHmacBenchmark("HmacSHA512", providerNames[i]);
588+
}
589+
590+
Security.removeProvider(providers[i].getName());
591+
}
592+
497593
System.out.println("-----------------------------------------------------------------------------\n");
498594

499595
/* Print delta table */

0 commit comments

Comments
 (0)