1 /*
2  *  Benchmark demonstration program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(TARGET_LIKE_MBED)
23 
24 #include <stdio.h>
25 
main()26 int main() {
27     printf("this version of this program only works on mbed OS\n");
28     return 0;
29 }
30 
31 #else
32 
33 #if !defined(MBEDTLS_CONFIG_FILE)
34 #include "mbedtls/config.h"
35 #else
36 #include MBEDTLS_CONFIG_FILE
37 #endif
38 
39 #if defined(MBEDTLS_PLATFORM_C)
40 #include "mbedtls/platform.h"
41 #else
42 #include <stdio.h>
43 #define mbedtls_exit       exit
44 #define mbedtls_printf     printf
45 #define mbedtls_snprintf   snprintf
46 #define mbedtls_free       free
47 #endif
48 
49 #include <string.h>
50 
51 #include "mbedtls/md4.h"
52 #include "mbedtls/md5.h"
53 #include "mbedtls/ripemd160.h"
54 #include "mbedtls/sha1.h"
55 #include "mbedtls/sha256.h"
56 #include "mbedtls/sha512.h"
57 #include "mbedtls/arc4.h"
58 #include "mbedtls/des.h"
59 #include "mbedtls/aes.h"
60 #include "mbedtls/blowfish.h"
61 #include "mbedtls/camellia.h"
62 #include "mbedtls/gcm.h"
63 #include "mbedtls/ccm.h"
64 #include "mbedtls/havege.h"
65 #include "mbedtls/ctr_drbg.h"
66 #include "mbedtls/hmac_drbg.h"
67 #include "mbedtls/rsa.h"
68 #include "mbedtls/pk.h"
69 #include "mbedtls/dhm.h"
70 #include "mbedtls/ecdsa.h"
71 #include "mbedtls/ecdh.h"
72 #include "mbedtls/error.h"
73 
74 #include "mbed-drivers/mbed.h"
75 
76 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
77 #include "mbedtls/memory_buffer_alloc.h"
78 #endif
79 
80 #define RSA_PRIVATE_KEY_2048                                            \
81 "-----BEGIN RSA PRIVATE KEY-----\r\n"                                   \
82 "MIIEogIBAAKCAQEA2dwVr+IMGEtA2/MCP6fA5eb/6B18Bq6e7gw8brNPkm3E6LyR\r\n"  \
83 "4DnMJVxZmw3bPDKBDoKzfntkMESi/Yw5UopLtVfjGfWeQWPClqffLZBsZ60BRAsg\r\n"  \
84 "/g+ID5tgzxSuxzftypK59uexOVCAm7hCKZHGO3DbI7bLY27j7VAgEP7d/yuaz5Fx\r\n"  \
85 "Kl/vu7shqrBoz6ABJVJD3KC8nUiMRUCXRINmxbyUUjA4DnicZv6+xrGKr36r6M8h\r\n"  \
86 "VYLa5msKc8WzbnBWzpUsrpb4/r7ML+qp92gdSfVJ8/bLiU7h2C7faDA59uaqrFK9\r\n"  \
87 "xmDdx7FaWhGQs3LWW6w1UNgkPS0FDYUslpsnsQIDAQABAoIBAC7IJNwM5V3+IuJY\r\n"  \
88 "T35Nzo1PyloUosJokvY5KGz5Ejg2XBdCDu0gXCcVqqQyGIbXrYDpLhQV+RCoXHun\r\n"  \
89 "tdN0oQdC5SB47s/J1Uo2qCUHo0+sBd6PqTkFKsl3KxWssk9TQjvCwC412IefMs69\r\n"  \
90 "hW+ZvwCanmQP56LleApIr2oW4KLfW8Ry/QfZlua+dizctdN7+H1mWwgZQTY9T27J\r\n"  \
91 "6RtGRA5NVkKVPzIHVJfdpKoO7xGg1g06aEbPB/VmGvZaaFWWnaf7uRvFjLZecBLu\r\n"  \
92 "QSx2DA/GDjirlDYj99PJb7DtB4xRtKzsyw0o+xapC8w6OtIl/3xFt9moCu2jGrsx\r\n"  \
93 "vpjHdfECgYEA7fSACRseIs9gAIVX8wq6g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096                                            \
110 "-----BEGIN RSA PRIVATE KEY-----\r\n"                                   \
111 "MIIJKgIBAAKCAgEAmkdGjoIshJuOt2NO47qB3Z3yyvmLg2j351isItSNuFQU3qr+\r\n"  \
112 "jXHIeANf03yw/K0Zvos8RPd+CqLjoxAQL3QDH4bZAl88bIo29i+SANbNSrKQmc0k\r\n"  \
113 "pH+yzw3alDzO0GZaOPZjsbo6AwBrno5msi0vRuC2aY8vGLPsZWSyLai7tneS1j/o\r\n"  \
114 "vYW6XIo8Cj61j2Ypy9HhVUW/4Wc+zAT25D/x7jTpkqJLWWT+YzibNbOY48M5eJcB\r\n"  \
115 "6/sMyUIeI3/u/wXyMrooNyLiCpedkuHRA0m7u5cWPTUISTunSRlVFij/NHJjuU8e\r\n"  \
116 "wA3B29yfZFsUqDEnyc+OxniIueAixTomVszxAaVn8zFEbYhFMPqziiFp99u3jfeG\r\n"  \
117 "k1q9mmUi/uCfUC4e2IC5rqq1ZbKSduH7Ug/Vn2bGQahww0sZFRHDXFrnBcotcW+M\r\n"  \
118 "bnC290VBDnYgzmdYrIOxuPb2aUwJo4ZlbKh5uBB1PigMuyhLKibQ1a+V5ZJGdpP6\r\n"  \
119 "SE9PGIdgYWSmh2QEMuLE6v+wTO2LQ5JgqsvFfi3GIZvkn0s8jTS72Jq2uMkFkMer\r\n"  \
120 "UBjPDYaSPy5kpo103KerWs+cMPOJ/3FtZzI++7MoSUTkWVr1ySQFt5i1EIZ/0Thi\r\n"  \
121 "jut2jNe8a4AoA3TtC8Rkk/3AIIbg8MVNT4EnT+KHROTMu6gET1oJ3YfBRpUCAwEA\r\n"  \
122 "AQKCAgEAhuNSmT7PVZH8kfLOAuYKrY1vvm+4v0iDl048Eqfs0QESziyLK3gUYnnw\r\n"  \
123 "yqP2yrU+EQ8Dvvj0xq/sf6GHxTWVlXb9PcmutueRbmXhLcKg83J0Y0StiPXtjIL8\r\n"  \
124 "XSddW3Bh6fPi7n14Qy+W6KZwu9AtybanRlvePabyRSRpdOpW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F8B+4WkkVuFXnpWW33yCOaRyPVOPHpnclr5WU1fS+3Q85QkW9rkej\r\n"  \
154 "97zlkl0QY9AHJqrXnoML1ywAK7ns+MVyNK8CggEAf62xcKZhOb1djeF72Ms+i/i/\r\n"  \
155 "WIAq4Q4YpsElgvJTHpNH2v9g4ngSTKe3ws3bGc502sWRlhcoTFMOW2rJNe/iqKkb\r\n"  \
156 "3cdeTkseDbpqozmJWz9dJWSVtXas2bZjzBEa//gQ7nHGVeQdqZJQ9rxPsoOAkfpi\r\n"  \
157 "qCFrmfUVUqC53e3XMt8+W+aSvKl+JZiB9ozkO9A6Q0vfQLKtjUMdQE3XaCFQT8DI\r\n"  \
158 "smaLBlBmeRaBpc02ENeC4ADlWosm1SwgxqMhuh2Alba/GrHOoPlVl4hDs9Fb5a6R\r\n"  \
159 "rmpXSt07GAxnG6j9jssA95E4rc1zO0CVKG5bvjVTxwi/sT0/VVX7VsJM4uTAQg==\r\n"  \
160 "-----END RSA PRIVATE KEY-----\r\n"
161 
162 #if defined _MSC_VER && !defined snprintf
163 #define snprintf _snprintf
164 #endif
165 
166 /*
167  * For heap usage estimates, we need an estimate of the overhead per allocated
168  * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
169  * so use that as our baseline.
170  */
171 #define MEM_BLOCK_OVERHEAD  ( 2 * sizeof( size_t ) )
172 
173 /*
174  * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
175  */
176 #define HEAP_SIZE       (1u << 16)  // 64k
177 
178 #define BUFSIZE         1024
179 #define HEADER_FORMAT   "  %-24s :  "
180 #define TITLE_LEN       25
181 
182 #define OPTIONS                                                         \
183     "md4, md5, ripemd160, sha1, sha256, sha512,\r\n"                      \
184     "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\r\n" \
185     "havege, ctr_drbg, hmac_drbg\r\n"                                     \
186     "rsa, dhm, ecdsa, ecdh.\r\n"
187 
188 #if defined(MBEDTLS_ERROR_C)
189 #define PRINT_ERROR                                                     \
190         mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) );         \
191         mbedtls_printf( "FAILED: %s\r\n", tmp );
192 #else
193 #define PRINT_ERROR                                                     \
194         mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
195 #endif
196 
mbedtls_timing_hardclock(void)197 static unsigned long mbedtls_timing_hardclock( void )
198 {
199     static int dwt_started = 0;
200 
201     if( dwt_started == 0 )
202     {
203         CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
204         DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
205     }
206 
207     return( DWT->CYCCNT );
208 }
209 
210 static volatile int alarmed;
alarm()211 static void alarm() { alarmed = 1; }
212 
213 #define TIME_AND_TSC( TITLE, CODE )                                     \
214 do {                                                                    \
215     unsigned long i, j, tsc;                                            \
216     Timeout t; \
217                                                                         \
218     mbedtls_printf( HEADER_FORMAT, TITLE );                            \
219     fflush( stdout );                                                   \
220                                                                         \
221     for( i = 1, alarmed = 0, t.attach( alarm, 1.0 ); !alarmed; i++ )                                        \
222     {                                                                   \
223         CODE;                                                           \
224     }                                                                   \
225                                                                         \
226     tsc = mbedtls_timing_hardclock();                                                  \
227     for( j = 0; j < 1024; j++ )                                         \
228     {                                                                   \
229         CODE;                                                           \
230     }                                                                   \
231                                                                         \
232     mbedtls_printf( "%9lu Kb/s,  %9lu cycles/byte\r\n",                  \
233                      i * BUFSIZE / 1024,                                \
234                      ( mbedtls_timing_hardclock() - tsc ) / ( j * BUFSIZE ) );         \
235 } while( 0 )
236 
237 #if defined(MBEDTLS_ERROR_C)
238 #define PRINT_ERROR                                                     \
239         mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) );         \
240         mbedtls_printf( "FAILED: %s\r\n", tmp );
241 #else
242 #define PRINT_ERROR                                                     \
243         mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
244 #endif
245 
246 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
247 
248 #define MEMORY_MEASURE_INIT                                             \
249     size_t max_used, max_blocks, max_bytes;                             \
250     size_t prv_used, prv_blocks;                                        \
251     mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks );              \
252     mbedtls_memory_buffer_alloc_max_reset( );
253 
254 #define MEMORY_MEASURE_PRINT( title_len )                               \
255     mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks );              \
256     for( i = 12 - title_len; i != 0; i-- ) mbedtls_printf( " " );      \
257     max_used -= prv_used;                                               \
258     max_blocks -= prv_blocks;                                           \
259     max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks;             \
260     mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
261 
262 #else
263 #define MEMORY_MEASURE_INIT
264 #define MEMORY_MEASURE_PRINT( title_len )
265 #endif
266 
267 #define TIME_PUBLIC( TITLE, TYPE, CODE )                                \
268 do {                                                                    \
269     unsigned long ms;                                                    \
270     int ret = 0;                                                            \
271     Timer t; \
272     MEMORY_MEASURE_INIT;                                                \
273                                                                         \
274     mbedtls_printf( HEADER_FORMAT, TITLE );                            \
275     fflush( stdout );                                                   \
276     \
277     t.start(); \
278     CODE; \
279     t.stop(); \
280     ms = t.read_ms(); \
281                                                                         \
282     if( ret != 0 )                                                      \
283     {                                                                   \
284         PRINT_ERROR;                                                    \
285     }                                                                   \
286     else                                                                \
287     {                                                                   \
288         mbedtls_printf( "%6lu ms/" TYPE, ms );                    \
289         MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 );                     \
290         mbedtls_printf( "\r\n" );                                        \
291     }                                                                   \
292 } while( 0 )
293 
myrand(void * rng_state,unsigned char * output,size_t len)294 static int myrand( void *rng_state, unsigned char *output, size_t len )
295 {
296     size_t use_len;
297     int rnd;
298 
299     if( rng_state != NULL )
300         rng_state  = NULL;
301 
302     while( len > 0 )
303     {
304         use_len = len;
305         if( use_len > sizeof(int) )
306             use_len = sizeof(int);
307 
308         rnd = rand();
309         memcpy( output, &rnd, use_len );
310         output += use_len;
311         len -= use_len;
312     }
313 
314     return( 0 );
315 }
316 
317 /*
318  * Clear some memory that was used to prepare the context
319  */
320 #if defined(MBEDTLS_ECP_C)
ecp_clear_precomputed(mbedtls_ecp_group * grp)321 void ecp_clear_precomputed( mbedtls_ecp_group *grp )
322 {
323     if( grp->T != NULL )
324     {
325         size_t i;
326         for( i = 0; i < grp->T_size; i++ )
327             mbedtls_ecp_point_free( &grp->T[i] );
328         mbedtls_free( grp->T );
329     }
330     grp->T = NULL;
331     grp->T_size = 0;
332 }
333 #else
334 #define ecp_clear_precomputed( g )
335 #endif
336 
337 unsigned char buf[BUFSIZE];
338 
339 typedef struct {
340     char md4, md5, ripemd160, sha1, sha256, sha512,
341          arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
342          havege, ctr_drbg, hmac_drbg,
343          rsa, dhm, ecdsa, ecdh;
344 } todo_list;
345 
benchmark(int argc,char * argv[])346 int benchmark( int argc, char *argv[] )
347 {
348     int i;
349     unsigned char tmp[200];
350     char title[TITLE_LEN];
351     todo_list todo;
352 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
353     unsigned char malloc_buf[HEAP_SIZE] = { 0 };
354 #endif
355 
356     if( argc <= 1 )
357     {
358         memset( &todo, 1, sizeof( todo ) );
359     }
360     else
361     {
362         memset( &todo, 0, sizeof( todo ) );
363 
364         for( i = 1; i < argc; i++ )
365         {
366             if( strcmp( argv[i], "md4" ) == 0 )
367                 todo.md4 = 1;
368             else if( strcmp( argv[i], "md5" ) == 0 )
369                 todo.md5 = 1;
370             else if( strcmp( argv[i], "ripemd160" ) == 0 )
371                 todo.ripemd160 = 1;
372             else if( strcmp( argv[i], "sha1" ) == 0 )
373                 todo.sha1 = 1;
374             else if( strcmp( argv[i], "sha256" ) == 0 )
375                 todo.sha256 = 1;
376             else if( strcmp( argv[i], "sha512" ) == 0 )
377                 todo.sha512 = 1;
378             else if( strcmp( argv[i], "arc4" ) == 0 )
379                 todo.arc4 = 1;
380             else if( strcmp( argv[i], "des3" ) == 0 )
381                 todo.des3 = 1;
382             else if( strcmp( argv[i], "des" ) == 0 )
383                 todo.des = 1;
384             else if( strcmp( argv[i], "aes_cbc" ) == 0 )
385                 todo.aes_cbc = 1;
386             else if( strcmp( argv[i], "aes_gcm" ) == 0 )
387                 todo.aes_gcm = 1;
388             else if( strcmp( argv[i], "aes_ccm" ) == 0 )
389                 todo.aes_ccm = 1;
390             else if( strcmp( argv[i], "camellia" ) == 0 )
391                 todo.camellia = 1;
392             else if( strcmp( argv[i], "blowfish" ) == 0 )
393                 todo.blowfish = 1;
394             else if( strcmp( argv[i], "havege" ) == 0 )
395                 todo.havege = 1;
396             else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
397                 todo.ctr_drbg = 1;
398             else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
399                 todo.hmac_drbg = 1;
400             else if( strcmp( argv[i], "rsa" ) == 0 )
401                 todo.rsa = 1;
402             else if( strcmp( argv[i], "dhm" ) == 0 )
403                 todo.dhm = 1;
404             else if( strcmp( argv[i], "ecdsa" ) == 0 )
405                 todo.ecdsa = 1;
406             else if( strcmp( argv[i], "ecdh" ) == 0 )
407                 todo.ecdh = 1;
408             else
409             {
410                 mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] );
411                 mbedtls_printf( "Available options: " OPTIONS );
412             }
413         }
414     }
415 
416     mbedtls_printf( "\r\n\r\n" );
417 
418 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
419     mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
420 #endif
421     memset( buf, 0xAA, sizeof( buf ) );
422     memset( tmp, 0xBB, sizeof( tmp ) );
423 
424 #if defined(MBEDTLS_MD4_C)
425     if( todo.md4 )
426         TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) );
427 #endif
428 
429 #if defined(MBEDTLS_MD5_C)
430     if( todo.md5 )
431         TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
432 #endif
433 
434 #if defined(MBEDTLS_RIPEMD160_C)
435     if( todo.ripemd160 )
436         TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
437 #endif
438 
439 #if defined(MBEDTLS_SHA1_C)
440     if( todo.sha1 )
441         TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
442 #endif
443 
444 #if defined(MBEDTLS_SHA256_C)
445     if( todo.sha256 )
446         TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
447 #endif
448 
449 #if defined(MBEDTLS_SHA512_C)
450     if( todo.sha512 )
451         TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
452 #endif
453 
454 #if defined(MBEDTLS_ARC4_C)
455     if( todo.arc4 )
456     {
457         mbedtls_arc4_context arc4;
458         mbedtls_arc4_init( &arc4 );
459         mbedtls_arc4_setup( &arc4, tmp, 32 );
460         TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
461         mbedtls_arc4_free( &arc4 );
462     }
463 #endif
464 
465 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
466     if( todo.des3 )
467     {
468         mbedtls_des3_context des3;
469         mbedtls_des3_init( &des3 );
470         mbedtls_des3_set3key_enc( &des3, tmp );
471         TIME_AND_TSC( "3DES",
472                 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
473         mbedtls_des3_free( &des3 );
474     }
475 
476     if( todo.des )
477     {
478         mbedtls_des_context des;
479         mbedtls_des_init( &des );
480         mbedtls_des_setkey_enc( &des, tmp );
481         TIME_AND_TSC( "DES",
482                 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
483         mbedtls_des_free( &des );
484     }
485 #endif
486 
487 #if defined(MBEDTLS_AES_C)
488 #if defined(MBEDTLS_CIPHER_MODE_CBC)
489     if( todo.aes_cbc )
490     {
491         int keysize;
492         mbedtls_aes_context aes;
493         mbedtls_aes_init( &aes );
494         for( keysize = 128; keysize <= 256; keysize += 64 )
495         {
496             mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
497 
498             memset( buf, 0, sizeof( buf ) );
499             memset( tmp, 0, sizeof( tmp ) );
500             mbedtls_aes_setkey_enc( &aes, tmp, keysize );
501 
502             TIME_AND_TSC( title,
503                 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
504         }
505         mbedtls_aes_free( &aes );
506     }
507 #endif
508 #if defined(MBEDTLS_GCM_C)
509     if( todo.aes_gcm )
510     {
511         int keysize;
512         mbedtls_gcm_context gcm;
513 
514         mbedtls_gcm_init( &gcm );
515         for( keysize = 128; keysize <= 256; keysize += 64 )
516         {
517             mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
518 
519             memset( buf, 0, sizeof( buf ) );
520             memset( tmp, 0, sizeof( tmp ) );
521             mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
522 
523             TIME_AND_TSC( title,
524                     mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
525                         12, NULL, 0, buf, buf, 16, tmp ) );
526 
527             mbedtls_gcm_free( &gcm );
528         }
529     }
530 #endif
531 #if defined(MBEDTLS_CCM_C)
532     if( todo.aes_ccm )
533     {
534         int keysize;
535         mbedtls_ccm_context ccm;
536 
537         mbedtls_ccm_init( &ccm );
538         for( keysize = 128; keysize <= 256; keysize += 64 )
539         {
540             mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
541 
542             memset( buf, 0, sizeof( buf ) );
543             memset( tmp, 0, sizeof( tmp ) );
544             mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
545 
546             TIME_AND_TSC( title,
547                     mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
548                         12, NULL, 0, buf, buf, tmp, 16 ) );
549 
550             mbedtls_ccm_free( &ccm );
551         }
552     }
553 #endif
554 #endif
555 
556 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
557     if( todo.camellia )
558     {
559         int keysize;
560         mbedtls_camellia_context camellia;
561         mbedtls_camellia_init( &camellia );
562         for( keysize = 128; keysize <= 256; keysize += 64 )
563         {
564             mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
565 
566             memset( buf, 0, sizeof( buf ) );
567             memset( tmp, 0, sizeof( tmp ) );
568             mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
569 
570             TIME_AND_TSC( title,
571                     mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
572                         BUFSIZE, tmp, buf, buf ) );
573         }
574         mbedtls_camellia_free( &camellia );
575     }
576 #endif
577 
578 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
579     if( todo.blowfish )
580     {
581         int keysize;
582         mbedtls_blowfish_context blowfish;
583         mbedtls_blowfish_init( &blowfish );
584 
585         for( keysize = 128; keysize <= 256; keysize += 64 )
586         {
587             mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
588 
589             memset( buf, 0, sizeof( buf ) );
590             memset( tmp, 0, sizeof( tmp ) );
591             mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
592 
593             TIME_AND_TSC( title,
594                     mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
595                         tmp, buf, buf ) );
596         }
597 
598         mbedtls_blowfish_free( &blowfish );
599     }
600 #endif
601 
602 #if defined(MBEDTLS_HAVEGE_C)
603     if( todo.havege )
604     {
605         mbedtls_havege_state hs;
606         mbedtls_havege_init( &hs );
607         TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
608         mbedtls_havege_free( &hs );
609     }
610 #endif
611 
612 #if defined(MBEDTLS_CTR_DRBG_C)
613     if( todo.ctr_drbg )
614     {
615         mbedtls_ctr_drbg_context ctr_drbg;
616 
617         mbedtls_ctr_drbg_init( &ctr_drbg );
618 
619         if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
620             mbedtls_exit(1);
621         TIME_AND_TSC( "CTR_DRBG (NOPR)",
622                 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
623                 mbedtls_exit(1) );
624 
625         if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
626             mbedtls_exit(1);
627         mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
628         TIME_AND_TSC( "CTR_DRBG (PR)",
629                 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
630                 mbedtls_exit(1) );
631         mbedtls_ctr_drbg_free( &ctr_drbg );
632     }
633 #endif
634 
635 #if defined(MBEDTLS_HMAC_DRBG_C)
636     if( todo.hmac_drbg )
637     {
638         mbedtls_hmac_drbg_context hmac_drbg;
639         const mbedtls_md_info_t *md_info;
640 
641         mbedtls_hmac_drbg_init( &hmac_drbg );
642 
643 #if defined(MBEDTLS_SHA1_C)
644         if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
645             mbedtls_exit(1);
646 
647         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
648             mbedtls_exit(1);
649         TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
650                 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
651                 mbedtls_exit(1) );
652         mbedtls_hmac_drbg_free( &hmac_drbg );
653 
654         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
655             mbedtls_exit(1);
656         mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
657                                              MBEDTLS_HMAC_DRBG_PR_ON );
658         TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
659                 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
660                 mbedtls_exit(1) );
661         mbedtls_hmac_drbg_free( &hmac_drbg );
662 #endif
663 
664 #if defined(MBEDTLS_SHA256_C)
665         if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
666             mbedtls_exit(1);
667 
668         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
669             mbedtls_exit(1);
670         TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
671                 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
672                 mbedtls_exit(1) );
673         mbedtls_hmac_drbg_free( &hmac_drbg );
674 
675         if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
676             mbedtls_exit(1);
677         mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
678                                              MBEDTLS_HMAC_DRBG_PR_ON );
679         TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
680                 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
681                 mbedtls_exit(1) );
682         mbedtls_hmac_drbg_free( &hmac_drbg );
683 #endif
684     }
685 #endif
686 
687 #if defined(MBEDTLS_RSA_C) && \
688     defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
689     if( todo.rsa )
690     {
691         mbedtls_pk_context pk;
692         mbedtls_rsa_context *rsa;
693         const char *rsa_keys[] = { RSA_PRIVATE_KEY_2048, RSA_PRIVATE_KEY_4096 };
694         size_t i;
695 
696         for( i = 0; i < sizeof( rsa_keys ) / sizeof( rsa_keys[0] ); i++ )
697         {
698             mbedtls_pk_init( &pk );
699             mbedtls_pk_parse_key( &pk, (const unsigned char *) rsa_keys[i],
700                                                        strlen( rsa_keys[i] ) + 1, NULL, 0 );
701             rsa = mbedtls_pk_rsa( pk );
702 
703             mbedtls_snprintf( title, sizeof( title ), "RSA-%d", mbedtls_pk_get_bitlen( &pk ) );
704 
705             TIME_PUBLIC( title, " public",
706                     buf[0] = 0;
707                     ret = mbedtls_rsa_public( rsa, buf, buf ) );
708 
709             TIME_PUBLIC( title, "private",
710                     buf[0] = 0;
711                     ret = mbedtls_rsa_private( rsa, myrand, NULL, buf, buf ) );
712 
713             mbedtls_pk_free( &pk );
714         }
715     }
716 #endif
717 
718 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
719     if( todo.dhm )
720     {
721         int dhm_sizes[] = { 2048, 3072 };
722         const char *dhm_P[] = {
723             MBEDTLS_DHM_RFC3526_MODP_2048_P,
724             MBEDTLS_DHM_RFC3526_MODP_3072_P,
725         };
726         const char *dhm_G[] = {
727             MBEDTLS_DHM_RFC3526_MODP_2048_G,
728             MBEDTLS_DHM_RFC3526_MODP_3072_G,
729         };
730 
731         mbedtls_dhm_context dhm;
732         size_t olen;
733         for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
734         {
735             mbedtls_dhm_init( &dhm );
736 
737             if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
738                 mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
739             {
740                 mbedtls_exit( 1 );
741             }
742 
743             dhm.len = mbedtls_mpi_size( &dhm.P );
744             mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
745             if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
746                 mbedtls_exit( 1 );
747 
748             mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
749             TIME_PUBLIC( title, "handshake",
750                     ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
751                                             myrand, NULL );
752                     ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
753 
754             mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
755             TIME_PUBLIC( title, "handshake",
756                     ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
757 
758             mbedtls_dhm_free( &dhm );
759         }
760     }
761 #endif
762 
763 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
764     if( todo.ecdsa )
765     {
766         mbedtls_ecdsa_context ecdsa;
767         const mbedtls_ecp_curve_info *curve_info;
768         size_t sig_len;
769 
770         memset( buf, 0x2A, sizeof( buf ) );
771 
772         for( curve_info = mbedtls_ecp_curve_list();
773              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
774              curve_info++ )
775         {
776             mbedtls_ecdsa_init( &ecdsa );
777 
778             if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
779                 mbedtls_exit( 1 );
780             ecp_clear_precomputed( &ecdsa.grp );
781 
782             mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
783                                               curve_info->name );
784             TIME_PUBLIC( title, "sign",
785                     ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
786                                                 tmp, &sig_len, myrand, NULL ) );
787 
788             mbedtls_ecdsa_free( &ecdsa );
789         }
790 
791         for( curve_info = mbedtls_ecp_curve_list();
792              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
793              curve_info++ )
794         {
795             mbedtls_ecdsa_init( &ecdsa );
796 
797             if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
798                 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
799                                                tmp, &sig_len, myrand, NULL ) != 0 )
800             {
801                 mbedtls_exit( 1 );
802             }
803             ecp_clear_precomputed( &ecdsa.grp );
804 
805             mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
806                                               curve_info->name );
807             TIME_PUBLIC( title, "verify",
808                     ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
809                                                 tmp, sig_len ) );
810 
811             mbedtls_ecdsa_free( &ecdsa );
812         }
813     }
814 #endif
815 
816 #if defined(MBEDTLS_ECDH_C)
817     if( todo.ecdh )
818     {
819         mbedtls_ecdh_context ecdh;
820 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
821         mbedtls_mpi z;
822 #endif
823         const mbedtls_ecp_curve_info *curve_info;
824         size_t olen;
825 
826         for( curve_info = mbedtls_ecp_curve_list();
827              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
828              curve_info++ )
829         {
830             mbedtls_ecdh_init( &ecdh );
831 
832             if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
833                 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
834                                   myrand, NULL ) != 0 ||
835                 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
836             {
837                 mbedtls_exit( 1 );
838             }
839             ecp_clear_precomputed( &ecdh.grp );
840 
841             mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
842                                               curve_info->name );
843             TIME_PUBLIC( title, "handshake",
844                     ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
845                                              myrand, NULL );
846                     ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
847                                              myrand, NULL ) );
848             mbedtls_ecdh_free( &ecdh );
849         }
850 
851         /* Curve25519 needs to be handled separately */
852 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
853         mbedtls_ecdh_init( &ecdh );
854         mbedtls_mpi_init( &z );
855 
856         if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
857             mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
858         {
859             mbedtls_exit( 1 );
860         }
861 
862         TIME_PUBLIC(  "ECDHE-Curve25519", "handshake",
863                 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
864                                         myrand, NULL );
865                 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
866                                             myrand, NULL ) );
867 
868         mbedtls_ecdh_free( &ecdh );
869         mbedtls_mpi_free( &z );
870 #endif
871 
872         for( curve_info = mbedtls_ecp_curve_list();
873              curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
874              curve_info++ )
875         {
876             mbedtls_ecdh_init( &ecdh );
877 
878             if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
879                 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
880                                   myrand, NULL ) != 0 ||
881                 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
882                 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
883                                   myrand, NULL ) != 0 )
884             {
885                 mbedtls_exit( 1 );
886             }
887             ecp_clear_precomputed( &ecdh.grp );
888 
889             mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
890                                               curve_info->name );
891             TIME_PUBLIC( title, "handshake",
892                     ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
893                                              myrand, NULL ) );
894             mbedtls_ecdh_free( &ecdh );
895         }
896 
897         /* Curve25519 needs to be handled separately */
898 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
899         mbedtls_ecdh_init( &ecdh );
900         mbedtls_mpi_init( &z );
901 
902         if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
903             mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
904                              myrand, NULL ) != 0 ||
905             mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
906         {
907             mbedtls_exit( 1 );
908         }
909 
910         TIME_PUBLIC(  "ECDH-Curve25519", "handshake",
911                 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
912                                             myrand, NULL ) );
913 
914         mbedtls_ecdh_free( &ecdh );
915         mbedtls_mpi_free( &z );
916 #endif
917     }
918 #endif
919 
920     mbedtls_printf( "\r\n" );
921 
922 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
923     mbedtls_memory_buffer_alloc_free();
924 #endif
925 
926 #if defined(_WIN32)
927     mbedtls_printf( "  Press Enter to exit this program.\r\n" );
928     fflush( stdout ); getchar();
929 #endif
930 
931     return( 0 );
932 }
933 
934 #include "mbed-drivers/test_env.h"
935 #include "minar/minar.h"
936 
run()937 static void run() {
938     MBED_HOSTTEST_TIMEOUT(150);
939     MBED_HOSTTEST_SELECT(default);
940     MBED_HOSTTEST_DESCRIPTION(mbed TLS benchmark program);
941     MBED_HOSTTEST_START("MBEDTLS_BENCHMARK");
942     MBED_HOSTTEST_RESULT(benchmark(0, NULL) == 0);
943 }
944 
app_start(int,char * [])945 void app_start(int, char*[]) {
946     /* Use 115200 bps for consistency with other examples */
947     get_stdio_serial().baud(115200);
948     minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind());
949 }
950 
951 #endif /* TARGET_LIKE_MBED */
952