blob: 60363933b2be2a45f8192419124e463a5342c138 [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");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040046 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000047}
48#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010049
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000050#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020051#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/md4.h"
56#include "mbedtls/md5.h"
57#include "mbedtls/ripemd160.h"
58#include "mbedtls/sha1.h"
59#include "mbedtls/sha256.h"
60#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/arc4.h"
63#include "mbedtls/des.h"
64#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010065#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/blowfish.h"
67#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030068#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/gcm.h"
70#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020071#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010072#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030073#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/havege.h"
76#include "mbedtls/ctr_drbg.h"
77#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010078
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/rsa.h"
80#include "mbedtls/dhm.h"
81#include "mbedtls/ecdsa.h"
82#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010083
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000088#endif
89
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000090/*
91 * For heap usage estimates, we need an estimate of the overhead per allocated
92 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
93 * so use that as our baseline.
94 */
95#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
96
97/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020098 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000099 */
100#define HEAP_SIZE (1u << 16) // 64k
101
Paul Bakker02faf452011-11-29 11:23:58 +0000102#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100103#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100104#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000105
Rich Evans85b05ec2015-02-12 11:37:29 +0000106#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200107 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300108 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200109 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300110 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000111 "havege, ctr_drbg, hmac_drbg\n" \
112 "rsa, dhm, ecdsa, ecdh.\n"
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000115#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100116 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000118#else
119#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000121#endif
122
123#define TIME_AND_TSC( TITLE, CODE ) \
124do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200125 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200126 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000127 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200128 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000129 fflush( stdout ); \
130 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200131 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200132 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000133 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200134 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135 } \
136 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200137 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200138 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000139 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200140 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000141 } \
142 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200143 if( ret != 0 ) \
144 { \
145 PRINT_ERROR; \
146 } \
147 else \
148 { \
149 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
150 ii * BUFSIZE / 1024, \
151 ( mbedtls_timing_hardclock() - tsc ) \
152 / ( jj * BUFSIZE ) ); \
153 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000154} while( 0 )
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100157
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400158/* How much space to reserve for the title when printing heap usage results.
159 * Updated manually as the output of the following command:
160 *
161 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
162 * awk '{print length+2}' | sort -rn | head -n1
163 *
164 * This computes the maximum length of a title +2 (because we appends "/s").
165 * (If the value is too small, the only consequence is poor alignement.) */
166#define TITLE_SPACE 11
167
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100168#define MEMORY_MEASURE_INIT \
169 size_t max_used, max_blocks, max_bytes; \
170 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200171 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100173
174#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200175 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400176 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
177 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100178 max_used -= prv_used; \
179 max_blocks -= prv_blocks; \
180 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100182
183#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000184#define MEMORY_MEASURE_INIT
185#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100186#endif
187
Rich Evans85b05ec2015-02-12 11:37:29 +0000188#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
189do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200190 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000191 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100192 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000193 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200194 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000195 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200196 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000197 \
198 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200199 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000200 { \
201 CODE; \
202 } \
203 \
204 if( ret != 0 ) \
205 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100206 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000207 } \
208 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100209 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200210 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100211 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200212 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100213 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000214} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Paul Bakkera3d195c2011-11-27 21:07:34 +0000216static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000218 size_t use_len;
219 int rnd;
220
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 if( rng_state != NULL )
222 rng_state = NULL;
223
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224 while( len > 0 )
225 {
226 use_len = len;
227 if( use_len > sizeof(int) )
228 use_len = sizeof(int);
229
230 rnd = rand();
231 memcpy( output, &rnd, use_len );
232 output += use_len;
233 len -= use_len;
234 }
235
236 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237}
238
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100239/*
240 * Clear some memory that was used to prepare the context
241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_ECP_C)
243void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100244{
245 if( grp->T != NULL )
246 {
247 size_t i;
248 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_ecp_point_free( &grp->T[i] );
250 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100251 }
252 grp->T = NULL;
253 grp->T_size = 0;
254}
255#else
256#define ecp_clear_precomputed( g )
257#endif
258
Paul Bakker5121ce52009-01-03 21:22:43 +0000259unsigned char buf[BUFSIZE];
260
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200261typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200262 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100263 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200264 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200265 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200266 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300267 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100268 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200269 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270} todo_list;
271
Jarno Lamsa642596e2019-10-04 12:52:42 +0300272#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
273int mbedtls_hardware_poll( void *data, unsigned char *output,
274 size_t len, size_t *olen )
275{
276 size_t i;
277 (void) data;
278 for( i = 0; i < len; ++i )
279 output[i] = rand();
280 *olen = len;
281 return( 0 );
282}
283#endif
Simon Butcher63cb97e2018-12-06 17:43:31 +0000284
Paul Bakkercce9d772011-11-18 14:26:47 +0000285int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000286{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100287 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200288 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200289 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200290 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200292 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000293#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000294
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000295 if( argc <= 1 )
296 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000298 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200299 else
300 {
301 memset( &todo, 0, sizeof( todo ) );
302
303 for( i = 1; i < argc; i++ )
304 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200305 if( strcmp( argv[i], "md4" ) == 0 )
306 todo.md4 = 1;
307 else if( strcmp( argv[i], "md5" ) == 0 )
308 todo.md5 = 1;
309 else if( strcmp( argv[i], "ripemd160" ) == 0 )
310 todo.ripemd160 = 1;
311 else if( strcmp( argv[i], "sha1" ) == 0 )
312 todo.sha1 = 1;
313 else if( strcmp( argv[i], "sha256" ) == 0 )
314 todo.sha256 = 1;
315 else if( strcmp( argv[i], "sha512" ) == 0 )
316 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200317 else if( strcmp( argv[i], "arc4" ) == 0 )
318 todo.arc4 = 1;
319 else if( strcmp( argv[i], "des3" ) == 0 )
320 todo.des3 = 1;
321 else if( strcmp( argv[i], "des" ) == 0 )
322 todo.des = 1;
323 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
324 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200325 else if( strcmp( argv[i], "aes_xts" ) == 0 )
326 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
328 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200329 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
330 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200331 else if( strcmp( argv[i], "chachapoly" ) == 0 )
332 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100333 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
334 todo.aes_cmac = 1;
335 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
336 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100337 else if( strcmp( argv[i], "aria" ) == 0 )
338 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200339 else if( strcmp( argv[i], "camellia" ) == 0 )
340 todo.camellia = 1;
341 else if( strcmp( argv[i], "blowfish" ) == 0 )
342 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300343 else if( strcmp( argv[i], "chacha20" ) == 0 )
344 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300345 else if( strcmp( argv[i], "poly1305" ) == 0 )
346 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200347 else if( strcmp( argv[i], "havege" ) == 0 )
348 todo.havege = 1;
349 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
350 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100351 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
352 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200353 else if( strcmp( argv[i], "rsa" ) == 0 )
354 todo.rsa = 1;
355 else if( strcmp( argv[i], "dhm" ) == 0 )
356 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200357 else if( strcmp( argv[i], "ecdsa" ) == 0 )
358 todo.ecdsa = 1;
359 else if( strcmp( argv[i], "ecdh" ) == 0 )
360 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200361 else
362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
364 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200365 }
366 }
367 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200372 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000373#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200375 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200378 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100379 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380#endif
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200383 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100384 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385#endif
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200388 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100389 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100390#endif
391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200393 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100394 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395#endif
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200398 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100399 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400#endif
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200403 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000405#endif
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200408 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_arc4_context arc4;
411 mbedtls_arc4_init( &arc4 );
412 mbedtls_arc4_setup( &arc4, tmp, 32 );
413 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
414 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000416#endif
417
Simon Butcher549dc3d2016-10-05 14:14:19 +0100418#if defined(MBEDTLS_DES_C)
419#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200420 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_des3_context des3;
423 mbedtls_des3_init( &des3 );
424 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
427 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200428 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200430 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_des_context des;
433 mbedtls_des_init( &des );
434 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200435 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
437 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200438 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100439
440#endif /* MBEDTLS_CIPHER_MODE_CBC */
441#if defined(MBEDTLS_CMAC_C)
442 if( todo.des3_cmac )
443 {
444 unsigned char output[8];
445 const mbedtls_cipher_info_t *cipher_info;
446
447 memset( buf, 0, sizeof( buf ) );
448 memset( tmp, 0, sizeof( tmp ) );
449
450 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
451
452 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100453 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
454 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100455 }
456#endif /* MBEDTLS_CMAC_C */
457#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_AES_C)
460#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200461 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100463 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_aes_context aes;
465 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200466 for( keysize = 128; keysize <= 256; keysize += 64 )
467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200470 memset( buf, 0, sizeof( buf ) );
471 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200474 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200479#endif
Aorimn5f778012016-06-09 23:22:58 +0200480#if defined(MBEDTLS_CIPHER_MODE_XTS)
481 if( todo.aes_xts )
482 {
483 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100484 mbedtls_aes_xts_context ctx;
485
486 mbedtls_aes_xts_init( &ctx );
487 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200488 {
489 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
490
491 memset( buf, 0, sizeof( buf ) );
492 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100493 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200494
495 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100496 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
497 tmp, buf, buf ) );
498
499 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200500 }
Aorimn5f778012016-06-09 23:22:58 +0200501 }
502#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200504 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000505 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100506 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200508
509 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +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-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000513
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200514 memset( buf, 0, sizeof( buf ) );
515 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200516 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000517
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200518 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200520 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000524 }
525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200527 if( todo.aes_ccm )
528 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100529 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200531
532 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200533 for( keysize = 128; keysize <= 256; keysize += 64 )
534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200536
537 memset( buf, 0, sizeof( buf ) );
538 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200539 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200540
541 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200543 12, NULL, 0, buf, buf, tmp, 16 ) );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200546 }
547 }
548#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200549#if defined(MBEDTLS_CHACHAPOLY_C)
550 if( todo.chachapoly )
551 {
552 mbedtls_chachapoly_context chachapoly;
553
554 mbedtls_chachapoly_init( &chachapoly );
555 memset( buf, 0, sizeof( buf ) );
556 memset( tmp, 0, sizeof( tmp ) );
557
558 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
559
560 mbedtls_chachapoly_setkey( &chachapoly, tmp );
561
562 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200563 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
564 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200565
566 mbedtls_chachapoly_free( &chachapoly );
567 }
568#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100569#if defined(MBEDTLS_CMAC_C)
570 if( todo.aes_cmac )
571 {
572 unsigned char output[16];
573 const mbedtls_cipher_info_t *cipher_info;
574 mbedtls_cipher_type_t cipher_type;
575 int keysize;
576
577 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
578 keysize <= 256;
579 keysize += 64, cipher_type++ )
580 {
581 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
582
583 memset( buf, 0, sizeof( buf ) );
584 memset( tmp, 0, sizeof( tmp ) );
585
586 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
587
588 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100589 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
590 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100591 }
592
593 memset( buf, 0, sizeof( buf ) );
594 memset( tmp, 0, sizeof( tmp ) );
595 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100596 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
597 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100598 }
599#endif /* MBEDTLS_CMAC_C */
600#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100602#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
603 if( todo.aria )
604 {
605 int keysize;
606 mbedtls_aria_context aria;
607 mbedtls_aria_init( &aria );
608 for( keysize = 128; keysize <= 256; keysize += 64 )
609 {
610 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
611
612 memset( buf, 0, sizeof( buf ) );
613 memset( tmp, 0, sizeof( tmp ) );
614 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
615
616 TIME_AND_TSC( title,
617 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
618 BUFSIZE, tmp, buf, buf ) );
619 }
620 mbedtls_aria_free( &aria );
621 }
622#endif
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200625 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000626 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100627 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_camellia_context camellia;
629 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200630 for( keysize = 128; keysize <= 256; keysize += 64 )
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000633
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200634 memset( buf, 0, sizeof( buf ) );
635 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000637
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200638 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200640 BUFSIZE, tmp, buf, buf ) );
641 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000643 }
644#endif
645
Daniel King34b822c2016-05-15 17:28:08 -0300646#if defined(MBEDTLS_CHACHA20_C)
647 if ( todo.chacha20 )
648 {
649 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
650 }
651#endif
652
Daniel Kingadc32c02016-05-16 18:25:45 -0300653#if defined(MBEDTLS_POLY1305_C)
654 if ( todo.poly1305 )
655 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200656 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300657 }
658#endif
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200661 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000662 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100663 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_blowfish_context blowfish;
665 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200666
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200667 for( keysize = 128; keysize <= 256; keysize += 64 )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000670
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200671 memset( buf, 0, sizeof( buf ) );
672 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000674
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200675 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200677 tmp, buf, buf ) );
678 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000681 }
682#endif
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200685 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_havege_state hs;
688 mbedtls_havege_init( &hs );
689 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
690 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200691 }
Paul Bakker02faf452011-11-29 11:23:58 +0000692#endif
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200695 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000698
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200699 mbedtls_ctr_drbg_init( &ctr_drbg );
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);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200702 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200703 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskine216040d2019-10-28 17:28:46 +0100704 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000705
Gilles Peskine216040d2019-10-28 17:28:46 +0100706 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200707 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_exit(1);
709 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200710 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200711 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200713 }
Paul Bakker02faf452011-11-29 11:23:58 +0000714#endif
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100717 if( todo.hmac_drbg )
718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_hmac_drbg_context hmac_drbg;
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100720 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100721
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200722 mbedtls_hmac_drbg_init( &hmac_drbg );
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_SHA1_C)
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100725 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
726 MBEDTLS_MD_INVALID_HANDLE )
727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_exit(1);
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100729 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100730
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200731 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100733 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200734 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100735
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200736 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_exit(1);
738 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
739 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100740 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
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#endif
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#if defined(MBEDTLS_SHA256_C)
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100745 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) ==
746 MBEDTLS_MD_INVALID_HANDLE )
747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_exit(1);
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100749 }
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100750
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200751 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100753 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200754 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100755
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200756 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_exit(1);
758 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
759 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100760 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200761 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100762#endif
Ron Eldor278af452018-06-20 18:40:21 +0300763 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100764 }
765#endif
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200768 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100770 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200772 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
777 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200778
779 TIME_PUBLIC( title, " public",
780 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200782
783 TIME_PUBLIC( title, "private",
784 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200788 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000790#endif
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200793 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100794 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200795 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800796 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100797 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800798 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100799 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800800 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100801 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800802 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100803 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
804
805 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
806 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
807 sizeof( dhm_P_3072 ) };
808
809 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
810 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
811 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200814 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200815 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200818
Hanno Beckerb9539212017-10-04 13:13:34 +0100819 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
820 dhm_P_size[i] ) != 0 ||
821 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
822 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200825 }
826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 dhm.len = mbedtls_mpi_size( &dhm.P );
828 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
829 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
830 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200833 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200835 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100836 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200839 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100840 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200843 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100844 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100845#endif
846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200848 if( todo.ecdsa )
849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_ecdsa_context ecdsa;
851 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200852 size_t sig_len;
853
854 memset( buf, 0x2A, sizeof( buf ) );
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 for( curve_info = mbedtls_ecp_curve_list();
857 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200858 curve_info++ )
859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
863 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100864 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200867 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200868 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200869 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200870 tmp, &sig_len, myrand, NULL ) );
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100873 }
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 for( curve_info = mbedtls_ecp_curve_list();
876 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100877 curve_info++ )
878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200882 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100883 tmp, &sig_len, myrand, NULL ) != 0 )
884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100886 }
887 ecp_clear_precomputed( &ecdsa.grp );
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100890 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200891 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200892 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200893 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200896 }
897 }
898#endif
899
Janos Follath52735ef2018-08-15 10:19:16 +0100900#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200901 if( todo.ecdh )
902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000905 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
906#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
907 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000908#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000909#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
910 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
911#endif
912 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
913 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200915 size_t olen;
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 for( curve_info = mbedtls_ecp_curve_list();
918 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200919 curve_info++ )
920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200922
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200923 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200925 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200929 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100930 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200933 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200934 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200936 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200938 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100940 }
941
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000942 /* Montgomery curves need to be handled separately */
943 for ( curve_info = montgomery_curve_list;
944 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
945 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000946 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000947 mbedtls_ecdh_init( &ecdh );
948 mbedtls_mpi_init( &z );
949
950 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
951 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
952 {
953 mbedtls_exit( 1 );
954 }
955
956 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
957 curve_info->name );
958 TIME_PUBLIC( title, "handshake",
959 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
960 myrand, NULL );
961 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
962 myrand, NULL ) );
963
964 mbedtls_ecdh_free( &ecdh );
965 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000966 }
967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 for( curve_info = mbedtls_ecp_curve_list();
969 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100970 curve_info++ )
971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100973
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200974 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100976 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
978 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100979 myrand, NULL ) != 0 )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100982 }
983 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200986 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200987 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200989 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200991 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000992
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000993 /* Montgomery curves need to be handled separately */
994 for ( curve_info = montgomery_curve_list;
995 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
996 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000997 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000998 mbedtls_ecdh_init( &ecdh );
999 mbedtls_mpi_init( &z );
1000
1001 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
1002 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1003 myrand, NULL ) != 0 ||
1004 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
1005 {
1006 mbedtls_exit( 1 );
1007 }
1008
1009 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1010 curve_info->name );
1011 TIME_PUBLIC( title, "handshake",
1012 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1013 myrand, NULL ) );
1014
1015 mbedtls_ecdh_free( &ecdh );
1016 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001017 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001018 }
1019#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1024 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001025#endif
1026
Paul Bakkercce9d772011-11-18 14:26:47 +00001027#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 fflush( stdout ); getchar();
1030#endif
1031
Andrzej Kurek825ebd42020-05-18 11:47:25 -04001032 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_TIMING_C */