blob: 17f9d0e2750d6cde46be70567fde536cd3d8aa04 [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 King34b822c2016-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"
Simon Butcher549dc3d2016-10-05 14:14:19 +010065#include "mbedtls/cmac.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030066#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/havege.h"
68#include "mbedtls/ctr_drbg.h"
69#include "mbedtls/hmac_drbg.h"
70#include "mbedtls/rsa.h"
71#include "mbedtls/dhm.h"
72#include "mbedtls/ecdsa.h"
73#include "mbedtls/ecdh.h"
74#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000078#endif
79
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000080/*
81 * For heap usage estimates, we need an estimate of the overhead per allocated
82 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
83 * so use that as our baseline.
84 */
85#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
86
87/*
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +020088 * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000089 */
90#define HEAP_SIZE (1u << 16) // 64k
91
Paul Bakker02faf452011-11-29 11:23:58 +000092#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010093#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010094#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000095
Rich Evans85b05ec2015-02-12 11:37:29 +000096#define OPTIONS \
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +020097 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Daniel King34b822c2016-05-15 17:28:08 -030098 "arc4, des3, des, camellia, blowfish, chacha20,\n" \
Daniel Kingadc32c02016-05-16 18:25:45 -030099 "aes_cbc, aes_gcm, aes_ccm,\n" \
100 "aes_cmac, des3_cmac, poly1305\n" \
Rich Evans85b05ec2015-02-12 11:37:29 +0000101 "havege, ctr_drbg, hmac_drbg\n" \
102 "rsa, dhm, ecdsa, ecdh.\n"
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_ERROR_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000105#define PRINT_ERROR \
Simon Butcherb981b162016-10-06 10:27:22 +0100106 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_printf( "FAILED: %s\n", tmp );
Rich Evans85b05ec2015-02-12 11:37:29 +0000108#else
109#define PRINT_ERROR \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_printf( "FAILED: -0x%04x\n", -ret );
Rich Evans85b05ec2015-02-12 11:37:29 +0000111#endif
112
113#define TIME_AND_TSC( TITLE, CODE ) \
114do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200115 unsigned long ii, jj, tsc; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000116 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200117 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000118 fflush( stdout ); \
119 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200120 mbedtls_set_alarm( 1 ); \
121 for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000122 { \
123 CODE; \
124 } \
125 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200126 tsc = mbedtls_timing_hardclock(); \
127 for( jj = 0; jj < 1024; jj++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000128 { \
129 CODE; \
130 } \
131 \
Ron Eldor0728d692017-11-29 12:08:35 +0200132 mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\n", \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200133 ii * BUFSIZE / 1024, \
134 ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135} while( 0 )
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100138
139#define MEMORY_MEASURE_INIT \
140 size_t max_used, max_blocks, max_bytes; \
141 size_t prv_used, prv_blocks; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200142 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_memory_buffer_alloc_max_reset( );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100144
145#define MEMORY_MEASURE_PRINT( title_len ) \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200146 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
147 for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100148 max_used -= prv_used; \
149 max_blocks -= prv_blocks; \
150 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100152
153#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000154#define MEMORY_MEASURE_INIT
155#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100156#endif
157
Rich Evans85b05ec2015-02-12 11:37:29 +0000158#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
159do { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200160 unsigned long ii; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000161 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100162 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000163 \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200164 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000165 fflush( stdout ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200166 mbedtls_set_alarm( 3 ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000167 \
168 ret = 0; \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200169 for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \
Rich Evans85b05ec2015-02-12 11:37:29 +0000170 { \
171 CODE; \
172 } \
173 \
174 if( ret != 0 ) \
175 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100176 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000177 } \
178 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100179 { \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200180 mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100181 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200182 mbedtls_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100183 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000184} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakkera3d195c2011-11-27 21:07:34 +0000186static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000187{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000188 size_t use_len;
189 int rnd;
190
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 if( rng_state != NULL )
192 rng_state = NULL;
193
Paul Bakkera3d195c2011-11-27 21:07:34 +0000194 while( len > 0 )
195 {
196 use_len = len;
197 if( use_len > sizeof(int) )
198 use_len = sizeof(int);
199
200 rnd = rand();
201 memcpy( output, &rnd, use_len );
202 output += use_len;
203 len -= use_len;
204 }
205
206 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207}
208
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100209/*
210 * Clear some memory that was used to prepare the context
211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_ECP_C)
213void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100214{
215 if( grp->T != NULL )
216 {
217 size_t i;
218 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_ecp_point_free( &grp->T[i] );
220 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100221 }
222 grp->T = NULL;
223 grp->T_size = 0;
224}
225#else
226#define ecp_clear_precomputed( g )
227#endif
228
Paul Bakker5121ce52009-01-03 21:22:43 +0000229unsigned char buf[BUFSIZE];
230
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200231typedef struct {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200232 char md4, md5, ripemd160, sha1, sha256, sha512,
Simon Butcher549dc3d2016-10-05 14:14:19 +0100233 arc4, des3, des,
234 aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,
Daniel King34b822c2016-05-15 17:28:08 -0300235 camellia, blowfish, chacha20,
Daniel Kingadc32c02016-05-16 18:25:45 -0300236 poly1305,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100237 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200238 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200239} todo_list;
240
Paul Bakkercce9d772011-11-18 14:26:47 +0000241int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000242{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100243 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200244 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200245 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200246 todo_list todo;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200248 unsigned char alloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000249#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000250
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000251 if( argc <= 1 )
252 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200253 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000254 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255 else
256 {
257 memset( &todo, 0, sizeof( todo ) );
258
259 for( i = 1; i < argc; i++ )
260 {
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200261 if( strcmp( argv[i], "md4" ) == 0 )
262 todo.md4 = 1;
263 else if( strcmp( argv[i], "md5" ) == 0 )
264 todo.md5 = 1;
265 else if( strcmp( argv[i], "ripemd160" ) == 0 )
266 todo.ripemd160 = 1;
267 else if( strcmp( argv[i], "sha1" ) == 0 )
268 todo.sha1 = 1;
269 else if( strcmp( argv[i], "sha256" ) == 0 )
270 todo.sha256 = 1;
271 else if( strcmp( argv[i], "sha512" ) == 0 )
272 todo.sha512 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200273 else if( strcmp( argv[i], "arc4" ) == 0 )
274 todo.arc4 = 1;
275 else if( strcmp( argv[i], "des3" ) == 0 )
276 todo.des3 = 1;
277 else if( strcmp( argv[i], "des" ) == 0 )
278 todo.des = 1;
279 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
280 todo.aes_cbc = 1;
281 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
282 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200283 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
284 todo.aes_ccm = 1;
Simon Butcher549dc3d2016-10-05 14:14:19 +0100285 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
286 todo.aes_cmac = 1;
287 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
288 todo.des3_cmac = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200289 else if( strcmp( argv[i], "camellia" ) == 0 )
290 todo.camellia = 1;
291 else if( strcmp( argv[i], "blowfish" ) == 0 )
292 todo.blowfish = 1;
Daniel King34b822c2016-05-15 17:28:08 -0300293 else if( strcmp( argv[i], "chacha20" ) == 0 )
294 todo.chacha20 = 1;
Daniel Kingadc32c02016-05-16 18:25:45 -0300295 else if( strcmp( argv[i], "poly1305" ) == 0 )
296 todo.poly1305 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297 else if( strcmp( argv[i], "havege" ) == 0 )
298 todo.havege = 1;
299 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
300 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100301 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
302 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200303 else if( strcmp( argv[i], "rsa" ) == 0 )
304 todo.rsa = 1;
305 else if( strcmp( argv[i], "dhm" ) == 0 )
306 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200307 else if( strcmp( argv[i], "ecdsa" ) == 0 )
308 todo.ecdsa = 1;
309 else if( strcmp( argv[i], "ecdh" ) == 0 )
310 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200311 else
312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
314 mbedtls_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200315 }
316 }
317 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +0200322 mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000323#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200324 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200325 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_MD4_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200328 if( todo.md4 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100329 TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330#endif
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_MD5_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200333 if( todo.md5 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100334 TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335#endif
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#if defined(MBEDTLS_RIPEMD160_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200338 if( todo.ripemd160 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100339 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100340#endif
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SHA1_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200343 if( todo.sha1 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100344 TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345#endif
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200348 if( todo.sha256 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100349 TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350#endif
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarde85fef12015-05-11 19:21:39 +0200353 if( todo.sha512 )
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100354 TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000355#endif
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200358 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_arc4_context arc4;
361 mbedtls_arc4_init( &arc4 );
362 mbedtls_arc4_setup( &arc4, tmp, 32 );
363 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
364 mbedtls_arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200365 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000366#endif
367
Simon Butcher549dc3d2016-10-05 14:14:19 +0100368#if defined(MBEDTLS_DES_C)
369#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200370 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_des3_context des3;
373 mbedtls_des3_init( &des3 );
374 mbedtls_des3_set3key_enc( &des3, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200375 TIME_AND_TSC( "3DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
377 mbedtls_des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200378 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200380 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_des_context des;
383 mbedtls_des_init( &des );
384 mbedtls_des_setkey_enc( &des, tmp );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200385 TIME_AND_TSC( "DES",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
387 mbedtls_des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200388 }
Simon Butcher549dc3d2016-10-05 14:14:19 +0100389
390#endif /* MBEDTLS_CIPHER_MODE_CBC */
391#if defined(MBEDTLS_CMAC_C)
392 if( todo.des3_cmac )
393 {
394 unsigned char output[8];
395 const mbedtls_cipher_info_t *cipher_info;
396
397 memset( buf, 0, sizeof( buf ) );
398 memset( tmp, 0, sizeof( tmp ) );
399
400 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
401
402 TIME_AND_TSC( "3DES-CMAC",
Simon Butcherb981b162016-10-06 10:27:22 +0100403 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
404 BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100405 }
406#endif /* MBEDTLS_CMAC_C */
407#endif /* MBEDTLS_DES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#if defined(MBEDTLS_AES_C)
410#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200411 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100413 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_aes_context aes;
415 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200416 for( keysize = 128; keysize <= 256; keysize += 64 )
417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 memset( buf, 0, sizeof( buf ) );
421 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200424 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200431 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000432 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100433 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_gcm_context gcm;
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200435
436 mbedtls_gcm_init( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200437 for( keysize = 128; keysize <= 256; keysize += 64 )
438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000440
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200441 memset( buf, 0, sizeof( buf ) );
442 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200443 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000444
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200445 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200447 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200450 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000451 }
452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200454 if( todo.aes_ccm )
455 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100456 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_ccm_context ccm;
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200458
459 mbedtls_ccm_init( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200460 for( keysize = 128; keysize <= 256; keysize += 64 )
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200463
464 memset( buf, 0, sizeof( buf ) );
465 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200466 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200467
468 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200470 12, NULL, 0, buf, buf, tmp, 16 ) );
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_ccm_free( &ccm );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200473 }
474 }
475#endif
Simon Butcher549dc3d2016-10-05 14:14:19 +0100476#if defined(MBEDTLS_CMAC_C)
477 if( todo.aes_cmac )
478 {
479 unsigned char output[16];
480 const mbedtls_cipher_info_t *cipher_info;
481 mbedtls_cipher_type_t cipher_type;
482 int keysize;
483
484 for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
485 keysize <= 256;
486 keysize += 64, cipher_type++ )
487 {
488 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
489
490 memset( buf, 0, sizeof( buf ) );
491 memset( tmp, 0, sizeof( tmp ) );
492
493 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
494
495 TIME_AND_TSC( title,
Andres AGa592dcc2016-10-06 15:23:39 +0100496 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
497 buf, BUFSIZE, output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100498 }
499
500 memset( buf, 0, sizeof( buf ) );
501 memset( tmp, 0, sizeof( tmp ) );
502 TIME_AND_TSC( "AES-CMAC-PRF-128",
Simon Butcherb981b162016-10-06 10:27:22 +0100503 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
504 output ) );
Simon Butcher549dc3d2016-10-05 14:14:19 +0100505 }
506#endif /* MBEDTLS_CMAC_C */
507#endif /* MBEDTLS_AES_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200510 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000511 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100512 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_camellia_context camellia;
514 mbedtls_camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200515 for( keysize = 128; keysize <= 256; keysize += 64 )
516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000518
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200519 memset( buf, 0, sizeof( buf ) );
520 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000522
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200525 BUFSIZE, tmp, buf, buf ) );
526 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000528 }
529#endif
530
Daniel King34b822c2016-05-15 17:28:08 -0300531#if defined(MBEDTLS_CHACHA20_C)
532 if ( todo.chacha20 )
533 {
534 TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
535 }
536#endif
537
Daniel Kingadc32c02016-05-16 18:25:45 -0300538#if defined(MBEDTLS_POLY1305_C)
539 if ( todo.poly1305 )
540 {
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200541 TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
Daniel Kingadc32c02016-05-16 18:25:45 -0300542 }
543#endif
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200546 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000547 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100548 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_blowfish_context blowfish;
550 mbedtls_blowfish_init( &blowfish );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200551
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200552 for( keysize = 128; keysize <= 256; keysize += 64 )
553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000555
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200556 memset( buf, 0, sizeof( buf ) );
557 memset( tmp, 0, sizeof( tmp ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000559
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200560 TIME_AND_TSC( title,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200562 tmp, buf, buf ) );
563 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000566 }
567#endif
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200570 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_havege_state hs;
573 mbedtls_havege_init( &hs );
574 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
575 mbedtls_havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200576 }
Paul Bakker02faf452011-11-29 11:23:58 +0000577#endif
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200580 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000583
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200584 mbedtls_ctr_drbg_init( &ctr_drbg );
585
586 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200588 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
590 mbedtls_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000591
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200592 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_exit(1);
594 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200595 TIME_AND_TSC( "CTR_DRBG (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
597 mbedtls_exit(1) );
598 mbedtls_ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200599 }
Paul Bakker02faf452011-11-29 11:23:58 +0000600#endif
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100603 if( todo.hmac_drbg )
604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_hmac_drbg_context hmac_drbg;
606 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100607
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200608 mbedtls_hmac_drbg_init( &hmac_drbg );
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_SHA1_C)
611 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
612 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100613
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200614 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100616 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
618 mbedtls_exit(1) );
619 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100620
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200621 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_exit(1);
623 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
624 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100625 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
627 mbedtls_exit(1) );
628 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100629#endif
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_SHA256_C)
632 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
633 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100634
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200635 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100637 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
639 mbedtls_exit(1) );
640 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100641
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200642 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_exit(1);
644 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
645 MBEDTLS_HMAC_DRBG_PR_ON );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100646 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
648 mbedtls_exit(1) );
649 mbedtls_hmac_drbg_free( &hmac_drbg );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100650#endif
651 }
652#endif
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200655 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100657 int keysize;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 mbedtls_rsa_context rsa;
Manuel Pégourié-Gonnarda6dbddc2015-07-06 11:20:33 +0200659 for( keysize = 2048; keysize <= 4096; keysize *= 2 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
664 mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200665
666 TIME_PUBLIC( title, " public",
667 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 ret = mbedtls_rsa_public( &rsa, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200669
670 TIME_PUBLIC( title, "private",
671 buf[0] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_rsa_free( &rsa );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200675 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000677#endif
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200680 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100681 {
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200682 int dhm_sizes[] = { 2048, 3072 };
Brendan Shankse61514d2018-03-08 17:40:56 -0800683 static const unsigned char dhm_P_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100684 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800685 static const unsigned char dhm_P_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100686 MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800687 static const unsigned char dhm_G_2048[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100688 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Brendan Shankse61514d2018-03-08 17:40:56 -0800689 static const unsigned char dhm_G_3072[] =
Hanno Beckerb9539212017-10-04 13:13:34 +0100690 MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
691
692 const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
693 const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
694 sizeof( dhm_P_3072 ) };
695
696 const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
697 const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
698 sizeof( dhm_G_3072 ) };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_dhm_context dhm;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200701 size_t olen;
Manuel Pégourié-Gonnard4f3368e2015-07-19 15:01:28 +0200702 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200705
Hanno Beckerb9539212017-10-04 13:13:34 +0100706 if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
707 dhm_P_size[i] ) != 0 ||
708 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
709 dhm_G_size[i] ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200712 }
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 dhm.len = mbedtls_mpi_size( &dhm.P );
715 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
716 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
717 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200720 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200722 myrand, NULL );
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100723 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200726 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100727 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_dhm_free( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200730 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100731 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100732#endif
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200735 if( todo.ecdsa )
736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_ecdsa_context ecdsa;
738 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200739 size_t sig_len;
740
741 memset( buf, 0x2A, sizeof( buf ) );
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 for( curve_info = mbedtls_ecp_curve_list();
744 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200745 curve_info++ )
746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
750 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100751 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200754 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200755 TIME_PUBLIC( title, "sign",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200756 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200757 tmp, &sig_len, myrand, NULL ) );
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100760 }
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 for( curve_info = mbedtls_ecp_curve_list();
763 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100764 curve_info++ )
765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_ecdsa_init( &ecdsa );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200769 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100770 tmp, &sig_len, myrand, NULL ) != 0 )
771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100773 }
774 ecp_clear_precomputed( &ecdsa.grp );
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100777 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200778 TIME_PUBLIC( title, "verify",
Manuel Pégourié-Gonnard797f48a2015-06-18 15:45:05 +0200779 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200780 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 mbedtls_ecdsa_free( &ecdsa );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200783 }
784 }
785#endif
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_ECDH_C)
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200788 if( todo.ecdh )
789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_ecdh_context ecdh;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_mpi z;
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000792 const mbedtls_ecp_curve_info montgomery_curve_list[] = {
793#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
794 { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000795#endif
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000796#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
797 { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
798#endif
799 { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
800 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200802 size_t olen;
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 for( curve_info = mbedtls_ecp_curve_list();
805 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200806 curve_info++ )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200809
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200810 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200812 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200816 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100817 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200820 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200821 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200823 myrand, NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200825 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100827 }
828
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000829 /* Montgomery curves need to be handled separately */
830 for ( curve_info = montgomery_curve_list;
831 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
832 curve_info++ )
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000833 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000834 mbedtls_ecdh_init( &ecdh );
835 mbedtls_mpi_init( &z );
836
837 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
838 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
839 {
840 mbedtls_exit( 1 );
841 }
842
843 mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
844 curve_info->name );
845 TIME_PUBLIC( title, "handshake",
846 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
847 myrand, NULL );
848 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
849 myrand, NULL ) );
850
851 mbedtls_ecdh_free( &ecdh );
852 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000853 }
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 for( curve_info = mbedtls_ecp_curve_list();
856 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100857 curve_info++ )
858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_ecdh_init( &ecdh );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100860
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200861 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100863 myrand, NULL ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
865 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100866 myrand, NULL ) != 0 )
867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100869 }
870 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200873 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200874 TIME_PUBLIC( title, "handshake",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200876 myrand, NULL ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_ecdh_free( &ecdh );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200878 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000879
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000880 /* Montgomery curves need to be handled separately */
881 for ( curve_info = montgomery_curve_list;
882 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
883 curve_info++)
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000884 {
Nicholas Wilson08f3ef12015-11-10 13:10:01 +0000885 mbedtls_ecdh_init( &ecdh );
886 mbedtls_mpi_init( &z );
887
888 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
889 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
890 myrand, NULL ) != 0 ||
891 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
892 {
893 mbedtls_exit( 1 );
894 }
895
896 mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
897 curve_info->name );
898 TIME_PUBLIC( title, "handshake",
899 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
900 myrand, NULL ) );
901
902 mbedtls_ecdh_free( &ecdh );
903 mbedtls_mpi_free( &z );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000904 }
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200905 }
906#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
911 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000912#endif
913
Paul Bakkercce9d772011-11-18 14:26:47 +0000914#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 fflush( stdout ); getchar();
917#endif
918
919 return( 0 );
920}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922#endif /* MBEDTLS_TIMING_C */