Skip to content

Commit f171289

Browse files
committed
generate reference-keys for timing
1 parent f3e1575 commit f171289

12 files changed

+154
-25
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/** export-subst
66

77
/tests/test.key -text
8+
*.privkey binary

demos/keys/ECC-112.privkey

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
07� ;4��Q&�EQ�V��j�3�f�@��D�
2+
�'�b�0E=�@�G

demos/keys/ECC-128.privkey

66 Bytes
Binary file not shown.

demos/keys/ECC-160.privkey

77 Bytes
Binary file not shown.

demos/keys/ECC-192.privkey

88 Bytes
Binary file not shown.

demos/keys/ECC-224.privkey

101 Bytes
Binary file not shown.

demos/keys/ECC-256.privkey

113 Bytes
Binary file not shown.

demos/keys/ECC-384.privkey

161 Bytes
Binary file not shown.

demos/keys/RSA-2048.privkey

1.16 KB
Binary file not shown.

demos/keys/RSA-4096.privkey

2.29 KB
Binary file not shown.

demos/keys/RSA-8192.privkey

4.54 KB
Binary file not shown.

demos/timing.c

+151-25
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@
44

55
#if defined(_WIN32)
66
#define PRI64 "I64d"
7+
#include <windows.h>
8+
#ifndef PATH_MAX
9+
#define PATH_MAX MAX_PATH
10+
#endif
711
#else
812
#define PRI64 "ll"
913
#endif
1014

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+
1124
static prng_state yarrow_prng;
1225

1326
/* timing */
@@ -896,50 +909,51 @@ static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
896909
#endif
897910

898911
#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[] = {
908913
#ifdef LTC_ECC_SECP112R1
909-
112/8,
914+
112,
910915
#endif
911916
#ifdef LTC_ECC_SECP128R1
912-
128/8,
917+
128,
913918
#endif
914919
#ifdef LTC_ECC_SECP160R1
915-
160/8,
920+
160,
916921
#endif
917922
#ifdef LTC_ECC_SECP192R1
918-
192/8,
923+
192,
919924
#endif
920925
#ifdef LTC_ECC_SECP224R1
921-
224/8,
926+
224,
922927
#endif
923928
#ifdef LTC_ECC_SECP256R1
924-
256/8,
929+
256,
925930
#endif
926931
#ifdef LTC_ECC_SECP384R1
927-
384/8,
932+
384,
928933
#endif
929934
#ifdef LTC_ECC_SECP512R1
930-
521/8,
935+
521,
931936
#endif
932937
100000};
933938

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+
934948
if (ltc_mp.name == NULL) return;
935949

936950
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]) {
938952
t2 = 0;
939953
for (y = 0; y < 256; y++) {
940954
t_start();
941955
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) {
943957
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));
944958
exit(EXIT_FAILURE);
945959
}
@@ -956,7 +970,7 @@ static void time_ecc(void)
956970
}
957971
}
958972
t2 >>= 8;
959-
print_csv("ECC", "make_key", x*8, t2);
973+
print_csv("ECC", "make_key", x, t2);
960974

961975
t2 = 0;
962976
for (y = 0; y < 256; y++) {
@@ -976,7 +990,7 @@ static void time_ecc(void)
976990
#endif
977991
}
978992
t2 >>= 8;
979-
print_csv("ECC", "encrypt_key", x*8, t2);
993+
print_csv("ECC", "encrypt_key", x, t2);
980994

981995
t2 = 0;
982996
for (y = 0; y < 256; y++) {
@@ -995,7 +1009,7 @@ static void time_ecc(void)
9951009
#endif
9961010
}
9971011
t2 >>= 8;
998-
print_csv("ECC", "decrypt_key", x*8, t2);
1012+
print_csv("ECC", "decrypt_key", x, t2);
9991013

10001014
t2 = 0;
10011015
for (y = 0; y < 256; y++) {
@@ -1015,7 +1029,7 @@ static void time_ecc(void)
10151029
#endif
10161030
}
10171031
t2 >>= 8;
1018-
print_csv("ECC", "sign_hash", x*8, t2);
1032+
print_csv("ECC", "sign_hash", x, t2);
10191033

10201034
t2 = 0;
10211035
for (y = 0; y < 256; y++) {
@@ -1026,7 +1040,7 @@ static void time_ecc(void)
10261040
exit(EXIT_FAILURE);
10271041
}
10281042
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);
10301044
exit(EXIT_FAILURE);
10311045
}
10321046
t1 = t_read() - t1;
@@ -1037,7 +1051,7 @@ static void time_ecc(void)
10371051
#endif
10381052
}
10391053
t2 >>= 8;
1040-
print_csv("ECC", "verify_hash", x*8, t2);
1054+
print_csv("ECC", "verify_hash", x, t2);
10411055

10421056
ecc_free(&key);
10431057
}
@@ -1046,6 +1060,117 @@ static void time_ecc(void)
10461060
static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
10471061
#endif
10481062

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+
10491174
static void time_macs_(unsigned long MAC_SIZE)
10501175
{
10511176
#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
13821507
LTC_TEST_FN(time_dsa),
13831508
LTC_TEST_FN(time_ecc),
13841509
LTC_TEST_FN(time_dh),
1510+
LTC_TEST_FN(time_generate_keys),
13851511
};
13861512
char *single_test = NULL;
13871513
unsigned int i;

0 commit comments

Comments
 (0)