|
16 | 16 | import javax.crypto.interfaces.DHPublicKey; |
17 | 17 | import javax.crypto.spec.DHParameterSpec; |
18 | 18 | import java.security.spec.ECGenParameterSpec; |
| 19 | +import javax.crypto.SecretKeyFactory; |
| 20 | +import javax.crypto.spec.PBEKeySpec; |
19 | 21 | import java.util.*; |
20 | 22 |
|
21 | 23 | import com.wolfssl.provider.jce.WolfCryptProvider; |
@@ -551,6 +553,62 @@ private static void runDHBenchmark(String providerName, int keySize) throws Exce |
551 | 553 | printKeyGenResults(agreementOps, elapsedTime, agreementOp, providerName, DH_ALGORITHM); |
552 | 554 | } |
553 | 555 |
|
| 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 | + |
554 | 612 | public static void main(String[] args) { |
555 | 613 | try { |
556 | 614 | /* Check if Bouncy Castle is available */ |
@@ -682,6 +740,42 @@ public static void main(String[] args) { |
682 | 740 | } |
683 | 741 | } |
684 | 742 |
|
| 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 | + |
685 | 779 | System.out.println("-----------------------------------------------------------------------------\n"); |
686 | 780 |
|
687 | 781 | /* Print delta table */ |
|
0 commit comments