|
1 | 1 | import javax.crypto.Cipher;
|
| 2 | +import javax.crypto.KeyGenerator; |
| 3 | +import javax.crypto.Mac; |
2 | 4 | import javax.crypto.SecretKey;
|
3 | 5 | import javax.crypto.spec.GCMParameterSpec;
|
4 | 6 | import javax.crypto.spec.IvParameterSpec;
|
|
7 | 9 | import java.security.SecureRandom;
|
8 | 10 | import java.security.Security;
|
9 | 11 | import java.security.spec.AlgorithmParameterSpec;
|
10 |
| -import java.util.*; |
11 | 12 | import java.security.KeyPair;
|
12 | 13 | import java.security.KeyPairGenerator;
|
13 | 14 | import java.security.spec.ECGenParameterSpec;
|
| 15 | +import java.util.*; |
14 | 16 |
|
15 | 17 | import com.wolfssl.provider.jce.WolfCryptProvider;
|
16 | 18 | import com.wolfssl.wolfcrypt.FeatureDetect;
|
@@ -72,6 +74,24 @@ private static byte[] generateTestData(int size) {
|
72 | 74 | return new byte[size];
|
73 | 75 | }
|
74 | 76 |
|
| 77 | + /* Bytes sizes from WC_*_DIGEST_SIZE for corresponding algorithm in text.c */ |
| 78 | + private static int getHmacKeySize(String algorithm) { |
| 79 | + switch (algorithm) { |
| 80 | + case "HmacMD5": |
| 81 | + return 16; |
| 82 | + case "HmacSHA1": |
| 83 | + return 20; |
| 84 | + case "HmacSHA256": |
| 85 | + return 32; |
| 86 | + case "HmacSHA384": |
| 87 | + return 48; |
| 88 | + case "HmacSHA512": |
| 89 | + return 64; |
| 90 | + default: |
| 91 | + throw new IllegalArgumentException("Unsupported HMAC algorithm: " + algorithm); |
| 92 | + } |
| 93 | + } |
| 94 | + |
75 | 95 | private static void printProviderInfo(Provider provider) {
|
76 | 96 | System.out.printf("%s version: %.1f%n", provider.getName(), provider.getVersion());
|
77 | 97 | }
|
@@ -156,7 +176,7 @@ private static void printDeltaTable() {
|
156 | 176 | }
|
157 | 177 | }
|
158 | 178 | System.out.println("--------------------------------------------------------------------------------");
|
159 |
| - System.out.println("* Delta Value: MiB/s for symmetric ciphers, operations/second for RSA"); |
| 179 | + System.out.println("* Delta Value: MiB/s for symmetric ciphers, operations/second for RSA and ECC"); |
160 | 180 | }
|
161 | 181 |
|
162 | 182 | /* Run symmetric encryption/decryption benchmarks */
|
@@ -403,6 +423,56 @@ private static void runECCBenchmark(String providerName, String curveName) throw
|
403 | 423 | printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, "EC");
|
404 | 424 | }
|
405 | 425 |
|
| 426 | + /* HMAC benchmark */ |
| 427 | + private static void runHmacBenchmark(String algorithm, String providerName) throws Exception { |
| 428 | + Mac mac; |
| 429 | + byte[] testData; |
| 430 | + double dataSizeMiB; |
| 431 | + long startTime; |
| 432 | + long endTime; |
| 433 | + long elapsedTime; |
| 434 | + double throughput; |
| 435 | + |
| 436 | + /* Generate test data */ |
| 437 | + testData = generateTestData(DATA_SIZE); |
| 438 | + |
| 439 | + /* Initialize Mac with specific provider */ |
| 440 | + mac = Mac.getInstance(algorithm, providerName); |
| 441 | + |
| 442 | + /* Initialize Mac with a random key of appropriate length */ |
| 443 | + SecureRandom secureRandom = new SecureRandom(); |
| 444 | + int keySize = getHmacKeySize(algorithm); |
| 445 | + byte[] keyBytes = new byte[keySize]; |
| 446 | + secureRandom.nextBytes(keyBytes); |
| 447 | + SecretKeySpec key = new SecretKeySpec(keyBytes, algorithm); |
| 448 | + mac.init(key); |
| 449 | + |
| 450 | + /* Warm up phase */ |
| 451 | + for (int i = 0; i < WARMUP_ITERATIONS; i++) { |
| 452 | + mac.update(testData); |
| 453 | + mac.doFinal(); |
| 454 | + } |
| 455 | + |
| 456 | + /* Benchmark */ |
| 457 | + startTime = System.nanoTime(); |
| 458 | + for (int i = 0; i < TEST_ITERATIONS; i++) { |
| 459 | + mac.update(testData); |
| 460 | + mac.doFinal(); |
| 461 | + } |
| 462 | + endTime = System.nanoTime(); |
| 463 | + elapsedTime = (endTime - startTime) / TEST_ITERATIONS; |
| 464 | + |
| 465 | + dataSizeMiB = (DATA_SIZE * TEST_ITERATIONS) / (1024.0 * 1024.0); |
| 466 | + throughput = (DATA_SIZE / (elapsedTime / 1000000000.0)) / (1024.0 * 1024.0); |
| 467 | + |
| 468 | + String testName = String.format("%s (%s)", algorithm, providerName); |
| 469 | + System.out.printf(" %-40s %8.3f MiB took %.3f seconds, %8.3f MiB/s%n", |
| 470 | + testName, dataSizeMiB, elapsedTime / 1_000_000_000.0, throughput); |
| 471 | + |
| 472 | + /* Store result */ |
| 473 | + results.add(new BenchmarkResult(providerName, algorithm, throughput)); |
| 474 | + } |
| 475 | + |
406 | 476 | public static void main(String[] args) {
|
407 | 477 | try {
|
408 | 478 | /* Check if Bouncy Castle is available */
|
@@ -494,6 +564,32 @@ public static void main(String[] args) {
|
494 | 564 | Security.removeProvider(provider.getName());
|
495 | 565 | }
|
496 | 566 |
|
| 567 | + System.out.println("\n-----------------------------------------------------------------------------"); |
| 568 | + System.out.println("HMAC Benchmark Results"); |
| 569 | + System.out.println("-----------------------------------------------------------------------------"); |
| 570 | + |
| 571 | + for (int i = 0; i < providers.length; i++) { |
| 572 | + Security.insertProviderAt(providers[i], 1); |
| 573 | + |
| 574 | + if (FeatureDetect.HmacMd5Enabled()) { |
| 575 | + runHmacBenchmark("HmacMD5", providerNames[i]); |
| 576 | + } |
| 577 | + if (FeatureDetect.HmacShaEnabled()) { |
| 578 | + runHmacBenchmark("HmacSHA1", providerNames[i]); |
| 579 | + } |
| 580 | + if (FeatureDetect.HmacSha256Enabled()) { |
| 581 | + runHmacBenchmark("HmacSHA256", providerNames[i]); |
| 582 | + } |
| 583 | + if (FeatureDetect.HmacSha384Enabled()) { |
| 584 | + runHmacBenchmark("HmacSHA384", providerNames[i]); |
| 585 | + } |
| 586 | + if (FeatureDetect.HmacSha512Enabled()) { |
| 587 | + runHmacBenchmark("HmacSHA512", providerNames[i]); |
| 588 | + } |
| 589 | + |
| 590 | + Security.removeProvider(providers[i].getName()); |
| 591 | + } |
| 592 | + |
497 | 593 | System.out.println("-----------------------------------------------------------------------------\n");
|
498 | 594 |
|
499 | 595 | /* Print delta table */
|
|
0 commit comments