File: dh_speed.c

package info (click to toggle)
strongswan 6.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 61,584 kB
  • sloc: ansic: 405,188; sh: 11,621; makefile: 6,254; sql: 2,885; javascript: 1,948; python: 990; xml: 608; perl: 586; cs: 522; lex: 485; yacc: 443; ruby: 398; ada: 114
file content (139 lines) | stat: -rw-r--r-- 3,633 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2023-2024 Tobias Brunner
 * Copyright (C) 2009 Martin Willi
 *
 * Copyright (C) secunet Security Networks AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <library.h>
#include <utils/debug.h>
#include <crypto/key_exchange.h>

static void usage()
{
	printf("usage: dh_speed plugins rounds ke1 [ke2 [...]]\n");
	exit(1);
}

static void start_timing(struct timespec *start)
{
	clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
}

static double end_timing(struct timespec *start)
{
	struct timespec end;

	clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
	return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
			(end.tv_sec - start->tv_sec) * 1.0;
}

static void run_test(key_exchange_method_t method, int rounds)
{
	key_exchange_t *l[rounds], *r[rounds];
	chunk_t lpublic[rounds], rpublic[rounds], lsecret[rounds], rsecret[rounds];
	struct timespec timing;
	int round;

	r[0] = lib->crypto->create_ke(lib->crypto, method);
	if (!r[0])
	{
		fprintf(stderr, "skipping %N, not supported\n", key_exchange_method_names,
				method);
		return;
	}
	for (round = 1; round < rounds; round++)
	{
		r[round] = lib->crypto->create_ke(lib->crypto, method);
	}

	/* make sure to use the method call order documented in the
	 * key_exchange_t header file */

	printf("%N:\t", key_exchange_method_names, method);

	start_timing(&timing);
	for (round = 0; round < rounds; round++)
	{
		l[round] = lib->crypto->create_ke(lib->crypto, method);
		assert(l[round]->get_public_key(l[round], &lpublic[round]));
	}
	printf("A = g^a/s: %8.1f", rounds / end_timing(&timing));

	start_timing(&timing);
	for (round = 0; round < rounds; round++)
	{
		assert(r[round]->set_public_key(r[round], lpublic[round]));
		assert(r[round]->get_public_key(r[round], &rpublic[round]));
		assert(r[round]->get_shared_secret(r[round], &rsecret[round]));
	}
	printf(" | S = A^b/s: %8.1f", rounds / end_timing(&timing));

	start_timing(&timing);
	for (round = 0; round < rounds; round++)
	{
		assert(l[round]->set_public_key(l[round], rpublic[round]));
		assert(l[round]->get_shared_secret(l[round], &lsecret[round]));
	}
	printf(" | S = B^a/s: %8.1f\n", rounds / end_timing(&timing));

	for (round = 0; round < rounds; round++)
	{
		assert(chunk_equals(rsecret[round], lsecret[round]));
		chunk_free(&lsecret[round]);
		chunk_free(&rsecret[round]);
		chunk_free(&lpublic[round]);
		chunk_free(&rpublic[round]);
		l[round]->destroy(l[round]);
		r[round]->destroy(r[round]);
	}
}

int main(int argc, char *argv[])
{
	const proposal_token_t *token;
	int rounds, i;

	if (argc < 4)
	{
		usage();
	}

	library_init(NULL, "dh_speed");
	lib->plugins->load(lib->plugins, argv[1]);
	atexit(library_deinit);

	rounds = atoi(argv[2]);

	for (i = 3; i < argc; i++)
	{
		token = lib->proposal->get_token(lib->proposal, argv[i]);
		if (!token)
		{
			fprintf(stderr, "KE method '%s' not found\n", argv[i]);
			return 1;
		}
		else if (token->type != KEY_EXCHANGE_METHOD)
		{
			fprintf(stderr, "'%s' is not a KE method\n", argv[i]);
			return 1;
		}

		run_test(token->algorithm, rounds);
	}
	return 0;
}