blob: 7845bee306e70eaf9e7855196e194b56e7c8907f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Gilles Peskinec72255f2021-10-01 18:08:41 +020054#if !defined(MBEDTLS_PLATFORM_C)
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <stdio.h>
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_exit exit
58#define mbedtls_printf printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010060#define mbedtls_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000061#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000064int main( void )
65{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020067 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000068}
69#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010070
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000071#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020072#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010073
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
78#include "mbedtls/ripemd160.h"
79#include "mbedtls/sha1.h"
80#include "mbedtls/sha256.h"
81#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010082
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/arc4.h"
84#include "mbedtls/des.h"
85#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010086#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/blowfish.h"
88#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030089#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000090#include "mbedtls/gcm.h"
91#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020092#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010093#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030094#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010095
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000096#include "mbedtls/havege.h"
97#include "mbedtls/ctr_drbg.h"
98#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010099
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000100#include "mbedtls/rsa.h"
101#include "mbedtls/dhm.h"
102#include "mbedtls/ecdsa.h"
103#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100104
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000105#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000108#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +0000109#endif
110
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000111/*
112 * For heap usage estimates, we need an estimate of the overhead per allocated
113 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
114 * so use that as our baseline.
115 */
116#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
117
118/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200119 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000120 */
121#define HEAP_SIZE (1u << 16) // 64k
122
Paul Bakker02faf452011-11-29 11:23:58 +0000123#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100124#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100125#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000126
Rich Evans85b05ec2015-02-12 11:37:29 +0000127#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200128 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300129 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200130 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300131 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000132 "havege, ctr_drbg, hmac_drbg\n" \
133 "rsa, dhm, ecdsa, ecdh.\n"
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000136#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100137 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000139#else
140#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000142#endif
143
144#define TIME_AND_TSC( TITLE, CODE ) \
145do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200146 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200147 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000148 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200149 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000150 fflush( stdout ); \
151 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200152 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200153 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000154 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200155 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000156 } \
157 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200158 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200159 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
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é-Gonnard51d7cfe2018-06-25 11:19:51 +0200164 if( ret != 0 ) \
165 { \
166 PRINT_ERROR; \
167 } \
168 else \
169 { \
170 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
171 ii * BUFSIZE / 1024, \
172 ( mbedtls_timing_hardclock() - tsc ) \
173 / ( jj * BUFSIZE ) ); \
174 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000175} while( 0 )
176
Gilles Peskine621333f2021-07-07 21:08:28 +0200177#define CHECK_AND_CONTINUE( R ) \
178 { \
179 int CHECK_AND_CONTINUE_ret = ( R ); \
180 if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
181 mbedtls_printf( "Feature not supported. Skipping.\n" ); \
182 continue; \
183 } \
184 else if( CHECK_AND_CONTINUE_ret != 0 ) { \
185 mbedtls_exit( 1 ); \
186 } \
187 }
188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100190
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200191/* How much space to reserve for the title when printing heap usage results.
192 * Updated manually as the output of the following command:
193 *
194 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
195 * awk '{print length+2}' | sort -rn | head -n1
196 *
197 * This computes the maximum length of a title +2 (because we appends "/s").
198 * (If the value is too small, the only consequence is poor alignement.) */
199#define TITLE_SPACE 11
200
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100201#define MEMORY_MEASURE_INIT \
202 size_t max_used, max_blocks, max_bytes; \
203 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200204 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100206
207#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200208 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200209 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
210 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100211 max_used -= prv_used; \
212 max_blocks -= prv_blocks; \
213 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100215
216#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000217#define MEMORY_MEASURE_INIT
218#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100219#endif
220
Rich Evans85b05ec2015-02-12 11:37:29 +0000221#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
222do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200223 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000224 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100225 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000226 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200227 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000228 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200229 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000230 \
231 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200232 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000233 { \
234 CODE; \
235 } \
236 \
237 if( ret != 0 ) \
238 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100239 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000240 } \
241 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100242 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200243 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100244 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200245 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100246 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000247} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Paul Bakkera3d195c2011-11-27 21:07:34 +0000249static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000250{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000251 size_t use_len;
252 int rnd;
253
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 if( rng_state != NULL )
255 rng_state = NULL;
256
Paul Bakkera3d195c2011-11-27 21:07:34 +0000257 while( len > 0 )
258 {
259 use_len = len;
260 if( use_len > sizeof(int) )
261 use_len = sizeof(int);
262
263 rnd = rand();
264 memcpy( output, &rnd, use_len );
265 output += use_len;
266 len -= use_len;
267 }
268
269 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270}
271
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100272/*
273 * Clear some memory that was used to prepare the context
274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_ECP_C)
276void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100277{
278 if( grp->T != NULL )
279 {
280 size_t i;
281 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_ecp_point_free( &grp->T[i] );
283 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100284 }
285 grp->T = NULL;
286 grp->T_size = 0;
287}
288#else
289#define ecp_clear_precomputed( g )
290#endif
291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292unsigned char buf[BUFSIZE];
293
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200294typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200295 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100296 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200297 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200298 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200299 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300300 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100301 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200302 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200303} todo_list;
304
Simon Butcher63cb97e2018-12-06 17:43:31 +0000305
Paul Bakkercce9d772011-11-18 14:26:47 +0000306int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000307{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100308 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200309 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200310 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200311 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200313 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000314#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000315
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000316 if( argc <= 1 )
317 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200318 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000319 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200320 else
321 {
322 memset( &todo, 0, sizeof( todo ) );
323
324 for( i = 1; i < argc; i++ )
325 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200326 if( strcmp( argv[i], "md4" ) == 0 )
327 todo.md4 = 1;
328 else if( strcmp( argv[i], "md5" ) == 0 )
329 todo.md5 = 1;
330 else if( strcmp( argv[i], "ripemd160" ) == 0 )
331 todo.ripemd160 = 1;
332 else if( strcmp( argv[i], "sha1" ) == 0 )
333 todo.sha1 = 1;
334 else if( strcmp( argv[i], "sha256" ) == 0 )
335 todo.sha256 = 1;
336 else if( strcmp( argv[i], "sha512" ) == 0 )
337 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200338 else if( strcmp( argv[i], "arc4" ) == 0 )
339 todo.arc4 = 1;
340 else if( strcmp( argv[i], "des3" ) == 0 )
341 todo.des3 = 1;
342 else if( strcmp( argv[i], "des" ) == 0 )
343 todo.des = 1;
344 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
345 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200346 else if( strcmp( argv[i], "aes_xts" ) == 0 )
347 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200348 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
349 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200350 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
351 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200352 else if( strcmp( argv[i], "chachapoly" ) == 0 )
353 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100354 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
355 todo.aes_cmac = 1;
356 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
357 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100358 else if( strcmp( argv[i], "aria" ) == 0 )
359 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200360 else if( strcmp( argv[i], "camellia" ) == 0 )
361 todo.camellia = 1;
362 else if( strcmp( argv[i], "blowfish" ) == 0 )
363 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300364 else if( strcmp( argv[i], "chacha20" ) == 0 )
365 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300366 else if( strcmp( argv[i], "poly1305" ) == 0 )
367 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200368 else if( strcmp( argv[i], "havege" ) == 0 )
369 todo.havege = 1;
370 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
371 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100372 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
373 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 else if( strcmp( argv[i], "rsa" ) == 0 )
375 todo.rsa = 1;
376 else if( strcmp( argv[i], "dhm" ) == 0 )
377 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200378 else if( strcmp( argv[i], "ecdsa" ) == 0 )
379 todo.ecdsa = 1;
380 else if( strcmp( argv[i], "ecdh" ) == 0 )
381 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200382 else
383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
385 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200386 }
387 }
388 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200393 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000394#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200395 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200396 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200399 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100400 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401#endif
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200404 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100405 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406#endif
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200409 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100410 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100411#endif
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200414 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100415 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416#endif
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200419 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100420 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421#endif
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200424 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100425 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000426#endif
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200429 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_arc4_context arc4;
432 mbedtls_arc4_init( &arc4 );
433 mbedtls_arc4_setup( &arc4, tmp, 32 );
434 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
435 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200436 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000437#endif
438
Simon Butcher549dc3d2016-10-05 14:14:19 +0100439#if defined(MBEDTLS_DES_C)
440#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200441 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_des3_context des3;
444 mbedtls_des3_init( &des3 );
Gilles Peskine621333f2021-07-07 21:08:28 +0200445 if( mbedtls_des3_set3key_enc( &des3, tmp ) != 0 )
446 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200447 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
449 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200450 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200452 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_des_context des;
455 mbedtls_des_init( &des );
Gilles Peskine621333f2021-07-07 21:08:28 +0200456 if( mbedtls_des_setkey_enc( &des, tmp ) != 0 )
457 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200458 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
460 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200461 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100462
463#endif /* MBEDTLS_CIPHER_MODE_CBC */
464#if defined(MBEDTLS_CMAC_C)
465 if( todo.des3_cmac )
466 {
467 unsigned char output[8];
468 const mbedtls_cipher_info_t *cipher_info;
469
470 memset( buf, 0, sizeof( buf ) );
471 memset( tmp, 0, sizeof( tmp ) );
472
473 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
474
475 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100476 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
477 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100478 }
479#endif /* MBEDTLS_CMAC_C */
480#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_AES_C)
483#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200484 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100486 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_aes_context aes;
488 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200489 for( keysize = 128; keysize <= 256; keysize += 64 )
490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200493 memset( buf, 0, sizeof( buf ) );
494 memset( tmp, 0, sizeof( tmp ) );
Gilles Peskine621333f2021-07-07 21:08:28 +0200495 CHECK_AND_CONTINUE( mbedtls_aes_setkey_enc( &aes, tmp, keysize ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200497 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200502#endif
Aorimn5f778012016-06-09 23:22:58 +0200503#if defined(MBEDTLS_CIPHER_MODE_XTS)
504 if( todo.aes_xts )
505 {
506 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100507 mbedtls_aes_xts_context ctx;
508
509 mbedtls_aes_xts_init( &ctx );
510 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200511 {
512 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
513
514 memset( buf, 0, sizeof( buf ) );
515 memset( tmp, 0, sizeof( tmp ) );
Gilles Peskine621333f2021-07-07 21:08:28 +0200516 CHECK_AND_CONTINUE( mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 ) );
Aorimn5f778012016-06-09 23:22:58 +0200517
518 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100519 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
520 tmp, buf, buf ) );
521
522 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200523 }
Aorimn5f778012016-06-09 23:22:58 +0200524 }
525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200527 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000528 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100529 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200531
532 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +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-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000536
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200537 memset( buf, 0, sizeof( buf ) );
538 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200539 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000540
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200541 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200543 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200546 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000547 }
548#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200550 if( todo.aes_ccm )
551 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100552 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200554
555 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200556 for( keysize = 128; keysize <= 256; keysize += 64 )
557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200559
560 memset( buf, 0, sizeof( buf ) );
561 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200562 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200563
564 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200566 12, NULL, 0, buf, buf, tmp, 16 ) );
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200569 }
570 }
571#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200572#if defined(MBEDTLS_CHACHAPOLY_C)
573 if( todo.chachapoly )
574 {
575 mbedtls_chachapoly_context chachapoly;
576
577 mbedtls_chachapoly_init( &chachapoly );
578 memset( buf, 0, sizeof( buf ) );
579 memset( tmp, 0, sizeof( tmp ) );
580
581 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
582
583 mbedtls_chachapoly_setkey( &chachapoly, tmp );
584
585 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200586 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
587 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200588
589 mbedtls_chachapoly_free( &chachapoly );
590 }
591#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100592#if defined(MBEDTLS_CMAC_C)
593 if( todo.aes_cmac )
594 {
595 unsigned char output[16];
596 const mbedtls_cipher_info_t *cipher_info;
597 mbedtls_cipher_type_t cipher_type;
598 int keysize;
599
600 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
601 keysize <= 256;
602 keysize += 64, cipher_type++ )
603 {
604 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
605
606 memset( buf, 0, sizeof( buf ) );
607 memset( tmp, 0, sizeof( tmp ) );
608
609 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
610
611 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100612 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
613 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100614 }
615
616 memset( buf, 0, sizeof( buf ) );
617 memset( tmp, 0, sizeof( tmp ) );
618 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100619 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
620 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100621 }
622#endif /* MBEDTLS_CMAC_C */
623#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100625#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
626 if( todo.aria )
627 {
628 int keysize;
629 mbedtls_aria_context aria;
630 mbedtls_aria_init( &aria );
631 for( keysize = 128; keysize <= 256; keysize += 64 )
632 {
633 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
634
635 memset( buf, 0, sizeof( buf ) );
636 memset( tmp, 0, sizeof( tmp ) );
637 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
638
639 TIME_AND_TSC( title,
640 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
641 BUFSIZE, tmp, buf, buf ) );
642 }
643 mbedtls_aria_free( &aria );
644 }
645#endif
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200648 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000649 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100650 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_camellia_context camellia;
652 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200653 for( keysize = 128; keysize <= 256; keysize += 64 )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000656
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200657 memset( buf, 0, sizeof( buf ) );
658 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000660
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200661 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200663 BUFSIZE, tmp, buf, buf ) );
664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000666 }
667#endif
668
Daniel King34b822c2016-05-15 17:28:08 -0300669#if defined(MBEDTLS_CHACHA20_C)
670 if ( todo.chacha20 )
671 {
672 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
673 }
674#endif
675
Daniel Kingadc32c02016-05-16 18:25:45 -0300676#if defined(MBEDTLS_POLY1305_C)
677 if ( todo.poly1305 )
678 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200679 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300680 }
681#endif
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200684 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000685 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100686 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_blowfish_context blowfish;
688 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200689
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200690 for( keysize = 128; keysize <= 256; keysize += 64 )
691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000693
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200694 memset( buf, 0, sizeof( buf ) );
695 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000697
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200698 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200700 tmp, buf, buf ) );
701 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000704 }
705#endif
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200708 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_havege_state hs;
711 mbedtls_havege_init( &hs );
712 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
713 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200714 }
Paul Bakker02faf452011-11-29 11:23:58 +0000715#endif
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200718 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000721
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200722 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200723 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200725 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200726 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100727 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000728
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100729 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200730 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_exit(1);
732 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200733 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200734 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200736 }
Paul Bakker02faf452011-11-29 11:23:58 +0000737#endif
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100740 if( todo.hmac_drbg )
741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_hmac_drbg_context hmac_drbg;
743 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100744
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200745 mbedtls_hmac_drbg_init( &hmac_drbg );
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_SHA1_C)
748 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
749 mbedtls_exit(1);
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-1 (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-1 (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
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_SHA256_C)
765 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
766 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100767
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200768 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100770 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200771 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100772
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200773 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_exit(1);
775 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
776 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100777 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200778 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100779#endif
Ron Eldor278af452018-06-20 18:40:21 +0300780 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100781 }
782#endif
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200785 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100787 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200789 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
794 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200795
796 TIME_PUBLIC( title, " public",
797 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200799
800 TIME_PUBLIC( title, "private",
801 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200805 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000807#endif
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200810 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100811 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200812 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800813 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100814 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800815 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100816 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800817 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100818 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800819 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100820 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
821
822 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
823 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
824 sizeof( dhm_P_3072 ) };
825
826 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
827 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
828 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200831 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200832 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200835
Hanno Beckerb9539212017-10-04 13:13:34 +0100836 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
837 dhm_P_size[i] ) != 0 ||
838 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
839 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200842 }
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 dhm.len = mbedtls_mpi_size( &dhm.P );
845 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
846 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
847 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200850 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200852 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100853 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200856 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100857 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200860 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100861 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100862#endif
863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200865 if( todo.ecdsa )
866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_ecdsa_context ecdsa;
868 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200869 size_t sig_len;
870
871 memset( buf, 0x2A, sizeof( buf ) );
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 for( curve_info = mbedtls_ecp_curve_list();
874 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200875 curve_info++ )
876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
880 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100881 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200884 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200885 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200886 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200887 tmp, &sig_len, myrand, NULL ) );
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100890 }
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 for( curve_info = mbedtls_ecp_curve_list();
893 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100894 curve_info++ )
895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200899 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100900 tmp, &sig_len, myrand, NULL ) != 0 )
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100903 }
904 ecp_clear_precomputed( &ecdsa.grp );
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100907 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200908 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200909 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200910 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200913 }
914 }
915#endif
916
Janos Follath52735ef2018-08-15 10:19:16 +0100917#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200918 if( todo.ecdh )
919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000922 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
923#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
924 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000925#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000926#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
927 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
928#endif
929 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
930 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200932 size_t olen;
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 for( curve_info = mbedtls_ecp_curve_list();
935 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200936 curve_info++ )
937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200939
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200940 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200942 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200946 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100947 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200950 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200951 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200953 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200955 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100957 }
958
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000959 /* Montgomery curves need to be handled separately */
960 for ( curve_info = montgomery_curve_list;
961 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
962 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000963 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000964 mbedtls_ecdh_init( &ecdh );
965 mbedtls_mpi_init( &z );
966
967 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
968 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
969 {
970 mbedtls_exit( 1 );
971 }
972
973 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
974 curve_info->name );
975 TIME_PUBLIC( title, "handshake",
976 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
977 myrand, NULL );
978 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
979 myrand, NULL ) );
980
981 mbedtls_ecdh_free( &ecdh );
982 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000983 }
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 for( curve_info = mbedtls_ecp_curve_list();
986 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100987 curve_info++ )
988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100990
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200991 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100993 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
995 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100996 myrand, NULL ) != 0 )
997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100999 }
1000 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +02001003 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001004 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001006 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001008 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001009
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001010 /* Montgomery curves need to be handled separately */
1011 for ( curve_info = montgomery_curve_list;
1012 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1013 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001014 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001015 mbedtls_ecdh_init( &ecdh );
1016 mbedtls_mpi_init( &z );
1017
1018 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
1019 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1020 myrand, NULL ) != 0 ||
1021 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
1022 {
1023 mbedtls_exit( 1 );
1024 }
1025
1026 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1027 curve_info->name );
1028 TIME_PUBLIC( title, "handshake",
1029 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1030 myrand, NULL ) );
1031
1032 mbedtls_ecdh_free( &ecdh );
1033 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001034 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001035 }
1036#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1041 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001042#endif
1043
Paul Bakkercce9d772011-11-18 14:26:47 +00001044#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 fflush( stdout ); getchar();
1047#endif
1048
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001049 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001050}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_TIMING_C */