blob: d63009a0a8a7be3b2901a1108e9d10d86f98ae72 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_exit exit
59#define mbedtls_printf printf
60#define mbedtls_snprintf snprintf
61#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010062#define mbedtls_exit exit
63#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
64#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000068int main( void )
69{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020071 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000072}
73#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010074
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000075#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020076#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010077
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/md4.h"
81#include "mbedtls/md5.h"
82#include "mbedtls/ripemd160.h"
83#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010086
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/arc4.h"
88#include "mbedtls/des.h"
89#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010090#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000091#include "mbedtls/blowfish.h"
92#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030093#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000094#include "mbedtls/gcm.h"
95#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020096#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010097#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030098#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010099
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000100#include "mbedtls/havege.h"
101#include "mbedtls/ctr_drbg.h"
102#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100103
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000104#include "mbedtls/rsa.h"
105#include "mbedtls/dhm.h"
106#include "mbedtls/ecdsa.h"
107#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100108
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000109#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000112#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +0000113#endif
114
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000115/*
116 * For heap usage estimates, we need an estimate of the overhead per allocated
117 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
118 * so use that as our baseline.
119 */
120#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
121
122/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200123 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000124 */
125#define HEAP_SIZE (1u << 16) // 64k
126
Paul Bakker02faf452011-11-29 11:23:58 +0000127#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100128#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100129#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000130
Rich Evans85b05ec2015-02-12 11:37:29 +0000131#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200132 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300133 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200134 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300135 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136 "havege, ctr_drbg, hmac_drbg\n" \
137 "rsa, dhm, ecdsa, ecdh.\n"
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000140#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100141 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000143#else
144#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000146#endif
147
148#define TIME_AND_TSC( TITLE, CODE ) \
149do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200150 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200151 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000152 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200153 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000154 fflush( stdout ); \
155 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200156 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200157 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000158 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200159 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000160 } \
161 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200162 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200163 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000164 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200165 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000166 } \
167 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200168 if( ret != 0 ) \
169 { \
170 PRINT_ERROR; \
171 } \
172 else \
173 { \
174 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
175 ii * BUFSIZE / 1024, \
176 ( mbedtls_timing_hardclock() - tsc ) \
177 / ( jj * BUFSIZE ) ); \
178 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000179} while( 0 )
180
Gilles Peskine621333f2021-07-07 21:08:28 +0200181#define CHECK_AND_CONTINUE( R ) \
182 { \
183 int CHECK_AND_CONTINUE_ret = ( R ); \
184 if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
185 mbedtls_printf( "Feature not supported. Skipping.\n" ); \
186 continue; \
187 } \
188 else if( CHECK_AND_CONTINUE_ret != 0 ) { \
189 mbedtls_exit( 1 ); \
190 } \
191 }
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100194
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200195/* How much space to reserve for the title when printing heap usage results.
196 * Updated manually as the output of the following command:
197 *
198 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
199 * awk '{print length+2}' | sort -rn | head -n1
200 *
201 * This computes the maximum length of a title +2 (because we appends "/s").
202 * (If the value is too small, the only consequence is poor alignement.) */
203#define TITLE_SPACE 11
204
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100205#define MEMORY_MEASURE_INIT \
206 size_t max_used, max_blocks, max_bytes; \
207 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200208 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100210
211#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200212 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200213 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
214 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100215 max_used -= prv_used; \
216 max_blocks -= prv_blocks; \
217 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100219
220#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000221#define MEMORY_MEASURE_INIT
222#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100223#endif
224
Rich Evans85b05ec2015-02-12 11:37:29 +0000225#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
226do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200227 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000228 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100229 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000230 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200231 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000232 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200233 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000234 \
235 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200236 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000237 { \
238 CODE; \
239 } \
240 \
241 if( ret != 0 ) \
242 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100243 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000244 } \
245 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100246 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200247 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100248 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200249 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100250 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000251} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
Paul Bakkera3d195c2011-11-27 21:07:34 +0000253static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000254{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000255 size_t use_len;
256 int rnd;
257
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 if( rng_state != NULL )
259 rng_state = NULL;
260
Paul Bakkera3d195c2011-11-27 21:07:34 +0000261 while( len > 0 )
262 {
263 use_len = len;
264 if( use_len > sizeof(int) )
265 use_len = sizeof(int);
266
267 rnd = rand();
268 memcpy( output, &rnd, use_len );
269 output += use_len;
270 len -= use_len;
271 }
272
273 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274}
275
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100276/*
277 * Clear some memory that was used to prepare the context
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_ECP_C)
280void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100281{
282 if( grp->T != NULL )
283 {
284 size_t i;
285 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_ecp_point_free( &grp->T[i] );
287 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100288 }
289 grp->T = NULL;
290 grp->T_size = 0;
291}
292#else
293#define ecp_clear_precomputed( g )
294#endif
295
Paul Bakker5121ce52009-01-03 21:22:43 +0000296unsigned char buf[BUFSIZE];
297
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200298typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200299 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100300 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200301 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200302 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200303 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300304 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100305 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200306 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200307} todo_list;
308
Simon Butcher63cb97e2018-12-06 17:43:31 +0000309
Paul Bakkercce9d772011-11-18 14:26:47 +0000310int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000311{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100312 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200313 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200314 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200315 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200317 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000318#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000319
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000320 if( argc <= 1 )
321 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200322 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000323 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200324 else
325 {
326 memset( &todo, 0, sizeof( todo ) );
327
328 for( i = 1; i < argc; i++ )
329 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200330 if( strcmp( argv[i], "md4" ) == 0 )
331 todo.md4 = 1;
332 else if( strcmp( argv[i], "md5" ) == 0 )
333 todo.md5 = 1;
334 else if( strcmp( argv[i], "ripemd160" ) == 0 )
335 todo.ripemd160 = 1;
336 else if( strcmp( argv[i], "sha1" ) == 0 )
337 todo.sha1 = 1;
338 else if( strcmp( argv[i], "sha256" ) == 0 )
339 todo.sha256 = 1;
340 else if( strcmp( argv[i], "sha512" ) == 0 )
341 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 else if( strcmp( argv[i], "arc4" ) == 0 )
343 todo.arc4 = 1;
344 else if( strcmp( argv[i], "des3" ) == 0 )
345 todo.des3 = 1;
346 else if( strcmp( argv[i], "des" ) == 0 )
347 todo.des = 1;
348 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
349 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200350 else if( strcmp( argv[i], "aes_xts" ) == 0 )
351 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200352 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
353 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200354 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
355 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200356 else if( strcmp( argv[i], "chachapoly" ) == 0 )
357 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100358 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
359 todo.aes_cmac = 1;
360 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
361 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100362 else if( strcmp( argv[i], "aria" ) == 0 )
363 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200364 else if( strcmp( argv[i], "camellia" ) == 0 )
365 todo.camellia = 1;
366 else if( strcmp( argv[i], "blowfish" ) == 0 )
367 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300368 else if( strcmp( argv[i], "chacha20" ) == 0 )
369 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300370 else if( strcmp( argv[i], "poly1305" ) == 0 )
371 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200372 else if( strcmp( argv[i], "havege" ) == 0 )
373 todo.havege = 1;
374 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
375 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100376 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
377 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200378 else if( strcmp( argv[i], "rsa" ) == 0 )
379 todo.rsa = 1;
380 else if( strcmp( argv[i], "dhm" ) == 0 )
381 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200382 else if( strcmp( argv[i], "ecdsa" ) == 0 )
383 todo.ecdsa = 1;
384 else if( strcmp( argv[i], "ecdh" ) == 0 )
385 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200386 else
387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
389 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200390 }
391 }
392 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200397 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000398#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200399 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200400 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200403 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405#endif
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200408 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100409 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410#endif
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200413 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100414 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100415#endif
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200418 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100419 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420#endif
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200423 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100424 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425#endif
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200428 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100429 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000430#endif
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200433 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_arc4_context arc4;
436 mbedtls_arc4_init( &arc4 );
437 mbedtls_arc4_setup( &arc4, tmp, 32 );
438 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
439 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200440 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000441#endif
442
Simon Butcher549dc3d2016-10-05 14:14:19 +0100443#if defined(MBEDTLS_DES_C)
444#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200445 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_des3_context des3;
448 mbedtls_des3_init( &des3 );
Gilles Peskine621333f2021-07-07 21:08:28 +0200449 if( mbedtls_des3_set3key_enc( &des3, tmp ) != 0 )
450 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200451 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
453 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200454 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200456 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_des_context des;
459 mbedtls_des_init( &des );
Gilles Peskine621333f2021-07-07 21:08:28 +0200460 if( mbedtls_des_setkey_enc( &des, tmp ) != 0 )
461 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200462 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
464 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200465 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100466
467#endif /* MBEDTLS_CIPHER_MODE_CBC */
468#if defined(MBEDTLS_CMAC_C)
469 if( todo.des3_cmac )
470 {
471 unsigned char output[8];
472 const mbedtls_cipher_info_t *cipher_info;
473
474 memset( buf, 0, sizeof( buf ) );
475 memset( tmp, 0, sizeof( tmp ) );
476
477 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
478
479 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100480 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
481 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100482 }
483#endif /* MBEDTLS_CMAC_C */
484#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_AES_C)
487#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200488 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100490 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_aes_context aes;
492 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200493 for( keysize = 128; keysize <= 256; keysize += 64 )
494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200497 memset( buf, 0, sizeof( buf ) );
498 memset( tmp, 0, sizeof( tmp ) );
Gilles Peskine621333f2021-07-07 21:08:28 +0200499 CHECK_AND_CONTINUE( mbedtls_aes_setkey_enc( &aes, tmp, keysize ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200501 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200506#endif
Aorimn5f778012016-06-09 23:22:58 +0200507#if defined(MBEDTLS_CIPHER_MODE_XTS)
508 if( todo.aes_xts )
509 {
510 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100511 mbedtls_aes_xts_context ctx;
512
513 mbedtls_aes_xts_init( &ctx );
514 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200515 {
516 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
517
518 memset( buf, 0, sizeof( buf ) );
519 memset( tmp, 0, sizeof( tmp ) );
Gilles Peskine621333f2021-07-07 21:08:28 +0200520 CHECK_AND_CONTINUE( mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 ) );
Aorimn5f778012016-06-09 23:22:58 +0200521
522 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100523 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
524 tmp, buf, buf ) );
525
526 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200527 }
Aorimn5f778012016-06-09 23:22:58 +0200528 }
529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200531 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000532 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100533 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200535
536 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200537 for( keysize = 128; keysize <= 256; keysize += 64 )
538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000540
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200541 memset( buf, 0, sizeof( buf ) );
542 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200543 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000544
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200545 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200547 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200550 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000551 }
552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200554 if( todo.aes_ccm )
555 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100556 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200558
559 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200560 for( keysize = 128; keysize <= 256; keysize += 64 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200563
564 memset( buf, 0, sizeof( buf ) );
565 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200566 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200567
568 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200570 12, NULL, 0, buf, buf, tmp, 16 ) );
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200573 }
574 }
575#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200576#if defined(MBEDTLS_CHACHAPOLY_C)
577 if( todo.chachapoly )
578 {
579 mbedtls_chachapoly_context chachapoly;
580
581 mbedtls_chachapoly_init( &chachapoly );
582 memset( buf, 0, sizeof( buf ) );
583 memset( tmp, 0, sizeof( tmp ) );
584
585 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
586
587 mbedtls_chachapoly_setkey( &chachapoly, tmp );
588
589 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200590 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
591 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200592
593 mbedtls_chachapoly_free( &chachapoly );
594 }
595#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100596#if defined(MBEDTLS_CMAC_C)
597 if( todo.aes_cmac )
598 {
599 unsigned char output[16];
600 const mbedtls_cipher_info_t *cipher_info;
601 mbedtls_cipher_type_t cipher_type;
602 int keysize;
603
604 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
605 keysize <= 256;
606 keysize += 64, cipher_type++ )
607 {
608 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
609
610 memset( buf, 0, sizeof( buf ) );
611 memset( tmp, 0, sizeof( tmp ) );
612
613 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
614
615 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100616 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
617 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100618 }
619
620 memset( buf, 0, sizeof( buf ) );
621 memset( tmp, 0, sizeof( tmp ) );
622 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100623 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
624 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100625 }
626#endif /* MBEDTLS_CMAC_C */
627#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100629#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
630 if( todo.aria )
631 {
632 int keysize;
633 mbedtls_aria_context aria;
634 mbedtls_aria_init( &aria );
635 for( keysize = 128; keysize <= 256; keysize += 64 )
636 {
637 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
638
639 memset( buf, 0, sizeof( buf ) );
640 memset( tmp, 0, sizeof( tmp ) );
641 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
642
643 TIME_AND_TSC( title,
644 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
645 BUFSIZE, tmp, buf, buf ) );
646 }
647 mbedtls_aria_free( &aria );
648 }
649#endif
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200652 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000653 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100654 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_camellia_context camellia;
656 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200657 for( keysize = 128; keysize <= 256; keysize += 64 )
658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000660
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200661 memset( buf, 0, sizeof( buf ) );
662 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000664
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200665 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200667 BUFSIZE, tmp, buf, buf ) );
668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000670 }
671#endif
672
Daniel King34b822c2016-05-15 17:28:08 -0300673#if defined(MBEDTLS_CHACHA20_C)
674 if ( todo.chacha20 )
675 {
676 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
677 }
678#endif
679
Daniel Kingadc32c02016-05-16 18:25:45 -0300680#if defined(MBEDTLS_POLY1305_C)
681 if ( todo.poly1305 )
682 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200683 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300684 }
685#endif
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200688 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000689 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100690 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_blowfish_context blowfish;
692 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200693
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200694 for( keysize = 128; keysize <= 256; keysize += 64 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000697
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200698 memset( buf, 0, sizeof( buf ) );
699 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000701
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200702 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200704 tmp, buf, buf ) );
705 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000708 }
709#endif
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200712 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_havege_state hs;
715 mbedtls_havege_init( &hs );
716 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
717 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200718 }
Paul Bakker02faf452011-11-29 11:23:58 +0000719#endif
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200722 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000725
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200726 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200727 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200729 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200730 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100731 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000732
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100733 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200734 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_exit(1);
736 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200737 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200738 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200740 }
Paul Bakker02faf452011-11-29 11:23:58 +0000741#endif
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100744 if( todo.hmac_drbg )
745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_hmac_drbg_context hmac_drbg;
747 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100748
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200749 mbedtls_hmac_drbg_init( &hmac_drbg );
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#if defined(MBEDTLS_SHA1_C)
752 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
753 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100754
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200755 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100757 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200758 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100759
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200760 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_exit(1);
762 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
763 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100764 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200765 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100766#endif
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SHA256_C)
769 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
770 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100771
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200772 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100774 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200775 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100776
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200777 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_exit(1);
779 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
780 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100781 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200782 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100783#endif
Ron Eldor278af452018-06-20 18:40:21 +0300784 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100785 }
786#endif
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200789 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100791 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200793 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
798 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200799
800 TIME_PUBLIC( title, " public",
801 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200803
804 TIME_PUBLIC( title, "private",
805 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200809 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000811#endif
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200814 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100815 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200816 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800817 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100818 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800819 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100820 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800821 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100822 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800823 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100824 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
825
826 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
827 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
828 sizeof( dhm_P_3072 ) };
829
830 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
831 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
832 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200835 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200836 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200839
Hanno Beckerb9539212017-10-04 13:13:34 +0100840 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
841 dhm_P_size[i] ) != 0 ||
842 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
843 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200846 }
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 dhm.len = mbedtls_mpi_size( &dhm.P );
849 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
850 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
851 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200854 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200856 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100857 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200860 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100861 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200864 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100865 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100866#endif
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200869 if( todo.ecdsa )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_ecdsa_context ecdsa;
872 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200873 size_t sig_len;
874
875 memset( buf, 0x2A, sizeof( buf ) );
876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 for( curve_info = mbedtls_ecp_curve_list();
878 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200879 curve_info++ )
880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
884 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100885 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200888 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200889 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200890 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200891 tmp, &sig_len, myrand, NULL ) );
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100894 }
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 for( curve_info = mbedtls_ecp_curve_list();
897 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100898 curve_info++ )
899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200903 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100904 tmp, &sig_len, myrand, NULL ) != 0 )
905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100907 }
908 ecp_clear_precomputed( &ecdsa.grp );
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100911 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200912 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200913 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200914 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200917 }
918 }
919#endif
920
Janos Follath52735ef2018-08-15 10:19:16 +0100921#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200922 if( todo.ecdh )
923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000926 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
927#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
928 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000929#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000930#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
931 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
932#endif
933 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
934 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200936 size_t olen;
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 for( curve_info = mbedtls_ecp_curve_list();
939 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200940 curve_info++ )
941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200943
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200944 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200946 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200950 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100951 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200954 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200955 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200957 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200959 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100961 }
962
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000963 /* Montgomery curves need to be handled separately */
964 for ( curve_info = montgomery_curve_list;
965 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
966 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000967 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000968 mbedtls_ecdh_init( &ecdh );
969 mbedtls_mpi_init( &z );
970
971 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
972 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
973 {
974 mbedtls_exit( 1 );
975 }
976
977 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
978 curve_info->name );
979 TIME_PUBLIC( title, "handshake",
980 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
981 myrand, NULL );
982 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
983 myrand, NULL ) );
984
985 mbedtls_ecdh_free( &ecdh );
986 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000987 }
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 for( curve_info = mbedtls_ecp_curve_list();
990 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100991 curve_info++ )
992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100994
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200995 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100997 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
999 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001000 myrand, NULL ) != 0 )
1001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001003 }
1004 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +02001007 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001008 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001010 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001012 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001013
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001014 /* Montgomery curves need to be handled separately */
1015 for ( curve_info = montgomery_curve_list;
1016 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1017 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001018 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001019 mbedtls_ecdh_init( &ecdh );
1020 mbedtls_mpi_init( &z );
1021
1022 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
1023 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1024 myrand, NULL ) != 0 ||
1025 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
1026 {
1027 mbedtls_exit( 1 );
1028 }
1029
1030 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1031 curve_info->name );
1032 TIME_PUBLIC( title, "handshake",
1033 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1034 myrand, NULL ) );
1035
1036 mbedtls_ecdh_free( &ecdh );
1037 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001038 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001039 }
1040#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1045 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001046#endif
1047
Paul Bakkercce9d772011-11-18 14:26:47 +00001048#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 fflush( stdout ); getchar();
1051#endif
1052
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001053 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_TIMING_C */