|
21 | 21 | import java.security.MessageDigest; |
22 | 22 | import java.security.NoSuchAlgorithmException; |
23 | 23 | import java.security.NoSuchProviderException; |
| 24 | +import java.security.Signature; |
24 | 25 | import java.util.*; |
25 | 26 |
|
26 | 27 | import com.wolfssl.provider.jce.WolfCryptProvider; |
@@ -103,6 +104,58 @@ private static int getHmacKeySize(String algorithm) { |
103 | 104 | } |
104 | 105 | } |
105 | 106 |
|
| 107 | + /* List of signature algorithms supported by wolfJCE - must match exactly what's in WolfCryptProvider */ |
| 108 | + private static final String[] WOLFJCE_SIGNATURE_ALGORITHMS = { |
| 109 | + /* RSA algorithms */ |
| 110 | + "MD5withRSA", |
| 111 | + "SHA1withRSA", |
| 112 | + "SHA224withRSA", |
| 113 | + "SHA256withRSA", |
| 114 | + "SHA384withRSA", |
| 115 | + "SHA512withRSA", |
| 116 | + "SHA3-224withRSA", |
| 117 | + "SHA3-256withRSA", |
| 118 | + "SHA3-384withRSA", |
| 119 | + "SHA3-512withRSA", |
| 120 | + |
| 121 | + /* ECDSA algorithms */ |
| 122 | + "SHA1withECDSA", |
| 123 | + "SHA224withECDSA", |
| 124 | + "SHA256withECDSA", |
| 125 | + "SHA384withECDSA", |
| 126 | + "SHA512withECDSA", |
| 127 | + "SHA3-224withECDSA", |
| 128 | + "SHA3-256withECDSA", |
| 129 | + "SHA3-384withECDSA", |
| 130 | + "SHA3-512withECDSA" |
| 131 | + }; |
| 132 | + |
| 133 | + /* List of signature algorithms supported by BC */ |
| 134 | + private static final String[] BC_SIGNATURE_ALGORITHMS = { |
| 135 | + /* RSA algorithms */ |
| 136 | + "MD5withRSA", |
| 137 | + "SHA1withRSA", |
| 138 | + "SHA224withRSA", |
| 139 | + "SHA256withRSA", |
| 140 | + "SHA384withRSA", |
| 141 | + "SHA512withRSA", |
| 142 | + "SHA3-224withRSA", |
| 143 | + "SHA3-256withRSA", |
| 144 | + "SHA3-384withRSA", |
| 145 | + "SHA3-512withRSA", |
| 146 | + |
| 147 | + /* ECDSA algorithms */ |
| 148 | + "SHA1withECDSA", |
| 149 | + "SHA224withECDSA", |
| 150 | + "SHA256withECDSA", |
| 151 | + "SHA384withECDSA", |
| 152 | + "SHA512withECDSA", |
| 153 | + "SHA3-224withECDSA", |
| 154 | + "SHA3-256withECDSA", |
| 155 | + "SHA3-384withECDSA", |
| 156 | + "SHA3-512withECDSA" |
| 157 | + }; |
| 158 | + |
106 | 159 | private static void printProviderInfo(Provider provider) { |
107 | 160 | System.out.printf("%s version: %.1f%n", provider.getName(), provider.getVersion()); |
108 | 161 | } |
@@ -650,6 +703,102 @@ private static void runMessageDigestBenchmark(String algorithm, String providerN |
650 | 703 | results.add(new BenchmarkResult(providerName, algorithm, throughput)); |
651 | 704 | } |
652 | 705 |
|
| 706 | + /* Run signature benchmarks */ |
| 707 | + private static void runSignatureBenchmark(String algorithm, String providerName) throws Exception { |
| 708 | + KeyPairGenerator keyGen; |
| 709 | + Signature signature; |
| 710 | + byte[] testData; |
| 711 | + int ops = 0; |
| 712 | + long startTime; |
| 713 | + double elapsedTime; |
| 714 | + KeyPair keyPair; |
| 715 | + |
| 716 | + /* Generate small test data */ |
| 717 | + testData = generateTestData(SMALL_MESSAGE_SIZE); |
| 718 | + |
| 719 | + /* Initialize based on algorithm type */ |
| 720 | + if (algorithm.contains("withRSA")) { |
| 721 | + /* RSA signature */ |
| 722 | + if (providerName.equals("SunJCE")) { |
| 723 | + keyGen = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); |
| 724 | + signature = Signature.getInstance(algorithm, "SunRsaSign"); |
| 725 | + providerName = "SunRsaSign"; |
| 726 | + } else { |
| 727 | + keyGen = KeyPairGenerator.getInstance("RSA", providerName); |
| 728 | + signature = Signature.getInstance(algorithm, providerName); |
| 729 | + } |
| 730 | + keyGen.initialize(2048); |
| 731 | + } else if (algorithm.contains("withECDSA")) { |
| 732 | + /* ECDSA signature */ |
| 733 | + if (providerName.equals("SunJCE")) { |
| 734 | + keyGen = KeyPairGenerator.getInstance("EC", "SunEC"); |
| 735 | + signature = Signature.getInstance(algorithm, "SunEC"); |
| 736 | + providerName = "SunEC"; |
| 737 | + keyGen.initialize(new ECGenParameterSpec("secp256r1")); |
| 738 | + } else { |
| 739 | + keyGen = KeyPairGenerator.getInstance("EC", providerName); |
| 740 | + signature = Signature.getInstance(algorithm, providerName); |
| 741 | + keyGen.initialize(new ECGenParameterSpec("secp256r1")); |
| 742 | + } |
| 743 | + } else { |
| 744 | + throw new IllegalArgumentException("Unsupported signature algorithm: " + algorithm); |
| 745 | + } |
| 746 | + |
| 747 | + /* Generate key pair */ |
| 748 | + keyPair = keyGen.generateKeyPair(); |
| 749 | + |
| 750 | + /* Initialize signature for signing */ |
| 751 | + signature.initSign(keyPair.getPrivate()); |
| 752 | + signature.update(testData); |
| 753 | + byte[] sig = signature.sign(); |
| 754 | + |
| 755 | + /* Warm up phase */ |
| 756 | + for (int i = 0; i < WARMUP_ITERATIONS; i++) { |
| 757 | + signature.initSign(keyPair.getPrivate()); |
| 758 | + signature.update(testData); |
| 759 | + signature.sign(); |
| 760 | + |
| 761 | + signature.initVerify(keyPair.getPublic()); |
| 762 | + signature.update(testData); |
| 763 | + signature.verify(sig); |
| 764 | + } |
| 765 | + |
| 766 | + /* Benchmark signing */ |
| 767 | + startTime = System.nanoTime(); |
| 768 | + elapsedTime = 0; |
| 769 | + |
| 770 | + do { |
| 771 | + signature.initSign(keyPair.getPrivate()); |
| 772 | + signature.update(testData); |
| 773 | + signature.sign(); |
| 774 | + ops++; |
| 775 | + elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0; |
| 776 | + } while (elapsedTime < TEST_MIN_TIME_SECONDS); |
| 777 | + |
| 778 | + double signOpsPerSec = ops / elapsedTime; |
| 779 | + System.out.printf(" %-40s %8d ops took %.3f sec, %8.3f ops/sec%n", |
| 780 | + algorithm + " sign (" + providerName + ")", ops, elapsedTime, signOpsPerSec); |
| 781 | + results.add(new BenchmarkResult(providerName, algorithm + " sign", signOpsPerSec)); |
| 782 | + |
| 783 | + /* Benchmark verification */ |
| 784 | + ops = 0; |
| 785 | + startTime = System.nanoTime(); |
| 786 | + elapsedTime = 0; |
| 787 | + |
| 788 | + do { |
| 789 | + signature.initVerify(keyPair.getPublic()); |
| 790 | + signature.update(testData); |
| 791 | + signature.verify(sig); |
| 792 | + ops++; |
| 793 | + elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0; |
| 794 | + } while (elapsedTime < TEST_MIN_TIME_SECONDS); |
| 795 | + |
| 796 | + double verifyOpsPerSec = ops / elapsedTime; |
| 797 | + System.out.printf(" %-40s %8d ops took %.3f sec, %8.3f ops/sec%n", |
| 798 | + algorithm + " verify (" + providerName + ")", ops, elapsedTime, verifyOpsPerSec); |
| 799 | + results.add(new BenchmarkResult(providerName, algorithm + " verify", verifyOpsPerSec)); |
| 800 | + } |
| 801 | + |
653 | 802 | public static void main(String[] args) { |
654 | 803 | try { |
655 | 804 | /* Check if Bouncy Castle is available */ |
@@ -890,6 +1039,43 @@ public static void main(String[] args) { |
890 | 1039 | } |
891 | 1040 | } |
892 | 1041 |
|
| 1042 | + System.out.println("\n-----------------------------------------------------------------------------"); |
| 1043 | + System.out.println("Signature Benchmark Results"); |
| 1044 | + System.out.println("-----------------------------------------------------------------------------"); |
| 1045 | + |
| 1046 | + /* First run wolfJCE and BC provider benchmarks as before */ |
| 1047 | + for (Provider provider : providers) { |
| 1048 | + if (provider.getName().equals("SunJCE")) { |
| 1049 | + continue; |
| 1050 | + } |
| 1051 | + |
| 1052 | + Security.insertProviderAt(provider, 1); |
| 1053 | + System.out.println("\n" + provider.getName() + ":"); |
| 1054 | + |
| 1055 | + if (provider.getName().equals("wolfJCE")) { |
| 1056 | + /* Test wolfJCE signature algorithms */ |
| 1057 | + for (String algorithm : WOLFJCE_SIGNATURE_ALGORITHMS) { |
| 1058 | + try { |
| 1059 | + runSignatureBenchmark(algorithm, "wolfJCE"); |
| 1060 | + } catch (Exception e) { |
| 1061 | + System.out.printf(" %-40s Not supported: %s%n", |
| 1062 | + algorithm + " (wolfJCE)", e.getMessage()); |
| 1063 | + } |
| 1064 | + } |
| 1065 | + } else if (provider.getName().equals("BC")) { |
| 1066 | + /* Test BC signature algorithms */ |
| 1067 | + for (String algorithm : BC_SIGNATURE_ALGORITHMS) { |
| 1068 | + try { |
| 1069 | + runSignatureBenchmark(algorithm, "BC"); |
| 1070 | + } catch (Exception e) { |
| 1071 | + System.out.printf(" %-40s Not supported: %s%n", |
| 1072 | + algorithm + " (BC)", e.getMessage()); |
| 1073 | + } |
| 1074 | + } |
| 1075 | + } |
| 1076 | + Security.removeProvider(provider.getName()); |
| 1077 | + } |
| 1078 | + |
893 | 1079 | System.out.println("-----------------------------------------------------------------------------\n"); |
894 | 1080 |
|
895 | 1081 | /* Print delta table */ |
|
0 commit comments