• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Benchmark demonstration program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52 
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_exit       exit
59 #define mbedtls_printf     printf
60 #define mbedtls_snprintf   snprintf
61 #define mbedtls_free       free
62 #define mbedtls_exit            exit
63 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
64 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
65 #endif
66 
67 #if !defined(MBEDTLS_TIMING_C)
main(void)68 int main( void )
69 {
70     mbedtls_printf("MBEDTLS_TIMING_C not defined.\n");
71     mbedtls_exit( 0 );
72 }
73 #else
74 
75 #include <string.h>
76 #include <stdlib.h>
77 
78 #include "mbedtls/timing.h"
79 
80 #include "mbedtls/md4.h"
81 #include "mbedtls/md5.h"
82 #include "mbedtls/ripemd160.h"
83 #include "mbedtls/sha1.h"
84 #include "mbedtls/sha256.h"
85 #include "mbedtls/sha512.h"
86 
87 #include "mbedtls/arc4.h"
88 #include "mbedtls/des.h"
89 #include "mbedtls/aes.h"
90 #include "mbedtls/aria.h"
91 #include "mbedtls/blowfish.h"
92 #include "mbedtls/camellia.h"
93 #include "mbedtls/chacha20.h"
94 #include "mbedtls/gcm.h"
95 #include "mbedtls/ccm.h"
96 #include "mbedtls/chachapoly.h"
97 #include "mbedtls/cmac.h"
98 #include "mbedtls/poly1305.h"
99 
100 #include "mbedtls/havege.h"
101 #include "mbedtls/ctr_drbg.h"
102 #include "mbedtls/hmac_drbg.h"
103 
104 #include "mbedtls/rsa.h"
105 #include "mbedtls/dhm.h"
106 #include "mbedtls/ecdsa.h"
107 #include "mbedtls/ecdh.h"
108 
109 #include "mbedtls/error.h"
110 
111 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
112 #include "mbedtls/memory_buffer_alloc.h"
113 #endif
114 
115 /*
116  * For heap usage estimates, we need an estimate of the overhead per allocated
117  * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
118  * so use that as our baseline.
119  */
120 #define MEM_BLOCK_OVERHEAD  ( 2 * sizeof( size_t ) )
121 
122 /*
123  * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
124  */
125 #define HEAP_SIZE       (1u << 16)  // 64k
126 
127 #define BUFSIZE         1024
128 #define HEADER_FORMAT   "  %-24s :  "
129 #define TITLE_LEN       25
130 
131 #define OPTIONS                                                         \
132     "md4, md5, ripemd160, sha1, sha256, sha512,\n"                      \
133     "arc4, des3, des, camellia, blowfish, chacha20,\n"                  \
134     "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n"                 \
135     "aes_cmac, des3_cmac, poly1305\n"                                   \
136     "havege, ctr_drbg, hmac_drbg\n"                                     \
137     "rsa, dhm, ecdsa, ecdh.\n"
138 
139 #if defined(MBEDTLS_ERROR_C)
140 #define PRINT_ERROR                                                     \
141         mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) );          \
142         mbedtls_printf( "FAILED: %s\n", tmp );
143 #else
144 #define PRINT_ERROR                                                     \
145         mbedtls_printf( "FAILED: -0x%04x\n", -ret );
146 #endif
147 
148 #define TIME_AND_TSC( TITLE, CODE )                                     \
149 do {                                                                    \
150     unsigned long ii, jj, tsc;                                          \
151     int ret = 0;                                                        \
152                                                                         \
153     mbedtls_printf( HEADER_FORMAT, TITLE );                             \
154     fflush( stdout );                                                   \
155                                                                         \
156     mbedtls_set_alarm( 1 );                                             \
157     for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ )           \
158     {                                                                   \
159         ret = CODE;                                                     \
160     }                                                                   \
161                                                                         \
162     tsc = mbedtls_timing_hardclock();                                   \
163     for( jj = 0; ret == 0 && jj < 1024; jj++ )                          \
164     {                                                                   \
165         ret = CODE;                                                     \
166     }                                                                   \
167                                                                         \
168     if( ret != 0 )                                                      \
169     {                                                                   \
170         PRINT_ERROR;                                                    \
171     }                                                                   \
172     else                                                                \
173     {                                                                   \
174         mbedtls_printf( "%9lu KiB/s,  %9lu cycles/byte\n",              \
175                          ii * BUFSIZE / 1024,                           \
176                          ( mbedtls_timing_hardclock() - tsc )           \
177                          / ( jj * BUFSIZE ) );                          \
178     }                                                                   \
179 } while( 0 )
180 
181 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
182 
183 /* How much space to reserve for the title when printing heap usage results.
184  * Updated manually as the output of the following command:
185  *
186  *  sed -n 's/.*[T]IME_PUBLIC.*"\(.*\)",/\1/p' programs/test/benchmark.c |
187  *      awk '{print length+2}' | sort -rn | head -n1
188  *
189  * This computes the maximum length of a title +2 (because we appends "/s").
190  * (If the value is too small, the only consequence is poor alignement.) */
191 #define TITLE_SPACE 11
192 
193 #define MEMORY_MEASURE_INIT                                             \
194     size_t max_used, max_blocks, max_bytes;                             \
195     size_t prv_used, prv_blocks;                                        \
196     mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks );      \
197     mbedtls_memory_buffer_alloc_max_reset( );
198 
199 #define MEMORY_MEASURE_PRINT( title_len )                               \
200     mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks );      \
201     ii = TITLE_SPACE > (title_len) ? TITLE_SPACE - (title_len) : 1;     \
202     while( ii-- ) mbedtls_printf( " " );                                \
203     max_used -= prv_used;                                               \
204     max_blocks -= prv_blocks;                                           \
205     max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks;             \
206     mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
207 
208 #else
209 #define MEMORY_MEASURE_INIT
210 #define MEMORY_MEASURE_PRINT( title_len )
211 #endif
212 
213 #define TIME_PUBLIC( TITLE, TYPE, CODE )                                \
214 do {                                                                    \
215     unsigned long ii;                                                   \
216     int ret;                                                            \
217     MEMORY_MEASURE_INIT;                                                \
218                                                                         \
219     mbedtls_printf( HEADER_FORMAT, TITLE );                             \
220     fflush( stdout );                                                   \
221     mbedtls_set_alarm( 3 );                                             \
222                                                                         \
223     ret = 0;                                                            \
224     for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ )             \
225     {                                                                   \
226         CODE;                                                           \
227     }                                                                   \
228                                                                         \
229     if( ret != 0 )                                                      \
230     {                                                                   \
231         PRINT_ERROR;                                                    \
232     }                                                                   \
233     else                                                                \
234     {                                                                   \
235         mbedtls_printf( "%6lu " TYPE "/s", ii / 3 );                    \
236         MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 );                     \
237         mbedtls_printf( "\n" );                                         \
238     }                                                                   \
239 } while( 0 )
240 
myrand(void * rng_state,unsigned char * output,size_t len)241 static int myrand( void *rng_state, unsigned char *output, size_t len )
242 {
243     size_t use_len;
244     int rnd;
245 
246     if( rng_state != NULL )
247         rng_state  = NULL;
248 
249     while( len > 0 )
250     {
251         use_len = len;
252         if( use_len > sizeof(int) )
253             use_len = sizeof(int);
254 
255         rnd = rand();
256         memcpy( output, &rnd, use_len );
257         output += use_len;
258         len -= use_len;
259     }
260 
261     return( 0 );
262 }
263 
264 /*
265  * Clear some memory that was used to prepare the context
266  */
267 #if defined(MBEDTLS_ECP_C)
ecp_clear_precomputed(mbedtls_ecp_group * grp)268 void ecp_clear_precomputed( mbedtls_ecp_group *grp )
269 {
270     if( grp->T != NULL )
271     {
272         size_t i;
273         for( i = 0; i < grp->T_size; i++ )
274             mbedtls_ecp_point_free( &grp->T[i] );
275         mbedtls_free( grp->T );
276     }
277     grp->T = NULL;
278     grp->T_size = 0;
279 }
280 #else
281 #define ecp_clear_precomputed( g )
282 #endif
283 
284 unsigned char buf[BUFSIZE];
285 
286 typedef struct {
287     char md4, md5, ripemd160, sha1, sha256, sha512,
288          arc4, des3, des,
289          aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly,
290          aes_cmac, des3_cmac,
291          aria, camellia, blowfish, chacha20,
292          poly1305,
293          havege, ctr_drbg, hmac_drbg,
294          rsa, dhm, ecdsa, ecdh;
295 } todo_list;
296 
297 
main(int argc,char * argv[])298 int main( int argc, char *argv[] )
299 {
300     int i;
301     unsigned char tmp[200];
302     char title[TITLE_LEN];
303     todo_list todo;
304 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
305     unsigned char alloc_buf[HEAP_SIZE] = { 0 };
306 #endif
307 
308     if( argc <= 1 )
309     {
310         memset( &todo, 1, sizeof( todo ) );
311     }
312     else
313     {
314         memset( &todo, 0, sizeof( todo ) );
315 
316         for( i = 1; i < argc; i++ )
317         {
318             if( strcmp( argv[i], "md4" ) == 0 )
319                 todo.md4 = 1;
320             else if( strcmp( argv[i], "md5" ) == 0 )
321                 todo.md5 = 1;
322             else if( strcmp( argv[i], "ripemd160" ) == 0 )
323                 todo.ripemd160 = 1;
324             else if( strcmp( argv[i], "sha1" ) == 0 )
325                 todo.sha1 = 1;
326             else if( strcmp( argv[i], "sha256" ) == 0 )
327                 todo.sha256 = 1;
328             else if( strcmp( argv[i], "sha512" ) == 0 )
329                 todo.sha512 = 1;
330             else if( strcmp( argv[i], "arc4" ) == 0 )
331                 todo.arc4 = 1;
332             else if( strcmp( argv[i], "des3" ) == 0 )
333                 todo.des3 = 1;
334             else if( strcmp( argv[i], "des" ) == 0 )
335                 todo.des = 1;
336             else if( strcmp( argv[i], "aes_cbc" ) == 0 )
337                 todo.aes_cbc = 1;
338             else if( strcmp( argv[i], "aes_xts" ) == 0 )
339                 todo.aes_xts = 1;
340             else if( strcmp( argv[i], "aes_gcm" ) == 0 )
341                 todo.aes_gcm = 1;
342             else if( strcmp( argv[i], "aes_ccm" ) == 0 )
343                 todo.aes_ccm = 1;
344             else if( strcmp( argv[i], "chachapoly" ) == 0 )
345                 todo.chachapoly = 1;
346             else if( strcmp( argv[i], "aes_cmac" ) == 0 )
347                 todo.aes_cmac = 1;
348             else if( strcmp( argv[i], "des3_cmac" ) == 0 )
349                 todo.des3_cmac = 1;
350             else if( strcmp( argv[i], "aria" ) == 0 )
351                 todo.aria = 1;
352             else if( strcmp( argv[i], "camellia" ) == 0 )
353                 todo.camellia = 1;
354             else if( strcmp( argv[i], "blowfish" ) == 0 )
355                 todo.blowfish = 1;
356             else if( strcmp( argv[i], "chacha20" ) == 0 )
357                 todo.chacha20 = 1;
358             else if( strcmp( argv[i], "poly1305" ) == 0 )
359                 todo.poly1305 = 1;
360             else if( strcmp( argv[i], "havege" ) == 0 )
361                 todo.havege = 1;
362             else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
363                 todo.ctr_drbg = 1;
364             else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
365                 todo.hmac_drbg = 1;
366             else if( strcmp( argv[i], "rsa" ) == 0 )
367                 todo.rsa = 1;
368             else if( strcmp( argv[i], "dhm" ) == 0 )
369                 todo.dhm = 1;
370             else if( strcmp( argv[i], "ecdsa" ) == 0 )
371                 todo.ecdsa = 1;
372             else if( strcmp( argv[i], "ecdh" ) == 0 )
373                 todo.ecdh = 1;
374             else
375             {
376                 mbedtls_printf( "Unrecognized option: %s\n", argv[i] );
377                 mbedtls_printf( "Available options: " OPTIONS );
378             }
379         }
380     }
381 
382     mbedtls_printf( "\n" );
383 
384 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
385     mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
386 #endif
387     memset( buf, 0xAA, sizeof( buf ) );
388     memset( tmp, 0xBB, sizeof( tmp ) );
389 
390 #if defined(MBEDTLS_MD4_C)
391     if( todo.md4 )
392         TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) );
393 #endif
394 
395 #if defined(MBEDTLS_MD5_C)
396     if( todo.md5 )
397         TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) );
398 #endif
399 
400 #if defined(MBEDTLS_RIPEMD160_C)
401     if( todo.ripemd160 )
402         TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) );
403 #endif
404 
405 #if defined(MBEDTLS_SHA1_C)
406     if( todo.sha1 )
407         TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) );
408 #endif
409 
410 #if defined(MBEDTLS_SHA256_C)
411     if( todo.sha256 )
412         TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) );
413 #endif
414 
415 #if defined(MBEDTLS_SHA512_C)
416     if( todo.sha512 )
417         TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) );
418 #endif
419 
420 #if defined(MBEDTLS_ARC4_C)
421     if( todo.arc4 )
422     {
423         mbedtls_arc4_context arc4;
424         mbedtls_arc4_init( &arc4 );
425         mbedtls_arc4_setup( &arc4, tmp, 32 );
426         TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
427         mbedtls_arc4_free( &arc4 );
428     }
429 #endif
430 
431 #if defined(MBEDTLS_DES_C)
432 #if defined(MBEDTLS_CIPHER_MODE_CBC)
433     if( todo.des3 )
434     {
435         mbedtls_des3_context des3;
436         mbedtls_des3_init( &des3 );
437         mbedtls_des3_set3key_enc( &des3, tmp );
438         TIME_AND_TSC( "3DES",
439                 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
440         mbedtls_des3_free( &des3 );
441     }
442 
443     if( todo.des )
444     {
445         mbedtls_des_context des;
446         mbedtls_des_init( &des );
447         mbedtls_des_setkey_enc( &des, tmp );
448         TIME_AND_TSC( "DES",
449                 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
450         mbedtls_des_free( &des );
451     }
452 
453 #endif /* MBEDTLS_CIPHER_MODE_CBC */
454 #if defined(MBEDTLS_CMAC_C)
455     if( todo.des3_cmac )
456     {
457         unsigned char output[8];
458         const mbedtls_cipher_info_t *cipher_info;
459 
460         memset( buf, 0, sizeof( buf ) );
461         memset( tmp, 0, sizeof( tmp ) );
462 
463         cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
464 
465         TIME_AND_TSC( "3DES-CMAC",
466                       mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
467                       BUFSIZE, output ) );
468     }
469 #endif /* MBEDTLS_CMAC_C */
470 #endif /* MBEDTLS_DES_C */
471 
472 #if defined(MBEDTLS_AES_C)
473 #if defined(MBEDTLS_CIPHER_MODE_CBC)
474     if( todo.aes_cbc )
475     {
476         int keysize;
477         mbedtls_aes_context aes;
478         mbedtls_aes_init( &aes );
479         for( keysize = 128; keysize <= 256; keysize += 64 )
480         {
481             mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
482 
483             memset( buf, 0, sizeof( buf ) );
484             memset( tmp, 0, sizeof( tmp ) );
485             mbedtls_aes_setkey_enc( &aes, tmp, keysize );
486 
487             TIME_AND_TSC( title,
488                 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
489         }
490         mbedtls_aes_free( &aes );
491     }
492 #endif
493 #if defined(MBEDTLS_CIPHER_MODE_XTS)
494     if( todo.aes_xts )
495     {
496         int keysize;
497         mbedtls_aes_xts_context ctx;
498 
499         mbedtls_aes_xts_init( &ctx );
500         for( keysize = 128; keysize <= 256; keysize += 128 )
501         {
502             mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize );
503 
504             memset( buf, 0, sizeof( buf ) );
505             memset( tmp, 0, sizeof( tmp ) );
506             mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 );
507 
508             TIME_AND_TSC( title,
509                     mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE,
510                                            tmp, buf, buf ) );
511 
512             mbedtls_aes_xts_free( &ctx );
513         }
514     }
515 #endif
516 #if defined(MBEDTLS_GCM_C)
517     if( todo.aes_gcm )
518     {
519         int keysize;
520         mbedtls_gcm_context gcm;
521 
522         mbedtls_gcm_init( &gcm );
523         for( keysize = 128; keysize <= 256; keysize += 64 )
524         {
525             mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
526 
527             memset( buf, 0, sizeof( buf ) );
528             memset( tmp, 0, sizeof( tmp ) );
529             mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
530 
531             TIME_AND_TSC( title,
532                     mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
533                         12, NULL, 0, buf, buf, 16, tmp ) );
534 
535             mbedtls_gcm_free( &gcm );
536         }
537     }
538 #endif
539 #if defined(MBEDTLS_CCM_C)
540     if( todo.aes_ccm )
541     {
542         int keysize;
543         mbedtls_ccm_context ccm;
544 
545         mbedtls_ccm_init( &ccm );
546         for( keysize = 128; keysize <= 256; keysize += 64 )
547         {
548             mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
549 
550             memset( buf, 0, sizeof( buf ) );
551             memset( tmp, 0, sizeof( tmp ) );
552             mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
553 
554             TIME_AND_TSC( title,
555                     mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
556                         12, NULL, 0, buf, buf, tmp, 16 ) );
557 
558             mbedtls_ccm_free( &ccm );
559         }
560     }
561 #endif
562 #if defined(MBEDTLS_CHACHAPOLY_C)
563     if( todo.chachapoly )
564     {
565         mbedtls_chachapoly_context chachapoly;
566 
567         mbedtls_chachapoly_init( &chachapoly );
568         memset( buf, 0, sizeof( buf ) );
569         memset( tmp, 0, sizeof( tmp ) );
570 
571         mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" );
572 
573         mbedtls_chachapoly_setkey( &chachapoly, tmp );
574 
575         TIME_AND_TSC( title,
576                 mbedtls_chachapoly_encrypt_and_tag( &chachapoly,
577                     BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) );
578 
579         mbedtls_chachapoly_free( &chachapoly );
580     }
581 #endif
582 #if defined(MBEDTLS_CMAC_C)
583     if( todo.aes_cmac )
584     {
585         unsigned char output[16];
586         const mbedtls_cipher_info_t *cipher_info;
587         mbedtls_cipher_type_t cipher_type;
588         int keysize;
589 
590         for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
591              keysize <= 256;
592              keysize += 64, cipher_type++ )
593         {
594             mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
595 
596             memset( buf, 0, sizeof( buf ) );
597             memset( tmp, 0, sizeof( tmp ) );
598 
599             cipher_info = mbedtls_cipher_info_from_type( cipher_type );
600 
601             TIME_AND_TSC( title,
602                           mbedtls_cipher_cmac( cipher_info, tmp, keysize,
603                                                buf, BUFSIZE, output ) );
604         }
605 
606         memset( buf, 0, sizeof( buf ) );
607         memset( tmp, 0, sizeof( tmp ) );
608         TIME_AND_TSC( "AES-CMAC-PRF-128",
609                       mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
610                                                 output ) );
611     }
612 #endif /* MBEDTLS_CMAC_C */
613 #endif /* MBEDTLS_AES_C */
614 
615 #if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
616     if( todo.aria )
617     {
618         int keysize;
619         mbedtls_aria_context aria;
620         mbedtls_aria_init( &aria );
621         for( keysize = 128; keysize <= 256; keysize += 64 )
622         {
623             mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize );
624 
625             memset( buf, 0, sizeof( buf ) );
626             memset( tmp, 0, sizeof( tmp ) );
627             mbedtls_aria_setkey_enc( &aria, tmp, keysize );
628 
629             TIME_AND_TSC( title,
630                     mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT,
631                         BUFSIZE, tmp, buf, buf ) );
632         }
633         mbedtls_aria_free( &aria );
634     }
635 #endif
636 
637 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
638     if( todo.camellia )
639     {
640         int keysize;
641         mbedtls_camellia_context camellia;
642         mbedtls_camellia_init( &camellia );
643         for( keysize = 128; keysize <= 256; keysize += 64 )
644         {
645             mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
646 
647             memset( buf, 0, sizeof( buf ) );
648             memset( tmp, 0, sizeof( tmp ) );
649             mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
650 
651             TIME_AND_TSC( title,
652                     mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
653                         BUFSIZE, tmp, buf, buf ) );
654         }
655         mbedtls_camellia_free( &camellia );
656     }
657 #endif
658 
659 #if defined(MBEDTLS_CHACHA20_C)
660     if ( todo.chacha20 )
661     {
662         TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) );
663     }
664 #endif
665 
666 #if defined(MBEDTLS_POLY1305_C)
667     if ( todo.poly1305 )
668     {
669         TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) );
670     }
671 #endif
672 
673 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
674     if( todo.blowfish )
675     {
676         int keysize;
677         mbedtls_blowfish_context blowfish;
678         mbedtls_blowfish_init( &blowfish );
679 
680         for( keysize = 128; keysize <= 256; keysize += 64 )
681         {
682             mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
683 
684             memset( buf, 0, sizeof( buf ) );
685             memset( tmp, 0, sizeof( tmp ) );
686             mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
687 
688             TIME_AND_TSC( title,
689                     mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
690                         tmp, buf, buf ) );
691         }
692 
693         mbedtls_blowfish_free( &blowfish );
694     }
695 #endif
696 
697 #if defined(MBEDTLS_HAVEGE_C)
698     if( todo.havege )
699     {
700         mbedtls_havege_state hs;
701         mbedtls_havege_init( &hs );
702         TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
703         mbedtls_havege_free( &hs );
704     }
705 #endif
706 
707 #if defined(MBEDTLS_CTR_DRBG_C)
708     if( todo.ctr_drbg )
709     {
710         mbedtls_ctr_drbg_context ctr_drbg;
711 
712         mbedtls_ctr_drbg_init( &ctr_drbg );
713         if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
714             mbedtls_exit(1);
715         TIME_AND_TSC( "CTR_DRBG (NOPR)",
716                 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
717         mbedtls_ctr_drbg_free( &ctr_drbg );
718 
719         mbedtls_ctr_drbg_init( &ctr_drbg );
720         if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
721             mbedtls_exit(1);
722         mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
723         TIME_AND_TSC( "CTR_DRBG (PR)",
724                 mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) );
725         mbedtls_ctr_drbg_free( &ctr_drbg );
726     }
727 #endif
728 
729 #if defined(MBEDTLS_HMAC_DRBG_C)
730     if( todo.hmac_drbg )
731     {
732         mbedtls_hmac_drbg_context hmac_drbg;
733         const mbedtls_md_info_t *md_info;
734 
735         mbedtls_hmac_drbg_init( &hmac_drbg );
736 
737 #if defined(MBEDTLS_SHA1_C)
738         if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
739             mbedtls_exit(1);
740 
741         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
742             mbedtls_exit(1);
743         TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
744                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
745 
746         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
747             mbedtls_exit(1);
748         mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
749                                              MBEDTLS_HMAC_DRBG_PR_ON );
750         TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
751                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
752 #endif
753 
754 #if defined(MBEDTLS_SHA256_C)
755         if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
756             mbedtls_exit(1);
757 
758         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
759             mbedtls_exit(1);
760         TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
761                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
762 
763         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
764             mbedtls_exit(1);
765         mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
766                                              MBEDTLS_HMAC_DRBG_PR_ON );
767         TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
768                 mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) );
769 #endif
770         mbedtls_hmac_drbg_free( &hmac_drbg );
771     }
772 #endif
773 
774 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
775     if( todo.rsa )
776     {
777         int keysize;
778         mbedtls_rsa_context rsa;
779         for( keysize = 2048; keysize <= 4096; keysize *= 2 )
780         {
781             mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
782 
783             mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
784             mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
785 
786             TIME_PUBLIC( title, " public",
787                     buf[0] = 0;
788                     ret = mbedtls_rsa_public( &rsa, buf, buf ) );
789 
790             TIME_PUBLIC( title, "private",
791                     buf[0] = 0;
792                     ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) );
793 
794             mbedtls_rsa_free( &rsa );
795         }
796     }
797 #endif
798 
799 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
800     if( todo.dhm )
801     {
802         int dhm_sizes[] = { 2048, 3072 };
803         static const unsigned char dhm_P_2048[] =
804             MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
805         static const unsigned char dhm_P_3072[] =
806             MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN;
807         static const unsigned char dhm_G_2048[] =
808             MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
809         static const unsigned char dhm_G_3072[] =
810             MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN;
811 
812         const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 };
813         const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ),
814                                       sizeof( dhm_P_3072 ) };
815 
816         const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 };
817         const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ),
818                                       sizeof( dhm_G_3072 ) };
819 
820         mbedtls_dhm_context dhm;
821         size_t olen;
822         for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
823         {
824             mbedtls_dhm_init( &dhm );
825 
826             if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i],
827                                          dhm_P_size[i] ) != 0 ||
828                 mbedtls_mpi_read_binary( &dhm.G, dhm_G[i],
829                                          dhm_G_size[i] ) != 0 )
830             {
831                 mbedtls_exit( 1 );
832             }
833 
834             dhm.len = mbedtls_mpi_size( &dhm.P );
835             mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
836             if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
837                 mbedtls_exit( 1 );
838 
839             mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
840             TIME_PUBLIC( title, "handshake",
841                     ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
842                                             myrand, NULL );
843                     ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
844 
845             mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
846             TIME_PUBLIC( title, "handshake",
847                     ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
848 
849             mbedtls_dhm_free( &dhm );
850         }
851     }
852 #endif
853 
854 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
855     if( todo.ecdsa )
856     {
857         mbedtls_ecdsa_context ecdsa;
858         const mbedtls_ecp_curve_info *curve_info;
859         size_t sig_len;
860 
861         memset( buf, 0x2A, sizeof( buf ) );
862 
863         for( curve_info = mbedtls_ecp_curve_list();
864              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
865              curve_info++ )
866         {
867             mbedtls_ecdsa_init( &ecdsa );
868 
869             if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
870                 mbedtls_exit( 1 );
871             ecp_clear_precomputed( &ecdsa.grp );
872 
873             mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
874                                               curve_info->name );
875             TIME_PUBLIC( title, "sign",
876                     ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
877                                                 tmp, &sig_len, myrand, NULL ) );
878 
879             mbedtls_ecdsa_free( &ecdsa );
880         }
881 
882         for( curve_info = mbedtls_ecp_curve_list();
883              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
884              curve_info++ )
885         {
886             mbedtls_ecdsa_init( &ecdsa );
887 
888             if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
889                 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
890                                                tmp, &sig_len, myrand, NULL ) != 0 )
891             {
892                 mbedtls_exit( 1 );
893             }
894             ecp_clear_precomputed( &ecdsa.grp );
895 
896             mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
897                                               curve_info->name );
898             TIME_PUBLIC( title, "verify",
899                     ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
900                                                 tmp, sig_len ) );
901 
902             mbedtls_ecdsa_free( &ecdsa );
903         }
904     }
905 #endif
906 
907 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
908     if( todo.ecdh )
909     {
910         mbedtls_ecdh_context ecdh;
911         mbedtls_mpi z;
912         const mbedtls_ecp_curve_info montgomery_curve_list[] = {
913 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
914             { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" },
915 #endif
916 #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
917             { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" },
918 #endif
919             { MBEDTLS_ECP_DP_NONE, 0, 0, 0 }
920         };
921         const mbedtls_ecp_curve_info *curve_info;
922         size_t olen;
923 
924         for( curve_info = mbedtls_ecp_curve_list();
925              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
926              curve_info++ )
927         {
928             mbedtls_ecdh_init( &ecdh );
929 
930             if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
931                 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
932                                   myrand, NULL ) != 0 ||
933                 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
934             {
935                 mbedtls_exit( 1 );
936             }
937             ecp_clear_precomputed( &ecdh.grp );
938 
939             mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
940                                               curve_info->name );
941             TIME_PUBLIC( title, "handshake",
942                     ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
943                                              myrand, NULL );
944                     ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
945                                              myrand, NULL ) );
946             mbedtls_ecdh_free( &ecdh );
947         }
948 
949         /* Montgomery curves need to be handled separately */
950         for ( curve_info = montgomery_curve_list;
951               curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
952               curve_info++ )
953         {
954             mbedtls_ecdh_init( &ecdh );
955             mbedtls_mpi_init( &z );
956 
957             if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
958                 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
959             {
960                 mbedtls_exit( 1 );
961             }
962 
963             mbedtls_snprintf( title, sizeof(title), "ECDHE-%s",
964                               curve_info->name );
965             TIME_PUBLIC(  title, "handshake",
966                     ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
967                                             myrand, NULL );
968                     ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
969                                                 myrand, NULL ) );
970 
971             mbedtls_ecdh_free( &ecdh );
972             mbedtls_mpi_free( &z );
973         }
974 
975         for( curve_info = mbedtls_ecp_curve_list();
976              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
977              curve_info++ )
978         {
979             mbedtls_ecdh_init( &ecdh );
980 
981             if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
982                 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
983                                   myrand, NULL ) != 0 ||
984                 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
985                 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
986                                   myrand, NULL ) != 0 )
987             {
988                 mbedtls_exit( 1 );
989             }
990             ecp_clear_precomputed( &ecdh.grp );
991 
992             mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
993                                               curve_info->name );
994             TIME_PUBLIC( title, "handshake",
995                     ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
996                                              myrand, NULL ) );
997             mbedtls_ecdh_free( &ecdh );
998         }
999 
1000         /* Montgomery curves need to be handled separately */
1001         for ( curve_info = montgomery_curve_list;
1002               curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
1003               curve_info++)
1004         {
1005             mbedtls_ecdh_init( &ecdh );
1006             mbedtls_mpi_init( &z );
1007 
1008             if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
1009                 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
1010                                  myrand, NULL ) != 0 ||
1011                 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
1012             {
1013                 mbedtls_exit( 1 );
1014             }
1015 
1016             mbedtls_snprintf( title, sizeof(title), "ECDH-%s",
1017                               curve_info->name );
1018             TIME_PUBLIC(  title, "handshake",
1019                     ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
1020                                                 myrand, NULL ) );
1021 
1022             mbedtls_ecdh_free( &ecdh );
1023             mbedtls_mpi_free( &z );
1024         }
1025     }
1026 #endif
1027 
1028     mbedtls_printf( "\n" );
1029 
1030 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
1031     mbedtls_memory_buffer_alloc_free();
1032 #endif
1033 
1034 #if defined(_WIN32)
1035     mbedtls_printf( "  Press Enter to exit this program.\n" );
1036     fflush( stdout ); getchar();
1037 #endif
1038 
1039     mbedtls_exit( 0 );
1040 }
1041 
1042 #endif /* MBEDTLS_TIMING_C */
1043