b15e01a5fc242a149fbaac059e5f71eb56c06795
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_dh_speed.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 */
15
16 #include <library.h>
17 #include <daemon.h>
18
19 #include <time.h>
20
21 static void start_timing(struct timespec *start)
22 {
23 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
24 }
25
26 static double end_timing(struct timespec *start)
27 {
28 struct timespec end;
29
30 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
31 return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
32 (end.tv_sec - start->tv_sec) * 1.0;
33 }
34
35
36 /*******************************************************************************
37 * public key sign/verify speed test
38 ******************************************************************************/
39 bool test_dh_speed()
40 {
41 struct {
42 diffie_hellman_group_t group;
43 int rounds;
44 } groups[] = {
45 { MODP_768_BIT, 600},
46 { MODP_1024_BIT, 400},
47 { MODP_1536_BIT, 200},
48 { MODP_2048_BIT, 100},
49 { ECP_192_BIT, 800},
50 { ECP_224_BIT, 600},
51 { ECP_256_BIT, 400},
52 { ECP_384_BIT, 200},
53 { ECP_521_BIT, 100},
54 };
55 int group, round;
56
57 for (group = 0; group < countof(groups); group++)
58 {
59 diffie_hellman_t *l[groups[group].rounds], *r;
60 chunk_t chunk;
61 struct timespec timing;
62
63 r = lib->crypto->create_dh(lib->crypto, groups[group].group);
64 if (!r)
65 {
66 DBG1(DBG_CFG, "skipping dh group %N, not supported",
67 diffie_hellman_group_names, groups[group].group);
68 continue;
69 }
70
71 DBG1(DBG_CFG, "testing dh group %N:",
72 diffie_hellman_group_names, groups[group].group);
73
74 start_timing(&timing);
75 for (round = 0; round < groups[group].rounds; round++)
76 {
77 l[round] = lib->crypto->create_dh(lib->crypto, groups[group].group);
78 }
79 DBG1(DBG_CFG, " %.0f A = g^a/s",
80 groups[group].rounds / end_timing(&timing));
81
82 for (round = 0; round < groups[group].rounds; round++)
83 {
84 l[round]->get_my_public_value(l[round], &chunk);
85 r->set_other_public_value(r, chunk);
86 chunk_free(&chunk);
87 }
88
89 r->get_my_public_value(r, &chunk);
90 start_timing(&timing);
91 for (round = 0; round < groups[group].rounds; round++)
92 {
93 l[round]->set_other_public_value(l[round], chunk);
94 }
95 DBG1(DBG_CFG, " %.0f S = B^a/s",
96 groups[group].rounds / end_timing(&timing));
97 chunk_free(&chunk);
98
99 for (round = 0; round < groups[group].rounds; round++)
100 {
101 l[round]->destroy(l[round]);
102 }
103 r->destroy(r);
104 }
105 return TRUE;
106 }
107