]> git.ipfire.org Git - people/ms/strongswan.git/blame - scripts/dh_speed.c
os_info: Parse /etc/os-release first
[people/ms/strongswan.git] / scripts / dh_speed.c
CommitLineData
f3af4969
TB
1/*
2 * Copyright (C) 2009 Martin Willi
1b671669 3 * HSR Hochschule fuer Technik Rapperswil
f3af4969
TB
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 */
3e3de01b
MW
15
16#include <stdio.h>
17#include <time.h>
42431690 18#include <assert.h>
3e3de01b 19#include <library.h>
f05b4272 20#include <utils/debug.h>
3e3de01b
MW
21#include <crypto/diffie_hellman.h>
22
23static void usage()
24{
25 printf("usage: dh_speed plugins rounds group1 [group2 [...]]\n");
26 exit(1);
27}
28
29struct {
30 char *name;
31 diffie_hellman_group_t group;
32} groups[] = {
4590260b
MW
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},
05327515 49 {"curve25519", CURVE_25519},
d222699c 50 {"curve448", CURVE_448},
3e3de01b
MW
51};
52
53static void start_timing(struct timespec *start)
54{
55 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
56}
57
58static double end_timing(struct timespec *start)
59{
60 struct timespec end;
7daf5226 61
3e3de01b
MW
62 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
63 return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
64 (end.tv_sec - start->tv_sec) * 1.0;
65}
66
67static void run_test(diffie_hellman_group_t group, int rounds)
68{
69 diffie_hellman_t *l[rounds], *r;
2ac95123 70 chunk_t chunk, chunks[rounds], lsecrets[rounds], rsecrets[rounds];
3e3de01b
MW
71 struct timespec timing;
72 int round;
7daf5226 73
3e3de01b
MW
74 r = lib->crypto->create_dh(lib->crypto, group);
75 if (!r)
76 {
77 printf("skipping %N, not supported\n",
78 diffie_hellman_group_names, group);
79 return;
80 }
7daf5226 81
2ac95123 82 printf("%N:\t", diffie_hellman_group_names, group);
7daf5226 83
3e3de01b
MW
84 start_timing(&timing);
85 for (round = 0; round < rounds; round++)
86 {
87 l[round] = lib->crypto->create_dh(lib->crypto, group);
0ab85478 88 assert(l[round]->get_my_public_value(l[round], &chunks[round]));
3e3de01b
MW
89 }
90 printf("A = g^a/s: %8.1f", rounds / end_timing(&timing));
7daf5226 91
3e3de01b
MW
92 for (round = 0; round < rounds; round++)
93 {
0ab85478 94 assert(r->set_other_public_value(r, chunks[round]));
2ac95123 95 assert(r->get_shared_secret(r, &rsecrets[round]));
0ab85478 96 chunk_free(&chunks[round]);
3e3de01b 97 }
7daf5226 98
42431690 99 assert(r->get_my_public_value(r, &chunk));
3e3de01b
MW
100 start_timing(&timing);
101 for (round = 0; round < rounds; round++)
102 {
a777155f 103 assert(l[round]->set_other_public_value(l[round], chunk));
2ac95123 104 assert(l[round]->get_shared_secret(l[round], &lsecrets[round]));
3e3de01b
MW
105 }
106 printf(" | S = B^a/s: %8.1f\n", rounds / end_timing(&timing));
107 chunk_free(&chunk);
7daf5226 108
3e3de01b
MW
109 for (round = 0; round < rounds; round++)
110 {
2ac95123
MW
111 assert(chunk_equals(rsecrets[round], lsecrets[round]));
112 free(lsecrets[round].ptr);
113 free(rsecrets[round].ptr);
3e3de01b
MW
114 l[round]->destroy(l[round]);
115 }
116 r->destroy(r);
117}
118
119int main(int argc, char *argv[])
120{
121 int rounds, i, j;
7daf5226 122
3e3de01b
MW
123 if (argc < 4)
124 {
125 usage();
126 }
7daf5226 127
34d3bfcf 128 library_init(NULL, "dh_speed");
b18a5317 129 lib->plugins->load(lib->plugins, argv[1]);
3e3de01b 130 atexit(library_deinit);
7daf5226 131
3e3de01b 132 rounds = atoi(argv[2]);
7daf5226 133
3e3de01b
MW
134 for (i = 3; i < argc; i++)
135 {
136 bool found = FALSE;
7daf5226 137
3e3de01b
MW
138 for (j = 0; j < countof(groups); j++)
139 {
140 if (streq(groups[j].name, argv[i]))
141 {
142 run_test(groups[j].group, rounds);
143 found = TRUE;
144 }
145 }
146 if (!found)
147 {
148 printf("group %s not found\n", argv[i]);
149 }
150 }
151 return 0;
152}