blob: 773c538291fc3fc44810acd086fe8c81509e1818 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Bence Szépkútic662b362021-05-27 11:25:03 +020022#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000025
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050026#if !defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000028{
Andrzej Kurek6056e7a2022-03-02 12:01:10 -050029 mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010030 mbedtls_exit(0);
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000031}
32#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010033
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000034#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020035#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/md5.h"
38#include "mbedtls/ripemd160.h"
39#include "mbedtls/sha1.h"
40#include "mbedtls/sha256.h"
41#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010042
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/des.h"
44#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010045#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030047#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/gcm.h"
49#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020050#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010051#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030052#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/ctr_drbg.h"
55#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/rsa.h"
58#include "mbedtls/dhm.h"
59#include "mbedtls/ecdsa.h"
60#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000063
David Horstmanne3d8f312023-01-03 11:07:09 +000064/* *INDENT-OFF* */
TRodziewicz90f304f2021-06-11 11:56:47 +020065#ifndef asm
66#define asm __asm
67#endif
David Horstmanne3d8f312023-01-03 11:07:09 +000068/* *INDENT-ON* */
TRodziewicz90f304f2021-06-11 11:56:47 +020069
TRodziewiczd8540832021-06-10 15:16:50 +020070#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
71
72#include <windows.h>
73#include <process.h>
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020076 LARGE_INTEGER start;
77};
78
79#else
80
81#include <unistd.h>
82#include <sys/types.h>
83#include <sys/time.h>
84#include <signal.h>
85#include <time.h>
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087struct _hr_time {
TRodziewiczd8540832021-06-10 15:16:50 +020088 struct timeval start;
89};
90
91#endif /* _WIN32 && !EFIX64 && !EFI32 */
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000094#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000095#endif
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097static void mbedtls_set_alarm(int seconds);
TRodziewiczd8540832021-06-10 15:16:50 +020098
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000099/*
100 * For heap usage estimates, we need an estimate of the overhead per allocated
101 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
102 * so use that as our baseline.
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104#define MEM_BLOCK_OVERHEAD (2 * sizeof(size_t))
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000105
106/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200107 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000108 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +0100109#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000110
Paul Bakker02faf452011-11-29 11:23:58 +0000111#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100112#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100113#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000114
Rich Evans85b05ec2015-02-12 11:37:29 +0000115#define OPTIONS \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200116 "md5, ripemd160, sha1, sha256, sha512,\n" \
117 "des3, des, camellia, chacha20,\n" \
Zhai Zhaoxuane22da992020-05-09 23:50:32 +0800118 "aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300119 "aes_cmac, des3_cmac, poly1305\n" \
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100120 "ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000121 "rsa, dhm, ecdsa, ecdh.\n"
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000124#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_strerror(ret, (char *) tmp, sizeof(tmp)); \
126 mbedtls_printf("FAILED: %s\n", tmp);
Rich Evans85b05ec2015-02-12 11:37:29 +0000127#else
128#define PRINT_ERROR \
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_printf("FAILED: -0x%04x\n", (unsigned int) -ret);
Rich Evans85b05ec2015-02-12 11:37:29 +0000130#endif
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132#define TIME_AND_TSC(TITLE, CODE) \
133 do { \
134 unsigned long ii, jj, tsc; \
135 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_printf(HEADER_FORMAT, TITLE); \
138 fflush(stdout); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000139 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_set_alarm(1); \
141 for (ii = 1; ret == 0 && !mbedtls_timing_alarmed; ii++) \
142 { \
143 ret = CODE; \
144 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000145 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 tsc = mbedtls_timing_hardclock(); \
147 for (jj = 0; ret == 0 && jj < 1024; jj++) \
148 { \
149 ret = CODE; \
150 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000151 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (ret != 0) \
153 { \
154 PRINT_ERROR; \
155 } \
156 else \
157 { \
158 mbedtls_printf("%9lu KiB/s, %9lu cycles/byte\n", \
159 ii * BUFSIZE / 1024, \
160 (mbedtls_timing_hardclock() - tsc) \
161 / (jj * BUFSIZE)); \
162 } \
163 } while (0)
Rich Evans85b05ec2015-02-12 11:37:29 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100166
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200167/* How much space to reserve for the title when printing heap usage results.
168 * Updated manually as the output of the following command:
169 *
170 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100171 * awk '{print length+3}' | sort -rn | head -n1
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200172 *
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100173 * This computes the maximum length of a title +3, because we appends "/s" and
174 * want at least one space. (If the value is too small, the only consequence
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800175 * is poor alignment.) */
Manuel Pégourié-Gonnardbf5b46c2022-01-05 10:34:17 +0100176#define TITLE_SPACE 17
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200177
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100178#define MEMORY_MEASURE_INIT \
179 size_t max_used, max_blocks, max_bytes; \
180 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnard6ced0022022-01-05 10:05:54 +0100181 size_t alloc_cnt, free_cnt, prv_alloc, prv_free; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_memory_buffer_alloc_cur_get(&prv_used, &prv_blocks); \
183 mbedtls_memory_buffer_alloc_max_reset();
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100184
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100185#define MEMORY_MEASURE_RESET \
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_memory_buffer_alloc_count_get(&prv_alloc, &prv_free);
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188#define MEMORY_MEASURE_PRINT(title_len) \
189 mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks); \
190 mbedtls_memory_buffer_alloc_count_get(&alloc_cnt, &free_cnt); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200191 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 while (ii--) mbedtls_printf(" "); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100193 max_used -= prv_used; \
194 max_blocks -= prv_blocks; \
195 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_printf("%6u heap bytes, %6u allocs", \
197 (unsigned) max_bytes, \
198 (unsigned) (alloc_cnt - prv_alloc));
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100199
200#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000201#define MEMORY_MEASURE_INIT
Manuel Pégourié-Gonnardc4055442022-01-04 10:24:01 +0100202#define MEMORY_MEASURE_RESET
Gilles Peskine449bd832023-01-11 14:50:10 +0100203#define MEMORY_MEASURE_PRINT(title_len)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100204#endif
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206#define TIME_PUBLIC(TITLE, TYPE, CODE) \
207 do { \
208 unsigned long ii; \
209 int ret; \
210 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000211 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 mbedtls_printf(HEADER_FORMAT, TITLE); \
213 fflush(stdout); \
214 mbedtls_set_alarm(3); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000215 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 ret = 0; \
217 for (ii = 1; !mbedtls_timing_alarmed && !ret; ii++) \
218 { \
219 MEMORY_MEASURE_RESET; \
220 CODE; \
221 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000222 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) \
224 { \
225 mbedtls_printf("Feature Not Supported. Skipping.\n"); \
226 ret = 0; \
227 } \
228 else if (ret != 0) \
229 { \
230 PRINT_ERROR; \
231 } \
232 else \
233 { \
234 mbedtls_printf("%6lu " TYPE "/s", ii / 3); \
235 MEMORY_MEASURE_PRINT(sizeof(TYPE) + 1); \
236 mbedtls_printf("\n"); \
237 } \
238 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
TRodziewiczd8540832021-06-10 15:16:50 +0200240#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
TRodziewiczd8540832021-06-10 15:16:50 +0200242
243#define HAVE_HARDCLOCK
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200246{
247 unsigned long tsc;
248 __asm rdtsc
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 __asm mov[tsc], eax
250 return tsc;
TRodziewiczd8540832021-06-10 15:16:50 +0200251}
252#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
253 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
254
255/* some versions of mingw-64 have 32-bit longs even on x84_64 */
256#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 defined(__GNUC__) && (defined(__i386__) || ( \
258 (defined(__amd64__) || defined(__x86_64__)) && __SIZEOF_LONG__ == 4))
TRodziewiczd8540832021-06-10 15:16:50 +0200259
260#define HAVE_HARDCLOCK
261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200263{
264 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
266 return lo;
TRodziewiczd8540832021-06-10 15:16:50 +0200267}
268#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
269 __GNUC__ && __i386__ */
270
271#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
TRodziewiczd8540832021-06-10 15:16:50 +0200273
274#define HAVE_HARDCLOCK
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200277{
278 unsigned long lo, hi;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
280 return lo | (hi << 32);
TRodziewiczd8540832021-06-10 15:16:50 +0200281}
282#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
283 __GNUC__ && ( __amd64__ || __x86_64__ ) */
284
285#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
TRodziewiczd8540832021-06-10 15:16:50 +0200287
288#define HAVE_HARDCLOCK
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200291{
292 unsigned long tbl, tbu0, tbu1;
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 do {
295 asm volatile ("mftbu %0" : "=r" (tbu0));
296 asm volatile ("mftb %0" : "=r" (tbl));
297 asm volatile ("mftbu %0" : "=r" (tbu1));
298 } while (tbu0 != tbu1);
TRodziewiczd8540832021-06-10 15:16:50 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return tbl;
TRodziewiczd8540832021-06-10 15:16:50 +0200301}
302#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
303 __GNUC__ && ( __powerpc__ || __ppc__ ) */
304
305#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
306 defined(__GNUC__) && defined(__sparc64__)
307
308#if defined(__OpenBSD__)
309#warning OpenBSD does not allow access to tick register using software version instead
310#else
311#define HAVE_HARDCLOCK
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200314{
315 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 asm volatile ("rdpr %%tick, %0;" : "=&r" (tick));
317 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200318}
319#endif /* __OpenBSD__ */
320#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
321 __GNUC__ && __sparc64__ */
322
323#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
324 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
325
326#define HAVE_HARDCLOCK
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200329{
330 unsigned long tick;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 asm volatile (".byte 0x83, 0x41, 0x00, 0x00");
332 asm volatile ("mov %%g1, %0" : "=r" (tick));
333 return tick;
TRodziewiczd8540832021-06-10 15:16:50 +0200334}
335#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
336 __GNUC__ && __sparc__ && !__sparc64__ */
337
338#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
339 defined(__GNUC__) && defined(__alpha__)
340
341#define HAVE_HARDCLOCK
342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200344{
345 unsigned long cc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 asm volatile ("rpcc %0" : "=r" (cc));
347 return cc & 0xFFFFFFFF;
TRodziewiczd8540832021-06-10 15:16:50 +0200348}
349#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
350 __GNUC__ && __alpha__ */
351
352#if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) && \
353 defined(__GNUC__) && defined(__ia64__)
354
355#define HAVE_HARDCLOCK
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200358{
359 unsigned long itc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 asm volatile ("mov %0 = ar.itc" : "=r" (itc));
361 return itc;
TRodziewiczd8540832021-06-10 15:16:50 +0200362}
363#endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
364 __GNUC__ && __ia64__ */
365
366#if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
367 !defined(EFIX64) && !defined(EFI32)
368
369#define HAVE_HARDCLOCK
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200372{
373 LARGE_INTEGER offset;
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 QueryPerformanceCounter(&offset);
TRodziewiczd8540832021-06-10 15:16:50 +0200376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return (unsigned long) (offset.QuadPart);
TRodziewiczd8540832021-06-10 15:16:50 +0200378}
379#endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
380
381#if !defined(HAVE_HARDCLOCK)
382
383#define HAVE_HARDCLOCK
384
385static int hardclock_init = 0;
386static struct timeval tv_init;
387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388static unsigned long mbedtls_timing_hardclock(void)
TRodziewiczd8540832021-06-10 15:16:50 +0200389{
390 struct timeval tv_cur;
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (hardclock_init == 0) {
393 gettimeofday(&tv_init, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200394 hardclock_init = 1;
395 }
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 gettimeofday(&tv_cur, NULL);
398 return (tv_cur.tv_sec - tv_init.tv_sec) * 1000000U
399 + (tv_cur.tv_usec - tv_init.tv_usec);
TRodziewiczd8540832021-06-10 15:16:50 +0200400}
401#endif /* !HAVE_HARDCLOCK */
402
403volatile int mbedtls_timing_alarmed = 0;
404
405#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
406
407/* It's OK to use a global because alarm() is supposed to be global anyway */
408static DWORD alarmMs;
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410static void TimerProc(void *TimerContext)
TRodziewiczd8540832021-06-10 15:16:50 +0200411{
412 (void) TimerContext;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 Sleep(alarmMs);
TRodziewiczd8540832021-06-10 15:16:50 +0200414 mbedtls_timing_alarmed = 1;
415 /* _endthread will be called implicitly on return
Tom Cosgrove1797b052022-12-04 17:19:59 +0000416 * That ensures execution of thread function's epilogue */
TRodziewiczd8540832021-06-10 15:16:50 +0200417}
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200420{
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200422 /* No need to create a thread for this simple case.
423 * Also, this shorcut is more reliable at least on MinGW32 */
424 mbedtls_timing_alarmed = 1;
425 return;
426 }
427
428 mbedtls_timing_alarmed = 0;
429 alarmMs = seconds * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 (void) _beginthread(TimerProc, 0, NULL);
TRodziewiczd8540832021-06-10 15:16:50 +0200431}
432
433#else /* _WIN32 && !EFIX64 && !EFI32 */
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static void sighandler(int signum)
TRodziewiczd8540832021-06-10 15:16:50 +0200436{
437 mbedtls_timing_alarmed = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 signal(signum, sighandler);
TRodziewiczd8540832021-06-10 15:16:50 +0200439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441static void mbedtls_set_alarm(int seconds)
TRodziewiczd8540832021-06-10 15:16:50 +0200442{
443 mbedtls_timing_alarmed = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 signal(SIGALRM, sighandler);
445 alarm(seconds);
446 if (seconds == 0) {
TRodziewiczd8540832021-06-10 15:16:50 +0200447 /* alarm(0) cancelled any previous pending alarm, but the
448 handler won't fire, so raise the flag straight away. */
449 mbedtls_timing_alarmed = 1;
450 }
451}
452
453#endif /* _WIN32 && !EFIX64 && !EFI32 */
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455static int myrand(void *rng_state, unsigned char *output, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000457 size_t use_len;
458 int rnd;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (rng_state != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 rng_state = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 while (len > 0) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000465 use_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (use_len > sizeof(int)) {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000467 use_len = sizeof(int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 }
Paul Bakkera3d195c2011-11-27 21:07:34 +0000469
470 rnd = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 memcpy(output, &rnd, use_len);
Paul Bakkera3d195c2011-11-27 21:07:34 +0000472 output += use_len;
473 len -= use_len;
474 }
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000477}
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479#define CHECK_AND_CONTINUE(R) \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000480 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 int CHECK_AND_CONTINUE_ret = (R); \
482 if (CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
483 mbedtls_printf("Feature not supported. Skipping.\n"); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000484 continue; \
485 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 else if (CHECK_AND_CONTINUE_ret != 0) { \
487 mbedtls_exit(1); \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000488 } \
489 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000490
Gilles Peskine28f62f62020-07-24 02:06:46 +0200491#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100492static int set_ecp_curve(const char *string, mbedtls_ecp_curve_info *curve)
Gilles Peskine28f62f62020-07-24 02:06:46 +0200493{
494 const mbedtls_ecp_curve_info *found =
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 mbedtls_ecp_curve_info_from_name(string);
496 if (found != NULL) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200497 *curve = *found;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return 1;
499 } else {
500 return 0;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200501 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200502}
503#endif
504
Paul Bakker5121ce52009-01-03 21:22:43 +0000505unsigned char buf[BUFSIZE];
506
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200507typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200508 char md5, ripemd160, sha1, sha256, sha512,
509 des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200510 aes_cbc, 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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 } else if (strcmp(argv[i], "des3") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200557 todo.des3 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 } else if (strcmp(argv[i], "des") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200559 todo.des = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 } else if (strcmp(argv[i], "aes_cbc") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200561 todo.aes_cbc = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 } else if (strcmp(argv[i], "aes_xts") == 0) {
Aorimn5f778012016-06-09 23:22:58 +0200563 todo.aes_xts = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 } else if (strcmp(argv[i], "aes_gcm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200565 todo.aes_gcm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 } else if (strcmp(argv[i], "aes_ccm") == 0) {
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200567 todo.aes_ccm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 } else if (strcmp(argv[i], "chachapoly") == 0) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200569 todo.chachapoly = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 } else if (strcmp(argv[i], "aes_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100571 todo.aes_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 } else if (strcmp(argv[i], "des3_cmac") == 0) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100573 todo.des3_cmac = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 } else if (strcmp(argv[i], "aria") == 0) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100575 todo.aria = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 } else if (strcmp(argv[i], "camellia") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200577 todo.camellia = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 } else if (strcmp(argv[i], "chacha20") == 0) {
Daniel King34b822c2016-05-15 17:28:08 -0300579 todo.chacha20 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 } else if (strcmp(argv[i], "poly1305") == 0) {
Daniel Kingadc32c02016-05-16 18:25:45 -0300581 todo.poly1305 = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 } else if (strcmp(argv[i], "ctr_drbg") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200583 todo.ctr_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 } else if (strcmp(argv[i], "hmac_drbg") == 0) {
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100585 todo.hmac_drbg = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 } else if (strcmp(argv[i], "rsa") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200587 todo.rsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else if (strcmp(argv[i], "dhm") == 0) {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200589 todo.dhm = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else if (strcmp(argv[i], "ecdsa") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200591 todo.ecdsa = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 } else if (strcmp(argv[i], "ecdh") == 0) {
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200593 todo.ecdh = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200595#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 else if (set_ecp_curve(argv[i], single_curve)) {
Gilles Peskine28f62f62020-07-24 02:06:46 +0200597 curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Gilles Peskine28f62f62020-07-24 02:06:46 +0200599#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 else {
601 mbedtls_printf("Unrecognized option: %s\n", argv[i]);
602 mbedtls_printf("Available options: " OPTIONS);
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200603 }
604 }
605 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 mbedtls_memory_buffer_alloc_init(alloc_buf, sizeof(alloc_buf));
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000611#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 memset(buf, 0xAA, sizeof(buf));
613 memset(tmp, 0xBB, sizeof(tmp));
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200614
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100615 /* Avoid "unused static function" warning in configurations without
616 * symmetric crypto. */
Manuel Pégourié-Gonnardcd4ad0c2022-01-05 09:54:37 +0100617 (void) mbedtls_timing_hardclock;
Manuel Pégourié-Gonnarda93aa582022-01-04 09:47:54 +0100618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (todo.md5) {
621 TIME_AND_TSC("MD5", mbedtls_md5(buf, BUFSIZE, tmp));
622 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000623#endif
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (todo.ripemd160) {
627 TIME_AND_TSC("RIPEMD160", mbedtls_ripemd160(buf, BUFSIZE, tmp));
628 }
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100629#endif
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (todo.sha1) {
633 TIME_AND_TSC("SHA-1", mbedtls_sha1(buf, BUFSIZE, tmp));
634 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000635#endif
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (todo.sha256) {
639 TIME_AND_TSC("SHA-256", mbedtls_sha256(buf, BUFSIZE, tmp, 0));
640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641#endif
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (todo.sha512) {
645 TIME_AND_TSC("SHA-512", mbedtls_sha512(buf, BUFSIZE, tmp, 0));
646 }
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000647#endif
648
Simon Butcher549dc3d2016-10-05 14:14:19 +0100649#if defined(MBEDTLS_DES_C)
650#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (todo.des3) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_des3_context des3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 mbedtls_des3_init(&des3);
654 if (mbedtls_des3_set3key_enc(&des3, tmp) != 0) {
655 mbedtls_exit(1);
656 }
657 TIME_AND_TSC("3DES",
658 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
659 mbedtls_des3_free(&des3);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200660 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (todo.des) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_des_context des;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 mbedtls_des_init(&des);
665 if (mbedtls_des_setkey_enc(&des, tmp) != 0) {
666 mbedtls_exit(1);
667 }
668 TIME_AND_TSC("DES",
669 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf));
670 mbedtls_des_free(&des);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200671 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100672
673#endif /* MBEDTLS_CIPHER_MODE_CBC */
674#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (todo.des3_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100676 unsigned char output[8];
677 const mbedtls_cipher_info_t *cipher_info;
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 memset(buf, 0, sizeof(buf));
680 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 TIME_AND_TSC("3DES-CMAC",
685 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
686 BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100687 }
688#endif /* MBEDTLS_CMAC_C */
689#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#if defined(MBEDTLS_AES_C)
692#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (todo.aes_cbc) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100694 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_aes_context aes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_aes_init(&aes);
697 for (keysize = 128; keysize <= 256; keysize += 64) {
698 mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 memset(buf, 0, sizeof(buf));
701 memset(tmp, 0, sizeof(tmp));
702 CHECK_AND_CONTINUE(mbedtls_aes_setkey_enc(&aes, tmp, keysize));
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 TIME_AND_TSC(title,
705 mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200706 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 mbedtls_aes_free(&aes);
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200709#endif
Aorimn5f778012016-06-09 23:22:58 +0200710#if defined(MBEDTLS_CIPHER_MODE_XTS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (todo.aes_xts) {
Aorimn5f778012016-06-09 23:22:58 +0200712 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100713 mbedtls_aes_xts_context ctx;
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 mbedtls_aes_xts_init(&ctx);
716 for (keysize = 128; keysize <= 256; keysize += 128) {
717 mbedtls_snprintf(title, sizeof(title), "AES-XTS-%d", keysize);
Aorimn5f778012016-06-09 23:22:58 +0200718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 memset(buf, 0, sizeof(buf));
720 memset(tmp, 0, sizeof(tmp));
721 CHECK_AND_CONTINUE(mbedtls_aes_xts_setkey_enc(&ctx, tmp, keysize * 2));
Aorimn5f778012016-06-09 23:22:58 +0200722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 TIME_AND_TSC(title,
724 mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
725 tmp, buf, buf));
Jaeden Amero9366feb2018-05-29 18:55:17 +0100726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 mbedtls_aes_xts_free(&ctx);
Aorimn5f778012016-06-09 23:22:58 +0200728 }
Aorimn5f778012016-06-09 23:22:58 +0200729 }
730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_GCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (todo.aes_gcm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100733 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_gcm_init(&gcm);
737 for (keysize = 128; keysize <= 256; keysize += 64) {
738 mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 memset(buf, 0, sizeof(buf));
741 memset(tmp, 0, sizeof(tmp));
742 mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Paul Bakker89e80c92012-03-20 13:50:09 +0000743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TIME_AND_TSC(title,
745 mbedtls_gcm_crypt_and_tag(&gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
746 12, NULL, 0, buf, buf, 16, tmp));
Paul Bakkerf70fe812013-12-16 16:43:10 +0100747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 mbedtls_gcm_free(&gcm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200749 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000750 }
751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_CCM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (todo.aes_ccm) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100754 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 mbedtls_ccm_init(&ccm);
758 for (keysize = 128; keysize <= 256; keysize += 64) {
759 mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 memset(buf, 0, sizeof(buf));
762 memset(tmp, 0, sizeof(tmp));
763 mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 TIME_AND_TSC(title,
766 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp,
767 12, NULL, 0, buf, buf, tmp, 16));
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 mbedtls_ccm_free(&ccm);
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200770 }
771 }
772#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200773#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (todo.chachapoly) {
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200775 mbedtls_chachapoly_context chachapoly;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 mbedtls_chachapoly_init(&chachapoly);
778 memset(buf, 0, sizeof(buf));
779 memset(tmp, 0, sizeof(tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 mbedtls_snprintf(title, sizeof(title), "ChaCha20-Poly1305");
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 mbedtls_chachapoly_setkey(&chachapoly, tmp);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 TIME_AND_TSC(title,
786 mbedtls_chachapoly_encrypt_and_tag(&chachapoly,
787 BUFSIZE, tmp, NULL, 0, buf, buf, tmp));
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 mbedtls_chachapoly_free(&chachapoly);
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200790 }
791#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100792#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (todo.aes_cmac) {
Simon Butcher549dc3d2016-10-05 14:14:19 +0100794 unsigned char output[16];
795 const mbedtls_cipher_info_t *cipher_info;
796 mbedtls_cipher_type_t cipher_type;
797 int keysize;
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 for (keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100800 keysize <= 256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 keysize += 64, cipher_type++) {
802 mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 memset(buf, 0, sizeof(buf));
805 memset(tmp, 0, sizeof(tmp));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
Simon Butcher549dc3d2016-10-05 14:14:19 +0100808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 TIME_AND_TSC(title,
810 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
811 buf, BUFSIZE, output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100812 }
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 memset(buf, 0, sizeof(buf));
815 memset(tmp, 0, sizeof(tmp));
816 TIME_AND_TSC("AES-CMAC-PRF-128",
817 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
818 output));
Simon Butcher549dc3d2016-10-05 14:14:19 +0100819 }
820#endif /* MBEDTLS_CMAC_C */
821#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100823#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (todo.aria) {
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100825 int keysize;
826 mbedtls_aria_context aria;
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 mbedtls_aria_init(&aria);
828 for (keysize = 128; keysize <= 256; keysize += 64) {
829 mbedtls_snprintf(title, sizeof(title), "ARIA-CBC-%d", keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 memset(buf, 0, sizeof(buf));
832 memset(tmp, 0, sizeof(tmp));
833 mbedtls_aria_setkey_enc(&aria, tmp, keysize);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 TIME_AND_TSC(title,
836 mbedtls_aria_crypt_cbc(&aria, MBEDTLS_ARIA_ENCRYPT,
837 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100838 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 mbedtls_aria_free(&aria);
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100840 }
841#endif
842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (todo.camellia) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100845 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_camellia_context camellia;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_camellia_init(&camellia);
848 for (keysize = 128; keysize <= 256; keysize += 64) {
849 mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d", keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 memset(buf, 0, sizeof(buf));
852 memset(tmp, 0, sizeof(tmp));
853 mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
Paul Bakker38119b12009-01-10 23:31:23 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 TIME_AND_TSC(title,
856 mbedtls_camellia_crypt_cbc(&camellia, MBEDTLS_CAMELLIA_ENCRYPT,
857 BUFSIZE, tmp, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200858 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 mbedtls_camellia_free(&camellia);
Paul Bakker38119b12009-01-10 23:31:23 +0000860 }
861#endif
862
Daniel King34b822c2016-05-15 17:28:08 -0300863#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (todo.chacha20) {
865 TIME_AND_TSC("ChaCha20", mbedtls_chacha20_crypt(buf, buf, 0U, BUFSIZE, buf, buf));
Daniel King34b822c2016-05-15 17:28:08 -0300866 }
867#endif
868
Daniel Kingadc32c02016-05-16 18:25:45 -0300869#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (todo.poly1305) {
871 TIME_AND_TSC("Poly1305", mbedtls_poly1305_mac(buf, buf, BUFSIZE, buf));
Daniel Kingadc32c02016-05-16 18:25:45 -0300872 }
873#endif
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (todo.ctr_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_ctr_drbg_init(&ctr_drbg);
880 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 }
883 TIME_AND_TSC("CTR_DRBG (NOPR)",
884 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
885 mbedtls_ctr_drbg_free(&ctr_drbg);
Paul Bakker02faf452011-11-29 11:23:58 +0000886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 mbedtls_ctr_drbg_init(&ctr_drbg);
888 if (mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 }
891 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON);
892 TIME_AND_TSC("CTR_DRBG (PR)",
893 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
894 mbedtls_ctr_drbg_free(&ctr_drbg);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200895 }
Paul Bakker02faf452011-11-29 11:23:58 +0000896#endif
897
Andrzej Kurek68327742022-10-03 06:18:18 -0400898#if defined(MBEDTLS_HMAC_DRBG_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 (defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C))
900 if (todo.hmac_drbg) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_hmac_drbg_context hmac_drbg;
902 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 mbedtls_hmac_drbg_init(&hmac_drbg);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 }
914 TIME_AND_TSC("HMAC_DRBG SHA-1 (NOPR)",
915 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 }
920 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
921 MBEDTLS_HMAC_DRBG_PR_ON);
922 TIME_AND_TSC("HMAC_DRBG SHA-1 (PR)",
923 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100924#endif
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)) == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 }
934 TIME_AND_TSC("HMAC_DRBG SHA-256 (NOPR)",
935 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_exit(1);
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 }
940 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
941 MBEDTLS_HMAC_DRBG_PR_ON);
942 TIME_AND_TSC("HMAC_DRBG SHA-256 (PR)",
943 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100944#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 mbedtls_hmac_drbg_free(&hmac_drbg);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100946 }
Andrzej Kurek68327742022-10-03 06:18:18 -0400947#endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (todo.rsa) {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100951 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_rsa_context rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 for (keysize = 2048; keysize <= 4096; keysize *= 2) {
954 mbedtls_snprintf(title, sizeof(title), "RSA-%d", keysize);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_rsa_init(&rsa);
957 mbedtls_rsa_gen_key(&rsa, myrand, NULL, keysize, 65537);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 TIME_PUBLIC(title, " public",
960 buf[0] = 0;
961 ret = mbedtls_rsa_public(&rsa, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 TIME_PUBLIC(title, "private",
964 buf[0] = 0;
965 ret = mbedtls_rsa_private(&rsa, myrand, NULL, buf, buf));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_rsa_free(&rsa);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200968 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000970#endif
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (todo.dhm) {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200974 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800975 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100976 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800977 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100978 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800979 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100980 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800981 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100982 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
983
984 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 const size_t dhm_P_size[] = { sizeof(dhm_P_2048),
986 sizeof(dhm_P_3072) };
Hanno Beckerb9539212017-10-04 13:13:34 +0100987
988 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 const size_t dhm_G_size[] = { sizeof(dhm_G_2048),
990 sizeof(dhm_G_3072) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200993 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +0200994 size_t n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) {
996 mbedtls_dhm_init(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (mbedtls_mpi_read_binary(&dhm.P, dhm_P[i],
999 dhm_P_size[i]) != 0 ||
1000 mbedtls_mpi_read_binary(&dhm.G, dhm_G[i],
1001 dhm_G_size[i]) != 0) {
1002 mbedtls_exit(1);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +02001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 n = mbedtls_mpi_size(&dhm.P);
1006 mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL);
1007 if (mbedtls_mpi_copy(&dhm.GY, &dhm.GX) != 0) {
1008 mbedtls_exit(1);
1009 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
1012 TIME_PUBLIC(title, "handshake",
1013 ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n,
1014 myrand, NULL);
1015 ret |=
1016 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
1019 TIME_PUBLIC(title, "handshake",
1020 ret |=
1021 mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL));
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 mbedtls_dhm_free(&dhm);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001024 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001025 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +01001026#endif
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (todo.ecdsa) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_ecdsa_context ecdsa;
1031 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001032 size_t sig_len;
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 memset(buf, 0x2A, sizeof(buf));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 curve_info++) {
1039 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +00001040 continue;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001041 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_ecdsa_init(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0) {
1046 mbedtls_exit(1);
1047 }
1048
1049 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1050 curve_info->name);
1051 TIME_PUBLIC(title,
1052 "sign",
1053 ret =
1054 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
1055 curve_info->bit_size,
1056 tmp, sizeof(tmp), &sig_len, myrand,
1057 NULL));
1058
1059 mbedtls_ecdsa_free(&ecdsa);
1060 }
1061
1062 for (curve_info = curve_list;
1063 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1064 curve_info++) {
1065 if (!mbedtls_ecdsa_can_do(curve_info->grp_id)) {
1066 continue;
1067 }
1068
1069 mbedtls_ecdsa_init(&ecdsa);
1070
1071 if (mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL) != 0 ||
1072 mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1073 tmp, sizeof(tmp), &sig_len, myrand, NULL) != 0) {
1074 mbedtls_exit(1);
1075 }
1076
1077 mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
1078 curve_info->name);
1079 TIME_PUBLIC(title, "verify",
1080 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf, curve_info->bit_size,
1081 tmp, sig_len));
1082
1083 mbedtls_ecdsa_free(&ecdsa);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001084 }
1085 }
1086#endif
1087
Janos Follath52735ef2018-08-15 10:19:16 +01001088#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (todo.ecdh) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001092 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
1093#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
1094 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001095#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001096#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
1097 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
1098#endif
1099 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
1100 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001102 size_t olen;
Gilles Peskine28f62f62020-07-24 02:06:46 +02001103 const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
1104 montgomery_curve_list;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (curve_list == (const mbedtls_ecp_curve_info *) &single_curve) {
Gilles Peskine28f62f62020-07-24 02:06:46 +02001107 mbedtls_ecp_group grp;
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 mbedtls_ecp_group_init(&grp);
1109 if (mbedtls_ecp_group_load(&grp, curve_list->grp_id) != 0) {
1110 mbedtls_exit(1);
1111 }
1112 if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
Gilles Peskine28f62f62020-07-24 02:06:46 +02001113 selected_montgomery_curve_list = single_curve;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 } else { /* empty list */
Gilles Peskine28f62f62020-07-24 02:06:46 +02001115 selected_montgomery_curve_list = single_curve + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 }
1117 mbedtls_ecp_group_free(&grp);
Gilles Peskine28f62f62020-07-24 02:06:46 +02001118 }
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 curve_info++) {
1123 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001124 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_ecdh_init(&ecdh);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1130 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1131 myrand, NULL));
1132 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1135 curve_info->name);
1136 TIME_PUBLIC(title, "handshake",
1137 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1138 myrand, NULL));
1139 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1140 myrand, NULL)));
1141 mbedtls_ecdh_free(&ecdh);
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001142 }
1143
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001144 /* Montgomery curves need to be handled separately */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 for (curve_info = selected_montgomery_curve_list;
1146 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1147 curve_info++) {
1148 mbedtls_ecdh_init(&ecdh);
1149 mbedtls_mpi_init(&z);
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1152 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
1155 curve_info->name);
1156 TIME_PUBLIC(title, "handshake",
1157 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q,
1158 myrand, NULL));
1159 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1160 &ecdh.d,
1161 myrand, NULL)));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 mbedtls_ecdh_free(&ecdh);
1164 mbedtls_mpi_free(&z);
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001165 }
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 for (curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 curve_info++) {
1170 if (!mbedtls_ecdh_can_do(curve_info->grp_id)) {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001171 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 mbedtls_ecdh_init(&ecdh);
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1177 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1178 myrand, NULL));
1179 CHECK_AND_CONTINUE(mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q));
1180 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
1181 myrand, NULL));
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1184 curve_info->name);
1185 TIME_PUBLIC(title, "handshake",
1186 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh, &olen, buf, sizeof(buf),
1187 myrand, NULL)));
1188 mbedtls_ecdh_free(&ecdh);
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001189 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001190
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001191 /* Montgomery curves need to be handled separately */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 for (curve_info = selected_montgomery_curve_list;
1193 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1194 curve_info++) {
1195 mbedtls_ecdh_init(&ecdh);
1196 mbedtls_mpi_init(&z);
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 CHECK_AND_CONTINUE(mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id));
1199 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp,
1200 myrand, NULL));
1201 CHECK_AND_CONTINUE(mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
1204 curve_info->name);
1205 TIME_PUBLIC(title, "handshake",
1206 CHECK_AND_CONTINUE(mbedtls_ecdh_compute_shared(&ecdh.grp, &z, &ecdh.Qp,
1207 &ecdh.d,
1208 myrand, NULL)));
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_ecdh_free(&ecdh);
1211 mbedtls_mpi_free(&z);
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001212 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001213 }
1214#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001215
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001216#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (todo.ecdh) {
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001218 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1219 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001220 const mbedtls_ecp_curve_info *curve_info;
1221 size_t olen;
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 for (curve_info = curve_list;
1224 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1225 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_srv);
1231 mbedtls_ecdh_init(&ecdh_cli);
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 mbedtls_snprintf(title, sizeof(title), "ECDHE-%s", curve_info->name);
1234 TIME_PUBLIC(title,
1235 "full handshake",
1236 const unsigned char *p_srv = buf_srv;
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 CHECK_AND_CONTINUE(mbedtls_ecdh_setup(&ecdh_srv, curve_info->grp_id));
1239 CHECK_AND_CONTINUE(mbedtls_ecdh_make_params(&ecdh_srv, &olen, buf_srv,
1240 sizeof(buf_srv), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 CHECK_AND_CONTINUE(mbedtls_ecdh_read_params(&ecdh_cli, &p_srv,
1243 p_srv + olen));
1244 CHECK_AND_CONTINUE(mbedtls_ecdh_make_public(&ecdh_cli, &olen, buf_cli,
1245 sizeof(buf_cli), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 CHECK_AND_CONTINUE(mbedtls_ecdh_read_public(&ecdh_srv, buf_cli, olen));
1248 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_srv, &olen, buf_srv,
1249 sizeof(buf_srv), myrand, NULL));
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 CHECK_AND_CONTINUE(mbedtls_ecdh_calc_secret(&ecdh_cli, &olen, buf_cli,
1252 sizeof(buf_cli), myrand, NULL));
1253 mbedtls_ecdh_free(&ecdh_cli);
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 mbedtls_ecdh_free(&ecdh_srv);
1256 );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001257
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001258 }
1259 }
1260#endif
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 mbedtls_printf("\n");
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1265 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001266#endif
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 mbedtls_exit(0);
Paul Bakker5121ce52009-01-03 21:22:43 +00001269}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001270
Andrzej Kurek6056e7a2022-03-02 12:01:10 -05001271#endif /* MBEDTLS_HAVE_TIME */