main.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. #if !defined(TARGET_LIKE_MBED)
  22. #include <stdio.h>
  23. int main() {
  24. printf("this version of this program only works on mbed OS\n");
  25. return 0;
  26. }
  27. #else
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "mbedtls/config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_PLATFORM_C)
  34. #include "mbedtls/platform.h"
  35. #else
  36. #include <stdio.h>
  37. #define mbedtls_exit exit
  38. #define mbedtls_printf printf
  39. #define mbedtls_snprintf snprintf
  40. #define mbedtls_free free
  41. #endif
  42. #include <string.h>
  43. #include "mbedtls/md4.h"
  44. #include "mbedtls/md5.h"
  45. #include "mbedtls/ripemd160.h"
  46. #include "mbedtls/sha1.h"
  47. #include "mbedtls/sha256.h"
  48. #include "mbedtls/sha512.h"
  49. #include "mbedtls/arc4.h"
  50. #include "mbedtls/des.h"
  51. #include "mbedtls/aes.h"
  52. #include "mbedtls/blowfish.h"
  53. #include "mbedtls/camellia.h"
  54. #include "mbedtls/gcm.h"
  55. #include "mbedtls/ccm.h"
  56. #include "mbedtls/havege.h"
  57. #include "mbedtls/ctr_drbg.h"
  58. #include "mbedtls/hmac_drbg.h"
  59. #include "mbedtls/rsa.h"
  60. #include "mbedtls/pk.h"
  61. #include "mbedtls/dhm.h"
  62. #include "mbedtls/ecdsa.h"
  63. #include "mbedtls/ecdh.h"
  64. #include "mbedtls/error.h"
  65. #include "mbed-drivers/mbed.h"
  66. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  67. #include "mbedtls/memory_buffer_alloc.h"
  68. #endif
  69. #define RSA_PRIVATE_KEY_2048 \
  70. "-----BEGIN RSA PRIVATE KEY-----\r\n" \
  71. "MIIEogIBAAKCAQEA2dwVr+IMGEtA2/MCP6fA5eb/6B18Bq6e7gw8brNPkm3E6LyR\r\n" \
  72. "4DnMJVxZmw3bPDKBDoKzfntkMESi/Yw5UopLtVfjGfWeQWPClqffLZBsZ60BRAsg\r\n" \
  73. "/g+ID5tgzxSuxzftypK59uexOVCAm7hCKZHGO3DbI7bLY27j7VAgEP7d/yuaz5Fx\r\n" \
  74. "Kl/vu7shqrBoz6ABJVJD3KC8nUiMRUCXRINmxbyUUjA4DnicZv6+xrGKr36r6M8h\r\n" \
  75. "VYLa5msKc8WzbnBWzpUsrpb4/r7ML+qp92gdSfVJ8/bLiU7h2C7faDA59uaqrFK9\r\n" \
  76. "xmDdx7FaWhGQs3LWW6w1UNgkPS0FDYUslpsnsQIDAQABAoIBAC7IJNwM5V3+IuJY\r\n" \
  77. "T35Nzo1PyloUosJokvY5KGz5Ejg2XBdCDu0gXCcVqqQyGIbXrYDpLhQV+RCoXHun\r\n" \
  78. "tdN0oQdC5SB47s/J1Uo2qCUHo0+sBd6PqTkFKsl3KxWssk9TQjvCwC412IefMs69\r\n" \
  79. "hW+ZvwCanmQP56LleApIr2oW4KLfW8Ry/QfZlua+dizctdN7+H1mWwgZQTY9T27J\r\n" \
  80. "6RtGRA5NVkKVPzIHVJfdpKoO7xGg1g06aEbPB/VmGvZaaFWWnaf7uRvFjLZecBLu\r\n" \
  81. "QSx2DA/GDjirlDYj99PJb7DtB4xRtKzsyw0o+xapC8w6OtIl/3xFt9moCu2jGrsx\r\n" \
  82. "vpjHdfECgYEA7fSACRseIs9gAIVX8wq6gayTpA47DHYWAD6IQfIj35SJ+AgsvbFF\r\n" \
  83. "4AmrwDhcJVPmDy1N4nLBfyGAMt/2CfiYkdkW6QFX/ULRMMBL/G7kWV8hYQDICB2g\r\n" \
  84. "xaMRN1lPCmFq6BkSWjwIYTnYDFBDWVm1GVT8TMtJoM8Erej9qC0PeFUCgYEA6mF3\r\n" \
  85. "bigO3t8f5sig+XepaftEUbkJMzo72TVRnIR2ycdR2ihelPQ+25g9dwV0ZA5XXhBS\r\n" \
  86. "DKOABWjMM739Mwmy9v26Dlmu9R01zHQktMvtEAyfz7lk2NF0aMuj8285OJUBf9bz\r\n" \
  87. "Cq3MjtMCD+4CZ6iaEqCdUKOuxfpx5cWVJV+qve0CgYBhD1YaYMFOGaBjFgDl1f51\r\n" \
  88. "Xltqk5NqZdBbkSYrIAWZ8RDF5y+4wFJsLAWuhk6vuyUgE66tK3nZzWRpXAkT0B8L\r\n" \
  89. "fq1lpXKqj1KcvBNCiEkEW1VWJ+dvyAYIF5eyJ++hoFLnETL3M32HivyhKSwPihPg\r\n" \
  90. "nVW8TT9fJJIYDe1JZ/fjcQKBgHJfv7UsrR0LSvkG3K8AOtbx+8PZhOjPuRbk0v+L\r\n" \
  91. "EKCkuIe5/XW4vtfQMeZb7hFJgk7vrepm+vkoy8VQKDf4urGW3W1VTHBmobM01hi4\r\n" \
  92. "DuYvEul+Mf0wMRtWjJolo4m+BO5KiW2jpFfqFm6JmfjVqOIAKOSKC6am8V/MDF0h\r\n" \
  93. "kyN9AoGAT9oOiEXMolbkDZw/QCaBiRoAGlGlNYUkJ+58U6OjIZLISw6aFv+Y2uE0\r\n" \
  94. "mEImItjuYZtSYKblWikp6ldPoKlt9bwEFe3c6IZ8kJ3+xyEyAGrvjXjEY7PzP6dp\r\n" \
  95. "Ajbjp9X9uocEBv9W/KsBLdQ7yizcL/toHwdBO4vQqmqTvAc5IIw=\r\n" \
  96. "-----END RSA PRIVATE KEY-----\r\n"
  97. #define RSA_PRIVATE_KEY_4096 \
  98. "-----BEGIN RSA PRIVATE KEY-----\r\n" \
  99. "MIIJKgIBAAKCAgEAmkdGjoIshJuOt2NO47qB3Z3yyvmLg2j351isItSNuFQU3qr+\r\n" \
  100. "jXHIeANf03yw/K0Zvos8RPd+CqLjoxAQL3QDH4bZAl88bIo29i+SANbNSrKQmc0k\r\n" \
  101. "pH+yzw3alDzO0GZaOPZjsbo6AwBrno5msi0vRuC2aY8vGLPsZWSyLai7tneS1j/o\r\n" \
  102. "vYW6XIo8Cj61j2Ypy9HhVUW/4Wc+zAT25D/x7jTpkqJLWWT+YzibNbOY48M5eJcB\r\n" \
  103. "6/sMyUIeI3/u/wXyMrooNyLiCpedkuHRA0m7u5cWPTUISTunSRlVFij/NHJjuU8e\r\n" \
  104. "wA3B29yfZFsUqDEnyc+OxniIueAixTomVszxAaVn8zFEbYhFMPqziiFp99u3jfeG\r\n" \
  105. "k1q9mmUi/uCfUC4e2IC5rqq1ZbKSduH7Ug/Vn2bGQahww0sZFRHDXFrnBcotcW+M\r\n" \
  106. "bnC290VBDnYgzmdYrIOxuPb2aUwJo4ZlbKh5uBB1PigMuyhLKibQ1a+V5ZJGdpP6\r\n" \
  107. "SE9PGIdgYWSmh2QEMuLE6v+wTO2LQ5JgqsvFfi3GIZvkn0s8jTS72Jq2uMkFkMer\r\n" \
  108. "UBjPDYaSPy5kpo103KerWs+cMPOJ/3FtZzI++7MoSUTkWVr1ySQFt5i1EIZ/0Thi\r\n" \
  109. "jut2jNe8a4AoA3TtC8Rkk/3AIIbg8MVNT4EnT+KHROTMu6gET1oJ3YfBRpUCAwEA\r\n" \
  110. "AQKCAgEAhuNSmT7PVZH8kfLOAuYKrY1vvm+4v0iDl048Eqfs0QESziyLK3gUYnnw\r\n" \
  111. "yqP2yrU+EQ8Dvvj0xq/sf6GHxTWVlXb9PcmutueRbmXhLcKg83J0Y0StiPXtjIL8\r\n" \
  112. "XSddW3Bh6fPi7n14Qy+W6KZwu9AtybanRlvePabyRSRpdOpWVQ7u30w5XZsSed6S\r\n" \
  113. "6BI0BBC68m2qqje1sInoqdCdXKtcB31TytUDNEHM+UuAyM8iGeGS2hCNqZlycHTS\r\n" \
  114. "jQ9KEsdMH3YLu0lQgRpWtxmg+VL6ROWwmAtKF12EwbDYZ+uoVl69OkQnCpv8pxKa\r\n" \
  115. "ec/4m6V+uEA1AOpaAMorHG3fH31IKWC/fTZstovgO/eG2XCtlbcCoWCQ7amFq16l\r\n" \
  116. "Gh1UKeBHxMXpDj4oDmIUGUvgzSNnEeSN/v76losWvWYQDjXR/LMDa/CNYsD8BmJR\r\n" \
  117. "PZidIjIXdVRlYOhA7ljtySQvp6RBujBfw3tsVMyZw2XzXFwM9O89b1xXC6+M5jf9\r\n" \
  118. "DXs/U7Fw+J9qq/YpByABcPCwWdttwdQFRbOxwxaSOKarIqS87TW1JuFcNJ59Ut6G\r\n" \
  119. "kMvAg6gC34U+0ktkG/AmI1hgjC+P7ErHCXBR2xARoGzcO/CMZF59S+Z2HFchpTSP\r\n" \
  120. "5T2o4mGy3VfHSBidQQrcZRukg8ZP8M1NF3bXjpY6QZpeLHc4oHECggEBAMjdgzzk\r\n" \
  121. "xp4mIYFxAEiXYt7tzuUXJk+0UpEJj5uboWLirUZqZmNUPyh6WDnzlREBH++Ms0LO\r\n" \
  122. "+AWSfaGPDoMb0NE2j3c4FRWAhe7Vn6lj7nLVpF2RdwRo88yGerZ4uwGMY8NUQCtn\r\n" \
  123. "zum3J7eCJ5DojiceRb6uMxTJ8xZmUC4W2f3J/lrR7wlYjyVnnHqH5HcemYUipWSw\r\n" \
  124. "sM0/cHp3lrz2VWrbAEu8HVpklvDQpdAgl7cjXt/JHYawY+p426IF/PzQSRROnzgy\r\n" \
  125. "4WI8FVYNV2tgu0TOFURbkkEvuj/duDKeooUIF0G0XHzha5oAX/j0iWiHbrOF6wHj\r\n" \
  126. "0xeajL9msKBnmD8CggEBAMSgLWmv7G31x4tndJCcXnX4AyVL7KpygAx/ZwCcyTR8\r\n" \
  127. "rY1rO07f/ta2noEra/xmEW/BW98qJFCHSU2nSLAQ5FpFSWyuQqrnffrMJnfWyvpr\r\n" \
  128. "ceQ0yQ/MiA6/JIOvGAjabcspzZijxzGp+Qk3eTT0yOXLSVOCH9B9XVHLodcy4PQM\r\n" \
  129. "KSCxy0vVHhVNl2SdPEwTXRmxk99Q/rw6IHVpQxBq1OhQt05nTKT+rZMD/grSK22e\r\n" \
  130. "my2F0DodAJwLo063Zv3RXQZhDYodMmjcp9Hqrtvj9P3HD7J3z6ACiV3SCi8cZumL\r\n" \
  131. "bSmnKCcd0bb45+aOWm31ieECJuIcJ9rOREEa/KDYTCsCggEBAMG5WkSVhLWsou37\r\n" \
  132. "dUGNuA63nq42SH3gtS0q4nU6gUkkw+dA4ST1cMByVrr1oRQ4WHup4I4TnQOKyF3T\r\n" \
  133. "4jQy1I+ipnVeAn+tZ/7zyzwMpEHeqNqRXA9FxbTBEoMAJ6QTqXgOvqDeSqIAQm7r\r\n" \
  134. "OYu5rrgtqyh/S8bGCwvUe4ooAfCSKx2ekYMbBVwW9MT8YS09tuS/iHJ3Mt2RTMLg\r\n" \
  135. "qeHvVmxrcXqZoFm44Ba7tN/pP0mi9HKyviZT4tmV3IYEbn3JyGGsfkUuVU9wEUfg\r\n" \
  136. "MCrgrVxrwfketAzooiHMjkVL2ASjzAJTmEvdAPETYXxzJD9LN0ovY3t8JfAC37IN\r\n" \
  137. "sVXS8/MCggEBALByOS59Y4Ktq1rLBQx8djwQyuneP0wZohUVAx7Gk7xZIfklQDyg\r\n" \
  138. "v/R4PrcVezstcPpDnykdjScCsGJR+uWc0v667I/ttP/e6utz5hVmmBGu965dPAzE\r\n" \
  139. "c1ggaSkOqFfRg/Nr2Qbf+fH0YPnHYSqHe/zSt0OMIvaaeXLcdKhEDSCUBRhE1HWB\r\n" \
  140. "kxR046WzgBeYzNQwycz9xwqsctJKGpeR9ute+5ANHPd3X9XtID0fqz8ctI5eZaSw\r\n" \
  141. "wApIW01ZQcAF8B+4WkkVuFXnpWW33yCOaRyPVOPHpnclr5WU1fS+3Q85QkW9rkej\r\n" \
  142. "97zlkl0QY9AHJqrXnoML1ywAK7ns+MVyNK8CggEAf62xcKZhOb1djeF72Ms+i/i/\r\n" \
  143. "WIAq4Q4YpsElgvJTHpNH2v9g4ngSTKe3ws3bGc502sWRlhcoTFMOW2rJNe/iqKkb\r\n" \
  144. "3cdeTkseDbpqozmJWz9dJWSVtXas2bZjzBEa//gQ7nHGVeQdqZJQ9rxPsoOAkfpi\r\n" \
  145. "qCFrmfUVUqC53e3XMt8+W+aSvKl+JZiB9ozkO9A6Q0vfQLKtjUMdQE3XaCFQT8DI\r\n" \
  146. "smaLBlBmeRaBpc02ENeC4ADlWosm1SwgxqMhuh2Alba/GrHOoPlVl4hDs9Fb5a6R\r\n" \
  147. "rmpXSt07GAxnG6j9jssA95E4rc1zO0CVKG5bvjVTxwi/sT0/VVX7VsJM4uTAQg==\r\n" \
  148. "-----END RSA PRIVATE KEY-----\r\n"
  149. #if defined _MSC_VER && !defined snprintf
  150. #define snprintf _snprintf
  151. #endif
  152. /*
  153. * For heap usage estimates, we need an estimate of the overhead per allocated
  154. * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
  155. * so use that as our baseline.
  156. */
  157. #define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
  158. /*
  159. * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
  160. */
  161. #define HEAP_SIZE (1u << 16) // 64k
  162. #define BUFSIZE 1024
  163. #define HEADER_FORMAT " %-24s : "
  164. #define TITLE_LEN 25
  165. #define OPTIONS \
  166. "md4, md5, ripemd160, sha1, sha256, sha512,\r\n" \
  167. "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\r\n" \
  168. "havege, ctr_drbg, hmac_drbg\r\n" \
  169. "rsa, dhm, ecdsa, ecdh.\r\n"
  170. #if defined(MBEDTLS_ERROR_C)
  171. #define PRINT_ERROR \
  172. mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
  173. mbedtls_printf( "FAILED: %s\r\n", tmp );
  174. #else
  175. #define PRINT_ERROR \
  176. mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
  177. #endif
  178. static unsigned long mbedtls_timing_hardclock( void )
  179. {
  180. static int dwt_started = 0;
  181. if( dwt_started == 0 )
  182. {
  183. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  184. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  185. }
  186. return( DWT->CYCCNT );
  187. }
  188. static volatile int alarmed;
  189. static void alarm() { alarmed = 1; }
  190. #define TIME_AND_TSC( TITLE, CODE ) \
  191. do { \
  192. unsigned long i, j, tsc; \
  193. Timeout t; \
  194. \
  195. mbedtls_printf( HEADER_FORMAT, TITLE ); \
  196. fflush( stdout ); \
  197. \
  198. for( i = 1, alarmed = 0, t.attach( alarm, 1.0 ); !alarmed; i++ ) \
  199. { \
  200. CODE; \
  201. } \
  202. \
  203. tsc = mbedtls_timing_hardclock(); \
  204. for( j = 0; j < 1024; j++ ) \
  205. { \
  206. CODE; \
  207. } \
  208. \
  209. mbedtls_printf( "%9lu Kb/s, %9lu cycles/byte\r\n", \
  210. i * BUFSIZE / 1024, \
  211. ( mbedtls_timing_hardclock() - tsc ) / ( j * BUFSIZE ) ); \
  212. } while( 0 )
  213. #if defined(MBEDTLS_ERROR_C)
  214. #define PRINT_ERROR \
  215. mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
  216. mbedtls_printf( "FAILED: %s\r\n", tmp );
  217. #else
  218. #define PRINT_ERROR \
  219. mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
  220. #endif
  221. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
  222. #define MEMORY_MEASURE_INIT \
  223. size_t max_used, max_blocks, max_bytes; \
  224. size_t prv_used, prv_blocks; \
  225. mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
  226. mbedtls_memory_buffer_alloc_max_reset( );
  227. #define MEMORY_MEASURE_PRINT( title_len ) \
  228. mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
  229. for( i = 12 - title_len; i != 0; i-- ) mbedtls_printf( " " ); \
  230. max_used -= prv_used; \
  231. max_blocks -= prv_blocks; \
  232. max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
  233. mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
  234. #else
  235. #define MEMORY_MEASURE_INIT
  236. #define MEMORY_MEASURE_PRINT( title_len )
  237. #endif
  238. #define TIME_PUBLIC( TITLE, TYPE, CODE ) \
  239. do { \
  240. unsigned long ms; \
  241. int ret = 0; \
  242. Timer t; \
  243. MEMORY_MEASURE_INIT; \
  244. \
  245. mbedtls_printf( HEADER_FORMAT, TITLE ); \
  246. fflush( stdout ); \
  247. \
  248. t.start(); \
  249. CODE; \
  250. t.stop(); \
  251. ms = t.read_ms(); \
  252. \
  253. if( ret != 0 ) \
  254. { \
  255. PRINT_ERROR; \
  256. } \
  257. else \
  258. { \
  259. mbedtls_printf( "%6lu ms/" TYPE, ms ); \
  260. MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
  261. mbedtls_printf( "\r\n" ); \
  262. } \
  263. } while( 0 )
  264. static int myrand( void *rng_state, unsigned char *output, size_t len )
  265. {
  266. size_t use_len;
  267. int rnd;
  268. if( rng_state != NULL )
  269. rng_state = NULL;
  270. while( len > 0 )
  271. {
  272. use_len = len;
  273. if( use_len > sizeof(int) )
  274. use_len = sizeof(int);
  275. rnd = rand();
  276. memcpy( output, &rnd, use_len );
  277. output += use_len;
  278. len -= use_len;
  279. }
  280. return( 0 );
  281. }
  282. /*
  283. * Clear some memory that was used to prepare the context
  284. */
  285. #if defined(MBEDTLS_ECP_C)
  286. void ecp_clear_precomputed( mbedtls_ecp_group *grp )
  287. {
  288. if( grp->T != NULL )
  289. {
  290. size_t i;
  291. for( i = 0; i < grp->T_size; i++ )
  292. mbedtls_ecp_point_free( &grp->T[i] );
  293. mbedtls_free( grp->T );
  294. }
  295. grp->T = NULL;
  296. grp->T_size = 0;
  297. }
  298. #else
  299. #define ecp_clear_precomputed( g )
  300. #endif
  301. unsigned char buf[BUFSIZE];
  302. typedef struct {
  303. char md4, md5, ripemd160, sha1, sha256, sha512,
  304. arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
  305. havege, ctr_drbg, hmac_drbg,
  306. rsa, dhm, ecdsa, ecdh;
  307. } todo_list;
  308. int benchmark( int argc, char *argv[] )
  309. {
  310. int i;
  311. unsigned char tmp[200];
  312. char title[TITLE_LEN];
  313. todo_list todo;
  314. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  315. unsigned char malloc_buf[HEAP_SIZE] = { 0 };
  316. #endif
  317. if( argc <= 1 )
  318. {
  319. memset( &todo, 1, sizeof( todo ) );
  320. }
  321. else
  322. {
  323. memset( &todo, 0, sizeof( todo ) );
  324. for( i = 1; i < argc; i++ )
  325. {
  326. if( strcmp( argv[i], "md4" ) == 0 )
  327. todo.md4 = 1;
  328. else if( strcmp( argv[i], "md5" ) == 0 )
  329. todo.md5 = 1;
  330. else if( strcmp( argv[i], "ripemd160" ) == 0 )
  331. todo.ripemd160 = 1;
  332. else if( strcmp( argv[i], "sha1" ) == 0 )
  333. todo.sha1 = 1;
  334. else if( strcmp( argv[i], "sha256" ) == 0 )
  335. todo.sha256 = 1;
  336. else if( strcmp( argv[i], "sha512" ) == 0 )
  337. todo.sha512 = 1;
  338. else if( strcmp( argv[i], "arc4" ) == 0 )
  339. todo.arc4 = 1;
  340. else if( strcmp( argv[i], "des3" ) == 0 )
  341. todo.des3 = 1;
  342. else if( strcmp( argv[i], "des" ) == 0 )
  343. todo.des = 1;
  344. else if( strcmp( argv[i], "aes_cbc" ) == 0 )
  345. todo.aes_cbc = 1;
  346. else if( strcmp( argv[i], "aes_gcm" ) == 0 )
  347. todo.aes_gcm = 1;
  348. else if( strcmp( argv[i], "aes_ccm" ) == 0 )
  349. todo.aes_ccm = 1;
  350. else if( strcmp( argv[i], "camellia" ) == 0 )
  351. todo.camellia = 1;
  352. else if( strcmp( argv[i], "blowfish" ) == 0 )
  353. todo.blowfish = 1;
  354. else if( strcmp( argv[i], "havege" ) == 0 )
  355. todo.havege = 1;
  356. else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
  357. todo.ctr_drbg = 1;
  358. else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
  359. todo.hmac_drbg = 1;
  360. else if( strcmp( argv[i], "rsa" ) == 0 )
  361. todo.rsa = 1;
  362. else if( strcmp( argv[i], "dhm" ) == 0 )
  363. todo.dhm = 1;
  364. else if( strcmp( argv[i], "ecdsa" ) == 0 )
  365. todo.ecdsa = 1;
  366. else if( strcmp( argv[i], "ecdh" ) == 0 )
  367. todo.ecdh = 1;
  368. else
  369. {
  370. mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] );
  371. mbedtls_printf( "Available options: " OPTIONS );
  372. }
  373. }
  374. }
  375. mbedtls_printf( "\r\n\r\n" );
  376. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  377. mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
  378. #endif
  379. memset( buf, 0xAA, sizeof( buf ) );
  380. memset( tmp, 0xBB, sizeof( tmp ) );
  381. #if defined(MBEDTLS_MD4_C)
  382. if( todo.md4 )
  383. TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) );
  384. #endif
  385. #if defined(MBEDTLS_MD5_C)
  386. if( todo.md5 )
  387. TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
  388. #endif
  389. #if defined(MBEDTLS_RIPEMD160_C)
  390. if( todo.ripemd160 )
  391. TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
  392. #endif
  393. #if defined(MBEDTLS_SHA1_C)
  394. if( todo.sha1 )
  395. TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
  396. #endif
  397. #if defined(MBEDTLS_SHA256_C)
  398. if( todo.sha256 )
  399. TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
  400. #endif
  401. #if defined(MBEDTLS_SHA512_C)
  402. if( todo.sha512 )
  403. TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
  404. #endif
  405. #if defined(MBEDTLS_ARC4_C)
  406. if( todo.arc4 )
  407. {
  408. mbedtls_arc4_context arc4;
  409. mbedtls_arc4_init( &arc4 );
  410. mbedtls_arc4_setup( &arc4, tmp, 32 );
  411. TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
  412. mbedtls_arc4_free( &arc4 );
  413. }
  414. #endif
  415. #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
  416. if( todo.des3 )
  417. {
  418. mbedtls_des3_context des3;
  419. mbedtls_des3_init( &des3 );
  420. mbedtls_des3_set3key_enc( &des3, tmp );
  421. TIME_AND_TSC( "3DES",
  422. mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
  423. mbedtls_des3_free( &des3 );
  424. }
  425. if( todo.des )
  426. {
  427. mbedtls_des_context des;
  428. mbedtls_des_init( &des );
  429. mbedtls_des_setkey_enc( &des, tmp );
  430. TIME_AND_TSC( "DES",
  431. mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
  432. mbedtls_des_free( &des );
  433. }
  434. #endif
  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. 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_GCM_C)
  455. if( todo.aes_gcm )
  456. {
  457. int keysize;
  458. mbedtls_gcm_context gcm;
  459. mbedtls_gcm_init( &gcm );
  460. for( keysize = 128; keysize <= 256; keysize += 64 )
  461. {
  462. mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
  463. memset( buf, 0, sizeof( buf ) );
  464. memset( tmp, 0, sizeof( tmp ) );
  465. mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
  466. TIME_AND_TSC( title,
  467. mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
  468. 12, NULL, 0, buf, buf, 16, tmp ) );
  469. mbedtls_gcm_free( &gcm );
  470. }
  471. }
  472. #endif
  473. #if defined(MBEDTLS_CCM_C)
  474. if( todo.aes_ccm )
  475. {
  476. int keysize;
  477. mbedtls_ccm_context ccm;
  478. mbedtls_ccm_init( &ccm );
  479. for( keysize = 128; keysize <= 256; keysize += 64 )
  480. {
  481. mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
  482. memset( buf, 0, sizeof( buf ) );
  483. memset( tmp, 0, sizeof( tmp ) );
  484. mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
  485. TIME_AND_TSC( title,
  486. mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
  487. 12, NULL, 0, buf, buf, tmp, 16 ) );
  488. mbedtls_ccm_free( &ccm );
  489. }
  490. }
  491. #endif
  492. #endif
  493. #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
  494. if( todo.camellia )
  495. {
  496. int keysize;
  497. mbedtls_camellia_context camellia;
  498. mbedtls_camellia_init( &camellia );
  499. for( keysize = 128; keysize <= 256; keysize += 64 )
  500. {
  501. mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
  502. memset( buf, 0, sizeof( buf ) );
  503. memset( tmp, 0, sizeof( tmp ) );
  504. mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
  505. TIME_AND_TSC( title,
  506. mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
  507. BUFSIZE, tmp, buf, buf ) );
  508. }
  509. mbedtls_camellia_free( &camellia );
  510. }
  511. #endif
  512. #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
  513. if( todo.blowfish )
  514. {
  515. int keysize;
  516. mbedtls_blowfish_context blowfish;
  517. mbedtls_blowfish_init( &blowfish );
  518. for( keysize = 128; keysize <= 256; keysize += 64 )
  519. {
  520. mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
  521. memset( buf, 0, sizeof( buf ) );
  522. memset( tmp, 0, sizeof( tmp ) );
  523. mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
  524. TIME_AND_TSC( title,
  525. mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
  526. tmp, buf, buf ) );
  527. }
  528. mbedtls_blowfish_free( &blowfish );
  529. }
  530. #endif
  531. #if defined(MBEDTLS_HAVEGE_C)
  532. if( todo.havege )
  533. {
  534. mbedtls_havege_state hs;
  535. mbedtls_havege_init( &hs );
  536. TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
  537. mbedtls_havege_free( &hs );
  538. }
  539. #endif
  540. #if defined(MBEDTLS_CTR_DRBG_C)
  541. if( todo.ctr_drbg )
  542. {
  543. mbedtls_ctr_drbg_context ctr_drbg;
  544. mbedtls_ctr_drbg_init( &ctr_drbg );
  545. if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
  546. mbedtls_exit(1);
  547. TIME_AND_TSC( "CTR_DRBG (NOPR)",
  548. if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
  549. mbedtls_exit(1) );
  550. if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
  551. mbedtls_exit(1);
  552. mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
  553. TIME_AND_TSC( "CTR_DRBG (PR)",
  554. if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
  555. mbedtls_exit(1) );
  556. mbedtls_ctr_drbg_free( &ctr_drbg );
  557. }
  558. #endif
  559. #if defined(MBEDTLS_HMAC_DRBG_C)
  560. if( todo.hmac_drbg )
  561. {
  562. mbedtls_hmac_drbg_context hmac_drbg;
  563. const mbedtls_md_info_t *md_info;
  564. mbedtls_hmac_drbg_init( &hmac_drbg );
  565. #if defined(MBEDTLS_SHA1_C)
  566. if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
  567. mbedtls_exit(1);
  568. if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
  569. mbedtls_exit(1);
  570. TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
  571. if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
  572. mbedtls_exit(1) );
  573. mbedtls_hmac_drbg_free( &hmac_drbg );
  574. if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
  575. mbedtls_exit(1);
  576. mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
  577. MBEDTLS_HMAC_DRBG_PR_ON );
  578. TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
  579. if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
  580. mbedtls_exit(1) );
  581. mbedtls_hmac_drbg_free( &hmac_drbg );
  582. #endif
  583. #if defined(MBEDTLS_SHA256_C)
  584. if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
  585. mbedtls_exit(1);
  586. if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
  587. mbedtls_exit(1);
  588. TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
  589. if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
  590. mbedtls_exit(1) );
  591. mbedtls_hmac_drbg_free( &hmac_drbg );
  592. if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
  593. mbedtls_exit(1);
  594. mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
  595. MBEDTLS_HMAC_DRBG_PR_ON );
  596. TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
  597. if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
  598. mbedtls_exit(1) );
  599. mbedtls_hmac_drbg_free( &hmac_drbg );
  600. #endif
  601. }
  602. #endif
  603. #if defined(MBEDTLS_RSA_C) && \
  604. defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
  605. if( todo.rsa )
  606. {
  607. mbedtls_pk_context pk;
  608. mbedtls_rsa_context *rsa;
  609. const char *rsa_keys[] = { RSA_PRIVATE_KEY_2048, RSA_PRIVATE_KEY_4096 };
  610. size_t i;
  611. for( i = 0; i < sizeof( rsa_keys ) / sizeof( rsa_keys[0] ); i++ )
  612. {
  613. mbedtls_pk_init( &pk );
  614. mbedtls_pk_parse_key( &pk, (const unsigned char *) rsa_keys[i],
  615. strlen( rsa_keys[i] ) + 1, NULL, 0 );
  616. rsa = mbedtls_pk_rsa( pk );
  617. mbedtls_snprintf( title, sizeof( title ), "RSA-%d", mbedtls_pk_get_bitlen( &pk ) );
  618. TIME_PUBLIC( title, " public",
  619. buf[0] = 0;
  620. ret = mbedtls_rsa_public( rsa, buf, buf ) );
  621. TIME_PUBLIC( title, "private",
  622. buf[0] = 0;
  623. ret = mbedtls_rsa_private( rsa, myrand, NULL, buf, buf ) );
  624. mbedtls_pk_free( &pk );
  625. }
  626. }
  627. #endif
  628. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
  629. if( todo.dhm )
  630. {
  631. int dhm_sizes[] = { 2048, 3072 };
  632. const char *dhm_P[] = {
  633. MBEDTLS_DHM_RFC3526_MODP_2048_P,
  634. MBEDTLS_DHM_RFC3526_MODP_3072_P,
  635. };
  636. const char *dhm_G[] = {
  637. MBEDTLS_DHM_RFC3526_MODP_2048_G,
  638. MBEDTLS_DHM_RFC3526_MODP_3072_G,
  639. };
  640. mbedtls_dhm_context dhm;
  641. size_t olen;
  642. for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
  643. {
  644. mbedtls_dhm_init( &dhm );
  645. if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
  646. mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
  647. {
  648. mbedtls_exit( 1 );
  649. }
  650. dhm.len = mbedtls_mpi_size( &dhm.P );
  651. mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
  652. if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
  653. mbedtls_exit( 1 );
  654. mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
  655. TIME_PUBLIC( title, "handshake",
  656. ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
  657. myrand, NULL );
  658. ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
  659. mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
  660. TIME_PUBLIC( title, "handshake",
  661. ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
  662. mbedtls_dhm_free( &dhm );
  663. }
  664. }
  665. #endif
  666. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
  667. if( todo.ecdsa )
  668. {
  669. mbedtls_ecdsa_context ecdsa;
  670. const mbedtls_ecp_curve_info *curve_info;
  671. size_t sig_len;
  672. memset( buf, 0x2A, sizeof( buf ) );
  673. for( curve_info = mbedtls_ecp_curve_list();
  674. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  675. curve_info++ )
  676. {
  677. mbedtls_ecdsa_init( &ecdsa );
  678. if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
  679. mbedtls_exit( 1 );
  680. ecp_clear_precomputed( &ecdsa.grp );
  681. mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
  682. curve_info->name );
  683. TIME_PUBLIC( title, "sign",
  684. ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
  685. tmp, &sig_len, myrand, NULL ) );
  686. mbedtls_ecdsa_free( &ecdsa );
  687. }
  688. for( curve_info = mbedtls_ecp_curve_list();
  689. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  690. curve_info++ )
  691. {
  692. mbedtls_ecdsa_init( &ecdsa );
  693. if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
  694. mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
  695. tmp, &sig_len, myrand, NULL ) != 0 )
  696. {
  697. mbedtls_exit( 1 );
  698. }
  699. ecp_clear_precomputed( &ecdsa.grp );
  700. mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
  701. curve_info->name );
  702. TIME_PUBLIC( title, "verify",
  703. ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
  704. tmp, sig_len ) );
  705. mbedtls_ecdsa_free( &ecdsa );
  706. }
  707. }
  708. #endif
  709. #if defined(MBEDTLS_ECDH_C)
  710. if( todo.ecdh )
  711. {
  712. mbedtls_ecdh_context ecdh;
  713. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  714. mbedtls_mpi z;
  715. #endif
  716. const mbedtls_ecp_curve_info *curve_info;
  717. size_t olen;
  718. for( curve_info = mbedtls_ecp_curve_list();
  719. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  720. curve_info++ )
  721. {
  722. mbedtls_ecdh_init( &ecdh );
  723. if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
  724. mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
  725. myrand, NULL ) != 0 ||
  726. mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
  727. {
  728. mbedtls_exit( 1 );
  729. }
  730. ecp_clear_precomputed( &ecdh.grp );
  731. mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
  732. curve_info->name );
  733. TIME_PUBLIC( title, "handshake",
  734. ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
  735. myrand, NULL );
  736. ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
  737. myrand, NULL ) );
  738. mbedtls_ecdh_free( &ecdh );
  739. }
  740. /* Curve25519 needs to be handled separately */
  741. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  742. mbedtls_ecdh_init( &ecdh );
  743. mbedtls_mpi_init( &z );
  744. if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
  745. mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
  746. {
  747. mbedtls_exit( 1 );
  748. }
  749. TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
  750. ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
  751. myrand, NULL );
  752. ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
  753. myrand, NULL ) );
  754. mbedtls_ecdh_free( &ecdh );
  755. mbedtls_mpi_free( &z );
  756. #endif
  757. for( curve_info = mbedtls_ecp_curve_list();
  758. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  759. curve_info++ )
  760. {
  761. mbedtls_ecdh_init( &ecdh );
  762. if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
  763. mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
  764. myrand, NULL ) != 0 ||
  765. mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
  766. mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
  767. myrand, NULL ) != 0 )
  768. {
  769. mbedtls_exit( 1 );
  770. }
  771. ecp_clear_precomputed( &ecdh.grp );
  772. mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
  773. curve_info->name );
  774. TIME_PUBLIC( title, "handshake",
  775. ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
  776. myrand, NULL ) );
  777. mbedtls_ecdh_free( &ecdh );
  778. }
  779. /* Curve25519 needs to be handled separately */
  780. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  781. mbedtls_ecdh_init( &ecdh );
  782. mbedtls_mpi_init( &z );
  783. if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
  784. mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
  785. myrand, NULL ) != 0 ||
  786. mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
  787. {
  788. mbedtls_exit( 1 );
  789. }
  790. TIME_PUBLIC( "ECDH-Curve25519", "handshake",
  791. ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
  792. myrand, NULL ) );
  793. mbedtls_ecdh_free( &ecdh );
  794. mbedtls_mpi_free( &z );
  795. #endif
  796. }
  797. #endif
  798. mbedtls_printf( "\r\n" );
  799. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  800. mbedtls_memory_buffer_alloc_free();
  801. #endif
  802. #if defined(_WIN32)
  803. mbedtls_printf( " Press Enter to exit this program.\r\n" );
  804. fflush( stdout ); getchar();
  805. #endif
  806. return( 0 );
  807. }
  808. #include "mbed-drivers/test_env.h"
  809. #include "minar/minar.h"
  810. static void run() {
  811. MBED_HOSTTEST_TIMEOUT(150);
  812. MBED_HOSTTEST_SELECT(default);
  813. MBED_HOSTTEST_DESCRIPTION(mbed TLS benchmark program);
  814. MBED_HOSTTEST_START("MBEDTLS_BENCHMARK");
  815. MBED_HOSTTEST_RESULT(benchmark(0, NULL) == 0);
  816. }
  817. void app_start(int, char*[]) {
  818. /* Use 115200 bps for consistency with other examples */
  819. get_stdio_serial().baud(115200);
  820. minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind());
  821. }
  822. #endif /* TARGET_LIKE_MBED */