Skip to content

Commit 018641f

Browse files
committed
JCE: Implements PBKDF2 benchmarks
1 parent bd82709 commit 018641f

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

examples/provider/CryptoBenchmark.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import javax.crypto.interfaces.DHPublicKey;
1717
import javax.crypto.spec.DHParameterSpec;
1818
import java.security.spec.ECGenParameterSpec;
19+
import javax.crypto.SecretKeyFactory;
20+
import javax.crypto.spec.PBEKeySpec;
1921
import java.util.*;
2022

2123
import com.wolfssl.provider.jce.WolfCryptProvider;
@@ -551,6 +553,62 @@ private static void runDHBenchmark(String providerName, int keySize) throws Exce
551553
printKeyGenResults(agreementOps, elapsedTime, agreementOp, providerName, DH_ALGORITHM);
552554
}
553555

556+
/* PBKDF2 benchmark */
557+
private static void runPBKDF2Benchmark(String algorithm, String providerName) throws Exception {
558+
/* Variables for benchmark */
559+
SecretKeyFactory secretKeyFactory;
560+
byte[] salt;
561+
char[] password;
562+
int iterationCount = 10000;
563+
int keyLength = 32; // 256 bits
564+
int processingBytes = 1024; // Simulate 1 KiB of data processing
565+
SecureRandom secureRandom = new SecureRandom();
566+
567+
/* Initialize test parameters */
568+
salt = new byte[16];
569+
secureRandom.nextBytes(salt);
570+
password = "wolfCryptBenchmarkTestPassword".toCharArray();
571+
572+
/* Initialize SecretKeyFactory with specific provider */
573+
try {
574+
secretKeyFactory = SecretKeyFactory.getInstance(algorithm, providerName);
575+
} catch (Exception e) {
576+
System.out.printf(" %-40s Not supported by provider %s%n", algorithm, providerName);
577+
return;
578+
}
579+
580+
/* Create PBEKeySpec */
581+
PBEKeySpec pbeKeySpec = new PBEKeySpec(password, salt, iterationCount, keyLength * 8);
582+
583+
/* Warm up phase */
584+
for (int i = 0; i < WARMUP_ITERATIONS; i++) {
585+
secretKeyFactory.generateSecret(pbeKeySpec);
586+
}
587+
588+
/* Benchmark */
589+
long startTime = System.nanoTime();
590+
int operations = 0;
591+
double elapsedTime = 0;
592+
593+
/* Run for at least 1 second */
594+
do {
595+
secretKeyFactory.generateSecret(pbeKeySpec);
596+
operations++;
597+
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
598+
} while (elapsedTime < 1.0);
599+
600+
/* Calculate metrics */
601+
double processedKiB = (operations * processingBytes) / 1024.0;
602+
double throughput = processedKiB / elapsedTime;
603+
604+
String testName = String.format("%s (%s)", algorithm, providerName);
605+
System.out.printf(" %-40s %8.3f KiB took %.3f seconds, %8.3f KiB/s%n",
606+
testName, processedKiB, elapsedTime, throughput);
607+
608+
/* Store result */
609+
results.add(new BenchmarkResult(providerName, algorithm, throughput));
610+
}
611+
554612
public static void main(String[] args) {
555613
try {
556614
/* Check if Bouncy Castle is available */
@@ -682,6 +740,42 @@ public static void main(String[] args) {
682740
}
683741
}
684742

743+
System.out.println("\n-----------------------------------------------------------------------------");
744+
System.out.println("PBKDF2 Benchmark Results");
745+
System.out.println("-----------------------------------------------------------------------------");
746+
747+
/* List of PBKDF2 algorithms to test */
748+
String[] pbkdf2Algorithms = {
749+
"PBKDF2WithHmacSHA1",
750+
"PBKDF2WithHmacSHA224",
751+
"PBKDF2WithHmacSHA256",
752+
"PBKDF2WithHmacSHA384",
753+
"PBKDF2WithHmacSHA512",
754+
"PBKDF2WithHmacSHA3-224",
755+
"PBKDF2WithHmacSHA3-256",
756+
"PBKDF2WithHmacSHA3-384",
757+
"PBKDF2WithHmacSHA3-512"
758+
};
759+
760+
for (String providerName : providerNames) {
761+
System.out.println("\n" + providerName + ":");
762+
763+
for (String algorithm : pbkdf2Algorithms) {
764+
try {
765+
/* Skip SHA3 algorithms for SunJCE */
766+
if (providerName.equals("SunJCE") && algorithm.contains("SHA3")) {
767+
continue;
768+
}
769+
770+
runPBKDF2Benchmark(algorithm, providerName);
771+
} catch (Exception e) {
772+
/* Print but continue with other algorithms */
773+
System.out.printf(" %-40s Error: %s%n",
774+
algorithm + " (" + providerName + ")", e.getMessage());
775+
}
776+
}
777+
}
778+
685779
System.out.println("-----------------------------------------------------------------------------\n");
686780

687781
/* Print delta table */

examples/provider/CryptoBenchmark.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ else
7878
fi
7979

8080
# Run the benchmark
81-
java -classpath $CLASSPATH -Dsun.boot.library.path=../../../lib/ CryptoBenchmark $@
81+
java -XX:-TieredCompilation -XX:ReservedCodeCacheSize=1024m -classpath $CLASSPATH -Dsun.boot.library.path=../../../lib/ CryptoBenchmark $@
8282

8383
# Always prompt for cleanup after benchmark completion if Bouncy Castle files exist
8484
if [ -f "../../../lib/bcprov-jdk18on-1.79.jar" ] && [ -f "../../../lib/bctls-jdk18on-1.79.jar" ]; then

0 commit comments

Comments
 (0)