dh-speed: Include the get_my_public_value() call in public exponent timing
[strongswan.git] / scripts / dh_speed.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdio.h>
17 #include <time.h>
18 #include <assert.h>
19 #include <library.h>
20 #include <utils/debug.h>
21 #include <crypto/diffie_hellman.h>
22
23 static void usage()
24 {
25 printf("usage: dh_speed plugins rounds group1 [group2 [...]]\n");
26 exit(1);
27 }
28
29 struct {
30 char *name;
31 diffie_hellman_group_t group;
32 } groups[] = {
33 {"modp768", MODP_768_BIT},
34 {"modp1024", MODP_1024_BIT},
35 {"modp1024s160", MODP_1024_160},
36 {"modp1536", MODP_1536_BIT},
37 {"modp2048", MODP_2048_BIT},
38 {"modp2048s224", MODP_2048_224},
39 {"modp2048s256", MODP_2048_256},
40 {"modp3072", MODP_3072_BIT},
41 {"modp4096", MODP_4096_BIT},
42 {"modp6144", MODP_6144_BIT},
43 {"modp8192", MODP_8192_BIT},
44 {"ecp256", ECP_256_BIT},
45 {"ecp384", ECP_384_BIT},
46 {"ecp521", ECP_521_BIT},
47 {"ecp192", ECP_192_BIT},
48 {"ecp224", ECP_224_BIT},
49 {"curve25519", CURVE_25519},
50 };
51
52 static void start_timing(struct timespec *start)
53 {
54 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
55 }
56
57 static double end_timing(struct timespec *start)
58 {
59 struct timespec end;
60
61 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
62 return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
63 (end.tv_sec - start->tv_sec) * 1.0;
64 }
65
66 static void run_test(diffie_hellman_group_t group, int rounds)
67 {
68 diffie_hellman_t *l[rounds], *r;
69 chunk_t chunk, chunks[rounds];
70 struct timespec timing;
71 int round;
72
73 r = lib->crypto->create_dh(lib->crypto, group);
74 if (!r)
75 {
76 printf("skipping %N, not supported\n",
77 diffie_hellman_group_names, group);
78 return;
79 }
80
81 printf("%N:\t",
82 diffie_hellman_group_names, group);
83
84 start_timing(&timing);
85 for (round = 0; round < rounds; round++)
86 {
87 l[round] = lib->crypto->create_dh(lib->crypto, group);
88 assert(l[round]->get_my_public_value(l[round], &chunks[round]));
89 }
90 printf("A = g^a/s: %8.1f", rounds / end_timing(&timing));
91
92 for (round = 0; round < rounds; round++)
93 {
94 assert(r->set_other_public_value(r, chunks[round]));
95 chunk_free(&chunks[round]);
96 }
97
98 assert(r->get_my_public_value(r, &chunk));
99 start_timing(&timing);
100 for (round = 0; round < rounds; round++)
101 {
102 assert(l[round]->set_other_public_value(l[round], chunk));
103 }
104 printf(" | S = B^a/s: %8.1f\n", rounds / end_timing(&timing));
105 chunk_free(&chunk);
106
107 for (round = 0; round < rounds; round++)
108 {
109 l[round]->destroy(l[round]);
110 }
111 r->destroy(r);
112 }
113
114 int main(int argc, char *argv[])
115 {
116 int rounds, i, j;
117
118 if (argc < 4)
119 {
120 usage();
121 }
122
123 library_init(NULL, "dh_speed");
124 lib->plugins->load(lib->plugins, argv[1]);
125 atexit(library_deinit);
126
127 rounds = atoi(argv[2]);
128
129 for (i = 3; i < argc; i++)
130 {
131 bool found = FALSE;
132
133 for (j = 0; j < countof(groups); j++)
134 {
135 if (streq(groups[j].name, argv[i]))
136 {
137 run_test(groups[j].group, rounds);
138 found = TRUE;
139 }
140 }
141 if (!found)
142 {
143 printf("group %s not found\n", argv[i]);
144 }
145 }
146 return 0;
147 }