1010import java .util .*;
1111import java .security .KeyPair ;
1212import java .security .KeyPairGenerator ;
13+ import java .security .spec .ECGenParameterSpec ;
1314
1415import com .wolfssl .provider .jce .WolfCryptProvider ;
1516import com .wolfssl .wolfcrypt .FeatureDetect ;
@@ -25,6 +26,7 @@ public class CryptoBenchmark {
2526 private static final int [] RSA_KEY_SIZES = {2048 , 3072 , 4096 };
2627 private static final int RSA_MIN_TIME_SECONDS = 1 ; /* minimum time to run each test */
2728 private static final int SMALL_MESSAGE_SIZE = 32 ; /* small message size for RSA ops */
29+ private static final String [] ECC_CURVES = {"secp256r1" }; /* Can add more curves benchmark.c only uses secp256r1 */
2830
2931 /* Class to store benchmark results */
3032 private static class BenchmarkResult {
@@ -267,7 +269,7 @@ private static void runEncDecBenchmark(String algorithm, String mode, String pad
267269 }
268270
269271 /* Print RSA results in simpler format */
270- private static void printRSAResults (int operations , double totalTime , String operation ,
272+ private static void printKeyGenResults (int operations , double totalTime , String operation ,
271273 String providerName , String mode ) {
272274 /* Variables for result calculations */
273275 double avgTimeMs ;
@@ -332,7 +334,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
332334 } while (elapsedTime < RSA_MIN_TIME_SECONDS );
333335
334336 keyGenOp = String .format ("RSA %d key gen" , keySize );
335- printRSAResults (keyGenOps , elapsedTime , keyGenOp , providerName , cipherMode );
337+ printKeyGenResults (keyGenOps , elapsedTime , keyGenOp , providerName , cipherMode );
336338
337339 /* For 2048-bit keys, test public/private operations */
338340 if (keySize == 2048 ) {
@@ -350,7 +352,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
350352 elapsedTime = (System .nanoTime () - startTime ) / 1_000_000_000.0 ;
351353 } while (elapsedTime < RSA_MIN_TIME_SECONDS );
352354
353- printRSAResults (publicOps , elapsedTime , "RSA 2048 public" , providerName , cipherMode );
355+ printKeyGenResults (publicOps , elapsedTime , "RSA 2048 public" , providerName , cipherMode );
354356
355357 /* Private key operations benchmark */
356358 cipher .init (Cipher .ENCRYPT_MODE , keyPair .getPublic ());
@@ -366,10 +368,41 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
366368 elapsedTime = (System .nanoTime () - startTime ) / 1_000_000_000.0 ;
367369 } while (elapsedTime < RSA_MIN_TIME_SECONDS );
368370
369- printRSAResults (privateOps , elapsedTime , "RSA 2048 private" , providerName , cipherMode );
371+ printKeyGenResults (privateOps , elapsedTime , "RSA 2048 private" , providerName , cipherMode );
370372 }
371373 }
372374
375+ /* ECC keygen benchmark */
376+ private static void runECCBenchmark (String providerName , String curveName ) throws Exception {
377+ KeyPairGenerator keyGen ;
378+ int keyGenOps = 0 ;
379+ long startTime ;
380+ double elapsedTime ;
381+
382+ /* Initialize key generator */
383+ if (providerName .equals ("SunJCE" )) {
384+ keyGen = KeyPairGenerator .getInstance ("EC" , "SunEC" );
385+ providerName = "SunEC" ;
386+ } else {
387+ keyGen = KeyPairGenerator .getInstance ("EC" , providerName );
388+ keyGen .initialize (new ECGenParameterSpec (curveName ));
389+ }
390+
391+ /* Key Generation benchmark */
392+ startTime = System .nanoTime ();
393+ elapsedTime = 0 ;
394+
395+ /* Run key generation benchmark */
396+ do {
397+ keyGen .generateKeyPair ();
398+ keyGenOps ++;
399+ elapsedTime = (System .nanoTime () - startTime ) / 1_000_000_000.0 ;
400+ } while (elapsedTime < RSA_MIN_TIME_SECONDS );
401+
402+ String keyGenOp = String .format ("ECC %s key gen" , curveName );
403+ printKeyGenResults (keyGenOps , elapsedTime , keyGenOp , providerName , "EC" );
404+ }
405+
373406 public static void main (String [] args ) {
374407 try {
375408 /* Check if Bouncy Castle is available */
@@ -439,8 +472,30 @@ public static void main(String[] args) {
439472 }
440473 Security .removeProvider (provider .getName ());
441474 }
475+
476+ System .out .println ("\n -----------------------------------------------------------------------------" );
477+ System .out .println ("ECC Benchmark Results" );
442478 System .out .println ("-----------------------------------------------------------------------------" );
443479
480+ for (Provider provider : providers ) {
481+ if (provider instanceof WolfCryptProvider && !FeatureDetect .EccKeyGenEnabled ()) {
482+ continue ;
483+ }
484+ Security .insertProviderAt (provider , 1 );
485+ System .out .println ("\n " + (provider .getName ().equals ("SunJCE" ) ? "SunJCE / SunEC" : provider .getName ()) + ":" );
486+ for (String curve : ECC_CURVES ) {
487+ try {
488+ runECCBenchmark (provider .getName (), curve );
489+ } catch (Exception e ) {
490+ System .out .printf ("Failed to benchmark %s with provider %s: %s%n" ,
491+ curve , provider .getName (), e .getMessage ());
492+ }
493+ }
494+ Security .removeProvider (provider .getName ());
495+ }
496+
497+ System .out .println ("-----------------------------------------------------------------------------\n " );
498+
444499 /* Print delta table */
445500 printDeltaTable ();
446501
0 commit comments