blob: 91f5c8599f359d355be0a6b3c7321a7ccef87f23 [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_exit exit
61#define mbedtls_printf printf
62#define mbedtls_snprintf snprintf
63#define mbedtls_free free
Rich Evansf90016a2015-01-19 14:26:37 +000064#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000067int main( void )
68{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020070 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000071}
72#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010073
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000074#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020075#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010076
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/md4.h"
80#include "mbedtls/md5.h"
81#include "mbedtls/ripemd160.h"
82#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
85#include "mbedtls/arc4.h"
86#include "mbedtls/des.h"
87#include "mbedtls/aes.h"
88#include "mbedtls/blowfish.h"
89#include "mbedtls/camellia.h"
90#include "mbedtls/gcm.h"
91#include "mbedtls/ccm.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010092#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000093#include "mbedtls/havege.h"
94#include "mbedtls/ctr_drbg.h"
95#include "mbedtls/hmac_drbg.h"
96#include "mbedtls/rsa.h"
97#include "mbedtls/dhm.h"
98#include "mbedtls/ecdsa.h"
99#include "mbedtls/ecdh.h"
100#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000103#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +0000104#endif
105
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000106/*
107 * For heap usage estimates, we need an estimate of the overhead per allocated
108 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
109 * so use that as our baseline.
110 */
111#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
112
113/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200114 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000115 */
116#define HEAP_SIZE (1u << 16) // 64k
117
Paul Bakker02faf452011-11-29 11:23:58 +0000118#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100119#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +0100120#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000121
Rich Evans85b05ec2015-02-12 11:37:29 +0000122#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200123 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Simon Butcherb981b162016-10-06 10:27:22 +0100124 "arc4, des3, des, camellia, blowfish,\n" \
Simon Butcher549dc3d2016-10-05 14:14:19 +0100125 "aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000126 "havege, ctr_drbg, hmac_drbg\n" \
127 "rsa, dhm, ecdsa, ecdh.\n"
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000130#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100131 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000133#else
134#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000136#endif
137
138#define TIME_AND_TSC( TITLE, CODE ) \
139do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200140 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000141 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200142 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000143 fflush( stdout ); \
144 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200145 mbedtls_set_alarm( 1 ); \
146 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000147 { \
148 CODE; \
149 } \
150 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200151 tsc = mbedtls_timing_hardclock(); \
152 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000153 { \
154 CODE; \
155 } \
156 \
Ron Eldor0728d692017-11-29 12:08:35 +0200157 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200158 ii * BUFSIZE / 1024, \
159 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000160} while( 0 )
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163
Manuel Pégourié-Gonnard49e7a952020-04-09 10:40:03 +0200164/* How much space to reserve for the title when printing heap usage results.
165 * Updated manually as the output of the following command:
166 *
167 * sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
168 * awk '{print length+2}' | sort -rn | head -n1
169 *
170 * This computes the maximum length of a title +2 (because we appends "/s").
171 * (If the value is too small, the only consequence is poor alignement.) */
172#define TITLE_SPACE 11
173
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100174#define MEMORY_MEASURE_INIT \
175 size_t max_used, max_blocks, max_bytes; \
176 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200177 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100179
180#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200181 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Manuel Pégourié-Gonnard49e7a952020-04-09 10:40:03 +0200182 ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1; \
183 while( ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100184 max_used -= prv_used; \
185 max_blocks -= prv_blocks; \
186 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100188
189#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000190#define MEMORY_MEASURE_INIT
191#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100192#endif
193
Rich Evans85b05ec2015-02-12 11:37:29 +0000194#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
195do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200196 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000197 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100198 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000199 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200200 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000201 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200202 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000203 \
204 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200205 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000206 { \
207 CODE; \
208 } \
209 \
210 if( ret != 0 ) \
211 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100212 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000213 } \
214 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100215 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200216 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100217 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200218 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100219 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000220} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakkera3d195c2011-11-27 21:07:34 +0000222static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000223{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224 size_t use_len;
225 int rnd;
226
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 if( rng_state != NULL )
228 rng_state = NULL;
229
Paul Bakkera3d195c2011-11-27 21:07:34 +0000230 while( len > 0 )
231 {
232 use_len = len;
233 if( use_len > sizeof(int) )
234 use_len = sizeof(int);
235
236 rnd = rand();
237 memcpy( output, &rnd, use_len );
238 output += use_len;
239 len -= use_len;
240 }
241
242 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243}
244
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100245/*
246 * Clear some memory that was used to prepare the context
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_ECP_C)
249void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100250{
251 if( grp->T != NULL )
252 {
253 size_t i;
254 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ecp_point_free( &grp->T[i] );
256 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100257 }
258 grp->T = NULL;
259 grp->T_size = 0;
260}
261#else
262#define ecp_clear_precomputed( g )
263#endif
264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265unsigned char buf[BUFSIZE];
266
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200267typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200268 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100269 arc4, des3, des,
270 aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,
271 camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100272 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200273 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200274} todo_list;
275
Paul Bakkercce9d772011-11-18 14:26:47 +0000276int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000277{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100278 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200279 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200280 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200281 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200283 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000284#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000285
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000286 if( argc <= 1 )
287 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000289 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200290 else
291 {
292 memset( &todo, 0, sizeof( todo ) );
293
294 for( i = 1; i < argc; i++ )
295 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200296 if( strcmp( argv[i], "md4" ) == 0 )
297 todo.md4 = 1;
298 else if( strcmp( argv[i], "md5" ) == 0 )
299 todo.md5 = 1;
300 else if( strcmp( argv[i], "ripemd160" ) == 0 )
301 todo.ripemd160 = 1;
302 else if( strcmp( argv[i], "sha1" ) == 0 )
303 todo.sha1 = 1;
304 else if( strcmp( argv[i], "sha256" ) == 0 )
305 todo.sha256 = 1;
306 else if( strcmp( argv[i], "sha512" ) == 0 )
307 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200308 else if( strcmp( argv[i], "arc4" ) == 0 )
309 todo.arc4 = 1;
310 else if( strcmp( argv[i], "des3" ) == 0 )
311 todo.des3 = 1;
312 else if( strcmp( argv[i], "des" ) == 0 )
313 todo.des = 1;
314 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
315 todo.aes_cbc = 1;
316 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
317 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200318 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
319 todo.aes_ccm = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100320 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
321 todo.aes_cmac = 1;
322 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
323 todo.des3_cmac = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200324 else if( strcmp( argv[i], "camellia" ) == 0 )
325 todo.camellia = 1;
326 else if( strcmp( argv[i], "blowfish" ) == 0 )
327 todo.blowfish = 1;
328 else if( strcmp( argv[i], "havege" ) == 0 )
329 todo.havege = 1;
330 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
331 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100332 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
333 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200334 else if( strcmp( argv[i], "rsa" ) == 0 )
335 todo.rsa = 1;
336 else if( strcmp( argv[i], "dhm" ) == 0 )
337 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200338 else if( strcmp( argv[i], "ecdsa" ) == 0 )
339 todo.ecdsa = 1;
340 else if( strcmp( argv[i], "ecdh" ) == 0 )
341 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 else
343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
345 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200346 }
347 }
348 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200353 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000354#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200355 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200356 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200359 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100360 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361#endif
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200364 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100365 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366#endif
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200369 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100370 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100371#endif
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200374 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100375 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376#endif
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200379 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100380 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381#endif
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200384 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100385 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000386#endif
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200389 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_arc4_context arc4;
392 mbedtls_arc4_init( &arc4 );
393 mbedtls_arc4_setup( &arc4, tmp, 32 );
394 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
395 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200396 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000397#endif
398
Simon Butcher549dc3d2016-10-05 14:14:19 +0100399#if defined(MBEDTLS_DES_C)
400#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200401 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_des3_context des3;
404 mbedtls_des3_init( &des3 );
405 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200406 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
408 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200411 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_des_context des;
414 mbedtls_des_init( &des );
415 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200416 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
418 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100420
421#endif /* MBEDTLS_CIPHER_MODE_CBC */
422#if defined(MBEDTLS_CMAC_C)
423 if( todo.des3_cmac )
424 {
425 unsigned char output[8];
426 const mbedtls_cipher_info_t *cipher_info;
427
428 memset( buf, 0, sizeof( buf ) );
429 memset( tmp, 0, sizeof( tmp ) );
430
431 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
432
433 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100434 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
435 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100436 }
437#endif /* MBEDTLS_CMAC_C */
438#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#if defined(MBEDTLS_AES_C)
441#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200442 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100444 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_aes_context aes;
446 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200447 for( keysize = 128; keysize <= 256; keysize += 64 )
448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200451 memset( buf, 0, sizeof( buf ) );
452 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200455 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200460#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200462 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000463 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100464 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200466
467 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200468 for( keysize = 128; keysize <= 256; keysize += 64 )
469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000471
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200472 memset( buf, 0, sizeof( buf ) );
473 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200474 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000475
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200476 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200478 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200481 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000482 }
483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200485 if( todo.aes_ccm )
486 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100487 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200489
490 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200491 for( keysize = 128; keysize <= 256; keysize += 64 )
492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200494
495 memset( buf, 0, sizeof( buf ) );
496 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200497 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200498
499 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200501 12, NULL, 0, buf, buf, tmp, 16 ) );
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200504 }
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200541 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000542 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100543 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 mbedtls_camellia_context camellia;
545 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200546 for( keysize = 128; keysize <= 256; keysize += 64 )
547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000549
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200550 memset( buf, 0, sizeof( buf ) );
551 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000553
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200554 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200556 BUFSIZE, tmp, buf, buf ) );
557 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000559 }
560#endif
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200563 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000564 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100565 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_blowfish_context blowfish;
567 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200568
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200569 for( keysize = 128; keysize <= 256; keysize += 64 )
570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000572
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200573 memset( buf, 0, sizeof( buf ) );
574 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000576
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200577 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200579 tmp, buf, buf ) );
580 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000583 }
584#endif
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200587 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_havege_state hs;
590 mbedtls_havege_init( &hs );
591 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
592 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200593 }
Paul Bakker02faf452011-11-29 11:23:58 +0000594#endif
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200597 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000600
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200601 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200602 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200604 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
606 mbedtls_exit(1) );
Gilles Peskineeab4d702019-10-28 17:28:46 +0100607 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker02faf452011-11-29 11:23:58 +0000608
Gilles Peskineeab4d702019-10-28 17:28:46 +0100609 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200610 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_exit(1);
612 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200613 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
615 mbedtls_exit(1) );
616 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200617 }
Paul Bakker02faf452011-11-29 11:23:58 +0000618#endif
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100621 if( todo.hmac_drbg )
622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_hmac_drbg_context hmac_drbg;
624 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100625
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200626 mbedtls_hmac_drbg_init( &hmac_drbg );
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SHA1_C)
629 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
630 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100631
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200632 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100634 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
636 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100637
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200638 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_exit(1);
640 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
641 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100642 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
644 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100645#endif
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#if defined(MBEDTLS_SHA256_C)
648 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
649 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100650
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200651 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100653 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
655 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100656
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200657 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_exit(1);
659 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
660 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100661 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
663 mbedtls_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100664#endif
Ron Eldorf19a7ab2018-06-20 18:40:21 +0300665 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100666 }
667#endif
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200670 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100672 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200674 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
679 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200680
681 TIME_PUBLIC( title, " public",
682 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200684
685 TIME_PUBLIC( title, "private",
686 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200690 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692#endif
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200695 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100696 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200697 int dhm_sizes[] = { 2048, 3072 };
Brendan Shanks968cda12018-03-08 17:40:56 -0800698 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100699 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shanks968cda12018-03-08 17:40:56 -0800700 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100701 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shanks968cda12018-03-08 17:40:56 -0800702 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100703 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shanks968cda12018-03-08 17:40:56 -0800704 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100705 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
706
707 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
708 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
709 sizeof( dhm_P_3072 ) };
710
711 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
712 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
713 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200716 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200717 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200720
Hanno Beckerb9539212017-10-04 13:13:34 +0100721 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
722 dhm_P_size[i] ) != 0 ||
723 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
724 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200727 }
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 dhm.len = mbedtls_mpi_size( &dhm.P );
730 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
731 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
732 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200735 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200737 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100738 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200741 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100742 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200745 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100746 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100747#endif
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200750 if( todo.ecdsa )
751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_ecdsa_context ecdsa;
753 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200754 size_t sig_len;
755
756 memset( buf, 0x2A, sizeof( buf ) );
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 for( curve_info = mbedtls_ecp_curve_list();
759 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200760 curve_info++ )
761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
765 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100766 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200769 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200770 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200771 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200772 tmp, &sig_len, myrand, NULL ) );
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100775 }
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 for( curve_info = mbedtls_ecp_curve_list();
778 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100779 curve_info++ )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200784 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100785 tmp, &sig_len, myrand, NULL ) != 0 )
786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100788 }
789 ecp_clear_precomputed( &ecdsa.grp );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100792 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200793 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200794 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200795 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200798 }
799 }
800#endif
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200803 if( todo.ecdh )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200806#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_mpi z;
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200810 size_t olen;
811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 for( curve_info = mbedtls_ecp_curve_list();
813 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200814 curve_info++ )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200817
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200818 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200820 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200824 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100825 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200828 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200829 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 ret |= mbedtls_ecdh_make_public( &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 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200833 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100835 }
836
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000837 /* Curve25519 needs to be handled separately */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200838#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_ecdh_init( &ecdh );
840 mbedtls_mpi_init( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000841
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200842 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000846 }
847
848 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000850 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000852 myrand, NULL ) );
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_ecdh_free( &ecdh );
855 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000856#endif
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 for( curve_info = mbedtls_ecp_curve_list();
859 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100860 curve_info++ )
861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200864 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100866 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
868 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100869 myrand, NULL ) != 0 )
870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100872 }
873 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200876 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200877 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200879 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200881 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000882
883 /* Curve25519 needs to be handled separately */
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200884#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_ecdh_init( &ecdh );
886 mbedtls_mpi_init( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000887
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200888 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000890 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000894 }
895
896 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000898 myrand, NULL ) );
899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_ecdh_free( &ecdh );
901 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000902#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200903 }
904#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
909 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000910#endif
911
Paul Bakkercce9d772011-11-18 14:26:47 +0000912#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 fflush( stdout ); getchar();
915#endif
916
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200917 mbedtls_exit( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000918}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#endif /* MBEDTLS_TIMING_C */