tls_bench.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  1. /* tls_bench.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. Example gcc build statement
  23. gcc -lwolfssl -lpthread -o tls_bench tls_bench.c
  24. ./tls_bench
  25. Or
  26. #include <examples/benchmark/tls_bench.h>
  27. bench_tls(args);
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. #ifndef WOLFSSL_USER_SETTINGS
  33. #include <wolfssl/options.h>
  34. #endif
  35. #include <wolfssl/wolfcrypt/settings.h>
  36. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  37. #undef OPENSSL_COEXIST /* can't use this option with this example */
  38. #include <wolfssl/wolfcrypt/types.h>
  39. #include <wolfssl/wolfcrypt/wc_port.h>
  40. #include <wolfssl/ssl.h>
  41. #include <wolfssl/wolfcrypt/hash.h> /* WC_MAX_DIGEST_SIZE */
  42. #include <wolfssl/test.h>
  43. #include <wolfssl/wolfio.h>
  44. #include <examples/benchmark/tls_bench.h>
  45. /* force certificate test buffers to be included via headers */
  46. #undef USE_CERT_BUFFERS_2048
  47. #define USE_CERT_BUFFERS_2048
  48. #undef USE_CERT_BUFFERS_256
  49. #define USE_CERT_BUFFERS_256
  50. #include <wolfssl/certs_test.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #include <sys/time.h>
  56. #include <errno.h>
  57. /* For testing no threading support */
  58. #if 0
  59. #undef HAVE_PTHREAD
  60. #define SINGLE_THREADED
  61. #endif
  62. /* PTHREAD requires server and client enabled */
  63. #if defined(NO_WOLFSSL_CLIENT) || defined(NO_WOLFSSL_SERVER)
  64. #if !defined(SINGLE_THREADED)
  65. #ifdef __GNUC__ /* GCC compiler */
  66. #pragma message "PTHREAD requires server and client enabled."
  67. #elif defined(_MSC_VER) /* Microsoft Visual C++ compiler */
  68. #pragma message("PTHREAD requires server and client enabled.")
  69. #else
  70. #warning "PTHREAD requires server and client enabled."
  71. #endif
  72. #define SINGLE_THREADED
  73. #endif
  74. #endif
  75. /* Conversely, if both server and client are enabled, we must require pthreads */
  76. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) \
  77. && defined(SINGLE_THREADED)
  78. #error "threads must be enabled if building benchmark suite \
  79. to run both client and server. Please define HAVE_PTHREAD if your \
  80. platform supports it"
  81. #endif
  82. #if 0
  83. #define BENCH_USE_NONBLOCK
  84. #endif
  85. /* Defaults for configuration parameters */
  86. #define BENCH_DEFAULT_HOST "localhost"
  87. #define BENCH_DEFAULT_PORT 11112
  88. #define NUM_THREAD_PAIRS 1 /* Thread pairs of server/client */
  89. #ifndef BENCH_RUNTIME_SEC
  90. #ifdef BENCH_EMBEDDED
  91. #define BENCH_RUNTIME_SEC 15
  92. #else
  93. #define BENCH_RUNTIME_SEC 1
  94. #endif
  95. #endif
  96. /* TLS packet size */
  97. #ifndef TEST_PACKET_SIZE
  98. #ifdef BENCH_EMBEDDED
  99. #define TEST_PACKET_SIZE (2 * 1024)
  100. #else
  101. #define TEST_PACKET_SIZE (16 * 1024)
  102. #endif
  103. #endif
  104. /* Total bytes to benchmark per connection */
  105. #ifndef TEST_MAX_SIZE
  106. #ifdef BENCH_EMBEDDED
  107. #define TEST_MAX_SIZE (16 * 1024)
  108. #else
  109. #define TEST_MAX_SIZE (128 * 1024)
  110. #endif
  111. #endif
  112. #ifdef WOLFSSL_DTLS
  113. #ifdef BENCH_EMBEDDED
  114. /* WOLFSSL_MAX_MTU in internal.h */
  115. #define TEST_DTLS_PACKET_SIZE (1500)
  116. #else
  117. /* MAX_UDP_SIZE in interna.h */
  118. #define TEST_DTLS_PACKET_SIZE (8092)
  119. #endif
  120. #endif
  121. /* In memory transfer buffer maximum size */
  122. /* Must be large enough to handle max TLS packet size plus max TLS header MAX_MSG_EXTRA */
  123. #define MEM_BUFFER_SZ (TEST_PACKET_SIZE + 38 + WC_MAX_DIGEST_SIZE)
  124. #define SHOW_VERBOSE 0 /* Default output is tab delimited format */
  125. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  126. !defined(WOLFCRYPT_ONLY) && defined(USE_WOLFSSL_IO)
  127. /* shutdown message - nice signal to server, we are done */
  128. static const char* kShutdown = "shutdown";
  129. #ifndef NO_WOLFSSL_CLIENT
  130. PEDANTIC_EXTENSION static const char* kTestStr =
  131. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  132. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  133. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  134. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  135. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  136. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  137. "\n"
  138. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  139. "four loko you probably haven't heard of them high life. Messenger bag\n"
  140. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  141. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  142. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  143. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  144. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  145. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  146. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  147. "four loko whatever street art yr farm-to-table.\n"
  148. "\n"
  149. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  150. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  151. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  152. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  153. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  154. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  155. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  156. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  157. "locavore.\n"
  158. "\n"
  159. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  160. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  161. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  162. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  163. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  164. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  165. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  166. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  167. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  168. "neutra PBR selvage.\n"
  169. "\n"
  170. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  171. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  172. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  173. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  174. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  175. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  176. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  177. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  178. "mollit magna, sriracha sartorial helvetica.\n"
  179. "\n"
  180. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  181. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  182. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  183. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  184. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  185. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  186. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  187. "\n"
  188. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  189. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  190. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  191. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  192. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  193. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  194. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  195. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  196. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  197. "\n"
  198. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  199. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  200. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  201. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  202. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  203. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  204. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  205. "bag dolor terry richardson sapiente.\n";
  206. #endif
  207. #if !defined(NO_DH)
  208. #define MIN_DHKEY_BITS 1024
  209. #if !defined(NO_WOLFSSL_SERVER)
  210. /* dh2048 p */
  211. static const unsigned char dhp[] =
  212. {
  213. 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13, 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30,
  214. 0xd5, 0xf5, 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a, 0xc6, 0x25, 0x27,
  215. 0x1c, 0xe8, 0xd2, 0x96, 0x53, 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  216. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1, 0x8a, 0xc5, 0xbe, 0x72, 0x5c,
  217. 0xa7, 0xe7, 0x91, 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66, 0x48, 0xc7,
  218. 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9, 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46,
  219. 0x3e, 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d, 0x9f, 0x2b, 0x96, 0x17,
  220. 0x39, 0x6d, 0x30, 0x8d, 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75, 0xe6,
  221. 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa, 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda,
  222. 0x76, 0x93, 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c, 0xe5, 0xb7, 0xc8,
  223. 0x09, 0xae, 0x6f, 0x50, 0x35, 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  224. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76, 0xcc, 0x16, 0x30, 0xdb, 0x0c,
  225. 0xfc, 0xc5, 0x62, 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0, 0x36, 0xf6,
  226. 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40, 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3,
  227. 0x1a, 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4, 0x48, 0x4e, 0x22, 0x73,
  228. 0x6f, 0xc3, 0x5f, 0xd4, 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90, 0xab,
  229. 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58, 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf,
  230. 0x68, 0x1f, 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6, 0xb6, 0x06, 0x65,
  231. 0xf7, 0xa6, 0xdc, 0x00, 0x67, 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  232. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f, 0x93
  233. };
  234. /* dh2048 g */
  235. static const unsigned char dhg[] =
  236. {
  237. 0x02,
  238. };
  239. #endif /* !NO_WOLFSSL_SERVER */
  240. #endif /* !NO_DH */
  241. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  242. struct group_info {
  243. word16 group;
  244. const char *name;
  245. };
  246. static struct group_info groups[] = {
  247. { WOLFSSL_ECC_SECP160K1, "ECC_SECP160K1" },
  248. { WOLFSSL_ECC_SECP160R1, "ECC_SECP160R1" },
  249. { WOLFSSL_ECC_SECP160R2, "ECC_SECP160R2" },
  250. { WOLFSSL_ECC_SECP192K1, "ECC_SECP192K1" },
  251. { WOLFSSL_ECC_SECP192R1, "ECC_SECP192R1" },
  252. { WOLFSSL_ECC_SECP224K1, "ECC_SECP224K1" },
  253. { WOLFSSL_ECC_SECP224R1, "ECC_SECP224R1" },
  254. { WOLFSSL_ECC_SECP256K1, "ECC_SECP256K1" },
  255. { WOLFSSL_ECC_SECP256R1, "ECC_SECP256R1" },
  256. { WOLFSSL_ECC_SECP384R1, "ECC_SECP384R1" },
  257. { WOLFSSL_ECC_SECP521R1, "ECC_SECP521R1" },
  258. { WOLFSSL_ECC_BRAINPOOLP256R1, "ECC_BRAINPOOLP256R1" },
  259. { WOLFSSL_ECC_BRAINPOOLP384R1, "ECC_BRAINPOOLP384R1" },
  260. { WOLFSSL_ECC_BRAINPOOLP512R1, "ECC_BRAINPOOLP512R1" },
  261. { WOLFSSL_ECC_X25519, "ECC_X25519" },
  262. { WOLFSSL_ECC_X448, "ECC_X448" },
  263. { WOLFSSL_FFDHE_2048, "FFDHE_2048" },
  264. { WOLFSSL_FFDHE_3072, "FFDHE_3072" },
  265. { WOLFSSL_FFDHE_4096, "FFDHE_4096" },
  266. { WOLFSSL_FFDHE_6144, "FFDHE_6144" },
  267. { WOLFSSL_FFDHE_8192, "FFDHE_8192" },
  268. #ifdef HAVE_PQC
  269. { WOLFSSL_KYBER_LEVEL1, "KYBER_LEVEL1" },
  270. { WOLFSSL_KYBER_LEVEL3, "KYBER_LEVEL3" },
  271. { WOLFSSL_KYBER_LEVEL5, "KYBER_LEVEL5" },
  272. { WOLFSSL_P256_KYBER_LEVEL1, "P256_KYBER_LEVEL1" },
  273. { WOLFSSL_P384_KYBER_LEVEL3, "P384_KYBER_LEVEL3" },
  274. { WOLFSSL_P521_KYBER_LEVEL5, "P521_KYBER_LEVEL5" },
  275. #endif
  276. { 0, NULL }
  277. };
  278. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  279. #ifndef SINGLE_THREADED
  280. typedef struct {
  281. unsigned char buf[MEM_BUFFER_SZ];
  282. int write_bytes;
  283. int write_idx;
  284. int read_bytes;
  285. int read_idx;
  286. COND_TYPE cond;
  287. int done;
  288. } memBuf_t;
  289. #endif
  290. typedef struct {
  291. double connTime;
  292. double rxTime;
  293. double txTime;
  294. int connCount;
  295. int rxTotal;
  296. int txTotal;
  297. } stats_t;
  298. typedef struct {
  299. int shutdown;
  300. int sockFd;
  301. int ret;
  302. } side_t;
  303. typedef struct {
  304. const char* cipher;
  305. word16 group;
  306. const char* host;
  307. word32 port;
  308. int packetSize; /* The data payload size in the packet */
  309. int maxSize;
  310. int runTimeSec;
  311. int showPeerInfo;
  312. int showVerbose;
  313. #ifndef NO_WOLFSSL_SERVER
  314. int listenFd;
  315. #endif
  316. #ifdef WOLFSSL_DTLS
  317. int doDTLS;
  318. struct sockaddr_in serverAddr;
  319. struct sockaddr_in clientAddr;
  320. #ifndef SINGLE_THREADED
  321. int serverReady;
  322. int clientOrserverOnly;
  323. wolfSSL_Mutex dtls_mutex;
  324. COND_TYPE dtls_cond;
  325. #endif
  326. #endif
  327. side_t client;
  328. side_t server;
  329. #ifndef SINGLE_THREADED
  330. int useLocalMem;
  331. /* client messages to server in memory */
  332. memBuf_t to_server;
  333. /* server messages to client in memory */
  334. memBuf_t to_client;
  335. /* Indicates that the server is ready for connection */
  336. int serverListening;
  337. #endif
  338. /* server */
  339. stats_t server_stats;
  340. /* client */
  341. stats_t client_stats;
  342. } info_t;
  343. /* Global vars for argument parsing */
  344. int myoptind = 0;
  345. char* myoptarg = NULL;
  346. #ifdef WOLFSSL_DTLS
  347. int DoneHandShake = 0;
  348. #endif
  349. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
  350. static int run_all_CAST(void)
  351. {
  352. int ret = 0;
  353. int cast_idx = 0;
  354. for (cast_idx = 0; cast_idx < FIPS_CAST_COUNT; cast_idx++) {
  355. if ((ret = wc_RunCast_fips(cast_idx)) != 0) {
  356. #ifdef NO_ERROR_STRINGS
  357. fprintf(stderr,
  358. "ERROR: FIPS CAST failed with return code: %d\n", ret);
  359. #else
  360. fprintf(stderr,
  361. "ERROR: FIPS CAST failed for algorithm: %s\n",
  362. wc_GetErrorString(ret));
  363. #endif
  364. return ret;
  365. }
  366. }
  367. return ret;
  368. }
  369. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
  370. static double gettime_secs(int reset)
  371. {
  372. struct timeval tv;
  373. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  374. (void)reset;
  375. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  376. }
  377. #ifndef SINGLE_THREADED
  378. /* server send callback */
  379. static int ServerMemSend(info_t* info, char* buf, int sz)
  380. {
  381. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
  382. #ifndef BENCH_USE_NONBLOCK
  383. /* check for overflow */
  384. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  385. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  386. fprintf(stderr, "ServerMemSend overflow\n");
  387. return -1;
  388. }
  389. #else
  390. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  391. sz = MEM_BUFFER_SZ - info->to_client.write_idx;
  392. }
  393. #endif
  394. XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, (size_t)sz);
  395. info->to_client.write_idx += sz;
  396. info->to_client.write_bytes += sz;
  397. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
  398. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  399. #ifdef BENCH_USE_NONBLOCK
  400. if (sz == 0) {
  401. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  402. }
  403. #endif
  404. return sz;
  405. }
  406. /* server recv callback */
  407. static int ServerMemRecv(info_t* info, char* buf, int sz)
  408. {
  409. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
  410. #ifndef BENCH_USE_NONBLOCK
  411. while (info->to_server.write_idx - info->to_server.read_idx < sz &&
  412. !info->to_client.done) {
  413. THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond));
  414. }
  415. #else
  416. if (info->to_server.write_idx - info->to_server.read_idx < sz) {
  417. sz = info->to_server.write_idx - info->to_server.read_idx;
  418. }
  419. #endif
  420. XMEMCPY(buf, &info->to_server.buf[info->to_server.read_idx], (size_t)sz);
  421. info->to_server.read_idx += sz;
  422. info->to_server.read_bytes += sz;
  423. /* if the rx has caught up with pending then reset buffer positions */
  424. if (info->to_server.read_bytes == info->to_server.write_bytes) {
  425. info->to_server.read_bytes = info->to_server.read_idx = 0;
  426. info->to_server.write_bytes = info->to_server.write_idx = 0;
  427. }
  428. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  429. if (info->to_client.done != 0) {
  430. return -1;
  431. }
  432. #ifdef BENCH_USE_NONBLOCK
  433. if (sz == 0) {
  434. return WOLFSSL_CBIO_ERR_WANT_READ;
  435. }
  436. #endif
  437. return sz;
  438. }
  439. /* client send callback */
  440. static int ClientMemSend(info_t* info, char* buf, int sz)
  441. {
  442. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
  443. #ifndef BENCH_USE_NONBLOCK
  444. /* check for overflow */
  445. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  446. fprintf(stderr, "ClientMemSend overflow %d %d %d\n",
  447. info->to_server.write_idx, sz, MEM_BUFFER_SZ);
  448. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  449. return -1;
  450. }
  451. #else
  452. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  453. sz = MEM_BUFFER_SZ - info->to_server.write_idx;
  454. }
  455. #endif
  456. XMEMCPY(&info->to_server.buf[info->to_server.write_idx], buf, (size_t)sz);
  457. info->to_server.write_idx += sz;
  458. info->to_server.write_bytes += sz;
  459. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
  460. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  461. #ifdef BENCH_USE_NONBLOCK
  462. if (sz == 0) {
  463. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  464. }
  465. #endif
  466. return sz;
  467. }
  468. /* client recv callback */
  469. static int ClientMemRecv(info_t* info, char* buf, int sz)
  470. {
  471. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
  472. #ifndef BENCH_USE_NONBLOCK
  473. while (info->to_client.write_idx - info->to_client.read_idx < sz &&
  474. !info->to_server.done) {
  475. THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond));
  476. }
  477. #else
  478. if (info->to_client.write_idx - info->to_client.read_idx < sz) {
  479. sz = info->to_client.write_idx - info->to_client.read_idx;
  480. }
  481. #endif
  482. XMEMCPY(buf, &info->to_client.buf[info->to_client.read_idx], (size_t)sz);
  483. info->to_client.read_idx += sz;
  484. info->to_client.read_bytes += sz;
  485. /* if the rx has caught up with pending then reset buffer positions */
  486. if (info->to_client.read_bytes == info->to_client.write_bytes) {
  487. info->to_client.read_bytes = info->to_client.read_idx = 0;
  488. info->to_client.write_bytes = info->to_client.write_idx = 0;
  489. }
  490. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  491. if (info->to_server.done != 0) {
  492. return -1;
  493. }
  494. #ifdef BENCH_USE_NONBLOCK
  495. if (sz == 0) {
  496. return WOLFSSL_CBIO_ERR_WANT_READ;
  497. }
  498. #endif
  499. return sz;
  500. }
  501. #endif /* !SINGLE_THREADED */
  502. static int SocketRecv(int sockFd, char* buf, int sz)
  503. {
  504. int recvd = (int)recv(sockFd, buf, (size_t)sz, 0);
  505. if (recvd == -1) {
  506. switch (errno) {
  507. #if EAGAIN != SOCKET_EWOULDBLOCK
  508. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  509. #endif
  510. case SOCKET_EWOULDBLOCK:
  511. return WOLFSSL_CBIO_ERR_WANT_READ;
  512. case SOCKET_ECONNRESET:
  513. return WOLFSSL_CBIO_ERR_CONN_RST;
  514. case SOCKET_EINTR:
  515. return WOLFSSL_CBIO_ERR_ISR;
  516. case SOCKET_ECONNREFUSED: /* DTLS case */
  517. return WOLFSSL_CBIO_ERR_WANT_READ;
  518. case SOCKET_ECONNABORTED:
  519. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  520. default:
  521. return WOLFSSL_CBIO_ERR_GENERAL;
  522. }
  523. }
  524. else if (recvd == 0) {
  525. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  526. }
  527. return recvd;
  528. }
  529. static int SocketSend(int sockFd, char* buf, int sz)
  530. {
  531. int sent = (int)send(sockFd, buf, (size_t)sz, 0);
  532. if (sent == -1) {
  533. switch (errno) {
  534. #if EAGAIN != SOCKET_EWOULDBLOCK
  535. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  536. #endif
  537. case SOCKET_EWOULDBLOCK:
  538. return WOLFSSL_CBIO_ERR_WANT_READ;
  539. case SOCKET_ECONNRESET:
  540. return WOLFSSL_CBIO_ERR_CONN_RST;
  541. case SOCKET_EINTR:
  542. return WOLFSSL_CBIO_ERR_ISR;
  543. case SOCKET_EPIPE:
  544. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  545. default:
  546. return WOLFSSL_CBIO_ERR_GENERAL;
  547. }
  548. }
  549. else if (sent == 0) {
  550. return 0;
  551. }
  552. return sent;
  553. }
  554. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  555. static int ReceiveFrom(WOLFSSL *ssl, int sd, char *buf, int sz)
  556. {
  557. int recvd;
  558. int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
  559. struct sockaddr peer;
  560. socklen_t peerSz = 0;
  561. if (DoneHandShake) {
  562. dtls_timeout = 0;
  563. }
  564. if (!wolfSSL_get_using_nonblock(ssl)) {
  565. struct timeval timeout;
  566. XMEMSET(&timeout, 0, sizeof(timeout));
  567. timeout.tv_sec = dtls_timeout;
  568. if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
  569. sizeof(timeout)) != 0) {
  570. fprintf(stderr, "setsockopt rcvtimeo failed\n");
  571. }
  572. }
  573. recvd = (int)recvfrom(sd, buf, (size_t)sz, 0, (SOCKADDR*)&peer, &peerSz);
  574. if (recvd < 0) {
  575. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  576. if (wolfSSL_dtls_get_using_nonblock(ssl)) {
  577. return WOLFSSL_CBIO_ERR_WANT_READ;
  578. }
  579. else {
  580. return WOLFSSL_CBIO_ERR_TIMEOUT;
  581. }
  582. }
  583. else if (errno == SOCKET_ECONNRESET) {
  584. return WOLFSSL_CBIO_ERR_CONN_RST;
  585. }
  586. else if (errno == SOCKET_EINTR) {
  587. return WOLFSSL_CBIO_ERR_ISR;
  588. }
  589. else if (errno == SOCKET_ECONNREFUSED) {
  590. return WOLFSSL_CBIO_ERR_WANT_READ;
  591. }
  592. else {
  593. return WOLFSSL_CBIO_ERR_GENERAL;
  594. }
  595. }
  596. else {
  597. if (recvd == 0) {
  598. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  599. }
  600. }
  601. return recvd;
  602. }
  603. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
  604. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  605. static int SendTo(int sd, char *buf, int sz, const struct sockaddr *peer,
  606. socklen_t peerSz)
  607. {
  608. int sent;
  609. sent = (int)sendto(sd, buf, (size_t)sz, 0, peer, peerSz);
  610. if (sent < 0) {
  611. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  612. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  613. }
  614. else if (errno == SOCKET_ECONNRESET) {
  615. return WOLFSSL_CBIO_ERR_CONN_RST;
  616. }
  617. else if (errno == SOCKET_EINTR) {
  618. return WOLFSSL_CBIO_ERR_ISR;
  619. }
  620. else if (errno == SOCKET_EPIPE) {
  621. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  622. }
  623. else {
  624. return WOLFSSL_CBIO_ERR_GENERAL;
  625. }
  626. }
  627. return sent;
  628. }
  629. static int myDoneHsCb(WOLFSSL* ssl, void* user_ctx)
  630. {
  631. (void) ssl;
  632. (void) user_ctx;
  633. DoneHandShake = 1;
  634. return 1;
  635. }
  636. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_CLIENT */
  637. #ifndef NO_WOLFSSL_SERVER
  638. static int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  639. {
  640. info_t* info = (info_t*)ctx;
  641. (void)ssl;
  642. #ifndef SINGLE_THREADED
  643. if (info->useLocalMem) {
  644. return ServerMemSend(info, buf, sz);
  645. }
  646. #endif
  647. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  648. if (info->doDTLS) {
  649. return SendTo(info->server.sockFd, buf, sz,
  650. (const struct sockaddr*)&info->clientAddr, sizeof(info->clientAddr));
  651. }
  652. else
  653. #endif
  654. {
  655. return SocketSend(info->server.sockFd, buf, sz);
  656. }
  657. }
  658. static int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  659. {
  660. info_t* info = (info_t*)ctx;
  661. (void)ssl;
  662. #ifndef SINGLE_THREADED
  663. if (info->useLocalMem) {
  664. return ServerMemRecv(info, buf, sz);
  665. }
  666. #endif
  667. #ifdef WOLFSSL_DTLS
  668. if (info->doDTLS) {
  669. return ReceiveFrom(ssl, info->server.sockFd, buf, sz);
  670. }
  671. else
  672. #endif
  673. {
  674. return SocketRecv(info->server.sockFd, buf, sz);
  675. }
  676. }
  677. #endif /* !NO_WOLFSSL_SERVER */
  678. #ifndef NO_WOLFSSL_CLIENT
  679. static int ClientSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  680. {
  681. info_t* info = (info_t*)ctx;
  682. (void)ssl;
  683. #ifndef SINGLE_THREADED
  684. if (info->useLocalMem) {
  685. return ClientMemSend(info, buf, sz);
  686. }
  687. #endif
  688. #ifdef WOLFSSL_DTLS
  689. if (info->doDTLS) {
  690. return SendTo(info->client.sockFd, buf, sz,
  691. (const struct sockaddr*)&info->serverAddr, sizeof(info->serverAddr));
  692. }
  693. else
  694. #endif
  695. {
  696. return SocketSend(info->client.sockFd, buf, sz);
  697. }
  698. }
  699. static int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  700. {
  701. info_t* info = (info_t*)ctx;
  702. (void)ssl;
  703. #ifndef SINGLE_THREADED
  704. if (info->useLocalMem) {
  705. return ClientMemRecv(info, buf, sz);
  706. }
  707. #endif
  708. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  709. if (info->doDTLS) {
  710. return ReceiveFrom(ssl, info->client.sockFd, buf, sz);
  711. }
  712. else
  713. #endif
  714. {
  715. return SocketRecv(info->client.sockFd, buf, sz);
  716. }
  717. }
  718. #endif /* !NO_WOLFSSL_CLIENT */
  719. static void CloseAndCleanupSocket(int* sockFd)
  720. {
  721. if (*sockFd != -1) {
  722. close(*sockFd);
  723. *sockFd = -1;
  724. }
  725. #ifdef WOLFSSL_DTLS
  726. DoneHandShake = 0;
  727. #endif
  728. }
  729. #ifdef BENCH_USE_NONBLOCK
  730. static int SetSocketNonBlocking(int sockFd)
  731. {
  732. int flags = fcntl(sockFd, F_GETFL, 0);
  733. if (flags < 0) {
  734. fprintf(stderr, "fcntl get failed\n");
  735. return -1;
  736. }
  737. flags = fcntl(sockFd, F_SETFL, flags | O_NONBLOCK);
  738. if (flags < 0) {
  739. fprintf(stderr, "fcntl set failed\n");
  740. return -1;
  741. }
  742. return 0;
  743. }
  744. #endif
  745. #ifndef NO_WOLFSSL_CLIENT
  746. static int SetupSocketAndConnect(info_t* info, const char* host,
  747. word32 port)
  748. {
  749. struct sockaddr_in servAddr;
  750. struct hostent* entry;
  751. /* Setup server address */
  752. XMEMSET(&servAddr, 0, sizeof(servAddr));
  753. servAddr.sin_family = AF_INET;
  754. servAddr.sin_port = htons((uint16_t)port);
  755. /* Resolve host */
  756. entry = gethostbyname(host);
  757. if (entry) {
  758. XMEMCPY(&servAddr.sin_addr.s_addr, entry->h_addr_list[0],
  759. (size_t)entry->h_length);
  760. }
  761. else {
  762. servAddr.sin_addr.s_addr = inet_addr(host);
  763. }
  764. #ifdef WOLFSSL_DTLS
  765. if (info->doDTLS) {
  766. /* Create the SOCK_DGRAM socket type is implemented on the User
  767. * Datagram Protocol/Internet Protocol(UDP/IP protocol).*/
  768. if ((info->client.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  769. fprintf(stderr, "ERROR: failed to create the SOCK_DGRAM socket\n");
  770. return -1;
  771. }
  772. XMEMCPY(&info->serverAddr, &servAddr, sizeof(servAddr));
  773. }
  774. else
  775. #endif
  776. {
  777. /* Create a socket that uses an Internet IPv4 address,
  778. * Sets the socket to be stream based (TCP),
  779. * 0 means choose the default protocol. */
  780. if ((info->client.sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  781. fprintf(stderr, "ERROR: failed to create the socket\n");
  782. return -1;
  783. }
  784. /* Connect to the server */
  785. #ifndef SINGLE_THREADED
  786. while ((info->serverListening == 0) && (info->server.shutdown == 0)) {
  787. if (info->showVerbose) {
  788. fprintf(stderr, "Waiting for server to listen...\n");
  789. }
  790. XSLEEP_MS(1);
  791. }
  792. #endif
  793. if (info->server.shutdown == 1) {
  794. fprintf(stderr, "ERROR: server side has shutdown\n");
  795. return -1;
  796. }
  797. if (connect(info->client.sockFd, (struct sockaddr*)&servAddr,
  798. sizeof(servAddr)) == -1) {
  799. fprintf(stderr, "ERROR: failed to connect\n");
  800. return -1;
  801. }
  802. }
  803. #ifdef BENCH_USE_NONBLOCK
  804. if (SetSocketNonBlocking(info->client.sockFd) != 0) {
  805. return -1;
  806. }
  807. #endif
  808. if (info->showVerbose) {
  809. fprintf(stderr, "Connected to %s on port %d\n", host, port);
  810. }
  811. return 0;
  812. }
  813. static int bench_tls_client(info_t* info)
  814. {
  815. byte *writeBuf = NULL, *readBuf = NULL;
  816. double start, total = 0;
  817. int ret, readBufSz;
  818. WOLFSSL_CTX* cli_ctx = NULL;
  819. WOLFSSL* cli_ssl = NULL;
  820. int haveShownPeerInfo = 0;
  821. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  822. int total_sz;
  823. total = gettime_secs(0);
  824. /* set up client */
  825. #ifdef WOLFSSL_DTLS
  826. if (info->doDTLS) {
  827. if (tls13) {
  828. #ifdef WOLFSSL_DTLS13
  829. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_3_client_method());
  830. #endif
  831. }
  832. else {
  833. #ifndef WOLFSSL_NO_TLS12
  834. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  835. #endif
  836. }
  837. }
  838. else
  839. #endif
  840. {
  841. #ifdef WOLFSSL_TLS13
  842. if (tls13) {
  843. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  844. }
  845. else
  846. #endif
  847. {
  848. #if !defined(WOLFSSL_TLS13)
  849. cli_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  850. #elif !defined(WOLFSSL_NO_TLS12)
  851. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  852. #endif
  853. }
  854. }
  855. if (cli_ctx == NULL) {
  856. fprintf(stderr, "error creating ctx\n");
  857. ret = MEMORY_E; goto exit;
  858. }
  859. #ifndef NO_CERTS
  860. #ifdef HAVE_ECC
  861. if (XSTRSTR(info->cipher, "ECDSA")) {
  862. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_ecc_cert_der_256,
  863. sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1);
  864. }
  865. else
  866. #endif
  867. {
  868. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_cert_der_2048,
  869. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  870. }
  871. if (ret != WOLFSSL_SUCCESS) {
  872. fprintf(stderr, "error loading CA\n");
  873. goto exit;
  874. }
  875. #endif
  876. wolfSSL_CTX_SetIOSend(cli_ctx, ClientSend);
  877. wolfSSL_CTX_SetIORecv(cli_ctx, ClientRecv);
  878. /* set cipher suite */
  879. ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher);
  880. if (ret != WOLFSSL_SUCCESS) {
  881. fprintf(stderr, "error setting cipher suite\n");
  882. goto exit;
  883. }
  884. #ifndef NO_DH
  885. ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS);
  886. if (ret != WOLFSSL_SUCCESS) {
  887. fprintf(stderr, "Error setting minimum DH key size\n");
  888. goto exit;
  889. }
  890. #endif
  891. #ifndef NO_PSK
  892. wolfSSL_CTX_set_psk_client_callback(cli_ctx, my_psk_client_cb);
  893. #ifdef WOLFSSL_TLS13
  894. #if !defined(WOLFSSL_PSK_TLS13_CB) && !defined(WOLFSSL_PSK_ONE_ID)
  895. wolfSSL_CTX_set_psk_client_cs_callback(cli_ctx, my_psk_client_cs_cb);
  896. #else
  897. wolfSSL_CTX_set_psk_client_tls13_callback(cli_ctx, my_psk_client_tls13_cb);
  898. #endif
  899. #endif
  900. wolfSSL_CTX_set_psk_callback_ctx(cli_ctx, (void*)info->cipher);
  901. #endif /* !NO_PSK */
  902. /* Allocate and initialize a packet sized buffer */
  903. writeBuf = (unsigned char*)XMALLOC((size_t)info->packetSize, NULL,
  904. DYNAMIC_TYPE_TMP_BUFFER);
  905. if (writeBuf == NULL) {
  906. fprintf(stderr, "failed to allocate write memory\n");
  907. ret = MEMORY_E; goto exit;
  908. }
  909. /* Allocate read buffer */
  910. readBufSz = info->packetSize;
  911. readBuf = (unsigned char*)XMALLOC((size_t)readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  912. if (readBuf == NULL) {
  913. fprintf(stderr, "failed to allocate read memory\n");
  914. ret = MEMORY_E; goto exit;
  915. }
  916. /* BENCHMARK CONNECTIONS LOOP */
  917. while (!info->client.shutdown) {
  918. int writeSz = info->packetSize;
  919. #ifdef BENCH_USE_NONBLOCK
  920. int err;
  921. #endif
  922. #ifndef SINGLE_THREADED
  923. if (!info->useLocalMem)
  924. #endif
  925. {
  926. /* Setup socket and connection */
  927. ret = SetupSocketAndConnect(info, info->host, info->port);
  928. if (ret != 0) goto exit;
  929. }
  930. cli_ssl = wolfSSL_new(cli_ctx);
  931. if (cli_ssl == NULL) {
  932. fprintf(stderr, "error creating client object\n");
  933. goto exit;
  934. }
  935. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  936. if (info->group != 0) {
  937. ret = wolfSSL_UseKeyShare(cli_ssl, info->group);
  938. if (ret != WOLFSSL_SUCCESS) {
  939. fprintf(stderr, "error setting client key share.\n");
  940. goto exit;
  941. }
  942. }
  943. #endif
  944. #ifdef WOLFSSL_DTLS
  945. if (info->doDTLS) {
  946. ret = wolfSSL_dtls_set_peer(cli_ssl, &info->serverAddr,
  947. sizeof(info->serverAddr));
  948. if (ret != WOLFSSL_SUCCESS) {
  949. fprintf(stderr, "error setting dtls peer\n");
  950. goto exit;
  951. }
  952. ret = wolfSSL_SetHsDoneCb(cli_ssl, myDoneHsCb, NULL);
  953. if (ret != WOLFSSL_SUCCESS) {
  954. fprintf(stderr, "error handshake done callback\n");
  955. goto exit;
  956. }
  957. }
  958. #endif
  959. wolfSSL_SetIOReadCtx(cli_ssl, info);
  960. wolfSSL_SetIOWriteCtx(cli_ssl, info);
  961. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_DTLS)
  962. /* synchronize with server */
  963. if (info->doDTLS && !info->clientOrserverOnly) {
  964. THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
  965. if (info->serverReady != 1) {
  966. THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond));
  967. }
  968. /* for next loop */
  969. info->serverReady = 0;
  970. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
  971. }
  972. #endif
  973. /* perform connect */
  974. start = gettime_secs(1);
  975. #ifndef BENCH_USE_NONBLOCK
  976. ret = wolfSSL_connect(cli_ssl);
  977. #else
  978. do
  979. {
  980. ret = wolfSSL_connect(cli_ssl);
  981. err = wolfSSL_get_error(cli_ssl, ret);
  982. }
  983. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  984. #endif
  985. start = gettime_secs(0) - start;
  986. if (ret != WOLFSSL_SUCCESS) {
  987. fprintf(stderr, "error connecting client\n");
  988. ret = wolfSSL_get_error(cli_ssl, ret);
  989. goto exit;
  990. }
  991. info->client_stats.connTime += start;
  992. info->client_stats.connCount++;
  993. if ((info->showPeerInfo) && (!haveShownPeerInfo)) {
  994. haveShownPeerInfo = 1;
  995. showPeer(cli_ssl);
  996. }
  997. /* check for run time completion and issue shutdown */
  998. if (gettime_secs(0) - total >= info->runTimeSec) {
  999. info->client.shutdown = 1;
  1000. writeSz = (int)XSTRLEN(kShutdown) + 1;
  1001. XMEMCPY(writeBuf, kShutdown, (size_t)writeSz); /* include null term */
  1002. if (info->showVerbose) {
  1003. fprintf(stderr, "Sending shutdown\n");
  1004. }
  1005. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  1006. if (ret < 0) {
  1007. fprintf(stderr, "error on client write\n");
  1008. ret = wolfSSL_get_error(cli_ssl, ret);
  1009. goto exit;
  1010. }
  1011. }
  1012. else {
  1013. XMEMSET(writeBuf, 0, (size_t)info->packetSize);
  1014. XSTRNCPY((char*)writeBuf, kTestStr, (size_t)info->packetSize);
  1015. }
  1016. /* write / read echo loop */
  1017. ret = 0;
  1018. total_sz = 0;
  1019. while (ret == 0 && total_sz < info->maxSize && !info->client.shutdown) {
  1020. /* write test message to server */
  1021. start = gettime_secs(1);
  1022. #ifndef BENCH_USE_NONBLOCK
  1023. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  1024. #else
  1025. do {
  1026. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  1027. err = wolfSSL_get_error(cli_ssl, ret);
  1028. }
  1029. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1030. #endif
  1031. info->client_stats.txTime += gettime_secs(0) - start;
  1032. if (ret < 0) {
  1033. fprintf(stderr, "error on client write\n");
  1034. ret = wolfSSL_get_error(cli_ssl, ret);
  1035. goto exit;
  1036. }
  1037. info->client_stats.txTotal += ret;
  1038. total_sz += ret;
  1039. /* read echo of message from server */
  1040. XMEMSET(readBuf, 0, (size_t)readBufSz);
  1041. start = gettime_secs(1);
  1042. #ifndef BENCH_USE_NONBLOCK
  1043. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  1044. #else
  1045. do {
  1046. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  1047. err = wolfSSL_get_error(cli_ssl, ret);
  1048. }
  1049. while (err == WOLFSSL_ERROR_WANT_READ);
  1050. #endif
  1051. info->client_stats.rxTime += gettime_secs(0) - start;
  1052. if (ret < 0) {
  1053. fprintf(stderr, "error on client read\n");
  1054. ret = wolfSSL_get_error(cli_ssl, ret);
  1055. goto exit;
  1056. }
  1057. info->client_stats.rxTotal += ret;
  1058. ret = 0; /* reset return code */
  1059. /* validate echo */
  1060. if (XMEMCMP((char*)writeBuf, (char*)readBuf, (size_t)writeSz) != 0) {
  1061. fprintf(stderr, "echo check failed!\n");
  1062. ret = wolfSSL_get_error(cli_ssl, ret);
  1063. goto exit;
  1064. }
  1065. }
  1066. CloseAndCleanupSocket(&info->client.sockFd);
  1067. wolfSSL_free(cli_ssl);
  1068. cli_ssl = NULL;
  1069. }
  1070. exit:
  1071. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1072. fprintf(stderr, "Client Error: %d (%s)\n", ret,
  1073. wolfSSL_ERR_reason_error_string((unsigned long)ret));
  1074. }
  1075. /* clean up */
  1076. CloseAndCleanupSocket(&info->client.sockFd);
  1077. if (cli_ssl != NULL) {
  1078. wolfSSL_free(cli_ssl);
  1079. }
  1080. if (cli_ctx != NULL) {
  1081. wolfSSL_CTX_free(cli_ctx);
  1082. }
  1083. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1084. XFREE(writeBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1085. info->client.ret = ret;
  1086. (void)tls13;
  1087. return ret;
  1088. }
  1089. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
  1090. static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args)
  1091. {
  1092. int ret;
  1093. info_t* info = (info_t*)args;
  1094. ret = bench_tls_client(info);
  1095. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
  1096. info->to_client.done = 1;
  1097. info->client.ret = ret;
  1098. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
  1099. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  1100. WOLFSSL_RETURN_FROM_THREAD(0);
  1101. }
  1102. #endif /* !SINGLE_THREADED */
  1103. #endif /* !NO_WOLFSSL_CLIENT */
  1104. #ifndef NO_WOLFSSL_SERVER
  1105. static int SetupSocketAndListen(int* listenFd, word32 port, int doDTLS)
  1106. {
  1107. struct sockaddr_in servAddr;
  1108. #if defined(_MSC_VER) || defined(__MINGW32__)
  1109. char optval = 1;
  1110. #else
  1111. int optval = 1;
  1112. #endif
  1113. #ifndef WOLFSSL_DTLS
  1114. (void) doDTLS;
  1115. #endif
  1116. /* Setup server address */
  1117. XMEMSET(&servAddr, 0, sizeof(servAddr));
  1118. servAddr.sin_family = AF_INET;
  1119. servAddr.sin_port = htons((uint16_t)port);
  1120. servAddr.sin_addr.s_addr = INADDR_ANY;
  1121. #ifdef WOLFSSL_DTLS
  1122. if (doDTLS) {
  1123. /* Create a socket that is implemented on the User Datagram Protocol/
  1124. * Interet Protocol(UDP/IP protocol). */
  1125. if((*listenFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  1126. fprintf(stderr, "ERROR: failed to create the socket\n");
  1127. return -1;
  1128. }
  1129. }
  1130. else
  1131. #endif
  1132. {
  1133. /* Create a socket that uses an Internet IPv4 address,
  1134. * Sets the socket to be stream based (TCP),
  1135. * 0 means choose the default protocol. */
  1136. if ((*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  1137. fprintf(stderr, "ERROR: failed to create the socket\n");
  1138. return -1;
  1139. }
  1140. /* allow reuse */
  1141. if (setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR,
  1142. &optval, sizeof(optval)) == -1) {
  1143. fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
  1144. return -1;
  1145. }
  1146. /* Listen for the client. */
  1147. if (bind(*listenFd, (struct sockaddr*)&servAddr,
  1148. sizeof(servAddr)) == -1) {
  1149. fprintf(stderr, "ERROR: failed to bind\n");
  1150. return -1;
  1151. }
  1152. }
  1153. if (listen(*listenFd, 5) != 0) {
  1154. fprintf(stderr, "ERROR: failed to listen\n");
  1155. return -1;
  1156. }
  1157. #ifdef BENCH_USE_NONBLOCK
  1158. if (SetSocketNonBlocking(*listenFd) != 0) {
  1159. return -1;
  1160. }
  1161. #endif
  1162. return 0;
  1163. }
  1164. static int SocketWaitClient(info_t* info)
  1165. {
  1166. int connd;
  1167. struct sockaddr_in clientAddr;
  1168. socklen_t size = sizeof(clientAddr);
  1169. #ifdef WOLFSSL_DTLS
  1170. if (info->doDTLS) {
  1171. char msg[64];
  1172. #ifndef SINGLE_THREADED
  1173. if (!info->clientOrserverOnly) {
  1174. THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
  1175. info->serverReady = 1;
  1176. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond));
  1177. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
  1178. }
  1179. #endif
  1180. connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
  1181. MSG_PEEK, (struct sockaddr*)&clientAddr, &size);
  1182. if (connd < -1) {
  1183. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1184. return -1;
  1185. }
  1186. XMEMCPY(&info->clientAddr, &clientAddr, sizeof(clientAddr));
  1187. info->server.sockFd = info->listenFd;
  1188. }
  1189. else
  1190. #endif
  1191. {
  1192. #ifndef SINGLE_THREADED
  1193. info->serverListening = 1;
  1194. #endif
  1195. if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr,
  1196. &size)) == -1) {
  1197. if (errno == SOCKET_EWOULDBLOCK) {
  1198. return -2;
  1199. }
  1200. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1201. return -1;
  1202. }
  1203. info->server.sockFd = connd;
  1204. }
  1205. if (info->showVerbose) {
  1206. fprintf(stderr, "Got client %d\n", connd);
  1207. }
  1208. return 0;
  1209. }
  1210. static void CloseAndCleanupListenSocket(int* listenFd)
  1211. {
  1212. if (*listenFd != -1) {
  1213. close(*listenFd);
  1214. *listenFd = -1;
  1215. }
  1216. }
  1217. static int bench_tls_server(info_t* info)
  1218. {
  1219. byte *readBuf = NULL;
  1220. double start;
  1221. int ret, len = 0, readBufSz;
  1222. WOLFSSL_CTX* srv_ctx = NULL;
  1223. WOLFSSL* srv_ssl = NULL;
  1224. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  1225. int total_sz;
  1226. /* set up server */
  1227. #ifdef WOLFSSL_DTLS
  1228. if (info->doDTLS) {
  1229. if (tls13) {
  1230. #ifdef WOLFSSL_DTLS13
  1231. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_3_server_method());
  1232. #endif
  1233. }
  1234. else {
  1235. #ifndef WOLFSSL_NO_TLS12
  1236. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  1237. #endif
  1238. }
  1239. }
  1240. else
  1241. #endif
  1242. {
  1243. #ifdef WOLFSSL_TLS13
  1244. if (tls13) {
  1245. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1246. }
  1247. else
  1248. #endif
  1249. {
  1250. #if !defined(WOLFSSL_TLS13)
  1251. srv_ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1252. #elif !defined(WOLFSSL_NO_TLS12)
  1253. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  1254. #endif
  1255. }
  1256. }
  1257. if (srv_ctx == NULL) {
  1258. fprintf(stderr, "error creating server ctx\n");
  1259. ret = MEMORY_E; goto exit;
  1260. }
  1261. #ifndef NO_CERTS
  1262. #ifdef HAVE_ECC
  1263. if (XSTRSTR(info->cipher, "ECDSA")) {
  1264. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, ecc_key_der_256,
  1265. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  1266. }
  1267. else
  1268. #endif
  1269. {
  1270. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, server_key_der_2048,
  1271. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  1272. }
  1273. if (ret != WOLFSSL_SUCCESS) {
  1274. fprintf(stderr, "error loading server key\n");
  1275. goto exit;
  1276. }
  1277. #ifdef HAVE_ECC
  1278. if (XSTRSTR(info->cipher, "ECDSA")) {
  1279. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, serv_ecc_der_256,
  1280. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  1281. }
  1282. else
  1283. #endif
  1284. {
  1285. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, server_cert_der_2048,
  1286. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  1287. }
  1288. if (ret != WOLFSSL_SUCCESS) {
  1289. fprintf(stderr, "error loading server cert\n");
  1290. goto exit;
  1291. }
  1292. #endif /* !NO_CERTS */
  1293. wolfSSL_CTX_SetIOSend(srv_ctx, ServerSend);
  1294. wolfSSL_CTX_SetIORecv(srv_ctx, ServerRecv);
  1295. /* set cipher suite */
  1296. ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher);
  1297. if (ret != WOLFSSL_SUCCESS) {
  1298. fprintf(stderr, "error setting cipher suite\n");
  1299. goto exit;
  1300. }
  1301. #ifndef NO_DH
  1302. ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS);
  1303. if (ret != WOLFSSL_SUCCESS) {
  1304. fprintf(stderr, "Error setting minimum DH key size\n");
  1305. goto exit;
  1306. }
  1307. #endif
  1308. #ifndef NO_PSK
  1309. wolfSSL_CTX_set_psk_server_callback(srv_ctx, my_psk_server_cb);
  1310. #ifdef WOLFSSL_TLS13
  1311. wolfSSL_CTX_set_psk_server_tls13_callback(srv_ctx, my_psk_server_tls13_cb);
  1312. #endif
  1313. #endif /* !NO_PSK */
  1314. /* Allocate read buffer */
  1315. readBufSz = info->packetSize;
  1316. readBuf = (unsigned char*)XMALLOC((size_t)readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1317. if (readBuf == NULL) {
  1318. fprintf(stderr, "failed to allocate read memory\n");
  1319. ret = MEMORY_E; goto exit;
  1320. }
  1321. /* BENCHMARK CONNECTIONS LOOP */
  1322. while (!info->server.shutdown) {
  1323. #ifdef BENCH_USE_NONBLOCK
  1324. int err;
  1325. #endif
  1326. #ifndef SINGLE_THREADED
  1327. if (!info->useLocalMem)
  1328. #endif
  1329. {
  1330. /* Accept client connections */
  1331. ret = SocketWaitClient(info);
  1332. #ifdef BENCH_USE_NONBLOCK
  1333. if (ret == -2) {
  1334. XSLEEP_MS(0);
  1335. continue;
  1336. }
  1337. #endif
  1338. if (ret != 0) {
  1339. goto exit;
  1340. }
  1341. }
  1342. srv_ssl = wolfSSL_new(srv_ctx);
  1343. if (srv_ssl == NULL) {
  1344. fprintf(stderr, "error creating server object\n");
  1345. ret = MEMORY_E; goto exit;
  1346. }
  1347. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1348. if (info->group != 0) {
  1349. ret = wolfSSL_UseKeyShare(srv_ssl, info->group);
  1350. if (ret != WOLFSSL_SUCCESS) {
  1351. fprintf(stderr, "error setting server key share.\n");
  1352. goto exit;
  1353. }
  1354. }
  1355. #endif
  1356. #ifdef WOLFSSL_DTLS
  1357. if (info->doDTLS) {
  1358. ret = wolfSSL_dtls_set_peer(srv_ssl, &info->clientAddr,
  1359. sizeof(info->clientAddr));
  1360. if (ret != WOLFSSL_SUCCESS) {
  1361. fprintf(stderr, "error setting dtls peer\n");
  1362. goto exit;
  1363. }
  1364. }
  1365. #endif
  1366. wolfSSL_SetIOReadCtx(srv_ssl, info);
  1367. wolfSSL_SetIOWriteCtx(srv_ssl, info);
  1368. #ifndef NO_DH
  1369. wolfSSL_SetTmpDH(srv_ssl, dhp, sizeof(dhp), dhg, sizeof(dhg));
  1370. #endif
  1371. /* accept TLS connection */
  1372. start = gettime_secs(1);
  1373. #ifndef BENCH_USE_NONBLOCK
  1374. ret = wolfSSL_accept(srv_ssl);
  1375. #else
  1376. do {
  1377. ret = wolfSSL_accept(srv_ssl);
  1378. err = wolfSSL_get_error(srv_ssl, ret);
  1379. }
  1380. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1381. #endif
  1382. start = gettime_secs(0) - start;
  1383. if (ret != WOLFSSL_SUCCESS) {
  1384. #ifndef SINGLE_THREADED
  1385. if (info->to_client.done) {
  1386. ret = 0; /* done - success */
  1387. }
  1388. else
  1389. #endif
  1390. {
  1391. fprintf(stderr, "error on server accept\n");
  1392. ret = wolfSSL_get_error(srv_ssl, ret);
  1393. }
  1394. goto exit;
  1395. }
  1396. info->server_stats.connTime += start;
  1397. info->server_stats.connCount++;
  1398. /* echo loop */
  1399. ret = 0;
  1400. total_sz = 0;
  1401. while (ret == 0 && total_sz < info->maxSize) {
  1402. double rxTime;
  1403. /* read message from client */
  1404. XMEMSET(readBuf, 0, (size_t)readBufSz);
  1405. start = gettime_secs(1);
  1406. #ifndef BENCH_USE_NONBLOCK
  1407. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1408. #else
  1409. do {
  1410. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1411. err = wolfSSL_get_error(srv_ssl, ret);
  1412. }
  1413. while (err == WOLFSSL_ERROR_WANT_READ);
  1414. #endif
  1415. rxTime = gettime_secs(0) - start;
  1416. /* shutdown signals, no more connections for this cipher */
  1417. if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) {
  1418. info->server.shutdown = 1;
  1419. if (info->showVerbose) {
  1420. fprintf(stderr, "Server shutdown done\n");
  1421. }
  1422. ret = 0; /* success */
  1423. break;
  1424. }
  1425. info->server_stats.rxTime += rxTime;
  1426. if (ret < 0) {
  1427. #ifndef SINGLE_THREADED
  1428. if (info->to_client.done) {
  1429. ret = 0; /* done - success */
  1430. }
  1431. else
  1432. #endif
  1433. {
  1434. fprintf(stderr, "error on server read\n");
  1435. ret = wolfSSL_get_error(srv_ssl, ret);
  1436. }
  1437. goto exit;
  1438. }
  1439. info->server_stats.rxTotal += ret;
  1440. len = ret;
  1441. total_sz += ret;
  1442. /* write message back to client */
  1443. start = gettime_secs(1);
  1444. #ifndef BENCH_USE_NONBLOCK
  1445. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1446. #else
  1447. do {
  1448. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1449. err = wolfSSL_get_error(srv_ssl, ret);
  1450. }
  1451. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1452. #endif
  1453. info->server_stats.txTime += gettime_secs(0) - start;
  1454. if (ret < 0) {
  1455. fprintf(stderr, "error on server write\n");
  1456. ret = wolfSSL_get_error(srv_ssl, ret);
  1457. goto exit;
  1458. }
  1459. info->server_stats.txTotal += ret;
  1460. ret = 0; /* reset return code */
  1461. }
  1462. CloseAndCleanupSocket(&info->server.sockFd);
  1463. wolfSSL_free(srv_ssl);
  1464. srv_ssl = NULL;
  1465. #ifdef WOLFSSL_DTLS
  1466. if (info->doDTLS) {
  1467. SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1468. }
  1469. #endif
  1470. }
  1471. exit:
  1472. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1473. fprintf(stderr, "Server Error: %d (%s)\n", ret,
  1474. wolfSSL_ERR_reason_error_string((unsigned long)ret));
  1475. }
  1476. /* clean up */
  1477. CloseAndCleanupSocket(&info->server.sockFd);
  1478. if (srv_ssl != NULL) {
  1479. wolfSSL_free(srv_ssl);
  1480. }
  1481. if (srv_ctx != NULL) {
  1482. wolfSSL_CTX_free(srv_ctx);
  1483. }
  1484. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1485. info->server.ret = ret;
  1486. (void)tls13;
  1487. return ret;
  1488. }
  1489. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
  1490. static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args)
  1491. {
  1492. int ret = 0;
  1493. info_t* info = (info_t*)args;
  1494. if (!info->useLocalMem) {
  1495. /* Setup TLS server listener */
  1496. #ifdef WOLFSSL_DTLS
  1497. ret = SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1498. #else
  1499. ret = SetupSocketAndListen(&info->listenFd, info->port, 0);
  1500. #endif
  1501. }
  1502. if (ret == 0) {
  1503. ret = bench_tls_server(info);
  1504. if (!info->useLocalMem) {
  1505. CloseAndCleanupListenSocket(&info->listenFd);
  1506. }
  1507. }
  1508. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
  1509. info->to_server.done = 1;
  1510. info->server.ret = ret;
  1511. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
  1512. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  1513. WOLFSSL_RETURN_FROM_THREAD(0);
  1514. }
  1515. #endif /* !SINGLE_THREADED */
  1516. #endif /* !NO_WOLFSSL_SERVER */
  1517. static void print_stats(stats_t* wcStat, const char* desc, const char* cipher, const char *group, int verbose)
  1518. {
  1519. if (verbose) {
  1520. fprintf(stderr,
  1521. "wolfSSL %s Benchmark on %s with group %s:\n"
  1522. "\tTotal : %9d bytes\n"
  1523. "\tNum Conns : %9d\n"
  1524. "\tRx Total : %9.3f ms\n"
  1525. "\tTx Total : %9.3f ms\n"
  1526. "\tRx : %9.3f MB/s\n"
  1527. "\tTx : %9.3f MB/s\n"
  1528. "\tConnect : %9.3f ms\n"
  1529. "\tConnect Avg : %9.3f ms\n",
  1530. desc,
  1531. cipher,
  1532. group,
  1533. wcStat->txTotal + wcStat->rxTotal,
  1534. wcStat->connCount,
  1535. wcStat->rxTime * 1000,
  1536. wcStat->txTime * 1000,
  1537. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1538. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1539. wcStat->connTime * 1000,
  1540. wcStat->connTime * 1000 / wcStat->connCount);
  1541. }
  1542. else {
  1543. fprintf(stderr,
  1544. "%-6s %-33s %-25s %11d %9d %9.3f %9.3f %9.3f "
  1545. "%9.3f %17.3f %15.3f\n",
  1546. desc,
  1547. cipher,
  1548. group,
  1549. wcStat->txTotal + wcStat->rxTotal,
  1550. wcStat->connCount,
  1551. wcStat->rxTime * 1000,
  1552. wcStat->txTime * 1000,
  1553. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1554. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1555. wcStat->connTime * 1000,
  1556. wcStat->connTime * 1000 / wcStat->connCount);
  1557. }
  1558. }
  1559. static void Usage(void)
  1560. {
  1561. fprintf(stderr, "tls_bench " LIBWOLFSSL_VERSION_STRING
  1562. " NOTE: All files relative to wolfSSL home dir\n");
  1563. fprintf(stderr, "-? Help, print this usage\n");
  1564. fprintf(stderr, "-c Run as client only, no threading and uses sockets\n");
  1565. fprintf(stderr, "-s Run as server only, no threading and uses sockets\n");
  1566. fprintf(stderr, "-h Host (default %s)\n", BENCH_DEFAULT_HOST);
  1567. fprintf(stderr, "-P Port (default %d)\n", BENCH_DEFAULT_PORT);
  1568. fprintf(stderr, "-e List Every cipher suite available\n");
  1569. fprintf(stderr, "-i Show peer info\n");
  1570. #ifdef WOLFSSL_TLS13
  1571. fprintf(stderr, "-g Run through each of the TLS 1.3 groups that are available\n");
  1572. #endif
  1573. fprintf(stderr, "-l <str> Cipher suite list (: delimited)\n");
  1574. fprintf(stderr, "-t <num> Time <num> (seconds) to run each test (default %d)\n", BENCH_RUNTIME_SEC);
  1575. fprintf(stderr, "-p <num> The packet size <num> in bytes [1-16kB] (default %d)\n", TEST_PACKET_SIZE);
  1576. #ifdef WOLFSSL_DTLS
  1577. fprintf(stderr, " In the case of DTLS, [1-8kB] (default %d)\n", TEST_DTLS_PACKET_SIZE);
  1578. #endif
  1579. fprintf(stderr, "-S <num> The total size <num> in bytes (default %d)\n", TEST_MAX_SIZE);
  1580. fprintf(stderr, "-v Show verbose output\n");
  1581. #ifdef DEBUG_WOLFSSL
  1582. fprintf(stderr, "-d Enable debug messages\n");
  1583. #endif
  1584. #ifndef SINGLE_THREADED
  1585. fprintf(stderr, "-T <num> Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS);
  1586. fprintf(stderr, "-m Use local memory, not socket\n");
  1587. #endif
  1588. #ifdef WOLFSSL_DTLS
  1589. fprintf(stderr, "-u Use DTLS\n");
  1590. #endif
  1591. }
  1592. static void ShowCiphers(void)
  1593. {
  1594. char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
  1595. int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
  1596. if (ret == WOLFSSL_SUCCESS) {
  1597. fprintf(stderr, "%s\n", ciphers);
  1598. }
  1599. }
  1600. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1601. static int SetupSupportedGroups(int verbose)
  1602. {
  1603. int i;
  1604. WOLFSSL_CTX* ctx = NULL;
  1605. WOLFSSL* ssl = NULL;
  1606. int ret = 0;
  1607. if (ret == 0) {
  1608. #ifdef NO_WOLFSSL_CLIENT
  1609. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1610. #else
  1611. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  1612. #endif
  1613. if (ctx == NULL) {
  1614. ret = -1;
  1615. }
  1616. }
  1617. if (ret == 0) {
  1618. ssl = wolfSSL_new(ctx);
  1619. if (ssl == NULL) {
  1620. ret = -1;
  1621. }
  1622. }
  1623. /* Determine which groups are actually supported. */
  1624. for (i = 0; groups[i].name != NULL; i++) {
  1625. if (ret == 0) {
  1626. int uks_ret = wolfSSL_UseKeyShare(ssl, groups[i].group);
  1627. if (uks_ret == WOLFSSL_SUCCESS) {
  1628. if (verbose) {
  1629. printf("Will benchmark the following group: %s\n",
  1630. groups[i].name);
  1631. }
  1632. } else if (uks_ret == WC_NO_ERR_TRACE(BAD_FUNC_ARG) ||
  1633. uks_ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN))
  1634. {
  1635. groups[i].group = 0;
  1636. if (verbose) {
  1637. printf("Will NOT benchmark the following group: %s\n",
  1638. groups[i].name);
  1639. }
  1640. } else {
  1641. ret = -1;
  1642. }
  1643. }
  1644. }
  1645. if (ssl != NULL) {
  1646. wolfSSL_free(ssl);
  1647. }
  1648. if (ctx != NULL) {
  1649. wolfSSL_CTX_free(ctx);
  1650. }
  1651. return ret;
  1652. }
  1653. #endif
  1654. int bench_tls(void* args)
  1655. {
  1656. int ret = 0;
  1657. info_t *theadInfo = NULL, *info;
  1658. stats_t cli_comb, srv_comb;
  1659. int i;
  1660. char *cipher, *next_cipher, *ciphers = NULL;
  1661. int argc = 0;
  1662. char** argv = NULL;
  1663. int ch;
  1664. /* Vars configured by command line arguments */
  1665. int argRuntimeSec = BENCH_RUNTIME_SEC;
  1666. char *argCipherList = NULL;
  1667. int argTestPacketSize = TEST_PACKET_SIZE;
  1668. int argTestMaxSize = TEST_MAX_SIZE;
  1669. int argThreadPairs = NUM_THREAD_PAIRS;
  1670. int argShowVerbose = SHOW_VERBOSE;
  1671. int argClientOnly = 0;
  1672. int argServerOnly = 0;
  1673. const char* argHost = BENCH_DEFAULT_HOST;
  1674. word32 argPort = BENCH_DEFAULT_PORT;
  1675. int argShowPeerInfo = 0;
  1676. #ifndef SINGLE_THREADED
  1677. int doShutdown;
  1678. #endif
  1679. #if !defined(NO_WOLFSSL_SERVER) || !defined(SINGLE_THREADED)
  1680. int argLocalMem = 0;
  1681. int listenFd = -1;
  1682. #endif
  1683. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1684. int option_p = 0;
  1685. #endif
  1686. #ifdef WOLFSSL_DTLS
  1687. int doDTLS = 0;
  1688. #endif
  1689. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1690. int group_index = 0;
  1691. int argDoGroups = 0;
  1692. #endif
  1693. if (args != NULL) {
  1694. argc = ((func_args*)args)->argc;
  1695. argv = ((func_args*)args)->argv;
  1696. ((func_args*)args)->return_code = -1; /* error state */
  1697. }
  1698. /* Initialize wolfSSL */
  1699. wolfSSL_Init();
  1700. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
  1701. /*
  1702. * When running benchmarks on FIPS builds, we need to run ALL CASTs up
  1703. * front before spawning client/server threads, otherwise there is the
  1704. * possibility that both threads try to run a CAST at the same time during
  1705. * the handshake. In this scenario, the thread that doesn't win the race
  1706. * will not be able to run the CAST, since it returns "busy", which is treated
  1707. * as a failure. Running the CASTs up front is a simpler solution than
  1708. * implementing an additional layer of synchronization.
  1709. */
  1710. if ((ret = run_all_CAST()) != 0)
  1711. {
  1712. fprintf(stderr, "CAST failed. Exiting benchmark\n");
  1713. goto exit;
  1714. }
  1715. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
  1716. /* Parse command line arguments */
  1717. while ((ch = mygetopt(argc, argv, "?" "udeil:p:t:vT:sch:P:mS:g")) != -1) {
  1718. switch (ch) {
  1719. case '?' :
  1720. Usage();
  1721. goto exit;
  1722. case 's':
  1723. argServerOnly = 1;
  1724. break;
  1725. case 'c':
  1726. argClientOnly = 1;
  1727. break;
  1728. case 'h':
  1729. argHost = myoptarg;
  1730. break;
  1731. case 'P':
  1732. argPort = (word32)atoi(myoptarg);
  1733. break;
  1734. case 'd' :
  1735. #ifdef DEBUG_WOLFSSL
  1736. wolfSSL_Debugging_ON();
  1737. #endif
  1738. break;
  1739. case 'e' :
  1740. ShowCiphers();
  1741. goto exit;
  1742. case 'g' :
  1743. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1744. argDoGroups = 1;
  1745. break;
  1746. #else
  1747. fprintf(stderr, "There are only groups in TLS 1.3\n");
  1748. Usage();
  1749. ret = MY_EX_USAGE; goto exit;
  1750. #endif
  1751. case 'i' :
  1752. argShowPeerInfo = 1;
  1753. break;
  1754. case 'l' :
  1755. argCipherList = myoptarg;
  1756. break;
  1757. case 'p' :
  1758. argTestPacketSize = atoi(myoptarg);
  1759. if (argTestPacketSize > (16 * 1024)) {
  1760. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1761. Usage();
  1762. ret = MY_EX_USAGE; goto exit;
  1763. }
  1764. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1765. option_p = 1;
  1766. #endif
  1767. break;
  1768. case 'S' :
  1769. argTestMaxSize = atoi(myoptarg);
  1770. break;
  1771. case 't' :
  1772. argRuntimeSec = atoi(myoptarg);
  1773. break;
  1774. case 'v' :
  1775. argShowVerbose = 1;
  1776. break;
  1777. case 'T' :
  1778. #ifndef SINGLE_THREADED
  1779. argThreadPairs = atoi(myoptarg);
  1780. #endif
  1781. break;
  1782. case 'm':
  1783. #ifndef SINGLE_THREADED
  1784. argLocalMem = 1;
  1785. #endif
  1786. break;
  1787. case 'u':
  1788. #ifdef WOLFSSL_DTLS
  1789. doDTLS = 1;
  1790. #ifdef BENCH_USE_NONBLOCK
  1791. fprintf(stderr, "tls_bench hasn't yet supported DTLS "
  1792. "non-blocking mode.\n");
  1793. Usage();
  1794. ret = MY_EX_USAGE; goto exit;
  1795. #endif
  1796. #endif
  1797. break;
  1798. default:
  1799. Usage();
  1800. ret = MY_EX_USAGE; goto exit;
  1801. }
  1802. }
  1803. /* reset for test cases */
  1804. myoptind = 0;
  1805. if (argCipherList != NULL) {
  1806. /* Use the list from CL argument */
  1807. cipher = argCipherList;
  1808. }
  1809. else {
  1810. /* Run for each cipher */
  1811. ciphers = (char*)XMALLOC(WOLFSSL_CIPHER_LIST_MAX_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1812. if (ciphers == NULL) {
  1813. goto exit;
  1814. }
  1815. wolfSSL_get_ciphers(ciphers, WOLFSSL_CIPHER_LIST_MAX_SIZE);
  1816. cipher = ciphers;
  1817. }
  1818. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1819. if (argDoGroups) {
  1820. if (SetupSupportedGroups(argShowVerbose) != 0) {
  1821. goto exit;
  1822. }
  1823. }
  1824. #endif
  1825. /* for server or client side only, only 1 thread is allowed */
  1826. if (argServerOnly || argClientOnly) {
  1827. argThreadPairs = 1;
  1828. }
  1829. #ifdef SINGLE_THREADED
  1830. else {
  1831. fprintf(stderr, "Threading is not enabled, so please use -s or -c to indicate side\n");
  1832. Usage();
  1833. ret = MY_EX_USAGE; goto exit;
  1834. }
  1835. #endif
  1836. /* Allocate test info array */
  1837. theadInfo = (info_t*)XMALLOC(sizeof(info_t) * (size_t)argThreadPairs, NULL,
  1838. DYNAMIC_TYPE_TMP_BUFFER);
  1839. if (theadInfo == NULL) {
  1840. ret = MEMORY_E; goto exit;
  1841. }
  1842. XMEMSET(theadInfo, 0, sizeof(info_t) * (size_t)argThreadPairs);
  1843. #ifndef NO_WOLFSSL_SERVER
  1844. /* Use same listen socket to avoid timing issues between client and server */
  1845. if (argServerOnly && !argLocalMem) {
  1846. /* Setup TLS server listener */
  1847. #ifdef WOLFSSL_DTLS
  1848. ret = SetupSocketAndListen(&listenFd, argPort, doDTLS);
  1849. #else
  1850. ret = SetupSocketAndListen(&listenFd, argPort, 0);
  1851. #endif
  1852. if (ret != 0) {
  1853. goto exit;
  1854. }
  1855. }
  1856. #endif
  1857. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1858. if (doDTLS) {
  1859. if (argLocalMem) {
  1860. fprintf(stderr, "tls_bench hasn't yet supported DTLS with local memory.\n");
  1861. ret = MY_EX_USAGE; goto exit;
  1862. }
  1863. if (option_p && argTestPacketSize > TEST_DTLS_PACKET_SIZE){
  1864. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1865. Usage();
  1866. ret = MY_EX_USAGE; goto exit;
  1867. } else {
  1868. /* argTestPacketSize would be default for tcp packet */
  1869. if (argTestPacketSize >= TEST_PACKET_SIZE)
  1870. argTestPacketSize = TEST_DTLS_PACKET_SIZE;
  1871. }
  1872. }
  1873. #endif
  1874. fprintf(stderr, "Running TLS Benchmarks...\n");
  1875. /* parse by : */
  1876. while ((cipher != NULL) && (cipher[0] != '\0')) {
  1877. #if ! (defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES))
  1878. const char *gname = "N/A";
  1879. #endif
  1880. next_cipher = strchr(cipher, ':');
  1881. if (next_cipher != NULL) {
  1882. cipher[next_cipher - cipher] = '\0';
  1883. }
  1884. if (argShowVerbose) {
  1885. fprintf(stderr, "Cipher: %s\n", cipher);
  1886. }
  1887. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1888. for (group_index = 0; groups[group_index].name != NULL; group_index++) {
  1889. const char *gname = theadInfo[0].group == 0 ? "N/A"
  1890. : groups[group_index].name;
  1891. if (argDoGroups && groups[group_index].group == 0) {
  1892. /* Skip unsupported group. */
  1893. continue;
  1894. }
  1895. #endif
  1896. for (i=0; i<argThreadPairs; i++) {
  1897. info = &theadInfo[i];
  1898. XMEMSET(info, 0, sizeof(info_t));
  1899. info->host = argHost;
  1900. info->port = argPort + (word32)i; /* threads must have separate ports */
  1901. info->cipher = cipher;
  1902. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1903. if (argDoGroups && XSTRNCMP(theadInfo[0].cipher, "TLS13", 5) == 0) {
  1904. info->group = groups[group_index].group;
  1905. }
  1906. else
  1907. #endif
  1908. {
  1909. info->group = 0;
  1910. }
  1911. info->packetSize = argTestPacketSize;
  1912. info->runTimeSec = argRuntimeSec;
  1913. info->maxSize = argTestMaxSize;
  1914. info->showPeerInfo = argShowPeerInfo;
  1915. info->showVerbose = argShowVerbose;
  1916. #ifndef NO_WOLFSSL_SERVER
  1917. info->listenFd = listenFd;
  1918. #endif
  1919. info->client.sockFd = -1;
  1920. info->server.sockFd = -1;
  1921. #ifdef WOLFSSL_DTLS
  1922. info->doDTLS = doDTLS;
  1923. #ifndef SINGLE_THREADED
  1924. info->serverReady = 0;
  1925. if (argServerOnly || argClientOnly) {
  1926. info->clientOrserverOnly = 1;
  1927. }
  1928. #endif
  1929. #endif
  1930. if (argClientOnly) {
  1931. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT) && !defined(SINGLE_THREADED)
  1932. /* to avoid to wait server forever */
  1933. info->serverListening = 1;
  1934. #endif
  1935. #ifndef NO_WOLFSSL_CLIENT
  1936. ret = bench_tls_client(info);
  1937. #endif
  1938. }
  1939. else if (argServerOnly) {
  1940. #ifndef NO_WOLFSSL_SERVER
  1941. ret = bench_tls_server(info);
  1942. #endif
  1943. }
  1944. else {
  1945. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
  1946. info->useLocalMem = argLocalMem;
  1947. #ifdef WOLFSSL_DTLS
  1948. THREAD_CHECK_RET(wc_InitMutex(&info->dtls_mutex));
  1949. THREAD_CHECK_RET(wolfSSL_CondInit(&info->dtls_cond));
  1950. #endif
  1951. THREAD_CHECK_RET(wolfSSL_CondInit(&info->to_server.cond));
  1952. THREAD_CHECK_RET(wolfSSL_CondInit(&info->to_client.cond));
  1953. THREAD_CHECK_RET(
  1954. wolfSSL_NewThreadNoJoin(server_thread, info));
  1955. THREAD_CHECK_RET(
  1956. wolfSSL_NewThreadNoJoin(client_thread, info));
  1957. #endif
  1958. }
  1959. }
  1960. #ifndef SINGLE_THREADED
  1961. /* For threading, wait for completion */
  1962. if (!argClientOnly && !argServerOnly) {
  1963. /* Wait until threads are marked done */
  1964. do {
  1965. doShutdown = 1;
  1966. for (i = 0; i < argThreadPairs; ++i) {
  1967. info = &theadInfo[i];
  1968. if (!info->to_client.done || !info->to_server.done) {
  1969. doShutdown = 0;
  1970. XSLEEP_MS(1000); /* Allow other threads to run */
  1971. }
  1972. }
  1973. } while (!doShutdown);
  1974. if (argShowVerbose) {
  1975. fprintf(stderr, "Shutdown complete\n");
  1976. }
  1977. }
  1978. #endif /* !SINGLE_THREADED */
  1979. if (argShowVerbose) {
  1980. /* print results */
  1981. for (i = 0; i < argThreadPairs; ++i) {
  1982. info = &theadInfo[i];
  1983. fprintf(stderr, "\nThread %d\n", i);
  1984. #ifndef NO_WOLFSSL_SERVER
  1985. if (!argClientOnly) {
  1986. print_stats(&info->server_stats, "Server", info->cipher,
  1987. gname, 1);
  1988. }
  1989. #endif
  1990. #ifndef NO_WOLFSSL_CLIENT
  1991. if (!argServerOnly) {
  1992. print_stats(&info->client_stats, "Client", info->cipher,
  1993. gname, 1);
  1994. }
  1995. #endif
  1996. }
  1997. }
  1998. /* print combined results for more than one thread */
  1999. XMEMSET(&cli_comb, 0, sizeof(cli_comb));
  2000. XMEMSET(&srv_comb, 0, sizeof(srv_comb));
  2001. for (i = 0; i < argThreadPairs; ++i) {
  2002. info = &theadInfo[i];
  2003. cli_comb.connCount += info->client_stats.connCount;
  2004. srv_comb.connCount += info->server_stats.connCount;
  2005. cli_comb.connTime += info->client_stats.connTime;
  2006. srv_comb.connTime += info->server_stats.connTime;
  2007. cli_comb.rxTotal += info->client_stats.rxTotal;
  2008. srv_comb.rxTotal += info->server_stats.rxTotal;
  2009. cli_comb.rxTime += info->client_stats.rxTime;
  2010. srv_comb.rxTime += info->server_stats.rxTime;
  2011. cli_comb.txTotal += info->client_stats.txTotal;
  2012. srv_comb.txTotal += info->server_stats.txTotal;
  2013. cli_comb.txTime += info->client_stats.txTime;
  2014. srv_comb.txTime += info->server_stats.txTime;
  2015. }
  2016. if (argShowVerbose) {
  2017. fprintf(stderr, "Totals for %d Threads\n", argThreadPairs);
  2018. }
  2019. else {
  2020. fprintf(stderr, "%-6s %-33s %-25s %11s %9s %9s %9s %9s %9s %17s %15s\n",
  2021. "Side", "Cipher", "Group", "Total Bytes", "Num Conns", "Rx ms", "Tx ms",
  2022. "Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms");
  2023. #ifndef NO_WOLFSSL_SERVER
  2024. if (!argClientOnly) {
  2025. print_stats(&srv_comb, "Server", theadInfo[0].cipher, gname,
  2026. 0);
  2027. }
  2028. #endif
  2029. #ifndef NO_WOLFSSL_CLIENT
  2030. if (!argServerOnly) {
  2031. print_stats(&cli_comb, "Client", theadInfo[0].cipher, gname,
  2032. 0);
  2033. }
  2034. #endif
  2035. }
  2036. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  2037. if (!argDoGroups || theadInfo[0].group == 0) {
  2038. /* We only needed to do this once because they don't want to
  2039. * benchmarks groups or this isn't a TLS 1.3 cipher. */
  2040. break;
  2041. }
  2042. }
  2043. #endif
  2044. /* target next cipher */
  2045. cipher = (next_cipher != NULL) ? (next_cipher + 1) : NULL;
  2046. } /* while */
  2047. exit:
  2048. #ifndef NO_WOLFSSL_SERVER
  2049. if (argServerOnly && !argLocalMem) {
  2050. /* Close server listener */
  2051. CloseAndCleanupListenSocket(&listenFd);
  2052. }
  2053. #endif
  2054. /* Cleanup the wolfSSL environment */
  2055. wolfSSL_Cleanup();
  2056. /* Free theadInfo array */
  2057. XFREE(theadInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2058. /* Free cipher list */
  2059. XFREE(ciphers, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2060. /* Return reporting a success */
  2061. if (args) {
  2062. ((func_args*)args)->return_code = ret;
  2063. }
  2064. return ret;
  2065. }
  2066. #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !WOLFCRYPT_ONLY && USE_WOLFSSL_IO */
  2067. #ifndef NO_MAIN_DRIVER
  2068. int main(int argc, char** argv)
  2069. {
  2070. func_args args;
  2071. args.argc = argc;
  2072. args.argv = argv;
  2073. args.return_code = 0;
  2074. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  2075. !defined(WOLFCRYPT_ONLY) && defined(USE_WOLFSSL_IO)
  2076. bench_tls(&args);
  2077. #endif
  2078. return args.return_code;
  2079. }
  2080. #endif /* !NO_MAIN_DRIVER */