blob: 36ac02259bdbe4b7210093f1cb18f589b4af715b [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 \
Harry Ramsey9c664052024-10-16 14:08:19 +0100120 mbedtls_printf("Error code: %d", ret);
121/* mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
122 mbedtls_printf("FAILED: %s\n", tmp); */
Rich Evans85b05ec2015-02-12 11:37:29 +0000123#else
124#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
Rich Evans85b05ec2015-02-12 11:37:29 +0000126#endif
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128#define TIME_AND_TSC(TITLE, CODE) \
129 do { \
130 unsigned long ii, jj, tsc; \
131 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000132 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_printf(HEADER_FORMAT, TITLE); \
134 fflush(stdout); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_set_alarm(1); \
137 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
138 { \
139 ret = CODE; \
140 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000141 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 tsc = mbedtls_timing_hardclock(); \
143 for (jj = 0; ret == 0 && jj < 1024; jj++) \
144 { \
145 ret = CODE; \
146 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000147 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ret != 0) \
149 { \
150 PRINT_ERROR; \
151 } \
152 else \
153 { \
154 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
155 ii * BUFSIZE / 1024, \
156 (mbedtls_timing_hardclock() - tsc) \
157 / (jj * BUFSIZE)); \
158 } \
159 } while (0)
Rich Evans85b05ec2015-02-12 11:37:29 +0000160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100162
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200163/* How much space to reserve for the title when printing heap usage results.
164 * Updated manually as the output of the following command:
165 *
166 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100167 * awk '{print length+3}' | sort -rn | head -n1
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200168 *
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100169 * This computes the maximum length of a title +3, because we appends "/s" and
170 * want at least one space. (If the value is too small, the only consequence
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800171 * is poor alignment.) */
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100172#define TITLE_SPACE 17
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200173
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100174#define MEMORY_MEASURE_INIT \
175 size_t max_used, max_blocks, max_bytes; \
176 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnard6ced0022022-01-05 10:05:54 +0100177 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
179 mbedtls_memory_buffer_alloc_max_reset();
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100180
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100181#define MEMORY_MEASURE_RESET \
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184#define MEMORY_MEASURE_PRINT(title_len) \
185 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
186 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200187 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 while (ii--) mbedtls_printf(" "); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100189 max_used -= prv_used; \
190 max_blocks -= prv_blocks; \
191 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_printf("%6u heap bytes, %6u allocs", \
193 (unsigned) max_bytes, \
194 (unsigned) (alloc_cnt - prv_alloc));
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100195
196#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000197#define MEMORY_MEASURE_INIT
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100198#define MEMORY_MEASURE_RESET
Gilles Peskine449bd832023-01-11 14:50:10 +0100199#define MEMORY_MEASURE_PRINT(title_len)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100200#endif
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202#define TIME_PUBLIC(TITLE, TYPE, CODE) \
203 do { \
204 unsigned long ii; \
205 int ret; \
206 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000207 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 mbedtls_printf(HEADER_FORMAT, TITLE); \
209 fflush(stdout); \
210 mbedtls_set_alarm(3); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000211 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ret = 0; \
213 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
214 { \
215 MEMORY_MEASURE_RESET; \
216 CODE; \
217 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000218 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
220 { \
221 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
222 ret = 0; \
223 } \
224 else if (ret != 0) \
225 { \
226 PRINT_ERROR; \
227 } \
228 else \
229 { \
230 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
231 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
232 mbedtls_printf("\n"); \
233 } \
234 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100236#if !defined(MBEDTLS_TIMING_ALT)
TRodziewiczd8540832021-06-10 15:16:50 +0200237#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
TRodziewiczd8540832021-06-10 15:16:50 +0200239
240#define HAVE_HARDCLOCK
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200243{
244 unsigned long tsc;
245 __asm rdtsc
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 __asm mov[tsc], eax
247 return tsc;
TRodziewiczd8540832021-06-10 15:16:50 +0200248}
249#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
250 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
251
252/* some versions of mingw-64 have 32-bit longs even on x84_64 */
253#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 defined(__GNUC__) && (defined(__i386__) || ( \
255 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
TRodziewiczd8540832021-06-10 15:16:50 +0200256
257#define HAVE_HARDCLOCK
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200260{
261 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
263 return lo;
TRodziewiczd8540832021-06-10 15:16:50 +0200264}
265#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
266 __GNUC__ && __i386__ */
267
268#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
TRodziewiczd8540832021-06-10 15:16:50 +0200270
271#define HAVE_HARDCLOCK
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200274{
275 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
277 return lo | (hi << 32);
TRodziewiczd8540832021-06-10 15:16:50 +0200278}
279#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
280 __GNUC__ && ( __amd64__ || __x86_64__ ) */
281
282#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
TRodziewiczd8540832021-06-10 15:16:50 +0200284
285#define HAVE_HARDCLOCK
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200288{
289 unsigned long tbl, tbu0, tbu1;
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 do {
292 asm volatile ("mftbu %0" : "=r" (tbu0));
293 asm volatile ("mftb %0" : "=r" (tbl));
294 asm volatile ("mftbu %0" : "=r" (tbu1));
295 } while (tbu0 != tbu1);
TRodziewiczd8540832021-06-10 15:16:50 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return tbl;
TRodziewiczd8540832021-06-10 15:16:50 +0200298}
299#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
300 __GNUC__ && ( __powerpc__ || __ppc__ ) */
301
302#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
303 defined(__GNUC__) && defined(__sparc64__)
304
305#if defined(__OpenBSD__)
306#warning OpenBSD does not allow access to tick register using software version instead
307#else
308#define HAVE_HARDCLOCK
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200311{
312 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
314 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200315}
316#endif /* __OpenBSD__ */
317#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
318 __GNUC__ && __sparc64__ */
319
320#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
321 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
322
323#define HAVE_HARDCLOCK
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200326{
327 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
329 asm volatile ("mov %%g1, %0" : "=r" (tick));
330 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200331}
332#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
333 __GNUC__ && __sparc__ && !__sparc64__ */
334
335#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
336 defined(__GNUC__) && defined(__alpha__)
337
338#define HAVE_HARDCLOCK
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200341{
342 unsigned long cc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 asm volatile ("rpcc %0" : "=r" (cc));
344 return cc & 0xFFFFFFFF;
TRodziewiczd8540832021-06-10 15:16:50 +0200345}
346#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
347 __GNUC__ && __alpha__ */
348
349#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
350 defined(__GNUC__) && defined(__ia64__)
351
352#define HAVE_HARDCLOCK
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200355{
356 unsigned long itc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
358 return itc;
TRodziewiczd8540832021-06-10 15:16:50 +0200359}
360#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
361 __GNUC__ && __ia64__ */
362
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300363#if !defined(HAVE_HARDCLOCK) && defined(_WIN32) && \
TRodziewiczd8540832021-06-10 15:16:50 +0200364 !defined(EFIX64) && !defined(EFI32)
365
366#define HAVE_HARDCLOCK
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200369{
370 LARGE_INTEGER offset;
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 QueryPerformanceCounter(&offset);
TRodziewiczd8540832021-06-10 15:16:50 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return (unsigned long) (offset.QuadPart);
TRodziewiczd8540832021-06-10 15:16:50 +0200375}
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300376#endif /* !HAVE_HARDCLOCK && _WIN32 && !EFIX64 && !EFI32 */
TRodziewiczd8540832021-06-10 15:16:50 +0200377
378#if !defined(HAVE_HARDCLOCK)
379
380#define HAVE_HARDCLOCK
381
382static int hardclock_init = 0;
383static struct timeval tv_init;
384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200386{
387 struct timeval tv_cur;
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (hardclock_init == 0) {
390 gettimeofday(&tv_init, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200391 hardclock_init = 1;
392 }
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 gettimeofday(&tv_cur, NULL);
395 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
396 + (tv_cur.tv_usec - tv_init.tv_usec);
TRodziewiczd8540832021-06-10 15:16:50 +0200397}
398#endif /* !HAVE_HARDCLOCK */
399
400volatile int mbedtls_timing_alarmed = 0;
401
402#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
403
404/* It's OK to use a global because alarm() is supposed to be global anyway */
405static DWORD alarmMs;
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407static void TimerProc(void *TimerContext)
TRodziewiczd8540832021-06-10 15:16:50 +0200408{
409 (void) TimerContext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 Sleep(alarmMs);
TRodziewiczd8540832021-06-10 15:16:50 +0200411 mbedtls_timing_alarmed = 1;
412 /* _endthread will be called implicitly on return
Tom Cosgrove1797b052022-12-04 17:19:59 +0000413 * That ensures execution of thread function's epilogue */
TRodziewiczd8540832021-06-10 15:16:50 +0200414}
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200417{
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200419 /* No need to create a thread for this simple case.
420 * Also, this shorcut is more reliable at least on MinGW32 */
421 mbedtls_timing_alarmed = 1;
422 return;
423 }
424
425 mbedtls_timing_alarmed = 0;
426 alarmMs = seconds * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 (void) _beginthread(TimerProc, 0, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200428}
429
430#else /* _WIN32 && !EFIX64 && !EFI32 */
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432static void sighandler(int signum)
TRodziewiczd8540832021-06-10 15:16:50 +0200433{
434 mbedtls_timing_alarmed = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 signal(signum, sighandler);
TRodziewiczd8540832021-06-10 15:16:50 +0200436}
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200439{
440 mbedtls_timing_alarmed = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 signal(SIGALRM, sighandler);
442 alarm(seconds);
443 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200444 /* alarm(0) cancelled any previous pending alarm, but the
445 handler won't fire, so raise the flag straight away. */
446 mbedtls_timing_alarmed = 1;
447 }
448}
449
450#endif /* _WIN32 && !EFIX64 && !EFI32 */
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100451#endif /* !MBEDTLS_TIMING_ALT */
TRodziewiczd8540832021-06-10 15:16:50 +0200452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000454{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000455 size_t use_len;
456 int rnd;
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (rng_state != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 while (len > 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000463 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (use_len > sizeof(int)) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000465 use_len = sizeof(int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 }
Paul Bakkera3d195c2011-11-27 21:07:34 +0000467
468 rnd = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 memcpy(output, &rnd, use_len);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000470 output += use_len;
471 len -= use_len;
472 }
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475}
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477#define CHECK_AND_CONTINUE(R) \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000478 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 int CHECK_AND_CONTINUE_ret = (R); \
480 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
481 mbedtls_printf("Feature not supported. Skipping.\n"); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000482 continue; \
483 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 else if (CHECK_AND_CONTINUE_ret != 0) { \
485 mbedtls_exit(1); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000486 } \
487 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000488
Gilles Peskine28f62f62020-07-24 02:06:46 +0200489#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100490static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
Gilles Peskine28f62f62020-07-24 02:06:46 +0200491{
492 const mbedtls_ecp_curve_info *found =
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 mbedtls_ecp_curve_info_from_name(string);
494 if (found != NULL) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200495 *curve = *found;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return 1;
497 } else {
498 return 0;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200499 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200500}
501#endif
502
Paul Bakker5121ce52009-01-03 21:22:43 +0000503unsigned char buf[BUFSIZE];
504
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200505typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200506 char md5, ripemd160, sha1, sha256, sha512,
Pol Henarejosebb36402022-05-20 14:26:00 +0200507 sha3_224, sha3_256, sha3_384, sha3_512,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200508 des3, des,
Dave Rodgman67223bb2024-01-12 16:37:07 +0000509 aes_cbc, aes_cfb128, aes_cfb8, aes_ctr, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200510 aes_cmac, des3_cmac,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200511 aria, camellia, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300512 poly1305,
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100513 ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200514 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200515} todo_list;
516
Simon Butcher63cb97e2018-12-06 17:43:31 +0000517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518int main(int argc, char *argv[])
Paul Bakker5690efc2011-05-26 13:16:06 +0000519{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100520 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200521 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200522 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200523 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200525 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000526#endif
Gilles Peskine28f62f62020-07-24 02:06:46 +0200527#if defined(MBEDTLS_ECP_C)
528 mbedtls_ecp_curve_info single_curve[2] = {
529 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
530 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
531 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
Gilles Peskine28f62f62020-07-24 02:06:46 +0200533#endif
534
535#if defined(MBEDTLS_ECP_C)
536 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
537#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (argc <= 1) {
540 memset(&todo, 1, sizeof(todo));
541 } else {
542 memset(&todo, 0, sizeof(todo));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 for (i = 1; i < argc; i++) {
545 if (strcmp(argv[i], "md5") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200546 todo.md5 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 } else if (strcmp(argv[i], "ripemd160") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200548 todo.ripemd160 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 } else if (strcmp(argv[i], "sha1") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200550 todo.sha1 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 } else if (strcmp(argv[i], "sha256") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200552 todo.sha256 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 } else if (strcmp(argv[i], "sha512") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200554 todo.sha512 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100555 } else if (strcmp(argv[i], "sha3_224") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200556 todo.sha3_224 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100557 } else if (strcmp(argv[i], "sha3_256") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200558 todo.sha3_256 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100559 } else if (strcmp(argv[i], "sha3_384") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200560 todo.sha3_384 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100561 } else if (strcmp(argv[i], "sha3_512") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200562 todo.sha3_512 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 } else if (strcmp(argv[i], "des3") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200564 todo.des3 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 } else if (strcmp(argv[i], "des") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200566 todo.des = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 } else if (strcmp(argv[i], "aes_cbc") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200568 todo.aes_cbc = 1;
Yanray Wang55aba192023-09-12 09:03:50 +0800569 } else if (strcmp(argv[i], "aes_cfb128") == 0) {
570 todo.aes_cfb128 = 1;
Yanray Wang022b9a12023-09-12 09:45:37 +0800571 } else if (strcmp(argv[i], "aes_cfb8") == 0) {
572 todo.aes_cfb8 = 1;
Dave Rodgman67223bb2024-01-12 16:37:07 +0000573 } else if (strcmp(argv[i], "aes_ctr") == 0) {
574 todo.aes_ctr = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 } else if (strcmp(argv[i], "aes_xts") == 0) {
Aorimn5f778012016-06-09 23:22:58 +0200576 todo.aes_xts = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 } else if (strcmp(argv[i], "aes_gcm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200578 todo.aes_gcm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 } else if (strcmp(argv[i], "aes_ccm") == 0) {
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200580 todo.aes_ccm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 } else if (strcmp(argv[i], "chachapoly") == 0) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200582 todo.chachapoly = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 } else if (strcmp(argv[i], "aes_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100584 todo.aes_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 } else if (strcmp(argv[i], "des3_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100586 todo.des3_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 } else if (strcmp(argv[i], "aria") == 0) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100588 todo.aria = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 } else if (strcmp(argv[i], "camellia") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200590 todo.camellia = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 } else if (strcmp(argv[i], "chacha20") == 0) {
Daniel King34b822c2016-05-15 17:28:08 -0300592 todo.chacha20 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 } else if (strcmp(argv[i], "poly1305") == 0) {
Daniel Kingadc32c02016-05-16 18:25:45 -0300594 todo.poly1305 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200596 todo.ctr_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100598 todo.hmac_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 } else if (strcmp(argv[i], "rsa") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200600 todo.rsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 } else if (strcmp(argv[i], "dhm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200602 todo.dhm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 } else if (strcmp(argv[i], "ecdsa") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200604 todo.ecdsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } else if (strcmp(argv[i], "ecdh") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200606 todo.ecdh = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200608#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 else if (set_ecp_curve(argv[i], single_curve)) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200610 curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200612#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 else {
614 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
615 mbedtls_printf("Available options: " OPTIONS);
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200616 }
617 }
618 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000624#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 memset(buf, 0xAA, sizeof(buf));
626 memset(tmp, 0xBB, sizeof(tmp));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200627
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100628 /* Avoid "unused static function" warning in configurations without
629 * symmetric crypto. */
Manuel Pégourié-Gonnardcd4ad0c2022-01-05 09:54:37 +0100630 (void) mbedtls_timing_hardclock;
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (todo.md5) {
634 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
635 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000636#endif
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (todo.ripemd160) {
640 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
641 }
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100642#endif
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (todo.sha1) {
646 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
647 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000648#endif
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (todo.sha256) {
652 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
653 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000654#endif
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (todo.sha512) {
658 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
659 }
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000660#endif
Pol Henarejosebb36402022-05-20 14:26:00 +0200661#if defined(MBEDTLS_SHA3_C)
Pol Henarejosa6779282023-02-08 00:50:04 +0100662 if (todo.sha3_224) {
663 TIME_AND_TSC("SHA3-224", mbedtls_sha3(MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28));
664 }
665 if (todo.sha3_256) {
666 TIME_AND_TSC("SHA3-256", mbedtls_sha3(MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32));
667 }
668 if (todo.sha3_384) {
669 TIME_AND_TSC("SHA3-384", mbedtls_sha3(MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48));
670 }
671 if (todo.sha3_512) {
672 TIME_AND_TSC("SHA3-512", mbedtls_sha3(MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64));
673 }
Pol Henarejosebb36402022-05-20 14:26:00 +0200674#endif
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000675
Simon Butcher549dc3d2016-10-05 14:14:19 +0100676#if defined(MBEDTLS_DES_C)
677#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (todo.des3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_des3_context des3;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 mbedtls_des3_init(&des3);
682 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
683 mbedtls_exit(1);
684 }
685 TIME_AND_TSC("3DES",
686 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
687 mbedtls_des3_free(&des3);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200688 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (todo.des) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_des_context des;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 mbedtls_des_init(&des);
694 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
695 mbedtls_exit(1);
696 }
697 TIME_AND_TSC("DES",
698 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
699 mbedtls_des_free(&des);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200700 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100701
702#endif /* MBEDTLS_CIPHER_MODE_CBC */
703#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (todo.des3_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100705 unsigned char output[8];
706 const mbedtls_cipher_info_t *cipher_info;
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 memset(buf, 0, sizeof(buf));
709 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 TIME_AND_TSC("3DES-CMAC",
714 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
715 BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100716 }
717#endif /* MBEDTLS_CMAC_C */
718#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#if defined(MBEDTLS_AES_C)
721#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if (todo.aes_cbc) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100723 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 mbedtls_aes_init(&aes);
727 for (keysize = 128; keysize <= 256; keysize += 64) {
728 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 memset(buf, 0, sizeof(buf));
731 memset(tmp, 0, sizeof(tmp));
732 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 TIME_AND_TSC(title,
735 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200736 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 mbedtls_aes_free(&aes);
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200739#endif
Yanray Wang55aba192023-09-12 09:03:50 +0800740#if defined(MBEDTLS_CIPHER_MODE_CFB)
741 if (todo.aes_cfb128) {
742 int keysize;
743 size_t iv_off = 0;
744 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800745
Yanray Wang55aba192023-09-12 09:03:50 +0800746 mbedtls_aes_init(&aes);
747 for (keysize = 128; keysize <= 256; keysize += 64) {
748 mbedtls_snprintf(title, sizeof(title), "AES-CFB128-%d", keysize);
749
750 memset(buf, 0, sizeof(buf));
751 memset(tmp, 0, sizeof(tmp));
752 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
753
754 TIME_AND_TSC(title,
755 mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE,
756 &iv_off, tmp, buf, buf));
757 }
758 mbedtls_aes_free(&aes);
759 }
Yanray Wang022b9a12023-09-12 09:45:37 +0800760 if (todo.aes_cfb8) {
761 int keysize;
762 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800763
Yanray Wang022b9a12023-09-12 09:45:37 +0800764 mbedtls_aes_init(&aes);
765 for (keysize = 128; keysize <= 256; keysize += 64) {
766 mbedtls_snprintf(title, sizeof(title), "AES-CFB8-%d", keysize);
767
768 memset(buf, 0, sizeof(buf));
769 memset(tmp, 0, sizeof(tmp));
770 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
771
772 TIME_AND_TSC(title,
773 mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
774 }
775 mbedtls_aes_free(&aes);
776 }
Yanray Wang55aba192023-09-12 09:03:50 +0800777#endif
Dave Rodgman67223bb2024-01-12 16:37:07 +0000778#if defined(MBEDTLS_CIPHER_MODE_CTR)
779 if (todo.aes_ctr) {
780 int keysize;
781 mbedtls_aes_context aes;
782
783 uint8_t stream_block[16];
784 size_t nc_off;
785
786 mbedtls_aes_init(&aes);
787 for (keysize = 128; keysize <= 256; keysize += 64) {
788 mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
789
790 memset(buf, 0, sizeof(buf));
791 memset(tmp, 0, sizeof(tmp));
792 memset(stream_block, 0, sizeof(stream_block));
793 nc_off = 0;
794
795 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
796
797 TIME_AND_TSC(title, mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_off, tmp, stream_block,
798 buf, buf));
799 }
800 mbedtls_aes_free(&aes);
801 }
802#endif
Aorimn5f778012016-06-09 23:22:58 +0200803#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (todo.aes_xts) {
Aorimn5f778012016-06-09 23:22:58 +0200805 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100806 mbedtls_aes_xts_context ctx;
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 mbedtls_aes_xts_init(&ctx);
809 for (keysize = 128; keysize <= 256; keysize += 128) {
810 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
Aorimn5f778012016-06-09 23:22:58 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 memset(buf, 0, sizeof(buf));
813 memset(tmp, 0, sizeof(tmp));
814 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
Aorimn5f778012016-06-09 23:22:58 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 TIME_AND_TSC(title,
817 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
818 tmp, buf, buf));
Jaeden Amero9366feb2018-05-29 18:55:17 +0100819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 mbedtls_aes_xts_free(&ctx);
Aorimn5f778012016-06-09 23:22:58 +0200821 }
Aorimn5f778012016-06-09 23:22:58 +0200822 }
823#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (todo.aes_gcm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100826 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 mbedtls_gcm_init(&gcm);
830 for (keysize = 128; keysize <= 256; keysize += 64) {
831 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 memset(buf, 0, sizeof(buf));
834 memset(tmp, 0, sizeof(tmp));
835 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 TIME_AND_TSC(title,
838 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
839 12, NULL, 0, buf, buf, 16, tmp));
Paul Bakkerf70fe812013-12-16 16:43:10 +0100840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 mbedtls_gcm_free(&gcm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200842 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000843 }
844#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (todo.aes_ccm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100847 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 mbedtls_ccm_init(&ccm);
851 for (keysize = 128; keysize <= 256; keysize += 64) {
852 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 memset(buf, 0, sizeof(buf));
855 memset(tmp, 0, sizeof(tmp));
856 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 TIME_AND_TSC(title,
859 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
860 12, NULL, 0, buf, buf, tmp, 16));
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 mbedtls_ccm_free(&ccm);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200863 }
864 }
865#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200866#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 if (todo.chachapoly) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200868 mbedtls_chachapoly_context chachapoly;
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 mbedtls_chachapoly_init(&chachapoly);
871 memset(buf, 0, sizeof(buf));
872 memset(tmp, 0, sizeof(tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 mbedtls_chachapoly_setkey(&chachapoly, tmp);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 TIME_AND_TSC(title,
879 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
880 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 mbedtls_chachapoly_free(&chachapoly);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200883 }
884#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100885#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if (todo.aes_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100887 unsigned char output[16];
888 const mbedtls_cipher_info_t *cipher_info;
889 mbedtls_cipher_type_t cipher_type;
890 int keysize;
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100893 keysize <= 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 keysize += 64, cipher_type++) {
895 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 memset(buf, 0, sizeof(buf));
898 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 TIME_AND_TSC(title,
903 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
904 buf, BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 memset(buf, 0, sizeof(buf));
908 memset(tmp, 0, sizeof(tmp));
909 TIME_AND_TSC("AES-CMAC-PRF-128",
910 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
911 output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100912 }
913#endif /* MBEDTLS_CMAC_C */
914#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100916#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (todo.aria) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100918 int keysize;
919 mbedtls_aria_context aria;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_aria_init(&aria);
922 for (keysize = 128; keysize <= 256; keysize += 64) {
923 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 memset(buf, 0, sizeof(buf));
926 memset(tmp, 0, sizeof(tmp));
927 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 TIME_AND_TSC(title,
930 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
931 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100932 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_aria_free(&aria);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100934 }
935#endif
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (todo.camellia) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100939 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_camellia_context camellia;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 mbedtls_camellia_init(&camellia);
943 for (keysize = 128; keysize <= 256; keysize += 64) {
944 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 memset(buf, 0, sizeof(buf));
947 memset(tmp, 0, sizeof(tmp));
948 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 TIME_AND_TSC(title,
951 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
952 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200953 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_camellia_free(&camellia);
Paul Bakker38119b12009-01-10 23:31:23 +0000955 }
956#endif
957
Daniel King34b822c2016-05-15 17:28:08 -0300958#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (todo.chacha20) {
960 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
Daniel King34b822c2016-05-15 17:28:08 -0300961 }
962#endif
963
Daniel Kingadc32c02016-05-16 18:25:45 -0300964#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (todo.poly1305) {
966 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
Daniel Kingadc32c02016-05-16 18:25:45 -0300967 }
968#endif
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (todo.ctr_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 mbedtls_ctr_drbg_init(&ctr_drbg);
975 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 }
978 TIME_AND_TSC("CTR_DRBG (NOPR)",
979 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
980 mbedtls_ctr_drbg_free(&ctr_drbg);
Paul Bakker02faf452011-11-29 11:23:58 +0000981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_ctr_drbg_init(&ctr_drbg);
983 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 }
986 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
987 TIME_AND_TSC("CTR_DRBG (PR)",
988 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
989 mbedtls_ctr_drbg_free(&ctr_drbg);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200990 }
Paul Bakker02faf452011-11-29 11:23:58 +0000991#endif
992
Andrzej Kurek68327742022-10-03 06:18:18 -0400993#if defined(MBEDTLS_HMAC_DRBG_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
995 if (todo.hmac_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_hmac_drbg_context hmac_drbg;
997 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 mbedtls_hmac_drbg_init(&hmac_drbg);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +02001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 }
1009 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
1010 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 }
1015 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1016 MBEDTLS_HMAC_DRBG_PR_ON);
1017 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
1018 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001019#endif
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 }
1029 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
1030 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 }
1035 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1036 MBEDTLS_HMAC_DRBG_PR_ON);
1037 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
1038 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001039#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_hmac_drbg_free(&hmac_drbg);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001041 }
Andrzej Kurek68327742022-10-03 06:18:18 -04001042#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (todo.rsa) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +01001046 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_rsa_context rsa;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001048
Matthias Schulzaa7dffa2023-11-16 15:31:32 +01001049 for (keysize = 2048; keysize <= 4096; keysize += 1024) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 mbedtls_rsa_init(&rsa);
1053 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 TIME_PUBLIC(title, " public",
1056 buf[0] = 0;
1057 ret = mbedtls_rsa_public(&rsa, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 TIME_PUBLIC(title, "private",
1060 buf[0] = 0;
1061 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001066#endif
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (todo.dhm) {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +02001070 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -08001071 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001072 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001073 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001074 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001075 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001076 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001077 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001078 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
1079
1080 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
1082 sizeof(dhm_P_3072) };
Hanno Beckerb9539212017-10-04 13:13:34 +01001083
1084 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
1086 sizeof(dhm_G_3072) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001089 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +02001090 size_t n;
Minos Galanakis97489dc2024-01-12 16:47:52 +00001091 mbedtls_mpi P, G;
1092 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
Yanray Wang5cae6e82023-10-09 18:40:17 +08001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
1095 mbedtls_dhm_init(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001096
Minos Galanakis97489dc2024-01-12 16:47:52 +00001097 if (mbedtls_mpi_read_binary(&P, dhm_P[i],
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 dhm_P_size[i]) != 0 ||
Minos Galanakis97489dc2024-01-12 16:47:52 +00001099 mbedtls_mpi_read_binary(&G, dhm_G[i],
1100 dhm_G_size[i]) != 0 ||
1101 mbedtls_dhm_set_group(&dhm, &P, &G) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_exit(1);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +02001103 }
1104
Minos Galanakis97489dc2024-01-12 16:47:52 +00001105 n = mbedtls_dhm_get_len(&dhm);
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
Minos Galanakis8ee1b5f2024-01-15 15:54:19 +00001107
1108 if (mbedtls_dhm_read_public(&dhm, buf, n) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_exit(1);
1110 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
1113 TIME_PUBLIC(title, "handshake",
1114 ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
1115 myrand, NULL);
1116 ret |=
1117 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
1120 TIME_PUBLIC(title, "handshake",
1121 ret |=
1122 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 mbedtls_dhm_free(&dhm);
Minos Galanakis97489dc2024-01-12 16:47:52 +00001125 mbedtls_mpi_free(&P), mbedtls_mpi_free(&G);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001126 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001127 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001128#endif
1129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (todo.ecdsa) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_ecdsa_context ecdsa;
1133 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001134 size_t sig_len;
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 memset(buf, 0x2A, sizeof(buf));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 curve_info++) {
1141 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +00001142 continue;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001143 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
1148 mbedtls_exit(1);
1149 }
1150
1151 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1152 curve_info->name);
1153 TIME_PUBLIC(title,
1154 "sign",
1155 ret =
1156 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
1157 curve_info->bit_size,
1158 tmp, sizeof(tmp), &sig_len, myrand,
1159 NULL));
1160
1161 mbedtls_ecdsa_free(&ecdsa);
1162 }
1163
1164 for (curve_info = curve_list;
1165 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1166 curve_info++) {
1167 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1168 continue;
1169 }
1170
1171 mbedtls_ecdsa_init(&ecdsa);
1172
1173 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
1174 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1175 tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
1176 mbedtls_exit(1);
1177 }
1178
1179 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1180 curve_info->name);
1181 TIME_PUBLIC(title, "verify",
1182 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
1183 tmp, sig_len));
1184
1185 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001186 }
1187 }
1188#endif
1189
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001190#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (todo.ecdh) {
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001192 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1193 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001194 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001195 size_t params_len, publen, seclen;
1196
1197 for (curve_info = curve_list;
1198 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1199 curve_info++) {
1200 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
1201 continue;
1202 }
1203
1204 mbedtls_ecdh_init(&ecdh_srv);
1205
1206 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1207 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &params_len, buf_srv,
1208 sizeof(buf_srv), myrand, NULL));
1209
1210 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1211 TIME_PUBLIC(title,
1212 "ephemeral handshake",
1213 const unsigned char *p_srv = buf_srv;
1214 mbedtls_ecdh_init(&ecdh_cli);
1215
1216 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1217 p_srv + params_len));
1218 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &publen, buf_cli,
1219 sizeof(buf_cli), myrand, NULL));
1220
1221 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &seclen, buf_cli,
1222 sizeof(buf_cli), myrand, NULL));
1223 mbedtls_ecdh_free(&ecdh_cli);
1224 );
1225
1226 mbedtls_ecdh_free(&ecdh_srv);
1227 }
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 for (curve_info = curve_list;
1230 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1231 curve_info++) {
1232 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001233 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 mbedtls_ecdh_init(&ecdh_srv);
1237 mbedtls_ecdh_init(&ecdh_cli);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001238
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001239 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1240 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &params_len, buf_srv,
1241 sizeof(buf_srv), myrand, NULL));
1242
1243 const unsigned char *p_srv = buf_srv;
1244 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1245 p_srv + params_len));
1246 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &publen, buf_cli,
1247 sizeof(buf_cli), myrand, NULL));
1248
1249
1250 mbedtls_snprintf(title, sizeof(title), "ECDH-%s", curve_info->name);
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 TIME_PUBLIC(title,
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001252 "static handshake",
1253 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &seclen, buf_cli,
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 sizeof(buf_cli), myrand, NULL));
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001256
Manuel Pégourié-Gonnarddd9cbf92024-02-22 12:14:28 +01001257 mbedtls_ecdh_free(&ecdh_cli);
1258 mbedtls_ecdh_free(&ecdh_srv);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001259 }
1260 }
1261#endif
1262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 mbedtls_printf("\n");
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1266 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001267#endif
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 mbedtls_exit(0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001270}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001271
Andrzej Kurek6056e7a2022-03-02 12:01:10 -05001272#endif /* MBEDTLS_HAVE_TIME */