4
4
5
5
#if defined(_WIN32 )
6
6
#define PRI64 "I64d"
7
+ #include <windows.h>
8
+ #ifndef PATH_MAX
9
+ #define PATH_MAX MAX_PATH
10
+ #endif
7
11
#else
8
12
#define PRI64 "ll"
9
13
#endif
10
14
15
+
16
+ #define DO (x ) do{ \
17
+ int err; \
18
+ if ((err = (x)) != CRYPT_OK) { \
19
+ fprintf(stderr, "\n\n " #x " says %s!\n", error_to_string(err)); \
20
+ exit(EXIT_FAILURE); \
21
+ } \
22
+ } while(0)
23
+
11
24
static prng_state yarrow_prng ;
12
25
13
26
/* timing */
@@ -896,50 +909,51 @@ static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
896
909
#endif
897
910
898
911
#if defined(LTC_MECC )
899
- /* time various ECC operations */
900
- static void time_ecc (void )
901
- {
902
- ecc_key key ;
903
- ulong64 t1 , t2 ;
904
- unsigned char buf [2 ][256 ] = { 0 };
905
- unsigned long i , w , x , y , z ;
906
- int err , stat ;
907
- static unsigned long sizes [] = {
912
+ static unsigned long ecc_key_sizes [] = {
908
913
#ifdef LTC_ECC_SECP112R1
909
- 112 / 8 ,
914
+ 112 ,
910
915
#endif
911
916
#ifdef LTC_ECC_SECP128R1
912
- 128 / 8 ,
917
+ 128 ,
913
918
#endif
914
919
#ifdef LTC_ECC_SECP160R1
915
- 160 / 8 ,
920
+ 160 ,
916
921
#endif
917
922
#ifdef LTC_ECC_SECP192R1
918
- 192 / 8 ,
923
+ 192 ,
919
924
#endif
920
925
#ifdef LTC_ECC_SECP224R1
921
- 224 / 8 ,
926
+ 224 ,
922
927
#endif
923
928
#ifdef LTC_ECC_SECP256R1
924
- 256 / 8 ,
929
+ 256 ,
925
930
#endif
926
931
#ifdef LTC_ECC_SECP384R1
927
- 384 / 8 ,
932
+ 384 ,
928
933
#endif
929
934
#ifdef LTC_ECC_SECP512R1
930
- 521 / 8 ,
935
+ 521 ,
931
936
#endif
932
937
100000 };
933
938
939
+ /* time various ECC operations */
940
+ static void time_ecc (void )
941
+ {
942
+ ecc_key key ;
943
+ ulong64 t1 , t2 ;
944
+ unsigned char buf [2 ][256 ] = { 0 };
945
+ unsigned long i , w , x , y , z ;
946
+ int err , stat ;
947
+
934
948
if (ltc_mp .name == NULL ) return ;
935
949
936
950
print_csv_header ("keysize" , NULL );
937
- for (x = sizes [i = 0 ]; x < 100000 ; x = sizes [++ i ]) {
951
+ for (x = ecc_key_sizes [i = 0 ]; x < 100000 ; x = ecc_key_sizes [++ i ]) {
938
952
t2 = 0 ;
939
953
for (y = 0 ; y < 256 ; y ++ ) {
940
954
t_start ();
941
955
t1 = t_read ();
942
- if ((err = ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x , & key )) != CRYPT_OK ) {
956
+ if ((err = ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x / 8 , & key )) != CRYPT_OK ) {
943
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 ));
944
958
exit (EXIT_FAILURE );
945
959
}
@@ -956,7 +970,7 @@ static void time_ecc(void)
956
970
}
957
971
}
958
972
t2 >>= 8 ;
959
- print_csv ("ECC" , "make_key" , x * 8 , t2 );
973
+ print_csv ("ECC" , "make_key" , x , t2 );
960
974
961
975
t2 = 0 ;
962
976
for (y = 0 ; y < 256 ; y ++ ) {
@@ -976,7 +990,7 @@ static void time_ecc(void)
976
990
#endif
977
991
}
978
992
t2 >>= 8 ;
979
- print_csv ("ECC" , "encrypt_key" , x * 8 , t2 );
993
+ print_csv ("ECC" , "encrypt_key" , x , t2 );
980
994
981
995
t2 = 0 ;
982
996
for (y = 0 ; y < 256 ; y ++ ) {
@@ -995,7 +1009,7 @@ static void time_ecc(void)
995
1009
#endif
996
1010
}
997
1011
t2 >>= 8 ;
998
- print_csv ("ECC" , "decrypt_key" , x * 8 , t2 );
1012
+ print_csv ("ECC" , "decrypt_key" , x , t2 );
999
1013
1000
1014
t2 = 0 ;
1001
1015
for (y = 0 ; y < 256 ; y ++ ) {
@@ -1015,7 +1029,7 @@ static void time_ecc(void)
1015
1029
#endif
1016
1030
}
1017
1031
t2 >>= 8 ;
1018
- print_csv ("ECC" , "sign_hash" , x * 8 , t2 );
1032
+ print_csv ("ECC" , "sign_hash" , x , t2 );
1019
1033
1020
1034
t2 = 0 ;
1021
1035
for (y = 0 ; y < 256 ; y ++ ) {
@@ -1026,7 +1040,7 @@ static void time_ecc(void)
1026
1040
exit (EXIT_FAILURE );
1027
1041
}
1028
1042
if (stat == 0 ) {
1029
- fprintf (stderr , "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n" , x * 8 , y );
1043
+ fprintf (stderr , "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n" , x , y );
1030
1044
exit (EXIT_FAILURE );
1031
1045
}
1032
1046
t1 = t_read () - t1 ;
@@ -1037,7 +1051,7 @@ static void time_ecc(void)
1037
1051
#endif
1038
1052
}
1039
1053
t2 >>= 8 ;
1040
- print_csv ("ECC" , "verify_hash" , x * 8 , t2 );
1054
+ print_csv ("ECC" , "verify_hash" , x , t2 );
1041
1055
1042
1056
ecc_free (& key );
1043
1057
}
@@ -1046,6 +1060,117 @@ static void time_ecc(void)
1046
1060
static void time_ecc (void ) { fprintf (stderr , "NO ECC\n" ); }
1047
1061
#endif
1048
1062
1063
+
1064
+ /* generate fresh PKA keys for the timing operations */
1065
+ #if defined(LTC_MRSA ) || defined(LTC_MECC )
1066
+
1067
+ static void write_key (const char * alg , unsigned long sz , struct list * elmnt , void * buf , unsigned long l )
1068
+ {
1069
+ char name [PATH_MAX ];
1070
+ FILE * f ;
1071
+
1072
+ snprintf (name , sizeof (name ) - 1 , "demos/keys/%s-%lu.privkey" , alg , sz );
1073
+ fprintf (stderr , "%s: Writing key %d which required %" PRI64 "u ticks to %s\n" , alg , elmnt -> id , elmnt -> avg , name );
1074
+ f = fopen (name , "wb+" );
1075
+ if (f == NULL ) {
1076
+ fprintf (stderr , "can't open %s" , name );
1077
+ exit (EXIT_FAILURE );
1078
+ }
1079
+ if (fwrite (buf , l , 1 , f ) != 1 ) {
1080
+ fprintf (stderr , "can't write to %s" , name );
1081
+ exit (EXIT_FAILURE );
1082
+ }
1083
+ fclose (f );
1084
+ }
1085
+
1086
+ static void time_generate_keys (void )
1087
+ {
1088
+ union
1089
+ {
1090
+ #if defined(LTC_MRSA )
1091
+ rsa_key rsa ;
1092
+ #endif
1093
+ #if defined(LTC_MECC )
1094
+ ecc_key ecc ;
1095
+ #endif
1096
+ } key [25 ];
1097
+ ulong64 t1 = 0 ;
1098
+ unsigned char buf [8192 ] = { 0 }, op_buf [8192 / 8 ];
1099
+ unsigned long n , x , y , z , l ;
1100
+ const unsigned median = ((sizeof (key ) / sizeof (key [0 ])) / 2 );
1101
+
1102
+ if (ltc_mp .name == NULL ) return ;
1103
+
1104
+ print_csv_header ("keysize" , NULL );
1105
+ #if defined(LTC_MRSA )
1106
+ for (x = 2048 ; x <= 8192 ; x <<= 1 ) {
1107
+
1108
+ for (y = 0 ; y < sizeof (key ) / sizeof (key [0 ]); y ++ ) {
1109
+ DO (rsa_make_key (& yarrow_prng , find_prng ("yarrow" ), x / 8 , 65537 , & key [y ].rsa ));
1110
+ t_start ();
1111
+ for (z = 0 ; z < 512 / (x / 1024 ); ++ z ) {
1112
+ if (z == 8 ) {
1113
+ t_start ();
1114
+ t1 = t_read ();
1115
+ }
1116
+ l = sizeof (op_buf );
1117
+ op_buf [0 ] = 0 ;
1118
+ op_buf [1 ] = 1 ;
1119
+ op_buf [2 ] = 0 ;
1120
+ DO (rsa_exptmod (op_buf , x / 8 , op_buf , & l , PK_PUBLIC , & key [y ].rsa ));
1121
+ }
1122
+ t1 = t_read () - t1 ;
1123
+ results [y ].id = y ;
1124
+ results [y ].avg = t1 ;
1125
+ print_csv ("RSA" , "exptmod" , x , t1 );
1126
+ }
1127
+
1128
+ qsort (results , sizeof (key ) / sizeof (key [0 ]), sizeof (struct list ), & sorter );
1129
+
1130
+ l = sizeof (buf );
1131
+ DO (rsa_export (buf , & l , PK_PRIVATE , & key [results [median ].id ].rsa ));
1132
+
1133
+ write_key ("RSA" , x , & results [median ], buf , l );
1134
+
1135
+ for (y = 0 ; y < sizeof (key ) / sizeof (key [0 ]); y ++ ) {
1136
+ rsa_free (& key [y ].rsa );
1137
+ }
1138
+ }
1139
+ #endif
1140
+ #if defined(LTC_MECC )
1141
+ for (x = ecc_key_sizes [n = 0 ]; x < 100000 ; x = ecc_key_sizes [++ n ]) {
1142
+ for (y = 0 ; y < sizeof (key ) / sizeof (key [0 ]); y ++ ) {
1143
+ DO (ecc_make_key (& yarrow_prng , find_prng ("yarrow" ), x /8 , & key [y ].ecc ));
1144
+ for (z = 0 ; z < 256 ; z ++ ) {
1145
+ if (z == 8 ) {
1146
+ t_start ();
1147
+ t1 = t_read ();
1148
+ }
1149
+ l = sizeof (op_buf );
1150
+ DO (ecc_shared_secret (& key [y ].ecc , & key [y ].ecc , op_buf , & l ));
1151
+ }
1152
+ t1 = t_read () - t1 ;
1153
+ results [y ].id = y ;
1154
+ results [y ].avg = t1 ;
1155
+ print_csv ("ECC" , "shared_secret" , x , t1 );
1156
+ }
1157
+
1158
+ qsort (results , sizeof (key ) / sizeof (key [0 ]), sizeof (struct list ), & sorter );
1159
+
1160
+ l = sizeof (buf );
1161
+ DO (ecc_export (buf , & l , PK_PRIVATE , & key [results [median ].id ].ecc ));
1162
+
1163
+ write_key ("ECC" , x , & results [median ], buf , l );
1164
+
1165
+ for (y = 0 ; y < sizeof (key ) / sizeof (key [0 ]); y ++ ) {
1166
+ ecc_free (& key [y ].ecc );
1167
+ }
1168
+ }
1169
+ #endif
1170
+ }
1171
+ #endif
1172
+
1173
+
1049
1174
static void time_macs_ (unsigned long MAC_SIZE )
1050
1175
{
1051
1176
#if defined(LTC_OMAC ) || defined(LTC_XCBC ) || defined(LTC_F9_MODE ) || defined(LTC_PMAC ) || defined(LTC_PELICAN ) || defined(LTC_HMAC )
@@ -1382,6 +1507,7 @@ const struct
1382
1507
LTC_TEST_FN (time_dsa ),
1383
1508
LTC_TEST_FN (time_ecc ),
1384
1509
LTC_TEST_FN (time_dh ),
1510
+ LTC_TEST_FN (time_generate_keys ),
1385
1511
};
1386
1512
char * single_test = NULL ;
1387
1513
unsigned int i ;
0 commit comments