Move poly1305_get_tag() into poly1305.c, hide poly1305_init().
[tinc] / src / sptps_speed.c
1 /*
2     sptps_speed.c -- SPTPS benchmark
3     Copyright (C) 2013-2022 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 #include "utils.h"
22
23 #include <poll.h>
24
25 #include "crypto.h"
26 #include "ecdh.h"
27 #include "ecdsa.h"
28 #include "ecdsagen.h"
29 #include "meta.h"
30 #include "protocol.h"
31 #include "sptps.h"
32 #include "random.h"
33
34 // Symbols necessary to link with logger.o
35 bool send_request(struct connection_t *c, const char *msg, ...) {
36         (void)c;
37         (void)msg;
38         return false;
39 }
40
41 list_t connection_list;
42
43 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
44         (void)c;
45         (void)msg;
46         (void)len;
47         return false;
48 }
49 bool do_detach = false;
50 struct timeval now;
51
52 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
53         (void)type;
54         int fd = *(int *)handle;
55         send(fd, data, len, 0);
56         return true;
57 }
58
59 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
60         (void)handle;
61         (void)type;
62         (void)data;
63         (void)len;
64         return true;
65 }
66
67 static void receive_data(sptps_t *sptps) {
68         uint8_t buf[4096], *bufp = buf;
69         int fd = *(int *)sptps->handle;
70         size_t len = recv(fd, buf, sizeof(buf), 0);
71
72         while(len) {
73                 size_t done = sptps_receive_data(sptps, bufp, len);
74
75                 if(!done) {
76                         abort();
77                 }
78
79                 bufp += done;
80                 len -= done;
81         }
82 }
83
84 struct timespec start;
85 struct timespec end;
86 double elapsed;
87 double rate;
88 unsigned int count;
89
90 static void clock_start(void) {
91         count = 0;
92         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
93 }
94
95 static bool clock_countto(double seconds) {
96         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
97         elapsed = (double) end.tv_sec + (double) end.tv_nsec * 1e-9
98                   - (double) start.tv_sec - (double) start.tv_nsec * 1e-9;
99
100         if(elapsed < seconds) {
101                 return ++count;
102         }
103
104         rate = count / elapsed;
105         return false;
106 }
107
108 static int run_benchmark(int argc, char *argv[]) {
109         ecdsa_t *key1, *key2;
110         ecdh_t *ecdh1, *ecdh2;
111         sptps_t sptps1, sptps2;
112         uint8_t buf1[4096], buf2[4096], buf3[4096];
113         double duration = argc > 1 ? atof(argv[1]) : 10;
114
115         randomize(buf1, sizeof(buf1));
116         randomize(buf2, sizeof(buf2));
117         randomize(buf3, sizeof(buf3));
118
119         // Key generation
120
121         fprintf(stderr, "Generating keys for %lg seconds: ", duration);
122
123         for(clock_start(); clock_countto(duration);) {
124                 ecdsa_free(ecdsa_generate());
125         }
126
127         fprintf(stderr, "%17.2lf op/s\n", rate);
128
129         key1 = ecdsa_generate();
130         key2 = ecdsa_generate();
131
132         // Ed25519 signatures
133
134         fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
135
136         for(clock_start(); clock_countto(duration);)
137                 if(!ecdsa_sign(key1, buf1, 256, buf2)) {
138                         return 1;
139                 }
140
141         fprintf(stderr, "%20.2lf op/s\n", rate);
142
143         fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
144
145         for(clock_start(); clock_countto(duration);)
146                 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
147                         fprintf(stderr, "Signature verification failed\n");
148                         return 1;
149                 }
150
151         fprintf(stderr, "%18.2lf op/s\n", rate);
152
153         ecdh1 = ecdh_generate_public(buf1);
154         fprintf(stderr, "ECDH for %lg seconds: ", duration);
155
156         for(clock_start(); clock_countto(duration);) {
157                 ecdh2 = ecdh_generate_public(buf2);
158
159                 if(!ecdh2) {
160                         return 1;
161                 }
162
163                 if(!ecdh_compute_shared(ecdh2, buf1, buf3)) {
164                         return 1;
165                 }
166         }
167
168         fprintf(stderr, "%28.2lf op/s\n", rate);
169         ecdh_free(ecdh1);
170
171         int fd[2];
172         struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
173
174         sptps_params_t params1 = {
175                 .handle = fd + 0,
176                 .initiator = true,
177                 .datagram = false,
178                 .mykey = key1,
179                 .hiskey = key2,
180                 .label = "sptps_speed",
181                 .send_data = send_data,
182                 .receive_record = receive_record,
183         };
184
185         sptps_params_t params2 = {
186                 .handle = fd + 1,
187                 .initiator = false,
188                 .datagram = false,
189                 .mykey = key2,
190                 .hiskey = key1,
191                 .label = "sptps_speed",
192                 .send_data = send_data,
193                 .receive_record = receive_record,
194         };
195
196         static const char *suite_names[] = {
197                 "Chacha20-Poly1305",
198                 "AES-256-GCM",
199         };
200
201         for(uint8_t suite = 0; suite < 2; suite++) {
202                 fprintf(stderr, "\nCipher suite %u (%s):\n", suite, suite_names[suite]);
203                 params1.preferred_suite = params2.preferred_suite = suite;
204
205                 // SPTPS authentication phase
206
207                 fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
208
209                 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
210                         fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
211                         return 1;
212                 }
213
214                 pfd[0].fd = fd[0], pfd[1].fd = fd[1];
215                 params1.datagram = params2.datagram = false;
216
217                 for(clock_start(); clock_countto(duration);) {
218                         sptps_start(&sptps1, &params1);
219                         sptps_start(&sptps2, &params2);
220
221                         while(poll(pfd, 2, 0)) {
222                                 if(pfd[0].revents) {
223                                         receive_data(&sptps1);
224                                 }
225
226                                 if(pfd[1].revents) {
227                                         receive_data(&sptps2);
228                                 }
229                         }
230
231                         sptps_stop(&sptps1);
232                         sptps_stop(&sptps2);
233                 }
234
235                 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
236
237                 // SPTPS data
238
239                 sptps_start(&sptps1, &params1);
240                 sptps_start(&sptps2, &params2);
241
242                 while(poll(pfd, 2, 0)) {
243                         if(pfd[0].revents) {
244                                 receive_data(&sptps1);
245                         }
246
247                         if(pfd[1].revents) {
248                                 receive_data(&sptps2);
249                         }
250                 }
251
252                 fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
253
254                 for(clock_start(); clock_countto(duration);) {
255                         if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
256                                 abort();
257                         }
258
259                         receive_data(&sptps2);
260                 }
261
262                 rate *= 2 * 1451 * 8;
263
264                 if(rate > 1e9) {
265                         fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
266                 } else if(rate > 1e6) {
267                         fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
268                 } else if(rate > 1e3) {
269                         fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
270                 }
271
272                 sptps_stop(&sptps1);
273                 sptps_stop(&sptps2);
274
275                 close(fd[0]);
276                 close(fd[1]);
277
278                 // SPTPS datagram authentication phase
279
280                 if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
281                         fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
282                         return 1;
283                 }
284
285                 pfd[0].fd = fd[0], pfd[1].fd = fd[1];
286                 params1.datagram = params2.datagram = true;
287
288                 fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
289
290                 for(clock_start(); clock_countto(duration);) {
291                         sptps_start(&sptps1, &params1);
292                         sptps_start(&sptps2, &params2);
293
294                         while(poll(pfd, 2, 0)) {
295                                 if(pfd[0].revents) {
296                                         receive_data(&sptps1);
297                                 }
298
299                                 if(pfd[1].revents) {
300                                         receive_data(&sptps2);
301                                 }
302                         }
303
304                         sptps_stop(&sptps1);
305                         sptps_stop(&sptps2);
306                 }
307
308                 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
309
310                 // SPTPS datagram data
311
312                 sptps_start(&sptps1, &params1);
313                 sptps_start(&sptps2, &params2);
314
315                 while(poll(pfd, 2, 0)) {
316                         if(pfd[0].revents) {
317                                 receive_data(&sptps1);
318                         }
319
320                         if(pfd[1].revents) {
321                                 receive_data(&sptps2);
322                         }
323                 }
324
325                 fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
326
327                 for(clock_start(); clock_countto(duration);) {
328                         if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
329                                 abort();
330                         }
331
332                         receive_data(&sptps2);
333                 }
334
335                 rate *= 2 * 1451 * 8;
336
337                 if(rate > 1e9) {
338                         fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
339                 } else if(rate > 1e6) {
340                         fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
341                 } else if(rate > 1e3) {
342                         fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
343                 }
344
345                 sptps_stop(&sptps1);
346                 sptps_stop(&sptps2);
347
348                 close(fd[0]);
349                 close(fd[1]);
350         }
351
352         // Clean up
353
354         ecdsa_free(key1);
355         ecdsa_free(key2);
356
357         return 0;
358 }
359
360 int main(int argc, char *argv[]) {
361         random_init();
362         crypto_init();
363
364         int result = run_benchmark(argc, argv);
365
366         random_exit();
367
368         return result;
369 }