1 /*
2  *  Benchmark demonstration program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  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.
18  */
19 
20 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
21 
22 #include "mbedtls/build_info.h"
23 
24 #include "mbedtls/platform.h"
25 
26 #if !defined(MBEDTLS_HAVE_TIME)
main(void)27 int main( void )
28 {
29     mbedtls_printf("MBEDTLS_HAVE_TIME not defined.\n");
30     mbedtls_exit( 0 );
31 }
32 #else
33 
34 #include <string.h>
35 #include <stdlib.h>
36 
37 #include "mbedtls/md5.h"
38 #include "mbedtls/ripemd160.h"
39 #include "mbedtls/sha1.h"
40 #include "mbedtls/sha256.h"
41 #include "mbedtls/sha512.h"
42 
43 #include "mbedtls/des.h"
44 #include "mbedtls/aes.h"
45 #include "mbedtls/aria.h"
46 #include "mbedtls/camellia.h"
47 #include "mbedtls/chacha20.h"
48 #include "mbedtls/gcm.h"
49 #include "mbedtls/ccm.h"
50 #include "mbedtls/chachapoly.h"
51 #include "mbedtls/cmac.h"
52 #include "mbedtls/poly1305.h"
53 
54 #include "mbedtls/ctr_drbg.h"
55 #include "mbedtls/hmac_drbg.h"
56 
57 #include "mbedtls/rsa.h"
58 #include "mbedtls/dhm.h"
59 #include "mbedtls/ecdsa.h"
60 #include "mbedtls/ecdh.h"
61 
62 #include "mbedtls/error.h"
63 
64 #ifndef asm
65 #define asm __asm
66 #endif
67 
68 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
69 
70 #include <windows.h>
71 #include <process.h>
72 
73 struct _hr_time
74 {
75     LARGE_INTEGER start;
76 };
77 
78 #else
79 
80 #include <unistd.h>
81 #include <sys/types.h>
82 #include <sys/time.h>
83 #include <signal.h>
84 #include <time.h>
85 
86 struct _hr_time
87 {
88     struct timeval start;
89 };
90 
91 #endif /* _WIN32 && !EFIX64 && !EFI32 */
92 
93 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
94 #include "mbedtls/memory_buffer_alloc.h"
95 #endif
96 
97 static void mbedtls_set_alarm( int seconds );
98 
99 /*
100  * For heap usage estimates, we need an estimate of the overhead per allocated
101  * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
102  * so use that as our baseline.
103  */
104 #define MEM_BLOCK_OVERHEAD  ( 2 * sizeof( size_t ) )
105 
106 /*
107  * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
108  */
109 #define HEAP_SIZE       (1u << 16)  /* 64k */
110 
111 #define BUFSIZE         1024
112 #define HEADER_FORMAT   "  %-24s :  "
113 #define TITLE_LEN       25
114 
115 #define OPTIONS                                                         \
116     "md5, ripemd160, sha1, sha256, sha512,\n"                      \
117     "des3, des, camellia, chacha20,\n"                  \
118     "aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,\n"                 \
119     "aes_cmac, des3_cmac, poly1305\n"                                   \
120     "ctr_drbg, hmac_drbg\n"                                     \
121     "rsa, dhm, ecdsa, ecdh.\n"
122 
123 #if defined(MBEDTLS_ERROR_C)
124 #define PRINT_ERROR                                                     \
125         mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) );          \
126         mbedtls_printf( "FAILED: %s\n", tmp );
127 #else
128 #define PRINT_ERROR                                                     \
129         mbedtls_printf( "FAILED: -0x%04x\n", (unsigned int) -ret );
130 #endif
131 
132 #define TIME_AND_TSC( TITLE, CODE )                                     \
133 do {                                                                    \
134     unsigned long ii, jj, tsc;                                          \
135     int ret = 0;                                                        \
136                                                                         \
137     mbedtls_printf( HEADER_FORMAT, TITLE );                             \
138     fflush( stdout );                                                   \
139                                                                         \
140     mbedtls_set_alarm( 1 );                                             \
141     for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ )           \
142     {                                                                   \
143         ret = CODE;                                                     \
144     }                                                                   \
145                                                                         \
146     tsc = mbedtls_timing_hardclock();                                   \
147     for( jj = 0; ret == 0 && jj < 1024; jj++ )                          \
148     {                                                                   \
149         ret = CODE;                                                     \
150     }                                                                   \
151                                                                         \
152     if( ret != 0 )                                                      \
153     {                                                                   \
154         PRINT_ERROR;                                                    \
155     }                                                                   \
156     else                                                                \
157     {                                                                   \
158         mbedtls_printf( "%9lu KiB/s,  %9lu cycles/byte\n",              \
159                          ii * BUFSIZE / 1024,                           \
160                          ( mbedtls_timing_hardclock() - tsc )           \
161                          / ( jj * BUFSIZE ) );                          \
162     }                                                                   \
163 } while( 0 )
164 
165 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
166 
167 /* How much space to reserve for the title when printing heap usage results.
168  * Updated manually as the output of the following command:
169  *
170  *  sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
171  *      awk '{print length+3}' | sort -rn | head -n1
172  *
173  * This computes the maximum length of a title +3, because we appends "/s" and
174  * want at least one space. (If the value is too small, the only consequence
175  * is poor alignment.) */
176 #define TITLE_SPACE 17
177 
178 #define MEMORY_MEASURE_INIT                                             \
179     size_t max_used, max_blocks, max_bytes;                             \
180     size_t prv_used, prv_blocks;                                        \
181     size_t alloc_cnt, free_cnt, prv_alloc, prv_free;                    \
182     mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks );      \
183     mbedtls_memory_buffer_alloc_max_reset( );
184 
185 #define MEMORY_MEASURE_RESET                                            \
186     mbedtls_memory_buffer_alloc_count_get( &prv_alloc, &prv_free );
187 
188 #define MEMORY_MEASURE_PRINT( title_len )                               \
189     mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks );      \
190     mbedtls_memory_buffer_alloc_count_get( &alloc_cnt, &free_cnt );     \
191     ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1;     \
192     while( ii-- ) mbedtls_printf( " " );                                \
193     max_used -= prv_used;                                               \
194     max_blocks -= prv_blocks;                                           \
195     max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks;             \
196     mbedtls_printf( "%6u heap bytes, %6u allocs",                       \
197                     (unsigned) max_bytes,                               \
198                     (unsigned)( alloc_cnt - prv_alloc ) );
199 
200 #else
201 #define MEMORY_MEASURE_INIT
202 #define MEMORY_MEASURE_RESET
203 #define MEMORY_MEASURE_PRINT( title_len )
204 #endif
205 
206 #define TIME_PUBLIC( TITLE, TYPE, CODE )                                \
207 do {                                                                    \
208     unsigned long ii;                                                   \
209     int ret;                                                            \
210     MEMORY_MEASURE_INIT;                                                \
211                                                                         \
212     mbedtls_printf( HEADER_FORMAT, TITLE );                             \
213     fflush( stdout );                                                   \
214     mbedtls_set_alarm( 3 );                                             \
215                                                                         \
216     ret = 0;                                                            \
217     for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ )             \
218     {                                                                   \
219         MEMORY_MEASURE_RESET;                                           \
220         CODE;                                                           \
221     }                                                                   \
222                                                                         \
223     if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED )               \
224     {                                                                   \
225         mbedtls_printf( "Feature Not Supported. Skipping.\n" );         \
226         ret = 0;                                                        \
227     }                                                                   \
228     else if( ret != 0 )                                                 \
229     {                                                                   \
230         PRINT_ERROR;                                                    \
231     }                                                                   \
232     else                                                                \
233     {                                                                   \
234         mbedtls_printf( "%6lu " TYPE "/s", ii / 3 );                    \
235         MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 );                     \
236         mbedtls_printf( "\n" );                                         \
237     }                                                                   \
238 } while( 0 )
239 
240 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
241     ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
242 
243 #define HAVE_HARDCLOCK
244 
mbedtls_timing_hardclock(void)245 static unsigned long mbedtls_timing_hardclock( void )
246 {
247     unsigned long tsc;
248     __asm   rdtsc
249     __asm   mov  [tsc], eax
250     return( tsc );
251 }
252 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
253           ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
254 
255 /* some versions of mingw-64 have 32-bit longs even on x84_64 */
256 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
257     defined(__GNUC__) && ( defined(__i386__) || (                       \
258     ( defined(__amd64__) || defined( __x86_64__) ) && __SIZEOF_LONG__ == 4 ) )
259 
260 #define HAVE_HARDCLOCK
261 
mbedtls_timing_hardclock(void)262 static unsigned long mbedtls_timing_hardclock( void )
263 {
264     unsigned long lo, hi;
265     asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
266     return( lo );
267 }
268 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
269           __GNUC__ && __i386__ */
270 
271 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
272     defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
273 
274 #define HAVE_HARDCLOCK
275 
mbedtls_timing_hardclock(void)276 static unsigned long mbedtls_timing_hardclock( void )
277 {
278     unsigned long lo, hi;
279     asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
280     return( lo | ( hi << 32 ) );
281 }
282 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
283           __GNUC__ && ( __amd64__ || __x86_64__ ) */
284 
285 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
286     defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
287 
288 #define HAVE_HARDCLOCK
289 
mbedtls_timing_hardclock(void)290 static unsigned long mbedtls_timing_hardclock( void )
291 {
292     unsigned long tbl, tbu0, tbu1;
293 
294     do
295     {
296         asm volatile( "mftbu %0" : "=r" (tbu0) );
297         asm volatile( "mftb  %0" : "=r" (tbl ) );
298         asm volatile( "mftbu %0" : "=r" (tbu1) );
299     }
300     while( tbu0 != tbu1 );
301 
302     return( tbl );
303 }
304 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
305           __GNUC__ && ( __powerpc__ || __ppc__ ) */
306 
307 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
308     defined(__GNUC__) && defined(__sparc64__)
309 
310 #if defined(__OpenBSD__)
311 #warning OpenBSD does not allow access to tick register using software version instead
312 #else
313 #define HAVE_HARDCLOCK
314 
mbedtls_timing_hardclock(void)315 static unsigned long mbedtls_timing_hardclock( void )
316 {
317     unsigned long tick;
318     asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
319     return( tick );
320 }
321 #endif /* __OpenBSD__ */
322 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
323           __GNUC__ && __sparc64__ */
324 
325 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&  \
326     defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
327 
328 #define HAVE_HARDCLOCK
329 
mbedtls_timing_hardclock(void)330 static unsigned long mbedtls_timing_hardclock( void )
331 {
332     unsigned long tick;
333     asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
334     asm volatile( "mov   %%g1, %0" : "=r" (tick) );
335     return( tick );
336 }
337 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
338           __GNUC__ && __sparc__ && !__sparc64__ */
339 
340 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&      \
341     defined(__GNUC__) && defined(__alpha__)
342 
343 #define HAVE_HARDCLOCK
344 
mbedtls_timing_hardclock(void)345 static unsigned long mbedtls_timing_hardclock( void )
346 {
347     unsigned long cc;
348     asm volatile( "rpcc %0" : "=r" (cc) );
349     return( cc & 0xFFFFFFFF );
350 }
351 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
352           __GNUC__ && __alpha__ */
353 
354 #if !defined(HAVE_HARDCLOCK) && defined(MBEDTLS_HAVE_ASM) &&      \
355     defined(__GNUC__) && defined(__ia64__)
356 
357 #define HAVE_HARDCLOCK
358 
mbedtls_timing_hardclock(void)359 static unsigned long mbedtls_timing_hardclock( void )
360 {
361     unsigned long itc;
362     asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
363     return( itc );
364 }
365 #endif /* !HAVE_HARDCLOCK && MBEDTLS_HAVE_ASM &&
366           __GNUC__ && __ia64__ */
367 
368 #if !defined(HAVE_HARDCLOCK) && defined(_MSC_VER) && \
369     !defined(EFIX64) && !defined(EFI32)
370 
371 #define HAVE_HARDCLOCK
372 
mbedtls_timing_hardclock(void)373 static unsigned long mbedtls_timing_hardclock( void )
374 {
375     LARGE_INTEGER offset;
376 
377     QueryPerformanceCounter( &offset );
378 
379     return( (unsigned long)( offset.QuadPart ) );
380 }
381 #endif /* !HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
382 
383 #if !defined(HAVE_HARDCLOCK)
384 
385 #define HAVE_HARDCLOCK
386 
387 static int hardclock_init = 0;
388 static struct timeval tv_init;
389 
mbedtls_timing_hardclock(void)390 static unsigned long mbedtls_timing_hardclock( void )
391 {
392     struct timeval tv_cur;
393 
394     if( hardclock_init == 0 )
395     {
396         gettimeofday( &tv_init, NULL );
397         hardclock_init = 1;
398     }
399 
400     gettimeofday( &tv_cur, NULL );
401     return( ( tv_cur.tv_sec  - tv_init.tv_sec  ) * 1000000
402           + ( tv_cur.tv_usec - tv_init.tv_usec ) );
403 }
404 #endif /* !HAVE_HARDCLOCK */
405 
406 volatile int mbedtls_timing_alarmed = 0;
407 
408 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
409 
410 /* It's OK to use a global because alarm() is supposed to be global anyway */
411 static DWORD alarmMs;
412 
TimerProc(void * TimerContext)413 static void TimerProc( void *TimerContext )
414 {
415     (void) TimerContext;
416     Sleep( alarmMs );
417     mbedtls_timing_alarmed = 1;
418     /* _endthread will be called implicitly on return
419      * That ensures execution of thread function's epilogue */
420 }
421 
mbedtls_set_alarm(int seconds)422 static void mbedtls_set_alarm( int seconds )
423 {
424     if( seconds == 0 )
425     {
426         /* No need to create a thread for this simple case.
427          * Also, this shorcut is more reliable at least on MinGW32 */
428         mbedtls_timing_alarmed = 1;
429         return;
430     }
431 
432     mbedtls_timing_alarmed = 0;
433     alarmMs = seconds * 1000;
434     (void) _beginthread( TimerProc, 0, NULL );
435 }
436 
437 #else /* _WIN32 && !EFIX64 && !EFI32 */
438 
sighandler(int signum)439 static void sighandler( int signum )
440 {
441     mbedtls_timing_alarmed = 1;
442     signal( signum, sighandler );
443 }
444 
mbedtls_set_alarm(int seconds)445 static void mbedtls_set_alarm( int seconds )
446 {
447     mbedtls_timing_alarmed = 0;
448     signal( SIGALRM, sighandler );
449     alarm( seconds );
450     if( seconds == 0 )
451     {
452         /* alarm(0) cancelled any previous pending alarm, but the
453            handler won't fire, so raise the flag straight away. */
454         mbedtls_timing_alarmed = 1;
455     }
456 }
457 
458 #endif /* _WIN32 && !EFIX64 && !EFI32 */
459 
myrand(void * rng_state,unsigned char * output,size_t len)460 static int myrand( void *rng_state, unsigned char *output, size_t len )
461 {
462     size_t use_len;
463     int rnd;
464 
465     if( rng_state != NULL )
466         rng_state  = NULL;
467 
468     while( len > 0 )
469     {
470         use_len = len;
471         if( use_len > sizeof(int) )
472             use_len = sizeof(int);
473 
474         rnd = rand();
475         memcpy( output, &rnd, use_len );
476         output += use_len;
477         len -= use_len;
478     }
479 
480     return( 0 );
481 }
482 
483 #define CHECK_AND_CONTINUE( R )                                         \
484     {                                                                   \
485         int CHECK_AND_CONTINUE_ret = ( R );                             \
486         if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \
487             mbedtls_printf( "Feature not supported. Skipping.\n" );     \
488             continue;                                                   \
489         }                                                               \
490         else if( CHECK_AND_CONTINUE_ret != 0 ) {                        \
491             mbedtls_exit( 1 );                                          \
492         }                                                               \
493     }
494 
495 #if defined(MBEDTLS_ECP_C)
set_ecp_curve(const char * string,mbedtls_ecp_curve_info * curve)496 static int set_ecp_curve( const char *string, mbedtls_ecp_curve_info *curve )
497 {
498     const mbedtls_ecp_curve_info *found =
499         mbedtls_ecp_curve_info_from_name( string );
500     if( found != NULL )
501     {
502         *curve = *found;
503         return( 1 );
504     }
505     else
506         return( 0 );
507 }
508 #endif
509 
510 unsigned char buf[BUFSIZE];
511 
512 typedef struct {
513     char md5, ripemd160, sha1, sha256, sha512,
514          des3, des,
515          aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
516          aes_cmac, des3_cmac,
517          aria, camellia, chacha20,
518          poly1305,
519          ctr_drbg, hmac_drbg,
520          rsa, dhm, ecdsa, ecdh;
521 } todo_list;
522 
523 
main(int argc,char * argv[])524 int main( int argc, char *argv[] )
525 {
526     int i;
527     unsigned char tmp[200];
528     char title[TITLE_LEN];
529     todo_list todo;
530 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
531     unsigned char alloc_buf[HEAP_SIZE] = { 0 };
532 #endif
533 #if defined(MBEDTLS_ECP_C)
534     mbedtls_ecp_curve_info single_curve[2] = {
535         { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
536         { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
537     };
538     const mbedtls_ecp_curve_info *curve_list = mbedtls_ecp_curve_list( );
539 #endif
540 
541 #if defined(MBEDTLS_ECP_C)
542     (void) curve_list; /* Unused in some configurations where no benchmark uses ECC */
543 #endif
544 
545     if( argc <= 1 )
546     {
547         memset( &todo, 1, sizeof( todo ) );
548     }
549     else
550     {
551         memset( &todo, 0, sizeof( todo ) );
552 
553         for( i = 1; i < argc; i++ )
554         {
555             if( strcmp( argv[i], "md5" ) == 0 )
556                 todo.md5 = 1;
557             else if( strcmp( argv[i], "ripemd160" ) == 0 )
558                 todo.ripemd160 = 1;
559             else if( strcmp( argv[i], "sha1" ) == 0 )
560                 todo.sha1 = 1;
561             else if( strcmp( argv[i], "sha256" ) == 0 )
562                 todo.sha256 = 1;
563             else if( strcmp( argv[i], "sha512" ) == 0 )
564                 todo.sha512 = 1;
565             else if( strcmp( argv[i], "des3" ) == 0 )
566                 todo.des3 = 1;
567             else if( strcmp( argv[i], "des" ) == 0 )
568                 todo.des = 1;
569             else if( strcmp( argv[i], "aes_cbc" ) == 0 )
570                 todo.aes_cbc = 1;
571             else if( strcmp( argv[i], "aes_xts" ) == 0 )
572                 todo.aes_xts = 1;
573             else if( strcmp( argv[i], "aes_gcm" ) == 0 )
574                 todo.aes_gcm = 1;
575             else if( strcmp( argv[i], "aes_ccm" ) == 0 )
576                 todo.aes_ccm = 1;
577             else if( strcmp( argv[i], "chachapoly" ) == 0 )
578                 todo.chachapoly = 1;
579             else if( strcmp( argv[i], "aes_cmac" ) == 0 )
580                 todo.aes_cmac = 1;
581             else if( strcmp( argv[i], "des3_cmac" ) == 0 )
582                 todo.des3_cmac = 1;
583             else if( strcmp( argv[i], "aria" ) == 0 )
584                 todo.aria = 1;
585             else if( strcmp( argv[i], "camellia" ) == 0 )
586                 todo.camellia = 1;
587             else if( strcmp( argv[i], "chacha20" ) == 0 )
588                 todo.chacha20 = 1;
589             else if( strcmp( argv[i], "poly1305" ) == 0 )
590                 todo.poly1305 = 1;
591             else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
592                 todo.ctr_drbg = 1;
593             else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
594                 todo.hmac_drbg = 1;
595             else if( strcmp( argv[i], "rsa" ) == 0 )
596                 todo.rsa = 1;
597             else if( strcmp( argv[i], "dhm" ) == 0 )
598                 todo.dhm = 1;
599             else if( strcmp( argv[i], "ecdsa" ) == 0 )
600                 todo.ecdsa = 1;
601             else if( strcmp( argv[i], "ecdh" ) == 0 )
602                 todo.ecdh = 1;
603 #if defined(MBEDTLS_ECP_C)
604             else if( set_ecp_curve( argv[i], single_curve ) )
605                 curve_list = single_curve;
606 #endif
607             else
608             {
609                 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
610                 mbedtls_printf( "Available options: " OPTIONS );
611             }
612         }
613     }
614 
615     mbedtls_printf( "\n" );
616 
617 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
618     mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
619 #endif
620     memset( buf, 0xAA, sizeof( buf ) );
621     memset( tmp, 0xBB, sizeof( tmp ) );
622 
623     /* Avoid "unused static function" warning in configurations without
624      * symmetric crypto. */
625     (void) mbedtls_timing_hardclock;
626 
627 #if defined(MBEDTLS_MD5_C)
628     if( todo.md5 )
629         TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
630 #endif
631 
632 #if defined(MBEDTLS_RIPEMD160_C)
633     if( todo.ripemd160 )
634         TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
635 #endif
636 
637 #if defined(MBEDTLS_SHA1_C)
638     if( todo.sha1 )
639         TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
640 #endif
641 
642 #if defined(MBEDTLS_SHA256_C)
643     if( todo.sha256 )
644         TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
645 #endif
646 
647 #if defined(MBEDTLS_SHA512_C)
648     if( todo.sha512 )
649         TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
650 #endif
651 
652 #if defined(MBEDTLS_DES_C)
653 #if defined(MBEDTLS_CIPHER_MODE_CBC)
654     if( todo.des3 )
655     {
656         mbedtls_des3_context des3;
657         mbedtls_des3_init( &des3 );
658         if( mbedtls_des3_set3key_enc( &des3, tmp ) != 0 )
659             mbedtls_exit( 1 );
660         TIME_AND_TSC( "3DES",
661                 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
662         mbedtls_des3_free( &des3 );
663     }
664 
665     if( todo.des )
666     {
667         mbedtls_des_context des;
668         mbedtls_des_init( &des );
669         if( mbedtls_des_setkey_enc( &des, tmp ) != 0 )
670             mbedtls_exit( 1 );
671         TIME_AND_TSC( "DES",
672                 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
673         mbedtls_des_free( &des );
674     }
675 
676 #endif /* MBEDTLS_CIPHER_MODE_CBC */
677 #if defined(MBEDTLS_CMAC_C)
678     if( todo.des3_cmac )
679     {
680         unsigned char output[8];
681         const mbedtls_cipher_info_t *cipher_info;
682 
683         memset( buf, 0, sizeof( buf ) );
684         memset( tmp, 0, sizeof( tmp ) );
685 
686         cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
687 
688         TIME_AND_TSC( "3DES-CMAC",
689                       mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
690                       BUFSIZE, output ) );
691     }
692 #endif /* MBEDTLS_CMAC_C */
693 #endif /* MBEDTLS_DES_C */
694 
695 #if defined(MBEDTLS_AES_C)
696 #if defined(MBEDTLS_CIPHER_MODE_CBC)
697     if( todo.aes_cbc )
698     {
699         int keysize;
700         mbedtls_aes_context aes;
701         mbedtls_aes_init( &aes );
702         for( keysize = 128; keysize <= 256; keysize += 64 )
703         {
704             mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
705 
706             memset( buf, 0, sizeof( buf ) );
707             memset( tmp, 0, sizeof( tmp ) );
708             CHECK_AND_CONTINUE( mbedtls_aes_setkey_enc( &aes, tmp, keysize ) );
709 
710             TIME_AND_TSC( title,
711                 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
712         }
713         mbedtls_aes_free( &aes );
714     }
715 #endif
716 #if defined(MBEDTLS_CIPHER_MODE_XTS)
717     if( todo.aes_xts )
718     {
719         int keysize;
720         mbedtls_aes_xts_context ctx;
721 
722         mbedtls_aes_xts_init( &ctx );
723         for( keysize = 128; keysize <= 256; keysize += 128 )
724         {
725             mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
726 
727             memset( buf, 0, sizeof( buf ) );
728             memset( tmp, 0, sizeof( tmp ) );
729             CHECK_AND_CONTINUE( mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 ) );
730 
731             TIME_AND_TSC( title,
732                     mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
733                                            tmp, buf, buf ) );
734 
735             mbedtls_aes_xts_free( &ctx );
736         }
737     }
738 #endif
739 #if defined(MBEDTLS_GCM_C)
740     if( todo.aes_gcm )
741     {
742         int keysize;
743         mbedtls_gcm_context gcm;
744 
745         mbedtls_gcm_init( &gcm );
746         for( keysize = 128; keysize <= 256; keysize += 64 )
747         {
748             mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
749 
750             memset( buf, 0, sizeof( buf ) );
751             memset( tmp, 0, sizeof( tmp ) );
752             mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
753 
754             TIME_AND_TSC( title,
755                     mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
756                         12, NULL, 0, buf, buf, 16, tmp ) );
757 
758             mbedtls_gcm_free( &gcm );
759         }
760     }
761 #endif
762 #if defined(MBEDTLS_CCM_C)
763     if( todo.aes_ccm )
764     {
765         int keysize;
766         mbedtls_ccm_context ccm;
767 
768         mbedtls_ccm_init( &ccm );
769         for( keysize = 128; keysize <= 256; keysize += 64 )
770         {
771             mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
772 
773             memset( buf, 0, sizeof( buf ) );
774             memset( tmp, 0, sizeof( tmp ) );
775             mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
776 
777             TIME_AND_TSC( title,
778                     mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
779                         12, NULL, 0, buf, buf, tmp, 16 ) );
780 
781             mbedtls_ccm_free( &ccm );
782         }
783     }
784 #endif
785 #if defined(MBEDTLS_CHACHAPOLY_C)
786     if( todo.chachapoly )
787     {
788         mbedtls_chachapoly_context chachapoly;
789 
790         mbedtls_chachapoly_init( &chachapoly );
791         memset( buf, 0, sizeof( buf ) );
792         memset( tmp, 0, sizeof( tmp ) );
793 
794         mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
795 
796         mbedtls_chachapoly_setkey( &chachapoly, tmp );
797 
798         TIME_AND_TSC( title,
799                 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
800                     BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
801 
802         mbedtls_chachapoly_free( &chachapoly );
803     }
804 #endif
805 #if defined(MBEDTLS_CMAC_C)
806     if( todo.aes_cmac )
807     {
808         unsigned char output[16];
809         const mbedtls_cipher_info_t *cipher_info;
810         mbedtls_cipher_type_t cipher_type;
811         int keysize;
812 
813         for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
814              keysize <= 256;
815              keysize += 64, cipher_type++ )
816         {
817             mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
818 
819             memset( buf, 0, sizeof( buf ) );
820             memset( tmp, 0, sizeof( tmp ) );
821 
822             cipher_info = mbedtls_cipher_info_from_type( cipher_type );
823 
824             TIME_AND_TSC( title,
825                           mbedtls_cipher_cmac( cipher_info, tmp, keysize,
826                                                buf, BUFSIZE, output ) );
827         }
828 
829         memset( buf, 0, sizeof( buf ) );
830         memset( tmp, 0, sizeof( tmp ) );
831         TIME_AND_TSC( "AES-CMAC-PRF-128",
832                       mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
833                                                 output ) );
834     }
835 #endif /* MBEDTLS_CMAC_C */
836 #endif /* MBEDTLS_AES_C */
837 
838 #if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
839     if( todo.aria )
840     {
841         int keysize;
842         mbedtls_aria_context aria;
843         mbedtls_aria_init( &aria );
844         for( keysize = 128; keysize <= 256; keysize += 64 )
845         {
846             mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
847 
848             memset( buf, 0, sizeof( buf ) );
849             memset( tmp, 0, sizeof( tmp ) );
850             mbedtls_aria_setkey_enc( &aria, tmp, keysize );
851 
852             TIME_AND_TSC( title,
853                     mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
854                         BUFSIZE, tmp, buf, buf ) );
855         }
856         mbedtls_aria_free( &aria );
857     }
858 #endif
859 
860 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
861     if( todo.camellia )
862     {
863         int keysize;
864         mbedtls_camellia_context camellia;
865         mbedtls_camellia_init( &camellia );
866         for( keysize = 128; keysize <= 256; keysize += 64 )
867         {
868             mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
869 
870             memset( buf, 0, sizeof( buf ) );
871             memset( tmp, 0, sizeof( tmp ) );
872             mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
873 
874             TIME_AND_TSC( title,
875                     mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
876                         BUFSIZE, tmp, buf, buf ) );
877         }
878         mbedtls_camellia_free( &camellia );
879     }
880 #endif
881 
882 #if defined(MBEDTLS_CHACHA20_C)
883     if ( todo.chacha20 )
884     {
885         TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
886     }
887 #endif
888 
889 #if defined(MBEDTLS_POLY1305_C)
890     if ( todo.poly1305 )
891     {
892         TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
893     }
894 #endif
895 
896 #if defined(MBEDTLS_CTR_DRBG_C)
897     if( todo.ctr_drbg )
898     {
899         mbedtls_ctr_drbg_context ctr_drbg;
900 
901         mbedtls_ctr_drbg_init( &ctr_drbg );
902         if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
903             mbedtls_exit(1);
904         TIME_AND_TSC( "CTR_DRBG (NOPR)",
905                 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
906         mbedtls_ctr_drbg_free( &ctr_drbg );
907 
908         mbedtls_ctr_drbg_init( &ctr_drbg );
909         if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
910             mbedtls_exit(1);
911         mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
912         TIME_AND_TSC( "CTR_DRBG (PR)",
913                 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
914         mbedtls_ctr_drbg_free( &ctr_drbg );
915     }
916 #endif
917 
918 #if defined(MBEDTLS_HMAC_DRBG_C) && \
919     ( defined(MBEDTLS_SHA1_C) || defined(MBEDTLS_SHA256_C) )
920     if( todo.hmac_drbg )
921     {
922         mbedtls_hmac_drbg_context hmac_drbg;
923         const mbedtls_md_info_t *md_info;
924 
925         mbedtls_hmac_drbg_init( &hmac_drbg );
926 
927 #if defined(MBEDTLS_SHA1_C)
928         if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
929             mbedtls_exit(1);
930 
931         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
932             mbedtls_exit(1);
933         TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
934                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
935 
936         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
937             mbedtls_exit(1);
938         mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
939                                              MBEDTLS_HMAC_DRBG_PR_ON );
940         TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
941                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
942 #endif
943 
944 #if defined(MBEDTLS_SHA256_C)
945         if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
946             mbedtls_exit(1);
947 
948         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
949             mbedtls_exit(1);
950         TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
951                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
952 
953         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
954             mbedtls_exit(1);
955         mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
956                                              MBEDTLS_HMAC_DRBG_PR_ON );
957         TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
958                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
959 #endif
960         mbedtls_hmac_drbg_free( &hmac_drbg );
961     }
962 #endif /* MBEDTLS_HMAC_DRBG_C && ( MBEDTLS_SHA1_C || MBEDTLS_SHA256_C ) */
963 
964 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
965     if( todo.rsa )
966     {
967         int keysize;
968         mbedtls_rsa_context rsa;
969         for( keysize = 2048; keysize <= 4096; keysize *= 2 )
970         {
971             mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
972 
973             mbedtls_rsa_init( &rsa );
974             mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
975 
976             TIME_PUBLIC( title, " public",
977                     buf[0] = 0;
978                     ret = mbedtls_rsa_public( &rsa, buf, buf ) );
979 
980             TIME_PUBLIC( title, "private",
981                     buf[0] = 0;
982                     ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
983 
984             mbedtls_rsa_free( &rsa );
985         }
986     }
987 #endif
988 
989 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
990     if( todo.dhm )
991     {
992         int dhm_sizes[] = { 2048, 3072 };
993         static const unsigned char dhm_P_2048[] =
994             MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
995         static const unsigned char dhm_P_3072[] =
996             MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
997         static const unsigned char dhm_G_2048[] =
998             MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
999         static const unsigned char dhm_G_3072[] =
1000             MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
1001 
1002         const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
1003         const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
1004                                       sizeof( dhm_P_3072 ) };
1005 
1006         const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
1007         const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
1008                                       sizeof( dhm_G_3072 ) };
1009 
1010         mbedtls_dhm_context dhm;
1011         size_t olen;
1012         size_t n;
1013         for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
1014         {
1015             mbedtls_dhm_init( &dhm );
1016 
1017             if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
1018                                          dhm_P_size[i] ) != 0 ||
1019                 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
1020                                          dhm_G_size[i] ) != 0 )
1021             {
1022                 mbedtls_exit( 1 );
1023             }
1024 
1025             n = mbedtls_mpi_size( &dhm.P );
1026             mbedtls_dhm_make_public( &dhm, (int) n, buf, n, myrand, NULL );
1027             if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
1028                 mbedtls_exit( 1 );
1029 
1030             mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
1031             TIME_PUBLIC( title, "handshake",
1032                     ret |= mbedtls_dhm_make_public( &dhm, (int) n, buf, n,
1033                                             myrand, NULL );
1034                     ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
1035 
1036             mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
1037             TIME_PUBLIC( title, "handshake",
1038                     ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
1039 
1040             mbedtls_dhm_free( &dhm );
1041         }
1042     }
1043 #endif
1044 
1045 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
1046     if( todo.ecdsa )
1047     {
1048         mbedtls_ecdsa_context ecdsa;
1049         const mbedtls_ecp_curve_info *curve_info;
1050         size_t sig_len;
1051 
1052         memset( buf, 0x2A, sizeof( buf ) );
1053 
1054         for( curve_info = curve_list;
1055              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1056              curve_info++ )
1057         {
1058             if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
1059                 continue;
1060 
1061             mbedtls_ecdsa_init( &ecdsa );
1062 
1063             if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
1064                 mbedtls_exit( 1 );
1065 
1066             mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
1067                                               curve_info->name );
1068             TIME_PUBLIC( title, "sign",
1069                     ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1070                                                 tmp, sizeof( tmp ), &sig_len, myrand, NULL ) );
1071 
1072             mbedtls_ecdsa_free( &ecdsa );
1073         }
1074 
1075         for( curve_info = curve_list;
1076              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1077              curve_info++ )
1078         {
1079             if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) )
1080                 continue;
1081 
1082             mbedtls_ecdsa_init( &ecdsa );
1083 
1084             if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
1085                 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
1086                                                tmp, sizeof( tmp ), &sig_len, myrand, NULL ) != 0 )
1087             {
1088                 mbedtls_exit( 1 );
1089             }
1090 
1091             mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
1092                                               curve_info->name );
1093             TIME_PUBLIC( title, "verify",
1094                     ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
1095                                                 tmp, sig_len ) );
1096 
1097             mbedtls_ecdsa_free( &ecdsa );
1098         }
1099     }
1100 #endif
1101 
1102 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
1103     if( todo.ecdh )
1104     {
1105         mbedtls_ecdh_context ecdh;
1106         mbedtls_mpi z;
1107         const mbedtls_ecp_curve_info montgomery_curve_list[] = {
1108 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
1109             { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
1110 #endif
1111 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
1112             { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
1113 #endif
1114             { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
1115         };
1116         const mbedtls_ecp_curve_info *curve_info;
1117         size_t olen;
1118         const mbedtls_ecp_curve_info *selected_montgomery_curve_list =
1119             montgomery_curve_list;
1120 
1121         if( curve_list == (const mbedtls_ecp_curve_info*) &single_curve )
1122         {
1123             mbedtls_ecp_group grp;
1124             mbedtls_ecp_group_init( &grp );
1125             if( mbedtls_ecp_group_load( &grp, curve_list->grp_id ) != 0 )
1126                 mbedtls_exit( 1 );
1127             if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
1128                 selected_montgomery_curve_list = single_curve;
1129             else /* empty list */
1130                 selected_montgomery_curve_list = single_curve + 1;
1131             mbedtls_ecp_group_free( &grp );
1132         }
1133 
1134         for( curve_info = curve_list;
1135              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1136              curve_info++ )
1137         {
1138             if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1139                 continue;
1140 
1141             mbedtls_ecdh_init( &ecdh );
1142 
1143             CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
1144             CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
1145                                                     myrand, NULL ) );
1146             CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
1147 
1148             mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
1149                                               curve_info->name );
1150             TIME_PUBLIC( title, "handshake",
1151                     CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
1152                                              myrand, NULL ) );
1153                     CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
1154                                              myrand, NULL ) ) );
1155             mbedtls_ecdh_free( &ecdh );
1156         }
1157 
1158         /* Montgomery curves need to be handled separately */
1159         for ( curve_info = selected_montgomery_curve_list;
1160               curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1161               curve_info++ )
1162         {
1163             mbedtls_ecdh_init( &ecdh );
1164             mbedtls_mpi_init( &z );
1165 
1166             CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
1167             CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) );
1168 
1169             mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
1170                               curve_info->name );
1171             TIME_PUBLIC(  title, "handshake",
1172                     CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
1173                                             myrand, NULL ) );
1174                     CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1175                                                 myrand, NULL ) ) );
1176 
1177             mbedtls_ecdh_free( &ecdh );
1178             mbedtls_mpi_free( &z );
1179         }
1180 
1181         for( curve_info = curve_list;
1182              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1183              curve_info++ )
1184         {
1185             if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1186                 continue;
1187 
1188             mbedtls_ecdh_init( &ecdh );
1189 
1190             CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
1191             CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
1192                                   myrand, NULL ) );
1193             CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) );
1194             CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
1195                                   myrand, NULL ) );
1196 
1197             mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
1198                                               curve_info->name );
1199             TIME_PUBLIC( title, "handshake",
1200                     CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
1201                                              myrand, NULL ) ) );
1202             mbedtls_ecdh_free( &ecdh );
1203         }
1204 
1205         /* Montgomery curves need to be handled separately */
1206         for ( curve_info = selected_montgomery_curve_list;
1207               curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1208               curve_info++)
1209         {
1210             mbedtls_ecdh_init( &ecdh );
1211             mbedtls_mpi_init( &z );
1212 
1213             CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) );
1214             CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1215                                  myrand, NULL ) );
1216             CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) );
1217 
1218             mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1219                               curve_info->name );
1220             TIME_PUBLIC(  title, "handshake",
1221                     CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1222                                                 myrand, NULL ) ) );
1223 
1224             mbedtls_ecdh_free( &ecdh );
1225             mbedtls_mpi_free( &z );
1226         }
1227     }
1228 #endif
1229 
1230 #if defined(MBEDTLS_ECDH_C)
1231     if( todo.ecdh )
1232     {
1233         mbedtls_ecdh_context ecdh_srv, ecdh_cli;
1234         unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE];
1235         const mbedtls_ecp_curve_info *curve_info;
1236         size_t olen;
1237 
1238         for( curve_info = curve_list;
1239             curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1240             curve_info++ )
1241         {
1242             if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) )
1243                 continue;
1244 
1245             mbedtls_ecdh_init( &ecdh_srv );
1246             mbedtls_ecdh_init( &ecdh_cli );
1247 
1248             mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name );
1249             TIME_PUBLIC( title, "full handshake",
1250                 const unsigned char * p_srv = buf_srv;
1251 
1252                 CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id ) );
1253                 CHECK_AND_CONTINUE( mbedtls_ecdh_make_params( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
1254 
1255                 CHECK_AND_CONTINUE( mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen ) );
1256                 CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
1257 
1258                 CHECK_AND_CONTINUE( mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen ) );
1259                 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) );
1260 
1261                 CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) );
1262                 mbedtls_ecdh_free( &ecdh_cli );
1263 
1264                 mbedtls_ecdh_free( &ecdh_srv );
1265             );
1266 
1267         }
1268     }
1269 #endif
1270 
1271     mbedtls_printf( "\n" );
1272 
1273 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1274     mbedtls_memory_buffer_alloc_free();
1275 #endif
1276 
1277     mbedtls_exit( 0 );
1278 }
1279 
1280 #endif /* MBEDTLS_HAVE_TIME */
1281