blob: 98d231a55212aa7c719a773e8c2fe2504e60c875 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Simon Butcher549dc3d2016-10-05 14:14:19 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_exit exit
33#define mbedtls_printf printf
34#define mbedtls_snprintf snprintf
35#define mbedtls_free free
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000039int main( void )
40{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000042 return( 0 );
43}
44#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010045
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000046#include <string.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020047#include <stdlib.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010048
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/md4.h"
52#include "mbedtls/md5.h"
53#include "mbedtls/ripemd160.h"
54#include "mbedtls/sha1.h"
55#include "mbedtls/sha256.h"
56#include "mbedtls/sha512.h"
57#include "mbedtls/arc4.h"
58#include "mbedtls/des.h"
59#include "mbedtls/aes.h"
60#include "mbedtls/blowfish.h"
61#include "mbedtls/camellia.h"
Daniel Kinga98ff5e2016-05-15 17:28:08 -030062#include "mbedtls/chacha20.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/gcm.h"
64#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnardc975b2c2018-05-09 10:21:28 +020065#include "mbedtls/chachapoly.h"
Simon Butcher549dc3d2016-10-05 14:14:19 +010066#include "mbedtls/cmac.h"
Daniel King5d77eaa2016-05-16 18:25:45 -030067#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/havege.h"
69#include "mbedtls/ctr_drbg.h"
70#include "mbedtls/hmac_drbg.h"
71#include "mbedtls/rsa.h"
72#include "mbedtls/dhm.h"
73#include "mbedtls/ecdsa.h"
74#include "mbedtls/ecdh.h"
75#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000079#endif
80
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000081/*
82 * For heap usage estimates, we need an estimate of the overhead per allocated
83 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
84 * so use that as our baseline.
85 */
86#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
87
88/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020089 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000090 */
91#define HEAP_SIZE (1u << 16) // 64k
92
Paul Bakker02faf452011-11-29 11:23:58 +000093#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010094#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010095#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000096
Rich Evans85b05ec2015-02-12 11:37:29 +000097#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +020098 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel Kinga98ff5e2016-05-15 17:28:08 -030099 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Jaeden Ameroc00cac72018-05-02 17:38:00 +0100100 "aes_cbc, aes_gcm, aes_ccm, aes_cmac, aes_xts, des3_cmac,\n" \
Jaeden Amerob1e4fc62018-05-11 11:06:48 +0100101 "chachapoly, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000102 "havege, ctr_drbg, hmac_drbg\n" \
103 "rsa, dhm, ecdsa, ecdh.\n"
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000106#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100107 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000109#else
110#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000112#endif
113
114#define TIME_AND_TSC( TITLE, CODE ) \
115do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200116 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000117 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200118 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000119 fflush( stdout ); \
120 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200121 mbedtls_set_alarm( 1 ); \
122 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000123 { \
124 CODE; \
125 } \
126 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200127 tsc = mbedtls_timing_hardclock(); \
128 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000129 { \
130 CODE; \
131 } \
132 \
Ron Eldor0728d692017-11-29 12:08:35 +0200133 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200134 ii * BUFSIZE / 1024, \
135 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136} while( 0 )
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100139
140#define MEMORY_MEASURE_INIT \
141 size_t max_used, max_blocks, max_bytes; \
142 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200143 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100145
146#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200147 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
148 for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100149 max_used -= prv_used; \
150 max_blocks -= prv_blocks; \
151 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100153
154#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000155#define MEMORY_MEASURE_INIT
156#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100157#endif
158
Rich Evans85b05ec2015-02-12 11:37:29 +0000159#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
160do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200161 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000162 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000164 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200165 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000166 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200167 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000168 \
169 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200170 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000171 { \
172 CODE; \
173 } \
174 \
175 if( ret != 0 ) \
176 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100177 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000178 } \
179 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100180 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200181 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100182 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200183 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100184 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000185} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakkera3d195c2011-11-27 21:07:34 +0000187static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000189 size_t use_len;
190 int rnd;
191
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 if( rng_state != NULL )
193 rng_state = NULL;
194
Paul Bakkera3d195c2011-11-27 21:07:34 +0000195 while( len > 0 )
196 {
197 use_len = len;
198 if( use_len > sizeof(int) )
199 use_len = sizeof(int);
200
201 rnd = rand();
202 memcpy( output, &rnd, use_len );
203 output += use_len;
204 len -= use_len;
205 }
206
207 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208}
209
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100210/*
211 * Clear some memory that was used to prepare the context
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_ECP_C)
214void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100215{
216 if( grp->T != NULL )
217 {
218 size_t i;
219 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_ecp_point_free( &grp->T[i] );
221 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100222 }
223 grp->T = NULL;
224 grp->T_size = 0;
225}
226#else
227#define ecp_clear_precomputed( g )
228#endif
229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230unsigned char buf[BUFSIZE];
231
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200232typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200233 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100234 arc4, des3, des,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100235 camellia, blowfish,
Jaeden Amerob1e4fc62018-05-11 11:06:48 +0100236 chacha20,
237 aes_cbc, aes_gcm, aes_ccm, aes_cmac, aes_xts, des3_cmac,
238 chachapoly, poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100239 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200240 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200241} todo_list;
242
Paul Bakkercce9d772011-11-18 14:26:47 +0000243int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000244{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100245 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200246 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200247 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200248 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200250 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000251#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000252
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000253 if( argc <= 1 )
254 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000256 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200257 else
258 {
259 memset( &todo, 0, sizeof( todo ) );
260
261 for( i = 1; i < argc; i++ )
262 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200263 if( strcmp( argv[i], "md4" ) == 0 )
264 todo.md4 = 1;
265 else if( strcmp( argv[i], "md5" ) == 0 )
266 todo.md5 = 1;
267 else if( strcmp( argv[i], "ripemd160" ) == 0 )
268 todo.ripemd160 = 1;
269 else if( strcmp( argv[i], "sha1" ) == 0 )
270 todo.sha1 = 1;
271 else if( strcmp( argv[i], "sha256" ) == 0 )
272 todo.sha256 = 1;
273 else if( strcmp( argv[i], "sha512" ) == 0 )
274 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200275 else if( strcmp( argv[i], "arc4" ) == 0 )
276 todo.arc4 = 1;
277 else if( strcmp( argv[i], "des3" ) == 0 )
278 todo.des3 = 1;
279 else if( strcmp( argv[i], "des" ) == 0 )
280 todo.des = 1;
281 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
282 todo.aes_cbc = 1;
283 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
284 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200285 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
286 todo.aes_ccm = 1;
Manuel Pégourié-Gonnardc975b2c2018-05-09 10:21:28 +0200287 else if( strcmp( argv[i], "chachapoly" ) == 0 )
288 todo.chachapoly = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100289 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
290 todo.aes_cmac = 1;
Jaeden Ameroc00cac72018-05-02 17:38:00 +0100291 else if( strcmp( argv[i], "aes_xts" ) == 0 )
292 todo.aes_xts = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100293 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
294 todo.des3_cmac = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200295 else if( strcmp( argv[i], "camellia" ) == 0 )
296 todo.camellia = 1;
297 else if( strcmp( argv[i], "blowfish" ) == 0 )
298 todo.blowfish = 1;
Daniel Kinga98ff5e2016-05-15 17:28:08 -0300299 else if( strcmp( argv[i], "chacha20" ) == 0 )
300 todo.chacha20 = 1;
Daniel King5d77eaa2016-05-16 18:25:45 -0300301 else if( strcmp( argv[i], "poly1305" ) == 0 )
302 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200303 else if( strcmp( argv[i], "havege" ) == 0 )
304 todo.havege = 1;
305 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
306 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100307 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
308 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200309 else if( strcmp( argv[i], "rsa" ) == 0 )
310 todo.rsa = 1;
311 else if( strcmp( argv[i], "dhm" ) == 0 )
312 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200313 else if( strcmp( argv[i], "ecdsa" ) == 0 )
314 todo.ecdsa = 1;
315 else if( strcmp( argv[i], "ecdh" ) == 0 )
316 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200317 else
318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
320 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200321 }
322 }
323 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200328 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000329#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200330 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200331 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200334 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100335 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336#endif
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200339 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100340 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341#endif
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200344 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100345 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100346#endif
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200349 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100350 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351#endif
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200354 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100355 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356#endif
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200359 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100360 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000361#endif
362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200364 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_arc4_context arc4;
367 mbedtls_arc4_init( &arc4 );
368 mbedtls_arc4_setup( &arc4, tmp, 32 );
369 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
370 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200371 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000372#endif
373
Simon Butcher549dc3d2016-10-05 14:14:19 +0100374#if defined(MBEDTLS_DES_C)
375#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200376 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_des3_context des3;
379 mbedtls_des3_init( &des3 );
380 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200381 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
383 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200384 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200386 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_des_context des;
389 mbedtls_des_init( &des );
390 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200391 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
393 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200394 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100395
396#endif /* MBEDTLS_CIPHER_MODE_CBC */
397#if defined(MBEDTLS_CMAC_C)
398 if( todo.des3_cmac )
399 {
400 unsigned char output[8];
401 const mbedtls_cipher_info_t *cipher_info;
402
403 memset( buf, 0, sizeof( buf ) );
404 memset( tmp, 0, sizeof( tmp ) );
405
406 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
407
408 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100409 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
410 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100411 }
412#endif /* MBEDTLS_CMAC_C */
413#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_AES_C)
416#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200417 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100419 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_aes_context aes;
421 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200422 for( keysize = 128; keysize <= 256; keysize += 64 )
423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200426 memset( buf, 0, sizeof( buf ) );
427 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200430 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200435#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200437 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000438 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100439 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200441
442 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200443 for( keysize = 128; keysize <= 256; keysize += 64 )
444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000446
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200447 memset( buf, 0, sizeof( buf ) );
448 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200449 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000450
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200451 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200456 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000457 }
458#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200460 if( todo.aes_ccm )
461 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100462 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200464
465 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +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-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200469
470 memset( buf, 0, sizeof( buf ) );
471 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200472 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200473
474 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200476 12, NULL, 0, buf, buf, tmp, 16 ) );
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200479 }
480 }
481#endif
Manuel Pégourié-Gonnardc975b2c2018-05-09 10:21:28 +0200482#if defined(MBEDTLS_CHACHAPOLY_C)
483 if( todo.chachapoly )
484 {
485 mbedtls_chachapoly_context chachapoly;
486
487 mbedtls_chachapoly_init( &chachapoly );
488 memset( buf, 0, sizeof( buf ) );
489 memset( tmp, 0, sizeof( tmp ) );
490
491 mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
492
493 mbedtls_chachapoly_setkey( &chachapoly, tmp );
494
495 TIME_AND_TSC( title,
496 mbedtls_chachapoly_crypt_and_tag( &chachapoly,
497 MBEDTLS_CHACHAPOLY_ENCRYPT, BUFSIZE, tmp,
498 NULL, 0, buf, buf, tmp ) );
499
500 mbedtls_chachapoly_free( &chachapoly );
501 }
502#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100503#if defined(MBEDTLS_CMAC_C)
504 if( todo.aes_cmac )
505 {
506 unsigned char output[16];
507 const mbedtls_cipher_info_t *cipher_info;
508 mbedtls_cipher_type_t cipher_type;
509 int keysize;
510
511 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
512 keysize <= 256;
513 keysize += 64, cipher_type++ )
514 {
515 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
516
517 memset( buf, 0, sizeof( buf ) );
518 memset( tmp, 0, sizeof( tmp ) );
519
520 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
521
522 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100523 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
524 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100525 }
526
527 memset( buf, 0, sizeof( buf ) );
528 memset( tmp, 0, sizeof( tmp ) );
529 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100530 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
531 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100532 }
533#endif /* MBEDTLS_CMAC_C */
Jaeden Ameroc00cac72018-05-02 17:38:00 +0100534#if defined(MBEDTLS_CIPHER_MODE_XTS)
535 if( todo.aes_xts )
536 {
537 int keysize;
538 mbedtls_aes_xts_context ctx;
539
540 mbedtls_aes_xts_init( &ctx );
541 for( keysize = 128; keysize <= 256; keysize += 64 )
542 {
543 mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
544
545 memset( buf, 0, sizeof( buf ) );
546 memset( tmp, 0, sizeof( tmp ) );
547 mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2);
548
549 TIME_AND_TSC( title,
550 mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
551 tmp, buf, buf ) );
552
553 mbedtls_aes_xts_free( &ctx );
554 }
555 }
556#endif /* MBEDTLS_CIPHER_MODE_XTS */
Simon Butcher549dc3d2016-10-05 14:14:19 +0100557#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200560 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000561 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100562 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_camellia_context camellia;
564 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200565 for( keysize = 128; keysize <= 256; keysize += 64 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000568
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200569 memset( buf, 0, sizeof( buf ) );
570 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000572
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200573 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200575 BUFSIZE, tmp, buf, buf ) );
576 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000578 }
579#endif
580
Daniel Kinga98ff5e2016-05-15 17:28:08 -0300581#if defined(MBEDTLS_CHACHA20_C)
582 if ( todo.chacha20 )
583 {
584 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
585 }
586#endif
587
Daniel King5d77eaa2016-05-16 18:25:45 -0300588#if defined(MBEDTLS_POLY1305_C)
589 if ( todo.poly1305 )
590 {
Manuel Pégourié-Gonnard9b7a93c2018-05-09 09:25:00 +0200591 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel King5d77eaa2016-05-16 18:25:45 -0300592 }
593#endif
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200596 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000597 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100598 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_blowfish_context blowfish;
600 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200601
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200602 for( keysize = 128; keysize <= 256; keysize += 64 )
603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000605
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200606 memset( buf, 0, sizeof( buf ) );
607 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000609
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200610 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200612 tmp, buf, buf ) );
613 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000616 }
617#endif
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200620 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_havege_state hs;
623 mbedtls_havege_init( &hs );
624 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
625 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200626 }
Paul Bakker02faf452011-11-29 11:23:58 +0000627#endif
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200630 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000633
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200634 mbedtls_ctr_drbg_init( &ctr_drbg );
635
636 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200638 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
640 mbedtls_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000641
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200642 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_exit(1);
644 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200645 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
647 mbedtls_exit(1) );
648 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200649 }
Paul Bakker02faf452011-11-29 11:23:58 +0000650#endif
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100653 if( todo.hmac_drbg )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_hmac_drbg_context hmac_drbg;
656 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100657
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200658 mbedtls_hmac_drbg_init( &hmac_drbg );
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_SHA1_C)
661 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
662 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100663
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200664 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100666 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
668 mbedtls_exit(1) );
669 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100670
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200671 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_exit(1);
673 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
674 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100675 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
677 mbedtls_exit(1) );
678 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100679#endif
680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA256_C)
682 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
683 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100684
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200685 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100687 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
689 mbedtls_exit(1) );
690 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100691
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200692 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_exit(1);
694 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
695 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100696 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
698 mbedtls_exit(1) );
699 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100700#endif
701 }
702#endif
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200705 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100707 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200709 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
714 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200715
716 TIME_PUBLIC( title, " public",
717 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200719
720 TIME_PUBLIC( title, "private",
721 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200725 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000727#endif
728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200730 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100731 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200732 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800733 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100734 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800735 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100736 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800737 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100738 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800739 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100740 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
741
742 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
743 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
744 sizeof( dhm_P_3072 ) };
745
746 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
747 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
748 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200751 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200752 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200755
Hanno Beckerb9539212017-10-04 13:13:34 +0100756 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
757 dhm_P_size[i] ) != 0 ||
758 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
759 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200762 }
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 dhm.len = mbedtls_mpi_size( &dhm.P );
765 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
766 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
767 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200770 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200772 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100773 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200776 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100777 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200780 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100781 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100782#endif
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200785 if( todo.ecdsa )
786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_ecdsa_context ecdsa;
788 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200789 size_t sig_len;
790
791 memset( buf, 0x2A, sizeof( buf ) );
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 for( curve_info = mbedtls_ecp_curve_list();
794 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200795 curve_info++ )
796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
800 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100801 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200804 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200805 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200806 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200807 tmp, &sig_len, myrand, NULL ) );
808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100810 }
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é-Gonnard50da0482014-12-19 12:10:37 +0100814 curve_info++ )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200819 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100820 tmp, &sig_len, myrand, NULL ) != 0 )
821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100823 }
824 ecp_clear_precomputed( &ecdsa.grp );
825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100827 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200828 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200829 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200830 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200833 }
834 }
835#endif
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200838 if( todo.ecdh )
839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000842 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
843#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
844 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000845#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000846#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
847 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
848#endif
849 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
850 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200852 size_t olen;
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 for( curve_info = mbedtls_ecp_curve_list();
855 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200856 curve_info++ )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200859
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200860 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200862 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200866 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100867 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200870 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200871 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200873 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200875 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100877 }
878
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000879 /* Montgomery curves need to be handled separately */
880 for ( curve_info = montgomery_curve_list;
881 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
882 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000883 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000884 mbedtls_ecdh_init( &ecdh );
885 mbedtls_mpi_init( &z );
886
887 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
888 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
889 {
890 mbedtls_exit( 1 );
891 }
892
893 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
894 curve_info->name );
895 TIME_PUBLIC( title, "handshake",
896 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
897 myrand, NULL );
898 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
899 myrand, NULL ) );
900
901 mbedtls_ecdh_free( &ecdh );
902 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000903 }
904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 for( curve_info = mbedtls_ecp_curve_list();
906 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100907 curve_info++ )
908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100910
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200911 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100913 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
915 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100916 myrand, NULL ) != 0 )
917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100919 }
920 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200923 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200924 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200926 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200928 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000929
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000930 /* Montgomery curves need to be handled separately */
931 for ( curve_info = montgomery_curve_list;
932 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
933 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000934 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000935 mbedtls_ecdh_init( &ecdh );
936 mbedtls_mpi_init( &z );
937
938 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
939 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
940 myrand, NULL ) != 0 ||
941 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
942 {
943 mbedtls_exit( 1 );
944 }
945
946 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
947 curve_info->name );
948 TIME_PUBLIC( title, "handshake",
949 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
950 myrand, NULL ) );
951
952 mbedtls_ecdh_free( &ecdh );
953 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000954 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200955 }
956#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
961 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000962#endif
963
Paul Bakkercce9d772011-11-18 14:26:47 +0000964#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000966 fflush( stdout ); getchar();
967#endif
968
969 return( 0 );
970}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_TIMING_C */