blob: 421aa112a4299606d13e4ae8b845c6155c2f0a19 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_exit exit
61#define mbedtls_printf printf
62#define mbedtls_snprintf snprintf
63#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010064#define mbedtls_exit exit
65#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
66#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000070int main( void )
71{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020073 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000074}
75#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010076
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000077#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020078#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010079
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/md4.h"
83#include "mbedtls/md5.h"
84#include "mbedtls/ripemd160.h"
85#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010088
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000089#include "mbedtls/arc4.h"
90#include "mbedtls/des.h"
91#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010092#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000093#include "mbedtls/blowfish.h"
94#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030095#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000096#include "mbedtls/gcm.h"
97#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020098#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010099#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -0300100#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100101
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000102#include "mbedtls/havege.h"
103#include "mbedtls/ctr_drbg.h"
104#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100105
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000106#include "mbedtls/rsa.h"
107#include "mbedtls/dhm.h"
108#include "mbedtls/ecdsa.h"
109#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100110
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000111#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000114#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +0000115#endif
116
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000117/*
118 * For heap usage estimates, we need an estimate of the overhead per allocated
119 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
120 * so use that as our baseline.
121 */
122#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
123
124/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200125 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000126 */
127#define HEAP_SIZE (1u << 16) // 64k
128
Paul Bakker02faf452011-11-29 11:23:58 +0000129#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100130#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100131#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000132
Rich Evans85b05ec2015-02-12 11:37:29 +0000133#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200134 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300135 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200136 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300137 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000138 "havege, ctr_drbg, hmac_drbg\n" \
139 "rsa, dhm, ecdsa, ecdh.\n"
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000142#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100143 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000145#else
146#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000148#endif
149
150#define TIME_AND_TSC( TITLE, CODE ) \
151do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200152 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200153 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000154 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200155 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000156 fflush( stdout ); \
157 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200158 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200159 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000160 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200161 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000162 } \
163 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200164 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200165 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000166 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200167 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000168 } \
169 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200170 if( ret != 0 ) \
171 { \
172 PRINT_ERROR; \
173 } \
174 else \
175 { \
176 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
177 ii * BUFSIZE / 1024, \
178 ( mbedtls_timing_hardclock() - tsc ) \
179 / ( jj * BUFSIZE ) ); \
180 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000181} while( 0 )
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100184
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200185/* How much space to reserve for the title when printing heap usage results.
186 * Updated manually as the output of the following command:
187 *
188 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
189 * awk '{print length+2}' | sort -rn | head -n1
190 *
191 * This computes the maximum length of a title +2 (because we appends "/s").
192 * (If the value is too small, the only consequence is poor alignement.) */
193#define TITLE_SPACE 11
194
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100195#define MEMORY_MEASURE_INIT \
196 size_t max_used, max_blocks, max_bytes; \
197 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200198 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100200
201#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200202 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200203 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
204 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100205 max_used -= prv_used; \
206 max_blocks -= prv_blocks; \
207 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100209
210#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000211#define MEMORY_MEASURE_INIT
212#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100213#endif
214
Rich Evans85b05ec2015-02-12 11:37:29 +0000215#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
216do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200217 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000218 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100219 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000220 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200221 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000222 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200223 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000224 \
225 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200226 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000227 { \
228 CODE; \
229 } \
230 \
231 if( ret != 0 ) \
232 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100233 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000234 } \
235 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100236 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200237 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100238 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200239 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100240 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000241} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Paul Bakkera3d195c2011-11-27 21:07:34 +0000243static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000244{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000245 size_t use_len;
246 int rnd;
247
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 if( rng_state != NULL )
249 rng_state = NULL;
250
Paul Bakkera3d195c2011-11-27 21:07:34 +0000251 while( len > 0 )
252 {
253 use_len = len;
254 if( use_len > sizeof(int) )
255 use_len = sizeof(int);
256
257 rnd = rand();
258 memcpy( output, &rnd, use_len );
259 output += use_len;
260 len -= use_len;
261 }
262
263 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264}
265
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100266/*
267 * Clear some memory that was used to prepare the context
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_ECP_C)
270void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100271{
272 if( grp->T != NULL )
273 {
274 size_t i;
275 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ecp_point_free( &grp->T[i] );
277 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100278 }
279 grp->T = NULL;
280 grp->T_size = 0;
281}
282#else
283#define ecp_clear_precomputed( g )
284#endif
285
Paul Bakker5121ce52009-01-03 21:22:43 +0000286unsigned char buf[BUFSIZE];
287
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200289 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100290 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200291 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200292 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200293 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300294 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100295 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200296 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297} todo_list;
298
Simon Butcher63cb97e2018-12-06 17:43:31 +0000299
Paul Bakkercce9d772011-11-18 14:26:47 +0000300int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000301{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100302 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200303 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200304 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200305 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200307 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000308#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000309
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000310 if( argc <= 1 )
311 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200312 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000313 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200314 else
315 {
316 memset( &todo, 0, sizeof( todo ) );
317
318 for( i = 1; i < argc; i++ )
319 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200320 if( strcmp( argv[i], "md4" ) == 0 )
321 todo.md4 = 1;
322 else if( strcmp( argv[i], "md5" ) == 0 )
323 todo.md5 = 1;
324 else if( strcmp( argv[i], "ripemd160" ) == 0 )
325 todo.ripemd160 = 1;
326 else if( strcmp( argv[i], "sha1" ) == 0 )
327 todo.sha1 = 1;
328 else if( strcmp( argv[i], "sha256" ) == 0 )
329 todo.sha256 = 1;
330 else if( strcmp( argv[i], "sha512" ) == 0 )
331 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200332 else if( strcmp( argv[i], "arc4" ) == 0 )
333 todo.arc4 = 1;
334 else if( strcmp( argv[i], "des3" ) == 0 )
335 todo.des3 = 1;
336 else if( strcmp( argv[i], "des" ) == 0 )
337 todo.des = 1;
338 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
339 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200340 else if( strcmp( argv[i], "aes_xts" ) == 0 )
341 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
343 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200344 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
345 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200346 else if( strcmp( argv[i], "chachapoly" ) == 0 )
347 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100348 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
349 todo.aes_cmac = 1;
350 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
351 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100352 else if( strcmp( argv[i], "aria" ) == 0 )
353 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200354 else if( strcmp( argv[i], "camellia" ) == 0 )
355 todo.camellia = 1;
356 else if( strcmp( argv[i], "blowfish" ) == 0 )
357 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300358 else if( strcmp( argv[i], "chacha20" ) == 0 )
359 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300360 else if( strcmp( argv[i], "poly1305" ) == 0 )
361 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200362 else if( strcmp( argv[i], "havege" ) == 0 )
363 todo.havege = 1;
364 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
365 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100366 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
367 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200368 else if( strcmp( argv[i], "rsa" ) == 0 )
369 todo.rsa = 1;
370 else if( strcmp( argv[i], "dhm" ) == 0 )
371 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200372 else if( strcmp( argv[i], "ecdsa" ) == 0 )
373 todo.ecdsa = 1;
374 else if( strcmp( argv[i], "ecdh" ) == 0 )
375 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200376 else
377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
379 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200380 }
381 }
382 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200387 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000388#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200389 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200390 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200393 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100394 TIME_AND_TSC( "MD4", mbedtls_md4_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_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200398 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100399 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400#endif
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200403 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100404 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100405#endif
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200408 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100409 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410#endif
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200413 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100414 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415#endif
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200418 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100419 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000420#endif
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200423 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_arc4_context arc4;
426 mbedtls_arc4_init( &arc4 );
427 mbedtls_arc4_setup( &arc4, tmp, 32 );
428 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
429 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200430 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000431#endif
432
Simon Butcher549dc3d2016-10-05 14:14:19 +0100433#if defined(MBEDTLS_DES_C)
434#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200435 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_des3_context des3;
438 mbedtls_des3_init( &des3 );
439 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200440 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
442 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200443 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200445 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_des_context des;
448 mbedtls_des_init( &des );
449 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200450 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
452 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100454
455#endif /* MBEDTLS_CIPHER_MODE_CBC */
456#if defined(MBEDTLS_CMAC_C)
457 if( todo.des3_cmac )
458 {
459 unsigned char output[8];
460 const mbedtls_cipher_info_t *cipher_info;
461
462 memset( buf, 0, sizeof( buf ) );
463 memset( tmp, 0, sizeof( tmp ) );
464
465 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
466
467 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100468 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
469 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100470 }
471#endif /* MBEDTLS_CMAC_C */
472#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_AES_C)
475#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200476 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100478 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_aes_context aes;
480 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200481 for( keysize = 128; keysize <= 256; keysize += 64 )
482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200485 memset( buf, 0, sizeof( buf ) );
486 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200489 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200491 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200494#endif
Aorimn5f778012016-06-09 23:22:58 +0200495#if defined(MBEDTLS_CIPHER_MODE_XTS)
496 if( todo.aes_xts )
497 {
498 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100499 mbedtls_aes_xts_context ctx;
500
501 mbedtls_aes_xts_init( &ctx );
502 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200503 {
504 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
505
506 memset( buf, 0, sizeof( buf ) );
507 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100508 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200509
510 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100511 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
512 tmp, buf, buf ) );
513
514 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200515 }
Aorimn5f778012016-06-09 23:22:58 +0200516 }
517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200519 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000520 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100521 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200523
524 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200525 for( keysize = 128; keysize <= 256; keysize += 64 )
526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000528
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200529 memset( buf, 0, sizeof( buf ) );
530 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200531 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000532
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200533 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200535 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200538 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000539 }
540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200542 if( todo.aes_ccm )
543 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100544 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200546
547 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200548 for( keysize = 128; keysize <= 256; keysize += 64 )
549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200551
552 memset( buf, 0, sizeof( buf ) );
553 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200554 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200555
556 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200558 12, NULL, 0, buf, buf, tmp, 16 ) );
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200561 }
562 }
563#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200564#if defined(MBEDTLS_CHACHAPOLY_C)
565 if( todo.chachapoly )
566 {
567 mbedtls_chachapoly_context chachapoly;
568
569 mbedtls_chachapoly_init( &chachapoly );
570 memset( buf, 0, sizeof( buf ) );
571 memset( tmp, 0, sizeof( tmp ) );
572
573 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
574
575 mbedtls_chachapoly_setkey( &chachapoly, tmp );
576
577 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200578 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
579 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200580
581 mbedtls_chachapoly_free( &chachapoly );
582 }
583#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100584#if defined(MBEDTLS_CMAC_C)
585 if( todo.aes_cmac )
586 {
587 unsigned char output[16];
588 const mbedtls_cipher_info_t *cipher_info;
589 mbedtls_cipher_type_t cipher_type;
590 int keysize;
591
592 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
593 keysize <= 256;
594 keysize += 64, cipher_type++ )
595 {
596 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
597
598 memset( buf, 0, sizeof( buf ) );
599 memset( tmp, 0, sizeof( tmp ) );
600
601 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
602
603 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100604 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
605 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100606 }
607
608 memset( buf, 0, sizeof( buf ) );
609 memset( tmp, 0, sizeof( tmp ) );
610 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100611 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
612 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100613 }
614#endif /* MBEDTLS_CMAC_C */
615#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100617#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
618 if( todo.aria )
619 {
620 int keysize;
621 mbedtls_aria_context aria;
622 mbedtls_aria_init( &aria );
623 for( keysize = 128; keysize <= 256; keysize += 64 )
624 {
625 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
626
627 memset( buf, 0, sizeof( buf ) );
628 memset( tmp, 0, sizeof( tmp ) );
629 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
630
631 TIME_AND_TSC( title,
632 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
633 BUFSIZE, tmp, buf, buf ) );
634 }
635 mbedtls_aria_free( &aria );
636 }
637#endif
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200640 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000641 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100642 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_camellia_context camellia;
644 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200645 for( keysize = 128; keysize <= 256; keysize += 64 )
646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000648
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200649 memset( buf, 0, sizeof( buf ) );
650 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000652
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200653 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200655 BUFSIZE, tmp, buf, buf ) );
656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000658 }
659#endif
660
Daniel King34b822c2016-05-15 17:28:08 -0300661#if defined(MBEDTLS_CHACHA20_C)
662 if ( todo.chacha20 )
663 {
664 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
665 }
666#endif
667
Daniel Kingadc32c02016-05-16 18:25:45 -0300668#if defined(MBEDTLS_POLY1305_C)
669 if ( todo.poly1305 )
670 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200671 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300672 }
673#endif
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200676 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000677 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100678 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_blowfish_context blowfish;
680 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200681
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200682 for( keysize = 128; keysize <= 256; keysize += 64 )
683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000685
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200686 memset( buf, 0, sizeof( buf ) );
687 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000689
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200690 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200692 tmp, buf, buf ) );
693 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000696 }
697#endif
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200700 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 mbedtls_havege_state hs;
703 mbedtls_havege_init( &hs );
704 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
705 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200706 }
Paul Bakker02faf452011-11-29 11:23:58 +0000707#endif
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200710 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000713
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200714 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200715 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200717 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200718 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100719 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000720
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100721 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200722 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_exit(1);
724 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200725 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200726 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200728 }
Paul Bakker02faf452011-11-29 11:23:58 +0000729#endif
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100732 if( todo.hmac_drbg )
733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_hmac_drbg_context hmac_drbg;
735 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100736
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200737 mbedtls_hmac_drbg_init( &hmac_drbg );
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SHA1_C)
740 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
741 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100742
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200743 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100745 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200746 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100747
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200748 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_exit(1);
750 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
751 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100752 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200753 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100754#endif
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SHA256_C)
757 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
758 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100759
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200760 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100762 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200763 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100764
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200765 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_exit(1);
767 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
768 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100769 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200770 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100771#endif
Ron Eldor278af452018-06-20 18:40:21 +0300772 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100773 }
774#endif
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200777 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100779 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200781 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
786 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200787
788 TIME_PUBLIC( title, " public",
789 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200791
792 TIME_PUBLIC( title, "private",
793 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200797 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000798 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000799#endif
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200802 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100803 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200804 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800805 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100806 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800807 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100808 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800809 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100810 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800811 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100812 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
813
814 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
815 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
816 sizeof( dhm_P_3072 ) };
817
818 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
819 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
820 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200823 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200824 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200827
Hanno Beckerb9539212017-10-04 13:13:34 +0100828 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
829 dhm_P_size[i] ) != 0 ||
830 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
831 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200834 }
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 dhm.len = mbedtls_mpi_size( &dhm.P );
837 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
838 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
839 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200842 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200844 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100845 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200848 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100849 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200852 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100853 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100854#endif
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200857 if( todo.ecdsa )
858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_ecdsa_context ecdsa;
860 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200861 size_t sig_len;
862
863 memset( buf, 0x2A, sizeof( buf ) );
864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 for( curve_info = mbedtls_ecp_curve_list();
866 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200867 curve_info++ )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
872 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100873 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200876 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200877 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200878 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200879 tmp, &sig_len, myrand, NULL ) );
880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100882 }
883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 for( curve_info = mbedtls_ecp_curve_list();
885 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100886 curve_info++ )
887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200891 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100892 tmp, &sig_len, myrand, NULL ) != 0 )
893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100895 }
896 ecp_clear_precomputed( &ecdsa.grp );
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100899 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200900 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200901 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200902 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200905 }
906 }
907#endif
908
Janos Follath52735ef2018-08-15 10:19:16 +0100909#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200910 if( todo.ecdh )
911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000914 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
915#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
916 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000917#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000918#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
919 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
920#endif
921 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
922 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200924 size_t olen;
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 for( curve_info = mbedtls_ecp_curve_list();
927 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200928 curve_info++ )
929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200931
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200932 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200934 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200938 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100939 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200942 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200943 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200945 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200947 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100949 }
950
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000951 /* Montgomery curves need to be handled separately */
952 for ( curve_info = montgomery_curve_list;
953 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
954 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000955 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000956 mbedtls_ecdh_init( &ecdh );
957 mbedtls_mpi_init( &z );
958
959 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
960 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
961 {
962 mbedtls_exit( 1 );
963 }
964
965 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
966 curve_info->name );
967 TIME_PUBLIC( title, "handshake",
968 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
969 myrand, NULL );
970 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
971 myrand, NULL ) );
972
973 mbedtls_ecdh_free( &ecdh );
974 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000975 }
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 for( curve_info = mbedtls_ecp_curve_list();
978 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100979 curve_info++ )
980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100982
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200983 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100985 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
987 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100988 myrand, NULL ) != 0 )
989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100991 }
992 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200995 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200996 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200998 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001000 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001001
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001002 /* Montgomery curves need to be handled separately */
1003 for ( curve_info = montgomery_curve_list;
1004 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1005 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001006 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001007 mbedtls_ecdh_init( &ecdh );
1008 mbedtls_mpi_init( &z );
1009
1010 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
1011 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1012 myrand, NULL ) != 0 ||
1013 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
1014 {
1015 mbedtls_exit( 1 );
1016 }
1017
1018 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1019 curve_info->name );
1020 TIME_PUBLIC( title, "handshake",
1021 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1022 myrand, NULL ) );
1023
1024 mbedtls_ecdh_free( &ecdh );
1025 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001026 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001027 }
1028#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1033 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001034#endif
1035
Paul Bakkercce9d772011-11-18 14:26:47 +00001036#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 fflush( stdout ); getchar();
1039#endif
1040
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001041 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#endif /* MBEDTLS_TIMING_C */