]> git.ipfire.org Git - thirdparty/strongswan.git/blob - scripts/dh_speed.c
x509: Also encode extendedKeyUsage in cert requests if there are no SANs or certifica...
[thirdparty/strongswan.git] / scripts / dh_speed.c
1 /*
2 * Copyright (C) 2023 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 *
5 * Copyright (C) secunet Security Networks AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdio.h>
19 #include <time.h>
20 #include <assert.h>
21 #include <library.h>
22 #include <utils/debug.h>
23 #include <crypto/key_exchange.h>
24
25 static void usage()
26 {
27 printf("usage: dh_speed plugins rounds ke1 [ke2 [...]]\n");
28 exit(1);
29 }
30
31 static void start_timing(struct timespec *start)
32 {
33 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
34 }
35
36 static double end_timing(struct timespec *start)
37 {
38 struct timespec end;
39
40 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
41 return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
42 (end.tv_sec - start->tv_sec) * 1.0;
43 }
44
45 static void run_test(key_exchange_method_t method, int rounds)
46 {
47 key_exchange_t *l[rounds], *r[rounds];
48 chunk_t lpublic[rounds], rpublic[rounds], lsecret[rounds], rsecret[rounds];
49 struct timespec timing;
50 int round;
51
52 r[0] = lib->crypto->create_ke(lib->crypto, method);
53 if (!r[0])
54 {
55 fprintf(stderr, "skipping %N, not supported\n", key_exchange_method_names,
56 method);
57 return;
58 }
59 assert(r[0]->get_public_key(r[0], &rpublic[0]));
60 for (round = 1; round < rounds; round++)
61 {
62 r[round] = lib->crypto->create_ke(lib->crypto, method);
63 assert(r[round]->get_public_key(r[round], &rpublic[round]));
64 }
65
66 printf("%N:\t", key_exchange_method_names, method);
67
68 start_timing(&timing);
69 for (round = 0; round < rounds; round++)
70 {
71 l[round] = lib->crypto->create_ke(lib->crypto, method);
72 assert(l[round]->get_public_key(l[round], &lpublic[round]));
73 }
74 printf("A = g^a/s: %8.1f", rounds / end_timing(&timing));
75
76 for (round = 0; round < rounds; round++)
77 {
78 assert(r[round]->set_public_key(r[round], lpublic[round]));
79 assert(r[round]->get_shared_secret(r[round], &rsecret[round]));
80 chunk_free(&lpublic[round]);
81 }
82
83 start_timing(&timing);
84 for (round = 0; round < rounds; round++)
85 {
86 assert(l[round]->set_public_key(l[round], rpublic[round]));
87 assert(l[round]->get_shared_secret(l[round], &lsecret[round]));
88 }
89 printf(" | S = B^a/s: %8.1f\n", rounds / end_timing(&timing));
90
91 for (round = 0; round < rounds; round++)
92 {
93 assert(chunk_equals(rsecret[round], lsecret[round]));
94 chunk_free(&lsecret[round]);
95 chunk_free(&rsecret[round]);
96 chunk_free(&rpublic[round]);
97 l[round]->destroy(l[round]);
98 r[round]->destroy(r[round]);
99 }
100 }
101
102 int main(int argc, char *argv[])
103 {
104 const proposal_token_t *token;
105 int rounds, i;
106
107 if (argc < 4)
108 {
109 usage();
110 }
111
112 library_init(NULL, "dh_speed");
113 lib->plugins->load(lib->plugins, argv[1]);
114 atexit(library_deinit);
115
116 rounds = atoi(argv[2]);
117
118 for (i = 3; i < argc; i++)
119 {
120 token = lib->proposal->get_token(lib->proposal, argv[i]);
121 if (!token)
122 {
123 fprintf(stderr, "KE method '%s' not found\n", argv[i]);
124 return 1;
125 }
126 else if (token->type != KEY_EXCHANGE_METHOD)
127 {
128 fprintf(stderr, "'%s' is not a KE method\n", argv[i]);
129 return 1;
130 }
131
132 run_test(token->algorithm, rounds);
133 }
134 return 0;
135 }