Skip to content

Commit 4bf623a

Browse files
authored
Merge pull request #102 from jackctj117/wolfJSSE_Benchmark
JCE: Implements DH key gen and key agreement benchmark
2 parents 41d9fa2 + bd82709 commit 4bf623a

File tree

1 file changed

+102
-10
lines changed

1 file changed

+102
-10
lines changed

examples/provider/CryptoBenchmark.java

+102-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import java.security.spec.AlgorithmParameterSpec;
1212
import java.security.KeyPair;
1313
import java.security.KeyPairGenerator;
14+
import javax.crypto.KeyAgreement;
15+
import java.math.BigInteger;
16+
import javax.crypto.interfaces.DHPublicKey;
17+
import javax.crypto.spec.DHParameterSpec;
1418
import java.security.spec.ECGenParameterSpec;
1519
import java.util.*;
1620

@@ -26,9 +30,11 @@ public class CryptoBenchmark {
2630
private static final int DES3_BLOCK_SIZE = 8;
2731
private static final int GCM_TAG_LENGTH = 128;
2832
private static final int[] RSA_KEY_SIZES = {2048, 3072, 4096};
29-
private static final int RSA_MIN_TIME_SECONDS = 1; /* minimum time to run each test */
33+
private static final int TEST_MIN_TIME_SECONDS = 1; /* minimum time to run each test */
3034
private static final int SMALL_MESSAGE_SIZE = 32; /* small message size for RSA ops */
3135
private static final String[] ECC_CURVES = {"secp256r1"}; /* Can add more curves benchmark.c only uses secp256r1 */
36+
private static final int[] DH_KEY_SIZES = {2048}; /* Can add more key sizes benchmark.c only uses 2048 */
37+
private static final String DH_ALGORITHM = "DiffieHellman";
3238

3339
/* Class to store benchmark results */
3440
private static class BenchmarkResult {
@@ -351,7 +357,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
351357
keyGen.generateKeyPair();
352358
keyGenOps++;
353359
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
354-
} while (elapsedTime < RSA_MIN_TIME_SECONDS);
360+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
355361

356362
keyGenOp = String.format("RSA %d key gen", keySize);
357363
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, cipherMode);
@@ -370,7 +376,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
370376
cipher.doFinal(testData);
371377
publicOps++;
372378
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
373-
} while (elapsedTime < RSA_MIN_TIME_SECONDS);
379+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
374380

375381
printKeyGenResults(publicOps, elapsedTime, "RSA 2048 public", providerName, cipherMode);
376382

@@ -386,7 +392,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
386392
cipher.doFinal(encrypted);
387393
privateOps++;
388394
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
389-
} while (elapsedTime < RSA_MIN_TIME_SECONDS);
395+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
390396

391397
printKeyGenResults(privateOps, elapsedTime, "RSA 2048 private", providerName, cipherMode);
392398
}
@@ -417,7 +423,7 @@ private static void runECCBenchmark(String providerName, String curveName) throw
417423
keyGen.generateKeyPair();
418424
keyGenOps++;
419425
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
420-
} while (elapsedTime < RSA_MIN_TIME_SECONDS);
426+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
421427

422428
String keyGenOp = String.format("ECC %s key gen", curveName);
423429
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, "EC");
@@ -473,6 +479,78 @@ private static void runHmacBenchmark(String algorithm, String providerName) thro
473479
results.add(new BenchmarkResult(providerName, algorithm, throughput));
474480
}
475481

482+
/* Run DH benchmarks for specified provider and key size */
483+
private static void runDHBenchmark(String providerName, int keySize) throws Exception {
484+
/* Variables for benchmark operations */
485+
KeyPairGenerator keyGen;
486+
KeyAgreement keyAgreement;
487+
int keyGenOps;
488+
int agreementOps;
489+
long startTime;
490+
double elapsedTime;
491+
KeyPair keyPair1 = null;
492+
KeyPair keyPair2 = null;
493+
494+
/* Standard DH parameters for 2048-bit key from RFC 3526 */
495+
BigInteger p = new BigInteger(
496+
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" +
497+
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD" +
498+
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" +
499+
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
500+
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" +
501+
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" +
502+
"83655D23DCA3AD961C62F356208552BB9ED529077096966D" +
503+
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" +
504+
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" +
505+
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510" +
506+
"15728E5A8AACAA68FFFFFFFFFFFFFFFF", 16);
507+
BigInteger g = BigInteger.valueOf(2);
508+
DHParameterSpec dhParams = new DHParameterSpec(p, g);
509+
510+
/* Get KeyPairGenerator for DH */
511+
keyGen = KeyPairGenerator.getInstance("DH", providerName);
512+
513+
/* Initialize with parameters */
514+
keyGen.initialize(dhParams);
515+
516+
/* Key Generation benchmark */
517+
keyGenOps = 0;
518+
startTime = System.nanoTime();
519+
elapsedTime = 0;
520+
521+
/* Run key generation benchmark */
522+
do {
523+
keyGen.generateKeyPair();
524+
keyGenOps++;
525+
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
526+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
527+
528+
String keyGenOp = String.format("DH %d key gen", keySize);
529+
printKeyGenResults(keyGenOps, elapsedTime, keyGenOp, providerName, DH_ALGORITHM);
530+
531+
/* Generate key pairs for agreement operations */
532+
keyPair1 = keyGen.generateKeyPair();
533+
keyPair2 = keyGen.generateKeyPair();
534+
535+
/* Key Agreement benchmark */
536+
keyAgreement = KeyAgreement.getInstance("DH", providerName);
537+
agreementOps = 0;
538+
startTime = System.nanoTime();
539+
elapsedTime = 0;
540+
541+
/* Run key agreement benchmark */
542+
do {
543+
keyAgreement.init(keyPair1.getPrivate());
544+
keyAgreement.doPhase(keyPair2.getPublic(), true);
545+
keyAgreement.generateSecret();
546+
agreementOps++;
547+
elapsedTime = (System.nanoTime() - startTime) / 1_000_000_000.0;
548+
} while (elapsedTime < TEST_MIN_TIME_SECONDS);
549+
550+
String agreementOp = String.format("DH %d agree", keySize);
551+
printKeyGenResults(agreementOps, elapsedTime, agreementOp, providerName, DH_ALGORITHM);
552+
}
553+
476554
public static void main(String[] args) {
477555
try {
478556
/* Check if Bouncy Castle is available */
@@ -524,11 +602,8 @@ public static void main(String[] args) {
524602
if (FeatureDetect.Des3Enabled()) {
525603
runEncDecBenchmark("DESede", "CBC", "NoPadding", providerNames[i]);
526604
}
527-
528-
Security.removeProvider(providers[i].getName());
529605
}
530606

531-
532607
/* Run RSA benchmarks */
533608
System.out.println("\n-----------------------------------------------------------------------------");
534609
System.out.println("RSA Benchmark Results");
@@ -561,7 +636,6 @@ public static void main(String[] args) {
561636
curve, provider.getName(), e.getMessage());
562637
}
563638
}
564-
Security.removeProvider(provider.getName());
565639
}
566640

567641
System.out.println("\n-----------------------------------------------------------------------------");
@@ -586,8 +660,26 @@ public static void main(String[] args) {
586660
if (FeatureDetect.HmacSha512Enabled()) {
587661
runHmacBenchmark("HmacSHA512", providerNames[i]);
588662
}
663+
}
664+
665+
System.out.println("\n-----------------------------------------------------------------------------");
666+
System.out.println("DH Benchmark Results");
667+
System.out.println("-----------------------------------------------------------------------------");
589668

590-
Security.removeProvider(providers[i].getName());
669+
for (Provider provider : providers) {
670+
if (provider instanceof WolfCryptProvider && !FeatureDetect.DhEnabled()) {
671+
continue;
672+
}
673+
Security.insertProviderAt(provider, 1);
674+
System.out.println("\n" + provider.getName() + ":");
675+
for (int keySize : DH_KEY_SIZES) {
676+
try {
677+
runDHBenchmark(provider.getName(), keySize);
678+
} catch (Exception e) {
679+
System.out.printf("Failed to benchmark DH %d with provider %s: %s%n",
680+
keySize, provider.getName(), e.getMessage());
681+
}
682+
}
591683
}
592684

593685
System.out.println("-----------------------------------------------------------------------------\n");

0 commit comments

Comments
 (0)