blob: e31faafeb3010c9397133657230ed2f4771416ce [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é-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_exit exit
34#define mbedtls_printf printf
35#define mbedtls_snprintf snprintf
36#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010037#define mbedtls_exit exit
38#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
39#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000043int main( void )
44{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000046 return( 0 );
47}
48#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010049
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000050#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020051#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/md4.h"
56#include "mbedtls/md5.h"
57#include "mbedtls/ripemd160.h"
58#include "mbedtls/sha1.h"
59#include "mbedtls/sha256.h"
60#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/arc4.h"
63#include "mbedtls/des.h"
64#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010065#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/blowfish.h"
67#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030068#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/gcm.h"
70#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020071#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010072#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030073#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/havege.h"
76#include "mbedtls/ctr_drbg.h"
77#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010078
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/rsa.h"
80#include "mbedtls/dhm.h"
81#include "mbedtls/ecdsa.h"
82#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010083
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000088#endif
89
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000090/*
91 * For heap usage estimates, we need an estimate of the overhead per allocated
92 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
93 * so use that as our baseline.
94 */
95#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
96
97/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020098 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000099 */
100#define HEAP_SIZE (1u << 16) // 64k
101
Paul Bakker02faf452011-11-29 11:23:58 +0000102#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100103#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100104#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000105
Rich Evans85b05ec2015-02-12 11:37:29 +0000106#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200107 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300108 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200109 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300110 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000111 "havege, ctr_drbg, hmac_drbg\n" \
112 "rsa, dhm, ecdsa, ecdh.\n"
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000115#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100116 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000118#else
119#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000121#endif
122
123#define TIME_AND_TSC( TITLE, CODE ) \
124do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200125 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200126 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000127 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200128 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000129 fflush( stdout ); \
130 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200131 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200132 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000133 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200134 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135 } \
136 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200137 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200138 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000139 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200140 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000141 } \
142 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200143 if( ret != 0 ) \
144 { \
145 PRINT_ERROR; \
146 } \
147 else \
148 { \
149 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
150 ii * BUFSIZE / 1024, \
151 ( mbedtls_timing_hardclock() - tsc ) \
152 / ( jj * BUFSIZE ) ); \
153 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000154} while( 0 )
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100157
158#define MEMORY_MEASURE_INIT \
159 size_t max_used, max_blocks, max_bytes; \
160 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200161 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163
164#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200165 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Hanno Becker1eeca412018-10-15 12:01:35 +0100166 for( ii = 12 - (title_len); ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100167 max_used -= prv_used; \
168 max_blocks -= prv_blocks; \
169 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100171
172#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000173#define MEMORY_MEASURE_INIT
174#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100175#endif
176
Rich Evans85b05ec2015-02-12 11:37:29 +0000177#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
178do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200179 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000180 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100181 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000182 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200183 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000184 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200185 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000186 \
187 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200188 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000189 { \
190 CODE; \
191 } \
192 \
193 if( ret != 0 ) \
194 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100195 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000196 } \
197 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100198 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200199 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100200 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200201 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100202 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000203} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakkera3d195c2011-11-27 21:07:34 +0000205static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000207 size_t use_len;
208 int rnd;
209
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 if( rng_state != NULL )
211 rng_state = NULL;
212
Paul Bakkera3d195c2011-11-27 21:07:34 +0000213 while( len > 0 )
214 {
215 use_len = len;
216 if( use_len > sizeof(int) )
217 use_len = sizeof(int);
218
219 rnd = rand();
220 memcpy( output, &rnd, use_len );
221 output += use_len;
222 len -= use_len;
223 }
224
225 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226}
227
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100228/*
229 * Clear some memory that was used to prepare the context
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_ECP_C)
232void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100233{
234 if( grp->T != NULL )
235 {
236 size_t i;
237 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_ecp_point_free( &grp->T[i] );
239 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100240 }
241 grp->T = NULL;
242 grp->T_size = 0;
243}
244#else
245#define ecp_clear_precomputed( g )
246#endif
247
Paul Bakker5121ce52009-01-03 21:22:43 +0000248unsigned char buf[BUFSIZE];
249
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200250typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200251 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100252 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200253 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200254 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200255 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300256 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100257 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200258 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200259} todo_list;
260
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100261#if defined(MBEDTLS_CHECK_PARAMS)
262#include "mbedtls/platform_util.h"
263void mbedtls_param_failed( const char *failure_condition,
264 const char *file,
265 int line )
Simon Butcher63cb97e2018-12-06 17:43:31 +0000266{
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100267 mbedtls_printf( "%s:%i: Input param failed - %s\n",
268 file, line, failure_condition );
Simon Butcher63cb97e2018-12-06 17:43:31 +0000269 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
270}
271#endif
272
Paul Bakkercce9d772011-11-18 14:26:47 +0000273int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000274{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100275 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200276 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200277 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200278 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200280 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000281#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000282
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000283 if( argc <= 1 )
284 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200285 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000286 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200287 else
288 {
289 memset( &todo, 0, sizeof( todo ) );
290
291 for( i = 1; i < argc; i++ )
292 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200293 if( strcmp( argv[i], "md4" ) == 0 )
294 todo.md4 = 1;
295 else if( strcmp( argv[i], "md5" ) == 0 )
296 todo.md5 = 1;
297 else if( strcmp( argv[i], "ripemd160" ) == 0 )
298 todo.ripemd160 = 1;
299 else if( strcmp( argv[i], "sha1" ) == 0 )
300 todo.sha1 = 1;
301 else if( strcmp( argv[i], "sha256" ) == 0 )
302 todo.sha256 = 1;
303 else if( strcmp( argv[i], "sha512" ) == 0 )
304 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200305 else if( strcmp( argv[i], "arc4" ) == 0 )
306 todo.arc4 = 1;
307 else if( strcmp( argv[i], "des3" ) == 0 )
308 todo.des3 = 1;
309 else if( strcmp( argv[i], "des" ) == 0 )
310 todo.des = 1;
311 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
312 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200313 else if( strcmp( argv[i], "aes_xts" ) == 0 )
314 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200315 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
316 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200317 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
318 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200319 else if( strcmp( argv[i], "chachapoly" ) == 0 )
320 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100321 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
322 todo.aes_cmac = 1;
323 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
324 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100325 else if( strcmp( argv[i], "aria" ) == 0 )
326 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 else if( strcmp( argv[i], "camellia" ) == 0 )
328 todo.camellia = 1;
329 else if( strcmp( argv[i], "blowfish" ) == 0 )
330 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300331 else if( strcmp( argv[i], "chacha20" ) == 0 )
332 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300333 else if( strcmp( argv[i], "poly1305" ) == 0 )
334 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200335 else if( strcmp( argv[i], "havege" ) == 0 )
336 todo.havege = 1;
337 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
338 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100339 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
340 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200341 else if( strcmp( argv[i], "rsa" ) == 0 )
342 todo.rsa = 1;
343 else if( strcmp( argv[i], "dhm" ) == 0 )
344 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200345 else if( strcmp( argv[i], "ecdsa" ) == 0 )
346 todo.ecdsa = 1;
347 else if( strcmp( argv[i], "ecdh" ) == 0 )
348 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200349 else
350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
352 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200353 }
354 }
355 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200360 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000361#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200362 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200363 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200366 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100367 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368#endif
369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200371 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100372 TIME_AND_TSC( "MD5", mbedtls_md5_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_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200376 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100377 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100378#endif
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200381 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100382 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383#endif
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200386 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100387 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388#endif
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200391 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100392 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000393#endif
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200396 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_arc4_context arc4;
399 mbedtls_arc4_init( &arc4 );
400 mbedtls_arc4_setup( &arc4, tmp, 32 );
401 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
402 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200403 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000404#endif
405
Simon Butcher549dc3d2016-10-05 14:14:19 +0100406#if defined(MBEDTLS_DES_C)
407#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200408 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_des3_context des3;
411 mbedtls_des3_init( &des3 );
412 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200413 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
415 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200416 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200418 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_des_context des;
421 mbedtls_des_init( &des );
422 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
425 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200426 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100427
428#endif /* MBEDTLS_CIPHER_MODE_CBC */
429#if defined(MBEDTLS_CMAC_C)
430 if( todo.des3_cmac )
431 {
432 unsigned char output[8];
433 const mbedtls_cipher_info_t *cipher_info;
434
435 memset( buf, 0, sizeof( buf ) );
436 memset( tmp, 0, sizeof( tmp ) );
437
438 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
439
440 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100441 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
442 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100443 }
444#endif /* MBEDTLS_CMAC_C */
445#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_AES_C)
448#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200449 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100451 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_aes_context aes;
453 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200454 for( keysize = 128; keysize <= 256; keysize += 64 )
455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200458 memset( buf, 0, sizeof( buf ) );
459 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200462 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200464 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200467#endif
Aorimn5f778012016-06-09 23:22:58 +0200468#if defined(MBEDTLS_CIPHER_MODE_XTS)
469 if( todo.aes_xts )
470 {
471 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100472 mbedtls_aes_xts_context ctx;
473
474 mbedtls_aes_xts_init( &ctx );
475 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200476 {
477 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
478
479 memset( buf, 0, sizeof( buf ) );
480 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100481 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200482
483 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100484 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
485 tmp, buf, buf ) );
486
487 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200488 }
Aorimn5f778012016-06-09 23:22:58 +0200489 }
490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200492 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000493 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100494 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200496
497 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200498 for( keysize = 128; keysize <= 256; keysize += 64 )
499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000501
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200502 memset( buf, 0, sizeof( buf ) );
503 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200504 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000505
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200506 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200508 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200511 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000512 }
513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200515 if( todo.aes_ccm )
516 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100517 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200519
520 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200521 for( keysize = 128; keysize <= 256; keysize += 64 )
522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200524
525 memset( buf, 0, sizeof( buf ) );
526 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200527 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200528
529 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200531 12, NULL, 0, buf, buf, tmp, 16 ) );
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200534 }
535 }
536#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200537#if defined(MBEDTLS_CHACHAPOLY_C)
538 if( todo.chachapoly )
539 {
540 mbedtls_chachapoly_context chachapoly;
541
542 mbedtls_chachapoly_init( &chachapoly );
543 memset( buf, 0, sizeof( buf ) );
544 memset( tmp, 0, sizeof( tmp ) );
545
546 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
547
548 mbedtls_chachapoly_setkey( &chachapoly, tmp );
549
550 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200551 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
552 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200553
554 mbedtls_chachapoly_free( &chachapoly );
555 }
556#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100557#if defined(MBEDTLS_CMAC_C)
558 if( todo.aes_cmac )
559 {
560 unsigned char output[16];
561 const mbedtls_cipher_info_t *cipher_info;
562 mbedtls_cipher_type_t cipher_type;
563 int keysize;
564
565 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
566 keysize <= 256;
567 keysize += 64, cipher_type++ )
568 {
569 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
570
571 memset( buf, 0, sizeof( buf ) );
572 memset( tmp, 0, sizeof( tmp ) );
573
574 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
575
576 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100577 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
578 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100579 }
580
581 memset( buf, 0, sizeof( buf ) );
582 memset( tmp, 0, sizeof( tmp ) );
583 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100584 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
585 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100586 }
587#endif /* MBEDTLS_CMAC_C */
588#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100590#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
591 if( todo.aria )
592 {
593 int keysize;
594 mbedtls_aria_context aria;
595 mbedtls_aria_init( &aria );
596 for( keysize = 128; keysize <= 256; keysize += 64 )
597 {
598 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
599
600 memset( buf, 0, sizeof( buf ) );
601 memset( tmp, 0, sizeof( tmp ) );
602 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
603
604 TIME_AND_TSC( title,
605 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
606 BUFSIZE, tmp, buf, buf ) );
607 }
608 mbedtls_aria_free( &aria );
609 }
610#endif
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200613 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000614 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100615 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_camellia_context camellia;
617 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200618 for( keysize = 128; keysize <= 256; keysize += 64 )
619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000621
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200622 memset( buf, 0, sizeof( buf ) );
623 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000625
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200626 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200628 BUFSIZE, tmp, buf, buf ) );
629 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000631 }
632#endif
633
Daniel King34b822c2016-05-15 17:28:08 -0300634#if defined(MBEDTLS_CHACHA20_C)
635 if ( todo.chacha20 )
636 {
637 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
638 }
639#endif
640
Daniel Kingadc32c02016-05-16 18:25:45 -0300641#if defined(MBEDTLS_POLY1305_C)
642 if ( todo.poly1305 )
643 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200644 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300645 }
646#endif
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200649 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000650 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100651 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_blowfish_context blowfish;
653 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200654
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200655 for( keysize = 128; keysize <= 256; keysize += 64 )
656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000658
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200659 memset( buf, 0, sizeof( buf ) );
660 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000662
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200663 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200665 tmp, buf, buf ) );
666 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000669 }
670#endif
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200673 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_havege_state hs;
676 mbedtls_havege_init( &hs );
677 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
678 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200679 }
Paul Bakker02faf452011-11-29 11:23:58 +0000680#endif
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200683 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000686
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200687 mbedtls_ctr_drbg_init( &ctr_drbg );
688
689 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200691 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200692 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Paul Bakker02faf452011-11-29 11:23:58 +0000693
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200694 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);
696 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200697 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200698 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200700 }
Paul Bakker02faf452011-11-29 11:23:58 +0000701#endif
702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100704 if( todo.hmac_drbg )
705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_hmac_drbg_context hmac_drbg;
707 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100708
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200709 mbedtls_hmac_drbg_init( &hmac_drbg );
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#if defined(MBEDTLS_SHA1_C)
712 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
713 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100714
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200715 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100717 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200718 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
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);
722 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
723 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100724 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200725 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100726#endif
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_SHA256_C)
729 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
730 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100731
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200732 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100734 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200735 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
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);
739 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
740 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100741 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200742 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100743#endif
Ron Eldor278af452018-06-20 18:40:21 +0300744 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100745 }
746#endif
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200749 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100751 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200753 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
758 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200759
760 TIME_PUBLIC( title, " public",
761 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200763
764 TIME_PUBLIC( title, "private",
765 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200769 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000771#endif
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200774 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100775 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200776 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800777 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100778 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800779 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100780 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800781 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100782 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800783 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100784 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
785
786 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
787 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
788 sizeof( dhm_P_3072 ) };
789
790 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
791 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
792 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200795 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200796 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200799
Hanno Beckerb9539212017-10-04 13:13:34 +0100800 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
801 dhm_P_size[i] ) != 0 ||
802 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
803 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200806 }
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 dhm.len = mbedtls_mpi_size( &dhm.P );
809 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
810 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
811 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200814 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200816 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100817 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200820 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100821 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200824 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100825 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100826#endif
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200829 if( todo.ecdsa )
830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_ecdsa_context ecdsa;
832 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200833 size_t sig_len;
834
835 memset( buf, 0x2A, sizeof( buf ) );
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 for( curve_info = mbedtls_ecp_curve_list();
838 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200839 curve_info++ )
840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
844 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100845 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200848 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200849 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200850 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200851 tmp, &sig_len, myrand, NULL ) );
852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100854 }
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 for( curve_info = mbedtls_ecp_curve_list();
857 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100858 curve_info++ )
859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200863 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100864 tmp, &sig_len, myrand, NULL ) != 0 )
865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100867 }
868 ecp_clear_precomputed( &ecdsa.grp );
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100871 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200872 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200873 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200874 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200877 }
878 }
879#endif
880
Janos Follath52735ef2018-08-15 10:19:16 +0100881#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200882 if( todo.ecdh )
883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000886 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
887#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
888 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000889#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000890#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
891 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
892#endif
893 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
894 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200896 size_t olen;
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 for( curve_info = mbedtls_ecp_curve_list();
899 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200900 curve_info++ )
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200903
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200904 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200906 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200910 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100911 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200914 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200915 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200917 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200919 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100921 }
922
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000923 /* Montgomery curves need to be handled separately */
924 for ( curve_info = montgomery_curve_list;
925 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
926 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000927 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000928 mbedtls_ecdh_init( &ecdh );
929 mbedtls_mpi_init( &z );
930
931 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
932 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
933 {
934 mbedtls_exit( 1 );
935 }
936
937 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
938 curve_info->name );
939 TIME_PUBLIC( title, "handshake",
940 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
941 myrand, NULL );
942 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
943 myrand, NULL ) );
944
945 mbedtls_ecdh_free( &ecdh );
946 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000947 }
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 for( curve_info = mbedtls_ecp_curve_list();
950 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100951 curve_info++ )
952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100954
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200955 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100957 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
959 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100960 myrand, NULL ) != 0 )
961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100963 }
964 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200967 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200968 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200970 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200972 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000973
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000974 /* Montgomery curves need to be handled separately */
975 for ( curve_info = montgomery_curve_list;
976 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
977 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000978 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000979 mbedtls_ecdh_init( &ecdh );
980 mbedtls_mpi_init( &z );
981
982 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
983 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
984 myrand, NULL ) != 0 ||
985 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
986 {
987 mbedtls_exit( 1 );
988 }
989
990 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
991 curve_info->name );
992 TIME_PUBLIC( title, "handshake",
993 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
994 myrand, NULL ) );
995
996 mbedtls_ecdh_free( &ecdh );
997 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000998 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200999 }
1000#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1005 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001006#endif
1007
Paul Bakkercce9d772011-11-18 14:26:47 +00001008#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 fflush( stdout ); getchar();
1011#endif
1012
1013 return( 0 );
1014}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_TIMING_C */