Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JCE: Implements HMAC benchmarks with SHA and MD5 #100

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion 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 @@ -403,6 +405,55 @@ 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 */
SecureRandom secureRandom = new SecureRandom();
byte[] keyBytes = new byte[64];
cconlon marked this conversation as resolved.
Show resolved Hide resolved
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 +545,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