@@ -73,6 +73,9 @@ static void tally_results(int type)
7373 }
7474 }
7575}
76+ #if defined(LTC_MRSA ) || defined(LTC_MECC )
77+ static void read_key (const char * alg , unsigned long sz , void * buf , unsigned long * l );
78+ #endif
7679
7780#define CSV_SEP ","
7881#define OUTFILE stdout
@@ -739,25 +742,22 @@ static void time_rsa(void)
739742{
740743 rsa_key key ;
741744 ulong64 t1 , t2 ;
742- unsigned char buf [2 ][2048 ] =
745+ unsigned char buf [2 ][4096 ] =
743746 { 0 };
744747 unsigned long x , y , z , zzz ;
745- int err , zz ;
748+ int zz ;
746749
747750 if (ltc_mp .name == NULL ) return ;
748751
749752 print_csv_header ("keysize" , NULL );
750753 for (x = 2048 ; x <= 8192 ; x <<= 1 ) {
751754
755+ #ifndef TIMING_DONT_MAKE_KEY
752756 t2 = 0 ;
753757 for (y = 0 ; y < 4 ; y ++ ) {
754758 t_start ();
755759 t1 = t_read ();
756- if ((err = rsa_make_key (& yarrow_prng , find_prng ("yarrow" ), x / 8 , 65537 , & key )) != CRYPT_OK ) {
757- fprintf (stderr , "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ),
758- error_to_string (CRYPT_OK ));
759- exit (EXIT_FAILURE );
760- }
760+ DO (rsa_make_key (& yarrow_prng , find_prng ("yarrow" ), x / 8 , 65537 , & key ));
761761 t1 = t_read () - t1 ;
762762 t2 += t1 ;
763763
@@ -770,19 +770,20 @@ static void time_rsa(void)
770770 }
771771 t2 >>= 2 ;
772772 print_csv ("RSA" , "make_key" , x , t2 );
773+ #endif
774+
775+ zzz = sizeof (buf );
776+ read_key ("RSA" , x , buf , & zzz );
777+
778+ DO (rsa_import ((void * )buf , zzz , & key ));
773779
774780 t2 = 0 ;
775781 for (y = 0 ; y < 256 ; y ++ ) {
776782 t_start ();
777783 t1 = t_read ();
778784 z = sizeof (buf [1 ]);
779- if ((err = rsa_encrypt_key (buf [0 ], 32 , buf [1 ], & z , (const unsigned char * )"testprog" , 8 , & yarrow_prng ,
780- find_prng ("yarrow" ), find_hash ("sha1" ), & key ))
781- != CRYPT_OK ) {
782- fprintf (stderr , "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n" ,
783- error_to_string (err ), error_to_string (CRYPT_OK ));
784- exit (EXIT_FAILURE );
785- }
785+ DO (rsa_encrypt_key (buf [0 ], 32 , buf [1 ], & z , (const unsigned char * )"testprog" , 8 , & yarrow_prng ,
786+ find_prng ("yarrow" ), find_hash ("sha1" ), & key ));
786787 t1 = t_read () - t1 ;
787788 t2 += t1 ;
788789#ifdef LTC_PROFILE
@@ -798,13 +799,8 @@ static void time_rsa(void)
798799 t_start ();
799800 t1 = t_read ();
800801 zzz = sizeof (buf [0 ]);
801- if ((err = rsa_decrypt_key (buf [1 ], z , buf [0 ], & zzz , (const unsigned char * )"testprog" , 8 , find_hash ("sha1" ),
802- & zz , & key ))
803- != CRYPT_OK ) {
804- fprintf (stderr , "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n" ,
805- error_to_string (err ), error_to_string (CRYPT_OK ));
806- exit (EXIT_FAILURE );
807- }
802+ DO (rsa_decrypt_key (buf [1 ], z , buf [0 ], & zzz , (const unsigned char * )"testprog" , 8 , find_hash ("sha1" ),
803+ & zz , & key ));
808804 t1 = t_read () - t1 ;
809805 t2 += t1 ;
810806#ifdef LTC_PROFILE
@@ -820,11 +816,7 @@ static void time_rsa(void)
820816 t_start ();
821817 t1 = t_read ();
822818 z = sizeof (buf [1 ]);
823- if ((err = rsa_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ), 8 , & key )) != CRYPT_OK ) {
824- fprintf (stderr , "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ),
825- error_to_string (CRYPT_OK ));
826- exit (EXIT_FAILURE );
827- }
819+ DO (rsa_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ), 8 , & key ));
828820 t1 = t_read () - t1 ;
829821 t2 += t1 ;
830822#ifdef LTC_PROFILE
@@ -840,11 +832,7 @@ static void time_rsa(void)
840832 int stat ;
841833 t_start ();
842834 t1 = t_read ();
843- if ((err = rsa_verify_hash (buf [1 ], z , buf [0 ], 20 , find_hash ("sha1" ), 8 , & stat , & key )) != CRYPT_OK ) {
844- fprintf (stderr , "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n" ,
845- error_to_string (err ), error_to_string (CRYPT_OK ));
846- exit (EXIT_FAILURE );
847- }
835+ DO (rsa_verify_hash (buf [1 ], z , buf [0 ], 20 , find_hash ("sha1" ), 8 , & stat , & key ));
848836 if (stat == 0 ) {
849837 fprintf (stderr , "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n" , x , y );
850838 exit (EXIT_FAILURE );
@@ -872,7 +860,6 @@ static void time_dh(void)
872860 dh_key key ;
873861 ulong64 t1 , t2 ;
874862 unsigned long i , x , y ;
875- int err ;
876863 static unsigned long sizes [] = {768 /8 , 1024 /8 , 1536 /8 , 2048 /8 ,
877864#ifndef TFM_DESC
878865 3072 /8 , 4096 /8 , 6144 /8 , 8192 /8 ,
@@ -886,17 +873,11 @@ static void time_dh(void)
886873 for (x = sizes [i = 0 ]; x < 100000 ; x = sizes [++ i ]) {
887874 t2 = 0 ;
888875 for (y = 0 ; y < 16 ; y ++ ) {
889- if ((err = dh_set_pg_groupsize (x , & key )) != CRYPT_OK ) {
890- fprintf (stderr , "\n\ndh_set_pg_groupsize says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
891- exit (EXIT_FAILURE );
892- }
876+ DO (dh_set_pg_groupsize (x , & key ));
893877
894878 t_start ();
895879 t1 = t_read ();
896- if ((err = dh_generate_key (& yarrow_prng , find_prng ("yarrow" ), & key )) != CRYPT_OK ) {
897- fprintf (stderr , "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
898- exit (EXIT_FAILURE );
899- }
880+ DO (dh_generate_key (& yarrow_prng , find_prng ("yarrow" ), & key ));
900881 t1 = t_read () - t1 ;
901882 t2 += t1 ;
902883
@@ -945,20 +926,19 @@ static void time_ecc(void)
945926 ulong64 t1 , t2 ;
946927 unsigned char buf [2 ][256 ] = { 0 };
947928 unsigned long i , w , x , y , z ;
948- int err , stat ;
929+ int stat ;
949930
950931 if (ltc_mp .name == NULL ) return ;
951932
952933 print_csv_header ("keysize" , NULL );
953934 for (x = ecc_key_sizes [i = 0 ]; x < 100000 ; x = ecc_key_sizes [++ i ]) {
935+
936+ #ifndef TIMING_DONT_MAKE_KEY
954937 t2 = 0 ;
955938 for (y = 0 ; y < 256 ; y ++ ) {
956939 t_start ();
957940 t1 = t_read ();
958- if ((err = ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x /8 , & key )) != CRYPT_OK ) {
959- fprintf (stderr , "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
960- exit (EXIT_FAILURE );
961- }
941+ DO (ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x /8 , & key ));
962942 t1 = t_read () - t1 ;
963943 t2 += t1 ;
964944
@@ -973,17 +953,20 @@ static void time_ecc(void)
973953 }
974954 t2 >>= 8 ;
975955 print_csv ("ECC" , "make_key" , x , t2 );
956+ #endif
957+
958+ w = sizeof (buf [0 ]);
959+ read_key ("ECC" , x , buf [0 ], & w );
960+
961+ DO (ecc_import (buf [0 ], w , & key ));
976962
977963 t2 = 0 ;
978964 for (y = 0 ; y < 256 ; y ++ ) {
979965 t_start ();
980966 t1 = t_read ();
981967 z = sizeof (buf [1 ]);
982- if ((err = ecc_encrypt_key (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ),
983- & key )) != CRYPT_OK ) {
984- fprintf (stderr , "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
985- exit (EXIT_FAILURE );
986- }
968+ DO (ecc_encrypt_key (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ),
969+ & key ));
987970 t1 = t_read () - t1 ;
988971 t2 += t1 ;
989972#ifdef LTC_PROFILE
@@ -999,10 +982,7 @@ static void time_ecc(void)
999982 t_start ();
1000983 t1 = t_read ();
1001984 w = 20 ;
1002- if ((err = ecc_decrypt_key (buf [1 ], z , buf [0 ], & w , & key )) != CRYPT_OK ) {
1003- fprintf (stderr , "\n\necc_decrypt_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
1004- exit (EXIT_FAILURE );
1005- }
985+ DO (ecc_decrypt_key (buf [1 ], z , buf [0 ], & w , & key ));
1006986 t1 = t_read () - t1 ;
1007987 t2 += t1 ;
1008988#ifdef LTC_PROFILE
@@ -1018,11 +998,8 @@ static void time_ecc(void)
1018998 t_start ();
1019999 t1 = t_read ();
10201000 z = sizeof (buf [1 ]);
1021- if ((err = ecc_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng ,
1022- find_prng ("yarrow" ), & key )) != CRYPT_OK ) {
1023- fprintf (stderr , "\n\necc_sign_hash says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
1024- exit (EXIT_FAILURE );
1025- }
1001+ DO (ecc_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng ,
1002+ find_prng ("yarrow" ), & key ));
10261003 t1 = t_read () - t1 ;
10271004 t2 += t1 ;
10281005#ifdef LTC_PROFILE
@@ -1037,10 +1014,7 @@ static void time_ecc(void)
10371014 for (y = 0 ; y < 256 ; y ++ ) {
10381015 t_start ();
10391016 t1 = t_read ();
1040- if ((err = ecc_verify_hash (buf [1 ], z , buf [0 ], 20 , & stat , & key )) != CRYPT_OK ) {
1041- fprintf (stderr , "\n\necc_verify_hash says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ), error_to_string (CRYPT_OK ));
1042- exit (EXIT_FAILURE );
1043- }
1017+ DO (ecc_verify_hash (buf [1 ], z , buf [0 ], 20 , & stat , & key ));
10441018 if (stat == 0 ) {
10451019 fprintf (stderr , "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n" , x , y );
10461020 exit (EXIT_FAILURE );
@@ -1066,6 +1040,28 @@ static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
10661040/* generate fresh PKA keys for the timing operations */
10671041#if defined(LTC_MRSA ) || defined(LTC_MECC )
10681042
1043+ static void read_key (const char * alg , unsigned long sz , void * buf , unsigned long * l )
1044+ {
1045+ char name [PATH_MAX ];
1046+ FILE * f ;
1047+ size_t n ;
1048+
1049+ snprintf (name , sizeof (name ) - 1 , "demos/keys/%s-%lu.privkey" , alg , sz );
1050+ f = fopen (name , "rb" );
1051+ if (f == NULL ) {
1052+ fprintf (stderr , "can't open %s" , name );
1053+ exit (EXIT_FAILURE );
1054+ }
1055+ n = fread (buf , 1 , * l , f );
1056+ if (feof (f )) {
1057+ * l = n ;
1058+ } else if (ferror (f )) {
1059+ fprintf (stderr , "reading of %s errored" , name );
1060+ exit (EXIT_FAILURE );
1061+ }
1062+ fclose (f );
1063+ }
1064+
10691065static void write_key (const char * alg , unsigned long sz , struct list * elmnt , void * buf , unsigned long l )
10701066{
10711067 char name [PATH_MAX ];
0 commit comments