2 * Copyright (C) 2023 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
5 * Copyright (C) secunet Security Networks AG
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>.
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
22 #include <utils/debug.h>
23 #include <crypto/key_exchange.h>
27 printf("usage: dh_speed plugins rounds ke1 [ke2 [...]]\n");
31 static void start_timing(struct timespec
*start
)
33 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, start
);
36 static double end_timing(struct timespec
*start
)
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;
45 static void run_test(key_exchange_method_t method
, int rounds
)
47 key_exchange_t
*l
[rounds
], *r
[rounds
];
48 chunk_t lpublic
[rounds
], rpublic
[rounds
], lsecret
[rounds
], rsecret
[rounds
];
49 struct timespec timing
;
52 r
[0] = lib
->crypto
->create_ke(lib
->crypto
, method
);
55 fprintf(stderr
, "skipping %N, not supported\n", key_exchange_method_names
,
59 assert(r
[0]->get_public_key(r
[0], &rpublic
[0]));
60 for (round
= 1; round
< rounds
; round
++)
62 r
[round
] = lib
->crypto
->create_ke(lib
->crypto
, method
);
63 assert(r
[round
]->get_public_key(r
[round
], &rpublic
[round
]));
66 printf("%N:\t", key_exchange_method_names
, method
);
68 start_timing(&timing
);
69 for (round
= 0; round
< rounds
; round
++)
71 l
[round
] = lib
->crypto
->create_ke(lib
->crypto
, method
);
72 assert(l
[round
]->get_public_key(l
[round
], &lpublic
[round
]));
74 printf("A = g^a/s: %8.1f", rounds
/ end_timing(&timing
));
76 for (round
= 0; round
< rounds
; round
++)
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
]);
83 start_timing(&timing
);
84 for (round
= 0; round
< rounds
; round
++)
86 assert(l
[round
]->set_public_key(l
[round
], rpublic
[round
]));
87 assert(l
[round
]->get_shared_secret(l
[round
], &lsecret
[round
]));
89 printf(" | S = B^a/s: %8.1f\n", rounds
/ end_timing(&timing
));
91 for (round
= 0; round
< rounds
; round
++)
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
]);
102 int main(int argc
, char *argv
[])
104 const proposal_token_t
*token
;
112 library_init(NULL
, "dh_speed");
113 lib
->plugins
->load(lib
->plugins
, argv
[1]);
114 atexit(library_deinit
);
116 rounds
= atoi(argv
[2]);
118 for (i
= 3; i
< argc
; i
++)
120 token
= lib
->proposal
->get_token(lib
->proposal
, argv
[i
]);
123 fprintf(stderr
, "KE method '%s' not found\n", argv
[i
]);
126 else if (token
->type
!= KEY_EXCHANGE_METHOD
)
128 fprintf(stderr
, "'%s' is not a KE method\n", argv
[i
]);
132 run_test(token
->algorithm
, rounds
);