blob: 7cdff10b1c77284950ede0ebd7aad7602c7d88c2 [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 */
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +0100100#define HEAP_SIZE (1u << 16) /* 64k */
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000101
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 \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000193 if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) \
194 { \
195 mbedtls_printf( "Feature Not Supported. Skipping.\n" ); \
196 ret = 0; \
197 } \
198 else if( ret != 0 ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000199 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100200 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000201 } \
202 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100203 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200204 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100205 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200206 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100207 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000208} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Paul Bakkera3d195c2011-11-27 21:07:34 +0000210static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000212 size_t use_len;
213 int rnd;
214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 if( rng_state != NULL )
216 rng_state = NULL;
217
Paul Bakkera3d195c2011-11-27 21:07:34 +0000218 while( len > 0 )
219 {
220 use_len = len;
221 if( use_len > sizeof(int) )
222 use_len = sizeof(int);
223
224 rnd = rand();
225 memcpy( output, &rnd, use_len );
226 output += use_len;
227 len -= use_len;
228 }
229
230 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231}
232
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000233#define CHECK_AND_CONTINUE( R ) \
234 { \
Gilles Peskineb14c4a52019-02-11 18:23:42 +0100235 int CHECK_AND_CONTINUE_ret = ( R ); \
236 if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000237 mbedtls_printf( "Feature not supported. Skipping.\n" ); \
238 continue; \
239 } \
Gilles Peskineb14c4a52019-02-11 18:23:42 +0100240 else if( CHECK_AND_CONTINUE_ret != 0 ) { \
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000241 mbedtls_exit( 1 ); \
242 } \
243 }
Christoph M. Wintersteiger3dca1a42018-12-14 11:54:59 +0000244
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100245/*
246 * Clear some memory that was used to prepare the context
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_ECP_C)
249void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100250{
251 if( grp->T != NULL )
252 {
253 size_t i;
254 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ecp_point_free( &grp->T[i] );
256 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100257 }
258 grp->T = NULL;
259 grp->T_size = 0;
260}
261#else
262#define ecp_clear_precomputed( g )
263#endif
264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265unsigned char buf[BUFSIZE];
266
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200267typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200268 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100269 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200270 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200271 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200272 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300273 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100274 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200275 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200276} todo_list;
277
Simon Butcher63cb97e2018-12-06 17:43:31 +0000278
Paul Bakkercce9d772011-11-18 14:26:47 +0000279int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000280{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100281 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200282 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200283 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200284 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200286 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000287#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000288
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000289 if( argc <= 1 )
290 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200291 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000292 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200293 else
294 {
295 memset( &todo, 0, sizeof( todo ) );
296
297 for( i = 1; i < argc; i++ )
298 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200299 if( strcmp( argv[i], "md4" ) == 0 )
300 todo.md4 = 1;
301 else if( strcmp( argv[i], "md5" ) == 0 )
302 todo.md5 = 1;
303 else if( strcmp( argv[i], "ripemd160" ) == 0 )
304 todo.ripemd160 = 1;
305 else if( strcmp( argv[i], "sha1" ) == 0 )
306 todo.sha1 = 1;
307 else if( strcmp( argv[i], "sha256" ) == 0 )
308 todo.sha256 = 1;
309 else if( strcmp( argv[i], "sha512" ) == 0 )
310 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200311 else if( strcmp( argv[i], "arc4" ) == 0 )
312 todo.arc4 = 1;
313 else if( strcmp( argv[i], "des3" ) == 0 )
314 todo.des3 = 1;
315 else if( strcmp( argv[i], "des" ) == 0 )
316 todo.des = 1;
317 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
318 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200319 else if( strcmp( argv[i], "aes_xts" ) == 0 )
320 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200321 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
322 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200323 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
324 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200325 else if( strcmp( argv[i], "chachapoly" ) == 0 )
326 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100327 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
328 todo.aes_cmac = 1;
329 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
330 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100331 else if( strcmp( argv[i], "aria" ) == 0 )
332 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200333 else if( strcmp( argv[i], "camellia" ) == 0 )
334 todo.camellia = 1;
335 else if( strcmp( argv[i], "blowfish" ) == 0 )
336 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300337 else if( strcmp( argv[i], "chacha20" ) == 0 )
338 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300339 else if( strcmp( argv[i], "poly1305" ) == 0 )
340 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200341 else if( strcmp( argv[i], "havege" ) == 0 )
342 todo.havege = 1;
343 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
344 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100345 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
346 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200347 else if( strcmp( argv[i], "rsa" ) == 0 )
348 todo.rsa = 1;
349 else if( strcmp( argv[i], "dhm" ) == 0 )
350 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200351 else if( strcmp( argv[i], "ecdsa" ) == 0 )
352 todo.ecdsa = 1;
353 else if( strcmp( argv[i], "ecdh" ) == 0 )
354 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200355 else
356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
358 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200359 }
360 }
361 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200366 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000367#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200368 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200369 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200372 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374#endif
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200377 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100378 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379#endif
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200382 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100383 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100384#endif
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200387 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100388 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389#endif
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200392 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100393 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394#endif
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200397 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100398 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000399#endif
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200402 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_arc4_context arc4;
405 mbedtls_arc4_init( &arc4 );
406 mbedtls_arc4_setup( &arc4, tmp, 32 );
407 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
408 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000410#endif
411
Simon Butcher549dc3d2016-10-05 14:14:19 +0100412#if defined(MBEDTLS_DES_C)
413#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200414 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_des3_context des3;
417 mbedtls_des3_init( &des3 );
418 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
421 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200422 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200424 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_des_context des;
427 mbedtls_des_init( &des );
428 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200429 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
431 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200432 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100433
434#endif /* MBEDTLS_CIPHER_MODE_CBC */
435#if defined(MBEDTLS_CMAC_C)
436 if( todo.des3_cmac )
437 {
438 unsigned char output[8];
439 const mbedtls_cipher_info_t *cipher_info;
440
441 memset( buf, 0, sizeof( buf ) );
442 memset( tmp, 0, sizeof( tmp ) );
443
444 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
445
446 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100447 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
448 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100449 }
450#endif /* MBEDTLS_CMAC_C */
451#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_AES_C)
454#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200455 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100457 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_aes_context aes;
459 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200460 for( keysize = 128; keysize <= 256; keysize += 64 )
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200464 memset( buf, 0, sizeof( buf ) );
465 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200468 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200473#endif
Aorimn5f778012016-06-09 23:22:58 +0200474#if defined(MBEDTLS_CIPHER_MODE_XTS)
475 if( todo.aes_xts )
476 {
477 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100478 mbedtls_aes_xts_context ctx;
479
480 mbedtls_aes_xts_init( &ctx );
481 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200482 {
483 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
484
485 memset( buf, 0, sizeof( buf ) );
486 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100487 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200488
489 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100490 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
491 tmp, buf, buf ) );
492
493 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200494 }
Aorimn5f778012016-06-09 23:22:58 +0200495 }
496#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200498 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000499 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100500 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200502
503 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200504 for( keysize = 128; keysize <= 256; keysize += 64 )
505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000507
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200508 memset( buf, 0, sizeof( buf ) );
509 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200510 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000511
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200512 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200514 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200517 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000518 }
519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200521 if( todo.aes_ccm )
522 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100523 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200525
526 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200527 for( keysize = 128; keysize <= 256; keysize += 64 )
528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200530
531 memset( buf, 0, sizeof( buf ) );
532 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200533 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200534
535 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200537 12, NULL, 0, buf, buf, tmp, 16 ) );
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200540 }
541 }
542#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200543#if defined(MBEDTLS_CHACHAPOLY_C)
544 if( todo.chachapoly )
545 {
546 mbedtls_chachapoly_context chachapoly;
547
548 mbedtls_chachapoly_init( &chachapoly );
549 memset( buf, 0, sizeof( buf ) );
550 memset( tmp, 0, sizeof( tmp ) );
551
552 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
553
554 mbedtls_chachapoly_setkey( &chachapoly, tmp );
555
556 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200557 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
558 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200559
560 mbedtls_chachapoly_free( &chachapoly );
561 }
562#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100563#if defined(MBEDTLS_CMAC_C)
564 if( todo.aes_cmac )
565 {
566 unsigned char output[16];
567 const mbedtls_cipher_info_t *cipher_info;
568 mbedtls_cipher_type_t cipher_type;
569 int keysize;
570
571 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
572 keysize <= 256;
573 keysize += 64, cipher_type++ )
574 {
575 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
576
577 memset( buf, 0, sizeof( buf ) );
578 memset( tmp, 0, sizeof( tmp ) );
579
580 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
581
582 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100583 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
584 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100585 }
586
587 memset( buf, 0, sizeof( buf ) );
588 memset( tmp, 0, sizeof( tmp ) );
589 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100590 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
591 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100592 }
593#endif /* MBEDTLS_CMAC_C */
594#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100596#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
597 if( todo.aria )
598 {
599 int keysize;
600 mbedtls_aria_context aria;
601 mbedtls_aria_init( &aria );
602 for( keysize = 128; keysize <= 256; keysize += 64 )
603 {
604 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
605
606 memset( buf, 0, sizeof( buf ) );
607 memset( tmp, 0, sizeof( tmp ) );
608 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
609
610 TIME_AND_TSC( title,
611 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
612 BUFSIZE, tmp, buf, buf ) );
613 }
614 mbedtls_aria_free( &aria );
615 }
616#endif
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200619 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000620 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100621 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_camellia_context camellia;
623 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200624 for( keysize = 128; keysize <= 256; keysize += 64 )
625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000627
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200628 memset( buf, 0, sizeof( buf ) );
629 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000631
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200632 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200634 BUFSIZE, tmp, buf, buf ) );
635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000637 }
638#endif
639
Daniel King34b822c2016-05-15 17:28:08 -0300640#if defined(MBEDTLS_CHACHA20_C)
641 if ( todo.chacha20 )
642 {
643 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
644 }
645#endif
646
Daniel Kingadc32c02016-05-16 18:25:45 -0300647#if defined(MBEDTLS_POLY1305_C)
648 if ( todo.poly1305 )
649 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200650 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300651 }
652#endif
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200655 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000656 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100657 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_blowfish_context blowfish;
659 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200660
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200661 for( keysize = 128; keysize <= 256; keysize += 64 )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000664
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200665 memset( buf, 0, sizeof( buf ) );
666 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000668
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200669 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200671 tmp, buf, buf ) );
672 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000675 }
676#endif
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200679 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_havege_state hs;
682 mbedtls_havege_init( &hs );
683 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
684 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200685 }
Paul Bakker02faf452011-11-29 11:23:58 +0000686#endif
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200689 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000692
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200693 mbedtls_ctr_drbg_init( &ctr_drbg );
694
695 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200697 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200698 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Paul Bakker02faf452011-11-29 11:23:58 +0000699
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200700 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_exit(1);
702 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200703 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200704 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200706 }
Paul Bakker02faf452011-11-29 11:23:58 +0000707#endif
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100710 if( todo.hmac_drbg )
711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_hmac_drbg_context hmac_drbg;
713 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100714
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200715 mbedtls_hmac_drbg_init( &hmac_drbg );
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA1_C)
718 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
719 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100720
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200721 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100723 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200724 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100725
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200726 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_exit(1);
728 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
729 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100730 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200731 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100732#endif
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_SHA256_C)
735 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
736 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100737
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200738 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100740 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200741 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100742
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200743 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_exit(1);
745 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
746 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100747 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200748 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100749#endif
Ron Eldor278af452018-06-20 18:40:21 +0300750 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100751 }
752#endif
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200755 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100757 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200759 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
764 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200765
766 TIME_PUBLIC( title, " public",
767 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200769
770 TIME_PUBLIC( title, "private",
771 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200775 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000777#endif
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200780 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100781 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200782 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800783 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100784 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800785 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100786 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800787 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100788 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800789 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100790 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
791
792 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
793 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
794 sizeof( dhm_P_3072 ) };
795
796 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
797 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
798 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200801 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200802 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200805
Hanno Beckerb9539212017-10-04 13:13:34 +0100806 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
807 dhm_P_size[i] ) != 0 ||
808 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
809 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200812 }
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 dhm.len = mbedtls_mpi_size( &dhm.P );
815 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
816 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
817 mbedtls_exit( 1 );
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 ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200820 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200822 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100823 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200826 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100827 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200830 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100831 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100832#endif
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200835 if( todo.ecdsa )
836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_ecdsa_context ecdsa;
838 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200839 size_t sig_len;
840
841 memset( buf, 0x2A, sizeof( buf ) );
842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 for( curve_info = mbedtls_ecp_curve_list();
844 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200845 curve_info++ )
846 {
Christoph M. Wintersteiger8cd4fba2019-02-15 12:34:40 +0000847 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +0000848 continue;
849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
853 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100854 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200857 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200858 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200859 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200860 tmp, &sig_len, myrand, NULL ) );
861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863 }
864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 for( curve_info = mbedtls_ecp_curve_list();
866 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100867 curve_info++ )
868 {
Christoph M. Wintersteiger8cd4fba2019-02-15 12:34:40 +0000869 if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
Christoph M. Wintersteiger6a1a9e42019-01-07 13:47:30 +0000870 continue;
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200875 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100876 tmp, &sig_len, myrand, NULL ) != 0 )
877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100879 }
880 ecp_clear_precomputed( &ecdsa.grp );
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100883 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200884 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200885 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200886 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200889 }
890 }
891#endif
892
Janos Follath52735ef2018-08-15 10:19:16 +0100893#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200894 if( todo.ecdh )
895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000898 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
899#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
900 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000901#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000902#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
903 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
904#endif
905 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
906 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200908 size_t olen;
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 for( curve_info = mbedtls_ecp_curve_list();
911 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200912 curve_info++ )
913 {
Gilles Peskinec6c7c492019-02-11 18:41:27 +0100914 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
915 continue;
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200918
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000919 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
920 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
921 myrand, NULL ) );
922 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100923 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200926 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200927 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000928 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200929 myrand, NULL ) );
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000930 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
931 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100933 }
934
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000935 /* Montgomery curves need to be handled separately */
936 for ( curve_info = montgomery_curve_list;
937 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
938 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000939 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000940 mbedtls_ecdh_init( &ecdh );
941 mbedtls_mpi_init( &z );
942
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000943 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
944 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000945
946 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
947 curve_info->name );
948 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000949 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
950 myrand, NULL ) );
951 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
952 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000953
954 mbedtls_ecdh_free( &ecdh );
955 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000956 }
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 for( curve_info = mbedtls_ecp_curve_list();
959 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100960 curve_info++ )
961 {
Gilles Peskinec6c7c492019-02-11 18:41:27 +0100962 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
963 continue;
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100966
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000967 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
968 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
969 myrand, NULL ) );
970 CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
971 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
972 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100973 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200976 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200977 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000978 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
979 myrand, NULL ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200981 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000982
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000983 /* Montgomery curves need to be handled separately */
984 for ( curve_info = montgomery_curve_list;
985 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
986 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000987 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000988 mbedtls_ecdh_init( &ecdh );
989 mbedtls_mpi_init( &z );
990
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000991 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
992 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
993 myrand, NULL ) );
994 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000995
996 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
997 curve_info->name );
998 TIME_PUBLIC( title, "handshake",
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +0000999 CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1000 myrand, NULL ) ) );
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001001
1002 mbedtls_ecdh_free( &ecdh );
1003 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001004 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001005 }
1006#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001007
Christoph M. Wintersteigere50b9702018-12-14 11:03:02 +00001008#if defined(MBEDTLS_ECDH_C)
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001009 if( todo.ecdh )
1010 {
1011 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1012 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
1013 const mbedtls_ecp_curve_info * curve_list = mbedtls_ecp_curve_list();
1014 const mbedtls_ecp_curve_info *curve_info;
1015 size_t olen;
1016
1017 for( curve_info = curve_list;
1018 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1019 curve_info++ )
1020 {
Gilles Peskinec6c7c492019-02-11 18:41:27 +01001021 if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1022 continue;
1023
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001024 mbedtls_ecdh_init( &ecdh_srv );
1025 mbedtls_ecdh_init( &ecdh_cli );
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001026 CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id ) );
1027 CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_cli, curve_info->grp_id ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001028
Christoph M. Wintersteigerb4e63a12018-12-07 13:32:59 +00001029#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001030 CHECK_AND_CONTINUE( mbedtls_ecp_group_load(&ecdh_srv.grp, curve_info->grp_id));
1031 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public(&ecdh_srv.grp,
1032 &ecdh_srv.d,
1033 &ecdh_srv.Q, myrand, NULL));
Christoph M. Wintersteigerb4e63a12018-12-07 13:32:59 +00001034#else
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001035 if( ecdh_srv.var == MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0 ) {
1036 CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh_srv.ctx.mbed_ecdh.grp, curve_info->grp_id ));
1037 CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh_srv.ctx.mbed_ecdh.grp,
1038 &ecdh_srv.ctx.mbed_ecdh.d,
1039 &ecdh_srv.ctx.mbed_ecdh.Q, myrand, NULL ));
1040 }
Christoph M. Wintersteigerb4e63a12018-12-07 13:32:59 +00001041#endif
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001042
1043 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name );
Christoph M. Wintersteiger0b931022018-12-06 17:15:12 +00001044 TIME_PUBLIC( title, "full handshake",
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001045 const unsigned char * p_srv = buf_srv;
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001046 CHECK_AND_CONTINUE( mbedtls_ecdh_make_params( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001047
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001048 CHECK_AND_CONTINUE( mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen ) );
1049 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001050
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001051 CHECK_AND_CONTINUE( mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen ) );
1052 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001053
Christoph M. Wintersteiger21411d22019-02-06 18:06:15 +00001054 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
Christoph M. Wintersteiger0bc9c692018-10-25 12:47:18 +01001055 );
1056
1057 mbedtls_ecdh_free( &ecdh_srv );
1058 mbedtls_ecdh_free( &ecdh_cli );
1059 }
1060 }
1061#endif
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1066 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001067#endif
1068
Paul Bakkercce9d772011-11-18 14:26:47 +00001069#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 fflush( stdout ); getchar();
1072#endif
1073
1074 return( 0 );
1075}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_TIMING_C */