blob: a7a01074cf80d20a00577aa29241c38000e8e844 [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. Wintersteiger696deda2018-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 \
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
Simon Butcher63cb97e2018-12-06 17:43:31 +0000261
Paul Bakkercce9d772011-11-18 14:26:47 +0000262int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000263{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100264 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200265 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200266 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200267 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200269 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000270#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000271
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000272 if( argc <= 1 )
273 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200274 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000275 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200276 else
277 {
278 memset( &todo, 0, sizeof( todo ) );
279
280 for( i = 1; i < argc; i++ )
281 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200282 if( strcmp( argv[i], "md4" ) == 0 )
283 todo.md4 = 1;
284 else if( strcmp( argv[i], "md5" ) == 0 )
285 todo.md5 = 1;
286 else if( strcmp( argv[i], "ripemd160" ) == 0 )
287 todo.ripemd160 = 1;
288 else if( strcmp( argv[i], "sha1" ) == 0 )
289 todo.sha1 = 1;
290 else if( strcmp( argv[i], "sha256" ) == 0 )
291 todo.sha256 = 1;
292 else if( strcmp( argv[i], "sha512" ) == 0 )
293 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200294 else if( strcmp( argv[i], "arc4" ) == 0 )
295 todo.arc4 = 1;
296 else if( strcmp( argv[i], "des3" ) == 0 )
297 todo.des3 = 1;
298 else if( strcmp( argv[i], "des" ) == 0 )
299 todo.des = 1;
300 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
301 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200302 else if( strcmp( argv[i], "aes_xts" ) == 0 )
303 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200304 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
305 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200306 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
307 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200308 else if( strcmp( argv[i], "chachapoly" ) == 0 )
309 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100310 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
311 todo.aes_cmac = 1;
312 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
313 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100314 else if( strcmp( argv[i], "aria" ) == 0 )
315 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200316 else if( strcmp( argv[i], "camellia" ) == 0 )
317 todo.camellia = 1;
318 else if( strcmp( argv[i], "blowfish" ) == 0 )
319 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300320 else if( strcmp( argv[i], "chacha20" ) == 0 )
321 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300322 else if( strcmp( argv[i], "poly1305" ) == 0 )
323 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200324 else if( strcmp( argv[i], "havege" ) == 0 )
325 todo.havege = 1;
326 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
327 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100328 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
329 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200330 else if( strcmp( argv[i], "rsa" ) == 0 )
331 todo.rsa = 1;
332 else if( strcmp( argv[i], "dhm" ) == 0 )
333 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200334 else if( strcmp( argv[i], "ecdsa" ) == 0 )
335 todo.ecdsa = 1;
336 else if( strcmp( argv[i], "ecdh" ) == 0 )
337 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200338 else
339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
341 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 }
343 }
344 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200349 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000350#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200351 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200352 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200355 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100356 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357#endif
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200360 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100361 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362#endif
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200365 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100366 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100367#endif
368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200370 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100371 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372#endif
373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200375 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100376 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377#endif
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200380 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100381 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000382#endif
383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200385 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_arc4_context arc4;
388 mbedtls_arc4_init( &arc4 );
389 mbedtls_arc4_setup( &arc4, tmp, 32 );
390 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
391 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200392 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000393#endif
394
Simon Butcher549dc3d2016-10-05 14:14:19 +0100395#if defined(MBEDTLS_DES_C)
396#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200397 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_des3_context des3;
400 mbedtls_des3_init( &des3 );
401 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
404 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200405 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200407 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_des_context des;
410 mbedtls_des_init( &des );
411 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200412 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
414 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100416
417#endif /* MBEDTLS_CIPHER_MODE_CBC */
418#if defined(MBEDTLS_CMAC_C)
419 if( todo.des3_cmac )
420 {
421 unsigned char output[8];
422 const mbedtls_cipher_info_t *cipher_info;
423
424 memset( buf, 0, sizeof( buf ) );
425 memset( tmp, 0, sizeof( tmp ) );
426
427 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
428
429 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100430 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
431 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100432 }
433#endif /* MBEDTLS_CMAC_C */
434#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_AES_C)
437#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200438 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100440 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_aes_context aes;
442 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200443 for( keysize = 128; keysize <= 256; keysize += 64 )
444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200447 memset( buf, 0, sizeof( buf ) );
448 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200451 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200456#endif
Aorimn5f778012016-06-09 23:22:58 +0200457#if defined(MBEDTLS_CIPHER_MODE_XTS)
458 if( todo.aes_xts )
459 {
460 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100461 mbedtls_aes_xts_context ctx;
462
463 mbedtls_aes_xts_init( &ctx );
464 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200465 {
466 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
467
468 memset( buf, 0, sizeof( buf ) );
469 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100470 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200471
472 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100473 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
474 tmp, buf, buf ) );
475
476 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200477 }
Aorimn5f778012016-06-09 23:22:58 +0200478 }
479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200481 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000482 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100483 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200485
486 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200487 for( keysize = 128; keysize <= 256; keysize += 64 )
488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000490
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200491 memset( buf, 0, sizeof( buf ) );
492 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200493 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000494
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200495 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200497 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200500 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000501 }
502#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200504 if( todo.aes_ccm )
505 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100506 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200508
509 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200510 for( keysize = 128; keysize <= 256; keysize += 64 )
511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200513
514 memset( buf, 0, sizeof( buf ) );
515 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200516 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200517
518 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200520 12, NULL, 0, buf, buf, tmp, 16 ) );
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200523 }
524 }
525#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200526#if defined(MBEDTLS_CHACHAPOLY_C)
527 if( todo.chachapoly )
528 {
529 mbedtls_chachapoly_context chachapoly;
530
531 mbedtls_chachapoly_init( &chachapoly );
532 memset( buf, 0, sizeof( buf ) );
533 memset( tmp, 0, sizeof( tmp ) );
534
535 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
536
537 mbedtls_chachapoly_setkey( &chachapoly, tmp );
538
539 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200540 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
541 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200542
543 mbedtls_chachapoly_free( &chachapoly );
544 }
545#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100546#if defined(MBEDTLS_CMAC_C)
547 if( todo.aes_cmac )
548 {
549 unsigned char output[16];
550 const mbedtls_cipher_info_t *cipher_info;
551 mbedtls_cipher_type_t cipher_type;
552 int keysize;
553
554 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
555 keysize <= 256;
556 keysize += 64, cipher_type++ )
557 {
558 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
559
560 memset( buf, 0, sizeof( buf ) );
561 memset( tmp, 0, sizeof( tmp ) );
562
563 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
564
565 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100566 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
567 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100568 }
569
570 memset( buf, 0, sizeof( buf ) );
571 memset( tmp, 0, sizeof( tmp ) );
572 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100573 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
574 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100575 }
576#endif /* MBEDTLS_CMAC_C */
577#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100579#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
580 if( todo.aria )
581 {
582 int keysize;
583 mbedtls_aria_context aria;
584 mbedtls_aria_init( &aria );
585 for( keysize = 128; keysize <= 256; keysize += 64 )
586 {
587 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
588
589 memset( buf, 0, sizeof( buf ) );
590 memset( tmp, 0, sizeof( tmp ) );
591 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
592
593 TIME_AND_TSC( title,
594 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
595 BUFSIZE, tmp, buf, buf ) );
596 }
597 mbedtls_aria_free( &aria );
598 }
599#endif
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200602 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000603 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100604 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_camellia_context camellia;
606 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200607 for( keysize = 128; keysize <= 256; keysize += 64 )
608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000610
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200611 memset( buf, 0, sizeof( buf ) );
612 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000614
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200615 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200617 BUFSIZE, tmp, buf, buf ) );
618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000620 }
621#endif
622
Daniel King34b822c2016-05-15 17:28:08 -0300623#if defined(MBEDTLS_CHACHA20_C)
624 if ( todo.chacha20 )
625 {
626 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
627 }
628#endif
629
Daniel Kingadc32c02016-05-16 18:25:45 -0300630#if defined(MBEDTLS_POLY1305_C)
631 if ( todo.poly1305 )
632 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200633 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300634 }
635#endif
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200638 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000639 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100640 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_blowfish_context blowfish;
642 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200643
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200644 for( keysize = 128; keysize <= 256; keysize += 64 )
645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000647
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200648 memset( buf, 0, sizeof( buf ) );
649 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000651
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200652 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200654 tmp, buf, buf ) );
655 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000658 }
659#endif
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200662 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_havege_state hs;
665 mbedtls_havege_init( &hs );
666 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
667 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200668 }
Paul Bakker02faf452011-11-29 11:23:58 +0000669#endif
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200672 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000675
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200676 mbedtls_ctr_drbg_init( &ctr_drbg );
677
678 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200680 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200681 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Paul Bakker02faf452011-11-29 11:23:58 +0000682
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200683 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_exit(1);
685 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200686 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200687 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200689 }
Paul Bakker02faf452011-11-29 11:23:58 +0000690#endif
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100693 if( todo.hmac_drbg )
694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_hmac_drbg_context hmac_drbg;
696 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100697
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200698 mbedtls_hmac_drbg_init( &hmac_drbg );
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SHA1_C)
701 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
702 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100703
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200704 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100706 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200707 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100708
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200709 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_exit(1);
711 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
712 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100713 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200714 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100715#endif
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA256_C)
718 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == 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-256 (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-256 (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
Ron Eldor278af452018-06-20 18:40:21 +0300733 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100734 }
735#endif
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200738 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100740 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200742 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
747 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200748
749 TIME_PUBLIC( title, " public",
750 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200752
753 TIME_PUBLIC( title, "private",
754 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200758 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000760#endif
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200763 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100764 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200765 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800766 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100767 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800768 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100769 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800770 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100771 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800772 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100773 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
774
775 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
776 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
777 sizeof( dhm_P_3072 ) };
778
779 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
780 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
781 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200784 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200785 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200788
Hanno Beckerb9539212017-10-04 13:13:34 +0100789 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
790 dhm_P_size[i] ) != 0 ||
791 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
792 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200795 }
796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 dhm.len = mbedtls_mpi_size( &dhm.P );
798 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
799 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
800 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200803 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200805 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100806 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200809 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100810 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200813 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100814 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100815#endif
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200818 if( todo.ecdsa )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_ecdsa_context ecdsa;
821 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200822 size_t sig_len;
823
824 memset( buf, 0x2A, sizeof( buf ) );
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 for( curve_info = mbedtls_ecp_curve_list();
827 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200828 curve_info++ )
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
833 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100834 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200837 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200838 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200839 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200840 tmp, &sig_len, myrand, NULL ) );
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100843 }
844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 for( curve_info = mbedtls_ecp_curve_list();
846 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100847 curve_info++ )
848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200852 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100853 tmp, &sig_len, myrand, NULL ) != 0 )
854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100856 }
857 ecp_clear_precomputed( &ecdsa.grp );
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100860 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200861 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200862 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200863 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200866 }
867 }
868#endif
869
Janos Follath52735ef2018-08-15 10:19:16 +0100870#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200871 if( todo.ecdh )
872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000875 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
876#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
877 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000878#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000879#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
880 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
881#endif
882 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
883 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200885 size_t olen;
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 for( curve_info = mbedtls_ecp_curve_list();
888 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200889 curve_info++ )
890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200892
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200893 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200895 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200899 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100900 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200903 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200904 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200906 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200908 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100910 }
911
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000912 /* Montgomery curves need to be handled separately */
913 for ( curve_info = montgomery_curve_list;
914 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
915 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000916 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000917 mbedtls_ecdh_init( &ecdh );
918 mbedtls_mpi_init( &z );
919
920 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
921 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
922 {
923 mbedtls_exit( 1 );
924 }
925
926 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
927 curve_info->name );
928 TIME_PUBLIC( title, "handshake",
929 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
930 myrand, NULL );
931 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
932 myrand, NULL ) );
933
934 mbedtls_ecdh_free( &ecdh );
935 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000936 }
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 for( curve_info = mbedtls_ecp_curve_list();
939 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100940 curve_info++ )
941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100943
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200944 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100946 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
948 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100949 myrand, NULL ) != 0 )
950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100952 }
953 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200956 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200957 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200959 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200961 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000962
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000963 /* Montgomery curves need to be handled separately */
964 for ( curve_info = montgomery_curve_list;
965 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
966 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000967 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000968 mbedtls_ecdh_init( &ecdh );
969 mbedtls_mpi_init( &z );
970
971 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
972 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
973 myrand, NULL ) != 0 ||
974 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
975 {
976 mbedtls_exit( 1 );
977 }
978
979 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
980 curve_info->name );
981 TIME_PUBLIC( title, "handshake",
982 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
983 myrand, NULL ) );
984
985 mbedtls_ecdh_free( &ecdh );
986 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000987 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200988 }
989#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100990
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +0100991#if defined(MBEDTLS_ECDH_C) && !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
992 if( todo.ecdh )
993 {
994 mbedtls_ecdh_context ecdh_srv, ecdh_cli;
995 unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
996 const mbedtls_ecp_curve_info * curve_list = mbedtls_ecp_curve_list();
997 const mbedtls_ecp_curve_info *curve_info;
998 size_t olen;
999
1000 for( curve_info = curve_list;
1001 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1002 curve_info++ )
1003 {
1004 mbedtls_ecdh_init( &ecdh_srv );
1005 mbedtls_ecdh_init( &ecdh_cli );
1006 mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id );
1007 mbedtls_ecdh_setup( &ecdh_cli, curve_info->grp_id );
1008
1009 if( ecdh_srv.var == MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0 && (
1010 mbedtls_ecp_group_load( &ecdh_srv.ctx.mbed_ecdh.grp, curve_info->grp_id ) != 0 ||
1011 mbedtls_ecdh_gen_public( &ecdh_srv.ctx.mbed_ecdh.grp,
1012 &ecdh_srv.ctx.mbed_ecdh.d,
1013 &ecdh_srv.ctx.mbed_ecdh.Q, myrand, NULL ) != 0 ))
1014 mbedtls_exit( 1 );
1015
1016 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name );
Christoph M. Wintersteiger65bab972018-12-06 17:15:12 +00001017 TIME_PUBLIC( title, "full handshake",
Christoph M. Wintersteiger696deda2018-10-25 12:47:18 +01001018 const unsigned char * p_srv = buf_srv;
1019 ret |= mbedtls_ecdh_make_params( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL );
1020
1021 ret |= mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen );
1022 ret |= mbedtls_ecdh_make_public( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL );
1023
1024 ret |= mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen );
1025 ret |= mbedtls_ecdh_calc_secret( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL );
1026
1027 ret |= mbedtls_ecdh_calc_secret( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL );
1028 );
1029
1030 mbedtls_ecdh_free( &ecdh_srv );
1031 mbedtls_ecdh_free( &ecdh_cli );
1032 }
1033 }
1034#endif
1035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1039 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001040#endif
1041
Paul Bakkercce9d772011-11-18 14:26:47 +00001042#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 fflush( stdout ); getchar();
1045#endif
1046
1047 return( 0 );
1048}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050#endif /* MBEDTLS_TIMING_C */