blob: fc84f5756e0c62b15ce053eddaad439d819bffae [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
34#define mbedtls_snprintf snprintf
35#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010036#define mbedtls_exit exit
37#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
38#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000042int main( void )
43{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000045 return( 0 );
46}
47#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010048
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000049#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020050#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010051
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/md4.h"
55#include "mbedtls/md5.h"
56#include "mbedtls/ripemd160.h"
57#include "mbedtls/sha1.h"
58#include "mbedtls/sha256.h"
59#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/arc4.h"
62#include "mbedtls/des.h"
63#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010064#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/blowfish.h"
66#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030067#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/gcm.h"
69#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020070#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010071#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030072#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010073
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/havege.h"
75#include "mbedtls/ctr_drbg.h"
76#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010077
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/rsa.h"
79#include "mbedtls/dhm.h"
80#include "mbedtls/ecdsa.h"
81#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010082
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000087#endif
88
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000089/*
90 * For heap usage estimates, we need an estimate of the overhead per allocated
91 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
92 * so use that as our baseline.
93 */
94#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
95
96/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020097 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000098 */
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +010099#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000100
Paul Bakker02faf452011-11-29 11:23:58 +0000101#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100102#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100103#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000104
Rich Evans85b05ec2015-02-12 11:37:29 +0000105#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200106 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300107 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200108 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300109 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000110 "havege, ctr_drbg, hmac_drbg\n" \
111 "rsa, dhm, ecdsa, ecdh.\n"
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000114#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100115 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000117#else
118#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000120#endif
121
122#define TIME_AND_TSC( TITLE, CODE ) \
123do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200124 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200125 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000126 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200127 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000128 fflush( stdout ); \
129 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200130 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200131 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000132 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200133 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000134 } \
135 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200136 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200137 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000138 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200139 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000140 } \
141 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200142 if( ret != 0 ) \
143 { \
144 PRINT_ERROR; \
145 } \
146 else \
147 { \
148 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
149 ii * BUFSIZE / 1024, \
150 ( mbedtls_timing_hardclock() - tsc ) \
151 / ( jj * BUFSIZE ) ); \
152 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000153} while( 0 )
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100156
157#define MEMORY_MEASURE_INIT \
158 size_t max_used, max_blocks, max_bytes; \
159 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200160 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100162
163#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200164 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100165 for( ii = 12 - (title_len); ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100166 max_used -= prv_used; \
167 max_blocks -= prv_blocks; \
168 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100170
171#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000172#define MEMORY_MEASURE_INIT
173#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100174#endif
175
Rich Evans85b05ec2015-02-12 11:37:29 +0000176#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
177do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200178 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000179 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100180 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000181 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200182 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000183 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200184 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000185 \
186 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200187 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000188 { \
189 CODE; \
190 } \
191 \
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000192 if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) \
193 { \
194 mbedtls_printf( "Feature Not Supported. Skipping.\n" ); \
195 ret = 0; \
196 } \
197 else if( ret != 0 ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000198 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100199 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000200 } \
201 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100202 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200203 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100204 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200205 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100206 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000207} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
Paul Bakkera3d195c2011-11-27 21:07:34 +0000209static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000211 size_t use_len;
212 int rnd;
213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 if( rng_state != NULL )
215 rng_state = NULL;
216
Paul Bakkera3d195c2011-11-27 21:07:34 +0000217 while( len > 0 )
218 {
219 use_len = len;
220 if( use_len > sizeof(int) )
221 use_len = sizeof(int);
222
223 rnd = rand();
224 memcpy( output, &rnd, use_len );
225 output += use_len;
226 len -= use_len;
227 }
228
229 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230}
231
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000232#define CHECK_AND_CONTINUE( R ) \
233 { \
Gilles Peskine1fdf2c22019-02-11 18:23:42 +0100234 int CHECK_AND_CONTINUE_ret = ( R ); \
235 if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000236 mbedtls_printf( "Feature not supported. Skipping.\n" ); \
237 continue; \
238 } \
Gilles Peskine1fdf2c22019-02-11 18:23:42 +0100239 else if( CHECK_AND_CONTINUE_ret != 0 ) { \
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000240 mbedtls_exit( 1 ); \
241 } \
242 }
Christoph M. Wintersteiger3c449622018-12-14 11:54:59 +0000243
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100244/*
245 * Clear some memory that was used to prepare the context
246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_ECP_C)
248void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100249{
250 if( grp->T != NULL )
251 {
252 size_t i;
253 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_ecp_point_free( &grp->T[i] );
255 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100256 }
257 grp->T = NULL;
258 grp->T_size = 0;
259}
260#else
261#define ecp_clear_precomputed( g )
262#endif
263
Paul Bakker5121ce52009-01-03 21:22:43 +0000264unsigned char buf[BUFSIZE];
265
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200266typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200267 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100268 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200269 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200270 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200271 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300272 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100273 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200274 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200275} todo_list;
276
Simon Butcher63cb97e2018-12-06 17:43:31 +0000277
Paul Bakkercce9d772011-11-18 14:26:47 +0000278int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000279{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100280 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200281 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200282 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200283 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200285 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000286#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000287
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000288 if( argc <= 1 )
289 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200290 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000291 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200292 else
293 {
294 memset( &todo, 0, sizeof( todo ) );
295
296 for( i = 1; i < argc; i++ )
297 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200298 if( strcmp( argv[i], "md4" ) == 0 )
299 todo.md4 = 1;
300 else if( strcmp( argv[i], "md5" ) == 0 )
301 todo.md5 = 1;
302 else if( strcmp( argv[i], "ripemd160" ) == 0 )
303 todo.ripemd160 = 1;
304 else if( strcmp( argv[i], "sha1" ) == 0 )
305 todo.sha1 = 1;
306 else if( strcmp( argv[i], "sha256" ) == 0 )
307 todo.sha256 = 1;
308 else if( strcmp( argv[i], "sha512" ) == 0 )
309 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200310 else if( strcmp( argv[i], "arc4" ) == 0 )
311 todo.arc4 = 1;
312 else if( strcmp( argv[i], "des3" ) == 0 )
313 todo.des3 = 1;
314 else if( strcmp( argv[i], "des" ) == 0 )
315 todo.des = 1;
316 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
317 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200318 else if( strcmp( argv[i], "aes_xts" ) == 0 )
319 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200320 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
321 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200322 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
323 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200324 else if( strcmp( argv[i], "chachapoly" ) == 0 )
325 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100326 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
327 todo.aes_cmac = 1;
328 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
329 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100330 else if( strcmp( argv[i], "aria" ) == 0 )
331 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200332 else if( strcmp( argv[i], "camellia" ) == 0 )
333 todo.camellia = 1;
334 else if( strcmp( argv[i], "blowfish" ) == 0 )
335 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300336 else if( strcmp( argv[i], "chacha20" ) == 0 )
337 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300338 else if( strcmp( argv[i], "poly1305" ) == 0 )
339 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200340 else if( strcmp( argv[i], "havege" ) == 0 )
341 todo.havege = 1;
342 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
343 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100344 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
345 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200346 else if( strcmp( argv[i], "rsa" ) == 0 )
347 todo.rsa = 1;
348 else if( strcmp( argv[i], "dhm" ) == 0 )
349 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200350 else if( strcmp( argv[i], "ecdsa" ) == 0 )
351 todo.ecdsa = 1;
352 else if( strcmp( argv[i], "ecdh" ) == 0 )
353 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200354 else
355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
357 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200358 }
359 }
360 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200365 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000366#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200367 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200368 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200371 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100372 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373#endif
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200376 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378#endif
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200381 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100382 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100383#endif
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200386 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388#endif
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200391 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100392 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393#endif
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200396 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000398#endif
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200401 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_arc4_context arc4;
404 mbedtls_arc4_init( &arc4 );
405 mbedtls_arc4_setup( &arc4, tmp, 32 );
406 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
407 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200408 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000409#endif
410
Simon Butcher549dc3d2016-10-05 14:14:19 +0100411#if defined(MBEDTLS_DES_C)
412#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200413 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_des3_context des3;
416 mbedtls_des3_init( &des3 );
417 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200418 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
420 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200421 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200423 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_des_context des;
426 mbedtls_des_init( &des );
427 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200428 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
430 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100432
433#endif /* MBEDTLS_CIPHER_MODE_CBC */
434#if defined(MBEDTLS_CMAC_C)
435 if( todo.des3_cmac )
436 {
437 unsigned char output[8];
438 const mbedtls_cipher_info_t *cipher_info;
439
440 memset( buf, 0, sizeof( buf ) );
441 memset( tmp, 0, sizeof( tmp ) );
442
443 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
444
445 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100446 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
447 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100448 }
449#endif /* MBEDTLS_CMAC_C */
450#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452#if defined(MBEDTLS_AES_C)
453#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200454 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100456 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_aes_context aes;
458 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200459 for( keysize = 128; keysize <= 256; keysize += 64 )
460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200463 memset( buf, 0, sizeof( buf ) );
464 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200467 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200469 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200472#endif
Aorimn5f778012016-06-09 23:22:58 +0200473#if defined(MBEDTLS_CIPHER_MODE_XTS)
474 if( todo.aes_xts )
475 {
476 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100477 mbedtls_aes_xts_context ctx;
478
479 mbedtls_aes_xts_init( &ctx );
480 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200481 {
482 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
483
484 memset( buf, 0, sizeof( buf ) );
485 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100486 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200487
488 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100489 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
490 tmp, buf, buf ) );
491
492 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200493 }
Aorimn5f778012016-06-09 23:22:58 +0200494 }
495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200497 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000498 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100499 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200501
502 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503 for( keysize = 128; keysize <= 256; keysize += 64 )
504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000506
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200507 memset( buf, 0, sizeof( buf ) );
508 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200509 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000510
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200511 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200513 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200516 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000517 }
518#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200520 if( todo.aes_ccm )
521 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100522 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200524
525 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200526 for( keysize = 128; keysize <= 256; keysize += 64 )
527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200529
530 memset( buf, 0, sizeof( buf ) );
531 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200532 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200533
534 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200536 12, NULL, 0, buf, buf, tmp, 16 ) );
537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200539 }
540 }
541#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200542#if defined(MBEDTLS_CHACHAPOLY_C)
543 if( todo.chachapoly )
544 {
545 mbedtls_chachapoly_context chachapoly;
546
547 mbedtls_chachapoly_init( &chachapoly );
548 memset( buf, 0, sizeof( buf ) );
549 memset( tmp, 0, sizeof( tmp ) );
550
551 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
552
553 mbedtls_chachapoly_setkey( &chachapoly, tmp );
554
555 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200556 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
557 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200558
559 mbedtls_chachapoly_free( &chachapoly );
560 }
561#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100562#if defined(MBEDTLS_CMAC_C)
563 if( todo.aes_cmac )
564 {
565 unsigned char output[16];
566 const mbedtls_cipher_info_t *cipher_info;
567 mbedtls_cipher_type_t cipher_type;
568 int keysize;
569
570 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
571 keysize <= 256;
572 keysize += 64, cipher_type++ )
573 {
574 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
575
576 memset( buf, 0, sizeof( buf ) );
577 memset( tmp, 0, sizeof( tmp ) );
578
579 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
580
581 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100582 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
583 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100584 }
585
586 memset( buf, 0, sizeof( buf ) );
587 memset( tmp, 0, sizeof( tmp ) );
588 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100589 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
590 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100591 }
592#endif /* MBEDTLS_CMAC_C */
593#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100595#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
596 if( todo.aria )
597 {
598 int keysize;
599 mbedtls_aria_context aria;
600 mbedtls_aria_init( &aria );
601 for( keysize = 128; keysize <= 256; keysize += 64 )
602 {
603 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
604
605 memset( buf, 0, sizeof( buf ) );
606 memset( tmp, 0, sizeof( tmp ) );
607 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
608
609 TIME_AND_TSC( title,
610 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
611 BUFSIZE, tmp, buf, buf ) );
612 }
613 mbedtls_aria_free( &aria );
614 }
615#endif
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200618 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000619 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100620 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_camellia_context camellia;
622 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200623 for( keysize = 128; keysize <= 256; keysize += 64 )
624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000626
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200627 memset( buf, 0, sizeof( buf ) );
628 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000630
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200631 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200633 BUFSIZE, tmp, buf, buf ) );
634 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000636 }
637#endif
638
Daniel King34b822c2016-05-15 17:28:08 -0300639#if defined(MBEDTLS_CHACHA20_C)
640 if ( todo.chacha20 )
641 {
642 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
643 }
644#endif
645
Daniel Kingadc32c02016-05-16 18:25:45 -0300646#if defined(MBEDTLS_POLY1305_C)
647 if ( todo.poly1305 )
648 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200649 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300650 }
651#endif
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200654 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000655 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100656 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_blowfish_context blowfish;
658 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200659
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200660 for( keysize = 128; keysize <= 256; keysize += 64 )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000663
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200664 memset( buf, 0, sizeof( buf ) );
665 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000667
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200668 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200670 tmp, buf, buf ) );
671 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000674 }
675#endif
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200678 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_havege_state hs;
681 mbedtls_havege_init( &hs );
682 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
683 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200684 }
Paul Bakker02faf452011-11-29 11:23:58 +0000685#endif
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200688 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000691
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200692 mbedtls_ctr_drbg_init( &ctr_drbg );
693
694 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200696 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200697 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Paul Bakker02faf452011-11-29 11:23:58 +0000698
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200699 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_exit(1);
701 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200702 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200703 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200705 }
Paul Bakker02faf452011-11-29 11:23:58 +0000706#endif
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100709 if( todo.hmac_drbg )
710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_hmac_drbg_context hmac_drbg;
712 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100713
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200714 mbedtls_hmac_drbg_init( &hmac_drbg );
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SHA1_C)
717 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
718 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100719
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200720 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100722 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200723 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100724
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200725 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_exit(1);
727 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
728 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100729 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200730 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100731#endif
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_SHA256_C)
734 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
735 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100736
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200737 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100739 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200740 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100741
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200742 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_exit(1);
744 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
745 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100746 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200747 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100748#endif
Ron Eldor278af452018-06-20 18:40:21 +0300749 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100750 }
751#endif
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200754 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000755 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100756 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200758 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
763 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200764
765 TIME_PUBLIC( title, " public",
766 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200768
769 TIME_PUBLIC( title, "private",
770 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200774 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000776#endif
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200779 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100780 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200781 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800782 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100783 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800784 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100785 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800786 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100787 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800788 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100789 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
790
791 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
792 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
793 sizeof( dhm_P_3072 ) };
794
795 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
796 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
797 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200800 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200801 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200804
Hanno Beckerb9539212017-10-04 13:13:34 +0100805 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
806 dhm_P_size[i] ) != 0 ||
807 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
808 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200811 }
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 dhm.len = mbedtls_mpi_size( &dhm.P );
814 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
815 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
816 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200819 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200821 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100822 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200825 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100826 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200829 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100830 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100831#endif
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200834 if( todo.ecdsa )
835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_ecdsa_context ecdsa;
837 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200838 size_t sig_len;
839
840 memset( buf, 0x2A, sizeof( buf ) );
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 for( curve_info = mbedtls_ecp_curve_list();
843 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200844 curve_info++ )
845 {
Christoph M. Wintersteiger41fb2c02019-02-15 12:34:40 +0000846 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000847 continue;
848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
852 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100853 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200856 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200857 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200858 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200859 tmp, &sig_len, myrand, NULL ) );
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100862 }
863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 for( curve_info = mbedtls_ecp_curve_list();
865 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100866 curve_info++ )
867 {
Christoph M. Wintersteiger41fb2c02019-02-15 12:34:40 +0000868 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000869 continue;
870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200874 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100875 tmp, &sig_len, myrand, NULL ) != 0 )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100878 }
879 ecp_clear_precomputed( &ecdsa.grp );
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100882 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200883 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200884 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200885 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200888 }
889 }
890#endif
891
Janos Follath52735ef2018-08-15 10:19:16 +0100892#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200893 if( todo.ecdh )
894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000897 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
898#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
899 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000900#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000901#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
902 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
903#endif
904 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
905 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200907 size_t olen;
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 for( curve_info = mbedtls_ecp_curve_list();
910 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200911 curve_info++ )
912 {
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100913 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
914 continue;
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200917
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000918 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
919 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
920 myrand, NULL ) );
921 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100922 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200925 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200926 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000927 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200928 myrand, NULL ) );
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000929 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
930 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100932 }
933
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000934 /* Montgomery curves need to be handled separately */
935 for ( curve_info = montgomery_curve_list;
936 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
937 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000938 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000939 mbedtls_ecdh_init( &ecdh );
940 mbedtls_mpi_init( &z );
941
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000942 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
943 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000944
945 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
946 curve_info->name );
947 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000948 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
949 myrand, NULL ) );
950 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
951 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000952
953 mbedtls_ecdh_free( &ecdh );
954 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000955 }
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 for( curve_info = mbedtls_ecp_curve_list();
958 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100959 curve_info++ )
960 {
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100961 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
962 continue;
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100965
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000966 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
967 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
968 myrand, NULL ) );
969 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
970 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
971 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100972 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200975 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200976 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000977 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
978 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200980 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000981
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000982 /* Montgomery curves need to be handled separately */
983 for ( curve_info = montgomery_curve_list;
984 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
985 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000986 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000987 mbedtls_ecdh_init( &ecdh );
988 mbedtls_mpi_init( &z );
989
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000990 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
991 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
992 myrand, NULL ) );
993 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000994
995 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
996 curve_info->name );
997 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +0000998 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
999 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001000
1001 mbedtls_ecdh_free( &ecdh );
1002 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001003 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001004 }
1005#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001006
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +00001007#if defined(MBEDTLS_ECDH_C)
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001008 if( todo.ecdh )
1009 {
1010 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1011 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
1012 const mbedtls_ecp_curve_info * curve_list = mbedtls_ecp_curve_list();
1013 const mbedtls_ecp_curve_info *curve_info;
1014 size_t olen;
1015
1016 for( curve_info = curve_list;
1017 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1018 curve_info++ )
1019 {
Gilles Peskine20b3ef32019-02-11 18:41:27 +01001020 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1021 continue;
1022
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001023 mbedtls_ecdh_init( &ecdh_srv );
Christoph M. Wintersteiger3b587002019-02-20 17:26:42 +00001024 mbedtls_ecdh_init( &ecdh_cli );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001025
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001026 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name );
Christoph M. Wintersteiger65bab972018-12-06 17:15:12 +00001027 TIME_PUBLIC( title, "full handshake",
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001028 const unsigned char * p_srv = buf_srv;
Christoph M. Wintersteiger3b587002019-02-20 17:26:42 +00001029
1030 CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id ) );
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001031 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 +01001032
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001033 CHECK_AND_CONTINUE( mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen ) );
1034 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 +01001035
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001036 CHECK_AND_CONTINUE( mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen ) );
1037 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 +01001038
Christoph M. Wintersteiger95972382019-02-06 18:06:15 +00001039 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 +00001040 mbedtls_ecdh_free( &ecdh_cli );
1041
1042 mbedtls_ecdh_free( &ecdh_srv );
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001043 );
1044
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001045 }
1046 }
1047#endif
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1052 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001053#endif
1054
Paul Bakkercce9d772011-11-18 14:26:47 +00001055#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 fflush( stdout ); getchar();
1058#endif
1059
1060 return( 0 );
1061}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_TIMING_C */