@@ -67,6 +67,9 @@ static void tally_results(int type)
6767 }
6868 }
6969}
70+ #if defined(LTC_MRSA ) || defined(LTC_MECC )
71+ static void read_key (const char * alg , unsigned long sz , void * buf , unsigned long * l );
72+ #endif
7073
7174#define CSV_SEP ","
7275#define OUTFILE stdout
@@ -737,25 +740,22 @@ static void time_rsa(void)
737740{
738741 rsa_key key ;
739742 ulong64 t1 , t2 ;
740- unsigned char buf [2 ][2048 ] =
743+ unsigned char buf [2 ][4096 ] =
741744 { 0 };
742745 unsigned long x , y , z , zzz ;
743- int err , zz ;
746+ int zz ;
744747
745748 if (ltc_mp .name == NULL ) return ;
746749
747750 print_csv_header ("keysize" , NULL );
748751 for (x = 2048 ; x <= 8192 ; x <<= 1 ) {
749752
753+ #ifndef TIMING_DONT_MAKE_KEY
750754 t2 = 0 ;
751755 for (y = 0 ; y < 4 ; y ++ ) {
752756 t_start ();
753757 t1 = t_read ();
754- if ((err = rsa_make_key (& yarrow_prng , find_prng ("yarrow" ), x / 8 , 65537 , & key )) != CRYPT_OK ) {
755- fprintf (stderr , "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ),
756- error_to_string (CRYPT_OK ));
757- exit (EXIT_FAILURE );
758- }
758+ DO (rsa_make_key (& yarrow_prng , find_prng ("yarrow" ), x / 8 , 65537 , & key ));
759759 t1 = t_read () - t1 ;
760760 t2 += t1 ;
761761
@@ -768,19 +768,20 @@ static void time_rsa(void)
768768 }
769769 t2 >>= 2 ;
770770 print_csv ("RSA" , "make_key" , x , t2 );
771+ #endif
772+
773+ zzz = sizeof (buf );
774+ read_key ("RSA" , x , buf , & zzz );
775+
776+ DO (rsa_import ((void * )buf , zzz , & key ));
771777
772778 t2 = 0 ;
773779 for (y = 0 ; y < 256 ; y ++ ) {
774780 t_start ();
775781 t1 = t_read ();
776782 z = sizeof (buf [1 ]);
777- if ((err = rsa_encrypt_key (buf [0 ], 32 , buf [1 ], & z , (const unsigned char * )"testprog" , 8 , & yarrow_prng ,
778- find_prng ("yarrow" ), find_hash ("sha1" ), & key ))
779- != CRYPT_OK ) {
780- fprintf (stderr , "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n" ,
781- error_to_string (err ), error_to_string (CRYPT_OK ));
782- exit (EXIT_FAILURE );
783- }
783+ DO (rsa_encrypt_key (buf [0 ], 32 , buf [1 ], & z , (const unsigned char * )"testprog" , 8 , & yarrow_prng ,
784+ find_prng ("yarrow" ), find_hash ("sha1" ), & key ));
784785 t1 = t_read () - t1 ;
785786 t2 += t1 ;
786787#ifdef LTC_PROFILE
@@ -796,13 +797,8 @@ static void time_rsa(void)
796797 t_start ();
797798 t1 = t_read ();
798799 zzz = sizeof (buf [0 ]);
799- if ((err = rsa_decrypt_key (buf [1 ], z , buf [0 ], & zzz , (const unsigned char * )"testprog" , 8 , find_hash ("sha1" ),
800- & zz , & key ))
801- != CRYPT_OK ) {
802- fprintf (stderr , "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n" ,
803- error_to_string (err ), error_to_string (CRYPT_OK ));
804- exit (EXIT_FAILURE );
805- }
800+ DO (rsa_decrypt_key (buf [1 ], z , buf [0 ], & zzz , (const unsigned char * )"testprog" , 8 , find_hash ("sha1" ),
801+ & zz , & key ));
806802 t1 = t_read () - t1 ;
807803 t2 += t1 ;
808804#ifdef LTC_PROFILE
@@ -818,11 +814,7 @@ static void time_rsa(void)
818814 t_start ();
819815 t1 = t_read ();
820816 z = sizeof (buf [1 ]);
821- if ((err = rsa_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ), 8 , & key )) != CRYPT_OK ) {
822- fprintf (stderr , "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n" , error_to_string (err ),
823- error_to_string (CRYPT_OK ));
824- exit (EXIT_FAILURE );
825- }
817+ DO (rsa_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ), 8 , & key ));
826818 t1 = t_read () - t1 ;
827819 t2 += t1 ;
828820#ifdef LTC_PROFILE
@@ -838,11 +830,7 @@ static void time_rsa(void)
838830 int stat ;
839831 t_start ();
840832 t1 = t_read ();
841- if ((err = rsa_verify_hash (buf [1 ], z , buf [0 ], 20 , find_hash ("sha1" ), 8 , & stat , & key )) != CRYPT_OK ) {
842- fprintf (stderr , "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n" ,
843- error_to_string (err ), error_to_string (CRYPT_OK ));
844- exit (EXIT_FAILURE );
845- }
833+ DO (rsa_verify_hash (buf [1 ], z , buf [0 ], 20 , find_hash ("sha1" ), 8 , & stat , & key ));
846834 if (stat == 0 ) {
847835 fprintf (stderr , "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n" , x , y );
848836 exit (EXIT_FAILURE );
@@ -870,7 +858,6 @@ static void time_dh(void)
870858 dh_key key ;
871859 ulong64 t1 , t2 ;
872860 unsigned long i , x , y ;
873- int err ;
874861 static unsigned long sizes [] = {768 /8 , 1024 /8 , 1536 /8 , 2048 /8 ,
875862#ifndef TFM_DESC
876863 3072 /8 , 4096 /8 , 6144 /8 , 8192 /8 ,
@@ -884,17 +871,11 @@ static void time_dh(void)
884871 for (x = sizes [i = 0 ]; x < 100000 ; x = sizes [++ i ]) {
885872 t2 = 0 ;
886873 for (y = 0 ; y < 16 ; y ++ ) {
887- if ((err = dh_set_pg_groupsize (x , & key )) != CRYPT_OK ) {
888- 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 ));
889- exit (EXIT_FAILURE );
890- }
874+ DO (dh_set_pg_groupsize (x , & key ));
891875
892876 t_start ();
893877 t1 = t_read ();
894- if ((err = dh_generate_key (& yarrow_prng , find_prng ("yarrow" ), & key )) != CRYPT_OK ) {
895- 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 ));
896- exit (EXIT_FAILURE );
897- }
878+ DO (dh_generate_key (& yarrow_prng , find_prng ("yarrow" ), & key ));
898879 t1 = t_read () - t1 ;
899880 t2 += t1 ;
900881
@@ -943,20 +924,19 @@ static void time_ecc(void)
943924 ulong64 t1 , t2 ;
944925 unsigned char buf [2 ][256 ] = { 0 };
945926 unsigned long i , w , x , y , z ;
946- int err , stat ;
927+ int stat ;
947928
948929 if (ltc_mp .name == NULL ) return ;
949930
950931 print_csv_header ("keysize" , NULL );
951932 for (x = ecc_key_sizes [i = 0 ]; x < 100000 ; x = ecc_key_sizes [++ i ]) {
933+
934+ #ifndef TIMING_DONT_MAKE_KEY
952935 t2 = 0 ;
953936 for (y = 0 ; y < 256 ; y ++ ) {
954937 t_start ();
955938 t1 = t_read ();
956- if ((err = ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x /8 , & key )) != CRYPT_OK ) {
957- 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 ));
958- exit (EXIT_FAILURE );
959- }
939+ DO (ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x /8 , & key ));
960940 t1 = t_read () - t1 ;
961941 t2 += t1 ;
962942
@@ -971,17 +951,20 @@ static void time_ecc(void)
971951 }
972952 t2 >>= 8 ;
973953 print_csv ("ECC" , "make_key" , x , t2 );
954+ #endif
955+
956+ w = sizeof (buf [0 ]);
957+ read_key ("ECC" , x , buf [0 ], & w );
958+
959+ DO (ecc_import (buf [0 ], w , & key ));
974960
975961 t2 = 0 ;
976962 for (y = 0 ; y < 256 ; y ++ ) {
977963 t_start ();
978964 t1 = t_read ();
979965 z = sizeof (buf [1 ]);
980- if ((err = ecc_encrypt_key (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ),
981- & key )) != CRYPT_OK ) {
982- 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 ));
983- exit (EXIT_FAILURE );
984- }
966+ DO (ecc_encrypt_key (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng , find_prng ("yarrow" ), find_hash ("sha1" ),
967+ & key ));
985968 t1 = t_read () - t1 ;
986969 t2 += t1 ;
987970#ifdef LTC_PROFILE
@@ -997,10 +980,7 @@ static void time_ecc(void)
997980 t_start ();
998981 t1 = t_read ();
999982 w = 20 ;
1000- if ((err = ecc_decrypt_key (buf [1 ], z , buf [0 ], & w , & key )) != CRYPT_OK ) {
1001- 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 ));
1002- exit (EXIT_FAILURE );
1003- }
983+ DO (ecc_decrypt_key (buf [1 ], z , buf [0 ], & w , & key ));
1004984 t1 = t_read () - t1 ;
1005985 t2 += t1 ;
1006986#ifdef LTC_PROFILE
@@ -1016,11 +996,8 @@ static void time_ecc(void)
1016996 t_start ();
1017997 t1 = t_read ();
1018998 z = sizeof (buf [1 ]);
1019- if ((err = ecc_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng ,
1020- find_prng ("yarrow" ), & key )) != CRYPT_OK ) {
1021- 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 ));
1022- exit (EXIT_FAILURE );
1023- }
999+ DO (ecc_sign_hash (buf [0 ], 20 , buf [1 ], & z , & yarrow_prng ,
1000+ find_prng ("yarrow" ), & key ));
10241001 t1 = t_read () - t1 ;
10251002 t2 += t1 ;
10261003#ifdef LTC_PROFILE
@@ -1035,10 +1012,7 @@ static void time_ecc(void)
10351012 for (y = 0 ; y < 256 ; y ++ ) {
10361013 t_start ();
10371014 t1 = t_read ();
1038- if ((err = ecc_verify_hash (buf [1 ], z , buf [0 ], 20 , & stat , & key )) != CRYPT_OK ) {
1039- 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 ));
1040- exit (EXIT_FAILURE );
1041- }
1015+ DO (ecc_verify_hash (buf [1 ], z , buf [0 ], 20 , & stat , & key ));
10421016 if (stat == 0 ) {
10431017 fprintf (stderr , "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n" , x , y );
10441018 exit (EXIT_FAILURE );
@@ -1064,6 +1038,28 @@ static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
10641038/* generate fresh PKA keys for the timing operations */
10651039#if defined(LTC_MRSA ) || defined(LTC_MECC )
10661040
1041+ static void read_key (const char * alg , unsigned long sz , void * buf , unsigned long * l )
1042+ {
1043+ char name [PATH_MAX ];
1044+ FILE * f ;
1045+ size_t n ;
1046+
1047+ snprintf (name , sizeof (name ) - 1 , "demos/keys/%s-%lu.privkey" , alg , sz );
1048+ f = fopen (name , "rb" );
1049+ if (f == NULL ) {
1050+ fprintf (stderr , "can't open %s" , name );
1051+ exit (EXIT_FAILURE );
1052+ }
1053+ n = fread (buf , 1 , * l , f );
1054+ if (feof (f )) {
1055+ * l = n ;
1056+ } else if (ferror (f )) {
1057+ fprintf (stderr , "reading of %s errored" , name );
1058+ exit (EXIT_FAILURE );
1059+ }
1060+ fclose (f );
1061+ }
1062+
10671063static void write_key (const char * alg , unsigned long sz , struct list * elmnt , void * buf , unsigned long l )
10681064{
10691065 char name [PATH_MAX ];
0 commit comments