blob: c9241cb2f3ba79a3415a7be078b749168f6a90e4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Simon Butcher549dc3d2016-10-05 14:14:19 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_exit exit
33#define mbedtls_printf printf
34#define mbedtls_snprintf snprintf
35#define mbedtls_free free
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000039int main( void )
40{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000042 return( 0 );
43}
44#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010045
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000046#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020047#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010048
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md4.h"
52#include "mbedtls/md5.h"
53#include "mbedtls/ripemd160.h"
54#include "mbedtls/sha1.h"
55#include "mbedtls/sha256.h"
56#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/arc4.h"
59#include "mbedtls/des.h"
60#include "mbedtls/aes.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010061#include "mbedtls/aria.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
64#include "mbedtls/gcm.h"
65#include "mbedtls/ccm.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010066#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010067
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/havege.h"
69#include "mbedtls/ctr_drbg.h"
70#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010071
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/rsa.h"
73#include "mbedtls/dhm.h"
74#include "mbedtls/ecdsa.h"
75#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010076
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000081#endif
82
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000083/*
84 * For heap usage estimates, we need an estimate of the overhead per allocated
85 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
86 * so use that as our baseline.
87 */
88#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
89
90/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020091 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000092 */
93#define HEAP_SIZE (1u << 16) // 64k
94
Paul Bakker02faf452011-11-29 11:23:58 +000095#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010096#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010097#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000098
Rich Evans85b05ec2015-02-12 11:37:29 +000099#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200100 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Simon Butcherb981b162016-10-06 10:27:22 +0100101 "arc4, des3, des, camellia, blowfish,\n" \
Aorimn5f778012016-06-09 23:22:58 +0200102 "aes_cbc, aes_gcm, aes_ccm, aes_cmac, aes_xex, aes_xts,\n" \
103 "des3_cmac, havege, ctr_drbg, hmac_drbg\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000104 "rsa, dhm, ecdsa, ecdh.\n"
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000107#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100108 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000110#else
111#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000113#endif
114
115#define TIME_AND_TSC( TITLE, CODE ) \
116do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200117 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000118 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200119 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000120 fflush( stdout ); \
121 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200122 mbedtls_set_alarm( 1 ); \
123 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000124 { \
125 CODE; \
126 } \
127 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200128 tsc = mbedtls_timing_hardclock(); \
129 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000130 { \
131 CODE; \
132 } \
133 \
Ron Eldor0728d692017-11-29 12:08:35 +0200134 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200135 ii * BUFSIZE / 1024, \
136 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000137} while( 0 )
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100140
141#define MEMORY_MEASURE_INIT \
142 size_t max_used, max_blocks, max_bytes; \
143 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200144 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100146
147#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200148 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
149 for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100150 max_used -= prv_used; \
151 max_blocks -= prv_blocks; \
152 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100154
155#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000156#define MEMORY_MEASURE_INIT
157#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100158#endif
159
Rich Evans85b05ec2015-02-12 11:37:29 +0000160#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
161do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200162 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000163 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100164 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000165 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200166 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000167 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200168 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000169 \
170 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200171 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000172 { \
173 CODE; \
174 } \
175 \
176 if( ret != 0 ) \
177 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100178 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000179 } \
180 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100181 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200182 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100183 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200184 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100185 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000186} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Paul Bakkera3d195c2011-11-27 21:07:34 +0000188static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000189{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000190 size_t use_len;
191 int rnd;
192
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 if( rng_state != NULL )
194 rng_state = NULL;
195
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196 while( len > 0 )
197 {
198 use_len = len;
199 if( use_len > sizeof(int) )
200 use_len = sizeof(int);
201
202 rnd = rand();
203 memcpy( output, &rnd, use_len );
204 output += use_len;
205 len -= use_len;
206 }
207
208 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209}
210
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100211/*
212 * Clear some memory that was used to prepare the context
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_ECP_C)
215void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100216{
217 if( grp->T != NULL )
218 {
219 size_t i;
220 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_ecp_point_free( &grp->T[i] );
222 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100223 }
224 grp->T = NULL;
225 grp->T_size = 0;
226}
227#else
228#define ecp_clear_precomputed( g )
229#endif
230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231unsigned char buf[BUFSIZE];
232
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200233typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200234 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100235 arc4, des3, des,
Aorimn5f778012016-06-09 23:22:58 +0200236 aes_cbc, aes_gcm, aes_ccm, aes_cmac, aes_xex, aes_xts,
237 des3_cmac, aria, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100238 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200239 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200240} todo_list;
241
Paul Bakkercce9d772011-11-18 14:26:47 +0000242int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000243{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100244 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200245 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200246 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200247 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200249 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000250#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000251
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000252 if( argc <= 1 )
253 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200254 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000255 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200256 else
257 {
258 memset( &todo, 0, sizeof( todo ) );
259
260 for( i = 1; i < argc; i++ )
261 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200262 if( strcmp( argv[i], "md4" ) == 0 )
263 todo.md4 = 1;
264 else if( strcmp( argv[i], "md5" ) == 0 )
265 todo.md5 = 1;
266 else if( strcmp( argv[i], "ripemd160" ) == 0 )
267 todo.ripemd160 = 1;
268 else if( strcmp( argv[i], "sha1" ) == 0 )
269 todo.sha1 = 1;
270 else if( strcmp( argv[i], "sha256" ) == 0 )
271 todo.sha256 = 1;
272 else if( strcmp( argv[i], "sha512" ) == 0 )
273 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200274 else if( strcmp( argv[i], "arc4" ) == 0 )
275 todo.arc4 = 1;
276 else if( strcmp( argv[i], "des3" ) == 0 )
277 todo.des3 = 1;
278 else if( strcmp( argv[i], "des" ) == 0 )
279 todo.des = 1;
280 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
281 todo.aes_cbc = 1;
Aorimndaf70452016-02-02 22:52:40 +0100282 else if( strcmp( argv[i], "aes_xex" ) == 0 )
283 todo.aes_xex = 1;
Aorimn5f778012016-06-09 23:22:58 +0200284 else if( strcmp( argv[i], "aes_xts" ) == 0 )
285 todo.aes_xts = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200286 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
287 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200288 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
289 todo.aes_ccm = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100290 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
291 todo.aes_cmac = 1;
292 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
293 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100294 else if( strcmp( argv[i], "aria" ) == 0 )
295 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200296 else if( strcmp( argv[i], "camellia" ) == 0 )
297 todo.camellia = 1;
298 else if( strcmp( argv[i], "blowfish" ) == 0 )
299 todo.blowfish = 1;
300 else if( strcmp( argv[i], "havege" ) == 0 )
301 todo.havege = 1;
302 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
303 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100304 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
305 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200306 else if( strcmp( argv[i], "rsa" ) == 0 )
307 todo.rsa = 1;
308 else if( strcmp( argv[i], "dhm" ) == 0 )
309 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200310 else if( strcmp( argv[i], "ecdsa" ) == 0 )
311 todo.ecdsa = 1;
312 else if( strcmp( argv[i], "ecdh" ) == 0 )
313 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200314 else
315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
317 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200318 }
319 }
320 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200325 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000326#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200328 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200331 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100332 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333#endif
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200336 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100337 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338#endif
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200341 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100342 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100343#endif
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200346 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100347 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348#endif
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200351 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100352 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353#endif
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200356 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100357 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000358#endif
359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200361 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_arc4_context arc4;
364 mbedtls_arc4_init( &arc4 );
365 mbedtls_arc4_setup( &arc4, tmp, 32 );
366 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
367 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200368 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000369#endif
370
Simon Butcher549dc3d2016-10-05 14:14:19 +0100371#if defined(MBEDTLS_DES_C)
372#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200373 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_des3_context des3;
376 mbedtls_des3_init( &des3 );
377 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200378 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
380 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200381 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200383 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_des_context des;
386 mbedtls_des_init( &des );
387 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200388 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
390 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200391 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100392
393#endif /* MBEDTLS_CIPHER_MODE_CBC */
394#if defined(MBEDTLS_CMAC_C)
395 if( todo.des3_cmac )
396 {
397 unsigned char output[8];
398 const mbedtls_cipher_info_t *cipher_info;
399
400 memset( buf, 0, sizeof( buf ) );
401 memset( tmp, 0, sizeof( tmp ) );
402
403 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
404
405 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100406 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
407 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100408 }
409#endif /* MBEDTLS_CMAC_C */
410#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_AES_C)
413#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200414 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100416 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_aes_context aes;
418 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 for( keysize = 128; keysize <= 256; keysize += 64 )
420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 memset( buf, 0, sizeof( buf ) );
424 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200427 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200432#endif
Aorimndaf70452016-02-02 22:52:40 +0100433#if defined(MBEDTLS_CIPHER_MODE_XEX)
434 if( todo.aes_xex )
435 {
436 int keysize;
437 mbedtls_aes_context crypt_ctx, tweak_ctx;
438 mbedtls_aes_init( &crypt_ctx );
439 mbedtls_aes_init( &tweak_ctx );
440 for( keysize = 128; keysize <= 256; keysize += 64 )
441 {
442 mbedtls_snprintf( title, sizeof( title ), "AES-XEX-%d", keysize );
443
444 memset( buf, 0, sizeof( buf ) );
445 memset( tmp, 0, sizeof( tmp ) );
446 mbedtls_aes_setkey_enc( &crypt_ctx, tmp, keysize );
447 mbedtls_aes_setkey_enc( &tweak_ctx, tmp, keysize );
448
449 TIME_AND_TSC( title,
450 mbedtls_aes_crypt_xex( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
451 }
452 mbedtls_aes_free( &crypt_ctx );
453 mbedtls_aes_free( &tweak_ctx );
454 }
455#endif
Aorimn5f778012016-06-09 23:22:58 +0200456#if defined(MBEDTLS_CIPHER_MODE_XTS)
457 if( todo.aes_xts )
458 {
459 int keysize;
460 mbedtls_aes_context crypt_ctx, tweak_ctx;
461 mbedtls_aes_init( &crypt_ctx );
462 mbedtls_aes_init( &tweak_ctx );
463 for( keysize = 128; keysize <= 256; keysize += 64 )
464 {
465 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
466
467 memset( buf, 0, sizeof( buf ) );
468 memset( tmp, 0, sizeof( tmp ) );
469 mbedtls_aes_setkey_enc( &crypt_ctx, tmp, keysize );
470 mbedtls_aes_setkey_enc( &tweak_ctx, tmp, keysize );
471
472 TIME_AND_TSC( title,
473 mbedtls_aes_crypt_xts( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE * 8, tmp, buf, buf ) );
474 }
475 mbedtls_aes_free( &crypt_ctx );
476 mbedtls_aes_free( &tweak_ctx );
477 }
478#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200480 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000481 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100482 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200484
485 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200486 for( keysize = 128; keysize <= 256; keysize += 64 )
487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000489
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200490 memset( buf, 0, sizeof( buf ) );
491 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200492 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000493
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200494 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200496 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000500 }
501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200503 if( todo.aes_ccm )
504 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100505 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200507
508 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200509 for( keysize = 128; keysize <= 256; keysize += 64 )
510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200512
513 memset( buf, 0, sizeof( buf ) );
514 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200515 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200516
517 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200519 12, NULL, 0, buf, buf, tmp, 16 ) );
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200522 }
523 }
524#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100525#if defined(MBEDTLS_CMAC_C)
526 if( todo.aes_cmac )
527 {
528 unsigned char output[16];
529 const mbedtls_cipher_info_t *cipher_info;
530 mbedtls_cipher_type_t cipher_type;
531 int keysize;
532
533 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
534 keysize <= 256;
535 keysize += 64, cipher_type++ )
536 {
537 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
538
539 memset( buf, 0, sizeof( buf ) );
540 memset( tmp, 0, sizeof( tmp ) );
541
542 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
543
544 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100545 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
546 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100547 }
548
549 memset( buf, 0, sizeof( buf ) );
550 memset( tmp, 0, sizeof( tmp ) );
551 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100552 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
553 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100554 }
555#endif /* MBEDTLS_CMAC_C */
556#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100558#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
559 if( todo.aria )
560 {
561 int keysize;
562 mbedtls_aria_context aria;
563 mbedtls_aria_init( &aria );
564 for( keysize = 128; keysize <= 256; keysize += 64 )
565 {
566 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
567
568 memset( buf, 0, sizeof( buf ) );
569 memset( tmp, 0, sizeof( tmp ) );
570 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
571
572 TIME_AND_TSC( title,
573 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
574 BUFSIZE, tmp, buf, buf ) );
575 }
576 mbedtls_aria_free( &aria );
577 }
578#endif
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200581 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000582 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100583 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_camellia_context camellia;
585 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200586 for( keysize = 128; keysize <= 256; keysize += 64 )
587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000589
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200590 memset( buf, 0, sizeof( buf ) );
591 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000593
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200594 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200596 BUFSIZE, tmp, buf, buf ) );
597 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000599 }
600#endif
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200603 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000604 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100605 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_blowfish_context blowfish;
607 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200608
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200609 for( keysize = 128; keysize <= 256; keysize += 64 )
610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000612
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200613 memset( buf, 0, sizeof( buf ) );
614 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000616
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200617 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200619 tmp, buf, buf ) );
620 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000623 }
624#endif
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200627 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_havege_state hs;
630 mbedtls_havege_init( &hs );
631 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
632 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200633 }
Paul Bakker02faf452011-11-29 11:23:58 +0000634#endif
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200637 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000640
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200641 mbedtls_ctr_drbg_init( &ctr_drbg );
642
643 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200645 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
647 mbedtls_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000648
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200649 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_exit(1);
651 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200652 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
654 mbedtls_exit(1) );
655 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200656 }
Paul Bakker02faf452011-11-29 11:23:58 +0000657#endif
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100660 if( todo.hmac_drbg )
661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_hmac_drbg_context hmac_drbg;
663 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100664
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200665 mbedtls_hmac_drbg_init( &hmac_drbg );
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_SHA1_C)
668 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
669 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100670
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200671 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100673 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
675 mbedtls_exit(1) );
676 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100677
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200678 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_exit(1);
680 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
681 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100682 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
684 mbedtls_exit(1) );
685 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100686#endif
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SHA256_C)
689 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
690 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100691
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200692 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100694 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
696 mbedtls_exit(1) );
697 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100698
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200699 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_exit(1);
701 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
702 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100703 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
705 mbedtls_exit(1) );
706 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100707#endif
708 }
709#endif
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200712 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100714 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200716 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
721 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200722
723 TIME_PUBLIC( title, " public",
724 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200726
727 TIME_PUBLIC( title, "private",
728 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200732 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000734#endif
735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200737 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100738 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200739 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800740 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100741 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800742 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100743 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800744 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100745 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800746 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100747 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
748
749 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
750 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
751 sizeof( dhm_P_3072 ) };
752
753 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
754 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
755 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200758 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200759 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200762
Hanno Beckerb9539212017-10-04 13:13:34 +0100763 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
764 dhm_P_size[i] ) != 0 ||
765 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
766 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200769 }
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 dhm.len = mbedtls_mpi_size( &dhm.P );
772 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
773 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
774 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200777 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200779 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100780 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200783 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100784 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200787 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100788 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100789#endif
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200792 if( todo.ecdsa )
793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_ecdsa_context ecdsa;
795 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200796 size_t sig_len;
797
798 memset( buf, 0x2A, sizeof( buf ) );
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 for( curve_info = mbedtls_ecp_curve_list();
801 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200802 curve_info++ )
803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
807 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100808 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200811 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200812 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200813 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200814 tmp, &sig_len, myrand, NULL ) );
815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100817 }
818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 for( curve_info = mbedtls_ecp_curve_list();
820 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100821 curve_info++ )
822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200826 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100827 tmp, &sig_len, myrand, NULL ) != 0 )
828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100830 }
831 ecp_clear_precomputed( &ecdsa.grp );
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100834 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200835 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200836 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200837 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200840 }
841 }
842#endif
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200845 if( todo.ecdh )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000849 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
850#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
851 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000852#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000853#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
854 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
855#endif
856 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
857 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200859 size_t olen;
860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 for( curve_info = mbedtls_ecp_curve_list();
862 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200863 curve_info++ )
864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200866
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200867 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200869 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200873 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100874 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200877 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200878 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200880 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200882 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100884 }
885
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000886 /* Montgomery curves need to be handled separately */
887 for ( curve_info = montgomery_curve_list;
888 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
889 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000890 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000891 mbedtls_ecdh_init( &ecdh );
892 mbedtls_mpi_init( &z );
893
894 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
895 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
896 {
897 mbedtls_exit( 1 );
898 }
899
900 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
901 curve_info->name );
902 TIME_PUBLIC( title, "handshake",
903 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
904 myrand, NULL );
905 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
906 myrand, NULL ) );
907
908 mbedtls_ecdh_free( &ecdh );
909 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000910 }
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 for( curve_info = mbedtls_ecp_curve_list();
913 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100914 curve_info++ )
915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100917
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200918 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100920 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
922 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100923 myrand, NULL ) != 0 )
924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100926 }
927 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200930 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200931 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200933 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200935 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000936
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000937 /* Montgomery curves need to be handled separately */
938 for ( curve_info = montgomery_curve_list;
939 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
940 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000941 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000942 mbedtls_ecdh_init( &ecdh );
943 mbedtls_mpi_init( &z );
944
945 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
946 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
947 myrand, NULL ) != 0 ||
948 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
949 {
950 mbedtls_exit( 1 );
951 }
952
953 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
954 curve_info->name );
955 TIME_PUBLIC( title, "handshake",
956 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
957 myrand, NULL ) );
958
959 mbedtls_ecdh_free( &ecdh );
960 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000961 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200962 }
963#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
968 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000969#endif
970
Paul Bakkercce9d772011-11-18 14:26:47 +0000971#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973 fflush( stdout ); getchar();
974#endif
975
976 return( 0 );
977}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979#endif /* MBEDTLS_TIMING_C */