Skip to content

Commit

Permalink
Merge pull request #100 from jackctj117/wolfJSSE_Benchmark
Browse files Browse the repository at this point in the history
JCE: Implements HMAC benchmarks with SHA and MD5
  • Loading branch information
cconlon authored Feb 14, 2025
2 parents 818807b + 98e85c9 commit 41d9fa2
Showing 1 changed file with 98 additions and 2 deletions.
100 changes: 98 additions & 2 deletions examples/provider/CryptoBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
Expand All @@ -7,10 +9,10 @@
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.util.*;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.spec.ECGenParameterSpec;
import java.util.*;

import com.wolfssl.provider.jce.WolfCryptProvider;
import com.wolfssl.wolfcrypt.FeatureDetect;
Expand Down Expand Up @@ -72,6 +74,24 @@ private static byte[] generateTestData(int size) {
return new byte[size];
}

/* Bytes sizes from WC_*_DIGEST_SIZE for corresponding algorithm in text.c */
private static int getHmacKeySize(String algorithm) {
switch (algorithm) {
case "HmacMD5":
return 16;
case "HmacSHA1":
return 20;
case "HmacSHA256":
return 32;
case "HmacSHA384":
return 48;
case "HmacSHA512":
return 64;
default:
throw new IllegalArgumentException("Unsupported HMAC algorithm: " + algorithm);
}
}

private static void printProviderInfo(Provider provider) {
System.out.printf("%s version: %.1f%n", provider.getName(), provider.getVersion());
}
Expand Down Expand Up @@ -156,7 +176,7 @@ private static void printDeltaTable() {
}
}
System.out.println("--------------------------------------------------------------------------------");
System.out.println("* Delta Value: MiB/s for symmetric ciphers, operations/second for RSA");
System.out.println("* Delta Value: MiB/s for symmetric ciphers, operations/second for RSA and ECC");
}

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

/* HMAC benchmark */
private static void runHmacBenchmark(String algorithm, String providerName) throws Exception {
Mac mac;
byte[] testData;
double dataSizeMiB;
long startTime;
long endTime;
long elapsedTime;
double throughput;

/* Generate test data */
testData = generateTestData(DATA_SIZE);

/* Initialize Mac with specific provider */
mac = Mac.getInstance(algorithm, providerName);

/* Initialize Mac with a random key of appropriate length */
SecureRandom secureRandom = new SecureRandom();
int keySize = getHmacKeySize(algorithm);
byte[] keyBytes = new byte[keySize];
secureRandom.nextBytes(keyBytes);
SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm);
mac.init(key);

/* Warm up phase */
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
mac.update(testData);
mac.doFinal();
}

/* Benchmark */
startTime = System.nanoTime();
for (int i = 0; i < TEST_ITERATIONS; i++) {
mac.update(testData);
mac.doFinal();
}
endTime = System.nanoTime();
elapsedTime = (endTime - startTime) / TEST_ITERATIONS;

dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0);
throughput = (DATA_SIZE / (elapsedTime / 1000000000.0)) / (1024.0 * 1024.0);

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

/* Store result */
results.add(new BenchmarkResult(providerName, algorithm, throughput));
}

public static void main(String[] args) {
try {
/* Check if Bouncy Castle is available */
Expand Down Expand Up @@ -494,6 +564,32 @@ public static void main(String[] args) {
Security.removeProvider(provider.getName());
}

System.out.println("\n-----------------------------------------------------------------------------");
System.out.println("HMAC Benchmark Results");
System.out.println("-----------------------------------------------------------------------------");

for (int i = 0; i < providers.length; i++) {
Security.insertProviderAt(providers[i], 1);

if (FeatureDetect.HmacMd5Enabled()) {
runHmacBenchmark("HmacMD5", providerNames[i]);
}
if (FeatureDetect.HmacShaEnabled()) {
runHmacBenchmark("HmacSHA1", providerNames[i]);
}
if (FeatureDetect.HmacSha256Enabled()) {
runHmacBenchmark("HmacSHA256", providerNames[i]);
}
if (FeatureDetect.HmacSha384Enabled()) {
runHmacBenchmark("HmacSHA384", providerNames[i]);
}
if (FeatureDetect.HmacSha512Enabled()) {
runHmacBenchmark("HmacSHA512", providerNames[i]);
}

Security.removeProvider(providers[i].getName());
}

System.out.println("-----------------------------------------------------------------------------\n");

/* Print delta table */
Expand Down

0 comments on commit 41d9fa2

Please sign in to comment.