blob: 0366f9c2e8357d87ef64e42d69889cacd7656362 [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"
57#include "mbedtls/arc4.h"
58#include "mbedtls/des.h"
59#include "mbedtls/aes.h"
60#include "mbedtls/blowfish.h"
61#include "mbedtls/camellia.h"
62#include "mbedtls/gcm.h"
63#include "mbedtls/ccm.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010064#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/havege.h"
66#include "mbedtls/ctr_drbg.h"
67#include "mbedtls/hmac_drbg.h"
68#include "mbedtls/rsa.h"
69#include "mbedtls/dhm.h"
70#include "mbedtls/ecdsa.h"
71#include "mbedtls/ecdh.h"
72#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000076#endif
77
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000078/*
79 * For heap usage estimates, we need an estimate of the overhead per allocated
80 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
81 * so use that as our baseline.
82 */
83#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
84
85/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020086 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000087 */
88#define HEAP_SIZE (1u << 16) // 64k
89
Paul Bakker02faf452011-11-29 11:23:58 +000090#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010091#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010092#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000093
Rich Evans85b05ec2015-02-12 11:37:29 +000094#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +020095 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Simon Butcherb981b162016-10-06 10:27:22 +010096 "arc4, des3, des, camellia, blowfish,\n" \
Jaeden Ameroc00cac72018-05-02 17:38:00 +010097 "aes_cbc, aes_gcm, aes_ccm, aes_cmac, aes_xts, des3_cmac,\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +000098 "havege, ctr_drbg, hmac_drbg\n" \
99 "rsa, dhm, ecdsa, ecdh.\n"
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000102#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100103 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000105#else
106#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000108#endif
109
110#define TIME_AND_TSC( TITLE, CODE ) \
111do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200112 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000113 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200114 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000115 fflush( stdout ); \
116 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200117 mbedtls_set_alarm( 1 ); \
118 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000119 { \
120 CODE; \
121 } \
122 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200123 tsc = mbedtls_timing_hardclock(); \
124 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000125 { \
126 CODE; \
127 } \
128 \
Ron Eldor0728d692017-11-29 12:08:35 +0200129 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200130 ii * BUFSIZE / 1024, \
131 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000132} while( 0 )
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100135
136#define MEMORY_MEASURE_INIT \
137 size_t max_used, max_blocks, max_bytes; \
138 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200139 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100141
142#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200143 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
144 for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100145 max_used -= prv_used; \
146 max_blocks -= prv_blocks; \
147 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100149
150#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000151#define MEMORY_MEASURE_INIT
152#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100153#endif
154
Rich Evans85b05ec2015-02-12 11:37:29 +0000155#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
156do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200157 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000158 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100159 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000160 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200161 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000162 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200163 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000164 \
165 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200166 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000167 { \
168 CODE; \
169 } \
170 \
171 if( ret != 0 ) \
172 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100173 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000174 } \
175 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100176 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200177 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100178 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200179 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100180 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000181} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
Paul Bakkera3d195c2011-11-27 21:07:34 +0000183static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000185 size_t use_len;
186 int rnd;
187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 if( rng_state != NULL )
189 rng_state = NULL;
190
Paul Bakkera3d195c2011-11-27 21:07:34 +0000191 while( len > 0 )
192 {
193 use_len = len;
194 if( use_len > sizeof(int) )
195 use_len = sizeof(int);
196
197 rnd = rand();
198 memcpy( output, &rnd, use_len );
199 output += use_len;
200 len -= use_len;
201 }
202
203 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204}
205
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100206/*
207 * Clear some memory that was used to prepare the context
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_ECP_C)
210void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100211{
212 if( grp->T != NULL )
213 {
214 size_t i;
215 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_ecp_point_free( &grp->T[i] );
217 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100218 }
219 grp->T = NULL;
220 grp->T_size = 0;
221}
222#else
223#define ecp_clear_precomputed( g )
224#endif
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226unsigned char buf[BUFSIZE];
227
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200228typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200229 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100230 arc4, des3, des,
Jaeden Ameroc00cac72018-05-02 17:38:00 +0100231 aes_cbc, aes_gcm, aes_ccm, aes_cmac, aes_xts, des3_cmac,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100232 camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100233 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200234 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200235} todo_list;
236
Paul Bakkercce9d772011-11-18 14:26:47 +0000237int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000238{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100239 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200240 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200241 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200242 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200244 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000245#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000246
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000247 if( argc <= 1 )
248 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200249 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000250 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200251 else
252 {
253 memset( &todo, 0, sizeof( todo ) );
254
255 for( i = 1; i < argc; i++ )
256 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200257 if( strcmp( argv[i], "md4" ) == 0 )
258 todo.md4 = 1;
259 else if( strcmp( argv[i], "md5" ) == 0 )
260 todo.md5 = 1;
261 else if( strcmp( argv[i], "ripemd160" ) == 0 )
262 todo.ripemd160 = 1;
263 else if( strcmp( argv[i], "sha1" ) == 0 )
264 todo.sha1 = 1;
265 else if( strcmp( argv[i], "sha256" ) == 0 )
266 todo.sha256 = 1;
267 else if( strcmp( argv[i], "sha512" ) == 0 )
268 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200269 else if( strcmp( argv[i], "arc4" ) == 0 )
270 todo.arc4 = 1;
271 else if( strcmp( argv[i], "des3" ) == 0 )
272 todo.des3 = 1;
273 else if( strcmp( argv[i], "des" ) == 0 )
274 todo.des = 1;
275 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
276 todo.aes_cbc = 1;
277 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
278 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200279 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
280 todo.aes_ccm = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100281 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
282 todo.aes_cmac = 1;
Jaeden Ameroc00cac72018-05-02 17:38:00 +0100283 else if( strcmp( argv[i], "aes_xts" ) == 0 )
284 todo.aes_xts = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100285 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
286 todo.des3_cmac = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200287 else if( strcmp( argv[i], "camellia" ) == 0 )
288 todo.camellia = 1;
289 else if( strcmp( argv[i], "blowfish" ) == 0 )
290 todo.blowfish = 1;
291 else if( strcmp( argv[i], "havege" ) == 0 )
292 todo.havege = 1;
293 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
294 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100295 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
296 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297 else if( strcmp( argv[i], "rsa" ) == 0 )
298 todo.rsa = 1;
299 else if( strcmp( argv[i], "dhm" ) == 0 )
300 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200301 else if( strcmp( argv[i], "ecdsa" ) == 0 )
302 todo.ecdsa = 1;
303 else if( strcmp( argv[i], "ecdh" ) == 0 )
304 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200305 else
306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
308 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200309 }
310 }
311 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200316 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000317#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200318 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200319 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200322 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100323 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324#endif
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200327 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100328 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329#endif
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200332 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100333 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100334#endif
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200337 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100338 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339#endif
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200342 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100343 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344#endif
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200347 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100348 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000349#endif
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200352 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_arc4_context arc4;
355 mbedtls_arc4_init( &arc4 );
356 mbedtls_arc4_setup( &arc4, tmp, 32 );
357 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
358 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200359 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000360#endif
361
Simon Butcher549dc3d2016-10-05 14:14:19 +0100362#if defined(MBEDTLS_DES_C)
363#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200364 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_des3_context des3;
367 mbedtls_des3_init( &des3 );
368 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200369 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
371 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200372 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_des_context des;
377 mbedtls_des_init( &des );
378 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200379 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
381 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200382 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100383
384#endif /* MBEDTLS_CIPHER_MODE_CBC */
385#if defined(MBEDTLS_CMAC_C)
386 if( todo.des3_cmac )
387 {
388 unsigned char output[8];
389 const mbedtls_cipher_info_t *cipher_info;
390
391 memset( buf, 0, sizeof( buf ) );
392 memset( tmp, 0, sizeof( tmp ) );
393
394 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
395
396 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100397 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
398 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100399 }
400#endif /* MBEDTLS_CMAC_C */
401#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_AES_C)
404#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200405 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100407 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_aes_context aes;
409 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200410 for( keysize = 128; keysize <= 256; keysize += 64 )
411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 memset( buf, 0, sizeof( buf ) );
415 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200418 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200425 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000426 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100427 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200429
430 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 for( keysize = 128; keysize <= 256; keysize += 64 )
432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000434
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200435 memset( buf, 0, sizeof( buf ) );
436 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200437 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000438
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200439 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200441 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200444 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000445 }
446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200448 if( todo.aes_ccm )
449 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100450 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200452
453 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200454 for( keysize = 128; keysize <= 256; keysize += 64 )
455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200457
458 memset( buf, 0, sizeof( buf ) );
459 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200460 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200461
462 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200464 12, NULL, 0, buf, buf, tmp, 16 ) );
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200467 }
468 }
469#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100470#if defined(MBEDTLS_CMAC_C)
471 if( todo.aes_cmac )
472 {
473 unsigned char output[16];
474 const mbedtls_cipher_info_t *cipher_info;
475 mbedtls_cipher_type_t cipher_type;
476 int keysize;
477
478 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
479 keysize <= 256;
480 keysize += 64, cipher_type++ )
481 {
482 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
483
484 memset( buf, 0, sizeof( buf ) );
485 memset( tmp, 0, sizeof( tmp ) );
486
487 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
488
489 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100490 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
491 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100492 }
493
494 memset( buf, 0, sizeof( buf ) );
495 memset( tmp, 0, sizeof( tmp ) );
496 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100497 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
498 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100499 }
500#endif /* MBEDTLS_CMAC_C */
Jaeden Ameroc00cac72018-05-02 17:38:00 +0100501#if defined(MBEDTLS_CIPHER_MODE_XTS)
502 if( todo.aes_xts )
503 {
504 int keysize;
505 mbedtls_aes_xts_context ctx;
506
507 mbedtls_aes_xts_init( &ctx );
508 for( keysize = 128; keysize <= 256; keysize += 64 )
509 {
510 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
511
512 memset( buf, 0, sizeof( buf ) );
513 memset( tmp, 0, sizeof( tmp ) );
514 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2);
515
516 TIME_AND_TSC( title,
517 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
518 tmp, buf, buf ) );
519
520 mbedtls_aes_xts_free( &ctx );
521 }
522 }
523#endif /* MBEDTLS_CIPHER_MODE_XTS */
Simon Butcher549dc3d2016-10-05 14:14:19 +0100524#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200527 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000528 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100529 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_camellia_context camellia;
531 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200532 for( keysize = 128; keysize <= 256; keysize += 64 )
533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000535
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200536 memset( buf, 0, sizeof( buf ) );
537 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000539
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200540 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200542 BUFSIZE, tmp, buf, buf ) );
543 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000545 }
546#endif
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200549 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000550 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100551 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_blowfish_context blowfish;
553 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200554
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200555 for( keysize = 128; keysize <= 256; keysize += 64 )
556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000558
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200559 memset( buf, 0, sizeof( buf ) );
560 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000562
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200563 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200565 tmp, buf, buf ) );
566 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000569 }
570#endif
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200573 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_havege_state hs;
576 mbedtls_havege_init( &hs );
577 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
578 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200579 }
Paul Bakker02faf452011-11-29 11:23:58 +0000580#endif
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200583 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000586
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200587 mbedtls_ctr_drbg_init( &ctr_drbg );
588
589 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200591 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
593 mbedtls_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000594
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200595 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_exit(1);
597 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200598 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
600 mbedtls_exit(1) );
601 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200602 }
Paul Bakker02faf452011-11-29 11:23:58 +0000603#endif
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100606 if( todo.hmac_drbg )
607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_hmac_drbg_context hmac_drbg;
609 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100610
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200611 mbedtls_hmac_drbg_init( &hmac_drbg );
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613#if defined(MBEDTLS_SHA1_C)
614 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
615 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100616
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200617 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100619 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
621 mbedtls_exit(1) );
622 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100623
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200624 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_exit(1);
626 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
627 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100628 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
630 mbedtls_exit(1) );
631 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100632#endif
633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#if defined(MBEDTLS_SHA256_C)
635 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
636 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100637
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200638 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100640 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
642 mbedtls_exit(1) );
643 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100644
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200645 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_exit(1);
647 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
648 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100649 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
651 mbedtls_exit(1) );
652 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100653#endif
654 }
655#endif
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200658 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100660 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200662 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
667 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200668
669 TIME_PUBLIC( title, " public",
670 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200672
673 TIME_PUBLIC( title, "private",
674 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200678 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000680#endif
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200683 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100684 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200685 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800686 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100687 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800688 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100689 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800690 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100691 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800692 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100693 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
694
695 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
696 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
697 sizeof( dhm_P_3072 ) };
698
699 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
700 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
701 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200704 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200705 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200708
Hanno Beckerb9539212017-10-04 13:13:34 +0100709 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
710 dhm_P_size[i] ) != 0 ||
711 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
712 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200715 }
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 dhm.len = mbedtls_mpi_size( &dhm.P );
718 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
719 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
720 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200723 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200725 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100726 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200729 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100730 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200733 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100734 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100735#endif
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200738 if( todo.ecdsa )
739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_ecdsa_context ecdsa;
741 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200742 size_t sig_len;
743
744 memset( buf, 0x2A, sizeof( buf ) );
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 for( curve_info = mbedtls_ecp_curve_list();
747 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200748 curve_info++ )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
753 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100754 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200757 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200758 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200759 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200760 tmp, &sig_len, myrand, NULL ) );
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100763 }
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 for( curve_info = mbedtls_ecp_curve_list();
766 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100767 curve_info++ )
768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200772 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100773 tmp, &sig_len, myrand, NULL ) != 0 )
774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100776 }
777 ecp_clear_precomputed( &ecdsa.grp );
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100780 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200781 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200782 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200783 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200786 }
787 }
788#endif
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200791 if( todo.ecdh )
792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000795 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
796#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
797 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000798#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000799#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
800 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
801#endif
802 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
803 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200805 size_t olen;
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 for( curve_info = mbedtls_ecp_curve_list();
808 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200809 curve_info++ )
810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200812
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200813 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200815 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200819 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100820 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200823 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200824 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200826 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200828 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100830 }
831
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000832 /* Montgomery curves need to be handled separately */
833 for ( curve_info = montgomery_curve_list;
834 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
835 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000836 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000837 mbedtls_ecdh_init( &ecdh );
838 mbedtls_mpi_init( &z );
839
840 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
841 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
842 {
843 mbedtls_exit( 1 );
844 }
845
846 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
847 curve_info->name );
848 TIME_PUBLIC( title, "handshake",
849 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
850 myrand, NULL );
851 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
852 myrand, NULL ) );
853
854 mbedtls_ecdh_free( &ecdh );
855 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000856 }
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 for( curve_info = mbedtls_ecp_curve_list();
859 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100860 curve_info++ )
861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200864 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100866 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
868 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100869 myrand, NULL ) != 0 )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100872 }
873 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200876 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200877 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200879 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200881 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000882
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000883 /* Montgomery curves need to be handled separately */
884 for ( curve_info = montgomery_curve_list;
885 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
886 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000887 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000888 mbedtls_ecdh_init( &ecdh );
889 mbedtls_mpi_init( &z );
890
891 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
892 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
893 myrand, NULL ) != 0 ||
894 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
895 {
896 mbedtls_exit( 1 );
897 }
898
899 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
900 curve_info->name );
901 TIME_PUBLIC( title, "handshake",
902 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
903 myrand, NULL ) );
904
905 mbedtls_ecdh_free( &ecdh );
906 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000907 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200908 }
909#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
914 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000915#endif
916
Paul Bakkercce9d772011-11-18 14:26:47 +0000917#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919 fflush( stdout ); getchar();
920#endif
921
922 return( 0 );
923}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#endif /* MBEDTLS_TIMING_C */