blob: 98ddd48534e60dfc06318aa9572a806b374e5819 [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050012#if !defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010013int main(void)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000014{
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050015 mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010016 mbedtls_exit(0);
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000017}
18#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010019
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000020#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020021#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010022
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/md5.h"
24#include "mbedtls/ripemd160.h"
25#include "mbedtls/sha1.h"
26#include "mbedtls/sha256.h"
27#include "mbedtls/sha512.h"
Pol Henarejosebb36402022-05-20 14:26:00 +020028#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/des.h"
31#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010032#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030034#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/gcm.h"
36#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020037#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010038#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030039#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010040
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ctr_drbg.h"
42#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010043
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/rsa.h"
45#include "mbedtls/dhm.h"
46#include "mbedtls/ecdsa.h"
47#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010048
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
David Horstmanne3d8f312023-01-03 11:07:09 +000051/* *INDENT-OFF* */
TRodziewicz90f304f2021-06-11 11:56:47 +020052#ifndef asm
53#define asm __asm
54#endif
David Horstmanne3d8f312023-01-03 11:07:09 +000055/* *INDENT-ON* */
TRodziewicz90f304f2021-06-11 11:56:47 +020056
TRodziewiczd8540832021-06-10 15:16:50 +020057#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
58
Alvaro Segura673e1eb2025-06-04 23:31:35 +020059#if defined(_MSC_VER)
60#pragma warning(disable : 5105) // warning inside winbase.h in C11 mode
61#endif
62
TRodziewiczd8540832021-06-10 15:16:50 +020063#include <windows.h>
64#include <process.h>
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020067 LARGE_INTEGER start;
68};
69
70#else
71
72#include <unistd.h>
73#include <sys/types.h>
74#include <sys/time.h>
75#include <signal.h>
76#include <time.h>
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020079 struct timeval start;
80};
81
82#endif /* _WIN32 && !EFIX64 && !EFI32 */
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000085#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000086#endif
87
Matthias Schulz3b9240b2023-11-16 17:39:43 +010088#ifdef MBEDTLS_TIMING_ALT
89void mbedtls_set_alarm(int seconds);
90unsigned long mbedtls_timing_hardclock(void);
91extern volatile int mbedtls_timing_alarmed;
92#else
Gilles Peskine449bd832023-01-11 14:50:10 +010093static void mbedtls_set_alarm(int seconds);
Matthias Schulz3b9240b2023-11-16 17:39:43 +010094static unsigned long mbedtls_timing_hardclock(void);
95#endif
TRodziewiczd8540832021-06-10 15:16:50 +020096
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000097/*
98 * For heap usage estimates, we need an estimate of the overhead per allocated
99 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
100 * so use that as our baseline.
101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102#define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000103
104/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200105 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000106 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +0100107#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000108
Paul Bakker02faf452011-11-29 11:23:58 +0000109#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100110#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100111#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000112
Yanray Wang022b9a12023-09-12 09:45:37 +0800113#define OPTIONS \
114 "md5, ripemd160, sha1, sha256, sha512,\n" \
115 "sha3_224, sha3_256, sha3_384, sha3_512,\n" \
116 "des3, des, camellia, chacha20,\n" \
117 "aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly\n" \
118 "aes_cmac, des3_cmac, poly1305\n" \
119 "ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000120 "rsa, dhm, ecdsa, ecdh.\n"
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000123#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
125 mbedtls_printf("FAILED: %s\n", tmp);
Rich Evans85b05ec2015-02-12 11:37:29 +0000126#else
127#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
Rich Evans85b05ec2015-02-12 11:37:29 +0000129#endif
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131#define TIME_AND_TSC(TITLE, CODE) \
132 do { \
133 unsigned long ii, jj, tsc; \
134 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_printf(HEADER_FORMAT, TITLE); \
137 fflush(stdout); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000138 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 mbedtls_set_alarm(1); \
140 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
141 { \
142 ret = CODE; \
143 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000144 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 tsc = mbedtls_timing_hardclock(); \
146 for (jj = 0; ret == 0 && jj < 1024; jj++) \
147 { \
148 ret = CODE; \
149 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000150 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (ret != 0) \
152 { \
153 PRINT_ERROR; \
154 } \
155 else \
156 { \
157 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
158 ii * BUFSIZE / 1024, \
159 (mbedtls_timing_hardclock() - tsc) \
160 / (jj * BUFSIZE)); \
161 } \
162 } while (0)
Rich Evans85b05ec2015-02-12 11:37:29 +0000163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100165
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200166/* How much space to reserve for the title when printing heap usage results.
167 * Updated manually as the output of the following command:
168 *
169 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100170 * awk '{print length+3}' | sort -rn | head -n1
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200171 *
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100172 * This computes the maximum length of a title +3, because we appends "/s" and
173 * want at least one space. (If the value is too small, the only consequence
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800174 * is poor alignment.) */
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100175#define TITLE_SPACE 17
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200176
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100177#define MEMORY_MEASURE_INIT \
178 size_t max_used, max_blocks, max_bytes; \
179 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnard6ced0022022-01-05 10:05:54 +0100180 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
182 mbedtls_memory_buffer_alloc_max_reset();
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100183
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100184#define MEMORY_MEASURE_RESET \
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187#define MEMORY_MEASURE_PRINT(title_len) \
188 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
189 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200190 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 while (ii--) mbedtls_printf(" "); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100192 max_used -= prv_used; \
193 max_blocks -= prv_blocks; \
194 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_printf("%6u heap bytes, %6u allocs", \
196 (unsigned) max_bytes, \
197 (unsigned) (alloc_cnt - prv_alloc));
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100198
199#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000200#define MEMORY_MEASURE_INIT
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100201#define MEMORY_MEASURE_RESET
Gilles Peskine449bd832023-01-11 14:50:10 +0100202#define MEMORY_MEASURE_PRINT(title_len)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100203#endif
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205#define TIME_PUBLIC(TITLE, TYPE, CODE) \
206 do { \
207 unsigned long ii; \
208 int ret; \
209 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000210 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_printf(HEADER_FORMAT, TITLE); \
212 fflush(stdout); \
213 mbedtls_set_alarm(3); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000214 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 ret = 0; \
216 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
217 { \
218 MEMORY_MEASURE_RESET; \
219 CODE; \
220 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000221 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
223 { \
224 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
225 ret = 0; \
226 } \
227 else if (ret != 0) \
228 { \
229 PRINT_ERROR; \
230 } \
231 else \
232 { \
233 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
234 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
235 mbedtls_printf("\n"); \
236 } \
237 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100239#if !defined(MBEDTLS_TIMING_ALT)
TRodziewiczd8540832021-06-10 15:16:50 +0200240#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
TRodziewiczd8540832021-06-10 15:16:50 +0200242
243#define HAVE_HARDCLOCK
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200246{
247 unsigned long tsc;
248 __asm rdtsc
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 __asm mov[tsc], eax
250 return tsc;
TRodziewiczd8540832021-06-10 15:16:50 +0200251}
252#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
253 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
254
255/* some versions of mingw-64 have 32-bit longs even on x84_64 */
256#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 defined(__GNUC__) && (defined(__i386__) || ( \
258 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
TRodziewiczd8540832021-06-10 15:16:50 +0200259
260#define HAVE_HARDCLOCK
261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200263{
264 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
266 return lo;
TRodziewiczd8540832021-06-10 15:16:50 +0200267}
268#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
269 __GNUC__ && __i386__ */
270
271#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
TRodziewiczd8540832021-06-10 15:16:50 +0200273
274#define HAVE_HARDCLOCK
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200277{
278 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
280 return lo | (hi << 32);
TRodziewiczd8540832021-06-10 15:16:50 +0200281}
282#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
283 __GNUC__ && ( __amd64__ || __x86_64__ ) */
284
285#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
TRodziewiczd8540832021-06-10 15:16:50 +0200287
288#define HAVE_HARDCLOCK
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200291{
292 unsigned long tbl, tbu0, tbu1;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 do {
295 asm volatile ("mftbu %0" : "=r" (tbu0));
296 asm volatile ("mftb %0" : "=r" (tbl));
297 asm volatile ("mftbu %0" : "=r" (tbu1));
298 } while (tbu0 != tbu1);
TRodziewiczd8540832021-06-10 15:16:50 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return tbl;
TRodziewiczd8540832021-06-10 15:16:50 +0200301}
302#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
303 __GNUC__ && ( __powerpc__ || __ppc__ ) */
304
305#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
306 defined(__GNUC__) && defined(__sparc64__)
307
308#if defined(__OpenBSD__)
309#warning OpenBSD does not allow access to tick register using software version instead
310#else
311#define HAVE_HARDCLOCK
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200314{
315 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
317 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200318}
319#endif /* __OpenBSD__ */
320#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
321 __GNUC__ && __sparc64__ */
322
323#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
324 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
325
326#define HAVE_HARDCLOCK
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200329{
330 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
332 asm volatile ("mov %%g1, %0" : "=r" (tick));
333 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200334}
335#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
336 __GNUC__ && __sparc__ && !__sparc64__ */
337
338#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
339 defined(__GNUC__) && defined(__alpha__)
340
341#define HAVE_HARDCLOCK
342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200344{
345 unsigned long cc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 asm volatile ("rpcc %0" : "=r" (cc));
347 return cc & 0xFFFFFFFF;
TRodziewiczd8540832021-06-10 15:16:50 +0200348}
349#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
350 __GNUC__ && __alpha__ */
351
352#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
353 defined(__GNUC__) && defined(__ia64__)
354
355#define HAVE_HARDCLOCK
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200358{
359 unsigned long itc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
361 return itc;
TRodziewiczd8540832021-06-10 15:16:50 +0200362}
363#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
364 __GNUC__ && __ia64__ */
365
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300366#if !defined(HAVE_HARDCLOCK) && defined(_WIN32) && \
TRodziewiczd8540832021-06-10 15:16:50 +0200367 !defined(EFIX64) && !defined(EFI32)
368
369#define HAVE_HARDCLOCK
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200372{
373 LARGE_INTEGER offset;
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 QueryPerformanceCounter(&offset);
TRodziewiczd8540832021-06-10 15:16:50 +0200376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return (unsigned long) (offset.QuadPart);
TRodziewiczd8540832021-06-10 15:16:50 +0200378}
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300379#endif /* !HAVE_HARDCLOCK && _WIN32 && !EFIX64 && !EFI32 */
TRodziewiczd8540832021-06-10 15:16:50 +0200380
381#if !defined(HAVE_HARDCLOCK)
382
383#define HAVE_HARDCLOCK
384
385static int hardclock_init = 0;
386static struct timeval tv_init;
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200389{
390 struct timeval tv_cur;
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (hardclock_init == 0) {
393 gettimeofday(&tv_init, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200394 hardclock_init = 1;
395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 gettimeofday(&tv_cur, NULL);
398 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
399 + (tv_cur.tv_usec - tv_init.tv_usec);
TRodziewiczd8540832021-06-10 15:16:50 +0200400}
401#endif /* !HAVE_HARDCLOCK */
402
403volatile int mbedtls_timing_alarmed = 0;
404
405#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
406
407/* It's OK to use a global because alarm() is supposed to be global anyway */
408static DWORD alarmMs;
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410static void TimerProc(void *TimerContext)
TRodziewiczd8540832021-06-10 15:16:50 +0200411{
412 (void) TimerContext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 Sleep(alarmMs);
TRodziewiczd8540832021-06-10 15:16:50 +0200414 mbedtls_timing_alarmed = 1;
415 /* _endthread will be called implicitly on return
Tom Cosgrove1797b052022-12-04 17:19:59 +0000416 * That ensures execution of thread function's epilogue */
TRodziewiczd8540832021-06-10 15:16:50 +0200417}
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200420{
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200422 /* No need to create a thread for this simple case.
423 * Also, this shorcut is more reliable at least on MinGW32 */
424 mbedtls_timing_alarmed = 1;
425 return;
426 }
427
428 mbedtls_timing_alarmed = 0;
429 alarmMs = seconds * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 (void) _beginthread(TimerProc, 0, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200431}
432
433#else /* _WIN32 && !EFIX64 && !EFI32 */
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static void sighandler(int signum)
TRodziewiczd8540832021-06-10 15:16:50 +0200436{
437 mbedtls_timing_alarmed = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 signal(signum, sighandler);
TRodziewiczd8540832021-06-10 15:16:50 +0200439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200442{
443 mbedtls_timing_alarmed = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 signal(SIGALRM, sighandler);
445 alarm(seconds);
446 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200447 /* alarm(0) cancelled any previous pending alarm, but the
448 handler won't fire, so raise the flag straight away. */
449 mbedtls_timing_alarmed = 1;
450 }
451}
452
453#endif /* _WIN32 && !EFIX64 && !EFI32 */
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100454#endif /* !MBEDTLS_TIMING_ALT */
TRodziewiczd8540832021-06-10 15:16:50 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000457{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000458 size_t use_len;
459 int rnd;
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (rng_state != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 while (len > 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000466 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (use_len > sizeof(int)) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000468 use_len = sizeof(int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 }
Paul Bakkera3d195c2011-11-27 21:07:34 +0000470
471 rnd = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 memcpy(output, &rnd, use_len);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000473 output += use_len;
474 len -= use_len;
475 }
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000478}
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480#define CHECK_AND_CONTINUE(R) \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000481 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 int CHECK_AND_CONTINUE_ret = (R); \
483 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
484 mbedtls_printf("Feature not supported. Skipping.\n"); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000485 continue; \
486 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 else if (CHECK_AND_CONTINUE_ret != 0) { \
488 mbedtls_exit(1); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000489 } \
490 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000491
Gilles Peskine28f62f62020-07-24 02:06:46 +0200492#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100493static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
Gilles Peskine28f62f62020-07-24 02:06:46 +0200494{
495 const mbedtls_ecp_curve_info *found =
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 mbedtls_ecp_curve_info_from_name(string);
497 if (found != NULL) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200498 *curve = *found;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return 1;
500 } else {
501 return 0;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200502 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200503}
504#endif
505
Paul Bakker5121ce52009-01-03 21:22:43 +0000506unsigned char buf[BUFSIZE];
507
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200508typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200509 char md5, ripemd160, sha1, sha256, sha512,
Pol Henarejosebb36402022-05-20 14:26:00 +0200510 sha3_224, sha3_256, sha3_384, sha3_512,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200511 des3, des,
Dave Rodgman67223bb2024-01-12 16:37:07 +0000512 aes_cbc, aes_cfb128, aes_cfb8, aes_ctr, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200513 aes_cmac, des3_cmac,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200514 aria, camellia, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300515 poly1305,
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100516 ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200517 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200518} todo_list;
519
Simon Butcher63cb97e2018-12-06 17:43:31 +0000520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521int main(int argc, char *argv[])
Paul Bakker5690efc2011-05-26 13:16:06 +0000522{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100523 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200524 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200525 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200526 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200528 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000529#endif
Gilles Peskine28f62f62020-07-24 02:06:46 +0200530#if defined(MBEDTLS_ECP_C)
531 mbedtls_ecp_curve_info single_curve[2] = {
532 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
533 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
534 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
Gilles Peskine28f62f62020-07-24 02:06:46 +0200536#endif
537
538#if defined(MBEDTLS_ECP_C)
539 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
540#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (argc <= 1) {
543 memset(&todo, 1, sizeof(todo));
544 } else {
545 memset(&todo, 0, sizeof(todo));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 for (i = 1; i < argc; i++) {
548 if (strcmp(argv[i], "md5") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200549 todo.md5 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 } else if (strcmp(argv[i], "ripemd160") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200551 todo.ripemd160 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 } else if (strcmp(argv[i], "sha1") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200553 todo.sha1 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 } else if (strcmp(argv[i], "sha256") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200555 todo.sha256 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 } else if (strcmp(argv[i], "sha512") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200557 todo.sha512 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100558 } else if (strcmp(argv[i], "sha3_224") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200559 todo.sha3_224 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100560 } else if (strcmp(argv[i], "sha3_256") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200561 todo.sha3_256 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100562 } else if (strcmp(argv[i], "sha3_384") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200563 todo.sha3_384 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100564 } else if (strcmp(argv[i], "sha3_512") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200565 todo.sha3_512 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 } else if (strcmp(argv[i], "des3") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200567 todo.des3 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 } else if (strcmp(argv[i], "des") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200569 todo.des = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 } else if (strcmp(argv[i], "aes_cbc") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200571 todo.aes_cbc = 1;
Yanray Wang55aba192023-09-12 09:03:50 +0800572 } else if (strcmp(argv[i], "aes_cfb128") == 0) {
573 todo.aes_cfb128 = 1;
Yanray Wang022b9a12023-09-12 09:45:37 +0800574 } else if (strcmp(argv[i], "aes_cfb8") == 0) {
575 todo.aes_cfb8 = 1;
Dave Rodgman67223bb2024-01-12 16:37:07 +0000576 } else if (strcmp(argv[i], "aes_ctr") == 0) {
577 todo.aes_ctr = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 } else if (strcmp(argv[i], "aes_xts") == 0) {
Aorimn5f778012016-06-09 23:22:58 +0200579 todo.aes_xts = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 } else if (strcmp(argv[i], "aes_gcm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200581 todo.aes_gcm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 } else if (strcmp(argv[i], "aes_ccm") == 0) {
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200583 todo.aes_ccm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 } else if (strcmp(argv[i], "chachapoly") == 0) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200585 todo.chachapoly = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 } else if (strcmp(argv[i], "aes_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100587 todo.aes_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else if (strcmp(argv[i], "des3_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100589 todo.des3_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else if (strcmp(argv[i], "aria") == 0) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100591 todo.aria = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 } else if (strcmp(argv[i], "camellia") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200593 todo.camellia = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 } else if (strcmp(argv[i], "chacha20") == 0) {
Daniel King34b822c2016-05-15 17:28:08 -0300595 todo.chacha20 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 } else if (strcmp(argv[i], "poly1305") == 0) {
Daniel Kingadc32c02016-05-16 18:25:45 -0300597 todo.poly1305 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200599 todo.ctr_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100601 todo.hmac_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 } else if (strcmp(argv[i], "rsa") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200603 todo.rsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 } else if (strcmp(argv[i], "dhm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200605 todo.dhm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 } else if (strcmp(argv[i], "ecdsa") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200607 todo.ecdsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 } else if (strcmp(argv[i], "ecdh") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200609 todo.ecdh = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200611#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 else if (set_ecp_curve(argv[i], single_curve)) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200613 curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200615#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 else {
617 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
618 mbedtls_printf("Available options: " OPTIONS);
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200619 }
620 }
621 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000627#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 memset(buf, 0xAA, sizeof(buf));
629 memset(tmp, 0xBB, sizeof(tmp));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200630
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100631 /* Avoid "unused static function" warning in configurations without
632 * symmetric crypto. */
Manuel Pégourié-Gonnardcd4ad0c2022-01-05 09:54:37 +0100633 (void) mbedtls_timing_hardclock;
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (todo.md5) {
637 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639#endif
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (todo.ripemd160) {
643 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
644 }
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100645#endif
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (todo.sha1) {
649 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
650 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000651#endif
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (todo.sha256) {
655 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
656 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000657#endif
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (todo.sha512) {
661 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
662 }
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000663#endif
Pol Henarejosebb36402022-05-20 14:26:00 +0200664#if defined(MBEDTLS_SHA3_C)
Pol Henarejosa6779282023-02-08 00:50:04 +0100665 if (todo.sha3_224) {
666 TIME_AND_TSC("SHA3-224", mbedtls_sha3(MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28));
667 }
668 if (todo.sha3_256) {
669 TIME_AND_TSC("SHA3-256", mbedtls_sha3(MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32));
670 }
671 if (todo.sha3_384) {
672 TIME_AND_TSC("SHA3-384", mbedtls_sha3(MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48));
673 }
674 if (todo.sha3_512) {
675 TIME_AND_TSC("SHA3-512", mbedtls_sha3(MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64));
676 }
Pol Henarejosebb36402022-05-20 14:26:00 +0200677#endif
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000678
Simon Butcher549dc3d2016-10-05 14:14:19 +0100679#if defined(MBEDTLS_DES_C)
680#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (todo.des3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_des3_context des3;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 mbedtls_des3_init(&des3);
685 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
686 mbedtls_exit(1);
687 }
688 TIME_AND_TSC("3DES",
689 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
690 mbedtls_des3_free(&des3);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (todo.des) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_des_context des;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_des_init(&des);
697 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
698 mbedtls_exit(1);
699 }
700 TIME_AND_TSC("DES",
701 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
702 mbedtls_des_free(&des);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200703 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100704
705#endif /* MBEDTLS_CIPHER_MODE_CBC */
706#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (todo.des3_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100708 unsigned char output[8];
709 const mbedtls_cipher_info_t *cipher_info;
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 memset(buf, 0, sizeof(buf));
712 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 TIME_AND_TSC("3DES-CMAC",
717 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
718 BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100719 }
720#endif /* MBEDTLS_CMAC_C */
721#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_AES_C)
724#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (todo.aes_cbc) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100726 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 mbedtls_aes_init(&aes);
730 for (keysize = 128; keysize <= 256; keysize += 64) {
731 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 memset(buf, 0, sizeof(buf));
734 memset(tmp, 0, sizeof(tmp));
735 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 TIME_AND_TSC(title,
738 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200739 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 mbedtls_aes_free(&aes);
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200742#endif
Yanray Wang55aba192023-09-12 09:03:50 +0800743#if defined(MBEDTLS_CIPHER_MODE_CFB)
744 if (todo.aes_cfb128) {
745 int keysize;
746 size_t iv_off = 0;
747 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800748
Yanray Wang55aba192023-09-12 09:03:50 +0800749 mbedtls_aes_init(&aes);
750 for (keysize = 128; keysize <= 256; keysize += 64) {
751 mbedtls_snprintf(title, sizeof(title), "AES-CFB128-%d", keysize);
752
753 memset(buf, 0, sizeof(buf));
754 memset(tmp, 0, sizeof(tmp));
755 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
756
757 TIME_AND_TSC(title,
758 mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE,
759 &iv_off, tmp, buf, buf));
760 }
761 mbedtls_aes_free(&aes);
762 }
Yanray Wang022b9a12023-09-12 09:45:37 +0800763 if (todo.aes_cfb8) {
764 int keysize;
765 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800766
Yanray Wang022b9a12023-09-12 09:45:37 +0800767 mbedtls_aes_init(&aes);
768 for (keysize = 128; keysize <= 256; keysize += 64) {
769 mbedtls_snprintf(title, sizeof(title), "AES-CFB8-%d", keysize);
770
771 memset(buf, 0, sizeof(buf));
772 memset(tmp, 0, sizeof(tmp));
773 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
774
775 TIME_AND_TSC(title,
776 mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
777 }
778 mbedtls_aes_free(&aes);
779 }
Yanray Wang55aba192023-09-12 09:03:50 +0800780#endif
Dave Rodgman67223bb2024-01-12 16:37:07 +0000781#if defined(MBEDTLS_CIPHER_MODE_CTR)
782 if (todo.aes_ctr) {
783 int keysize;
784 mbedtls_aes_context aes;
785
786 uint8_t stream_block[16];
787 size_t nc_off;
788
789 mbedtls_aes_init(&aes);
790 for (keysize = 128; keysize <= 256; keysize += 64) {
791 mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
792
793 memset(buf, 0, sizeof(buf));
794 memset(tmp, 0, sizeof(tmp));
795 memset(stream_block, 0, sizeof(stream_block));
796 nc_off = 0;
797
798 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
799
800 TIME_AND_TSC(title, mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_off, tmp, stream_block,
801 buf, buf));
802 }
803 mbedtls_aes_free(&aes);
804 }
805#endif
Aorimn5f778012016-06-09 23:22:58 +0200806#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (todo.aes_xts) {
Aorimn5f778012016-06-09 23:22:58 +0200808 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100809 mbedtls_aes_xts_context ctx;
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_aes_xts_init(&ctx);
812 for (keysize = 128; keysize <= 256; keysize += 128) {
813 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
Aorimn5f778012016-06-09 23:22:58 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 memset(buf, 0, sizeof(buf));
816 memset(tmp, 0, sizeof(tmp));
817 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
Aorimn5f778012016-06-09 23:22:58 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 TIME_AND_TSC(title,
820 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
821 tmp, buf, buf));
Jaeden Amero9366feb2018-05-29 18:55:17 +0100822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_aes_xts_free(&ctx);
Aorimn5f778012016-06-09 23:22:58 +0200824 }
Aorimn5f778012016-06-09 23:22:58 +0200825 }
826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (todo.aes_gcm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100829 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 mbedtls_gcm_init(&gcm);
833 for (keysize = 128; keysize <= 256; keysize += 64) {
834 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 memset(buf, 0, sizeof(buf));
837 memset(tmp, 0, sizeof(tmp));
838 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 TIME_AND_TSC(title,
841 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
842 12, NULL, 0, buf, buf, 16, tmp));
Paul Bakkerf70fe812013-12-16 16:43:10 +0100843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_gcm_free(&gcm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200845 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000846 }
847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (todo.aes_ccm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100850 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 mbedtls_ccm_init(&ccm);
854 for (keysize = 128; keysize <= 256; keysize += 64) {
855 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 memset(buf, 0, sizeof(buf));
858 memset(tmp, 0, sizeof(tmp));
859 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 TIME_AND_TSC(title,
862 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
863 12, NULL, 0, buf, buf, tmp, 16));
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 mbedtls_ccm_free(&ccm);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200866 }
867 }
868#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200869#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (todo.chachapoly) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200871 mbedtls_chachapoly_context chachapoly;
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_chachapoly_init(&chachapoly);
874 memset(buf, 0, sizeof(buf));
875 memset(tmp, 0, sizeof(tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_chachapoly_setkey(&chachapoly, tmp);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 TIME_AND_TSC(title,
882 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
883 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 mbedtls_chachapoly_free(&chachapoly);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200886 }
887#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100888#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (todo.aes_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100890 unsigned char output[16];
891 const mbedtls_cipher_info_t *cipher_info;
892 mbedtls_cipher_type_t cipher_type;
893 int keysize;
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100896 keysize <= 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 keysize += 64, cipher_type++) {
898 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 memset(buf, 0, sizeof(buf));
901 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 TIME_AND_TSC(title,
906 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
907 buf, BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100908 }
909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 memset(buf, 0, sizeof(buf));
911 memset(tmp, 0, sizeof(tmp));
912 TIME_AND_TSC("AES-CMAC-PRF-128",
913 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
914 output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100915 }
916#endif /* MBEDTLS_CMAC_C */
917#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100919#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (todo.aria) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100921 int keysize;
922 mbedtls_aria_context aria;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 mbedtls_aria_init(&aria);
925 for (keysize = 128; keysize <= 256; keysize += 64) {
926 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 memset(buf, 0, sizeof(buf));
929 memset(tmp, 0, sizeof(tmp));
930 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 TIME_AND_TSC(title,
933 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
934 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100935 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 mbedtls_aria_free(&aria);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100937 }
938#endif
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (todo.camellia) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100942 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_camellia_context camellia;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 mbedtls_camellia_init(&camellia);
946 for (keysize = 128; keysize <= 256; keysize += 64) {
947 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 memset(buf, 0, sizeof(buf));
950 memset(tmp, 0, sizeof(tmp));
951 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 TIME_AND_TSC(title,
954 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
955 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200956 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 mbedtls_camellia_free(&camellia);
Paul Bakker38119b12009-01-10 23:31:23 +0000958 }
959#endif
960
Daniel King34b822c2016-05-15 17:28:08 -0300961#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (todo.chacha20) {
963 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
Daniel King34b822c2016-05-15 17:28:08 -0300964 }
965#endif
966
Daniel Kingadc32c02016-05-16 18:25:45 -0300967#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (todo.poly1305) {
969 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
Daniel Kingadc32c02016-05-16 18:25:45 -0300970 }
971#endif
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (todo.ctr_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 mbedtls_ctr_drbg_init(&ctr_drbg);
978 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 }
981 TIME_AND_TSC("CTR_DRBG (NOPR)",
982 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
983 mbedtls_ctr_drbg_free(&ctr_drbg);
Paul Bakker02faf452011-11-29 11:23:58 +0000984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_ctr_drbg_init(&ctr_drbg);
986 if (mbedtls_ctr_drbg_seed(&ctr_drbg, 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_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
990 TIME_AND_TSC("CTR_DRBG (PR)",
991 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
992 mbedtls_ctr_drbg_free(&ctr_drbg);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200993 }
Paul Bakker02faf452011-11-29 11:23:58 +0000994#endif
995
Andrzej Kurek68327742022-10-03 06:18:18 -0400996#if defined(MBEDTLS_HMAC_DRBG_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
998 if (todo.hmac_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_hmac_drbg_context hmac_drbg;
1000 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_hmac_drbg_init(&hmac_drbg);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +02001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 }
1012 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
1013 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 }
1018 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1019 MBEDTLS_HMAC_DRBG_PR_ON);
1020 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
1021 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001022#endif
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 }
1032 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
1033 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 }
1038 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1039 MBEDTLS_HMAC_DRBG_PR_ON);
1040 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
1041 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001042#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_hmac_drbg_free(&hmac_drbg);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001044 }
Andrzej Kurek68327742022-10-03 06:18:18 -04001045#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (todo.rsa) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +01001049 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 mbedtls_rsa_context rsa;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001051
Matthias Schulzaa7dffa2023-11-16 15:31:32 +01001052 for (keysize = 2048; keysize <= 4096; keysize += 1024) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_rsa_init(&rsa);
1056 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 TIME_PUBLIC(title, " public",
1059 buf[0] = 0;
1060 ret = mbedtls_rsa_public(&rsa, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 TIME_PUBLIC(title, "private",
1063 buf[0] = 0;
1064 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001067 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001068 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001069#endif
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (todo.dhm) {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +02001073 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -08001074 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001075 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001076 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001077 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001078 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001079 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001080 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001081 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
1082
1083 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
1085 sizeof(dhm_P_3072) };
Hanno Beckerb9539212017-10-04 13:13:34 +01001086
1087 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
1089 sizeof(dhm_G_3072) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001092 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +02001093 size_t n;
Minos Galanakis97489dc2024-01-12 16:47:52 +00001094 mbedtls_mpi P, G;
1095 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
Yanray Wang5cae6e82023-10-09 18:40:17 +08001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
1098 mbedtls_dhm_init(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001099
Minos Galanakis97489dc2024-01-12 16:47:52 +00001100 if (mbedtls_mpi_read_binary(&P, dhm_P[i],
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 dhm_P_size[i]) != 0 ||
Minos Galanakis97489dc2024-01-12 16:47:52 +00001102 mbedtls_mpi_read_binary(&G, dhm_G[i],
1103 dhm_G_size[i]) != 0 ||
1104 mbedtls_dhm_set_group(&dhm, &P, &G) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_exit(1);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +02001106 }
1107
Minos Galanakis97489dc2024-01-12 16:47:52 +00001108 n = mbedtls_dhm_get_len(&dhm);
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
Minos Galanakis8ee1b5f2024-01-15 15:54:19 +00001110
1111 if (mbedtls_dhm_read_public(&dhm, buf, n) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_exit(1);
1113 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
1116 TIME_PUBLIC(title, "handshake",
1117 ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
1118 myrand, NULL);
1119 ret |=
1120 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
1123 TIME_PUBLIC(title, "handshake",
1124 ret |=
1125 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_dhm_free(&dhm);
Minos Galanakis97489dc2024-01-12 16:47:52 +00001128 mbedtls_mpi_free(&P), mbedtls_mpi_free(&G);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001129 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001130 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001131#endif
1132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (todo.ecdsa) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_ecdsa_context ecdsa;
1136 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001137 size_t sig_len;
1138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 memset(buf, 0x2A, sizeof(buf));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 curve_info++) {
1144 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +00001145 continue;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001146 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
1151 mbedtls_exit(1);
1152 }
1153
1154 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1155 curve_info->name);
1156 TIME_PUBLIC(title,
1157 "sign",
1158 ret =
1159 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
1160 curve_info->bit_size,
1161 tmp, sizeof(tmp), &sig_len, myrand,
1162 NULL));
1163
1164 mbedtls_ecdsa_free(&ecdsa);
1165 }
1166
1167 for (curve_info = curve_list;
1168 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1169 curve_info++) {
1170 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1171 continue;
1172 }
1173
1174 mbedtls_ecdsa_init(&ecdsa);
1175
1176 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
1177 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1178 tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
1179 mbedtls_exit(1);
1180 }
1181
1182 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1183 curve_info->name);
1184 TIME_PUBLIC(title, "verify",
1185 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
1186 tmp, sig_len));
1187
1188 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001189 }
1190 }
1191#endif
1192
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001193#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (todo.ecdh) {
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001195 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1196 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001197 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001198 size_t params_len, publen, seclen;
1199
1200 for (curve_info = curve_list;
1201 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1202 curve_info++) {
1203 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1204 continue;
1205 }
1206
1207 mbedtls_ecdh_init(&ecdh_srv);
1208
1209 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1210 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &params_len, buf_srv,
1211 sizeof(buf_srv), myrand, NULL));
1212
1213 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1214 TIME_PUBLIC(title,
1215 "ephemeral handshake",
1216 const unsigned char *p_srv = buf_srv;
1217 mbedtls_ecdh_init(&ecdh_cli);
1218
1219 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1220 p_srv + params_len));
1221 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &publen, buf_cli,
1222 sizeof(buf_cli), myrand, NULL));
1223
1224 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &seclen, buf_cli,
1225 sizeof(buf_cli), myrand, NULL));
1226 mbedtls_ecdh_free(&ecdh_cli);
1227 );
1228
1229 mbedtls_ecdh_free(&ecdh_srv);
1230 }
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 for (curve_info = curve_list;
1233 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1234 curve_info++) {
1235 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001236 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 mbedtls_ecdh_init(&ecdh_srv);
1240 mbedtls_ecdh_init(&ecdh_cli);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001241
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001242 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1243 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &params_len, buf_srv,
1244 sizeof(buf_srv), myrand, NULL));
1245
1246 const unsigned char *p_srv = buf_srv;
1247 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1248 p_srv + params_len));
1249 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &publen, buf_cli,
1250 sizeof(buf_cli), myrand, NULL));
1251
1252
1253 mbedtls_snprintf(title, sizeof(title), "ECDH-%s", curve_info->name);
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 TIME_PUBLIC(title,
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001255 "static handshake",
1256 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &seclen, buf_cli,
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 sizeof(buf_cli), myrand, NULL));
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001259
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001260 mbedtls_ecdh_free(&ecdh_cli);
1261 mbedtls_ecdh_free(&ecdh_srv);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001262 }
1263 }
1264#endif
1265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_printf("\n");
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1269 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001270#endif
1271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 mbedtls_exit(0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001273}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001274
Andrzej Kurek6056e7a2022-03-02 12:01:10 -05001275#endif /* MBEDTLS_HAVE_TIME */