]>
Commit | Line | Data |
---|---|---|
f3af4969 TB |
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 | */ | |
3e3de01b MW |
15 | |
16 | #include <stdio.h> | |
17 | #include <time.h> | |
18 | #include <library.h> | |
f05b4272 | 19 | #include <utils/debug.h> |
3e3de01b MW |
20 | #include <crypto/diffie_hellman.h> |
21 | ||
22 | static void usage() | |
23 | { | |
24 | printf("usage: dh_speed plugins rounds group1 [group2 [...]]\n"); | |
25 | exit(1); | |
26 | } | |
27 | ||
28 | struct { | |
29 | char *name; | |
30 | diffie_hellman_group_t group; | |
31 | } groups[] = { | |
4590260b MW |
32 | {"modp768", MODP_768_BIT}, |
33 | {"modp1024", MODP_1024_BIT}, | |
34 | {"modp1024s160", MODP_1024_160}, | |
35 | {"modp1536", MODP_1536_BIT}, | |
36 | {"modp2048", MODP_2048_BIT}, | |
37 | {"modp2048s224", MODP_2048_224}, | |
38 | {"modp2048s256", MODP_2048_256}, | |
39 | {"modp3072", MODP_3072_BIT}, | |
40 | {"modp4096", MODP_4096_BIT}, | |
41 | {"modp6144", MODP_6144_BIT}, | |
42 | {"modp8192", MODP_8192_BIT}, | |
43 | {"ecp256", ECP_256_BIT}, | |
44 | {"ecp384", ECP_384_BIT}, | |
45 | {"ecp521", ECP_521_BIT}, | |
46 | {"ecp192", ECP_192_BIT}, | |
47 | {"ecp224", ECP_224_BIT}, | |
3e3de01b MW |
48 | }; |
49 | ||
50 | static void start_timing(struct timespec *start) | |
51 | { | |
52 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, start); | |
53 | } | |
54 | ||
55 | static double end_timing(struct timespec *start) | |
56 | { | |
57 | struct timespec end; | |
7daf5226 | 58 | |
3e3de01b MW |
59 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end); |
60 | return (end.tv_nsec - start->tv_nsec) / 1000000000.0 + | |
61 | (end.tv_sec - start->tv_sec) * 1.0; | |
62 | } | |
63 | ||
64 | static void run_test(diffie_hellman_group_t group, int rounds) | |
65 | { | |
66 | diffie_hellman_t *l[rounds], *r; | |
67 | chunk_t chunk; | |
68 | struct timespec timing; | |
69 | int round; | |
7daf5226 | 70 | |
3e3de01b MW |
71 | r = lib->crypto->create_dh(lib->crypto, group); |
72 | if (!r) | |
73 | { | |
74 | printf("skipping %N, not supported\n", | |
75 | diffie_hellman_group_names, group); | |
76 | return; | |
77 | } | |
7daf5226 | 78 | |
3e3de01b MW |
79 | printf("%N:\t", |
80 | diffie_hellman_group_names, group); | |
7daf5226 | 81 | |
3e3de01b MW |
82 | start_timing(&timing); |
83 | for (round = 0; round < rounds; round++) | |
84 | { | |
85 | l[round] = lib->crypto->create_dh(lib->crypto, group); | |
86 | } | |
87 | printf("A = g^a/s: %8.1f", rounds / end_timing(&timing)); | |
7daf5226 | 88 | |
3e3de01b MW |
89 | for (round = 0; round < rounds; round++) |
90 | { | |
91 | l[round]->get_my_public_value(l[round], &chunk); | |
92 | r->set_other_public_value(r, chunk); | |
93 | chunk_free(&chunk); | |
94 | } | |
7daf5226 | 95 | |
3e3de01b MW |
96 | r->get_my_public_value(r, &chunk); |
97 | start_timing(&timing); | |
98 | for (round = 0; round < rounds; round++) | |
99 | { | |
100 | l[round]->set_other_public_value(l[round], chunk); | |
101 | } | |
102 | printf(" | S = B^a/s: %8.1f\n", rounds / end_timing(&timing)); | |
103 | chunk_free(&chunk); | |
7daf5226 | 104 | |
3e3de01b MW |
105 | for (round = 0; round < rounds; round++) |
106 | { | |
107 | l[round]->destroy(l[round]); | |
108 | } | |
109 | r->destroy(r); | |
110 | } | |
111 | ||
112 | int main(int argc, char *argv[]) | |
113 | { | |
114 | int rounds, i, j; | |
7daf5226 | 115 | |
3e3de01b MW |
116 | if (argc < 4) |
117 | { | |
118 | usage(); | |
119 | } | |
7daf5226 | 120 | |
356b2b27 | 121 | library_init(NULL); |
5b03a350 | 122 | lib->plugins->load(lib->plugins, NULL, argv[1]); |
3e3de01b | 123 | atexit(library_deinit); |
7daf5226 | 124 | |
3e3de01b | 125 | rounds = atoi(argv[2]); |
7daf5226 | 126 | |
3e3de01b MW |
127 | for (i = 3; i < argc; i++) |
128 | { | |
129 | bool found = FALSE; | |
7daf5226 | 130 | |
3e3de01b MW |
131 | for (j = 0; j < countof(groups); j++) |
132 | { | |
133 | if (streq(groups[j].name, argv[i])) | |
134 | { | |
135 | run_test(groups[j].group, rounds); | |
136 | found = TRUE; | |
137 | } | |
138 | } | |
139 | if (!found) | |
140 | { | |
141 | printf("group %s not found\n", argv[i]); | |
142 | } | |
143 | } | |
144 | return 0; | |
145 | } | |
146 |