blob: 895b1488f77d6e8f02cf8e0b8785a05755be906f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +02008#define MBEDTLS_ALLOW_PRIVATE_ACCESS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050014#if !defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010015int main(void)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000016{
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050017 mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010018 mbedtls_exit(0);
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000019}
20#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010021
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000022#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020023#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010024
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/md5.h"
26#include "mbedtls/ripemd160.h"
27#include "mbedtls/sha1.h"
28#include "mbedtls/sha256.h"
29#include "mbedtls/sha512.h"
Pol Henarejosebb36402022-05-20 14:26:00 +020030#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/des.h"
33#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010034#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030036#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/gcm.h"
38#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020039#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010040#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030041#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010042
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ctr_drbg.h"
44#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/rsa.h"
47#include "mbedtls/dhm.h"
48#include "mbedtls/ecdsa.h"
49#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010050
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
David Horstmanne3d8f312023-01-03 11:07:09 +000053/* *INDENT-OFF* */
TRodziewicz90f304f2021-06-11 11:56:47 +020054#ifndef asm
55#define asm __asm
56#endif
David Horstmanne3d8f312023-01-03 11:07:09 +000057/* *INDENT-ON* */
TRodziewicz90f304f2021-06-11 11:56:47 +020058
TRodziewiczd8540832021-06-10 15:16:50 +020059#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
60
61#include <windows.h>
62#include <process.h>
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020065 LARGE_INTEGER start;
66};
67
68#else
69
70#include <unistd.h>
71#include <sys/types.h>
72#include <sys/time.h>
73#include <signal.h>
74#include <time.h>
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020077 struct timeval start;
78};
79
80#endif /* _WIN32 && !EFIX64 && !EFI32 */
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000084#endif
85
Matthias Schulz3b9240b2023-11-16 17:39:43 +010086#ifdef MBEDTLS_TIMING_ALT
87void mbedtls_set_alarm(int seconds);
88unsigned long mbedtls_timing_hardclock(void);
89extern volatile int mbedtls_timing_alarmed;
90#else
Gilles Peskine449bd832023-01-11 14:50:10 +010091static void mbedtls_set_alarm(int seconds);
Matthias Schulz3b9240b2023-11-16 17:39:43 +010092static unsigned long mbedtls_timing_hardclock(void);
93#endif
TRodziewiczd8540832021-06-10 15:16:50 +020094
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000095/*
96 * For heap usage estimates, we need an estimate of the overhead per allocated
97 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
98 * so use that as our baseline.
99 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100#define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000101
102/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200103 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000104 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +0100105#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000106
Paul Bakker02faf452011-11-29 11:23:58 +0000107#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100108#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100109#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000110
Yanray Wang022b9a12023-09-12 09:45:37 +0800111#define OPTIONS \
112 "md5, ripemd160, sha1, sha256, sha512,\n" \
113 "sha3_224, sha3_256, sha3_384, sha3_512,\n" \
114 "des3, des, camellia, chacha20,\n" \
115 "aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly\n" \
116 "aes_cmac, des3_cmac, poly1305\n" \
117 "ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000118 "rsa, dhm, ecdsa, ecdh.\n"
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000121#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
123 mbedtls_printf("FAILED: %s\n", tmp);
Rich Evans85b05ec2015-02-12 11:37:29 +0000124#else
125#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
Rich Evans85b05ec2015-02-12 11:37:29 +0000127#endif
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129#define TIME_AND_TSC(TITLE, CODE) \
130 do { \
131 unsigned long ii, jj, tsc; \
132 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000133 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_printf(HEADER_FORMAT, TITLE); \
135 fflush(stdout); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_set_alarm(1); \
138 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
139 { \
140 ret = CODE; \
141 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000142 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 tsc = mbedtls_timing_hardclock(); \
144 for (jj = 0; ret == 0 && jj < 1024; jj++) \
145 { \
146 ret = CODE; \
147 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000148 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (ret != 0) \
150 { \
151 PRINT_ERROR; \
152 } \
153 else \
154 { \
155 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
156 ii * BUFSIZE / 1024, \
157 (mbedtls_timing_hardclock() - tsc) \
158 / (jj * BUFSIZE)); \
159 } \
160 } while (0)
Rich Evans85b05ec2015-02-12 11:37:29 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200164/* How much space to reserve for the title when printing heap usage results.
165 * Updated manually as the output of the following command:
166 *
167 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100168 * awk '{print length+3}' | sort -rn | head -n1
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200169 *
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100170 * This computes the maximum length of a title +3, because we appends "/s" and
171 * want at least one space. (If the value is too small, the only consequence
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800172 * is poor alignment.) */
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100173#define TITLE_SPACE 17
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200174
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100175#define MEMORY_MEASURE_INIT \
176 size_t max_used, max_blocks, max_bytes; \
177 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnard6ced0022022-01-05 10:05:54 +0100178 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
180 mbedtls_memory_buffer_alloc_max_reset();
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100181
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100182#define MEMORY_MEASURE_RESET \
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185#define MEMORY_MEASURE_PRINT(title_len) \
186 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
187 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200188 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 while (ii--) mbedtls_printf(" "); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100190 max_used -= prv_used; \
191 max_blocks -= prv_blocks; \
192 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_printf("%6u heap bytes, %6u allocs", \
194 (unsigned) max_bytes, \
195 (unsigned) (alloc_cnt - prv_alloc));
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100196
197#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000198#define MEMORY_MEASURE_INIT
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100199#define MEMORY_MEASURE_RESET
Gilles Peskine449bd832023-01-11 14:50:10 +0100200#define MEMORY_MEASURE_PRINT(title_len)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100201#endif
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203#define TIME_PUBLIC(TITLE, TYPE, CODE) \
204 do { \
205 unsigned long ii; \
206 int ret; \
207 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000208 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_printf(HEADER_FORMAT, TITLE); \
210 fflush(stdout); \
211 mbedtls_set_alarm(3); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000212 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ret = 0; \
214 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
215 { \
216 MEMORY_MEASURE_RESET; \
217 CODE; \
218 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000219 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
221 { \
222 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
223 ret = 0; \
224 } \
225 else if (ret != 0) \
226 { \
227 PRINT_ERROR; \
228 } \
229 else \
230 { \
231 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
232 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
233 mbedtls_printf("\n"); \
234 } \
235 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100237#if !defined(MBEDTLS_TIMING_ALT)
TRodziewiczd8540832021-06-10 15:16:50 +0200238#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
TRodziewiczd8540832021-06-10 15:16:50 +0200240
241#define HAVE_HARDCLOCK
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200244{
245 unsigned long tsc;
246 __asm rdtsc
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 __asm mov[tsc], eax
248 return tsc;
TRodziewiczd8540832021-06-10 15:16:50 +0200249}
250#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
251 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
252
253/* some versions of mingw-64 have 32-bit longs even on x84_64 */
254#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 defined(__GNUC__) && (defined(__i386__) || ( \
256 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
TRodziewiczd8540832021-06-10 15:16:50 +0200257
258#define HAVE_HARDCLOCK
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200261{
262 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
264 return lo;
TRodziewiczd8540832021-06-10 15:16:50 +0200265}
266#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
267 __GNUC__ && __i386__ */
268
269#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
TRodziewiczd8540832021-06-10 15:16:50 +0200271
272#define HAVE_HARDCLOCK
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200275{
276 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
278 return lo | (hi << 32);
TRodziewiczd8540832021-06-10 15:16:50 +0200279}
280#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
281 __GNUC__ && ( __amd64__ || __x86_64__ ) */
282
283#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
TRodziewiczd8540832021-06-10 15:16:50 +0200285
286#define HAVE_HARDCLOCK
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200289{
290 unsigned long tbl, tbu0, tbu1;
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 do {
293 asm volatile ("mftbu %0" : "=r" (tbu0));
294 asm volatile ("mftb %0" : "=r" (tbl));
295 asm volatile ("mftbu %0" : "=r" (tbu1));
296 } while (tbu0 != tbu1);
TRodziewiczd8540832021-06-10 15:16:50 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return tbl;
TRodziewiczd8540832021-06-10 15:16:50 +0200299}
300#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
301 __GNUC__ && ( __powerpc__ || __ppc__ ) */
302
303#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
304 defined(__GNUC__) && defined(__sparc64__)
305
306#if defined(__OpenBSD__)
307#warning OpenBSD does not allow access to tick register using software version instead
308#else
309#define HAVE_HARDCLOCK
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200312{
313 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
315 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200316}
317#endif /* __OpenBSD__ */
318#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
319 __GNUC__ && __sparc64__ */
320
321#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
322 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
323
324#define HAVE_HARDCLOCK
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200327{
328 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
330 asm volatile ("mov %%g1, %0" : "=r" (tick));
331 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200332}
333#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
334 __GNUC__ && __sparc__ && !__sparc64__ */
335
336#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
337 defined(__GNUC__) && defined(__alpha__)
338
339#define HAVE_HARDCLOCK
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200342{
343 unsigned long cc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 asm volatile ("rpcc %0" : "=r" (cc));
345 return cc & 0xFFFFFFFF;
TRodziewiczd8540832021-06-10 15:16:50 +0200346}
347#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
348 __GNUC__ && __alpha__ */
349
350#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
351 defined(__GNUC__) && defined(__ia64__)
352
353#define HAVE_HARDCLOCK
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200356{
357 unsigned long itc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
359 return itc;
TRodziewiczd8540832021-06-10 15:16:50 +0200360}
361#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
362 __GNUC__ && __ia64__ */
363
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300364#if !defined(HAVE_HARDCLOCK) && defined(_WIN32) && \
TRodziewiczd8540832021-06-10 15:16:50 +0200365 !defined(EFIX64) && !defined(EFI32)
366
367#define HAVE_HARDCLOCK
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200370{
371 LARGE_INTEGER offset;
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 QueryPerformanceCounter(&offset);
TRodziewiczd8540832021-06-10 15:16:50 +0200374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return (unsigned long) (offset.QuadPart);
TRodziewiczd8540832021-06-10 15:16:50 +0200376}
Martin Storsjö5c1479d2022-04-22 13:11:42 +0300377#endif /* !HAVE_HARDCLOCK && _WIN32 && !EFIX64 && !EFI32 */
TRodziewiczd8540832021-06-10 15:16:50 +0200378
379#if !defined(HAVE_HARDCLOCK)
380
381#define HAVE_HARDCLOCK
382
383static int hardclock_init = 0;
384static struct timeval tv_init;
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200387{
388 struct timeval tv_cur;
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (hardclock_init == 0) {
391 gettimeofday(&tv_init, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200392 hardclock_init = 1;
393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 gettimeofday(&tv_cur, NULL);
396 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
397 + (tv_cur.tv_usec - tv_init.tv_usec);
TRodziewiczd8540832021-06-10 15:16:50 +0200398}
399#endif /* !HAVE_HARDCLOCK */
400
401volatile int mbedtls_timing_alarmed = 0;
402
403#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
404
405/* It's OK to use a global because alarm() is supposed to be global anyway */
406static DWORD alarmMs;
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static void TimerProc(void *TimerContext)
TRodziewiczd8540832021-06-10 15:16:50 +0200409{
410 (void) TimerContext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 Sleep(alarmMs);
TRodziewiczd8540832021-06-10 15:16:50 +0200412 mbedtls_timing_alarmed = 1;
413 /* _endthread will be called implicitly on return
Tom Cosgrove1797b052022-12-04 17:19:59 +0000414 * That ensures execution of thread function's epilogue */
TRodziewiczd8540832021-06-10 15:16:50 +0200415}
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200418{
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200420 /* No need to create a thread for this simple case.
421 * Also, this shorcut is more reliable at least on MinGW32 */
422 mbedtls_timing_alarmed = 1;
423 return;
424 }
425
426 mbedtls_timing_alarmed = 0;
427 alarmMs = seconds * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 (void) _beginthread(TimerProc, 0, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200429}
430
431#else /* _WIN32 && !EFIX64 && !EFI32 */
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433static void sighandler(int signum)
TRodziewiczd8540832021-06-10 15:16:50 +0200434{
435 mbedtls_timing_alarmed = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 signal(signum, sighandler);
TRodziewiczd8540832021-06-10 15:16:50 +0200437}
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200440{
441 mbedtls_timing_alarmed = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 signal(SIGALRM, sighandler);
443 alarm(seconds);
444 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200445 /* alarm(0) cancelled any previous pending alarm, but the
446 handler won't fire, so raise the flag straight away. */
447 mbedtls_timing_alarmed = 1;
448 }
449}
450
451#endif /* _WIN32 && !EFIX64 && !EFI32 */
Matthias Schulz3b9240b2023-11-16 17:39:43 +0100452#endif /* !MBEDTLS_TIMING_ALT */
TRodziewiczd8540832021-06-10 15:16:50 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000455{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000456 size_t use_len;
457 int rnd;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (rng_state != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 while (len > 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000464 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (use_len > sizeof(int)) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000466 use_len = sizeof(int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Paul Bakkera3d195c2011-11-27 21:07:34 +0000468
469 rnd = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 memcpy(output, &rnd, use_len);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000471 output += use_len;
472 len -= use_len;
473 }
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000476}
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478#define CHECK_AND_CONTINUE(R) \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000479 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 int CHECK_AND_CONTINUE_ret = (R); \
481 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
482 mbedtls_printf("Feature not supported. Skipping.\n"); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000483 continue; \
484 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 else if (CHECK_AND_CONTINUE_ret != 0) { \
486 mbedtls_exit(1); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000487 } \
488 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000489
Gilles Peskine28f62f62020-07-24 02:06:46 +0200490#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100491static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
Gilles Peskine28f62f62020-07-24 02:06:46 +0200492{
493 const mbedtls_ecp_curve_info *found =
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_ecp_curve_info_from_name(string);
495 if (found != NULL) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200496 *curve = *found;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return 1;
498 } else {
499 return 0;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200500 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200501}
502#endif
503
Paul Bakker5121ce52009-01-03 21:22:43 +0000504unsigned char buf[BUFSIZE];
505
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200506typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200507 char md5, ripemd160, sha1, sha256, sha512,
Pol Henarejosebb36402022-05-20 14:26:00 +0200508 sha3_224, sha3_256, sha3_384, sha3_512,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200509 des3, des,
Dave Rodgman67223bb2024-01-12 16:37:07 +0000510 aes_cbc, aes_cfb128, aes_cfb8, aes_ctr, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200511 aes_cmac, des3_cmac,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200512 aria, camellia, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300513 poly1305,
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100514 ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200515 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200516} todo_list;
517
Simon Butcher63cb97e2018-12-06 17:43:31 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int main(int argc, char *argv[])
Paul Bakker5690efc2011-05-26 13:16:06 +0000520{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100521 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200522 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200524 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200526 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000527#endif
Gilles Peskine28f62f62020-07-24 02:06:46 +0200528#if defined(MBEDTLS_ECP_C)
529 mbedtls_ecp_curve_info single_curve[2] = {
530 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
531 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
532 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list();
Gilles Peskine28f62f62020-07-24 02:06:46 +0200534#endif
535
536#if defined(MBEDTLS_ECP_C)
537 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
538#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (argc <= 1) {
541 memset(&todo, 1, sizeof(todo));
542 } else {
543 memset(&todo, 0, sizeof(todo));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 for (i = 1; i < argc; i++) {
546 if (strcmp(argv[i], "md5") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200547 todo.md5 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 } else if (strcmp(argv[i], "ripemd160") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200549 todo.ripemd160 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 } else if (strcmp(argv[i], "sha1") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200551 todo.sha1 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 } else if (strcmp(argv[i], "sha256") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200553 todo.sha256 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 } else if (strcmp(argv[i], "sha512") == 0) {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200555 todo.sha512 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100556 } else if (strcmp(argv[i], "sha3_224") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200557 todo.sha3_224 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100558 } else if (strcmp(argv[i], "sha3_256") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200559 todo.sha3_256 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100560 } else if (strcmp(argv[i], "sha3_384") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200561 todo.sha3_384 = 1;
Pol Henarejos4e747332023-02-07 19:55:31 +0100562 } else if (strcmp(argv[i], "sha3_512") == 0) {
Pol Henarejosebb36402022-05-20 14:26:00 +0200563 todo.sha3_512 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 } else if (strcmp(argv[i], "des3") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200565 todo.des3 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 } else if (strcmp(argv[i], "des") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200567 todo.des = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 } else if (strcmp(argv[i], "aes_cbc") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200569 todo.aes_cbc = 1;
Yanray Wang55aba192023-09-12 09:03:50 +0800570 } else if (strcmp(argv[i], "aes_cfb128") == 0) {
571 todo.aes_cfb128 = 1;
Yanray Wang022b9a12023-09-12 09:45:37 +0800572 } else if (strcmp(argv[i], "aes_cfb8") == 0) {
573 todo.aes_cfb8 = 1;
Dave Rodgman67223bb2024-01-12 16:37:07 +0000574 } else if (strcmp(argv[i], "aes_ctr") == 0) {
575 todo.aes_ctr = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 } else if (strcmp(argv[i], "aes_xts") == 0) {
Aorimn5f778012016-06-09 23:22:58 +0200577 todo.aes_xts = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 } else if (strcmp(argv[i], "aes_gcm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200579 todo.aes_gcm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 } else if (strcmp(argv[i], "aes_ccm") == 0) {
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200581 todo.aes_ccm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 } else if (strcmp(argv[i], "chachapoly") == 0) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200583 todo.chachapoly = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 } else if (strcmp(argv[i], "aes_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100585 todo.aes_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 } else if (strcmp(argv[i], "des3_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100587 todo.des3_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else if (strcmp(argv[i], "aria") == 0) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100589 todo.aria = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else if (strcmp(argv[i], "camellia") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200591 todo.camellia = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 } else if (strcmp(argv[i], "chacha20") == 0) {
Daniel King34b822c2016-05-15 17:28:08 -0300593 todo.chacha20 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 } else if (strcmp(argv[i], "poly1305") == 0) {
Daniel Kingadc32c02016-05-16 18:25:45 -0300595 todo.poly1305 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200597 todo.ctr_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100599 todo.hmac_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 } else if (strcmp(argv[i], "rsa") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200601 todo.rsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 } else if (strcmp(argv[i], "dhm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200603 todo.dhm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 } else if (strcmp(argv[i], "ecdsa") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200605 todo.ecdsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 } else if (strcmp(argv[i], "ecdh") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200607 todo.ecdh = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200609#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 else if (set_ecp_curve(argv[i], single_curve)) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200611 curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200613#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 else {
615 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
616 mbedtls_printf("Available options: " OPTIONS);
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200617 }
618 }
619 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000625#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 memset(buf, 0xAA, sizeof(buf));
627 memset(tmp, 0xBB, sizeof(tmp));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200628
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100629 /* Avoid "unused static function" warning in configurations without
630 * symmetric crypto. */
Manuel Pégourié-Gonnardcd4ad0c2022-01-05 09:54:37 +0100631 (void) mbedtls_timing_hardclock;
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (todo.md5) {
635 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
636 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (todo.ripemd160) {
641 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
642 }
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100643#endif
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (todo.sha1) {
647 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
648 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000649#endif
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (todo.sha256) {
653 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
654 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000655#endif
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (todo.sha512) {
659 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
660 }
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000661#endif
Pol Henarejosebb36402022-05-20 14:26:00 +0200662#if defined(MBEDTLS_SHA3_C)
Pol Henarejosa6779282023-02-08 00:50:04 +0100663 if (todo.sha3_224) {
664 TIME_AND_TSC("SHA3-224", mbedtls_sha3(MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28));
665 }
666 if (todo.sha3_256) {
667 TIME_AND_TSC("SHA3-256", mbedtls_sha3(MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32));
668 }
669 if (todo.sha3_384) {
670 TIME_AND_TSC("SHA3-384", mbedtls_sha3(MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48));
671 }
672 if (todo.sha3_512) {
673 TIME_AND_TSC("SHA3-512", mbedtls_sha3(MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64));
674 }
Pol Henarejosebb36402022-05-20 14:26:00 +0200675#endif
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000676
Simon Butcher549dc3d2016-10-05 14:14:19 +0100677#if defined(MBEDTLS_DES_C)
678#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (todo.des3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_des3_context des3;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 mbedtls_des3_init(&des3);
683 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
684 mbedtls_exit(1);
685 }
686 TIME_AND_TSC("3DES",
687 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
688 mbedtls_des3_free(&des3);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200689 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (todo.des) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_des_context des;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 mbedtls_des_init(&des);
695 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
696 mbedtls_exit(1);
697 }
698 TIME_AND_TSC("DES",
699 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
700 mbedtls_des_free(&des);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200701 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100702
703#endif /* MBEDTLS_CIPHER_MODE_CBC */
704#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (todo.des3_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100706 unsigned char output[8];
707 const mbedtls_cipher_info_t *cipher_info;
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 memset(buf, 0, sizeof(buf));
710 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 TIME_AND_TSC("3DES-CMAC",
715 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
716 BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100717 }
718#endif /* MBEDTLS_CMAC_C */
719#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_AES_C)
722#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (todo.aes_cbc) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100724 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 mbedtls_aes_init(&aes);
728 for (keysize = 128; keysize <= 256; keysize += 64) {
729 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 memset(buf, 0, sizeof(buf));
732 memset(tmp, 0, sizeof(tmp));
733 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 TIME_AND_TSC(title,
736 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200737 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 mbedtls_aes_free(&aes);
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200740#endif
Yanray Wang55aba192023-09-12 09:03:50 +0800741#if defined(MBEDTLS_CIPHER_MODE_CFB)
742 if (todo.aes_cfb128) {
743 int keysize;
744 size_t iv_off = 0;
745 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800746
Yanray Wang55aba192023-09-12 09:03:50 +0800747 mbedtls_aes_init(&aes);
748 for (keysize = 128; keysize <= 256; keysize += 64) {
749 mbedtls_snprintf(title, sizeof(title), "AES-CFB128-%d", keysize);
750
751 memset(buf, 0, sizeof(buf));
752 memset(tmp, 0, sizeof(tmp));
753 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
754
755 TIME_AND_TSC(title,
756 mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE,
757 &iv_off, tmp, buf, buf));
758 }
759 mbedtls_aes_free(&aes);
760 }
Yanray Wang022b9a12023-09-12 09:45:37 +0800761 if (todo.aes_cfb8) {
762 int keysize;
763 mbedtls_aes_context aes;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800764
Yanray Wang022b9a12023-09-12 09:45:37 +0800765 mbedtls_aes_init(&aes);
766 for (keysize = 128; keysize <= 256; keysize += 64) {
767 mbedtls_snprintf(title, sizeof(title), "AES-CFB8-%d", keysize);
768
769 memset(buf, 0, sizeof(buf));
770 memset(tmp, 0, sizeof(tmp));
771 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
772
773 TIME_AND_TSC(title,
774 mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
775 }
776 mbedtls_aes_free(&aes);
777 }
Yanray Wang55aba192023-09-12 09:03:50 +0800778#endif
Dave Rodgman67223bb2024-01-12 16:37:07 +0000779#if defined(MBEDTLS_CIPHER_MODE_CTR)
780 if (todo.aes_ctr) {
781 int keysize;
782 mbedtls_aes_context aes;
783
784 uint8_t stream_block[16];
785 size_t nc_off;
786
787 mbedtls_aes_init(&aes);
788 for (keysize = 128; keysize <= 256; keysize += 64) {
789 mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
790
791 memset(buf, 0, sizeof(buf));
792 memset(tmp, 0, sizeof(tmp));
793 memset(stream_block, 0, sizeof(stream_block));
794 nc_off = 0;
795
796 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
797
798 TIME_AND_TSC(title, mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_off, tmp, stream_block,
799 buf, buf));
800 }
801 mbedtls_aes_free(&aes);
802 }
803#endif
Aorimn5f778012016-06-09 23:22:58 +0200804#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (todo.aes_xts) {
Aorimn5f778012016-06-09 23:22:58 +0200806 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100807 mbedtls_aes_xts_context ctx;
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 mbedtls_aes_xts_init(&ctx);
810 for (keysize = 128; keysize <= 256; keysize += 128) {
811 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
Aorimn5f778012016-06-09 23:22:58 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 memset(buf, 0, sizeof(buf));
814 memset(tmp, 0, sizeof(tmp));
815 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
Aorimn5f778012016-06-09 23:22:58 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 TIME_AND_TSC(title,
818 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
819 tmp, buf, buf));
Jaeden Amero9366feb2018-05-29 18:55:17 +0100820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_aes_xts_free(&ctx);
Aorimn5f778012016-06-09 23:22:58 +0200822 }
Aorimn5f778012016-06-09 23:22:58 +0200823 }
824#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (todo.aes_gcm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100827 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_gcm_init(&gcm);
831 for (keysize = 128; keysize <= 256; keysize += 64) {
832 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 memset(buf, 0, sizeof(buf));
835 memset(tmp, 0, sizeof(tmp));
836 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 TIME_AND_TSC(title,
839 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
840 12, NULL, 0, buf, buf, 16, tmp));
Paul Bakkerf70fe812013-12-16 16:43:10 +0100841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 mbedtls_gcm_free(&gcm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200843 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000844 }
845#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (todo.aes_ccm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100848 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 mbedtls_ccm_init(&ccm);
852 for (keysize = 128; keysize <= 256; keysize += 64) {
853 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 memset(buf, 0, sizeof(buf));
856 memset(tmp, 0, sizeof(tmp));
857 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 TIME_AND_TSC(title,
860 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
861 12, NULL, 0, buf, buf, tmp, 16));
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 mbedtls_ccm_free(&ccm);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200864 }
865 }
866#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200867#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (todo.chachapoly) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200869 mbedtls_chachapoly_context chachapoly;
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_chachapoly_init(&chachapoly);
872 memset(buf, 0, sizeof(buf));
873 memset(tmp, 0, sizeof(tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 mbedtls_chachapoly_setkey(&chachapoly, tmp);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 TIME_AND_TSC(title,
880 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
881 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 mbedtls_chachapoly_free(&chachapoly);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200884 }
885#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100886#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (todo.aes_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100888 unsigned char output[16];
889 const mbedtls_cipher_info_t *cipher_info;
890 mbedtls_cipher_type_t cipher_type;
891 int keysize;
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100894 keysize <= 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 keysize += 64, cipher_type++) {
896 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 memset(buf, 0, sizeof(buf));
899 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 TIME_AND_TSC(title,
904 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
905 buf, BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100906 }
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 memset(buf, 0, sizeof(buf));
909 memset(tmp, 0, sizeof(tmp));
910 TIME_AND_TSC("AES-CMAC-PRF-128",
911 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
912 output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100913 }
914#endif /* MBEDTLS_CMAC_C */
915#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100917#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (todo.aria) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100919 int keysize;
920 mbedtls_aria_context aria;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_aria_init(&aria);
923 for (keysize = 128; keysize <= 256; keysize += 64) {
924 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 memset(buf, 0, sizeof(buf));
927 memset(tmp, 0, sizeof(tmp));
928 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 TIME_AND_TSC(title,
931 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
932 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100933 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_aria_free(&aria);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100935 }
936#endif
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (todo.camellia) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100940 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_camellia_context camellia;
Yanray Wang5cae6e82023-10-09 18:40:17 +0800942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 mbedtls_camellia_init(&camellia);
944 for (keysize = 128; keysize <= 256; keysize += 64) {
945 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 memset(buf, 0, sizeof(buf));
948 memset(tmp, 0, sizeof(tmp));
949 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 TIME_AND_TSC(title,
952 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
953 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200954 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 mbedtls_camellia_free(&camellia);
Paul Bakker38119b12009-01-10 23:31:23 +0000956 }
957#endif
958
Daniel King34b822c2016-05-15 17:28:08 -0300959#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (todo.chacha20) {
961 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
Daniel King34b822c2016-05-15 17:28:08 -0300962 }
963#endif
964
Daniel Kingadc32c02016-05-16 18:25:45 -0300965#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (todo.poly1305) {
967 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
Daniel Kingadc32c02016-05-16 18:25:45 -0300968 }
969#endif
970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (todo.ctr_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 mbedtls_ctr_drbg_init(&ctr_drbg);
976 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 }
979 TIME_AND_TSC("CTR_DRBG (NOPR)",
980 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
981 mbedtls_ctr_drbg_free(&ctr_drbg);
Paul Bakker02faf452011-11-29 11:23:58 +0000982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_ctr_drbg_init(&ctr_drbg);
984 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 }
987 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
988 TIME_AND_TSC("CTR_DRBG (PR)",
989 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
990 mbedtls_ctr_drbg_free(&ctr_drbg);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200991 }
Paul Bakker02faf452011-11-29 11:23:58 +0000992#endif
993
Andrzej Kurek68327742022-10-03 06:18:18 -0400994#if defined(MBEDTLS_HMAC_DRBG_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
996 if (todo.hmac_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_hmac_drbg_context hmac_drbg;
998 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 mbedtls_hmac_drbg_init(&hmac_drbg);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 }
1010 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
1011 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 }
1016 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1017 MBEDTLS_HMAC_DRBG_PR_ON);
1018 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
1019 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001020#endif
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 }
1030 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
1031 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 }
1036 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
1037 MBEDTLS_HMAC_DRBG_PR_ON);
1038 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
1039 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001040#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 mbedtls_hmac_drbg_free(&hmac_drbg);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001042 }
Andrzej Kurek68327742022-10-03 06:18:18 -04001043#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +01001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (todo.rsa) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +01001047 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_rsa_context rsa;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001049
Matthias Schulzaa7dffa2023-11-16 15:31:32 +01001050 for (keysize = 2048; keysize <= 4096; keysize += 1024) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_rsa_init(&rsa);
1054 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 TIME_PUBLIC(title, " public",
1057 buf[0] = 0;
1058 ret = mbedtls_rsa_public(&rsa, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 TIME_PUBLIC(title, "private",
1061 buf[0] = 0;
1062 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001067#endif
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (todo.dhm) {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +02001071 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -08001072 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001073 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001074 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001075 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001076 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001077 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -08001078 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +01001079 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
1080
1081 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
1083 sizeof(dhm_P_3072) };
Hanno Beckerb9539212017-10-04 13:13:34 +01001084
1085 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
1087 sizeof(dhm_G_3072) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001090 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +02001091 size_t n;
Minos Galanakis97489dc2024-01-12 16:47:52 +00001092 mbedtls_mpi P, G;
1093 mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
Yanray Wang5cae6e82023-10-09 18:40:17 +08001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
1096 mbedtls_dhm_init(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001097
Minos Galanakis97489dc2024-01-12 16:47:52 +00001098 if (mbedtls_mpi_read_binary(&P, dhm_P[i],
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 dhm_P_size[i]) != 0 ||
Minos Galanakis97489dc2024-01-12 16:47:52 +00001100 mbedtls_mpi_read_binary(&G, dhm_G[i],
1101 dhm_G_size[i]) != 0 ||
1102 mbedtls_dhm_set_group(&dhm, &P, &G) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 mbedtls_exit(1);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +02001104 }
1105
Minos Galanakis97489dc2024-01-12 16:47:52 +00001106 n = mbedtls_dhm_get_len(&dhm);
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
Matthias Schulz70595f72023-11-16 17:43:58 +01001108 if (mbedtls_mpi_copy(&dhm.MBEDTLS_PRIVATE(GY), &dhm.MBEDTLS_PRIVATE(GX)) != 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
Janos Follath52735ef2018-08-15 10:19:16 +01001190#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (todo.ecdh) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001194 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
1195#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
1196 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001197#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001198#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
1199 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
1200#endif
1201 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
1202 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001204 size_t olen;
Gilles Peskine28f62f62020-07-24 02:06:46 +02001205 const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
1206 montgomery_curve_list;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (curve_list == (const mbedtls_ecp_curve_info *) &single_curve) {
Gilles Peskine28f62f62020-07-24 02:06:46 +02001209 mbedtls_ecp_group grp;
Yanray Wang5cae6e82023-10-09 18:40:17 +08001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 mbedtls_ecp_group_init(&grp);
1212 if (mbedtls_ecp_group_load(&grp, curve_list->grp_id) != 0) {
1213 mbedtls_exit(1);
1214 }
1215 if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Gilles Peskine28f62f62020-07-24 02:06:46 +02001216 selected_montgomery_curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 } else { /* empty list */
Gilles Peskine28f62f62020-07-24 02:06:46 +02001218 selected_montgomery_curve_list = single_curve + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 }
1220 mbedtls_ecp_group_free(&grp);
Gilles Peskine28f62f62020-07-24 02:06:46 +02001221 }
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 curve_info++) {
1226 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001227 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 mbedtls_ecdh_init(&ecdh);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1233 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1234 myrand, NULL));
1235 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1238 curve_info->name);
1239 TIME_PUBLIC(title, "handshake",
1240 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1241 myrand, NULL));
1242 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1243 myrand, NULL)));
1244 mbedtls_ecdh_free(&ecdh);
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001245 }
1246
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001247 /* Montgomery curves need to be handled separately */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 for (curve_info = selected_montgomery_curve_list;
1249 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1250 curve_info++) {
1251 mbedtls_ecdh_init(&ecdh);
1252 mbedtls_mpi_init(&z);
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1255 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1258 curve_info->name);
1259 TIME_PUBLIC(title, "handshake",
1260 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q,
1261 myrand, NULL));
1262 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1263 &ecdh.d,
1264 myrand, NULL)));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_ecdh_free(&ecdh);
1267 mbedtls_mpi_free(&z);
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001268 }
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 curve_info++) {
1273 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001274 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 mbedtls_ecdh_init(&ecdh);
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1280 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1281 myrand, NULL));
1282 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
1283 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1284 myrand, NULL));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1287 curve_info->name);
1288 TIME_PUBLIC(title, "handshake",
1289 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1290 myrand, NULL)));
1291 mbedtls_ecdh_free(&ecdh);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001292 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001293
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001294 /* Montgomery curves need to be handled separately */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 for (curve_info = selected_montgomery_curve_list;
1296 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1297 curve_info++) {
1298 mbedtls_ecdh_init(&ecdh);
1299 mbedtls_mpi_init(&z);
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1302 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp,
1303 myrand, NULL));
1304 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1307 curve_info->name);
1308 TIME_PUBLIC(title, "handshake",
1309 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1310 &ecdh.d,
1311 myrand, NULL)));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 mbedtls_ecdh_free(&ecdh);
1314 mbedtls_mpi_free(&z);
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001315 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001316 }
1317#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001318
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001319#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (todo.ecdh) {
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001321 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1322 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001323 const mbedtls_ecp_curve_info *curve_info;
1324 size_t olen;
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 for (curve_info = curve_list;
1327 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1328 curve_info++) {
1329 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001330 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 mbedtls_ecdh_init(&ecdh_srv);
1334 mbedtls_ecdh_init(&ecdh_cli);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1337 TIME_PUBLIC(title,
1338 "full handshake",
1339 const unsigned char *p_srv = buf_srv;
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1342 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &olen, buf_srv,
1343 sizeof(buf_srv), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1346 p_srv + olen));
1347 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &olen, buf_cli,
1348 sizeof(buf_cli), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 CHECK_AND_CONTINUE(mbedtls_ecdh_read_public(&ecdh_srv, buf_cli, olen));
1351 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_srv, &olen, buf_srv,
1352 sizeof(buf_srv), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &olen, buf_cli,
1355 sizeof(buf_cli), myrand, NULL));
1356 mbedtls_ecdh_free(&ecdh_cli);
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 mbedtls_ecdh_free(&ecdh_srv);
1359 );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001360
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001361 }
1362 }
1363#endif
1364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 mbedtls_printf("\n");
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1368 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001369#endif
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_exit(0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001372}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001373
Andrzej Kurek6056e7a2022-03-02 12:01:10 -05001374#endif /* MBEDTLS_HAVE_TIME */