@@ -73,6 +73,9 @@ static void tally_results(int type)
73
73
}
74
74
}
75
75
}
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
76
79
77
80
#define CSV_SEP ","
78
81
#define OUTFILE stdout
@@ -739,25 +742,22 @@ static void time_rsa(void)
739
742
{
740
743
rsa_key key ;
741
744
ulong64 t1 , t2 ;
742
- unsigned char buf [2 ][2048 ] =
745
+ unsigned char buf [2 ][4096 ] =
743
746
{ 0 };
744
747
unsigned long x , y , z , zzz ;
745
- int err , zz ;
748
+ int zz ;
746
749
747
750
if (ltc_mp .name == NULL ) return ;
748
751
749
752
print_csv_header ("keysize" , NULL );
750
753
for (x = 2048 ; x <= 8192 ; x <<= 1 ) {
751
754
755
+ #ifndef TIMING_DONT_MAKE_KEY
752
756
t2 = 0 ;
753
757
for (y = 0 ; y < 4 ; y ++ ) {
754
758
t_start ();
755
759
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 ));
761
761
t1 = t_read () - t1 ;
762
762
t2 += t1 ;
763
763
@@ -770,19 +770,20 @@ static void time_rsa(void)
770
770
}
771
771
t2 >>= 2 ;
772
772
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 ));
773
779
774
780
t2 = 0 ;
775
781
for (y = 0 ; y < 256 ; y ++ ) {
776
782
t_start ();
777
783
t1 = t_read ();
778
784
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 ));
786
787
t1 = t_read () - t1 ;
787
788
t2 += t1 ;
788
789
#ifdef LTC_PROFILE
@@ -798,13 +799,8 @@ static void time_rsa(void)
798
799
t_start ();
799
800
t1 = t_read ();
800
801
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 ));
808
804
t1 = t_read () - t1 ;
809
805
t2 += t1 ;
810
806
#ifdef LTC_PROFILE
@@ -820,11 +816,7 @@ static void time_rsa(void)
820
816
t_start ();
821
817
t1 = t_read ();
822
818
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 ));
828
820
t1 = t_read () - t1 ;
829
821
t2 += t1 ;
830
822
#ifdef LTC_PROFILE
@@ -840,11 +832,7 @@ static void time_rsa(void)
840
832
int stat ;
841
833
t_start ();
842
834
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 ));
848
836
if (stat == 0 ) {
849
837
fprintf (stderr , "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n" , x , y );
850
838
exit (EXIT_FAILURE );
@@ -872,7 +860,6 @@ static void time_dh(void)
872
860
dh_key key ;
873
861
ulong64 t1 , t2 ;
874
862
unsigned long i , x , y ;
875
- int err ;
876
863
static unsigned long sizes [] = {768 /8 , 1024 /8 , 1536 /8 , 2048 /8 ,
877
864
#ifndef TFM_DESC
878
865
3072 /8 , 4096 /8 , 6144 /8 , 8192 /8 ,
@@ -886,17 +873,11 @@ static void time_dh(void)
886
873
for (x = sizes [i = 0 ]; x < 100000 ; x = sizes [++ i ]) {
887
874
t2 = 0 ;
888
875
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 ));
893
877
894
878
t_start ();
895
879
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 ));
900
881
t1 = t_read () - t1 ;
901
882
t2 += t1 ;
902
883
@@ -945,20 +926,19 @@ static void time_ecc(void)
945
926
ulong64 t1 , t2 ;
946
927
unsigned char buf [2 ][256 ] = { 0 };
947
928
unsigned long i , w , x , y , z ;
948
- int err , stat ;
929
+ int stat ;
949
930
950
931
if (ltc_mp .name == NULL ) return ;
951
932
952
933
print_csv_header ("keysize" , NULL );
953
934
for (x = ecc_key_sizes [i = 0 ]; x < 100000 ; x = ecc_key_sizes [++ i ]) {
935
+
936
+ #ifndef TIMING_DONT_MAKE_KEY
954
937
t2 = 0 ;
955
938
for (y = 0 ; y < 256 ; y ++ ) {
956
939
t_start ();
957
940
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 ));
962
942
t1 = t_read () - t1 ;
963
943
t2 += t1 ;
964
944
@@ -973,17 +953,20 @@ static void time_ecc(void)
973
953
}
974
954
t2 >>= 8 ;
975
955
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 ));
976
962
977
963
t2 = 0 ;
978
964
for (y = 0 ; y < 256 ; y ++ ) {
979
965
t_start ();
980
966
t1 = t_read ();
981
967
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 ));
987
970
t1 = t_read () - t1 ;
988
971
t2 += t1 ;
989
972
#ifdef LTC_PROFILE
@@ -999,10 +982,7 @@ static void time_ecc(void)
999
982
t_start ();
1000
983
t1 = t_read ();
1001
984
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 ));
1006
986
t1 = t_read () - t1 ;
1007
987
t2 += t1 ;
1008
988
#ifdef LTC_PROFILE
@@ -1018,11 +998,8 @@ static void time_ecc(void)
1018
998
t_start ();
1019
999
t1 = t_read ();
1020
1000
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 ));
1026
1003
t1 = t_read () - t1 ;
1027
1004
t2 += t1 ;
1028
1005
#ifdef LTC_PROFILE
@@ -1037,10 +1014,7 @@ static void time_ecc(void)
1037
1014
for (y = 0 ; y < 256 ; y ++ ) {
1038
1015
t_start ();
1039
1016
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 ));
1044
1018
if (stat == 0 ) {
1045
1019
fprintf (stderr , "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n" , x , y );
1046
1020
exit (EXIT_FAILURE );
@@ -1066,6 +1040,28 @@ static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
1066
1040
/* generate fresh PKA keys for the timing operations */
1067
1041
#if defined(LTC_MRSA ) || defined(LTC_MECC )
1068
1042
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
+
1069
1065
static void write_key (const char * alg , unsigned long sz , struct list * elmnt , void * buf , unsigned long l )
1070
1066
{
1071
1067
char name [PATH_MAX ];
0 commit comments