blob: 22c893070d664af626a3a2fe7b7382fb08ba5ae3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +02008#define MBEDTLS_ALLOW_PRIVATE_ACCESS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050014#if !defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010015int main(void)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000016{
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050017 mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010018 mbedtls_exit(0);
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000019}
20#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010021
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000022#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020023#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010024
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/md5.h"
26#include "mbedtls/ripemd160.h"
27#include "mbedtls/sha1.h"
28#include "mbedtls/sha256.h"
29#include "mbedtls/sha512.h"
Pol Henarejosebb36402022-05-20 14:26:00 +020030#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/des.h"
33#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010034#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030036#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/gcm.h"
38#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020039#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010040#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030041#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010042
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ctr_drbg.h"
44#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/rsa.h"
47#include "mbedtls/dhm.h"
48#include "mbedtls/ecdsa.h"
49#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010050
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
David Horstmanne3d8f312023-01-03 11:07:09 +000053/* *INDENT-OFF* */
TRodziewicz90f304f2021-06-11 11:56:47 +020054#ifndef asm
55#define asm __asm
56#endif
David Horstmanne3d8f312023-01-03 11:07:09 +000057/* *INDENT-ON* */
TRodziewicz90f304f2021-06-11 11:56:47 +020058
TRodziewiczd8540832021-06-10 15:16:50 +020059#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
60
61#include <windows.h>
62#include <process.h>
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020065 LARGE_INTEGER start;
66};
67
68#else
69
70#include <unistd.h>
71#include <sys/types.h>
72#include <sys/time.h>
73#include <signal.h>
74#include <time.h>
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020077 struct timeval start;
78};
79
80#endif /* _WIN32 && !EFIX64 && !EFI32 */
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000084#endif
85
Matthias Schulz3b9240b2023-11-16 17:39:43 +010086#ifdef MBEDTLS_TIMING_ALT
87void mbedtls_set_alarm(int seconds);
88unsigned long mbedtls_timing_hardclock(void);
89extern volatile int mbedtls_timing_alarmed;
90#else
Gilles Peskine449bd832023-01-11 14:50:10 +010091static void mbedtls_set_alarm(int seconds);
Matthias Schulz3b9240b2023-11-16 17:39:43 +010092static unsigned long mbedtls_timing_hardclock(void);
93#endif
TRodziewiczd8540832021-06-10 15:16:50 +020094
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000095/*
96 * For heap usage estimates, we need an estimate of the overhead per allocated
97 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
98 * so use that as our baseline.
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100#define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000101
102/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200103 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000104 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +0100105#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000106
Paul Bakker02faf452011-11-29 11:23:58 +0000107#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100108#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100109#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000110
Yanray Wang022b9a12023-09-12 09:45:37 +0800111#define OPTIONS \
112 "md5, ripemd160, sha1, sha256, sha512,\n" \
113 "sha3_224, sha3_256, sha3_384, sha3_512,\n" \
114 "des3, des, camellia, chacha20,\n" \
115 "aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly\n" \
116 "aes_cmac, des3_cmac, poly1305\n" \
117 "ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000118 "rsa, dhm, ecdsa, ecdh.\n"
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000121#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
123 mbedtls_printf("FAILED: %s\n", tmp);
Rich Evans85b05ec2015-02-12 11:37:29 +0000124#else
125#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
Rich Evans85b05ec2015-02-12 11:37:29 +0000127#endif
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129#define TIME_AND_TSC(TITLE, CODE) \
130 do { \
131 unsigned long ii, jj, tsc; \
132 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000133 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_printf(HEADER_FORMAT, TITLE); \
135 fflush(stdout); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_set_alarm(1); \
138 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
139 { \
140 ret = CODE; \
141 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000142 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 tsc = mbedtls_timing_hardclock(); \
144 for (jj = 0; ret == 0 && jj < 1024; jj++) \
145 { \
146 ret = CODE; \
147 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000148 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (ret != 0) \
150 { \
151 PRINT_ERROR; \
152 } \
153 else \
154 { \
155 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
156 ii * BUFSIZE / 1024, \
157 (mbedtls_timing_hardclock() - tsc) \
158 / (jj * BUFSIZE)); \
159 } \
160 } while (0)
Rich Evans85b05ec2015-02-12 11:37:29 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200164/* How much space to reserve for the title when printing heap usage results.
165 * Updated manually as the output of the following command:
166 *
167 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100168 * awk '{print length+3}' | sort -rn | head -n1
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200169 *
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100170 * This computes the maximum length of a title +3, because we appends "/s" and
171 * want at least one space. (If the value is too small, the only consequence
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800172 * is poor alignment.) */
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100173#define TITLE_SPACE 17
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200174
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100175#define MEMORY_MEASURE_INIT \
176 size_t max_used, max_blocks, max_bytes; \
177 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnard6ced0022022-01-05 10:05:54 +0100178 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
180 mbedtls_memory_buffer_alloc_max_reset();
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100181
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100182#define MEMORY_MEASURE_RESET \
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185#define MEMORY_MEASURE_PRINT(title_len) \
186 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
187 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200188 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 while (ii--) mbedtls_printf(" "); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100190 max_used -= prv_used; \
191 max_blocks -= prv_blocks; \
192 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_printf("%6u heap bytes, %6u allocs", \
194 (unsigned) max_bytes, \
195 (unsigned) (alloc_cnt - prv_alloc));
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100196
197#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000198#define MEMORY_MEASURE_INIT
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100199#define MEMORY_MEASURE_RESET
Gilles Peskine449bd832023-01-11 14:50:10 +0100200#define MEMORY_MEASURE_PRINT(title_len)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100201#endif
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203#define TIME_PUBLIC(TITLE, TYPE, CODE) \
204 do { \
205 unsigned long ii; \
206 int ret; \
207 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000208 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_printf(HEADER_FORMAT, TITLE); \
210 fflush(stdout); \
211 mbedtls_set_alarm(3); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000212 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ret = 0; \
214 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
215 { \
216 MEMORY_MEASURE_RESET; \
217 CODE; \
218 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000219 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
221 { \
222 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
223 ret = 0; \
224 } \
225 else if (ret != 0) \
226 { \
227 PRINT_ERROR; \
228 } \
229 else \
230 { \
231 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
232 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
233 mbedtls_printf("\n"); \
234 } \
235 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100237#if !defined(MBEDTLS_TIMING_ALT)
TRodziewiczd8540832021-06-10 15:16:50 +0200238#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
TRodziewiczd8540832021-06-10 15:16:50 +0200240
241#define HAVE_HARDCLOCK
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200244{
245 unsigned long tsc;
246 __asm rdtsc
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 __asm mov[tsc], eax
248 return tsc;
TRodziewiczd8540832021-06-10 15:16:50 +0200249}
250#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
251 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
252
253/* some versions of mingw-64 have 32-bit longs even on x84_64 */
254#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 defined(__GNUC__) && (defined(__i386__) || ( \
256 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
TRodziewiczd8540832021-06-10 15:16:50 +0200257
258#define HAVE_HARDCLOCK
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200261{
262 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
264 return lo;
TRodziewiczd8540832021-06-10 15:16:50 +0200265}
266#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
267 __GNUC__ && __i386__ */
268
269#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
TRodziewiczd8540832021-06-10 15:16:50 +0200271
272#define HAVE_HARDCLOCK
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200275{
276 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
278 return lo | (hi << 32);
TRodziewiczd8540832021-06-10 15:16:50 +0200279}
280#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
281 __GNUC__ && ( __amd64__ || __x86_64__ ) */
282
283#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
TRodziewiczd8540832021-06-10 15:16:50 +0200285
286#define HAVE_HARDCLOCK
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200289{
290 unsigned long tbl, tbu0, tbu1;
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 do {
293 asm volatile ("mftbu %0" : "=r" (tbu0));
294 asm volatile ("mftb %0" : "=r" (tbl));
295 asm volatile ("mftbu %0" : "=r" (tbu1));
296 } while (tbu0 != tbu1);
TRodziewiczd8540832021-06-10 15:16:50 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return tbl;
TRodziewiczd8540832021-06-10 15:16:50 +0200299}
300#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
301 __GNUC__ && ( __powerpc__ || __ppc__ ) */
302
303#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
304 defined(__GNUC__) && defined(__sparc64__)
305
306#if defined(__OpenBSD__)
307#warning OpenBSD does not allow access to tick register using software version instead
308#else
309#define HAVE_HARDCLOCK
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200312{
313 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
315 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200316}
317#endif /* __OpenBSD__ */
318#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
319 __GNUC__ && __sparc64__ */
320
321#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
322 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
323
324#define HAVE_HARDCLOCK
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200327{
328 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
330 asm volatile ("mov %%g1, %0" : "=r" (tick));
331 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200332}
333#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
334 __GNUC__ && __sparc__ && !__sparc64__ */
335
336#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
337 defined(__GNUC__) && defined(__alpha__)
338
339#define HAVE_HARDCLOCK
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200342{
343 unsigned long cc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 asm volatile ("rpcc %0" : "=r" (cc));
345 return cc & 0xFFFFFFFF;
TRodziewiczd8540832021-06-10 15:16:50 +0200346}
347#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
348 __GNUC__ && __alpha__ */
349
350#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
351 defined(__GNUC__) && defined(__ia64__)
352
353#define HAVE_HARDCLOCK
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200356{
357 unsigned long itc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
359 return itc;
TRodziewiczd8540832021-06-10 15:16:50 +0200360}
361#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
362 __GNUC__ && __ia64__ */
363
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300364#if !defined(HAVE_HARDCLOCK) && defined(_WIN32) && \
TRodziewiczd8540832021-06-10 15:16:50 +0200365 !defined(EFIX64) && !defined(EFI32)
366
367#define HAVE_HARDCLOCK
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200370{
371 LARGE_INTEGER offset;
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 QueryPerformanceCounter(&offset);
TRodziewiczd8540832021-06-10 15:16:50 +0200374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return (unsigned long) (offset.QuadPart);
TRodziewiczd8540832021-06-10 15:16:50 +0200376}
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300377#endif /* !HAVE_HARDCLOCK && _WIN32 && !EFIX64 && !EFI32 */
TRodziewiczd8540832021-06-10 15:16:50 +0200378
379#if !defined(HAVE_HARDCLOCK)
380
381#define HAVE_HARDCLOCK
382
383static int hardclock_init = 0;
384static struct timeval tv_init;
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200387{
388 struct timeval tv_cur;
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (hardclock_init == 0) {
391 gettimeofday(&tv_init, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200392 hardclock_init = 1;
393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 gettimeofday(&tv_cur, NULL);
396 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
397 + (tv_cur.tv_usec - tv_init.tv_usec);
TRodziewiczd8540832021-06-10 15:16:50 +0200398}
399#endif /* !HAVE_HARDCLOCK */
400
401volatile int mbedtls_timing_alarmed = 0;
402
403#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
404
405/* It's OK to use a global because alarm() is supposed to be global anyway */
406static DWORD alarmMs;
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static void TimerProc(void *TimerContext)
TRodziewiczd8540832021-06-10 15:16:50 +0200409{
410 (void) TimerContext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 Sleep(alarmMs);
TRodziewiczd8540832021-06-10 15:16:50 +0200412 mbedtls_timing_alarmed = 1;
413 /* _endthread will be called implicitly on return
Tom Cosgrove1797b052022-12-04 17:19:59 +0000414 * That ensures execution of thread function's epilogue */
TRodziewiczd8540832021-06-10 15:16:50 +0200415}
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200418{
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200420 /* No need to create a thread for this simple case.
421 * Also, this shorcut is more reliable at least on MinGW32 */
422 mbedtls_timing_alarmed = 1;
423 return;
424 }
425
426 mbedtls_timing_alarmed = 0;
427 alarmMs = seconds * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 (void) _beginthread(TimerProc, 0, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200429}
430
431#else /* _WIN32 && !EFIX64 && !EFI32 */
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433static void sighandler(int signum)
TRodziewiczd8540832021-06-10 15:16:50 +0200434{
435 mbedtls_timing_alarmed = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 signal(signum, sighandler);
TRodziewiczd8540832021-06-10 15:16:50 +0200437}
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200440{
441 mbedtls_timing_alarmed = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 signal(SIGALRM, sighandler);
443 alarm(seconds);
444 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200445 /* alarm(0) cancelled any previous pending alarm, but the
446 handler won't fire, so raise the flag straight away. */
447 mbedtls_timing_alarmed = 1;
448 }
449}
450
451#endif /* _WIN32 && !EFIX64 && !EFI32 */
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100452#endif /* !MBEDTLS_TIMING_ALT */
TRodziewiczd8540832021-06-10 15:16:50 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000455{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000456 size_t use_len;
457 int rnd;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (rng_state != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 while (len > 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000464 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (use_len > sizeof(int)) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000466 use_len = sizeof(int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Paul Bakkera3d195c2011-11-27 21:07:34 +0000468
469 rnd = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 memcpy(output, &rnd, use_len);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000471 output += use_len;
472 len -= use_len;
473 }
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000476}
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478#define CHECK_AND_CONTINUE(R) \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000479 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 int CHECK_AND_CONTINUE_ret = (R); \
481 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
482 mbedtls_printf("Feature not supported. Skipping.\n"); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000483 continue; \
484 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 else if (CHECK_AND_CONTINUE_ret != 0) { \
486 mbedtls_exit(1); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000487 } \
488 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000489
Gilles Peskine28f62f62020-07-24 02:06:46 +0200490#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100491static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
Gilles Peskine28f62f62020-07-24 02:06:46 +0200492{
493 const mbedtls_ecp_curve_info *found =
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_ecp_curve_info_from_name(string);
495 if (found != NULL) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200496 *curve = *found;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return 1;
498 } else {
499 return 0;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200500 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200501}
502#endif
503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504unsigned char buf[BUFSIZE];
505
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200506typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200507 char md5, ripemd160, sha1, sha256, sha512,
Pol Henarejosebb36402022-05-20 14:26:00 +0200508 sha3_224, sha3_256, sha3_384, sha3_512,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200509 des3, des,
Yanray Wang022b9a12023-09-12 09:45:37 +0800510 aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200511 aes_cmac, des3_cmac,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200512 aria, camellia, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300513 poly1305,
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100514 ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200515 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200516} todo_list;
517
Simon Butcher63cb97e2018-12-06 17:43:31 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int main(int argc, char *argv[])
Paul Bakker5690efc2011-05-26 13:16:06 +0000520{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100521 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200522 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200524 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200526 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000527#endif
Gilles Peskine28f62f62020-07-24 02:06:46 +0200528#if defined(MBEDTLS_ECP_C)
529 mbedtls_ecp_curve_info single_curve[2] = {
530 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
531 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
532 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
Gilles Peskine28f62f62020-07-24 02:06:46 +0200534#endif
535
536#if defined(MBEDTLS_ECP_C)
537 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
538#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (argc <= 1) {
541 memset(&todo, 1, sizeof(todo));
542 } else {
543 memset(&todo, 0, sizeof(todo));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 for (i = 1; i < argc; i++) {
546 if (strcmp(argv[i], "md5") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200547 todo.md5 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 } else if (strcmp(argv[i], "ripemd160") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200549 todo.ripemd160 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 } else if (strcmp(argv[i], "sha1") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200551 todo.sha1 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 } else if (strcmp(argv[i], "sha256") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200553 todo.sha256 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 } else if (strcmp(argv[i], "sha512") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200555 todo.sha512 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100556 } else if (strcmp(argv[i], "sha3_224") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200557 todo.sha3_224 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100558 } else if (strcmp(argv[i], "sha3_256") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200559 todo.sha3_256 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100560 } else if (strcmp(argv[i], "sha3_384") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200561 todo.sha3_384 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100562 } else if (strcmp(argv[i], "sha3_512") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200563 todo.sha3_512 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 } else if (strcmp(argv[i], "des3") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200565 todo.des3 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 } else if (strcmp(argv[i], "des") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200567 todo.des = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 } else if (strcmp(argv[i], "aes_cbc") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200569 todo.aes_cbc = 1;
Yanray Wang55aba192023-09-12 09:03:50 +0800570 } else if (strcmp(argv[i], "aes_cfb128") == 0) {
571 todo.aes_cfb128 = 1;
Yanray Wang022b9a12023-09-12 09:45:37 +0800572 } else if (strcmp(argv[i], "aes_cfb8") == 0) {
573 todo.aes_cfb8 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 } else if (strcmp(argv[i], "aes_xts") == 0) {
Aorimn5f778012016-06-09 23:22:58 +0200575 todo.aes_xts = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 } else if (strcmp(argv[i], "aes_gcm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200577 todo.aes_gcm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 } else if (strcmp(argv[i], "aes_ccm") == 0) {
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200579 todo.aes_ccm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 } else if (strcmp(argv[i], "chachapoly") == 0) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200581 todo.chachapoly = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 } else if (strcmp(argv[i], "aes_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100583 todo.aes_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 } else if (strcmp(argv[i], "des3_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100585 todo.des3_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 } else if (strcmp(argv[i], "aria") == 0) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100587 todo.aria = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else if (strcmp(argv[i], "camellia") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200589 todo.camellia = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else if (strcmp(argv[i], "chacha20") == 0) {
Daniel King34b822c2016-05-15 17:28:08 -0300591 todo.chacha20 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 } else if (strcmp(argv[i], "poly1305") == 0) {
Daniel Kingadc32c02016-05-16 18:25:45 -0300593 todo.poly1305 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200595 todo.ctr_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100597 todo.hmac_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 } else if (strcmp(argv[i], "rsa") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200599 todo.rsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 } else if (strcmp(argv[i], "dhm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200601 todo.dhm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 } else if (strcmp(argv[i], "ecdsa") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200603 todo.ecdsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 } else if (strcmp(argv[i], "ecdh") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200605 todo.ecdh = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200607#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 else if (set_ecp_curve(argv[i], single_curve)) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200609 curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200611#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 else {
613 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
614 mbedtls_printf("Available options: " OPTIONS);
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200615 }
616 }
617 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000623#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 memset(buf, 0xAA, sizeof(buf));
625 memset(tmp, 0xBB, sizeof(tmp));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200626
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100627 /* Avoid "unused static function" warning in configurations without
628 * symmetric crypto. */
Manuel Pégourié-Gonnardcd4ad0c2022-01-05 09:54:37 +0100629 (void) mbedtls_timing_hardclock;
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (todo.md5) {
633 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
634 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000635#endif
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (todo.ripemd160) {
639 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
640 }
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100641#endif
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (todo.sha1) {
645 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
646 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000647#endif
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (todo.sha256) {
651 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
652 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000653#endif
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (todo.sha512) {
657 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
658 }
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000659#endif
Pol Henarejosebb36402022-05-20 14:26:00 +0200660#if defined(MBEDTLS_SHA3_C)
Pol Henarejosa6779282023-02-08 00:50:04 +0100661 if (todo.sha3_224) {
662 TIME_AND_TSC("SHA3-224", mbedtls_sha3(MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28));
663 }
664 if (todo.sha3_256) {
665 TIME_AND_TSC("SHA3-256", mbedtls_sha3(MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32));
666 }
667 if (todo.sha3_384) {
668 TIME_AND_TSC("SHA3-384", mbedtls_sha3(MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48));
669 }
670 if (todo.sha3_512) {
671 TIME_AND_TSC("SHA3-512", mbedtls_sha3(MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64));
672 }
Pol Henarejosebb36402022-05-20 14:26:00 +0200673#endif
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000674
Simon Butcher549dc3d2016-10-05 14:14:19 +0100675#if defined(MBEDTLS_DES_C)
676#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (todo.des3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_des3_context des3;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_des3_init(&des3);
681 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
682 mbedtls_exit(1);
683 }
684 TIME_AND_TSC("3DES",
685 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
686 mbedtls_des3_free(&des3);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200687 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (todo.des) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_des_context des;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 mbedtls_des_init(&des);
693 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
694 mbedtls_exit(1);
695 }
696 TIME_AND_TSC("DES",
697 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
698 mbedtls_des_free(&des);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200699 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100700
701#endif /* MBEDTLS_CIPHER_MODE_CBC */
702#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (todo.des3_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100704 unsigned char output[8];
705 const mbedtls_cipher_info_t *cipher_info;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 memset(buf, 0, sizeof(buf));
708 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 TIME_AND_TSC("3DES-CMAC",
713 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
714 BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100715 }
716#endif /* MBEDTLS_CMAC_C */
717#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719#if defined(MBEDTLS_AES_C)
720#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (todo.aes_cbc) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100722 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_aes_init(&aes);
726 for (keysize = 128; keysize <= 256; keysize += 64) {
727 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 memset(buf, 0, sizeof(buf));
730 memset(tmp, 0, sizeof(tmp));
731 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TIME_AND_TSC(title,
734 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200735 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_aes_free(&aes);
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200738#endif
Yanray Wang55aba192023-09-12 09:03:50 +0800739#if defined(MBEDTLS_CIPHER_MODE_CFB)
740 if (todo.aes_cfb128) {
741 int keysize;
742 size_t iv_off = 0;
743 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800744
Yanray Wang55aba192023-09-12 09:03:50 +0800745 mbedtls_aes_init(&aes);
746 for (keysize = 128; keysize <= 256; keysize += 64) {
747 mbedtls_snprintf(title, sizeof(title), "AES-CFB128-%d", keysize);
748
749 memset(buf, 0, sizeof(buf));
750 memset(tmp, 0, sizeof(tmp));
751 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
752
753 TIME_AND_TSC(title,
754 mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE,
755 &iv_off, tmp, buf, buf));
756 }
757 mbedtls_aes_free(&aes);
758 }
Yanray Wang022b9a12023-09-12 09:45:37 +0800759 if (todo.aes_cfb8) {
760 int keysize;
761 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800762
Yanray Wang022b9a12023-09-12 09:45:37 +0800763 mbedtls_aes_init(&aes);
764 for (keysize = 128; keysize <= 256; keysize += 64) {
765 mbedtls_snprintf(title, sizeof(title), "AES-CFB8-%d", keysize);
766
767 memset(buf, 0, sizeof(buf));
768 memset(tmp, 0, sizeof(tmp));
769 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
770
771 TIME_AND_TSC(title,
772 mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
773 }
774 mbedtls_aes_free(&aes);
775 }
Yanray Wang55aba192023-09-12 09:03:50 +0800776#endif
Aorimn5f778012016-06-09 23:22:58 +0200777#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (todo.aes_xts) {
Aorimn5f778012016-06-09 23:22:58 +0200779 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100780 mbedtls_aes_xts_context ctx;
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 mbedtls_aes_xts_init(&ctx);
783 for (keysize = 128; keysize <= 256; keysize += 128) {
784 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
Aorimn5f778012016-06-09 23:22:58 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 memset(buf, 0, sizeof(buf));
787 memset(tmp, 0, sizeof(tmp));
788 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
Aorimn5f778012016-06-09 23:22:58 +0200789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 TIME_AND_TSC(title,
791 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
792 tmp, buf, buf));
Jaeden Amero9366feb2018-05-29 18:55:17 +0100793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 mbedtls_aes_xts_free(&ctx);
Aorimn5f778012016-06-09 23:22:58 +0200795 }
Aorimn5f778012016-06-09 23:22:58 +0200796 }
797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (todo.aes_gcm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100800 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 mbedtls_gcm_init(&gcm);
804 for (keysize = 128; keysize <= 256; keysize += 64) {
805 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 memset(buf, 0, sizeof(buf));
808 memset(tmp, 0, sizeof(tmp));
809 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 TIME_AND_TSC(title,
812 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
813 12, NULL, 0, buf, buf, 16, tmp));
Paul Bakkerf70fe812013-12-16 16:43:10 +0100814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_gcm_free(&gcm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200816 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000817 }
818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (todo.aes_ccm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100821 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 mbedtls_ccm_init(&ccm);
825 for (keysize = 128; keysize <= 256; keysize += 64) {
826 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 memset(buf, 0, sizeof(buf));
829 memset(tmp, 0, sizeof(tmp));
830 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 TIME_AND_TSC(title,
833 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
834 12, NULL, 0, buf, buf, tmp, 16));
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 mbedtls_ccm_free(&ccm);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200837 }
838 }
839#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200840#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (todo.chachapoly) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200842 mbedtls_chachapoly_context chachapoly;
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_chachapoly_init(&chachapoly);
845 memset(buf, 0, sizeof(buf));
846 memset(tmp, 0, sizeof(tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 mbedtls_chachapoly_setkey(&chachapoly, tmp);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 TIME_AND_TSC(title,
853 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
854 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 mbedtls_chachapoly_free(&chachapoly);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200857 }
858#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100859#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (todo.aes_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100861 unsigned char output[16];
862 const mbedtls_cipher_info_t *cipher_info;
863 mbedtls_cipher_type_t cipher_type;
864 int keysize;
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100867 keysize <= 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 keysize += 64, cipher_type++) {
869 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 memset(buf, 0, sizeof(buf));
872 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 TIME_AND_TSC(title,
877 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
878 buf, BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100879 }
880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 memset(buf, 0, sizeof(buf));
882 memset(tmp, 0, sizeof(tmp));
883 TIME_AND_TSC("AES-CMAC-PRF-128",
884 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
885 output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100886 }
887#endif /* MBEDTLS_CMAC_C */
888#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100890#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (todo.aria) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100892 int keysize;
893 mbedtls_aria_context aria;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 mbedtls_aria_init(&aria);
896 for (keysize = 128; keysize <= 256; keysize += 64) {
897 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 memset(buf, 0, sizeof(buf));
900 memset(tmp, 0, sizeof(tmp));
901 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 TIME_AND_TSC(title,
904 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
905 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100906 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_aria_free(&aria);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100908 }
909#endif
910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (todo.camellia) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100913 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_camellia_context camellia;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_camellia_init(&camellia);
917 for (keysize = 128; keysize <= 256; keysize += 64) {
918 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 memset(buf, 0, sizeof(buf));
921 memset(tmp, 0, sizeof(tmp));
922 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 TIME_AND_TSC(title,
925 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
926 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200927 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 mbedtls_camellia_free(&camellia);
Paul Bakker38119b12009-01-10 23:31:23 +0000929 }
930#endif
931
Daniel King34b822c2016-05-15 17:28:08 -0300932#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (todo.chacha20) {
934 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
Daniel King34b822c2016-05-15 17:28:08 -0300935 }
936#endif
937
Daniel Kingadc32c02016-05-16 18:25:45 -0300938#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (todo.poly1305) {
940 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
Daniel Kingadc32c02016-05-16 18:25:45 -0300941 }
942#endif
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (todo.ctr_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 mbedtls_ctr_drbg_init(&ctr_drbg);
949 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 }
952 TIME_AND_TSC("CTR_DRBG (NOPR)",
953 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
954 mbedtls_ctr_drbg_free(&ctr_drbg);
Paul Bakker02faf452011-11-29 11:23:58 +0000955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_ctr_drbg_init(&ctr_drbg);
957 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 }
960 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
961 TIME_AND_TSC("CTR_DRBG (PR)",
962 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
963 mbedtls_ctr_drbg_free(&ctr_drbg);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200964 }
Paul Bakker02faf452011-11-29 11:23:58 +0000965#endif
966
Andrzej Kurek68327742022-10-03 06:18:18 -0400967#if defined(MBEDTLS_HMAC_DRBG_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
969 if (todo.hmac_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_hmac_drbg_context hmac_drbg;
971 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 mbedtls_hmac_drbg_init(&hmac_drbg);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100979
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 }
983 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
984 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
989 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
990 MBEDTLS_HMAC_DRBG_PR_ON);
991 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
992 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100993#endif
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 }
1003 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
1004 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 }
1009 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1010 MBEDTLS_HMAC_DRBG_PR_ON);
1011 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
1012 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001013#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_hmac_drbg_free(&hmac_drbg);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001015 }
Andrzej Kurek68327742022-10-03 06:18:18 -04001016#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (todo.rsa) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +01001020 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_rsa_context rsa;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 for (keysize = 2048; keysize <= 4096; keysize *= 2) {
1024 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_rsa_init(&rsa);
1027 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 TIME_PUBLIC(title, " public",
1030 buf[0] = 0;
1031 ret = mbedtls_rsa_public(&rsa, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 TIME_PUBLIC(title, "private",
1034 buf[0] = 0;
1035 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001038 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040#endif
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (todo.dhm) {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +02001044 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -08001045 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001046 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001047 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001048 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001049 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001050 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001051 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001052 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
1053
1054 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
1056 sizeof(dhm_P_3072) };
Hanno Beckerb9539212017-10-04 13:13:34 +01001057
1058 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
1060 sizeof(dhm_G_3072) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001063 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +02001064 size_t n;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
1067 mbedtls_dhm_init(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (mbedtls_mpi_read_binary(&dhm.P, dhm_P[i],
1070 dhm_P_size[i]) != 0 ||
1071 mbedtls_mpi_read_binary(&dhm.G, dhm_G[i],
1072 dhm_G_size[i]) != 0) {
1073 mbedtls_exit(1);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +02001074 }
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 n = mbedtls_mpi_size(&dhm.P);
1077 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
1078 if (mbedtls_mpi_copy(&dhm.GY, &dhm.GX) != 0) {
1079 mbedtls_exit(1);
1080 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
1083 TIME_PUBLIC(title, "handshake",
1084 ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
1085 myrand, NULL);
1086 ret |=
1087 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
1090 TIME_PUBLIC(title, "handshake",
1091 ret |=
1092 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_dhm_free(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001095 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001096 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001097#endif
1098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (todo.ecdsa) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_ecdsa_context ecdsa;
1102 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001103 size_t sig_len;
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 memset(buf, 0x2A, sizeof(buf));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 curve_info++) {
1110 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +00001111 continue;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001112 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
1117 mbedtls_exit(1);
1118 }
1119
1120 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1121 curve_info->name);
1122 TIME_PUBLIC(title,
1123 "sign",
1124 ret =
1125 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
1126 curve_info->bit_size,
1127 tmp, sizeof(tmp), &sig_len, myrand,
1128 NULL));
1129
1130 mbedtls_ecdsa_free(&ecdsa);
1131 }
1132
1133 for (curve_info = curve_list;
1134 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1135 curve_info++) {
1136 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1137 continue;
1138 }
1139
1140 mbedtls_ecdsa_init(&ecdsa);
1141
1142 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
1143 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1144 tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
1145 mbedtls_exit(1);
1146 }
1147
1148 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1149 curve_info->name);
1150 TIME_PUBLIC(title, "verify",
1151 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
1152 tmp, sig_len));
1153
1154 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001155 }
1156 }
1157#endif
1158
Janos Follath52735ef2018-08-15 10:19:16 +01001159#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (todo.ecdh) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001163 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
1164#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
1165 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001166#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001167#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
1168 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
1169#endif
1170 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
1171 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001173 size_t olen;
Gilles Peskine28f62f62020-07-24 02:06:46 +02001174 const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
1175 montgomery_curve_list;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (curve_list == (const mbedtls_ecp_curve_info *) &single_curve) {
Gilles Peskine28f62f62020-07-24 02:06:46 +02001178 mbedtls_ecp_group grp;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 mbedtls_ecp_group_init(&grp);
1181 if (mbedtls_ecp_group_load(&grp, curve_list->grp_id) != 0) {
1182 mbedtls_exit(1);
1183 }
1184 if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Gilles Peskine28f62f62020-07-24 02:06:46 +02001185 selected_montgomery_curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 } else { /* empty list */
Gilles Peskine28f62f62020-07-24 02:06:46 +02001187 selected_montgomery_curve_list = single_curve + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 }
1189 mbedtls_ecp_group_free(&grp);
Gilles Peskine28f62f62020-07-24 02:06:46 +02001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 curve_info++) {
1195 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001196 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 mbedtls_ecdh_init(&ecdh);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1202 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1203 myrand, NULL));
1204 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1207 curve_info->name);
1208 TIME_PUBLIC(title, "handshake",
1209 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1210 myrand, NULL));
1211 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1212 myrand, NULL)));
1213 mbedtls_ecdh_free(&ecdh);
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001214 }
1215
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001216 /* Montgomery curves need to be handled separately */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 for (curve_info = selected_montgomery_curve_list;
1218 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1219 curve_info++) {
1220 mbedtls_ecdh_init(&ecdh);
1221 mbedtls_mpi_init(&z);
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1224 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1227 curve_info->name);
1228 TIME_PUBLIC(title, "handshake",
1229 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q,
1230 myrand, NULL));
1231 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1232 &ecdh.d,
1233 myrand, NULL)));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_ecdh_free(&ecdh);
1236 mbedtls_mpi_free(&z);
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001237 }
1238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 curve_info++) {
1242 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001243 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 mbedtls_ecdh_init(&ecdh);
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001247
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1249 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1250 myrand, NULL));
1251 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
1252 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1253 myrand, NULL));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1256 curve_info->name);
1257 TIME_PUBLIC(title, "handshake",
1258 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1259 myrand, NULL)));
1260 mbedtls_ecdh_free(&ecdh);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001261 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001262
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001263 /* Montgomery curves need to be handled separately */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 for (curve_info = selected_montgomery_curve_list;
1265 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1266 curve_info++) {
1267 mbedtls_ecdh_init(&ecdh);
1268 mbedtls_mpi_init(&z);
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1271 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp,
1272 myrand, NULL));
1273 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1276 curve_info->name);
1277 TIME_PUBLIC(title, "handshake",
1278 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1279 &ecdh.d,
1280 myrand, NULL)));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 mbedtls_ecdh_free(&ecdh);
1283 mbedtls_mpi_free(&z);
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001284 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001285 }
1286#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001287
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001288#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if (todo.ecdh) {
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001290 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1291 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001292 const mbedtls_ecp_curve_info *curve_info;
1293 size_t olen;
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 for (curve_info = curve_list;
1296 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1297 curve_info++) {
1298 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001299 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 mbedtls_ecdh_init(&ecdh_srv);
1303 mbedtls_ecdh_init(&ecdh_cli);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1306 TIME_PUBLIC(title,
1307 "full handshake",
1308 const unsigned char *p_srv = buf_srv;
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1311 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &olen, buf_srv,
1312 sizeof(buf_srv), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1315 p_srv + olen));
1316 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &olen, buf_cli,
1317 sizeof(buf_cli), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 CHECK_AND_CONTINUE(mbedtls_ecdh_read_public(&ecdh_srv, buf_cli, olen));
1320 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_srv, &olen, buf_srv,
1321 sizeof(buf_srv), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &olen, buf_cli,
1324 sizeof(buf_cli), myrand, NULL));
1325 mbedtls_ecdh_free(&ecdh_cli);
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_ecdh_free(&ecdh_srv);
1328 );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001329
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001330 }
1331 }
1332#endif
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 mbedtls_printf("\n");
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1337 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001338#endif
1339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 mbedtls_exit(0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001341}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001342
Andrzej Kurek6056e7a2022-03-02 12:01:10 -05001343#endif /* MBEDTLS_HAVE_TIME */