benchmark.c 40 KB

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