blob: 8f89c70c6157b82732be290f2e3b969f058bd9c4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Simon Butcher549dc3d2016-10-05 14:14:19 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
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. Wintersteigercf5603f2019-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");
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000041 return( 0 );
42}
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/md4.h"
51#include "mbedtls/md5.h"
52#include "mbedtls/ripemd160.h"
53#include "mbedtls/sha1.h"
54#include "mbedtls/sha256.h"
55#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/arc4.h"
58#include "mbedtls/des.h"
59#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010060#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/blowfish.h"
62#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030063#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/gcm.h"
65#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020066#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010067#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030068#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010069
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/havege.h"
71#include "mbedtls/ctr_drbg.h"
72#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010073
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/rsa.h"
75#include "mbedtls/dhm.h"
76#include "mbedtls/ecdsa.h"
77#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010078
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000083#endif
84
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000085/*
86 * For heap usage estimates, we need an estimate of the overhead per allocated
87 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
88 * so use that as our baseline.
89 */
90#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
91
92/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020093 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000094 */
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +010095#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000096
Paul Bakker02faf452011-11-29 11:23:58 +000097#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010098#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010099#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000100
Rich Evans85b05ec2015-02-12 11:37:29 +0000101#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200102 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300103 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200104 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300105 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000106 "havege, ctr_drbg, hmac_drbg\n" \
107 "rsa, dhm, ecdsa, ecdh.\n"
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000110#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100111 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000113#else
114#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000116#endif
117
118#define TIME_AND_TSC( TITLE, CODE ) \
119do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200120 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200121 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000122 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200123 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000124 fflush( stdout ); \
125 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200126 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200127 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000128 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200129 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000130 } \
131 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200132 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200133 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000134 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200135 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136 } \
137 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200138 if( ret != 0 ) \
139 { \
140 PRINT_ERROR; \
141 } \
142 else \
143 { \
144 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
145 ii * BUFSIZE / 1024, \
146 ( mbedtls_timing_hardclock() - tsc ) \
147 / ( jj * BUFSIZE ) ); \
148 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000149} while( 0 )
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100152
153#define MEMORY_MEASURE_INIT \
154 size_t max_used, max_blocks, max_bytes; \
155 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200156 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100158
159#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200160 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100161 for( ii = 12 - (title_len); ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100162 max_used -= prv_used; \
163 max_blocks -= prv_blocks; \
164 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100166
167#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000168#define MEMORY_MEASURE_INIT
169#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100170#endif
171
Rich Evans85b05ec2015-02-12 11:37:29 +0000172#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
173do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200174 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000175 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100176 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000177 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200178 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000179 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200180 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000181 \
182 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200183 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000184 { \
185 CODE; \
186 } \
187 \
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000188 if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) \
189 { \
190 mbedtls_printf( "Feature Not Supported. Skipping.\n" ); \
191 ret = 0; \
192 } \
193 else if( ret != 0 ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000194 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100195 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000196 } \
197 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100198 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200199 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100200 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200201 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100202 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000203} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakkera3d195c2011-11-27 21:07:34 +0000205static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000207 size_t use_len;
208 int rnd;
209
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 if( rng_state != NULL )
211 rng_state = NULL;
212
Paul Bakkera3d195c2011-11-27 21:07:34 +0000213 while( len > 0 )
214 {
215 use_len = len;
216 if( use_len > sizeof(int) )
217 use_len = sizeof(int);
218
219 rnd = rand();
220 memcpy( output, &rnd, use_len );
221 output += use_len;
222 len -= use_len;
223 }
224
225 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226}
227
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000228#define CHECK_AND_CONTINUE( R ) \
229 { \
Gilles Peskine1fdf2c22019-02-11 18:23:42 +0100230 int CHECK_AND_CONTINUE_ret = ( R ); \
231 if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000232 mbedtls_printf( "Feature not supported. Skipping.\n" ); \
233 continue; \
234 } \
Gilles Peskine1fdf2c22019-02-11 18:23:42 +0100235 else if( CHECK_AND_CONTINUE_ret != 0 ) { \
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000236 mbedtls_exit( 1 ); \
237 } \
238 }
Christoph M. Wintersteiger3c449622018-12-14 11:54:59 +0000239
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100240/*
241 * Clear some memory that was used to prepare the context
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_ECP_C)
244void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100245{
246 if( grp->T != NULL )
247 {
248 size_t i;
249 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ecp_point_free( &grp->T[i] );
251 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100252 }
253 grp->T = NULL;
254 grp->T_size = 0;
255}
256#else
257#define ecp_clear_precomputed( g )
258#endif
259
Paul Bakker5121ce52009-01-03 21:22:43 +0000260unsigned char buf[BUFSIZE];
261
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200262typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200263 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100264 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200265 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200266 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200267 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300268 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100269 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200270 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200271} todo_list;
272
Simon Butcher63cb97e2018-12-06 17:43:31 +0000273
Paul Bakkercce9d772011-11-18 14:26:47 +0000274int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000275{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100276 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200277 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200278 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200279 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200281 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000282#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000283
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000284 if( argc <= 1 )
285 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200286 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000287 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288 else
289 {
290 memset( &todo, 0, sizeof( todo ) );
291
292 for( i = 1; i < argc; i++ )
293 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200294 if( strcmp( argv[i], "md4" ) == 0 )
295 todo.md4 = 1;
296 else if( strcmp( argv[i], "md5" ) == 0 )
297 todo.md5 = 1;
298 else if( strcmp( argv[i], "ripemd160" ) == 0 )
299 todo.ripemd160 = 1;
300 else if( strcmp( argv[i], "sha1" ) == 0 )
301 todo.sha1 = 1;
302 else if( strcmp( argv[i], "sha256" ) == 0 )
303 todo.sha256 = 1;
304 else if( strcmp( argv[i], "sha512" ) == 0 )
305 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200306 else if( strcmp( argv[i], "arc4" ) == 0 )
307 todo.arc4 = 1;
308 else if( strcmp( argv[i], "des3" ) == 0 )
309 todo.des3 = 1;
310 else if( strcmp( argv[i], "des" ) == 0 )
311 todo.des = 1;
312 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
313 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200314 else if( strcmp( argv[i], "aes_xts" ) == 0 )
315 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200316 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
317 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200318 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
319 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200320 else if( strcmp( argv[i], "chachapoly" ) == 0 )
321 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100322 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
323 todo.aes_cmac = 1;
324 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
325 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100326 else if( strcmp( argv[i], "aria" ) == 0 )
327 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200328 else if( strcmp( argv[i], "camellia" ) == 0 )
329 todo.camellia = 1;
330 else if( strcmp( argv[i], "blowfish" ) == 0 )
331 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300332 else if( strcmp( argv[i], "chacha20" ) == 0 )
333 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300334 else if( strcmp( argv[i], "poly1305" ) == 0 )
335 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200336 else if( strcmp( argv[i], "havege" ) == 0 )
337 todo.havege = 1;
338 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
339 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100340 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
341 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 else if( strcmp( argv[i], "rsa" ) == 0 )
343 todo.rsa = 1;
344 else if( strcmp( argv[i], "dhm" ) == 0 )
345 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200346 else if( strcmp( argv[i], "ecdsa" ) == 0 )
347 todo.ecdsa = 1;
348 else if( strcmp( argv[i], "ecdh" ) == 0 )
349 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200350 else
351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
353 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200354 }
355 }
356 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200361 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000362#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200363 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200364 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200367 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100368 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369#endif
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200372 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374#endif
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200377 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100378 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100379#endif
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200382 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100383 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384#endif
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200387 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100388 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389#endif
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200392 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000394#endif
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200397 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_arc4_context arc4;
400 mbedtls_arc4_init( &arc4 );
401 mbedtls_arc4_setup( &arc4, tmp, 32 );
402 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
403 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200404 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000405#endif
406
Simon Butcher549dc3d2016-10-05 14:14:19 +0100407#if defined(MBEDTLS_DES_C)
408#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200409 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_des3_context des3;
412 mbedtls_des3_init( &des3 );
413 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
416 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200417 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200419 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_des_context des;
422 mbedtls_des_init( &des );
423 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200424 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
426 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200427 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100428
429#endif /* MBEDTLS_CIPHER_MODE_CBC */
430#if defined(MBEDTLS_CMAC_C)
431 if( todo.des3_cmac )
432 {
433 unsigned char output[8];
434 const mbedtls_cipher_info_t *cipher_info;
435
436 memset( buf, 0, sizeof( buf ) );
437 memset( tmp, 0, sizeof( tmp ) );
438
439 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
440
441 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100442 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
443 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100444 }
445#endif /* MBEDTLS_CMAC_C */
446#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_AES_C)
449#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200450 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100452 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_aes_context aes;
454 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200455 for( keysize = 128; keysize <= 256; keysize += 64 )
456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200459 memset( buf, 0, sizeof( buf ) );
460 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200463 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200465 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200468#endif
Aorimn5f778012016-06-09 23:22:58 +0200469#if defined(MBEDTLS_CIPHER_MODE_XTS)
470 if( todo.aes_xts )
471 {
472 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100473 mbedtls_aes_xts_context ctx;
474
475 mbedtls_aes_xts_init( &ctx );
476 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200477 {
478 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
479
480 memset( buf, 0, sizeof( buf ) );
481 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100482 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200483
484 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100485 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
486 tmp, buf, buf ) );
487
488 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200489 }
Aorimn5f778012016-06-09 23:22:58 +0200490 }
491#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200493 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000494 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100495 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200497
498 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499 for( keysize = 128; keysize <= 256; keysize += 64 )
500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000502
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503 memset( buf, 0, sizeof( buf ) );
504 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200505 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000506
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200507 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200509 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200512 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000513 }
514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200516 if( todo.aes_ccm )
517 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100518 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200520
521 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200522 for( keysize = 128; keysize <= 256; keysize += 64 )
523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200525
526 memset( buf, 0, sizeof( buf ) );
527 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200528 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200529
530 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200532 12, NULL, 0, buf, buf, tmp, 16 ) );
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200535 }
536 }
537#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200538#if defined(MBEDTLS_CHACHAPOLY_C)
539 if( todo.chachapoly )
540 {
541 mbedtls_chachapoly_context chachapoly;
542
543 mbedtls_chachapoly_init( &chachapoly );
544 memset( buf, 0, sizeof( buf ) );
545 memset( tmp, 0, sizeof( tmp ) );
546
547 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
548
549 mbedtls_chachapoly_setkey( &chachapoly, tmp );
550
551 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200552 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
553 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200554
555 mbedtls_chachapoly_free( &chachapoly );
556 }
557#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100558#if defined(MBEDTLS_CMAC_C)
559 if( todo.aes_cmac )
560 {
561 unsigned char output[16];
562 const mbedtls_cipher_info_t *cipher_info;
563 mbedtls_cipher_type_t cipher_type;
564 int keysize;
565
566 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
567 keysize <= 256;
568 keysize += 64, cipher_type++ )
569 {
570 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
571
572 memset( buf, 0, sizeof( buf ) );
573 memset( tmp, 0, sizeof( tmp ) );
574
575 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
576
577 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100578 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
579 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100580 }
581
582 memset( buf, 0, sizeof( buf ) );
583 memset( tmp, 0, sizeof( tmp ) );
584 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100585 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
586 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100587 }
588#endif /* MBEDTLS_CMAC_C */
589#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100591#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
592 if( todo.aria )
593 {
594 int keysize;
595 mbedtls_aria_context aria;
596 mbedtls_aria_init( &aria );
597 for( keysize = 128; keysize <= 256; keysize += 64 )
598 {
599 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
600
601 memset( buf, 0, sizeof( buf ) );
602 memset( tmp, 0, sizeof( tmp ) );
603 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
604
605 TIME_AND_TSC( title,
606 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
607 BUFSIZE, tmp, buf, buf ) );
608 }
609 mbedtls_aria_free( &aria );
610 }
611#endif
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200614 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000615 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100616 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_camellia_context camellia;
618 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200619 for( keysize = 128; keysize <= 256; keysize += 64 )
620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000622
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200623 memset( buf, 0, sizeof( buf ) );
624 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000626
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200627 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200629 BUFSIZE, tmp, buf, buf ) );
630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000632 }
633#endif
634
Daniel King34b822c2016-05-15 17:28:08 -0300635#if defined(MBEDTLS_CHACHA20_C)
636 if ( todo.chacha20 )
637 {
638 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
639 }
640#endif
641
Daniel Kingadc32c02016-05-16 18:25:45 -0300642#if defined(MBEDTLS_POLY1305_C)
643 if ( todo.poly1305 )
644 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200645 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300646 }
647#endif
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200650 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000651 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100652 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_blowfish_context blowfish;
654 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200655
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200656 for( keysize = 128; keysize <= 256; keysize += 64 )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000659
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200660 memset( buf, 0, sizeof( buf ) );
661 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000663
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200664 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200666 tmp, buf, buf ) );
667 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000670 }
671#endif
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200674 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_havege_state hs;
677 mbedtls_havege_init( &hs );
678 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
679 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200680 }
Paul Bakker02faf452011-11-29 11:23:58 +0000681#endif
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200684 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000687
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200688 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200689 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200691 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200692 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskinef0ebbfb2019-10-28 17:28:46 +0100693 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000694
Gilles Peskinef0ebbfb2019-10-28 17:28:46 +0100695 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200696 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_exit(1);
698 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200699 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200700 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200702 }
Paul Bakker02faf452011-11-29 11:23:58 +0000703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100706 if( todo.hmac_drbg )
707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_hmac_drbg_context hmac_drbg;
709 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100710
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200711 mbedtls_hmac_drbg_init( &hmac_drbg );
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SHA1_C)
714 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == 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-1 (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-1 (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
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_SHA256_C)
731 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
732 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100733
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200734 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100736 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200737 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100738
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200739 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_exit(1);
741 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
742 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100743 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200744 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100745#endif
Ron Eldor278af452018-06-20 18:40:21 +0300746 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100747 }
748#endif
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200751 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000752 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100753 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200755 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
760 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200761
762 TIME_PUBLIC( title, " public",
763 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200765
766 TIME_PUBLIC( title, "private",
767 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200771 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000773#endif
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200776 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100777 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200778 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800779 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100780 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800781 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100782 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800783 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100784 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800785 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100786 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
787
788 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
789 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
790 sizeof( dhm_P_3072 ) };
791
792 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
793 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
794 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200797 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200798 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200801
Hanno Beckerb9539212017-10-04 13:13:34 +0100802 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
803 dhm_P_size[i] ) != 0 ||
804 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
805 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200808 }
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 dhm.len = mbedtls_mpi_size( &dhm.P );
811 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
812 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
813 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200816 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200818 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100819 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200822 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100823 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200826 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100827 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100828#endif
829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200831 if( todo.ecdsa )
832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_ecdsa_context ecdsa;
834 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200835 size_t sig_len;
836
837 memset( buf, 0x2A, sizeof( buf ) );
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 for( curve_info = mbedtls_ecp_curve_list();
840 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200841 curve_info++ )
842 {
Christoph M. Wintersteiger41fb2c02019-02-15 12:34:40 +0000843 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000844 continue;
845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
849 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100850 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200853 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200854 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200855 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200856 tmp, &sig_len, myrand, NULL ) );
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100859 }
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 for( curve_info = mbedtls_ecp_curve_list();
862 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863 curve_info++ )
864 {
Christoph M. Wintersteiger41fb2c02019-02-15 12:34:40 +0000865 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000866 continue;
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200871 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100872 tmp, &sig_len, myrand, NULL ) != 0 )
873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100875 }
876 ecp_clear_precomputed( &ecdsa.grp );
877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100879 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200880 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200881 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200882 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200885 }
886 }
887#endif
888
Janos Follath52735ef2018-08-15 10:19:16 +0100889#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200890 if( todo.ecdh )
891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000894 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
895#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
896 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000897#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000898#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
899 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
900#endif
901 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
902 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200904 size_t olen;
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 for( curve_info = mbedtls_ecp_curve_list();
907 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200908 curve_info++ )
909 {
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100910 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
911 continue;
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200914
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000915 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
916 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
917 myrand, NULL ) );
918 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100919 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200922 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200923 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000924 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200925 myrand, NULL ) );
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000926 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
927 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100929 }
930
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000931 /* Montgomery curves need to be handled separately */
932 for ( curve_info = montgomery_curve_list;
933 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
934 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000935 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000936 mbedtls_ecdh_init( &ecdh );
937 mbedtls_mpi_init( &z );
938
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000939 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
940 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000941
942 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
943 curve_info->name );
944 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000945 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
946 myrand, NULL ) );
947 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
948 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000949
950 mbedtls_ecdh_free( &ecdh );
951 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000952 }
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 for( curve_info = mbedtls_ecp_curve_list();
955 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100956 curve_info++ )
957 {
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100958 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
959 continue;
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100962
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000963 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
964 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
965 myrand, NULL ) );
966 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
967 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
968 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100969 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200972 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200973 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000974 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
975 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200977 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000978
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000979 /* Montgomery curves need to be handled separately */
980 for ( curve_info = montgomery_curve_list;
981 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
982 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000983 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000984 mbedtls_ecdh_init( &ecdh );
985 mbedtls_mpi_init( &z );
986
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000987 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
988 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
989 myrand, NULL ) );
990 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000991
992 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
993 curve_info->name );
994 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000995 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
996 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000997
998 mbedtls_ecdh_free( &ecdh );
999 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001000 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001001 }
1002#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001003
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +00001004#if defined(MBEDTLS_ECDH_C)
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001005 if( todo.ecdh )
1006 {
1007 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1008 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
1009 const mbedtls_ecp_curve_info * curve_list = mbedtls_ecp_curve_list();
1010 const mbedtls_ecp_curve_info *curve_info;
1011 size_t olen;
1012
1013 for( curve_info = curve_list;
1014 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1015 curve_info++ )
1016 {
Gilles Peskine20b3ef32019-02-11 18:41:27 +01001017 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1018 continue;
1019
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001020 mbedtls_ecdh_init( &ecdh_srv );
Christoph M. Wintersteiger26b98e12019-02-26 12:26:04 +00001021 mbedtls_ecdh_init( &ecdh_cli );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001022
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001023 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name );
Christoph M. Wintersteiger65bab972018-12-06 17:15:12 +00001024 TIME_PUBLIC( title, "full handshake",
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001025 const unsigned char * p_srv = buf_srv;
Christoph M. Wintersteiger26b98e12019-02-26 12:26:04 +00001026
Christoph M. Wintersteiger3b587002019-02-20 17:26:42 +00001027 CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id ) );
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001028 CHECK_AND_CONTINUE( mbedtls_ecdh_make_params( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001029
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001030 CHECK_AND_CONTINUE( mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen ) );
1031 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001032
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001033 CHECK_AND_CONTINUE( mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen ) );
1034 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001035
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001036 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
Christoph M. Wintersteiger3b587002019-02-20 17:26:42 +00001037 mbedtls_ecdh_free( &ecdh_cli );
Christoph M. Wintersteiger26b98e12019-02-26 12:26:04 +00001038
Christoph M. Wintersteiger3b587002019-02-20 17:26:42 +00001039 mbedtls_ecdh_free( &ecdh_srv );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001040 );
1041
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001042 }
1043 }
1044#endif
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1049 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001050#endif
1051
Paul Bakkercce9d772011-11-18 14:26:47 +00001052#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 fflush( stdout ); getchar();
1055#endif
1056
1057 return( 0 );
1058}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#endif /* MBEDTLS_TIMING_C */