blob: c24ace4a666ff096b8a1e829052077aa03424d60 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_exit exit
59#define mbedtls_printf printf
60#define mbedtls_snprintf snprintf
61#define mbedtls_free free
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010062#define mbedtls_exit exit
63#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
64#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evansf90016a2015-01-19 14:26:37 +000065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000068int main( void )
69{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020071 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000072}
73#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010074
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000075#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020076#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010077
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/md4.h"
81#include "mbedtls/md5.h"
82#include "mbedtls/ripemd160.h"
83#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010086
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000087#include "mbedtls/arc4.h"
88#include "mbedtls/des.h"
89#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010090#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000091#include "mbedtls/blowfish.h"
92#include "mbedtls/camellia.h"
Daniel King34b822c2016-05-15 17:28:08 -030093#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000094#include "mbedtls/gcm.h"
95#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020096#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010097#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030098#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010099
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000100#include "mbedtls/havege.h"
101#include "mbedtls/ctr_drbg.h"
102#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100103
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000104#include "mbedtls/rsa.h"
105#include "mbedtls/dhm.h"
106#include "mbedtls/ecdsa.h"
107#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100108
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000109#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000112#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +0000113#endif
114
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000115/*
116 * For heap usage estimates, we need an estimate of the overhead per allocated
117 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
118 * so use that as our baseline.
119 */
120#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
121
122/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200123 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000124 */
125#define HEAP_SIZE (1u << 16) // 64k
126
Paul Bakker02faf452011-11-29 11:23:58 +0000127#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100128#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100129#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000130
Rich Evans85b05ec2015-02-12 11:37:29 +0000131#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200132 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300133 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200134 "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300135 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136 "havege, ctr_drbg, hmac_drbg\n" \
137 "rsa, dhm, ecdsa, ecdh.\n"
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000140#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100141 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000143#else
144#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000146#endif
147
148#define TIME_AND_TSC( TITLE, CODE ) \
149do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200150 unsigned long ii, jj, tsc; \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200151 int ret = 0; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000152 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200153 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000154 fflush( stdout ); \
155 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200156 mbedtls_set_alarm( 1 ); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200157 for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000158 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200159 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000160 } \
161 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200162 tsc = mbedtls_timing_hardclock(); \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200163 for( jj = 0; ret == 0 && jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000164 { \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200165 ret = CODE; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000166 } \
167 \
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200168 if( ret != 0 ) \
169 { \
170 PRINT_ERROR; \
171 } \
172 else \
173 { \
174 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
175 ii * BUFSIZE / 1024, \
176 ( mbedtls_timing_hardclock() - tsc ) \
177 / ( jj * BUFSIZE ) ); \
178 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000179} while( 0 )
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100182
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200183/* How much space to reserve for the title when printing heap usage results.
184 * Updated manually as the output of the following command:
185 *
186 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
187 * awk '{print length+2}' | sort -rn | head -n1
188 *
189 * This computes the maximum length of a title +2 (because we appends "/s").
190 * (If the value is too small, the only consequence is poor alignement.) */
191#define TITLE_SPACE 11
192
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100193#define MEMORY_MEASURE_INIT \
194 size_t max_used, max_blocks, max_bytes; \
195 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200196 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100198
199#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200200 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnarde675a8d2020-04-09 10:40:03 +0200201 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
202 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100203 max_used -= prv_used; \
204 max_blocks -= prv_blocks; \
205 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100207
208#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000209#define MEMORY_MEASURE_INIT
210#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100211#endif
212
Rich Evans85b05ec2015-02-12 11:37:29 +0000213#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
214do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200215 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000216 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100217 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000218 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200219 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000220 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200221 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000222 \
223 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200224 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000225 { \
226 CODE; \
227 } \
228 \
229 if( ret != 0 ) \
230 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100231 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000232 } \
233 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100234 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200235 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100236 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200237 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100238 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000239} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakkera3d195c2011-11-27 21:07:34 +0000241static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000243 size_t use_len;
244 int rnd;
245
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 if( rng_state != NULL )
247 rng_state = NULL;
248
Paul Bakkera3d195c2011-11-27 21:07:34 +0000249 while( len > 0 )
250 {
251 use_len = len;
252 if( use_len > sizeof(int) )
253 use_len = sizeof(int);
254
255 rnd = rand();
256 memcpy( output, &rnd, use_len );
257 output += use_len;
258 len -= use_len;
259 }
260
261 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262}
263
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100264/*
265 * Clear some memory that was used to prepare the context
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_ECP_C)
268void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100269{
270 if( grp->T != NULL )
271 {
272 size_t i;
273 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_ecp_point_free( &grp->T[i] );
275 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100276 }
277 grp->T = NULL;
278 grp->T_size = 0;
279}
280#else
281#define ecp_clear_precomputed( g )
282#endif
283
Paul Bakker5121ce52009-01-03 21:22:43 +0000284unsigned char buf[BUFSIZE];
285
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200286typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200287 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100288 arc4, des3, des,
Manuel Pégourié-Gonnard0dadba22018-06-19 11:11:15 +0200289 aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200290 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200291 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300292 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100293 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200294 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200295} todo_list;
296
Simon Butcher63cb97e2018-12-06 17:43:31 +0000297
Paul Bakkercce9d772011-11-18 14:26:47 +0000298int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000299{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100300 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200301 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200302 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200303 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200305 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000306#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000307
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000308 if( argc <= 1 )
309 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200310 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000311 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200312 else
313 {
314 memset( &todo, 0, sizeof( todo ) );
315
316 for( i = 1; i < argc; i++ )
317 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200318 if( strcmp( argv[i], "md4" ) == 0 )
319 todo.md4 = 1;
320 else if( strcmp( argv[i], "md5" ) == 0 )
321 todo.md5 = 1;
322 else if( strcmp( argv[i], "ripemd160" ) == 0 )
323 todo.ripemd160 = 1;
324 else if( strcmp( argv[i], "sha1" ) == 0 )
325 todo.sha1 = 1;
326 else if( strcmp( argv[i], "sha256" ) == 0 )
327 todo.sha256 = 1;
328 else if( strcmp( argv[i], "sha512" ) == 0 )
329 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200330 else if( strcmp( argv[i], "arc4" ) == 0 )
331 todo.arc4 = 1;
332 else if( strcmp( argv[i], "des3" ) == 0 )
333 todo.des3 = 1;
334 else if( strcmp( argv[i], "des" ) == 0 )
335 todo.des = 1;
336 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
337 todo.aes_cbc = 1;
Aorimn5f778012016-06-09 23:22:58 +0200338 else if( strcmp( argv[i], "aes_xts" ) == 0 )
339 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200340 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
341 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200342 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
343 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200344 else if( strcmp( argv[i], "chachapoly" ) == 0 )
345 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100346 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
347 todo.aes_cmac = 1;
348 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
349 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100350 else if( strcmp( argv[i], "aria" ) == 0 )
351 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200352 else if( strcmp( argv[i], "camellia" ) == 0 )
353 todo.camellia = 1;
354 else if( strcmp( argv[i], "blowfish" ) == 0 )
355 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300356 else if( strcmp( argv[i], "chacha20" ) == 0 )
357 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300358 else if( strcmp( argv[i], "poly1305" ) == 0 )
359 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200360 else if( strcmp( argv[i], "havege" ) == 0 )
361 todo.havege = 1;
362 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
363 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100364 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
365 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200366 else if( strcmp( argv[i], "rsa" ) == 0 )
367 todo.rsa = 1;
368 else if( strcmp( argv[i], "dhm" ) == 0 )
369 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200370 else if( strcmp( argv[i], "ecdsa" ) == 0 )
371 todo.ecdsa = 1;
372 else if( strcmp( argv[i], "ecdh" ) == 0 )
373 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 else
375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
377 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200378 }
379 }
380 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200385 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000386#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200387 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200388 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200391 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100392 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393#endif
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200396 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100397 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398#endif
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200401 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100402 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100403#endif
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200406 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100407 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408#endif
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200411 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100412 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413#endif
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200416 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100417 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000418#endif
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200421 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_arc4_context arc4;
424 mbedtls_arc4_init( &arc4 );
425 mbedtls_arc4_setup( &arc4, tmp, 32 );
426 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
427 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200428 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000429#endif
430
Simon Butcher549dc3d2016-10-05 14:14:19 +0100431#if defined(MBEDTLS_DES_C)
432#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200433 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_des3_context des3;
436 mbedtls_des3_init( &des3 );
437 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200438 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
440 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200441 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200443 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_des_context des;
446 mbedtls_des_init( &des );
447 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200448 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
450 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200451 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100452
453#endif /* MBEDTLS_CIPHER_MODE_CBC */
454#if defined(MBEDTLS_CMAC_C)
455 if( todo.des3_cmac )
456 {
457 unsigned char output[8];
458 const mbedtls_cipher_info_t *cipher_info;
459
460 memset( buf, 0, sizeof( buf ) );
461 memset( tmp, 0, sizeof( tmp ) );
462
463 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
464
465 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100466 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
467 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100468 }
469#endif /* MBEDTLS_CMAC_C */
470#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_AES_C)
473#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200474 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100476 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_aes_context aes;
478 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200479 for( keysize = 128; keysize <= 256; keysize += 64 )
480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200483 memset( buf, 0, sizeof( buf ) );
484 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200487 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200489 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200492#endif
Aorimn5f778012016-06-09 23:22:58 +0200493#if defined(MBEDTLS_CIPHER_MODE_XTS)
494 if( todo.aes_xts )
495 {
496 int keysize;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100497 mbedtls_aes_xts_context ctx;
498
499 mbedtls_aes_xts_init( &ctx );
500 for( keysize = 128; keysize <= 256; keysize += 128 )
Aorimn5f778012016-06-09 23:22:58 +0200501 {
502 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
503
504 memset( buf, 0, sizeof( buf ) );
505 memset( tmp, 0, sizeof( tmp ) );
Jaeden Amero9366feb2018-05-29 18:55:17 +0100506 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
Aorimn5f778012016-06-09 23:22:58 +0200507
508 TIME_AND_TSC( title,
Jaeden Amero9366feb2018-05-29 18:55:17 +0100509 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
510 tmp, buf, buf ) );
511
512 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200513 }
Aorimn5f778012016-06-09 23:22:58 +0200514 }
515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200517 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000518 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100519 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200521
522 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 for( keysize = 128; keysize <= 256; keysize += 64 )
524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000526
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200527 memset( buf, 0, sizeof( buf ) );
528 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200529 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000530
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200531 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200533 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200536 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000537 }
538#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200540 if( todo.aes_ccm )
541 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100542 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200544
545 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200546 for( keysize = 128; keysize <= 256; keysize += 64 )
547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200549
550 memset( buf, 0, sizeof( buf ) );
551 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200552 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200553
554 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200556 12, NULL, 0, buf, buf, tmp, 16 ) );
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200559 }
560 }
561#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200562#if defined(MBEDTLS_CHACHAPOLY_C)
563 if( todo.chachapoly )
564 {
565 mbedtls_chachapoly_context chachapoly;
566
567 mbedtls_chachapoly_init( &chachapoly );
568 memset( buf, 0, sizeof( buf ) );
569 memset( tmp, 0, sizeof( tmp ) );
570
571 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
572
573 mbedtls_chachapoly_setkey( &chachapoly, tmp );
574
575 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200576 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
577 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200578
579 mbedtls_chachapoly_free( &chachapoly );
580 }
581#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100582#if defined(MBEDTLS_CMAC_C)
583 if( todo.aes_cmac )
584 {
585 unsigned char output[16];
586 const mbedtls_cipher_info_t *cipher_info;
587 mbedtls_cipher_type_t cipher_type;
588 int keysize;
589
590 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
591 keysize <= 256;
592 keysize += 64, cipher_type++ )
593 {
594 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
595
596 memset( buf, 0, sizeof( buf ) );
597 memset( tmp, 0, sizeof( tmp ) );
598
599 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
600
601 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100602 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
603 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100604 }
605
606 memset( buf, 0, sizeof( buf ) );
607 memset( tmp, 0, sizeof( tmp ) );
608 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100609 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
610 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100611 }
612#endif /* MBEDTLS_CMAC_C */
613#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100615#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
616 if( todo.aria )
617 {
618 int keysize;
619 mbedtls_aria_context aria;
620 mbedtls_aria_init( &aria );
621 for( keysize = 128; keysize <= 256; keysize += 64 )
622 {
623 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
624
625 memset( buf, 0, sizeof( buf ) );
626 memset( tmp, 0, sizeof( tmp ) );
627 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
628
629 TIME_AND_TSC( title,
630 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
631 BUFSIZE, tmp, buf, buf ) );
632 }
633 mbedtls_aria_free( &aria );
634 }
635#endif
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200638 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000639 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100640 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_camellia_context camellia;
642 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200643 for( keysize = 128; keysize <= 256; keysize += 64 )
644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000646
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200647 memset( buf, 0, sizeof( buf ) );
648 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000650
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200651 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200653 BUFSIZE, tmp, buf, buf ) );
654 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000656 }
657#endif
658
Daniel King34b822c2016-05-15 17:28:08 -0300659#if defined(MBEDTLS_CHACHA20_C)
660 if ( todo.chacha20 )
661 {
662 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
663 }
664#endif
665
Daniel Kingadc32c02016-05-16 18:25:45 -0300666#if defined(MBEDTLS_POLY1305_C)
667 if ( todo.poly1305 )
668 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200669 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300670 }
671#endif
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200674 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000675 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100676 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_blowfish_context blowfish;
678 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200679
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200680 for( keysize = 128; keysize <= 256; keysize += 64 )
681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000683
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200684 memset( buf, 0, sizeof( buf ) );
685 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000687
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200688 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200690 tmp, buf, buf ) );
691 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000694 }
695#endif
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200698 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_havege_state hs;
701 mbedtls_havege_init( &hs );
702 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
703 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200704 }
Paul Bakker02faf452011-11-29 11:23:58 +0000705#endif
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200708 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000711
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200712 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200713 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200715 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200716 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100717 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000718
Gilles Peskinef4c0dbc2019-10-28 17:28:46 +0100719 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200720 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_exit(1);
722 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200723 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200724 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200726 }
Paul Bakker02faf452011-11-29 11:23:58 +0000727#endif
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100730 if( todo.hmac_drbg )
731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_hmac_drbg_context hmac_drbg;
733 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100734
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200735 mbedtls_hmac_drbg_init( &hmac_drbg );
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_SHA1_C)
738 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
739 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100740
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200741 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100743 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200744 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100745
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200746 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_exit(1);
748 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
749 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100750 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200751 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100752#endif
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_SHA256_C)
755 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
756 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100757
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200758 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100760 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
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
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200763 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_exit(1);
765 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
766 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100767 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard51d7cfe2018-06-25 11:19:51 +0200768 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100769#endif
Ron Eldor278af452018-06-20 18:40:21 +0300770 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100771 }
772#endif
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200775 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000776 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100777 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200779 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
784 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200785
786 TIME_PUBLIC( title, " public",
787 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200789
790 TIME_PUBLIC( title, "private",
791 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200795 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000796 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000797#endif
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200800 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100801 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200802 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800803 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100804 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800805 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100806 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800807 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100808 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800809 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100810 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
811
812 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
813 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
814 sizeof( dhm_P_3072 ) };
815
816 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
817 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
818 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200821 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200822 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200825
Hanno Beckerb9539212017-10-04 13:13:34 +0100826 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
827 dhm_P_size[i] ) != 0 ||
828 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
829 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200832 }
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 dhm.len = mbedtls_mpi_size( &dhm.P );
835 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
836 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
837 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200840 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200842 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100843 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200846 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100847 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200850 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100851 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100852#endif
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200855 if( todo.ecdsa )
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_ecdsa_context ecdsa;
858 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200859 size_t sig_len;
860
861 memset( buf, 0x2A, sizeof( buf ) );
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 for( curve_info = mbedtls_ecp_curve_list();
864 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200865 curve_info++ )
866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
870 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100871 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200874 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200875 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200876 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200877 tmp, &sig_len, myrand, NULL ) );
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100880 }
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 for( curve_info = mbedtls_ecp_curve_list();
883 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100884 curve_info++ )
885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200889 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100890 tmp, &sig_len, myrand, NULL ) != 0 )
891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100893 }
894 ecp_clear_precomputed( &ecdsa.grp );
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100897 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200898 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200899 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200900 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200903 }
904 }
905#endif
906
Janos Follath52735ef2018-08-15 10:19:16 +0100907#if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200908 if( todo.ecdh )
909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000912 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
913#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
914 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000915#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000916#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
917 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
918#endif
919 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
920 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200922 size_t olen;
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 for( curve_info = mbedtls_ecp_curve_list();
925 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200926 curve_info++ )
927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200929
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200930 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200932 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200936 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100937 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200940 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200941 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200943 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 ret |= mbedtls_ecdh_calc_secret( &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 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100947 }
948
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000949 /* Montgomery curves need to be handled separately */
950 for ( curve_info = montgomery_curve_list;
951 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
952 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000953 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000954 mbedtls_ecdh_init( &ecdh );
955 mbedtls_mpi_init( &z );
956
957 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
958 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
959 {
960 mbedtls_exit( 1 );
961 }
962
963 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
964 curve_info->name );
965 TIME_PUBLIC( title, "handshake",
966 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
967 myrand, NULL );
968 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
969 myrand, NULL ) );
970
971 mbedtls_ecdh_free( &ecdh );
972 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000973 }
974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 for( curve_info = mbedtls_ecp_curve_list();
976 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100977 curve_info++ )
978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100980
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200981 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100983 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
985 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100986 myrand, NULL ) != 0 )
987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100989 }
990 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200993 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200994 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200996 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200998 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000999
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001000 /* Montgomery curves need to be handled separately */
1001 for ( curve_info = montgomery_curve_list;
1002 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1003 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001004 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +00001005 mbedtls_ecdh_init( &ecdh );
1006 mbedtls_mpi_init( &z );
1007
1008 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
1009 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1010 myrand, NULL ) != 0 ||
1011 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
1012 {
1013 mbedtls_exit( 1 );
1014 }
1015
1016 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1017 curve_info->name );
1018 TIME_PUBLIC( title, "handshake",
1019 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1020 myrand, NULL ) );
1021
1022 mbedtls_ecdh_free( &ecdh );
1023 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +00001024 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +02001025 }
1026#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +01001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +00001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1031 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +00001032#endif
1033
Paul Bakkercce9d772011-11-18 14:26:47 +00001034#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 fflush( stdout ); getchar();
1037#endif
1038
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +02001039 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001040}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +02001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#endif /* MBEDTLS_TIMING_C */