blob: 3eceec00234bb98da44c69a9ad7677c79ac28b0d [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Christoph M. Wintersteigercc91fe22019-02-20 18:06:00 +000029#if !defined(MBEDTLS_PLATFORM_C)
Rich Evans18b78c72015-02-11 14:06:19 +000030#include <stdio.h>
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010031#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_exit exit
33#define mbedtls_printf printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_free free
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000038int main( void )
39{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
k-stachowiak776521a2019-05-23 09:46:47 +020041 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000042}
43#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010044
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000045#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020046#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010047
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/md5.h"
51#include "mbedtls/ripemd160.h"
52#include "mbedtls/sha1.h"
53#include "mbedtls/sha256.h"
54#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010055
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/des.h"
57#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010058#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030060#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/gcm.h"
62#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020063#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010064#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030065#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010066
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/ctr_drbg.h"
68#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010069
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/rsa.h"
71#include "mbedtls/dhm.h"
72#include "mbedtls/ecdsa.h"
73#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000079#endif
80
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000081/*
82 * For heap usage estimates, we need an estimate of the overhead per allocated
83 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
84 * so use that as our baseline.
85 */
86#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
87
88/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020089 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000090 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +010091#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000092
Paul Bakker02faf452011-11-29 11:23:58 +000093#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010094#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010095#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000096
Rich Evans85b05ec2015-02-12 11:37:29 +000097#define OPTIONS \
TRodziewicz10e8cf52021-05-31 17:58:57 +020098 "md5, ripemd160, sha1, sha256, sha512,\n" \
99 "des3, des, camellia, chacha20,\n" \
Zhai Zhaoxuane22da992020-05-09 23:50:32 +0800100 "aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300101 "aes_cmac, des3_cmac, poly1305\n" \
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100102 "ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000103 "rsa, dhm, ecdsa, ecdh.\n"
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000106#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100107 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000109#else
110#define PRINT_ERROR \
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200111 mbedtls_printf( "FAILED: -0x%04x\n", (unsigned int) -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000112#endif
113
114#define TIME_AND_TSC( TITLE, CODE ) \
115do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200116 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200117 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000118 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200119 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000120 fflush( stdout ); \
121 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200122 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200123 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000124 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200125 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000126 } \
127 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200128 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200129 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000130 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200131 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000132 } \
133 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200134 if( ret != 0 ) \
135 { \
136 PRINT_ERROR; \
137 } \
138 else \
139 { \
140 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
141 ii * BUFSIZE / 1024, \
142 ( mbedtls_timing_hardclock() - tsc ) \
143 / ( jj * BUFSIZE ) ); \
144 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000145} while( 0 )
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100148
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200149/* How much space to reserve for the title when printing heap usage results.
150 * Updated manually as the output of the following command:
151 *
152 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
153 * awk '{print length+2}' | sort -rn | head -n1
154 *
155 * This computes the maximum length of a title +2 (because we appends "/s").
156 * (If the value is too small, the only consequence is poor alignement.) */
157#define TITLE_SPACE 16
158
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100159#define MEMORY_MEASURE_INIT \
160 size_t max_used, max_blocks, max_bytes; \
161 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200162 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100164
165#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200166 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnard5edd3882020-04-09 10:40:03 +0200167 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
168 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100169 max_used -= prv_used; \
170 max_blocks -= prv_blocks; \
171 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100173
174#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000175#define MEMORY_MEASURE_INIT
176#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100177#endif
178
Rich Evans85b05ec2015-02-12 11:37:29 +0000179#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
180do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200181 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000182 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100183 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000184 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200185 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000186 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200187 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000188 \
189 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200190 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000191 { \
192 CODE; \
193 } \
194 \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000195 if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) \
196 { \
197 mbedtls_printf( "Feature Not Supported. Skipping.\n" ); \
198 ret = 0; \
199 } \
200 else if( ret != 0 ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000201 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100202 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000203 } \
204 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100205 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200206 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100207 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200208 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100209 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000210} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Paul Bakkera3d195c2011-11-27 21:07:34 +0000212static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000214 size_t use_len;
215 int rnd;
216
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 if( rng_state != NULL )
218 rng_state = NULL;
219
Paul Bakkera3d195c2011-11-27 21:07:34 +0000220 while( len > 0 )
221 {
222 use_len = len;
223 if( use_len > sizeof(int) )
224 use_len = sizeof(int);
225
226 rnd = rand();
227 memcpy( output, &rnd, use_len );
228 output += use_len;
229 len -= use_len;
230 }
231
232 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233}
234
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000235#define CHECK_AND_CONTINUE( R ) \
236 { \
Gilles Peskineb14c4a52019-02-11 18:23:42 +0100237 int CHECK_AND_CONTINUE_ret = ( R ); \
238 if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000239 mbedtls_printf( "Feature not supported. Skipping.\n" ); \
240 continue; \
241 } \
Gilles Peskineb14c4a52019-02-11 18:23:42 +0100242 else if( CHECK_AND_CONTINUE_ret != 0 ) { \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000243 mbedtls_exit( 1 ); \
244 } \
245 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000246
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100247/*
248 * Clear some memory that was used to prepare the context
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_ECP_C)
251void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100252{
kXuanba9cb762021-04-08 14:32:06 +0800253 if( grp->T != NULL
254#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
255 && grp->T_size != 0
256#endif
257 )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100258 {
259 size_t i;
260 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_ecp_point_free( &grp->T[i] );
262 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100263 }
264 grp->T = NULL;
265 grp->T_size = 0;
266}
267#else
268#define ecp_clear_precomputed( g )
269#endif
270
Gilles Peskine28f62f62020-07-24 02:06:46 +0200271#if defined(MBEDTLS_ECP_C)
272static int set_ecp_curve( const char *string, mbedtls_ecp_curve_info *curve )
273{
274 const mbedtls_ecp_curve_info *found =
275 mbedtls_ecp_curve_info_from_name( string );
276 if( found != NULL )
277 {
278 *curve = *found;
279 return( 1 );
280 }
281 else
282 return( 0 );
283}
284#endif
285
Paul Bakker5121ce52009-01-03 21:22:43 +0000286unsigned char buf[BUFSIZE];
287
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288typedef struct {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200289 char md5, ripemd160, sha1, sha256, sha512,
290 des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200291 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200292 aes_cmac, des3_cmac,
TRodziewicz10e8cf52021-05-31 17:58:57 +0200293 aria, camellia, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300294 poly1305,
Mateusz Starzyk0fdcc8e2021-01-29 16:46:31 +0100295 ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200296 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297} todo_list;
298
Simon Butcher63cb97e2018-12-06 17:43:31 +0000299
Paul Bakkercce9d772011-11-18 14:26:47 +0000300int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000301{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100302 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200303 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200304 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200305 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200307 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000308#endif
Gilles Peskine28f62f62020-07-24 02:06:46 +0200309#if defined(MBEDTLS_ECP_C)
310 mbedtls_ecp_curve_info single_curve[2] = {
311 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
312 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
313 };
314 const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list( );
315#endif
316
317#if defined(MBEDTLS_ECP_C)
318 (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
319#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000320
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000321 if( argc <= 1 )
322 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200323 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000324 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200325 else
326 {
327 memset( &todo, 0, sizeof( todo ) );
328
329 for( i = 1; i < argc; i++ )
330 {
TRodziewicz10e8cf52021-05-31 17:58:57 +0200331 if( strcmp( argv[i], "md5" ) == 0 )
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200332 todo.md5 = 1;
333 else if( strcmp( argv[i], "ripemd160" ) == 0 )
334 todo.ripemd160 = 1;
335 else if( strcmp( argv[i], "sha1" ) == 0 )
336 todo.sha1 = 1;
337 else if( strcmp( argv[i], "sha256" ) == 0 )
338 todo.sha256 = 1;
339 else if( strcmp( argv[i], "sha512" ) == 0 )
340 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200341 else if( strcmp( argv[i], "des3" ) == 0 )
342 todo.des3 = 1;
343 else if( strcmp( argv[i], "des" ) == 0 )
344 todo.des = 1;
345 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
346 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200347 else if( strcmp( argv[i], "aes_xts" ) == 0 )
348 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200349 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
350 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200351 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
352 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200353 else if( strcmp( argv[i], "chachapoly" ) == 0 )
354 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100355 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
356 todo.aes_cmac = 1;
357 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
358 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100359 else if( strcmp( argv[i], "aria" ) == 0 )
360 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200361 else if( strcmp( argv[i], "camellia" ) == 0 )
362 todo.camellia = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300363 else if( strcmp( argv[i], "chacha20" ) == 0 )
364 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300365 else if( strcmp( argv[i], "poly1305" ) == 0 )
366 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200367 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
368 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100369 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
370 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200371 else if( strcmp( argv[i], "rsa" ) == 0 )
372 todo.rsa = 1;
373 else if( strcmp( argv[i], "dhm" ) == 0 )
374 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200375 else if( strcmp( argv[i], "ecdsa" ) == 0 )
376 todo.ecdsa = 1;
377 else if( strcmp( argv[i], "ecdh" ) == 0 )
378 todo.ecdh = 1;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200379#if defined(MBEDTLS_ECP_C)
380 else if( set_ecp_curve( argv[i], single_curve ) )
381 curve_list = single_curve;
382#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200383 else
384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
386 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200387 }
388 }
389 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200394 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000395#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200396 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200397 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200400 if( todo.md5 )
TRodziewicz26371e42021-06-08 16:45:41 +0200401 TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402#endif
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200405 if( todo.ripemd160 )
TRodziewicz26371e42021-06-08 16:45:41 +0200406 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100407#endif
408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200410 if( todo.sha1 )
TRodziewicz26371e42021-06-08 16:45:41 +0200411 TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412#endif
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200415 if( todo.sha256 )
TRodziewicz26371e42021-06-08 16:45:41 +0200416 TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417#endif
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200420 if( todo.sha512 )
TRodziewicz26371e42021-06-08 16:45:41 +0200421 TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000422#endif
423
Simon Butcher549dc3d2016-10-05 14:14:19 +0100424#if defined(MBEDTLS_DES_C)
425#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200426 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_des3_context des3;
429 mbedtls_des3_init( &des3 );
430 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
433 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200434 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200436 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_des_context des;
439 mbedtls_des_init( &des );
440 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200441 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
443 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200444 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100445
446#endif /* MBEDTLS_CIPHER_MODE_CBC */
447#if defined(MBEDTLS_CMAC_C)
448 if( todo.des3_cmac )
449 {
450 unsigned char output[8];
451 const mbedtls_cipher_info_t *cipher_info;
452
453 memset( buf, 0, sizeof( buf ) );
454 memset( tmp, 0, sizeof( tmp ) );
455
456 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
457
458 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100459 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
460 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100461 }
462#endif /* MBEDTLS_CMAC_C */
463#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_AES_C)
466#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200467 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100469 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_aes_context aes;
471 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200472 for( keysize = 128; keysize <= 256; keysize += 64 )
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200476 memset( buf, 0, sizeof( buf ) );
477 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200480 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200485#endif
Aorimn5f778012016-06-09 23:22:58 +0200486#if defined(MBEDTLS_CIPHER_MODE_XTS)
487 if( todo.aes_xts )
488 {
489 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100490 mbedtls_aes_xts_context ctx;
491
492 mbedtls_aes_xts_init( &ctx );
493 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200494 {
495 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
496
497 memset( buf, 0, sizeof( buf ) );
498 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100499 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200500
501 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100502 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
503 tmp, buf, buf ) );
504
505 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200506 }
Aorimn5f778012016-06-09 23:22:58 +0200507 }
508#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200510 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000511 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100512 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200514
515 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200516 for( keysize = 128; keysize <= 256; keysize += 64 )
517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000519
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200520 memset( buf, 0, sizeof( buf ) );
521 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200522 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000523
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200524 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200526 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200529 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000530 }
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200533 if( todo.aes_ccm )
534 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100535 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200537
538 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200539 for( keysize = 128; keysize <= 256; keysize += 64 )
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200542
543 memset( buf, 0, sizeof( buf ) );
544 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200545 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200546
547 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200549 12, NULL, 0, buf, buf, tmp, 16 ) );
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200552 }
553 }
554#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200555#if defined(MBEDTLS_CHACHAPOLY_C)
556 if( todo.chachapoly )
557 {
558 mbedtls_chachapoly_context chachapoly;
559
560 mbedtls_chachapoly_init( &chachapoly );
561 memset( buf, 0, sizeof( buf ) );
562 memset( tmp, 0, sizeof( tmp ) );
563
564 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
565
566 mbedtls_chachapoly_setkey( &chachapoly, tmp );
567
568 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200569 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
570 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200571
572 mbedtls_chachapoly_free( &chachapoly );
573 }
574#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100575#if defined(MBEDTLS_CMAC_C)
576 if( todo.aes_cmac )
577 {
578 unsigned char output[16];
579 const mbedtls_cipher_info_t *cipher_info;
580 mbedtls_cipher_type_t cipher_type;
581 int keysize;
582
583 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
584 keysize <= 256;
585 keysize += 64, cipher_type++ )
586 {
587 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
588
589 memset( buf, 0, sizeof( buf ) );
590 memset( tmp, 0, sizeof( tmp ) );
591
592 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
593
594 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100595 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
596 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100597 }
598
599 memset( buf, 0, sizeof( buf ) );
600 memset( tmp, 0, sizeof( tmp ) );
601 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100602 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
603 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100604 }
605#endif /* MBEDTLS_CMAC_C */
606#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100608#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
609 if( todo.aria )
610 {
611 int keysize;
612 mbedtls_aria_context aria;
613 mbedtls_aria_init( &aria );
614 for( keysize = 128; keysize <= 256; keysize += 64 )
615 {
616 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
617
618 memset( buf, 0, sizeof( buf ) );
619 memset( tmp, 0, sizeof( tmp ) );
620 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
621
622 TIME_AND_TSC( title,
623 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
624 BUFSIZE, tmp, buf, buf ) );
625 }
626 mbedtls_aria_free( &aria );
627 }
628#endif
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200631 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000632 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100633 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 mbedtls_camellia_context camellia;
635 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200636 for( keysize = 128; keysize <= 256; keysize += 64 )
637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000639
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200640 memset( buf, 0, sizeof( buf ) );
641 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000643
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200644 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200646 BUFSIZE, tmp, buf, buf ) );
647 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000649 }
650#endif
651
Daniel King34b822c2016-05-15 17:28:08 -0300652#if defined(MBEDTLS_CHACHA20_C)
653 if ( todo.chacha20 )
654 {
655 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
656 }
657#endif
658
Daniel Kingadc32c02016-05-16 18:25:45 -0300659#if defined(MBEDTLS_POLY1305_C)
660 if ( todo.poly1305 )
661 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200662 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300663 }
664#endif
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200667 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000670
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200671 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200672 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200674 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200675 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskinedb0cb252019-10-28 17:28:46 +0100676 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000677
Gilles Peskinedb0cb252019-10-28 17:28:46 +0100678 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200679 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_exit(1);
681 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200682 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200683 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200685 }
Paul Bakker02faf452011-11-29 11:23:58 +0000686#endif
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100689 if( todo.hmac_drbg )
690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_hmac_drbg_context hmac_drbg;
692 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100693
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200694 mbedtls_hmac_drbg_init( &hmac_drbg );
695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#if defined(MBEDTLS_SHA1_C)
697 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
698 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100699
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200700 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100702 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200703 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100704
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200705 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_exit(1);
707 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
708 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100709 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200710 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100711#endif
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SHA256_C)
714 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
715 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100716
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200717 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100719 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200720 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100721
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200722 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_exit(1);
724 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
725 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100726 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200727 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100728#endif
Ron Eldor278af452018-06-20 18:40:21 +0300729 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100730 }
731#endif
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200734 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100736 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200738 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200741
Ronald Cronc1905a12021-06-05 11:11:14 +0200742 mbedtls_rsa_init( &rsa );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200744
745 TIME_PUBLIC( title, " public",
746 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200748
749 TIME_PUBLIC( title, "private",
750 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200754 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000756#endif
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200759 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100760 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200761 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800762 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100763 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800764 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100765 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800766 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100767 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800768 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100769 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
770
771 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
772 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
773 sizeof( dhm_P_3072 ) };
774
775 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
776 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
777 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200780 size_t olen;
Gilles Peskine487bbf62021-05-27 22:17:07 +0200781 size_t n;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200782 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200785
Hanno Beckerb9539212017-10-04 13:13:34 +0100786 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
787 dhm_P_size[i] ) != 0 ||
788 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
789 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200792 }
793
Gilles Peskine487bbf62021-05-27 22:17:07 +0200794 n = mbedtls_mpi_size( &dhm.P );
795 mbedtls_dhm_make_public( &dhm, (int) n, buf, n, myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
797 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200800 TIME_PUBLIC( title, "handshake",
Gilles Peskine487bbf62021-05-27 22:17:07 +0200801 ret |= mbedtls_dhm_make_public( &dhm, (int) n, buf, n,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200802 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100803 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200806 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100807 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200810 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100811 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100812#endif
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200815 if( todo.ecdsa )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_ecdsa_context ecdsa;
818 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200819 size_t sig_len;
820
821 memset( buf, 0x2A, sizeof( buf ) );
822
Gilles Peskine28f62f62020-07-24 02:06:46 +0200823 for( curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200825 curve_info++ )
826 {
Christoph M. Wintersteiger8cd4fba2019-02-15 12:34:40 +0000827 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +0000828 continue;
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
833 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100834 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200837 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200838 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200839 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200840 tmp, &sig_len, myrand, NULL ) );
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100843 }
844
Gilles Peskine28f62f62020-07-24 02:06:46 +0200845 for( curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100847 curve_info++ )
848 {
Christoph M. Wintersteiger8cd4fba2019-02-15 12:34:40 +0000849 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +0000850 continue;
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200855 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100856 tmp, &sig_len, myrand, NULL ) != 0 )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100859 }
860 ecp_clear_precomputed( &ecdsa.grp );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200864 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200865 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200866 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200869 }
870 }
871#endif
872
Janos Follath52735ef2018-08-15 10:19:16 +0100873#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874 if( todo.ecdh )
875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000878 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
879#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
880 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000881#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000882#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
883 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
884#endif
885 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
886 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200888 size_t olen;
Gilles Peskine28f62f62020-07-24 02:06:46 +0200889 const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
890 montgomery_curve_list;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200891
Gilles Peskine28f62f62020-07-24 02:06:46 +0200892 if( curve_list == (const mbedtls_ecp_curve_info*) &single_curve )
893 {
894 mbedtls_ecp_group grp;
895 mbedtls_ecp_group_init( &grp );
896 if( mbedtls_ecp_group_load( &grp, curve_list->grp_id ) != 0 )
897 mbedtls_exit( 1 );
898 if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
899 selected_montgomery_curve_list = single_curve;
900 else /* empty list */
901 selected_montgomery_curve_list = single_curve + 1;
902 mbedtls_ecp_group_free( &grp );
903 }
904
905 for( curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200907 curve_info++ )
908 {
Gilles Peskinec6c7c492019-02-11 18:41:27 +0100909 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
910 continue;
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200913
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000914 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
915 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
916 myrand, NULL ) );
917 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100918 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200921 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200922 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000923 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200924 myrand, NULL ) );
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000925 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
926 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100928 }
929
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000930 /* Montgomery curves need to be handled separately */
Gilles Peskine28f62f62020-07-24 02:06:46 +0200931 for ( curve_info = selected_montgomery_curve_list;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000932 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
933 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000934 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000935 mbedtls_ecdh_init( &ecdh );
936 mbedtls_mpi_init( &z );
937
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000938 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
939 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000940
941 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
942 curve_info->name );
943 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000944 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
945 myrand, NULL ) );
946 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
947 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000948
949 mbedtls_ecdh_free( &ecdh );
950 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000951 }
952
Gilles Peskine28f62f62020-07-24 02:06:46 +0200953 for( curve_info = curve_list;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100955 curve_info++ )
956 {
Gilles Peskinec6c7c492019-02-11 18:41:27 +0100957 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
958 continue;
959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100961
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000962 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
963 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
964 myrand, NULL ) );
965 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
966 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
967 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100968 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200971 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200972 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000973 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
974 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200976 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000977
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000978 /* Montgomery curves need to be handled separately */
Gilles Peskine28f62f62020-07-24 02:06:46 +0200979 for ( curve_info = selected_montgomery_curve_list;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000980 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
981 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000982 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000983 mbedtls_ecdh_init( &ecdh );
984 mbedtls_mpi_init( &z );
985
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000986 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
987 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
988 myrand, NULL ) );
989 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000990
991 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
992 curve_info->name );
993 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000994 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
995 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000996
997 mbedtls_ecdh_free( &ecdh );
998 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000999 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001000 }
1001#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001002
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001003#if defined(MBEDTLS_ECDH_C)
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001004 if( todo.ecdh )
1005 {
1006 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1007 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001008 const mbedtls_ecp_curve_info *curve_info;
1009 size_t olen;
1010
1011 for( curve_info = curve_list;
1012 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1013 curve_info++ )
1014 {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001015 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1016 continue;
1017
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001018 mbedtls_ecdh_init( &ecdh_srv );
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001019 mbedtls_ecdh_init( &ecdh_cli );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001020
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001021 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name );
Christoph M. Wintersteiger0b931022018-12-06 17:15:12 +00001022 TIME_PUBLIC( title, "full handshake",
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001023 const unsigned char * p_srv = buf_srv;
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001024
Christoph M. Wintersteiger5d536cd2019-02-20 17:26:42 +00001025 CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id ) );
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001026 CHECK_AND_CONTINUE( mbedtls_ecdh_make_params( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001027
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001028 CHECK_AND_CONTINUE( mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen ) );
1029 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001030
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001031 CHECK_AND_CONTINUE( mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen ) );
1032 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001033
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001034 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
Christoph M. Wintersteiger5d536cd2019-02-20 17:26:42 +00001035 mbedtls_ecdh_free( &ecdh_cli );
Christoph M. Wintersteiger12f359f2019-02-26 12:26:04 +00001036
Christoph M. Wintersteiger5d536cd2019-02-20 17:26:42 +00001037 mbedtls_ecdh_free( &ecdh_srv );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001038 );
1039
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001040 }
1041 }
1042#endif
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1047 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001048#endif
1049
Paul Bakkercce9d772011-11-18 14:26:47 +00001050#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 fflush( stdout ); getchar();
1053#endif
1054
k-stachowiak776521a2019-05-23 09:46:47 +02001055 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001056}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#endif /* MBEDTLS_TIMING_C */