2
0

tls_bench.c 71 KB

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