@@ -67,6 +67,9 @@ static void tally_results(int type)
67
67
}
68
68
}
69
69
}
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
70
73
71
74
#define CSV_SEP ","
72
75
#define OUTFILE stdout
@@ -737,25 +740,22 @@ static void time_rsa(void)
737
740
{
738
741
rsa_key key ;
739
742
ulong64 t1 , t2 ;
740
- unsigned char buf [2 ][2048 ] =
743
+ unsigned char buf [2 ][4096 ] =
741
744
{ 0 };
742
745
unsigned long x , y , z , zzz ;
743
- int err , zz ;
746
+ int zz ;
744
747
745
748
if (ltc_mp .name == NULL ) return ;
746
749
747
750
print_csv_header ("keysize" , NULL );
748
751
for (x = 2048 ; x <= 8192 ; x <<= 1 ) {
749
752
753
+ #ifndef TIMING_DONT_MAKE_KEY
750
754
t2 = 0 ;
751
755
for (y = 0 ; y < 4 ; y ++ ) {
752
756
t_start ();
753
757
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 ));
759
759
t1 = t_read () - t1 ;
760
760
t2 += t1 ;
761
761
@@ -768,19 +768,20 @@ static void time_rsa(void)
768
768
}
769
769
t2 >>= 2 ;
770
770
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 ));
771
777
772
778
t2 = 0 ;
773
779
for (y = 0 ; y < 256 ; y ++ ) {
774
780
t_start ();
775
781
t1 = t_read ();
776
782
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 ));
784
785
t1 = t_read () - t1 ;
785
786
t2 += t1 ;
786
787
#ifdef LTC_PROFILE
@@ -796,13 +797,8 @@ static void time_rsa(void)
796
797
t_start ();
797
798
t1 = t_read ();
798
799
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 ));
806
802
t1 = t_read () - t1 ;
807
803
t2 += t1 ;
808
804
#ifdef LTC_PROFILE
@@ -818,11 +814,7 @@ static void time_rsa(void)
818
814
t_start ();
819
815
t1 = t_read ();
820
816
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 ));
826
818
t1 = t_read () - t1 ;
827
819
t2 += t1 ;
828
820
#ifdef LTC_PROFILE
@@ -838,11 +830,7 @@ static void time_rsa(void)
838
830
int stat ;
839
831
t_start ();
840
832
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 ));
846
834
if (stat == 0 ) {
847
835
fprintf (stderr , "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n" , x , y );
848
836
exit (EXIT_FAILURE );
@@ -870,7 +858,6 @@ static void time_dh(void)
870
858
dh_key key ;
871
859
ulong64 t1 , t2 ;
872
860
unsigned long i , x , y ;
873
- int err ;
874
861
static unsigned long sizes [] = {768 /8 , 1024 /8 , 1536 /8 , 2048 /8 ,
875
862
#ifndef TFM_DESC
876
863
3072 /8 , 4096 /8 , 6144 /8 , 8192 /8 ,
@@ -884,17 +871,11 @@ static void time_dh(void)
884
871
for (x = sizes [i = 0 ]; x < 100000 ; x = sizes [++ i ]) {
885
872
t2 = 0 ;
886
873
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 ));
891
875
892
876
t_start ();
893
877
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 ));
898
879
t1 = t_read () - t1 ;
899
880
t2 += t1 ;
900
881
@@ -943,20 +924,19 @@ static void time_ecc(void)
943
924
ulong64 t1 , t2 ;
944
925
unsigned char buf [2 ][256 ] = { 0 };
945
926
unsigned long i , w , x , y , z ;
946
- int err , stat ;
927
+ int stat ;
947
928
948
929
if (ltc_mp .name == NULL ) return ;
949
930
950
931
print_csv_header ("keysize" , NULL );
951
932
for (x = ecc_key_sizes [i = 0 ]; x < 100000 ; x = ecc_key_sizes [++ i ]) {
933
+
934
+ #ifndef TIMING_DONT_MAKE_KEY
952
935
t2 = 0 ;
953
936
for (y = 0 ; y < 256 ; y ++ ) {
954
937
t_start ();
955
938
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 ));
960
940
t1 = t_read () - t1 ;
961
941
t2 += t1 ;
962
942
@@ -971,17 +951,20 @@ static void time_ecc(void)
971
951
}
972
952
t2 >>= 8 ;
973
953
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 ));
974
960
975
961
t2 = 0 ;
976
962
for (y = 0 ; y < 256 ; y ++ ) {
977
963
t_start ();
978
964
t1 = t_read ();
979
965
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 ));
985
968
t1 = t_read () - t1 ;
986
969
t2 += t1 ;
987
970
#ifdef LTC_PROFILE
@@ -997,10 +980,7 @@ static void time_ecc(void)
997
980
t_start ();
998
981
t1 = t_read ();
999
982
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 ));
1004
984
t1 = t_read () - t1 ;
1005
985
t2 += t1 ;
1006
986
#ifdef LTC_PROFILE
@@ -1016,11 +996,8 @@ static void time_ecc(void)
1016
996
t_start ();
1017
997
t1 = t_read ();
1018
998
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 ));
1024
1001
t1 = t_read () - t1 ;
1025
1002
t2 += t1 ;
1026
1003
#ifdef LTC_PROFILE
@@ -1035,10 +1012,7 @@ static void time_ecc(void)
1035
1012
for (y = 0 ; y < 256 ; y ++ ) {
1036
1013
t_start ();
1037
1014
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 ));
1042
1016
if (stat == 0 ) {
1043
1017
fprintf (stderr , "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n" , x , y );
1044
1018
exit (EXIT_FAILURE );
@@ -1064,6 +1038,28 @@ static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
1064
1038
/* generate fresh PKA keys for the timing operations */
1065
1039
#if defined(LTC_MRSA ) || defined(LTC_MECC )
1066
1040
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
+
1067
1063
static void write_key (const char * alg , unsigned long sz , struct list * elmnt , void * buf , unsigned long l )
1068
1064
{
1069
1065
char name [PATH_MAX ];
0 commit comments