Use Ed25519 keys.
[oweals/tinc.git] / src / sptps_speed.c
1 /*
2     sptps_speed.c -- SPTPS benchmark
3     Copyright (C) 2013 Guus Sliepen <guus@tinc-vpn.org>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <poll.h>
23
24 #include "crypto.h"
25 #include "ecdh.h"
26 #include "ecdsa.h"
27 #include "ecdsagen.h"
28 #include "sptps.h"
29
30 // Symbols necessary to link with logger.o
31 bool send_request(void *c, const char *msg, ...) { return false; }
32 struct list_t *connection_list = NULL;
33 bool send_meta(void *c, const char *msg , int len) { return false; }
34 char *logfilename = NULL;
35 struct timeval now;
36
37 static bool send_data(void *handle, uint8_t type, const char *data, size_t len) {
38         int fd = *(int *)handle;
39         send(fd, data, len, 0);
40         return true;
41 }
42
43 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
44         return true;
45 }
46
47 static void receive_data(sptps_t *sptps) {
48         char buf[4096];
49         int fd = *(int *)sptps->handle;
50         size_t len = recv(fd, buf, sizeof buf, 0);
51         if(!sptps_receive_data(sptps, buf, len))
52                 abort();
53 }
54
55 struct timespec start;
56 struct timespec end;
57 double elapsed;
58 double rate;
59 unsigned int count;
60
61 static void clock_start() {
62         count = 0;
63         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
64 }
65
66 static bool clock_countto(double seconds) {
67         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
68         elapsed = end.tv_sec + end.tv_nsec * 1e-9 - start.tv_sec - start.tv_nsec * 1e-9;
69         if(elapsed < seconds)
70                 return ++count;
71
72         rate = count / elapsed;
73         return false;
74 }
75
76 int main(int argc, char *argv[]) {
77         ecdsa_t *key1, *key2;
78         ecdh_t *ecdh1, *ecdh2;
79         sptps_t sptps1, sptps2;
80         char buf1[4096], buf2[4096], buf3[4096];
81         double duration = argc > 1 ? atof(argv[1]) : 10;
82
83         crypto_init();
84
85         randomize(buf1, sizeof buf1);
86         randomize(buf2, sizeof buf2);
87         randomize(buf3, sizeof buf3);
88
89         // Key generation
90
91         fprintf(stderr, "Generating keys for %lg seconds: ", duration);
92         for(clock_start(); clock_countto(duration);)
93                 ecdsa_free(ecdsa_generate());
94         fprintf(stderr, "%17.2lf op/s\n", rate);
95
96         key1 = ecdsa_generate();
97         key2 = ecdsa_generate();
98
99         // ECDSA signatures
100
101         fprintf(stderr, "ECDSA sign for %lg seconds: ", duration);
102         for(clock_start(); clock_countto(duration);)
103                 ecdsa_sign(key1, buf1, 256, buf2);
104         fprintf(stderr, "%22.2lf op/s\n", rate);
105
106         fprintf(stderr, "ECDSA verify for %lg seconds: ", duration);
107         for(clock_start(); clock_countto(duration);)
108                 ecdsa_verify(key1, buf1, 256, buf2);
109         fprintf(stderr, "%20.2lf op/s\n", rate);
110
111         ecdh1 = ecdh_generate_public(buf1);
112         fprintf(stderr, "ECDH for %lg seconds: ", duration);
113         for(clock_start(); clock_countto(duration);) {
114                 ecdh2 = ecdh_generate_public(buf2);
115                 ecdh_compute_shared(ecdh2, buf1, buf3);
116         }
117         fprintf(stderr, "%28.2lf op/s\n", rate);
118         ecdh_free(ecdh1);
119
120         // SPTPS authentication phase
121
122         int fd[2];
123         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
124                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
125                 return 1;
126         }
127
128         struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
129
130         fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
131         for(clock_start(); clock_countto(duration);) {
132                 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
133                 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
134                 while(poll(pfd, 2, 0)) {
135                         if(pfd[0].revents)
136                                 receive_data(&sptps1);
137                         if(pfd[1].revents)
138                                 receive_data(&sptps2);
139                 }
140                 sptps_stop(&sptps1);
141                 sptps_stop(&sptps2);
142         }
143         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
144
145         // SPTPS data
146
147         sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
148         sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
149         while(poll(pfd, 2, 0)) {
150                 if(pfd[0].revents)
151                         receive_data(&sptps1);
152                 if(pfd[1].revents)
153                         receive_data(&sptps2);
154         }
155         fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
156         for(clock_start(); clock_countto(duration);) {
157                 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
158                         abort();
159                 receive_data(&sptps2);
160         }
161         rate *= 2 * 1451 * 8;
162         if(rate > 1e9)
163                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
164         else if(rate > 1e6)
165                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
166         else if(rate > 1e3)
167                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
168         sptps_stop(&sptps1);
169         sptps_stop(&sptps2);
170
171         // SPTPS datagram authentication phase
172
173         close(fd[0]);
174         close(fd[1]);
175
176         if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
177                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
178                 return 1;
179         }
180
181         fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
182         for(clock_start(); clock_countto(duration);) {
183                 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
184                 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
185                 while(poll(pfd, 2, 0)) {
186                         if(pfd[0].revents)
187                                 receive_data(&sptps1);
188                         if(pfd[1].revents)
189                                 receive_data(&sptps2);
190                 }
191                 sptps_stop(&sptps1);
192                 sptps_stop(&sptps2);
193         }
194         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
195
196         // SPTPS datagram data
197
198         sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
199         sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
200         while(poll(pfd, 2, 0)) {
201                 if(pfd[0].revents)
202                         receive_data(&sptps1);
203                 if(pfd[1].revents)
204                         receive_data(&sptps2);
205         }
206         fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
207         for(clock_start(); clock_countto(duration);) {
208                 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
209                         abort();
210                 receive_data(&sptps2);
211         }
212         rate *= 2 * 1451 * 8;
213         if(rate > 1e9)
214                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
215         else if(rate > 1e6)
216                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
217         else if(rate > 1e3)
218                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
219         sptps_stop(&sptps1);
220         sptps_stop(&sptps2);
221
222         // Clean up
223
224         close(fd[0]);
225         close(fd[1]);
226         ecdsa_free(key1);
227         ecdsa_free(key2);
228         crypto_exit();
229
230         return 0;
231 }