10
10
import java .util .*;
11
11
import java .security .KeyPair ;
12
12
import java .security .KeyPairGenerator ;
13
+ import java .security .spec .ECGenParameterSpec ;
13
14
14
15
import com .wolfssl .provider .jce .WolfCryptProvider ;
15
16
import com .wolfssl .wolfcrypt .FeatureDetect ;
@@ -25,6 +26,7 @@ public class CryptoBenchmark {
25
26
private static final int [] RSA_KEY_SIZES = {2048 , 3072 , 4096 };
26
27
private static final int RSA_MIN_TIME_SECONDS = 1 ; /* minimum time to run each test */
27
28
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 */
28
30
29
31
/* Class to store benchmark results */
30
32
private static class BenchmarkResult {
@@ -267,7 +269,7 @@ private static void runEncDecBenchmark(String algorithm, String mode, String pad
267
269
}
268
270
269
271
/* 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 ,
271
273
String providerName , String mode ) {
272
274
/* Variables for result calculations */
273
275
double avgTimeMs ;
@@ -332,7 +334,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
332
334
} while (elapsedTime < RSA_MIN_TIME_SECONDS );
333
335
334
336
keyGenOp = String .format ("RSA %d key gen" , keySize );
335
- printRSAResults (keyGenOps , elapsedTime , keyGenOp , providerName , cipherMode );
337
+ printKeyGenResults (keyGenOps , elapsedTime , keyGenOp , providerName , cipherMode );
336
338
337
339
/* For 2048-bit keys, test public/private operations */
338
340
if (keySize == 2048 ) {
@@ -350,7 +352,7 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
350
352
elapsedTime = (System .nanoTime () - startTime ) / 1_000_000_000.0 ;
351
353
} while (elapsedTime < RSA_MIN_TIME_SECONDS );
352
354
353
- printRSAResults (publicOps , elapsedTime , "RSA 2048 public" , providerName , cipherMode );
355
+ printKeyGenResults (publicOps , elapsedTime , "RSA 2048 public" , providerName , cipherMode );
354
356
355
357
/* Private key operations benchmark */
356
358
cipher .init (Cipher .ENCRYPT_MODE , keyPair .getPublic ());
@@ -366,10 +368,41 @@ private static void runRSABenchmark(String providerName, int keySize) throws Exc
366
368
elapsedTime = (System .nanoTime () - startTime ) / 1_000_000_000.0 ;
367
369
} while (elapsedTime < RSA_MIN_TIME_SECONDS );
368
370
369
- printRSAResults (privateOps , elapsedTime , "RSA 2048 private" , providerName , cipherMode );
371
+ printKeyGenResults (privateOps , elapsedTime , "RSA 2048 private" , providerName , cipherMode );
370
372
}
371
373
}
372
374
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
+
373
406
public static void main (String [] args ) {
374
407
try {
375
408
/* Check if Bouncy Castle is available */
@@ -439,8 +472,30 @@ public static void main(String[] args) {
439
472
}
440
473
Security .removeProvider (provider .getName ());
441
474
}
475
+
476
+ System .out .println ("\n -----------------------------------------------------------------------------" );
477
+ System .out .println ("ECC Benchmark Results" );
442
478
System .out .println ("-----------------------------------------------------------------------------" );
443
479
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
+
444
499
/* Print delta table */
445
500
printDeltaTable ();
446
501
0 commit comments