blob: 61abba1452c6ad4ebdff79e25d76079f0f35a7bd [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"
Daniel King34b822c2016-05-15 17:28:08 -030064#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/gcm.h"
66#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +020067#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010068#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030069#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010070
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/havege.h"
72#include "mbedtls/ctr_drbg.h"
73#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/rsa.h"
76#include "mbedtls/dhm.h"
77#include "mbedtls/ecdsa.h"
78#include "mbedtls/ecdh.h"
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +010079
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000084#endif
85
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000086/*
87 * For heap usage estimates, we need an estimate of the overhead per allocated
88 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
89 * so use that as our baseline.
90 */
91#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
92
93/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020094 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000095 */
96#define HEAP_SIZE (1u << 16) // 64k
97
Paul Bakker02faf452011-11-29 11:23:58 +000098#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010099#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100100#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000101
Rich Evans85b05ec2015-02-12 11:37:29 +0000102#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200103 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -0300104 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Manuel Pégourié-Gonnard9c82e2c2018-06-04 12:30:04 +0200105 "aes_cbc, aes_gcm, aes_ccm, chachapoly,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -0300106 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000107 "havege, ctr_drbg, hmac_drbg\n" \
108 "rsa, dhm, ecdsa, ecdh.\n"
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000111#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100112 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000114#else
115#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000117#endif
118
119#define TIME_AND_TSC( TITLE, CODE ) \
120do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200121 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000122 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200123 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000124 fflush( stdout ); \
125 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200126 mbedtls_set_alarm( 1 ); \
127 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000128 { \
129 CODE; \
130 } \
131 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200132 tsc = mbedtls_timing_hardclock(); \
133 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000134 { \
135 CODE; \
136 } \
137 \
Ron Eldor0728d692017-11-29 12:08:35 +0200138 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200139 ii * BUFSIZE / 1024, \
140 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000141} while( 0 )
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100144
145#define MEMORY_MEASURE_INIT \
146 size_t max_used, max_blocks, max_bytes; \
147 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200148 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100150
151#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200152 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
153 for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100154 max_used -= prv_used; \
155 max_blocks -= prv_blocks; \
156 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100158
159#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000160#define MEMORY_MEASURE_INIT
161#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100162#endif
163
Rich Evans85b05ec2015-02-12 11:37:29 +0000164#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
165do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200166 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000167 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100168 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000169 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200170 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000171 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200172 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000173 \
174 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200175 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000176 { \
177 CODE; \
178 } \
179 \
180 if( ret != 0 ) \
181 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100182 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000183 } \
184 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100185 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200186 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100187 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200188 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100189 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000190} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Paul Bakkera3d195c2011-11-27 21:07:34 +0000192static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000194 size_t use_len;
195 int rnd;
196
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 if( rng_state != NULL )
198 rng_state = NULL;
199
Paul Bakkera3d195c2011-11-27 21:07:34 +0000200 while( len > 0 )
201 {
202 use_len = len;
203 if( use_len > sizeof(int) )
204 use_len = sizeof(int);
205
206 rnd = rand();
207 memcpy( output, &rnd, use_len );
208 output += use_len;
209 len -= use_len;
210 }
211
212 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213}
214
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100215/*
216 * Clear some memory that was used to prepare the context
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_ECP_C)
219void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100220{
221 if( grp->T != NULL )
222 {
223 size_t i;
224 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_ecp_point_free( &grp->T[i] );
226 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100227 }
228 grp->T = NULL;
229 grp->T_size = 0;
230}
231#else
232#define ecp_clear_precomputed( g )
233#endif
234
Paul Bakker5121ce52009-01-03 21:22:43 +0000235unsigned char buf[BUFSIZE];
236
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200237typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200238 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100239 arc4, des3, des,
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200240 aes_cbc, aes_gcm, aes_ccm, chachapoly,
241 aes_cmac, des3_cmac,
Manuel Pégourié-Gonnard39b19042018-06-07 12:01:33 +0200242 aria, camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300243 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100244 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200245 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200246} todo_list;
247
Paul Bakkercce9d772011-11-18 14:26:47 +0000248int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000249{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100250 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200251 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200252 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200253 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200255 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000256#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000257
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000258 if( argc <= 1 )
259 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200260 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000261 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200262 else
263 {
264 memset( &todo, 0, sizeof( todo ) );
265
266 for( i = 1; i < argc; i++ )
267 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200268 if( strcmp( argv[i], "md4" ) == 0 )
269 todo.md4 = 1;
270 else if( strcmp( argv[i], "md5" ) == 0 )
271 todo.md5 = 1;
272 else if( strcmp( argv[i], "ripemd160" ) == 0 )
273 todo.ripemd160 = 1;
274 else if( strcmp( argv[i], "sha1" ) == 0 )
275 todo.sha1 = 1;
276 else if( strcmp( argv[i], "sha256" ) == 0 )
277 todo.sha256 = 1;
278 else if( strcmp( argv[i], "sha512" ) == 0 )
279 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200280 else if( strcmp( argv[i], "arc4" ) == 0 )
281 todo.arc4 = 1;
282 else if( strcmp( argv[i], "des3" ) == 0 )
283 todo.des3 = 1;
284 else if( strcmp( argv[i], "des" ) == 0 )
285 todo.des = 1;
286 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
287 todo.aes_cbc = 1;
288 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
289 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200290 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
291 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200292 else if( strcmp( argv[i], "chachapoly" ) == 0 )
293 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100294 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
295 todo.aes_cmac = 1;
296 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
297 todo.des3_cmac = 1;
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100298 else if( strcmp( argv[i], "aria" ) == 0 )
299 todo.aria = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200300 else if( strcmp( argv[i], "camellia" ) == 0 )
301 todo.camellia = 1;
302 else if( strcmp( argv[i], "blowfish" ) == 0 )
303 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300304 else if( strcmp( argv[i], "chacha20" ) == 0 )
305 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300306 else if( strcmp( argv[i], "poly1305" ) == 0 )
307 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200308 else if( strcmp( argv[i], "havege" ) == 0 )
309 todo.havege = 1;
310 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
311 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100312 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
313 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200314 else if( strcmp( argv[i], "rsa" ) == 0 )
315 todo.rsa = 1;
316 else if( strcmp( argv[i], "dhm" ) == 0 )
317 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200318 else if( strcmp( argv[i], "ecdsa" ) == 0 )
319 todo.ecdsa = 1;
320 else if( strcmp( argv[i], "ecdh" ) == 0 )
321 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200322 else
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
325 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200326 }
327 }
328 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200333 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000334#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200335 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200336 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200339 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100340 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341#endif
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200344 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100345 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346#endif
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200349 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100350 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100351#endif
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200354 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100355 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356#endif
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200359 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100360 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361#endif
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200364 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100365 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000366#endif
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200369 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_arc4_context arc4;
372 mbedtls_arc4_init( &arc4 );
373 mbedtls_arc4_setup( &arc4, tmp, 32 );
374 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
375 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000377#endif
378
Simon Butcher549dc3d2016-10-05 14:14:19 +0100379#if defined(MBEDTLS_DES_C)
380#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200381 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_des3_context des3;
384 mbedtls_des3_init( &des3 );
385 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200386 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
388 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200389 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200391 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_des_context des;
394 mbedtls_des_init( &des );
395 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200396 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
398 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200399 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100400
401#endif /* MBEDTLS_CIPHER_MODE_CBC */
402#if defined(MBEDTLS_CMAC_C)
403 if( todo.des3_cmac )
404 {
405 unsigned char output[8];
406 const mbedtls_cipher_info_t *cipher_info;
407
408 memset( buf, 0, sizeof( buf ) );
409 memset( tmp, 0, sizeof( tmp ) );
410
411 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
412
413 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100414 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
415 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100416 }
417#endif /* MBEDTLS_CMAC_C */
418#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_AES_C)
421#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200422 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100424 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_aes_context aes;
426 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200427 for( keysize = 128; keysize <= 256; keysize += 64 )
428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 memset( buf, 0, sizeof( buf ) );
432 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200435 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200440#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200442 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000443 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100444 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200446
447 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200448 for( keysize = 128; keysize <= 256; keysize += 64 )
449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000451
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200452 memset( buf, 0, sizeof( buf ) );
453 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200454 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000455
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200456 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200458 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200461 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000462 }
463#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200465 if( todo.aes_ccm )
466 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100467 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200469
470 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200471 for( keysize = 128; keysize <= 256; keysize += 64 )
472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200474
475 memset( buf, 0, sizeof( buf ) );
476 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200477 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200478
479 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200481 12, NULL, 0, buf, buf, tmp, 16 ) );
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200484 }
485 }
486#endif
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200487#if defined(MBEDTLS_CHACHAPOLY_C)
488 if( todo.chachapoly )
489 {
490 mbedtls_chachapoly_context chachapoly;
491
492 mbedtls_chachapoly_init( &chachapoly );
493 memset( buf, 0, sizeof( buf ) );
494 memset( tmp, 0, sizeof( tmp ) );
495
496 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
497
498 mbedtls_chachapoly_setkey( &chachapoly, tmp );
499
500 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard3dc62a02018-06-04 12:18:19 +0200501 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
502 BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
Manuel Pégourié-Gonnardd6aea182018-05-09 10:21:28 +0200503
504 mbedtls_chachapoly_free( &chachapoly );
505 }
506#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100507#if defined(MBEDTLS_CMAC_C)
508 if( todo.aes_cmac )
509 {
510 unsigned char output[16];
511 const mbedtls_cipher_info_t *cipher_info;
512 mbedtls_cipher_type_t cipher_type;
513 int keysize;
514
515 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
516 keysize <= 256;
517 keysize += 64, cipher_type++ )
518 {
519 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
520
521 memset( buf, 0, sizeof( buf ) );
522 memset( tmp, 0, sizeof( tmp ) );
523
524 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
525
526 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100527 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
528 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100529 }
530
531 memset( buf, 0, sizeof( buf ) );
532 memset( tmp, 0, sizeof( tmp ) );
533 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100534 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
535 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100536 }
537#endif /* MBEDTLS_CMAC_C */
538#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Manuel Pégourié-Gonnard62e813c2018-02-21 10:47:47 +0100540#if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
541 if( todo.aria )
542 {
543 int keysize;
544 mbedtls_aria_context aria;
545 mbedtls_aria_init( &aria );
546 for( keysize = 128; keysize <= 256; keysize += 64 )
547 {
548 mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
549
550 memset( buf, 0, sizeof( buf ) );
551 memset( tmp, 0, sizeof( tmp ) );
552 mbedtls_aria_setkey_enc( &aria, tmp, keysize );
553
554 TIME_AND_TSC( title,
555 mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
556 BUFSIZE, tmp, buf, buf ) );
557 }
558 mbedtls_aria_free( &aria );
559 }
560#endif
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200563 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000564 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100565 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_camellia_context camellia;
567 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200568 for( keysize = 128; keysize <= 256; keysize += 64 )
569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000571
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200572 memset( buf, 0, sizeof( buf ) );
573 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000575
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200576 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200578 BUFSIZE, tmp, buf, buf ) );
579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000581 }
582#endif
583
Daniel King34b822c2016-05-15 17:28:08 -0300584#if defined(MBEDTLS_CHACHA20_C)
585 if ( todo.chacha20 )
586 {
587 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
588 }
589#endif
590
Daniel Kingadc32c02016-05-16 18:25:45 -0300591#if defined(MBEDTLS_POLY1305_C)
592 if ( todo.poly1305 )
593 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200594 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300595 }
596#endif
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200599 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000600 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100601 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_blowfish_context blowfish;
603 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200604
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200605 for( keysize = 128; keysize <= 256; keysize += 64 )
606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000608
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200609 memset( buf, 0, sizeof( buf ) );
610 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000612
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200613 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200615 tmp, buf, buf ) );
616 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000619 }
620#endif
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200623 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_havege_state hs;
626 mbedtls_havege_init( &hs );
627 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
628 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200629 }
Paul Bakker02faf452011-11-29 11:23:58 +0000630#endif
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200633 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000636
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200637 mbedtls_ctr_drbg_init( &ctr_drbg );
638
639 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200641 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
643 mbedtls_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000644
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200645 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_exit(1);
647 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200648 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
650 mbedtls_exit(1) );
651 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200652 }
Paul Bakker02faf452011-11-29 11:23:58 +0000653#endif
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100656 if( todo.hmac_drbg )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_hmac_drbg_context hmac_drbg;
659 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100660
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200661 mbedtls_hmac_drbg_init( &hmac_drbg );
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SHA1_C)
664 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
665 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100666
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200667 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100669 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
671 mbedtls_exit(1) );
672 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100673
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200674 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_exit(1);
676 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
677 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100678 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
680 mbedtls_exit(1) );
681 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100682#endif
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SHA256_C)
685 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
686 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100687
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200688 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100690 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
692 mbedtls_exit(1) );
693 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100694
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200695 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_exit(1);
697 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
698 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100699 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
701 mbedtls_exit(1) );
702 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100703#endif
704 }
705#endif
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200708 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100710 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200712 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
717 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200718
719 TIME_PUBLIC( title, " public",
720 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200722
723 TIME_PUBLIC( title, "private",
724 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200728 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000730#endif
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200733 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100734 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200735 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800736 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100737 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800738 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100739 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800740 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100741 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800742 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100743 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
744
745 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
746 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
747 sizeof( dhm_P_3072 ) };
748
749 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
750 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
751 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200754 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200755 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200758
Hanno Beckerb9539212017-10-04 13:13:34 +0100759 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
760 dhm_P_size[i] ) != 0 ||
761 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
762 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200765 }
766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 dhm.len = mbedtls_mpi_size( &dhm.P );
768 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
769 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
770 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200773 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200775 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100776 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200779 TIME_PUBLIC( title, "handshake",
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_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200783 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100784 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100785#endif
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200788 if( todo.ecdsa )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_ecdsa_context ecdsa;
791 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200792 size_t sig_len;
793
794 memset( buf, 0x2A, sizeof( buf ) );
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 for( curve_info = mbedtls_ecp_curve_list();
797 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200798 curve_info++ )
799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
803 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100804 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200807 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200808 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200809 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200810 tmp, &sig_len, myrand, NULL ) );
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100813 }
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 for( curve_info = mbedtls_ecp_curve_list();
816 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100817 curve_info++ )
818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200822 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100823 tmp, &sig_len, myrand, NULL ) != 0 )
824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100826 }
827 ecp_clear_precomputed( &ecdsa.grp );
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100830 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200831 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200832 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200833 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200836 }
837 }
838#endif
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200841 if( todo.ecdh )
842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000845 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
846#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
847 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000848#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000849#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
850 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
851#endif
852 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
853 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200855 size_t olen;
856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 for( curve_info = mbedtls_ecp_curve_list();
858 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200859 curve_info++ )
860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200862
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200863 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200865 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200869 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100870 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200873 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200876 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200878 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100880 }
881
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000882 /* Montgomery curves need to be handled separately */
883 for ( curve_info = montgomery_curve_list;
884 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
885 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000886 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000887 mbedtls_ecdh_init( &ecdh );
888 mbedtls_mpi_init( &z );
889
890 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
891 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
892 {
893 mbedtls_exit( 1 );
894 }
895
896 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
897 curve_info->name );
898 TIME_PUBLIC( title, "handshake",
899 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
900 myrand, NULL );
901 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
902 myrand, NULL ) );
903
904 mbedtls_ecdh_free( &ecdh );
905 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000906 }
907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 for( curve_info = mbedtls_ecp_curve_list();
909 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100910 curve_info++ )
911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100913
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200914 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100916 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
918 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100919 myrand, NULL ) != 0 )
920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100922 }
923 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200926 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200927 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200929 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200931 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000932
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000933 /* Montgomery curves need to be handled separately */
934 for ( curve_info = montgomery_curve_list;
935 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
936 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000937 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000938 mbedtls_ecdh_init( &ecdh );
939 mbedtls_mpi_init( &z );
940
941 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
942 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
943 myrand, NULL ) != 0 ||
944 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
945 {
946 mbedtls_exit( 1 );
947 }
948
949 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
950 curve_info->name );
951 TIME_PUBLIC( title, "handshake",
952 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
953 myrand, NULL ) );
954
955 mbedtls_ecdh_free( &ecdh );
956 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000957 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200958 }
959#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
964 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000965#endif
966
Paul Bakkercce9d772011-11-18 14:26:47 +0000967#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 fflush( stdout ); getchar();
970#endif
971
972 return( 0 );
973}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#endif /* MBEDTLS_TIMING_C */