removed trailing spaces ([[:space:]]+$)
[strongswan.git] / scripts / dh_speed.c
1
2 #include <stdio.h>
3 #include <time.h>
4 #include <library.h>
5 #include <debug.h>
6 #include <crypto/diffie_hellman.h>
7
8 static void usage()
9 {
10 printf("usage: dh_speed plugins rounds group1 [group2 [...]]\n");
11 exit(1);
12 }
13
14 struct {
15 char *name;
16 diffie_hellman_group_t group;
17 } groups[] = {
18 {"modp768", MODP_768_BIT},
19 {"modp1024", MODP_1024_BIT},
20 {"modp1536", MODP_1536_BIT},
21 {"modp2048", MODP_2048_BIT},
22 {"modp3072", MODP_3072_BIT},
23 {"modp4096", MODP_4096_BIT},
24 {"modp6144", MODP_6144_BIT},
25 {"modp8192", MODP_8192_BIT},
26 {"ecp256", ECP_256_BIT},
27 {"ecp384", ECP_384_BIT},
28 {"ecp521", ECP_521_BIT},
29 {"ecp192", ECP_192_BIT},
30 {"ecp224", ECP_224_BIT},
31 };
32
33 static void start_timing(struct timespec *start)
34 {
35 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
36 }
37
38 static double end_timing(struct timespec *start)
39 {
40 struct timespec end;
41
42 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
43 return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
44 (end.tv_sec - start->tv_sec) * 1.0;
45 }
46
47 static void run_test(diffie_hellman_group_t group, int rounds)
48 {
49 diffie_hellman_t *l[rounds], *r;
50 chunk_t chunk;
51 struct timespec timing;
52 int round;
53
54 r = lib->crypto->create_dh(lib->crypto, group);
55 if (!r)
56 {
57 printf("skipping %N, not supported\n",
58 diffie_hellman_group_names, group);
59 return;
60 }
61
62 printf("%N:\t",
63 diffie_hellman_group_names, group);
64
65 start_timing(&timing);
66 for (round = 0; round < rounds; round++)
67 {
68 l[round] = lib->crypto->create_dh(lib->crypto, group);
69 }
70 printf("A = g^a/s: %8.1f", rounds / end_timing(&timing));
71
72 for (round = 0; round < rounds; round++)
73 {
74 l[round]->get_my_public_value(l[round], &chunk);
75 r->set_other_public_value(r, chunk);
76 chunk_free(&chunk);
77 }
78
79 r->get_my_public_value(r, &chunk);
80 start_timing(&timing);
81 for (round = 0; round < rounds; round++)
82 {
83 l[round]->set_other_public_value(l[round], chunk);
84 }
85 printf(" | S = B^a/s: %8.1f\n", rounds / end_timing(&timing));
86 chunk_free(&chunk);
87
88 for (round = 0; round < rounds; round++)
89 {
90 l[round]->destroy(l[round]);
91 }
92 r->destroy(r);
93 }
94
95 int main(int argc, char *argv[])
96 {
97 int rounds, i, j;
98
99 if (argc < 4)
100 {
101 usage();
102 }
103
104 library_init(STRONGSWAN_CONF);
105 lib->plugins->load(lib->plugins, IPSEC_PLUGINDIR, argv[1]);
106 atexit(library_deinit);
107
108 rounds = atoi(argv[2]);
109
110 for (i = 3; i < argc; i++)
111 {
112 bool found = FALSE;
113
114 for (j = 0; j < countof(groups); j++)
115 {
116 if (streq(groups[j].name, argv[i]))
117 {
118 run_test(groups[j].group, rounds);
119 found = TRUE;
120 }
121 }
122 if (!found)
123 {
124 printf("group %s not found\n", argv[i]);
125 }
126 }
127 return 0;
128 }
129