blob: d141454acfa7e6e23295697d43e0ffff0cbfb2c4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020057#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_exit exit
59#define mbedtls_printf printf
60#define mbedtls_snprintf snprintf
61#define mbedtls_free free
Rich Evansf90016a2015-01-19 14:26:37 +000062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000065int main( void )
66{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020068 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000069}
70#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010071
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000072#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020073#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010074
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/md4.h"
78#include "mbedtls/md5.h"
79#include "mbedtls/ripemd160.h"
80#include "mbedtls/sha1.h"
81#include "mbedtls/sha256.h"
82#include "mbedtls/sha512.h"
83#include "mbedtls/arc4.h"
84#include "mbedtls/des.h"
85#include "mbedtls/aes.h"
86#include "mbedtls/blowfish.h"
87#include "mbedtls/camellia.h"
88#include "mbedtls/gcm.h"
89#include "mbedtls/ccm.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010090#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000091#include "mbedtls/havege.h"
92#include "mbedtls/ctr_drbg.h"
93#include "mbedtls/hmac_drbg.h"
94#include "mbedtls/rsa.h"
95#include "mbedtls/dhm.h"
96#include "mbedtls/ecdsa.h"
97#include "mbedtls/ecdh.h"
98#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000101#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +0000102#endif
103
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000104/*
105 * For heap usage estimates, we need an estimate of the overhead per allocated
106 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
107 * so use that as our baseline.
108 */
109#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
110
111/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200112 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000113 */
114#define HEAP_SIZE (1u << 16) // 64k
115
Paul Bakker02faf452011-11-29 11:23:58 +0000116#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100117#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100118#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000119
Rich Evans85b05ec2015-02-12 11:37:29 +0000120#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200121 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Simon Butcherb981b162016-10-06 10:27:22 +0100122 "arc4, des3, des, camellia, blowfish,\n" \
Simon Butcher549dc3d2016-10-05 14:14:19 +0100123 "aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000124 "havege, ctr_drbg, hmac_drbg\n" \
125 "rsa, dhm, ecdsa, ecdh.\n"
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000128#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100129 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000131#else
132#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000134#endif
135
136#define TIME_AND_TSC( TITLE, CODE ) \
137do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200138 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000139 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200140 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000141 fflush( stdout ); \
142 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200143 mbedtls_set_alarm( 1 ); \
144 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000145 { \
146 CODE; \
147 } \
148 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200149 tsc = mbedtls_timing_hardclock(); \
150 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000151 { \
152 CODE; \
153 } \
154 \
Ron Eldor0728d692017-11-29 12:08:35 +0200155 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200156 ii * BUFSIZE / 1024, \
157 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000158} while( 0 )
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100161
Manuel Pégourié-Gonnard49e7a952020-04-09 10:40:03 +0200162/* How much space to reserve for the title when printing heap usage results.
163 * Updated manually as the output of the following command:
164 *
165 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
166 * awk '{print length+2}' | sort -rn | head -n1
167 *
168 * This computes the maximum length of a title +2 (because we appends "/s").
169 * (If the value is too small, the only consequence is poor alignement.) */
170#define TITLE_SPACE 11
171
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100172#define MEMORY_MEASURE_INIT \
173 size_t max_used, max_blocks, max_bytes; \
174 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200175 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100177
178#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200179 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnard49e7a952020-04-09 10:40:03 +0200180 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
181 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100182 max_used -= prv_used; \
183 max_blocks -= prv_blocks; \
184 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100186
187#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000188#define MEMORY_MEASURE_INIT
189#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100190#endif
191
Rich Evans85b05ec2015-02-12 11:37:29 +0000192#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
193do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200194 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000195 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100196 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000197 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200198 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000199 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200200 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000201 \
202 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200203 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000204 { \
205 CODE; \
206 } \
207 \
208 if( ret != 0 ) \
209 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100210 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000211 } \
212 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100213 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200214 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100215 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200216 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100217 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000218} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Paul Bakkera3d195c2011-11-27 21:07:34 +0000220static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000222 size_t use_len;
223 int rnd;
224
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 if( rng_state != NULL )
226 rng_state = NULL;
227
Paul Bakkera3d195c2011-11-27 21:07:34 +0000228 while( len > 0 )
229 {
230 use_len = len;
231 if( use_len > sizeof(int) )
232 use_len = sizeof(int);
233
234 rnd = rand();
235 memcpy( output, &rnd, use_len );
236 output += use_len;
237 len -= use_len;
238 }
239
240 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241}
242
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100243/*
244 * Clear some memory that was used to prepare the context
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_ECP_C)
247void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100248{
249 if( grp->T != NULL )
250 {
251 size_t i;
252 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_ecp_point_free( &grp->T[i] );
254 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100255 }
256 grp->T = NULL;
257 grp->T_size = 0;
258}
259#else
260#define ecp_clear_precomputed( g )
261#endif
262
Paul Bakker5121ce52009-01-03 21:22:43 +0000263unsigned char buf[BUFSIZE];
264
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200265typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200266 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100267 arc4, des3, des,
268 aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,
269 camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100270 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200271 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200272} todo_list;
273
Paul Bakkercce9d772011-11-18 14:26:47 +0000274int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000275{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100276 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200277 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200278 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200279 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200281 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000282#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000283
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000284 if( argc <= 1 )
285 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200286 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000287 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288 else
289 {
290 memset( &todo, 0, sizeof( todo ) );
291
292 for( i = 1; i < argc; i++ )
293 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200294 if( strcmp( argv[i], "md4" ) == 0 )
295 todo.md4 = 1;
296 else if( strcmp( argv[i], "md5" ) == 0 )
297 todo.md5 = 1;
298 else if( strcmp( argv[i], "ripemd160" ) == 0 )
299 todo.ripemd160 = 1;
300 else if( strcmp( argv[i], "sha1" ) == 0 )
301 todo.sha1 = 1;
302 else if( strcmp( argv[i], "sha256" ) == 0 )
303 todo.sha256 = 1;
304 else if( strcmp( argv[i], "sha512" ) == 0 )
305 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200306 else if( strcmp( argv[i], "arc4" ) == 0 )
307 todo.arc4 = 1;
308 else if( strcmp( argv[i], "des3" ) == 0 )
309 todo.des3 = 1;
310 else if( strcmp( argv[i], "des" ) == 0 )
311 todo.des = 1;
312 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
313 todo.aes_cbc = 1;
314 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
315 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200316 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
317 todo.aes_ccm = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100318 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
319 todo.aes_cmac = 1;
320 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
321 todo.des3_cmac = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200322 else if( strcmp( argv[i], "camellia" ) == 0 )
323 todo.camellia = 1;
324 else if( strcmp( argv[i], "blowfish" ) == 0 )
325 todo.blowfish = 1;
326 else if( strcmp( argv[i], "havege" ) == 0 )
327 todo.havege = 1;
328 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
329 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100330 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
331 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200332 else if( strcmp( argv[i], "rsa" ) == 0 )
333 todo.rsa = 1;
334 else if( strcmp( argv[i], "dhm" ) == 0 )
335 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200336 else if( strcmp( argv[i], "ecdsa" ) == 0 )
337 todo.ecdsa = 1;
338 else if( strcmp( argv[i], "ecdh" ) == 0 )
339 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200340 else
341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
343 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200344 }
345 }
346 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200351 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000352#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200353 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200354 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200357 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100358 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359#endif
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200362 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100363 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364#endif
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200367 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100368 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100369#endif
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200372 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100373 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374#endif
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200377 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100378 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379#endif
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200382 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100383 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000384#endif
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200387 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_arc4_context arc4;
390 mbedtls_arc4_init( &arc4 );
391 mbedtls_arc4_setup( &arc4, tmp, 32 );
392 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
393 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200394 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000395#endif
396
Simon Butcher549dc3d2016-10-05 14:14:19 +0100397#if defined(MBEDTLS_DES_C)
398#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200399 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_des3_context des3;
402 mbedtls_des3_init( &des3 );
403 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200404 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
406 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200407 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200409 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_des_context des;
412 mbedtls_des_init( &des );
413 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
416 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200417 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100418
419#endif /* MBEDTLS_CIPHER_MODE_CBC */
420#if defined(MBEDTLS_CMAC_C)
421 if( todo.des3_cmac )
422 {
423 unsigned char output[8];
424 const mbedtls_cipher_info_t *cipher_info;
425
426 memset( buf, 0, sizeof( buf ) );
427 memset( tmp, 0, sizeof( tmp ) );
428
429 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
430
431 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100432 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
433 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100434 }
435#endif /* MBEDTLS_CMAC_C */
436#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_AES_C)
439#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200440 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100442 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_aes_context aes;
444 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200445 for( keysize = 128; keysize <= 256; keysize += 64 )
446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200449 memset( buf, 0, sizeof( buf ) );
450 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200458#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200460 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000461 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100462 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200464
465 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200466 for( keysize = 128; keysize <= 256; keysize += 64 )
467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000469
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200470 memset( buf, 0, sizeof( buf ) );
471 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200472 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000473
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200474 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200476 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200479 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000480 }
481#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200483 if( todo.aes_ccm )
484 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100485 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200487
488 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200489 for( keysize = 128; keysize <= 256; keysize += 64 )
490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200492
493 memset( buf, 0, sizeof( buf ) );
494 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200495 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200496
497 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200499 12, NULL, 0, buf, buf, tmp, 16 ) );
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200502 }
503 }
504#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100505#if defined(MBEDTLS_CMAC_C)
506 if( todo.aes_cmac )
507 {
508 unsigned char output[16];
509 const mbedtls_cipher_info_t *cipher_info;
510 mbedtls_cipher_type_t cipher_type;
511 int keysize;
512
513 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
514 keysize <= 256;
515 keysize += 64, cipher_type++ )
516 {
517 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
518
519 memset( buf, 0, sizeof( buf ) );
520 memset( tmp, 0, sizeof( tmp ) );
521
522 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
523
524 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100525 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
526 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100527 }
528
529 memset( buf, 0, sizeof( buf ) );
530 memset( tmp, 0, sizeof( tmp ) );
531 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100532 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
533 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100534 }
535#endif /* MBEDTLS_CMAC_C */
536#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200539 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000540 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100541 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_camellia_context camellia;
543 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200544 for( keysize = 128; keysize <= 256; keysize += 64 )
545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000547
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200548 memset( buf, 0, sizeof( buf ) );
549 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000551
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200552 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200554 BUFSIZE, tmp, buf, buf ) );
555 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000557 }
558#endif
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200561 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000562 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100563 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_blowfish_context blowfish;
565 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200566
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200567 for( keysize = 128; keysize <= 256; keysize += 64 )
568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000570
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200571 memset( buf, 0, sizeof( buf ) );
572 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000574
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200575 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200577 tmp, buf, buf ) );
578 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000581 }
582#endif
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200585 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_havege_state hs;
588 mbedtls_havege_init( &hs );
589 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
590 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200591 }
Paul Bakker02faf452011-11-29 11:23:58 +0000592#endif
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200595 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000598
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200599 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200600 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200602 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
604 mbedtls_exit(1) );
Gilles Peskineeab4d702019-10-28 17:28:46 +0100605 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000606
Gilles Peskineeab4d702019-10-28 17:28:46 +0100607 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200608 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_exit(1);
610 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200611 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
613 mbedtls_exit(1) );
614 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200615 }
Paul Bakker02faf452011-11-29 11:23:58 +0000616#endif
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100619 if( todo.hmac_drbg )
620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_hmac_drbg_context hmac_drbg;
622 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100623
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200624 mbedtls_hmac_drbg_init( &hmac_drbg );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#if defined(MBEDTLS_SHA1_C)
627 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
628 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100629
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200630 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100632 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
634 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100635
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200636 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_exit(1);
638 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
639 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100640 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
642 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100643#endif
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#if defined(MBEDTLS_SHA256_C)
646 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
647 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100648
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200649 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100651 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
653 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100654
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200655 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_exit(1);
657 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
658 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100659 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
661 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100662#endif
Ron Eldorf19a7ab2018-06-20 18:40:21 +0300663 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100664 }
665#endif
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200668 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100670 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200672 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
677 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200678
679 TIME_PUBLIC( title, " public",
680 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200682
683 TIME_PUBLIC( title, "private",
684 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200688 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000690#endif
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200693 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100694 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200695 int dhm_sizes[] = { 2048, 3072 };
Brendan Shanks968cda12018-03-08 17:40:56 -0800696 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100697 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shanks968cda12018-03-08 17:40:56 -0800698 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100699 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shanks968cda12018-03-08 17:40:56 -0800700 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100701 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shanks968cda12018-03-08 17:40:56 -0800702 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100703 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
704
705 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
706 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
707 sizeof( dhm_P_3072 ) };
708
709 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
710 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
711 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200714 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200715 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200718
Hanno Beckerb9539212017-10-04 13:13:34 +0100719 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
720 dhm_P_size[i] ) != 0 ||
721 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
722 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200725 }
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 dhm.len = mbedtls_mpi_size( &dhm.P );
728 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
729 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
730 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200733 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200735 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100736 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200739 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100740 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200743 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100744 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100745#endif
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200748 if( todo.ecdsa )
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_ecdsa_context ecdsa;
751 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200752 size_t sig_len;
753
754 memset( buf, 0x2A, sizeof( buf ) );
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 for( curve_info = mbedtls_ecp_curve_list();
757 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200758 curve_info++ )
759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
763 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100764 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200767 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200768 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200769 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200770 tmp, &sig_len, myrand, NULL ) );
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100773 }
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 for( curve_info = mbedtls_ecp_curve_list();
776 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100777 curve_info++ )
778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200782 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100783 tmp, &sig_len, myrand, NULL ) != 0 )
784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100786 }
787 ecp_clear_precomputed( &ecdsa.grp );
788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100790 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200791 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200792 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200793 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200796 }
797 }
798#endif
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200801 if( todo.ecdh )
802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200804#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_mpi z;
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200808 size_t olen;
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 for( curve_info = mbedtls_ecp_curve_list();
811 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200812 curve_info++ )
813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200815
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200816 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200818 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200822 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100823 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200826 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200827 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200829 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200831 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100833 }
834
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000835 /* Curve25519 needs to be handled separately */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200836#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_ecdh_init( &ecdh );
838 mbedtls_mpi_init( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000839
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200840 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000844 }
845
846 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000848 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000850 myrand, NULL ) );
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_ecdh_free( &ecdh );
853 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000854#endif
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 for( curve_info = mbedtls_ecp_curve_list();
857 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100858 curve_info++ )
859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100861
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200862 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100864 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
866 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100867 myrand, NULL ) != 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100870 }
871 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200874 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200875 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200877 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200879 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000880
881 /* Curve25519 needs to be handled separately */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200882#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 mbedtls_ecdh_init( &ecdh );
884 mbedtls_mpi_init( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000885
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200886 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000888 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000892 }
893
894 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000896 myrand, NULL ) );
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_ecdh_free( &ecdh );
899 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000900#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200901 }
902#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
907 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000908#endif
909
Paul Bakkercce9d772011-11-18 14:26:47 +0000910#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912 fflush( stdout ); getchar();
913#endif
914
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200915 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#endif /* MBEDTLS_TIMING_C */