blob: 93c17291f265480cd9c507d1fbf7b6a33ead0446 [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
59#include <windows.h>
60#include <process.h>
61
Gilles Peskine449bd832023-01-11 14:50:10 +010062struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020063 LARGE_INTEGER start;
64};
65
66#else
67
68#include <unistd.h>
69#include <sys/types.h>
70#include <sys/time.h>
71#include <signal.h>
72#include <time.h>
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020075 struct timeval start;
76};
77
78#endif /* _WIN32 && !EFIX64 && !EFI32 */
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000082#endif
83
Matthias Schulz3b9240b2023-11-16 17:39:43 +010084#ifdef MBEDTLS_TIMING_ALT
85void mbedtls_set_alarm(int seconds);
86unsigned long mbedtls_timing_hardclock(void);
87extern volatile int mbedtls_timing_alarmed;
88#else
Gilles Peskine449bd832023-01-11 14:50:10 +010089static void mbedtls_set_alarm(int seconds);
Matthias Schulz3b9240b2023-11-16 17:39:43 +010090static unsigned long mbedtls_timing_hardclock(void);
91#endif
TRodziewiczd8540832021-06-10 15:16:50 +020092
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000093/*
94 * For heap usage estimates, we need an estimate of the overhead per allocated
95 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
96 * so use that as our baseline.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000099
100/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200101 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000102 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +0100103#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000104
Paul Bakker02faf452011-11-29 11:23:58 +0000105#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100106#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100107#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000108
Yanray Wang022b9a12023-09-12 09:45:37 +0800109#define OPTIONS \
110 "md5, ripemd160, sha1, sha256, sha512,\n" \
111 "sha3_224, sha3_256, sha3_384, sha3_512,\n" \
112 "des3, des, camellia, chacha20,\n" \
113 "aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly\n" \
114 "aes_cmac, des3_cmac, poly1305\n" \
115 "ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000116 "rsa, dhm, ecdsa, ecdh.\n"
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000119#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
121 mbedtls_printf("FAILED: %s\n", tmp);
Rich Evans85b05ec2015-02-12 11:37:29 +0000122#else
123#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
Rich Evans85b05ec2015-02-12 11:37:29 +0000125#endif
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127#define TIME_AND_TSC(TITLE, CODE) \
128 do { \
129 unsigned long ii, jj, tsc; \
130 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000131 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_printf(HEADER_FORMAT, TITLE); \
133 fflush(stdout); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000134 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_set_alarm(1); \
136 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
137 { \
138 ret = CODE; \
139 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000140 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 tsc = mbedtls_timing_hardclock(); \
142 for (jj = 0; ret == 0 && jj < 1024; jj++) \
143 { \
144 ret = CODE; \
145 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000146 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (ret != 0) \
148 { \
149 PRINT_ERROR; \
150 } \
151 else \
152 { \
153 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
154 ii * BUFSIZE / 1024, \
155 (mbedtls_timing_hardclock() - tsc) \
156 / (jj * BUFSIZE)); \
157 } \
158 } while (0)
Rich Evans85b05ec2015-02-12 11:37:29 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100161
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200162/* How much space to reserve for the title when printing heap usage results.
163 * Updated manually as the output of the following command:
164 *
165 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100166 * awk '{print length+3}' | sort -rn | head -n1
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200167 *
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100168 * This computes the maximum length of a title +3, because we appends "/s" and
169 * want at least one space. (If the value is too small, the only consequence
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800170 * is poor alignment.) */
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100171#define TITLE_SPACE 17
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200172
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100173#define MEMORY_MEASURE_INIT \
174 size_t max_used, max_blocks, max_bytes; \
175 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnard6ced0022022-01-05 10:05:54 +0100176 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
178 mbedtls_memory_buffer_alloc_max_reset();
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100179
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100180#define MEMORY_MEASURE_RESET \
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#define MEMORY_MEASURE_PRINT(title_len) \
184 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
185 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200186 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 while (ii--) mbedtls_printf(" "); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100188 max_used -= prv_used; \
189 max_blocks -= prv_blocks; \
190 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 mbedtls_printf("%6u heap bytes, %6u allocs", \
192 (unsigned) max_bytes, \
193 (unsigned) (alloc_cnt - prv_alloc));
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100194
195#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000196#define MEMORY_MEASURE_INIT
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100197#define MEMORY_MEASURE_RESET
Gilles Peskine449bd832023-01-11 14:50:10 +0100198#define MEMORY_MEASURE_PRINT(title_len)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100199#endif
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201#define TIME_PUBLIC(TITLE, TYPE, CODE) \
202 do { \
203 unsigned long ii; \
204 int ret; \
205 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000206 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_printf(HEADER_FORMAT, TITLE); \
208 fflush(stdout); \
209 mbedtls_set_alarm(3); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000210 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ret = 0; \
212 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
213 { \
214 MEMORY_MEASURE_RESET; \
215 CODE; \
216 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000217 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
219 { \
220 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
221 ret = 0; \
222 } \
223 else if (ret != 0) \
224 { \
225 PRINT_ERROR; \
226 } \
227 else \
228 { \
229 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
230 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
231 mbedtls_printf("\n"); \
232 } \
233 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100235#if !defined(MBEDTLS_TIMING_ALT)
TRodziewiczd8540832021-06-10 15:16:50 +0200236#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
TRodziewiczd8540832021-06-10 15:16:50 +0200238
239#define HAVE_HARDCLOCK
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200242{
243 unsigned long tsc;
244 __asm rdtsc
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 __asm mov[tsc], eax
246 return tsc;
TRodziewiczd8540832021-06-10 15:16:50 +0200247}
248#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
249 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
250
251/* some versions of mingw-64 have 32-bit longs even on x84_64 */
252#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 defined(__GNUC__) && (defined(__i386__) || ( \
254 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
TRodziewiczd8540832021-06-10 15:16:50 +0200255
256#define HAVE_HARDCLOCK
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200259{
260 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
262 return lo;
TRodziewiczd8540832021-06-10 15:16:50 +0200263}
264#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
265 __GNUC__ && __i386__ */
266
267#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
TRodziewiczd8540832021-06-10 15:16:50 +0200269
270#define HAVE_HARDCLOCK
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200273{
274 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
276 return lo | (hi << 32);
TRodziewiczd8540832021-06-10 15:16:50 +0200277}
278#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
279 __GNUC__ && ( __amd64__ || __x86_64__ ) */
280
281#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
TRodziewiczd8540832021-06-10 15:16:50 +0200283
284#define HAVE_HARDCLOCK
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200287{
288 unsigned long tbl, tbu0, tbu1;
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 do {
291 asm volatile ("mftbu %0" : "=r" (tbu0));
292 asm volatile ("mftb %0" : "=r" (tbl));
293 asm volatile ("mftbu %0" : "=r" (tbu1));
294 } while (tbu0 != tbu1);
TRodziewiczd8540832021-06-10 15:16:50 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return tbl;
TRodziewiczd8540832021-06-10 15:16:50 +0200297}
298#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
299 __GNUC__ && ( __powerpc__ || __ppc__ ) */
300
301#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
302 defined(__GNUC__) && defined(__sparc64__)
303
304#if defined(__OpenBSD__)
305#warning OpenBSD does not allow access to tick register using software version instead
306#else
307#define HAVE_HARDCLOCK
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200310{
311 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
313 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200314}
315#endif /* __OpenBSD__ */
316#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
317 __GNUC__ && __sparc64__ */
318
319#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
320 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
321
322#define HAVE_HARDCLOCK
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200325{
326 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
328 asm volatile ("mov %%g1, %0" : "=r" (tick));
329 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200330}
331#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
332 __GNUC__ && __sparc__ && !__sparc64__ */
333
334#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
335 defined(__GNUC__) && defined(__alpha__)
336
337#define HAVE_HARDCLOCK
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200340{
341 unsigned long cc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 asm volatile ("rpcc %0" : "=r" (cc));
343 return cc & 0xFFFFFFFF;
TRodziewiczd8540832021-06-10 15:16:50 +0200344}
345#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
346 __GNUC__ && __alpha__ */
347
348#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
349 defined(__GNUC__) && defined(__ia64__)
350
351#define HAVE_HARDCLOCK
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200354{
355 unsigned long itc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
357 return itc;
TRodziewiczd8540832021-06-10 15:16:50 +0200358}
359#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
360 __GNUC__ && __ia64__ */
361
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300362#if !defined(HAVE_HARDCLOCK) && defined(_WIN32) && \
TRodziewiczd8540832021-06-10 15:16:50 +0200363 !defined(EFIX64) && !defined(EFI32)
364
365#define HAVE_HARDCLOCK
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200368{
369 LARGE_INTEGER offset;
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 QueryPerformanceCounter(&offset);
TRodziewiczd8540832021-06-10 15:16:50 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return (unsigned long) (offset.QuadPart);
TRodziewiczd8540832021-06-10 15:16:50 +0200374}
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300375#endif /* !HAVE_HARDCLOCK && _WIN32 && !EFIX64 && !EFI32 */
TRodziewiczd8540832021-06-10 15:16:50 +0200376
377#if !defined(HAVE_HARDCLOCK)
378
379#define HAVE_HARDCLOCK
380
381static int hardclock_init = 0;
382static struct timeval tv_init;
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200385{
386 struct timeval tv_cur;
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (hardclock_init == 0) {
389 gettimeofday(&tv_init, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200390 hardclock_init = 1;
391 }
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 gettimeofday(&tv_cur, NULL);
394 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
395 + (tv_cur.tv_usec - tv_init.tv_usec);
TRodziewiczd8540832021-06-10 15:16:50 +0200396}
397#endif /* !HAVE_HARDCLOCK */
398
399volatile int mbedtls_timing_alarmed = 0;
400
401#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
402
403/* It's OK to use a global because alarm() is supposed to be global anyway */
404static DWORD alarmMs;
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static void TimerProc(void *TimerContext)
TRodziewiczd8540832021-06-10 15:16:50 +0200407{
408 (void) TimerContext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 Sleep(alarmMs);
TRodziewiczd8540832021-06-10 15:16:50 +0200410 mbedtls_timing_alarmed = 1;
411 /* _endthread will be called implicitly on return
Tom Cosgrove1797b052022-12-04 17:19:59 +0000412 * That ensures execution of thread function's epilogue */
TRodziewiczd8540832021-06-10 15:16:50 +0200413}
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200416{
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200418 /* No need to create a thread for this simple case.
419 * Also, this shorcut is more reliable at least on MinGW32 */
420 mbedtls_timing_alarmed = 1;
421 return;
422 }
423
424 mbedtls_timing_alarmed = 0;
425 alarmMs = seconds * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 (void) _beginthread(TimerProc, 0, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200427}
428
429#else /* _WIN32 && !EFIX64 && !EFI32 */
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431static void sighandler(int signum)
TRodziewiczd8540832021-06-10 15:16:50 +0200432{
433 mbedtls_timing_alarmed = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 signal(signum, sighandler);
TRodziewiczd8540832021-06-10 15:16:50 +0200435}
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200438{
439 mbedtls_timing_alarmed = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 signal(SIGALRM, sighandler);
441 alarm(seconds);
442 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200443 /* alarm(0) cancelled any previous pending alarm, but the
444 handler won't fire, so raise the flag straight away. */
445 mbedtls_timing_alarmed = 1;
446 }
447}
448
449#endif /* _WIN32 && !EFIX64 && !EFI32 */
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100450#endif /* !MBEDTLS_TIMING_ALT */
TRodziewiczd8540832021-06-10 15:16:50 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000453{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000454 size_t use_len;
455 int rnd;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (rng_state != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 while (len > 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000462 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (use_len > sizeof(int)) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000464 use_len = sizeof(int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
Paul Bakkera3d195c2011-11-27 21:07:34 +0000466
467 rnd = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 memcpy(output, &rnd, use_len);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000469 output += use_len;
470 len -= use_len;
471 }
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000474}
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476#define CHECK_AND_CONTINUE(R) \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000477 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 int CHECK_AND_CONTINUE_ret = (R); \
479 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
480 mbedtls_printf("Feature not supported. Skipping.\n"); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000481 continue; \
482 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 else if (CHECK_AND_CONTINUE_ret != 0) { \
484 mbedtls_exit(1); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000485 } \
486 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000487
Gilles Peskine28f62f62020-07-24 02:06:46 +0200488#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
Gilles Peskine28f62f62020-07-24 02:06:46 +0200490{
491 const mbedtls_ecp_curve_info *found =
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 mbedtls_ecp_curve_info_from_name(string);
493 if (found != NULL) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200494 *curve = *found;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return 1;
496 } else {
497 return 0;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200498 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200499}
500#endif
501
Paul Bakker5121ce52009-01-03 21:22:43 +0000502unsigned char buf[BUFSIZE];
503
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200504typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200505 char md5, ripemd160, sha1, sha256, sha512,
Pol Henarejosebb36402022-05-20 14:26:00 +0200506 sha3_224, sha3_256, sha3_384, sha3_512,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200507 des3, des,
Dave Rodgman67223bb2024-01-12 16:37:07 +0000508 aes_cbc, aes_cfb128, aes_cfb8, aes_ctr, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200509 aes_cmac, des3_cmac,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200510 aria, camellia, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300511 poly1305,
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100512 ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200513 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200514} todo_list;
515
Simon Butcher63cb97e2018-12-06 17:43:31 +0000516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517int main(int argc, char *argv[])
Paul Bakker5690efc2011-05-26 13:16:06 +0000518{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100519 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200520 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200521 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200522 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200524 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000525#endif
Gilles Peskine28f62f62020-07-24 02:06:46 +0200526#if defined(MBEDTLS_ECP_C)
527 mbedtls_ecp_curve_info single_curve[2] = {
528 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
529 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
530 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
Gilles Peskine28f62f62020-07-24 02:06:46 +0200532#endif
533
534#if defined(MBEDTLS_ECP_C)
535 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
536#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (argc <= 1) {
539 memset(&todo, 1, sizeof(todo));
540 } else {
541 memset(&todo, 0, sizeof(todo));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 for (i = 1; i < argc; i++) {
544 if (strcmp(argv[i], "md5") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200545 todo.md5 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 } else if (strcmp(argv[i], "ripemd160") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200547 todo.ripemd160 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 } else if (strcmp(argv[i], "sha1") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200549 todo.sha1 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 } else if (strcmp(argv[i], "sha256") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200551 todo.sha256 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 } else if (strcmp(argv[i], "sha512") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200553 todo.sha512 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100554 } else if (strcmp(argv[i], "sha3_224") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200555 todo.sha3_224 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100556 } else if (strcmp(argv[i], "sha3_256") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200557 todo.sha3_256 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100558 } else if (strcmp(argv[i], "sha3_384") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200559 todo.sha3_384 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100560 } else if (strcmp(argv[i], "sha3_512") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200561 todo.sha3_512 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 } else if (strcmp(argv[i], "des3") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200563 todo.des3 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 } else if (strcmp(argv[i], "des") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200565 todo.des = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 } else if (strcmp(argv[i], "aes_cbc") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200567 todo.aes_cbc = 1;
Yanray Wang55aba192023-09-12 09:03:50 +0800568 } else if (strcmp(argv[i], "aes_cfb128") == 0) {
569 todo.aes_cfb128 = 1;
Yanray Wang022b9a12023-09-12 09:45:37 +0800570 } else if (strcmp(argv[i], "aes_cfb8") == 0) {
571 todo.aes_cfb8 = 1;
Dave Rodgman67223bb2024-01-12 16:37:07 +0000572 } else if (strcmp(argv[i], "aes_ctr") == 0) {
573 todo.aes_ctr = 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
Dave Rodgman67223bb2024-01-12 16:37:07 +0000777#if defined(MBEDTLS_CIPHER_MODE_CTR)
778 if (todo.aes_ctr) {
779 int keysize;
780 mbedtls_aes_context aes;
781
782 uint8_t stream_block[16];
783 size_t nc_off;
784
785 mbedtls_aes_init(&aes);
786 for (keysize = 128; keysize <= 256; keysize += 64) {
787 mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
788
789 memset(buf, 0, sizeof(buf));
790 memset(tmp, 0, sizeof(tmp));
791 memset(stream_block, 0, sizeof(stream_block));
792 nc_off = 0;
793
794 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
795
796 TIME_AND_TSC(title, mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_off, tmp, stream_block,
797 buf, buf));
798 }
799 mbedtls_aes_free(&aes);
800 }
801#endif
Aorimn5f778012016-06-09 23:22:58 +0200802#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (todo.aes_xts) {
Aorimn5f778012016-06-09 23:22:58 +0200804 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100805 mbedtls_aes_xts_context ctx;
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 mbedtls_aes_xts_init(&ctx);
808 for (keysize = 128; keysize <= 256; keysize += 128) {
809 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
Aorimn5f778012016-06-09 23:22:58 +0200810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 memset(buf, 0, sizeof(buf));
812 memset(tmp, 0, sizeof(tmp));
813 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
Aorimn5f778012016-06-09 23:22:58 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 TIME_AND_TSC(title,
816 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
817 tmp, buf, buf));
Jaeden Amero9366feb2018-05-29 18:55:17 +0100818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 mbedtls_aes_xts_free(&ctx);
Aorimn5f778012016-06-09 23:22:58 +0200820 }
Aorimn5f778012016-06-09 23:22:58 +0200821 }
822#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (todo.aes_gcm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100825 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 mbedtls_gcm_init(&gcm);
829 for (keysize = 128; keysize <= 256; keysize += 64) {
830 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 memset(buf, 0, sizeof(buf));
833 memset(tmp, 0, sizeof(tmp));
834 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 TIME_AND_TSC(title,
837 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
838 12, NULL, 0, buf, buf, 16, tmp));
Paul Bakkerf70fe812013-12-16 16:43:10 +0100839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 mbedtls_gcm_free(&gcm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200841 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000842 }
843#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (todo.aes_ccm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100846 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 mbedtls_ccm_init(&ccm);
850 for (keysize = 128; keysize <= 256; keysize += 64) {
851 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 memset(buf, 0, sizeof(buf));
854 memset(tmp, 0, sizeof(tmp));
855 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 TIME_AND_TSC(title,
858 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
859 12, NULL, 0, buf, buf, tmp, 16));
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 mbedtls_ccm_free(&ccm);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200862 }
863 }
864#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200865#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (todo.chachapoly) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200867 mbedtls_chachapoly_context chachapoly;
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 mbedtls_chachapoly_init(&chachapoly);
870 memset(buf, 0, sizeof(buf));
871 memset(tmp, 0, sizeof(tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_chachapoly_setkey(&chachapoly, tmp);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 TIME_AND_TSC(title,
878 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
879 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 mbedtls_chachapoly_free(&chachapoly);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200882 }
883#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100884#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (todo.aes_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100886 unsigned char output[16];
887 const mbedtls_cipher_info_t *cipher_info;
888 mbedtls_cipher_type_t cipher_type;
889 int keysize;
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100892 keysize <= 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 keysize += 64, cipher_type++) {
894 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 memset(buf, 0, sizeof(buf));
897 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 TIME_AND_TSC(title,
902 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
903 buf, BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100904 }
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 memset(buf, 0, sizeof(buf));
907 memset(tmp, 0, sizeof(tmp));
908 TIME_AND_TSC("AES-CMAC-PRF-128",
909 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
910 output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100911 }
912#endif /* MBEDTLS_CMAC_C */
913#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100915#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (todo.aria) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100917 int keysize;
918 mbedtls_aria_context aria;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_aria_init(&aria);
921 for (keysize = 128; keysize <= 256; keysize += 64) {
922 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 memset(buf, 0, sizeof(buf));
925 memset(tmp, 0, sizeof(tmp));
926 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 TIME_AND_TSC(title,
929 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
930 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100931 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 mbedtls_aria_free(&aria);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100933 }
934#endif
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (todo.camellia) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100938 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_camellia_context camellia;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 mbedtls_camellia_init(&camellia);
942 for (keysize = 128; keysize <= 256; keysize += 64) {
943 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 memset(buf, 0, sizeof(buf));
946 memset(tmp, 0, sizeof(tmp));
947 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 TIME_AND_TSC(title,
950 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
951 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200952 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_camellia_free(&camellia);
Paul Bakker38119b12009-01-10 23:31:23 +0000954 }
955#endif
956
Daniel King34b822c2016-05-15 17:28:08 -0300957#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (todo.chacha20) {
959 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
Daniel King34b822c2016-05-15 17:28:08 -0300960 }
961#endif
962
Daniel Kingadc32c02016-05-16 18:25:45 -0300963#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (todo.poly1305) {
965 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
Daniel Kingadc32c02016-05-16 18:25:45 -0300966 }
967#endif
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (todo.ctr_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 mbedtls_ctr_drbg_init(&ctr_drbg);
974 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
977 TIME_AND_TSC("CTR_DRBG (NOPR)",
978 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
979 mbedtls_ctr_drbg_free(&ctr_drbg);
Paul Bakker02faf452011-11-29 11:23:58 +0000980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_ctr_drbg_init(&ctr_drbg);
982 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 }
985 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
986 TIME_AND_TSC("CTR_DRBG (PR)",
987 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
988 mbedtls_ctr_drbg_free(&ctr_drbg);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200989 }
Paul Bakker02faf452011-11-29 11:23:58 +0000990#endif
991
Andrzej Kurek68327742022-10-03 06:18:18 -0400992#if defined(MBEDTLS_HMAC_DRBG_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
994 if (todo.hmac_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_hmac_drbg_context hmac_drbg;
996 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_hmac_drbg_init(&hmac_drbg);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 }
1008 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
1009 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 }
1014 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1015 MBEDTLS_HMAC_DRBG_PR_ON);
1016 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
1017 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001018#endif
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 }
1028 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
1029 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 }
1034 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1035 MBEDTLS_HMAC_DRBG_PR_ON);
1036 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
1037 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001038#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_hmac_drbg_free(&hmac_drbg);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001040 }
Andrzej Kurek68327742022-10-03 06:18:18 -04001041#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (todo.rsa) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +01001045 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_rsa_context rsa;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001047
Matthias Schulzaa7dffa2023-11-16 15:31:32 +01001048 for (keysize = 2048; keysize <= 4096; keysize += 1024) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_rsa_init(&rsa);
1052 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 TIME_PUBLIC(title, " public",
1055 buf[0] = 0;
1056 ret = mbedtls_rsa_public(&rsa, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 TIME_PUBLIC(title, "private",
1059 buf[0] = 0;
1060 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001063 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001065#endif
1066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (todo.dhm) {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +02001069 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -08001070 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001071 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001072 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001073 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001074 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001075 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001076 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001077 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
1078
1079 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
1081 sizeof(dhm_P_3072) };
Hanno Beckerb9539212017-10-04 13:13:34 +01001082
1083 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
1085 sizeof(dhm_G_3072) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001088 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +02001089 size_t n;
Minos Galanakis97489dc2024-01-12 16:47:52 +00001090 mbedtls_mpi P, G;
1091 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
Yanray Wang5cae6e82023-10-09 18:40:17 +08001092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
1094 mbedtls_dhm_init(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001095
Minos Galanakis97489dc2024-01-12 16:47:52 +00001096 if (mbedtls_mpi_read_binary(&P, dhm_P[i],
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 dhm_P_size[i]) != 0 ||
Minos Galanakis97489dc2024-01-12 16:47:52 +00001098 mbedtls_mpi_read_binary(&G, dhm_G[i],
1099 dhm_G_size[i]) != 0 ||
1100 mbedtls_dhm_set_group(&dhm, &P, &G) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 mbedtls_exit(1);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +02001102 }
1103
Minos Galanakis97489dc2024-01-12 16:47:52 +00001104 n = mbedtls_dhm_get_len(&dhm);
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
Minos Galanakis8ee1b5f2024-01-15 15:54:19 +00001106
1107 if (mbedtls_dhm_read_public(&dhm, buf, n) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 mbedtls_exit(1);
1109 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
1112 TIME_PUBLIC(title, "handshake",
1113 ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
1114 myrand, NULL);
1115 ret |=
1116 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
1119 TIME_PUBLIC(title, "handshake",
1120 ret |=
1121 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 mbedtls_dhm_free(&dhm);
Minos Galanakis97489dc2024-01-12 16:47:52 +00001124 mbedtls_mpi_free(&P), mbedtls_mpi_free(&G);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001125 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001126 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001127#endif
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (todo.ecdsa) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_ecdsa_context ecdsa;
1132 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001133 size_t sig_len;
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 memset(buf, 0x2A, sizeof(buf));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 curve_info++) {
1140 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +00001141 continue;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001142 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
1147 mbedtls_exit(1);
1148 }
1149
1150 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1151 curve_info->name);
1152 TIME_PUBLIC(title,
1153 "sign",
1154 ret =
1155 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
1156 curve_info->bit_size,
1157 tmp, sizeof(tmp), &sig_len, myrand,
1158 NULL));
1159
1160 mbedtls_ecdsa_free(&ecdsa);
1161 }
1162
1163 for (curve_info = curve_list;
1164 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1165 curve_info++) {
1166 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1167 continue;
1168 }
1169
1170 mbedtls_ecdsa_init(&ecdsa);
1171
1172 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
1173 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1174 tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
1175 mbedtls_exit(1);
1176 }
1177
1178 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1179 curve_info->name);
1180 TIME_PUBLIC(title, "verify",
1181 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
1182 tmp, sig_len));
1183
1184 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001185 }
1186 }
1187#endif
1188
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001189#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (todo.ecdh) {
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001191 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1192 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001193 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001194 size_t params_len, publen, seclen;
1195
1196 for (curve_info = curve_list;
1197 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1198 curve_info++) {
1199 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1200 continue;
1201 }
1202
1203 mbedtls_ecdh_init(&ecdh_srv);
1204
1205 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1206 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &params_len, buf_srv,
1207 sizeof(buf_srv), myrand, NULL));
1208
1209 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1210 TIME_PUBLIC(title,
1211 "ephemeral handshake",
1212 const unsigned char *p_srv = buf_srv;
1213 mbedtls_ecdh_init(&ecdh_cli);
1214
1215 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1216 p_srv + params_len));
1217 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &publen, buf_cli,
1218 sizeof(buf_cli), myrand, NULL));
1219
1220 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &seclen, buf_cli,
1221 sizeof(buf_cli), myrand, NULL));
1222 mbedtls_ecdh_free(&ecdh_cli);
1223 );
1224
1225 mbedtls_ecdh_free(&ecdh_srv);
1226 }
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 for (curve_info = curve_list;
1229 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1230 curve_info++) {
1231 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001232 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_ecdh_init(&ecdh_srv);
1236 mbedtls_ecdh_init(&ecdh_cli);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001237
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001238 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1239 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &params_len, buf_srv,
1240 sizeof(buf_srv), myrand, NULL));
1241
1242 const unsigned char *p_srv = buf_srv;
1243 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1244 p_srv + params_len));
1245 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &publen, buf_cli,
1246 sizeof(buf_cli), myrand, NULL));
1247
1248
1249 mbedtls_snprintf(title, sizeof(title), "ECDH-%s", curve_info->name);
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 TIME_PUBLIC(title,
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001251 "static handshake",
1252 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &seclen, buf_cli,
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 sizeof(buf_cli), myrand, NULL));
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001255
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001256 mbedtls_ecdh_free(&ecdh_cli);
1257 mbedtls_ecdh_free(&ecdh_srv);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001258 }
1259 }
1260#endif
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_printf("\n");
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1265 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001266#endif
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 mbedtls_exit(0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001269}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001270
Andrzej Kurek6056e7a2022-03-02 12:01:10 -05001271#endif /* MBEDTLS_HAVE_TIME */