blob: ff1f4d1ba9acebb7c190ed577150efdcc01cc869 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.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
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000039int main( void )
40{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000042 return( 0 );
43}
44#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010045
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000046#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020047#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010048
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md4.h"
52#include "mbedtls/md5.h"
53#include "mbedtls/ripemd160.h"
54#include "mbedtls/sha1.h"
55#include "mbedtls/sha256.h"
56#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/arc4.h"
59#include "mbedtls/des.h"
60#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010061#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030064#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/gcm.h"
66#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020067#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010068#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030069#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010070
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/havege.h"
72#include "mbedtls/ctr_drbg.h"
73#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/rsa.h"
76#include "mbedtls/dhm.h"
77#include "mbedtls/ecdsa.h"
78#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010079
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000084#endif
85
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000086/*
87 * For heap usage estimates, we need an estimate of the overhead per allocated
88 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
89 * so use that as our baseline.
90 */
91#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
92
93/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020094 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000095 */
96#define HEAP_SIZE (1u << 16) // 64k
97
Paul Bakker02faf452011-11-29 11:23:58 +000098#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010099#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100100#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000101
Rich Evans85b05ec2015-02-12 11:37:29 +0000102#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200103 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300104 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200105 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300106 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000107 "havege, ctr_drbg, hmac_drbg\n" \
108 "rsa, dhm, ecdsa, ecdh.\n"
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000111#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100112 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000114#else
115#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000117#endif
118
119#define TIME_AND_TSC( TITLE, CODE ) \
120do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200121 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200122 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000123 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200124 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000125 fflush( stdout ); \
126 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200127 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200128 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000129 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200130 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000131 } \
132 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200133 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200134 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200136 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000137 } \
138 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200139 if( ret != 0 ) \
140 { \
141 PRINT_ERROR; \
142 } \
143 else \
144 { \
145 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
146 ii * BUFSIZE / 1024, \
147 ( mbedtls_timing_hardclock() - tsc ) \
148 / ( jj * BUFSIZE ) ); \
149 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000150} while( 0 )
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100153
154#define MEMORY_MEASURE_INIT \
155 size_t max_used, max_blocks, max_bytes; \
156 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200157 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100159
160#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200161 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
162 for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163 max_used -= prv_used; \
164 max_blocks -= prv_blocks; \
165 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100167
168#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000169#define MEMORY_MEASURE_INIT
170#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100171#endif
172
Rich Evans85b05ec2015-02-12 11:37:29 +0000173#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
174do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200175 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000176 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100177 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000178 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200179 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000180 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200181 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000182 \
183 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200184 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000185 { \
186 CODE; \
187 } \
188 \
189 if( ret != 0 ) \
190 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100191 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000192 } \
193 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100194 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200195 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100196 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200197 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100198 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000199} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Paul Bakkera3d195c2011-11-27 21:07:34 +0000201static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000202{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000203 size_t use_len;
204 int rnd;
205
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 if( rng_state != NULL )
207 rng_state = NULL;
208
Paul Bakkera3d195c2011-11-27 21:07:34 +0000209 while( len > 0 )
210 {
211 use_len = len;
212 if( use_len > sizeof(int) )
213 use_len = sizeof(int);
214
215 rnd = rand();
216 memcpy( output, &rnd, use_len );
217 output += use_len;
218 len -= use_len;
219 }
220
221 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222}
223
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100224/*
225 * Clear some memory that was used to prepare the context
226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_ECP_C)
228void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100229{
230 if( grp->T != NULL )
231 {
232 size_t i;
233 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_ecp_point_free( &grp->T[i] );
235 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100236 }
237 grp->T = NULL;
238 grp->T_size = 0;
239}
240#else
241#define ecp_clear_precomputed( g )
242#endif
243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244unsigned char buf[BUFSIZE];
245
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200246typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200247 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100248 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200249 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200250 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200251 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300252 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100253 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200254 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255} todo_list;
256
Simon Butcher63cb97e2018-12-06 17:43:31 +0000257#if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C)
258void mbedtls_param_failed( char* failure_condition, char* file, int line )
259{
260 mbedtls_printf("%s:%i: Input param failed - %sn", file, line, failure_condition );
261 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
262}
263#endif
264
Paul Bakkercce9d772011-11-18 14:26:47 +0000265int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000266{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100267 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200268 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200269 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200272 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000273#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000274
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000275 if( argc <= 1 )
276 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200277 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000278 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200279 else
280 {
281 memset( &todo, 0, sizeof( todo ) );
282
283 for( i = 1; i < argc; i++ )
284 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200285 if( strcmp( argv[i], "md4" ) == 0 )
286 todo.md4 = 1;
287 else if( strcmp( argv[i], "md5" ) == 0 )
288 todo.md5 = 1;
289 else if( strcmp( argv[i], "ripemd160" ) == 0 )
290 todo.ripemd160 = 1;
291 else if( strcmp( argv[i], "sha1" ) == 0 )
292 todo.sha1 = 1;
293 else if( strcmp( argv[i], "sha256" ) == 0 )
294 todo.sha256 = 1;
295 else if( strcmp( argv[i], "sha512" ) == 0 )
296 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297 else if( strcmp( argv[i], "arc4" ) == 0 )
298 todo.arc4 = 1;
299 else if( strcmp( argv[i], "des3" ) == 0 )
300 todo.des3 = 1;
301 else if( strcmp( argv[i], "des" ) == 0 )
302 todo.des = 1;
303 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
304 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200305 else if( strcmp( argv[i], "aes_xts" ) == 0 )
306 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200307 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
308 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200309 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
310 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200311 else if( strcmp( argv[i], "chachapoly" ) == 0 )
312 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100313 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
314 todo.aes_cmac = 1;
315 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
316 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100317 else if( strcmp( argv[i], "aria" ) == 0 )
318 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200319 else if( strcmp( argv[i], "camellia" ) == 0 )
320 todo.camellia = 1;
321 else if( strcmp( argv[i], "blowfish" ) == 0 )
322 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300323 else if( strcmp( argv[i], "chacha20" ) == 0 )
324 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300325 else if( strcmp( argv[i], "poly1305" ) == 0 )
326 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 else if( strcmp( argv[i], "havege" ) == 0 )
328 todo.havege = 1;
329 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
330 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100331 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
332 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200333 else if( strcmp( argv[i], "rsa" ) == 0 )
334 todo.rsa = 1;
335 else if( strcmp( argv[i], "dhm" ) == 0 )
336 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200337 else if( strcmp( argv[i], "ecdsa" ) == 0 )
338 todo.ecdsa = 1;
339 else if( strcmp( argv[i], "ecdh" ) == 0 )
340 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200341 else
342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
344 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200345 }
346 }
347 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200352 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000353#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200354 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200355 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200358 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100359 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360#endif
361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200363 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100364 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365#endif
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200368 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100369 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100370#endif
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200373 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100374 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375#endif
376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200378 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100379 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380#endif
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200383 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100384 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000385#endif
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200388 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_arc4_context arc4;
391 mbedtls_arc4_init( &arc4 );
392 mbedtls_arc4_setup( &arc4, tmp, 32 );
393 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
394 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200395 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000396#endif
397
Simon Butcher549dc3d2016-10-05 14:14:19 +0100398#if defined(MBEDTLS_DES_C)
399#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200400 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_des3_context des3;
403 mbedtls_des3_init( &des3 );
404 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200405 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
407 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200408 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200410 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_des_context des;
413 mbedtls_des_init( &des );
414 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
417 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200418 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100419
420#endif /* MBEDTLS_CIPHER_MODE_CBC */
421#if defined(MBEDTLS_CMAC_C)
422 if( todo.des3_cmac )
423 {
424 unsigned char output[8];
425 const mbedtls_cipher_info_t *cipher_info;
426
427 memset( buf, 0, sizeof( buf ) );
428 memset( tmp, 0, sizeof( tmp ) );
429
430 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
431
432 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100433 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
434 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100435 }
436#endif /* MBEDTLS_CMAC_C */
437#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#if defined(MBEDTLS_AES_C)
440#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200441 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100443 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_aes_context aes;
445 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200446 for( keysize = 128; keysize <= 256; keysize += 64 )
447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200450 memset( buf, 0, sizeof( buf ) );
451 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200454 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200456 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200459#endif
Aorimn5f778012016-06-09 23:22:58 +0200460#if defined(MBEDTLS_CIPHER_MODE_XTS)
461 if( todo.aes_xts )
462 {
463 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100464 mbedtls_aes_xts_context ctx;
465
466 mbedtls_aes_xts_init( &ctx );
467 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200468 {
469 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
470
471 memset( buf, 0, sizeof( buf ) );
472 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100473 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200474
475 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100476 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
477 tmp, buf, buf ) );
478
479 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200480 }
Aorimn5f778012016-06-09 23:22:58 +0200481 }
482#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200484 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000485 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100486 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200488
489 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200490 for( keysize = 128; keysize <= 256; keysize += 64 )
491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000493
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200494 memset( buf, 0, sizeof( buf ) );
495 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200496 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000497
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200498 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200500 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000504 }
505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200507 if( todo.aes_ccm )
508 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100509 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200511
512 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200513 for( keysize = 128; keysize <= 256; keysize += 64 )
514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200516
517 memset( buf, 0, sizeof( buf ) );
518 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200519 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200520
521 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200523 12, NULL, 0, buf, buf, tmp, 16 ) );
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200526 }
527 }
528#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200529#if defined(MBEDTLS_CHACHAPOLY_C)
530 if( todo.chachapoly )
531 {
532 mbedtls_chachapoly_context chachapoly;
533
534 mbedtls_chachapoly_init( &chachapoly );
535 memset( buf, 0, sizeof( buf ) );
536 memset( tmp, 0, sizeof( tmp ) );
537
538 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
539
540 mbedtls_chachapoly_setkey( &chachapoly, tmp );
541
542 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200543 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
544 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200545
546 mbedtls_chachapoly_free( &chachapoly );
547 }
548#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100549#if defined(MBEDTLS_CMAC_C)
550 if( todo.aes_cmac )
551 {
552 unsigned char output[16];
553 const mbedtls_cipher_info_t *cipher_info;
554 mbedtls_cipher_type_t cipher_type;
555 int keysize;
556
557 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
558 keysize <= 256;
559 keysize += 64, cipher_type++ )
560 {
561 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
562
563 memset( buf, 0, sizeof( buf ) );
564 memset( tmp, 0, sizeof( tmp ) );
565
566 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
567
568 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100569 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
570 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100571 }
572
573 memset( buf, 0, sizeof( buf ) );
574 memset( tmp, 0, sizeof( tmp ) );
575 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100576 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
577 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100578 }
579#endif /* MBEDTLS_CMAC_C */
580#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100582#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
583 if( todo.aria )
584 {
585 int keysize;
586 mbedtls_aria_context aria;
587 mbedtls_aria_init( &aria );
588 for( keysize = 128; keysize <= 256; keysize += 64 )
589 {
590 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
591
592 memset( buf, 0, sizeof( buf ) );
593 memset( tmp, 0, sizeof( tmp ) );
594 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
595
596 TIME_AND_TSC( title,
597 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
598 BUFSIZE, tmp, buf, buf ) );
599 }
600 mbedtls_aria_free( &aria );
601 }
602#endif
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200605 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000606 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100607 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_camellia_context camellia;
609 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200610 for( keysize = 128; keysize <= 256; keysize += 64 )
611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000613
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200614 memset( buf, 0, sizeof( buf ) );
615 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000617
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200618 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200620 BUFSIZE, tmp, buf, buf ) );
621 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000623 }
624#endif
625
Daniel King34b822c2016-05-15 17:28:08 -0300626#if defined(MBEDTLS_CHACHA20_C)
627 if ( todo.chacha20 )
628 {
629 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
630 }
631#endif
632
Daniel Kingadc32c02016-05-16 18:25:45 -0300633#if defined(MBEDTLS_POLY1305_C)
634 if ( todo.poly1305 )
635 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200636 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300637 }
638#endif
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200641 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000642 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100643 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_blowfish_context blowfish;
645 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200646
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200647 for( keysize = 128; keysize <= 256; keysize += 64 )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000650
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200651 memset( buf, 0, sizeof( buf ) );
652 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000654
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200655 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200657 tmp, buf, buf ) );
658 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000661 }
662#endif
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200665 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_havege_state hs;
668 mbedtls_havege_init( &hs );
669 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
670 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200671 }
Paul Bakker02faf452011-11-29 11:23:58 +0000672#endif
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200675 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000678
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200679 mbedtls_ctr_drbg_init( &ctr_drbg );
680
681 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200683 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200684 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Paul Bakker02faf452011-11-29 11:23:58 +0000685
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200686 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_exit(1);
688 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200689 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200690 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200692 }
Paul Bakker02faf452011-11-29 11:23:58 +0000693#endif
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100696 if( todo.hmac_drbg )
697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_hmac_drbg_context hmac_drbg;
699 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100700
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200701 mbedtls_hmac_drbg_init( &hmac_drbg );
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SHA1_C)
704 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
705 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100706
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200707 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100709 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200710 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100711
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200712 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_exit(1);
714 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
715 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100716 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200717 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100718#endif
719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#if defined(MBEDTLS_SHA256_C)
721 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
722 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100723
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200724 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100726 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
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
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200729 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_exit(1);
731 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
732 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100733 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200734 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100735#endif
Ron Eldor278af452018-06-20 18:40:21 +0300736 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100737 }
738#endif
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200741 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100743 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200745 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
750 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200751
752 TIME_PUBLIC( title, " public",
753 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200755
756 TIME_PUBLIC( title, "private",
757 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200761 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000763#endif
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200766 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100767 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200768 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800769 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100770 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800771 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100772 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800773 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100774 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800775 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100776 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
777
778 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
779 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
780 sizeof( dhm_P_3072 ) };
781
782 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
783 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
784 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200787 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200788 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200791
Hanno Beckerb9539212017-10-04 13:13:34 +0100792 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
793 dhm_P_size[i] ) != 0 ||
794 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
795 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200798 }
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 dhm.len = mbedtls_mpi_size( &dhm.P );
801 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
802 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
803 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200806 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200808 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100809 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200812 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100813 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200816 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100817 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100818#endif
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200821 if( todo.ecdsa )
822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_ecdsa_context ecdsa;
824 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200825 size_t sig_len;
826
827 memset( buf, 0x2A, sizeof( buf ) );
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 for( curve_info = mbedtls_ecp_curve_list();
830 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200831 curve_info++ )
832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
836 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100837 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200840 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200841 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200842 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200843 tmp, &sig_len, myrand, NULL ) );
844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100846 }
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 for( curve_info = mbedtls_ecp_curve_list();
849 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100850 curve_info++ )
851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200855 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100856 tmp, &sig_len, myrand, NULL ) != 0 )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100859 }
860 ecp_clear_precomputed( &ecdsa.grp );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200864 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200865 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200866 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200869 }
870 }
871#endif
872
Janos Follath52735ef2018-08-15 10:19:16 +0100873#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874 if( todo.ecdh )
875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000878 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
879#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
880 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000881#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000882#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
883 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
884#endif
885 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
886 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200888 size_t olen;
889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 for( curve_info = mbedtls_ecp_curve_list();
891 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200892 curve_info++ )
893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200895
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200896 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200898 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200902 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100903 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200906 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200907 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200909 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200911 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100913 }
914
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000915 /* Montgomery curves need to be handled separately */
916 for ( curve_info = montgomery_curve_list;
917 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
918 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000919 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000920 mbedtls_ecdh_init( &ecdh );
921 mbedtls_mpi_init( &z );
922
923 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
924 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
925 {
926 mbedtls_exit( 1 );
927 }
928
929 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
930 curve_info->name );
931 TIME_PUBLIC( title, "handshake",
932 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
933 myrand, NULL );
934 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
935 myrand, NULL ) );
936
937 mbedtls_ecdh_free( &ecdh );
938 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000939 }
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 for( curve_info = mbedtls_ecp_curve_list();
942 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100943 curve_info++ )
944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100946
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200947 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100949 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
951 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100952 myrand, NULL ) != 0 )
953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100955 }
956 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200959 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200960 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200962 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200964 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000965
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000966 /* Montgomery curves need to be handled separately */
967 for ( curve_info = montgomery_curve_list;
968 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
969 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000970 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000971 mbedtls_ecdh_init( &ecdh );
972 mbedtls_mpi_init( &z );
973
974 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
975 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
976 myrand, NULL ) != 0 ||
977 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
978 {
979 mbedtls_exit( 1 );
980 }
981
982 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
983 curve_info->name );
984 TIME_PUBLIC( title, "handshake",
985 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
986 myrand, NULL ) );
987
988 mbedtls_ecdh_free( &ecdh );
989 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000990 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200991 }
992#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
997 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000998#endif
999
Paul Bakkercce9d772011-11-18 14:26:47 +00001000#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 fflush( stdout ); getchar();
1003#endif
1004
1005 return( 0 );
1006}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#endif /* MBEDTLS_TIMING_C */