1
0

tls_bench.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. /* tls_bench.c
  2. *
  3. * Copyright (C) 2006-2023 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. static double gettime_secs(int reset)
  348. {
  349. struct timeval tv;
  350. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  351. (void)reset;
  352. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  353. }
  354. #ifndef SINGLE_THREADED
  355. /* server send callback */
  356. static int ServerMemSend(info_t* info, char* buf, int sz)
  357. {
  358. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
  359. #ifndef BENCH_USE_NONBLOCK
  360. /* check for overflow */
  361. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  362. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  363. fprintf(stderr, "ServerMemSend overflow\n");
  364. return -1;
  365. }
  366. #else
  367. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  368. sz = MEM_BUFFER_SZ - info->to_client.write_idx;
  369. }
  370. #endif
  371. XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz);
  372. info->to_client.write_idx += sz;
  373. info->to_client.write_bytes += sz;
  374. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
  375. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  376. #ifdef BENCH_USE_NONBLOCK
  377. if (sz == 0) {
  378. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  379. }
  380. #endif
  381. return sz;
  382. }
  383. /* server recv callback */
  384. static int ServerMemRecv(info_t* info, char* buf, int sz)
  385. {
  386. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
  387. #ifndef BENCH_USE_NONBLOCK
  388. while (info->to_server.write_idx - info->to_server.read_idx < sz &&
  389. !info->to_client.done) {
  390. THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_server.cond));
  391. }
  392. #else
  393. if (info->to_server.write_idx - info->to_server.read_idx < sz) {
  394. sz = info->to_server.write_idx - info->to_server.read_idx;
  395. }
  396. #endif
  397. XMEMCPY(buf, &info->to_server.buf[info->to_server.read_idx], sz);
  398. info->to_server.read_idx += sz;
  399. info->to_server.read_bytes += sz;
  400. /* if the rx has caught up with pending then reset buffer positions */
  401. if (info->to_server.read_bytes == info->to_server.write_bytes) {
  402. info->to_server.read_bytes = info->to_server.read_idx = 0;
  403. info->to_server.write_bytes = info->to_server.write_idx = 0;
  404. }
  405. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  406. if (info->to_client.done != 0) {
  407. return -1;
  408. }
  409. #ifdef BENCH_USE_NONBLOCK
  410. if (sz == 0) {
  411. return WOLFSSL_CBIO_ERR_WANT_READ;
  412. }
  413. #endif
  414. return sz;
  415. }
  416. /* client send callback */
  417. static int ClientMemSend(info_t* info, char* buf, int sz)
  418. {
  419. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
  420. #ifndef BENCH_USE_NONBLOCK
  421. /* check for overflow */
  422. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  423. fprintf(stderr, "ClientMemSend overflow %d %d %d\n",
  424. info->to_server.write_idx, sz, MEM_BUFFER_SZ);
  425. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  426. return -1;
  427. }
  428. #else
  429. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  430. sz = MEM_BUFFER_SZ - info->to_server.write_idx;
  431. }
  432. #endif
  433. XMEMCPY(&info->to_server.buf[info->to_server.write_idx], buf, sz);
  434. info->to_server.write_idx += sz;
  435. info->to_server.write_bytes += sz;
  436. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
  437. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  438. #ifdef BENCH_USE_NONBLOCK
  439. if (sz == 0) {
  440. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  441. }
  442. #endif
  443. return sz;
  444. }
  445. /* client recv callback */
  446. static int ClientMemRecv(info_t* info, char* buf, int sz)
  447. {
  448. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
  449. #ifndef BENCH_USE_NONBLOCK
  450. while (info->to_client.write_idx - info->to_client.read_idx < sz &&
  451. !info->to_server.done) {
  452. THREAD_CHECK_RET(wolfSSL_CondWait(&info->to_client.cond));
  453. }
  454. #else
  455. if (info->to_client.write_idx - info->to_client.read_idx < sz) {
  456. sz = info->to_client.write_idx - info->to_client.read_idx;
  457. }
  458. #endif
  459. XMEMCPY(buf, &info->to_client.buf[info->to_client.read_idx], sz);
  460. info->to_client.read_idx += sz;
  461. info->to_client.read_bytes += sz;
  462. /* if the rx has caught up with pending then reset buffer positions */
  463. if (info->to_client.read_bytes == info->to_client.write_bytes) {
  464. info->to_client.read_bytes = info->to_client.read_idx = 0;
  465. info->to_client.write_bytes = info->to_client.write_idx = 0;
  466. }
  467. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  468. if (info->to_server.done != 0) {
  469. return -1;
  470. }
  471. #ifdef BENCH_USE_NONBLOCK
  472. if (sz == 0) {
  473. return WOLFSSL_CBIO_ERR_WANT_READ;
  474. }
  475. #endif
  476. return sz;
  477. }
  478. #endif /* !SINGLE_THREADED */
  479. static int SocketRecv(int sockFd, char* buf, int sz)
  480. {
  481. int recvd = (int)recv(sockFd, buf, sz, 0);
  482. if (recvd == -1) {
  483. switch (errno) {
  484. #if EAGAIN != SOCKET_EWOULDBLOCK
  485. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  486. #endif
  487. case SOCKET_EWOULDBLOCK:
  488. return WOLFSSL_CBIO_ERR_WANT_READ;
  489. case SOCKET_ECONNRESET:
  490. return WOLFSSL_CBIO_ERR_CONN_RST;
  491. case SOCKET_EINTR:
  492. return WOLFSSL_CBIO_ERR_ISR;
  493. case SOCKET_ECONNREFUSED: /* DTLS case */
  494. return WOLFSSL_CBIO_ERR_WANT_READ;
  495. case SOCKET_ECONNABORTED:
  496. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  497. default:
  498. return WOLFSSL_CBIO_ERR_GENERAL;
  499. }
  500. }
  501. else if (recvd == 0) {
  502. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  503. }
  504. return recvd;
  505. }
  506. static int SocketSend(int sockFd, char* buf, int sz)
  507. {
  508. int sent = (int)send(sockFd, buf, sz, 0);
  509. if (sent == -1) {
  510. switch (errno) {
  511. #if EAGAIN != SOCKET_EWOULDBLOCK
  512. case EAGAIN: /* EAGAIN == EWOULDBLOCK on some systems, but not others */
  513. #endif
  514. case SOCKET_EWOULDBLOCK:
  515. return WOLFSSL_CBIO_ERR_WANT_READ;
  516. case SOCKET_ECONNRESET:
  517. return WOLFSSL_CBIO_ERR_CONN_RST;
  518. case SOCKET_EINTR:
  519. return WOLFSSL_CBIO_ERR_ISR;
  520. case SOCKET_EPIPE:
  521. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  522. default:
  523. return WOLFSSL_CBIO_ERR_GENERAL;
  524. }
  525. }
  526. else if (sent == 0) {
  527. return 0;
  528. }
  529. return sent;
  530. }
  531. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  532. static int ReceiveFrom(WOLFSSL *ssl, int sd, char *buf, int sz)
  533. {
  534. int recvd;
  535. int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
  536. struct sockaddr peer;
  537. socklen_t peerSz = 0;
  538. if (DoneHandShake) {
  539. dtls_timeout = 0;
  540. }
  541. if (!wolfSSL_get_using_nonblock(ssl)) {
  542. struct timeval timeout;
  543. XMEMSET(&timeout, 0, sizeof(timeout));
  544. timeout.tv_sec = dtls_timeout;
  545. if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
  546. sizeof(timeout)) != 0) {
  547. fprintf(stderr, "setsockopt rcvtimeo failed\n");
  548. }
  549. }
  550. recvd = (int)recvfrom(sd, buf, sz, 0, (SOCKADDR*)&peer, &peerSz);
  551. if (recvd < 0) {
  552. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  553. if (wolfSSL_dtls_get_using_nonblock(ssl)) {
  554. return WOLFSSL_CBIO_ERR_WANT_READ;
  555. }
  556. else {
  557. return WOLFSSL_CBIO_ERR_TIMEOUT;
  558. }
  559. }
  560. else if (errno == SOCKET_ECONNRESET) {
  561. return WOLFSSL_CBIO_ERR_CONN_RST;
  562. }
  563. else if (errno == SOCKET_EINTR) {
  564. return WOLFSSL_CBIO_ERR_ISR;
  565. }
  566. else if (errno == SOCKET_ECONNREFUSED) {
  567. return WOLFSSL_CBIO_ERR_WANT_READ;
  568. }
  569. else {
  570. return WOLFSSL_CBIO_ERR_GENERAL;
  571. }
  572. }
  573. else {
  574. if (recvd == 0) {
  575. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  576. }
  577. }
  578. return recvd;
  579. }
  580. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
  581. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  582. static int SendTo(int sd, char *buf, int sz, const struct sockaddr *peer,
  583. socklen_t peerSz)
  584. {
  585. int sent;
  586. sent = (int)sendto(sd, buf, sz, 0, peer, peerSz);
  587. if (sent < 0) {
  588. if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
  589. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  590. }
  591. else if (errno == SOCKET_ECONNRESET) {
  592. return WOLFSSL_CBIO_ERR_CONN_RST;
  593. }
  594. else if (errno == SOCKET_EINTR) {
  595. return WOLFSSL_CBIO_ERR_ISR;
  596. }
  597. else if (errno == SOCKET_EPIPE) {
  598. return WOLFSSL_CBIO_ERR_CONN_CLOSE;
  599. }
  600. else {
  601. return WOLFSSL_CBIO_ERR_GENERAL;
  602. }
  603. }
  604. return sent;
  605. }
  606. static int myDoneHsCb(WOLFSSL* ssl, void* user_ctx)
  607. {
  608. (void) ssl;
  609. (void) user_ctx;
  610. DoneHandShake = 1;
  611. return 1;
  612. }
  613. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_CLIENT */
  614. #ifndef NO_WOLFSSL_SERVER
  615. static int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  616. {
  617. info_t* info = (info_t*)ctx;
  618. (void)ssl;
  619. #ifndef SINGLE_THREADED
  620. if (info->useLocalMem) {
  621. return ServerMemSend(info, buf, sz);
  622. }
  623. #endif
  624. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_CLIENT)
  625. if (info->doDTLS) {
  626. return SendTo(info->server.sockFd, buf, sz,
  627. (const struct sockaddr*)&info->clientAddr, sizeof(info->clientAddr));
  628. }
  629. else
  630. #endif
  631. {
  632. return SocketSend(info->server.sockFd, buf, sz);
  633. }
  634. }
  635. static int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  636. {
  637. info_t* info = (info_t*)ctx;
  638. (void)ssl;
  639. #ifndef SINGLE_THREADED
  640. if (info->useLocalMem) {
  641. return ServerMemRecv(info, buf, sz);
  642. }
  643. #endif
  644. #ifdef WOLFSSL_DTLS
  645. if (info->doDTLS) {
  646. return ReceiveFrom(ssl, info->server.sockFd, buf, sz);
  647. }
  648. else
  649. #endif
  650. {
  651. return SocketRecv(info->server.sockFd, buf, sz);
  652. }
  653. }
  654. #endif /* !NO_WOLFSSL_SERVER */
  655. #ifndef NO_WOLFSSL_CLIENT
  656. static int ClientSend(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 ClientMemSend(info, buf, sz);
  663. }
  664. #endif
  665. #ifdef WOLFSSL_DTLS
  666. if (info->doDTLS) {
  667. return SendTo(info->client.sockFd, buf, sz,
  668. (const struct sockaddr*)&info->serverAddr, sizeof(info->serverAddr));
  669. }
  670. else
  671. #endif
  672. {
  673. return SocketSend(info->client.sockFd, buf, sz);
  674. }
  675. }
  676. static int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  677. {
  678. info_t* info = (info_t*)ctx;
  679. (void)ssl;
  680. #ifndef SINGLE_THREADED
  681. if (info->useLocalMem) {
  682. return ClientMemRecv(info, buf, sz);
  683. }
  684. #endif
  685. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  686. if (info->doDTLS) {
  687. return ReceiveFrom(ssl, info->client.sockFd, buf, sz);
  688. }
  689. else
  690. #endif
  691. {
  692. return SocketRecv(info->client.sockFd, buf, sz);
  693. }
  694. }
  695. #endif /* !NO_WOLFSSL_CLIENT */
  696. static void CloseAndCleanupSocket(int* sockFd)
  697. {
  698. if (*sockFd != -1) {
  699. close(*sockFd);
  700. *sockFd = -1;
  701. }
  702. #ifdef WOLFSSL_DTLS
  703. DoneHandShake = 0;
  704. #endif
  705. }
  706. #ifdef BENCH_USE_NONBLOCK
  707. static int SetSocketNonBlocking(int sockFd)
  708. {
  709. int flags = fcntl(sockFd, F_GETFL, 0);
  710. if (flags < 0) {
  711. fprintf(stderr, "fcntl get failed\n");
  712. return -1;
  713. }
  714. flags = fcntl(sockFd, F_SETFL, flags | O_NONBLOCK);
  715. if (flags < 0) {
  716. fprintf(stderr, "fcntl set failed\n");
  717. return -1;
  718. }
  719. return 0;
  720. }
  721. #endif
  722. #ifndef NO_WOLFSSL_CLIENT
  723. static int SetupSocketAndConnect(info_t* info, const char* host,
  724. word32 port)
  725. {
  726. struct sockaddr_in servAddr;
  727. struct hostent* entry;
  728. /* Setup server address */
  729. XMEMSET(&servAddr, 0, sizeof(servAddr));
  730. servAddr.sin_family = AF_INET;
  731. servAddr.sin_port = htons(port);
  732. /* Resolve host */
  733. entry = gethostbyname(host);
  734. if (entry) {
  735. XMEMCPY(&servAddr.sin_addr.s_addr, entry->h_addr_list[0],
  736. entry->h_length);
  737. }
  738. else {
  739. servAddr.sin_addr.s_addr = inet_addr(host);
  740. }
  741. #ifdef WOLFSSL_DTLS
  742. if (info->doDTLS) {
  743. /* Create the SOCK_DGRAM socket type is implemented on the User
  744. * Datagram Protocol/Internet Protocol(UDP/IP protocol).*/
  745. if ((info->client.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  746. fprintf(stderr, "ERROR: failed to create the SOCK_DGRAM socket\n");
  747. return -1;
  748. }
  749. XMEMCPY(&info->serverAddr, &servAddr, sizeof(servAddr));
  750. }
  751. else
  752. #endif
  753. {
  754. /* Create a socket that uses an Internet IPv4 address,
  755. * Sets the socket to be stream based (TCP),
  756. * 0 means choose the default protocol. */
  757. if ((info->client.sockFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  758. fprintf(stderr, "ERROR: failed to create the socket\n");
  759. return -1;
  760. }
  761. /* Connect to the server */
  762. #ifndef SINGLE_THREADED
  763. while ((info->serverListening == 0) && (info->server.shutdown == 0)) {
  764. if (info->showVerbose) {
  765. fprintf(stderr, "Waiting for server to listen...\n");
  766. }
  767. XSLEEP_MS(1);
  768. }
  769. #endif
  770. if (info->server.shutdown == 1) {
  771. fprintf(stderr, "ERROR: server side has shutdown\n");
  772. return -1;
  773. }
  774. if (connect(info->client.sockFd, (struct sockaddr*)&servAddr,
  775. sizeof(servAddr)) == -1) {
  776. fprintf(stderr, "ERROR: failed to connect\n");
  777. return -1;
  778. }
  779. }
  780. #ifdef BENCH_USE_NONBLOCK
  781. if (SetSocketNonBlocking(info->client.sockFd) != 0) {
  782. return -1;
  783. }
  784. #endif
  785. if (info->showVerbose) {
  786. fprintf(stderr, "Connected to %s on port %d\n", host, port);
  787. }
  788. return 0;
  789. }
  790. static int bench_tls_client(info_t* info)
  791. {
  792. byte *writeBuf = NULL, *readBuf = NULL;
  793. double start, total = 0;
  794. int ret, readBufSz;
  795. WOLFSSL_CTX* cli_ctx = NULL;
  796. WOLFSSL* cli_ssl = NULL;
  797. int haveShownPeerInfo = 0;
  798. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  799. int total_sz;
  800. total = gettime_secs(0);
  801. /* set up client */
  802. #ifdef WOLFSSL_DTLS
  803. if (info->doDTLS) {
  804. if (tls13) {
  805. #ifdef WOLFSSL_DTLS13
  806. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_3_client_method());
  807. #endif
  808. }
  809. else {
  810. #ifndef WOLFSSL_NO_TLS12
  811. cli_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  812. #endif
  813. }
  814. }
  815. else
  816. #endif
  817. {
  818. #ifdef WOLFSSL_TLS13
  819. if (tls13) {
  820. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  821. }
  822. else
  823. #endif
  824. {
  825. #if !defined(WOLFSSL_TLS13)
  826. cli_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  827. #elif !defined(WOLFSSL_NO_TLS12)
  828. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  829. #endif
  830. }
  831. }
  832. if (cli_ctx == NULL) {
  833. fprintf(stderr, "error creating ctx\n");
  834. ret = MEMORY_E; goto exit;
  835. }
  836. #ifndef NO_CERTS
  837. #ifdef HAVE_ECC
  838. if (XSTRSTR(info->cipher, "ECDSA")) {
  839. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_ecc_cert_der_256,
  840. sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1);
  841. }
  842. else
  843. #endif
  844. {
  845. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_cert_der_2048,
  846. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  847. }
  848. if (ret != WOLFSSL_SUCCESS) {
  849. fprintf(stderr, "error loading CA\n");
  850. goto exit;
  851. }
  852. #endif
  853. wolfSSL_CTX_SetIOSend(cli_ctx, ClientSend);
  854. wolfSSL_CTX_SetIORecv(cli_ctx, ClientRecv);
  855. /* set cipher suite */
  856. ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher);
  857. if (ret != WOLFSSL_SUCCESS) {
  858. fprintf(stderr, "error setting cipher suite\n");
  859. goto exit;
  860. }
  861. #ifndef NO_DH
  862. ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS);
  863. if (ret != WOLFSSL_SUCCESS) {
  864. fprintf(stderr, "Error setting minimum DH key size\n");
  865. goto exit;
  866. }
  867. #endif
  868. #ifndef NO_PSK
  869. wolfSSL_CTX_set_psk_client_callback(cli_ctx, my_psk_client_cb);
  870. #ifdef WOLFSSL_TLS13
  871. #if !defined(WOLFSSL_PSK_TLS13_CB) && !defined(WOLFSSL_PSK_ONE_ID)
  872. wolfSSL_CTX_set_psk_client_cs_callback(cli_ctx, my_psk_client_cs_cb);
  873. #else
  874. wolfSSL_CTX_set_psk_client_tls13_callback(cli_ctx, my_psk_client_tls13_cb);
  875. #endif
  876. #endif
  877. wolfSSL_CTX_set_psk_callback_ctx(cli_ctx, (void*)info->cipher);
  878. #endif /* !NO_PSK */
  879. /* Allocate and initialize a packet sized buffer */
  880. writeBuf = (unsigned char*)XMALLOC(info->packetSize, NULL,
  881. DYNAMIC_TYPE_TMP_BUFFER);
  882. if (writeBuf == NULL) {
  883. fprintf(stderr, "failed to allocate write memory\n");
  884. ret = MEMORY_E; goto exit;
  885. }
  886. /* Allocate read buffer */
  887. readBufSz = info->packetSize;
  888. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  889. if (readBuf == NULL) {
  890. fprintf(stderr, "failed to allocate read memory\n");
  891. ret = MEMORY_E; goto exit;
  892. }
  893. /* BENCHMARK CONNECTIONS LOOP */
  894. while (!info->client.shutdown) {
  895. int writeSz = info->packetSize;
  896. #ifdef BENCH_USE_NONBLOCK
  897. int err;
  898. #endif
  899. #ifndef SINGLE_THREADED
  900. if (!info->useLocalMem)
  901. #endif
  902. {
  903. /* Setup socket and connection */
  904. ret = SetupSocketAndConnect(info, info->host, info->port);
  905. if (ret != 0) goto exit;
  906. }
  907. cli_ssl = wolfSSL_new(cli_ctx);
  908. if (cli_ssl == NULL) {
  909. fprintf(stderr, "error creating client object\n");
  910. goto exit;
  911. }
  912. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  913. if (info->group != 0) {
  914. ret = wolfSSL_UseKeyShare(cli_ssl, info->group);
  915. if (ret != WOLFSSL_SUCCESS) {
  916. fprintf(stderr, "error setting client key share.\n");
  917. goto exit;
  918. }
  919. }
  920. #endif
  921. #ifdef WOLFSSL_DTLS
  922. if (info->doDTLS) {
  923. ret = wolfSSL_dtls_set_peer(cli_ssl, &info->serverAddr,
  924. sizeof(info->serverAddr));
  925. if (ret != WOLFSSL_SUCCESS) {
  926. fprintf(stderr, "error setting dtls peer\n");
  927. goto exit;
  928. }
  929. ret = wolfSSL_SetHsDoneCb(cli_ssl, myDoneHsCb, NULL);
  930. if (ret != WOLFSSL_SUCCESS) {
  931. fprintf(stderr, "error handshake done callback\n");
  932. goto exit;
  933. }
  934. }
  935. #endif
  936. wolfSSL_SetIOReadCtx(cli_ssl, info);
  937. wolfSSL_SetIOWriteCtx(cli_ssl, info);
  938. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_DTLS)
  939. /* synchronize with server */
  940. if (info->doDTLS && !info->clientOrserverOnly) {
  941. THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
  942. if (info->serverReady != 1) {
  943. THREAD_CHECK_RET(wolfSSL_CondWait(&info->dtls_cond));
  944. }
  945. /* for next loop */
  946. info->serverReady = 0;
  947. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
  948. }
  949. #endif
  950. /* perform connect */
  951. start = gettime_secs(1);
  952. #ifndef BENCH_USE_NONBLOCK
  953. ret = wolfSSL_connect(cli_ssl);
  954. #else
  955. do
  956. {
  957. ret = wolfSSL_connect(cli_ssl);
  958. err = wolfSSL_get_error(cli_ssl, ret);
  959. }
  960. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  961. #endif
  962. start = gettime_secs(0) - start;
  963. if (ret != WOLFSSL_SUCCESS) {
  964. fprintf(stderr, "error connecting client\n");
  965. ret = wolfSSL_get_error(cli_ssl, ret);
  966. goto exit;
  967. }
  968. info->client_stats.connTime += start;
  969. info->client_stats.connCount++;
  970. if ((info->showPeerInfo) && (!haveShownPeerInfo)) {
  971. haveShownPeerInfo = 1;
  972. showPeer(cli_ssl);
  973. }
  974. /* check for run time completion and issue shutdown */
  975. if (gettime_secs(0) - total >= info->runTimeSec) {
  976. info->client.shutdown = 1;
  977. writeSz = (int)XSTRLEN(kShutdown) + 1;
  978. XMEMCPY(writeBuf, kShutdown, writeSz); /* include null term */
  979. if (info->showVerbose) {
  980. fprintf(stderr, "Sending shutdown\n");
  981. }
  982. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  983. if (ret < 0) {
  984. fprintf(stderr, "error on client write\n");
  985. ret = wolfSSL_get_error(cli_ssl, ret);
  986. goto exit;
  987. }
  988. }
  989. else {
  990. XMEMSET(writeBuf, 0, info->packetSize);
  991. XSTRNCPY((char*)writeBuf, kTestStr, info->packetSize);
  992. }
  993. /* write / read echo loop */
  994. ret = 0;
  995. total_sz = 0;
  996. while (ret == 0 && total_sz < info->maxSize && !info->client.shutdown) {
  997. /* write test message to server */
  998. start = gettime_secs(1);
  999. #ifndef BENCH_USE_NONBLOCK
  1000. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  1001. #else
  1002. do {
  1003. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  1004. err = wolfSSL_get_error(cli_ssl, ret);
  1005. }
  1006. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1007. #endif
  1008. info->client_stats.txTime += gettime_secs(0) - start;
  1009. if (ret < 0) {
  1010. fprintf(stderr, "error on client write\n");
  1011. ret = wolfSSL_get_error(cli_ssl, ret);
  1012. goto exit;
  1013. }
  1014. info->client_stats.txTotal += ret;
  1015. total_sz += ret;
  1016. /* read echo of message from server */
  1017. XMEMSET(readBuf, 0, readBufSz);
  1018. start = gettime_secs(1);
  1019. #ifndef BENCH_USE_NONBLOCK
  1020. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  1021. #else
  1022. do {
  1023. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  1024. err = wolfSSL_get_error(cli_ssl, ret);
  1025. }
  1026. while (err == WOLFSSL_ERROR_WANT_READ);
  1027. #endif
  1028. info->client_stats.rxTime += gettime_secs(0) - start;
  1029. if (ret < 0) {
  1030. fprintf(stderr, "error on client read\n");
  1031. ret = wolfSSL_get_error(cli_ssl, ret);
  1032. goto exit;
  1033. }
  1034. info->client_stats.rxTotal += ret;
  1035. ret = 0; /* reset return code */
  1036. /* validate echo */
  1037. if (XMEMCMP((char*)writeBuf, (char*)readBuf, writeSz) != 0) {
  1038. fprintf(stderr, "echo check failed!\n");
  1039. ret = wolfSSL_get_error(cli_ssl, ret);
  1040. goto exit;
  1041. }
  1042. }
  1043. CloseAndCleanupSocket(&info->client.sockFd);
  1044. wolfSSL_free(cli_ssl);
  1045. cli_ssl = NULL;
  1046. }
  1047. exit:
  1048. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1049. fprintf(stderr, "Client Error: %d (%s)\n", ret,
  1050. wolfSSL_ERR_reason_error_string(ret));
  1051. }
  1052. /* clean up */
  1053. CloseAndCleanupSocket(&info->client.sockFd);
  1054. if (cli_ssl != NULL) {
  1055. wolfSSL_free(cli_ssl);
  1056. }
  1057. if (cli_ctx != NULL) {
  1058. wolfSSL_CTX_free(cli_ctx);
  1059. }
  1060. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1061. XFREE(writeBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1062. info->client.ret = ret;
  1063. (void)tls13;
  1064. return ret;
  1065. }
  1066. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
  1067. static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args)
  1068. {
  1069. int ret;
  1070. info_t* info = (info_t*)args;
  1071. ret = bench_tls_client(info);
  1072. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_server.cond));
  1073. info->to_client.done = 1;
  1074. info->client.ret = ret;
  1075. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond));
  1076. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond));
  1077. WOLFSSL_RETURN_FROM_THREAD(0);
  1078. }
  1079. #endif /* !SINGLE_THREADED */
  1080. #endif /* !NO_WOLFSSL_CLIENT */
  1081. #ifndef NO_WOLFSSL_SERVER
  1082. static int SetupSocketAndListen(int* listenFd, word32 port, int doDTLS)
  1083. {
  1084. struct sockaddr_in servAddr;
  1085. #if defined(_MSC_VER) || defined(__MINGW32__)
  1086. char optval = 1;
  1087. #else
  1088. int optval = 1;
  1089. #endif
  1090. #ifndef WOLFSSL_DTLS
  1091. (void) doDTLS;
  1092. #endif
  1093. /* Setup server address */
  1094. XMEMSET(&servAddr, 0, sizeof(servAddr));
  1095. servAddr.sin_family = AF_INET;
  1096. servAddr.sin_port = htons(port);
  1097. servAddr.sin_addr.s_addr = INADDR_ANY;
  1098. #ifdef WOLFSSL_DTLS
  1099. if (doDTLS) {
  1100. /* Create a socket that is implemented on the User Datagram Protocol/
  1101. * Interet Protocol(UDP/IP protocol). */
  1102. if((*listenFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  1103. fprintf(stderr, "ERROR: failed to create the socket\n");
  1104. return -1;
  1105. }
  1106. }
  1107. else
  1108. #endif
  1109. {
  1110. /* Create a socket that uses an Internet IPv4 address,
  1111. * Sets the socket to be stream based (TCP),
  1112. * 0 means choose the default protocol. */
  1113. if ((*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  1114. fprintf(stderr, "ERROR: failed to create the socket\n");
  1115. return -1;
  1116. }
  1117. /* allow reuse */
  1118. if (setsockopt(*listenFd, SOL_SOCKET, SO_REUSEADDR,
  1119. &optval, sizeof(optval)) == -1) {
  1120. fprintf(stderr, "setsockopt SO_REUSEADDR failed\n");
  1121. return -1;
  1122. }
  1123. /* Listen for the client. */
  1124. if (bind(*listenFd, (struct sockaddr*)&servAddr,
  1125. sizeof(servAddr)) == -1) {
  1126. fprintf(stderr, "ERROR: failed to bind\n");
  1127. return -1;
  1128. }
  1129. }
  1130. if (listen(*listenFd, 5) != 0) {
  1131. fprintf(stderr, "ERROR: failed to listen\n");
  1132. return -1;
  1133. }
  1134. #ifdef BENCH_USE_NONBLOCK
  1135. if (SetSocketNonBlocking(*listenFd) != 0) {
  1136. return -1;
  1137. }
  1138. #endif
  1139. return 0;
  1140. }
  1141. static int SocketWaitClient(info_t* info)
  1142. {
  1143. int connd;
  1144. struct sockaddr_in clientAddr;
  1145. socklen_t size = sizeof(clientAddr);
  1146. #ifdef WOLFSSL_DTLS
  1147. if (info->doDTLS) {
  1148. char msg[64];
  1149. #ifndef SINGLE_THREADED
  1150. if (!info->clientOrserverOnly) {
  1151. THREAD_CHECK_RET(wolfSSL_CondStart(&info->dtls_cond));
  1152. info->serverReady = 1;
  1153. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->dtls_cond));
  1154. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->dtls_cond));
  1155. }
  1156. #endif
  1157. connd = (int)recvfrom(info->listenFd, (char *)msg, sizeof(msg),
  1158. MSG_PEEK, (struct sockaddr*)&clientAddr, &size);
  1159. if (connd < -1) {
  1160. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1161. return -1;
  1162. }
  1163. XMEMCPY(&info->clientAddr, &clientAddr, sizeof(clientAddr));
  1164. info->server.sockFd = info->listenFd;
  1165. }
  1166. else
  1167. #endif
  1168. {
  1169. #ifndef SINGLE_THREADED
  1170. info->serverListening = 1;
  1171. #endif
  1172. if ((connd = accept(info->listenFd, (struct sockaddr*)&clientAddr,
  1173. &size)) == -1) {
  1174. if (errno == SOCKET_EWOULDBLOCK) {
  1175. return -2;
  1176. }
  1177. fprintf(stderr, "ERROR: failed to accept the connection\n");
  1178. return -1;
  1179. }
  1180. info->server.sockFd = connd;
  1181. }
  1182. if (info->showVerbose) {
  1183. fprintf(stderr, "Got client %d\n", connd);
  1184. }
  1185. return 0;
  1186. }
  1187. static void CloseAndCleanupListenSocket(int* listenFd)
  1188. {
  1189. if (*listenFd != -1) {
  1190. close(*listenFd);
  1191. *listenFd = -1;
  1192. }
  1193. }
  1194. static int bench_tls_server(info_t* info)
  1195. {
  1196. byte *readBuf = NULL;
  1197. double start;
  1198. int ret, len = 0, readBufSz;
  1199. WOLFSSL_CTX* srv_ctx = NULL;
  1200. WOLFSSL* srv_ssl = NULL;
  1201. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  1202. int total_sz;
  1203. /* set up server */
  1204. #ifdef WOLFSSL_DTLS
  1205. if (info->doDTLS) {
  1206. if (tls13) {
  1207. #ifdef WOLFSSL_DTLS13
  1208. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_3_server_method());
  1209. #endif
  1210. }
  1211. else {
  1212. #ifndef WOLFSSL_NO_TLS12
  1213. srv_ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  1214. #endif
  1215. }
  1216. }
  1217. else
  1218. #endif
  1219. {
  1220. #ifdef WOLFSSL_TLS13
  1221. if (tls13) {
  1222. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1223. }
  1224. else
  1225. #endif
  1226. {
  1227. #if !defined(WOLFSSL_TLS13)
  1228. srv_ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1229. #elif !defined(WOLFSSL_NO_TLS12)
  1230. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  1231. #endif
  1232. }
  1233. }
  1234. if (srv_ctx == NULL) {
  1235. fprintf(stderr, "error creating server ctx\n");
  1236. ret = MEMORY_E; goto exit;
  1237. }
  1238. #ifndef NO_CERTS
  1239. #ifdef HAVE_ECC
  1240. if (XSTRSTR(info->cipher, "ECDSA")) {
  1241. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, ecc_key_der_256,
  1242. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  1243. }
  1244. else
  1245. #endif
  1246. {
  1247. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, server_key_der_2048,
  1248. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  1249. }
  1250. if (ret != WOLFSSL_SUCCESS) {
  1251. fprintf(stderr, "error loading server key\n");
  1252. goto exit;
  1253. }
  1254. #ifdef HAVE_ECC
  1255. if (XSTRSTR(info->cipher, "ECDSA")) {
  1256. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, serv_ecc_der_256,
  1257. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  1258. }
  1259. else
  1260. #endif
  1261. {
  1262. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, server_cert_der_2048,
  1263. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  1264. }
  1265. if (ret != WOLFSSL_SUCCESS) {
  1266. fprintf(stderr, "error loading server cert\n");
  1267. goto exit;
  1268. }
  1269. #endif /* !NO_CERTS */
  1270. wolfSSL_CTX_SetIOSend(srv_ctx, ServerSend);
  1271. wolfSSL_CTX_SetIORecv(srv_ctx, ServerRecv);
  1272. /* set cipher suite */
  1273. ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher);
  1274. if (ret != WOLFSSL_SUCCESS) {
  1275. fprintf(stderr, "error setting cipher suite\n");
  1276. goto exit;
  1277. }
  1278. #ifndef NO_DH
  1279. ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS);
  1280. if (ret != WOLFSSL_SUCCESS) {
  1281. fprintf(stderr, "Error setting minimum DH key size\n");
  1282. goto exit;
  1283. }
  1284. #endif
  1285. #ifndef NO_PSK
  1286. wolfSSL_CTX_set_psk_server_callback(srv_ctx, my_psk_server_cb);
  1287. #ifdef WOLFSSL_TLS13
  1288. wolfSSL_CTX_set_psk_server_tls13_callback(srv_ctx, my_psk_server_tls13_cb);
  1289. #endif
  1290. #endif /* !NO_PSK */
  1291. /* Allocate read buffer */
  1292. readBufSz = info->packetSize;
  1293. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1294. if (readBuf == NULL) {
  1295. fprintf(stderr, "failed to allocate read memory\n");
  1296. ret = MEMORY_E; goto exit;
  1297. }
  1298. /* BENCHMARK CONNECTIONS LOOP */
  1299. while (!info->server.shutdown) {
  1300. #ifdef BENCH_USE_NONBLOCK
  1301. int err;
  1302. #endif
  1303. #ifndef SINGLE_THREADED
  1304. if (!info->useLocalMem)
  1305. #endif
  1306. {
  1307. /* Accept client connections */
  1308. ret = SocketWaitClient(info);
  1309. #ifdef BENCH_USE_NONBLOCK
  1310. if (ret == -2) {
  1311. XSLEEP_MS(0);
  1312. continue;
  1313. }
  1314. #endif
  1315. if (ret != 0) {
  1316. goto exit;
  1317. }
  1318. }
  1319. srv_ssl = wolfSSL_new(srv_ctx);
  1320. if (srv_ssl == NULL) {
  1321. fprintf(stderr, "error creating server object\n");
  1322. ret = MEMORY_E; goto exit;
  1323. }
  1324. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1325. if (info->group != 0) {
  1326. ret = wolfSSL_UseKeyShare(srv_ssl, info->group);
  1327. if (ret != WOLFSSL_SUCCESS) {
  1328. fprintf(stderr, "error setting server key share.\n");
  1329. goto exit;
  1330. }
  1331. }
  1332. #endif
  1333. #ifdef WOLFSSL_DTLS
  1334. if (info->doDTLS) {
  1335. ret = wolfSSL_dtls_set_peer(srv_ssl, &info->clientAddr,
  1336. sizeof(info->clientAddr));
  1337. if (ret != WOLFSSL_SUCCESS) {
  1338. fprintf(stderr, "error setting dtls peer\n");
  1339. goto exit;
  1340. }
  1341. }
  1342. #endif
  1343. wolfSSL_SetIOReadCtx(srv_ssl, info);
  1344. wolfSSL_SetIOWriteCtx(srv_ssl, info);
  1345. #ifndef NO_DH
  1346. wolfSSL_SetTmpDH(srv_ssl, dhp, sizeof(dhp), dhg, sizeof(dhg));
  1347. #endif
  1348. /* accept TLS connection */
  1349. start = gettime_secs(1);
  1350. #ifndef BENCH_USE_NONBLOCK
  1351. ret = wolfSSL_accept(srv_ssl);
  1352. #else
  1353. do {
  1354. ret = wolfSSL_accept(srv_ssl);
  1355. err = wolfSSL_get_error(srv_ssl, ret);
  1356. }
  1357. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1358. #endif
  1359. start = gettime_secs(0) - start;
  1360. if (ret != WOLFSSL_SUCCESS) {
  1361. #ifndef SINGLE_THREADED
  1362. if (info->to_client.done) {
  1363. ret = 0; /* done - success */
  1364. }
  1365. else
  1366. #endif
  1367. {
  1368. fprintf(stderr, "error on server accept\n");
  1369. ret = wolfSSL_get_error(srv_ssl, ret);
  1370. }
  1371. goto exit;
  1372. }
  1373. info->server_stats.connTime += start;
  1374. info->server_stats.connCount++;
  1375. /* echo loop */
  1376. ret = 0;
  1377. total_sz = 0;
  1378. while (ret == 0 && total_sz < info->maxSize) {
  1379. double rxTime;
  1380. /* read message from client */
  1381. XMEMSET(readBuf, 0, readBufSz);
  1382. start = gettime_secs(1);
  1383. #ifndef BENCH_USE_NONBLOCK
  1384. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1385. #else
  1386. do {
  1387. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1388. err = wolfSSL_get_error(srv_ssl, ret);
  1389. }
  1390. while (err == WOLFSSL_ERROR_WANT_READ);
  1391. #endif
  1392. rxTime = gettime_secs(0) - start;
  1393. /* shutdown signals, no more connections for this cipher */
  1394. if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) {
  1395. info->server.shutdown = 1;
  1396. if (info->showVerbose) {
  1397. fprintf(stderr, "Server shutdown done\n");
  1398. }
  1399. ret = 0; /* success */
  1400. break;
  1401. }
  1402. info->server_stats.rxTime += rxTime;
  1403. if (ret < 0) {
  1404. #ifndef SINGLE_THREADED
  1405. if (info->to_client.done) {
  1406. ret = 0; /* done - success */
  1407. }
  1408. else
  1409. #endif
  1410. {
  1411. fprintf(stderr, "error on server read\n");
  1412. ret = wolfSSL_get_error(srv_ssl, ret);
  1413. }
  1414. goto exit;
  1415. }
  1416. info->server_stats.rxTotal += ret;
  1417. len = ret;
  1418. total_sz += ret;
  1419. /* write message back to client */
  1420. start = gettime_secs(1);
  1421. #ifndef BENCH_USE_NONBLOCK
  1422. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1423. #else
  1424. do {
  1425. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1426. err = wolfSSL_get_error(srv_ssl, ret);
  1427. }
  1428. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1429. #endif
  1430. info->server_stats.txTime += gettime_secs(0) - start;
  1431. if (ret < 0) {
  1432. fprintf(stderr, "error on server write\n");
  1433. ret = wolfSSL_get_error(srv_ssl, ret);
  1434. goto exit;
  1435. }
  1436. info->server_stats.txTotal += ret;
  1437. ret = 0; /* reset return code */
  1438. }
  1439. CloseAndCleanupSocket(&info->server.sockFd);
  1440. wolfSSL_free(srv_ssl);
  1441. srv_ssl = NULL;
  1442. #ifdef WOLFSSL_DTLS
  1443. if (info->doDTLS) {
  1444. SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1445. }
  1446. #endif
  1447. }
  1448. exit:
  1449. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1450. fprintf(stderr, "Server Error: %d (%s)\n", ret,
  1451. wolfSSL_ERR_reason_error_string(ret));
  1452. }
  1453. /* clean up */
  1454. CloseAndCleanupSocket(&info->server.sockFd);
  1455. if (srv_ssl != NULL) {
  1456. wolfSSL_free(srv_ssl);
  1457. }
  1458. if (srv_ctx != NULL) {
  1459. wolfSSL_CTX_free(srv_ctx);
  1460. }
  1461. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1462. info->server.ret = ret;
  1463. (void)tls13;
  1464. return ret;
  1465. }
  1466. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
  1467. static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args)
  1468. {
  1469. int ret = 0;
  1470. info_t* info = (info_t*)args;
  1471. if (!info->useLocalMem) {
  1472. /* Setup TLS server listener */
  1473. #ifdef WOLFSSL_DTLS
  1474. ret = SetupSocketAndListen(&info->listenFd, info->port, info->doDTLS);
  1475. #else
  1476. ret = SetupSocketAndListen(&info->listenFd, info->port, 0);
  1477. #endif
  1478. }
  1479. if (ret == 0) {
  1480. ret = bench_tls_server(info);
  1481. if (!info->useLocalMem) {
  1482. CloseAndCleanupListenSocket(&info->listenFd);
  1483. }
  1484. }
  1485. THREAD_CHECK_RET(wolfSSL_CondStart(&info->to_client.cond));
  1486. info->to_server.done = 1;
  1487. info->server.ret = ret;
  1488. THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond));
  1489. THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond));
  1490. WOLFSSL_RETURN_FROM_THREAD(0);
  1491. }
  1492. #endif /* !SINGLE_THREADED */
  1493. #endif /* !NO_WOLFSSL_SERVER */
  1494. static void print_stats(stats_t* wcStat, const char* desc, const char* cipher, const char *group, int verbose)
  1495. {
  1496. if (verbose) {
  1497. fprintf(stderr,
  1498. "wolfSSL %s Benchmark on %s with group %s:\n"
  1499. "\tTotal : %9d bytes\n"
  1500. "\tNum Conns : %9d\n"
  1501. "\tRx Total : %9.3f ms\n"
  1502. "\tTx Total : %9.3f ms\n"
  1503. "\tRx : %9.3f MB/s\n"
  1504. "\tTx : %9.3f MB/s\n"
  1505. "\tConnect : %9.3f ms\n"
  1506. "\tConnect Avg : %9.3f ms\n",
  1507. desc,
  1508. cipher,
  1509. group,
  1510. wcStat->txTotal + wcStat->rxTotal,
  1511. wcStat->connCount,
  1512. wcStat->rxTime * 1000,
  1513. wcStat->txTime * 1000,
  1514. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1515. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1516. wcStat->connTime * 1000,
  1517. wcStat->connTime * 1000 / wcStat->connCount);
  1518. }
  1519. else {
  1520. fprintf(stderr,
  1521. "%-6s %-33s %-25s %11d %9d %9.3f %9.3f %9.3f "
  1522. "%9.3f %17.3f %15.3f\n",
  1523. desc,
  1524. cipher,
  1525. group,
  1526. wcStat->txTotal + wcStat->rxTotal,
  1527. wcStat->connCount,
  1528. wcStat->rxTime * 1000,
  1529. wcStat->txTime * 1000,
  1530. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  1531. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  1532. wcStat->connTime * 1000,
  1533. wcStat->connTime * 1000 / wcStat->connCount);
  1534. }
  1535. }
  1536. static void Usage(void)
  1537. {
  1538. fprintf(stderr, "tls_bench " LIBWOLFSSL_VERSION_STRING
  1539. " NOTE: All files relative to wolfSSL home dir\n");
  1540. fprintf(stderr, "-? Help, print this usage\n");
  1541. fprintf(stderr, "-c Run as client only, no threading and uses sockets\n");
  1542. fprintf(stderr, "-s Run as server only, no threading and uses sockets\n");
  1543. fprintf(stderr, "-h Host (default %s)\n", BENCH_DEFAULT_HOST);
  1544. fprintf(stderr, "-P Port (default %d)\n", BENCH_DEFAULT_PORT);
  1545. fprintf(stderr, "-e List Every cipher suite available\n");
  1546. fprintf(stderr, "-i Show peer info\n");
  1547. #ifdef WOLFSSL_TLS13
  1548. fprintf(stderr, "-g Run through each of the TLS 1.3 groups that are available\n");
  1549. #endif
  1550. fprintf(stderr, "-l <str> Cipher suite list (: delimited)\n");
  1551. fprintf(stderr, "-t <num> Time <num> (seconds) to run each test (default %d)\n", BENCH_RUNTIME_SEC);
  1552. fprintf(stderr, "-p <num> The packet size <num> in bytes [1-16kB] (default %d)\n", TEST_PACKET_SIZE);
  1553. #ifdef WOLFSSL_DTLS
  1554. fprintf(stderr, " In the case of DTLS, [1-8kB] (default %d)\n", TEST_DTLS_PACKET_SIZE);
  1555. #endif
  1556. fprintf(stderr, "-S <num> The total size <num> in bytes (default %d)\n", TEST_MAX_SIZE);
  1557. fprintf(stderr, "-v Show verbose output\n");
  1558. #ifdef DEBUG_WOLFSSL
  1559. fprintf(stderr, "-d Enable debug messages\n");
  1560. #endif
  1561. #ifndef SINGLE_THREADED
  1562. fprintf(stderr, "-T <num> Number of threaded server/client pairs (default %d)\n", NUM_THREAD_PAIRS);
  1563. fprintf(stderr, "-m Use local memory, not socket\n");
  1564. #endif
  1565. #ifdef WOLFSSL_DTLS
  1566. fprintf(stderr, "-u Use DTLS\n");
  1567. #endif
  1568. }
  1569. static void ShowCiphers(void)
  1570. {
  1571. char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE];
  1572. int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
  1573. if (ret == WOLFSSL_SUCCESS) {
  1574. fprintf(stderr, "%s\n", ciphers);
  1575. }
  1576. }
  1577. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1578. static int SetupSupportedGroups(int verbose)
  1579. {
  1580. int i;
  1581. WOLFSSL_CTX* ctx = NULL;
  1582. WOLFSSL* ssl = NULL;
  1583. int ret = 0;
  1584. if (ret == 0) {
  1585. #ifdef NO_WOLFSSL_CLIENT
  1586. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1587. #else
  1588. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  1589. #endif
  1590. if (ctx == NULL) {
  1591. ret = -1;
  1592. }
  1593. }
  1594. if (ret == 0) {
  1595. ssl = wolfSSL_new(ctx);
  1596. if (ssl == NULL) {
  1597. ret = -1;
  1598. }
  1599. }
  1600. /* Determine which groups are actually supported. */
  1601. for (i = 0; groups[i].name != NULL; i++) {
  1602. if (ret == 0) {
  1603. int uks_ret = wolfSSL_UseKeyShare(ssl, groups[i].group);
  1604. if (uks_ret == WOLFSSL_SUCCESS) {
  1605. if (verbose) {
  1606. printf("Will benchmark the following group: %s\n",
  1607. groups[i].name);
  1608. }
  1609. } else if (uks_ret == BAD_FUNC_ARG || uks_ret == NOT_COMPILED_IN) {
  1610. groups[i].group = 0;
  1611. if (verbose) {
  1612. printf("Will NOT benchmark the following group: %s\n",
  1613. groups[i].name);
  1614. }
  1615. } else {
  1616. ret = -1;
  1617. }
  1618. }
  1619. }
  1620. if (ssl != NULL) {
  1621. wolfSSL_free(ssl);
  1622. }
  1623. if (ctx != NULL) {
  1624. wolfSSL_CTX_free(ctx);
  1625. }
  1626. return ret;
  1627. }
  1628. #endif
  1629. int bench_tls(void* args)
  1630. {
  1631. int ret = 0;
  1632. info_t *theadInfo = NULL, *info;
  1633. stats_t cli_comb, srv_comb;
  1634. int i;
  1635. char *cipher, *next_cipher, *ciphers = NULL;
  1636. int argc = 0;
  1637. char** argv = NULL;
  1638. int ch;
  1639. /* Vars configured by command line arguments */
  1640. int argRuntimeSec = BENCH_RUNTIME_SEC;
  1641. char *argCipherList = NULL;
  1642. int argTestPacketSize = TEST_PACKET_SIZE;
  1643. int argTestMaxSize = TEST_MAX_SIZE;
  1644. int argThreadPairs = NUM_THREAD_PAIRS;
  1645. int argShowVerbose = SHOW_VERBOSE;
  1646. int argClientOnly = 0;
  1647. int argServerOnly = 0;
  1648. const char* argHost = BENCH_DEFAULT_HOST;
  1649. int argPort = BENCH_DEFAULT_PORT;
  1650. int argShowPeerInfo = 0;
  1651. #ifndef SINGLE_THREADED
  1652. int doShutdown;
  1653. #endif
  1654. #if !defined(NO_WOLFSSL_SERVER) || !defined(SINGLE_THREADED)
  1655. int argLocalMem = 0;
  1656. int listenFd = -1;
  1657. #endif
  1658. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1659. int option_p = 0;
  1660. #endif
  1661. #ifdef WOLFSSL_DTLS
  1662. int doDTLS = 0;
  1663. #endif
  1664. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1665. int group_index = 0;
  1666. int argDoGroups = 0;
  1667. #endif
  1668. if (args != NULL) {
  1669. argc = ((func_args*)args)->argc;
  1670. argv = ((func_args*)args)->argv;
  1671. ((func_args*)args)->return_code = -1; /* error state */
  1672. }
  1673. /* Initialize wolfSSL */
  1674. wolfSSL_Init();
  1675. /* Parse command line arguments */
  1676. while ((ch = mygetopt(argc, argv, "?" "udeil:p:t:vT:sch:P:mS:g")) != -1) {
  1677. switch (ch) {
  1678. case '?' :
  1679. Usage();
  1680. goto exit;
  1681. case 's':
  1682. argServerOnly = 1;
  1683. break;
  1684. case 'c':
  1685. argClientOnly = 1;
  1686. break;
  1687. case 'h':
  1688. argHost = myoptarg;
  1689. break;
  1690. case 'P':
  1691. argPort = atoi(myoptarg);
  1692. break;
  1693. case 'd' :
  1694. #ifdef DEBUG_WOLFSSL
  1695. wolfSSL_Debugging_ON();
  1696. #endif
  1697. break;
  1698. case 'e' :
  1699. ShowCiphers();
  1700. goto exit;
  1701. case 'g' :
  1702. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1703. argDoGroups = 1;
  1704. break;
  1705. #else
  1706. fprintf(stderr, "There are only groups in TLS 1.3\n");
  1707. Usage();
  1708. ret = MY_EX_USAGE; goto exit;
  1709. #endif
  1710. case 'i' :
  1711. argShowPeerInfo = 1;
  1712. break;
  1713. case 'l' :
  1714. argCipherList = myoptarg;
  1715. break;
  1716. case 'p' :
  1717. argTestPacketSize = atoi(myoptarg);
  1718. if (argTestPacketSize > (16 * 1024)) {
  1719. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1720. Usage();
  1721. ret = MY_EX_USAGE; goto exit;
  1722. }
  1723. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1724. option_p = 1;
  1725. #endif
  1726. break;
  1727. case 'S' :
  1728. argTestMaxSize = atoi(myoptarg);
  1729. break;
  1730. case 't' :
  1731. argRuntimeSec = atoi(myoptarg);
  1732. break;
  1733. case 'v' :
  1734. argShowVerbose = 1;
  1735. break;
  1736. case 'T' :
  1737. #ifndef SINGLE_THREADED
  1738. argThreadPairs = atoi(myoptarg);
  1739. #endif
  1740. break;
  1741. case 'm':
  1742. #ifndef SINGLE_THREADED
  1743. argLocalMem = 1;
  1744. #endif
  1745. break;
  1746. case 'u':
  1747. #ifdef WOLFSSL_DTLS
  1748. doDTLS = 1;
  1749. #ifdef BENCH_USE_NONBLOCK
  1750. fprintf(stderr, "tls_bench hasn't yet supported DTLS "
  1751. "non-blocking mode.\n");
  1752. Usage();
  1753. ret = MY_EX_USAGE; goto exit;
  1754. #endif
  1755. #endif
  1756. break;
  1757. default:
  1758. Usage();
  1759. ret = MY_EX_USAGE; goto exit;
  1760. }
  1761. }
  1762. /* reset for test cases */
  1763. myoptind = 0;
  1764. if (argCipherList != NULL) {
  1765. /* Use the list from CL argument */
  1766. cipher = argCipherList;
  1767. }
  1768. else {
  1769. /* Run for each cipher */
  1770. ciphers = (char*)XMALLOC(WOLFSSL_CIPHER_LIST_MAX_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1771. if (ciphers == NULL) {
  1772. goto exit;
  1773. }
  1774. wolfSSL_get_ciphers(ciphers, WOLFSSL_CIPHER_LIST_MAX_SIZE);
  1775. cipher = ciphers;
  1776. }
  1777. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1778. if (argDoGroups) {
  1779. if (SetupSupportedGroups(argShowVerbose) != 0) {
  1780. goto exit;
  1781. }
  1782. }
  1783. #endif
  1784. /* for server or client side only, only 1 thread is allowed */
  1785. if (argServerOnly || argClientOnly) {
  1786. argThreadPairs = 1;
  1787. }
  1788. #ifdef SINGLE_THREADED
  1789. else {
  1790. fprintf(stderr, "Threading is not enabled, so please use -s or -c to indicate side\n");
  1791. Usage();
  1792. ret = MY_EX_USAGE; goto exit;
  1793. }
  1794. #endif
  1795. /* Allocate test info array */
  1796. theadInfo = (info_t*)XMALLOC(sizeof(info_t) * argThreadPairs, NULL,
  1797. DYNAMIC_TYPE_TMP_BUFFER);
  1798. if (theadInfo == NULL) {
  1799. ret = MEMORY_E; goto exit;
  1800. }
  1801. XMEMSET(theadInfo, 0, sizeof(info_t) * argThreadPairs);
  1802. #ifndef NO_WOLFSSL_SERVER
  1803. /* Use same listen socket to avoid timing issues between client and server */
  1804. if (argServerOnly && !argLocalMem) {
  1805. /* Setup TLS server listener */
  1806. #ifdef WOLFSSL_DTLS
  1807. ret = SetupSocketAndListen(&listenFd, argPort, doDTLS);
  1808. #else
  1809. ret = SetupSocketAndListen(&listenFd, argPort, 0);
  1810. #endif
  1811. if (ret != 0) {
  1812. goto exit;
  1813. }
  1814. }
  1815. #endif
  1816. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  1817. if (doDTLS) {
  1818. if (argLocalMem) {
  1819. fprintf(stderr, "tls_bench hasn't yet supported DTLS with local memory.\n");
  1820. ret = MY_EX_USAGE; goto exit;
  1821. }
  1822. if (option_p && argTestPacketSize > TEST_DTLS_PACKET_SIZE){
  1823. fprintf(stderr, "Invalid packet size %d\n", argTestPacketSize);
  1824. Usage();
  1825. ret = MY_EX_USAGE; goto exit;
  1826. } else {
  1827. /* argTestPacketSize would be default for tcp packet */
  1828. if (argTestPacketSize >= TEST_PACKET_SIZE)
  1829. argTestPacketSize = TEST_DTLS_PACKET_SIZE;
  1830. }
  1831. }
  1832. #endif
  1833. fprintf(stderr, "Running TLS Benchmarks...\n");
  1834. /* parse by : */
  1835. while ((cipher != NULL) && (cipher[0] != '\0')) {
  1836. #if ! (defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES))
  1837. const char *gname = "N/A";
  1838. #endif
  1839. next_cipher = strchr(cipher, ':');
  1840. if (next_cipher != NULL) {
  1841. cipher[next_cipher - cipher] = '\0';
  1842. }
  1843. if (argShowVerbose) {
  1844. fprintf(stderr, "Cipher: %s\n", cipher);
  1845. }
  1846. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1847. for (group_index = 0; groups[group_index].name != NULL; group_index++) {
  1848. const char *gname = theadInfo[0].group == 0 ? "N/A"
  1849. : groups[group_index].name;
  1850. if (argDoGroups && groups[group_index].group == 0) {
  1851. /* Skip unsupported group. */
  1852. continue;
  1853. }
  1854. #endif
  1855. for (i=0; i<argThreadPairs; i++) {
  1856. info = &theadInfo[i];
  1857. XMEMSET(info, 0, sizeof(info_t));
  1858. info->host = argHost;
  1859. info->port = argPort + i; /* threads must have separate ports */
  1860. info->cipher = cipher;
  1861. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1862. if (argDoGroups && XSTRNCMP(theadInfo[0].cipher, "TLS13", 5) == 0) {
  1863. info->group = groups[group_index].group;
  1864. }
  1865. else
  1866. #endif
  1867. {
  1868. info->group = 0;
  1869. }
  1870. info->packetSize = argTestPacketSize;
  1871. info->runTimeSec = argRuntimeSec;
  1872. info->maxSize = argTestMaxSize;
  1873. info->showPeerInfo = argShowPeerInfo;
  1874. info->showVerbose = argShowVerbose;
  1875. #ifndef NO_WOLFSSL_SERVER
  1876. info->listenFd = listenFd;
  1877. #endif
  1878. info->client.sockFd = -1;
  1879. info->server.sockFd = -1;
  1880. #ifdef WOLFSSL_DTLS
  1881. info->doDTLS = doDTLS;
  1882. #ifndef SINGLE_THREADED
  1883. info->serverReady = 0;
  1884. if (argServerOnly || argClientOnly) {
  1885. info->clientOrserverOnly = 1;
  1886. }
  1887. #endif
  1888. #endif
  1889. if (argClientOnly) {
  1890. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT) && !defined(SINGLE_THREADED)
  1891. /* to avoid to wait server forever */
  1892. info->serverListening = 1;
  1893. #endif
  1894. #ifndef NO_WOLFSSL_CLIENT
  1895. ret = bench_tls_client(info);
  1896. #endif
  1897. }
  1898. else if (argServerOnly) {
  1899. #ifndef NO_WOLFSSL_SERVER
  1900. ret = bench_tls_server(info);
  1901. #endif
  1902. }
  1903. else {
  1904. #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN)
  1905. info->useLocalMem = argLocalMem;
  1906. #ifdef WOLFSSL_DTLS
  1907. THREAD_CHECK_RET(wc_InitMutex(&info->dtls_mutex));
  1908. THREAD_CHECK_RET(wolfSSL_CondInit(&info->dtls_cond));
  1909. #endif
  1910. THREAD_CHECK_RET(wolfSSL_CondInit(&info->to_server.cond));
  1911. THREAD_CHECK_RET(wolfSSL_CondInit(&info->to_client.cond));
  1912. THREAD_CHECK_RET(
  1913. wolfSSL_NewThreadNoJoin(server_thread, info));
  1914. THREAD_CHECK_RET(
  1915. wolfSSL_NewThreadNoJoin(client_thread, info));
  1916. #endif
  1917. }
  1918. }
  1919. #ifndef SINGLE_THREADED
  1920. /* For threading, wait for completion */
  1921. if (!argClientOnly && !argServerOnly) {
  1922. /* Wait until threads are marked done */
  1923. do {
  1924. doShutdown = 1;
  1925. for (i = 0; i < argThreadPairs; ++i) {
  1926. info = &theadInfo[i];
  1927. if (!info->to_client.done || !info->to_server.done) {
  1928. doShutdown = 0;
  1929. XSLEEP_MS(1000); /* Allow other threads to run */
  1930. }
  1931. }
  1932. } while (!doShutdown);
  1933. if (argShowVerbose) {
  1934. fprintf(stderr, "Shutdown complete\n");
  1935. }
  1936. }
  1937. #endif /* !SINGLE_THREADED */
  1938. if (argShowVerbose) {
  1939. /* print results */
  1940. for (i = 0; i < argThreadPairs; ++i) {
  1941. info = &theadInfo[i];
  1942. fprintf(stderr, "\nThread %d\n", i);
  1943. #ifndef NO_WOLFSSL_SERVER
  1944. if (!argClientOnly) {
  1945. print_stats(&info->server_stats, "Server", info->cipher,
  1946. gname, 1);
  1947. }
  1948. #endif
  1949. #ifndef NO_WOLFSSL_CLIENT
  1950. if (!argServerOnly) {
  1951. print_stats(&info->client_stats, "Client", info->cipher,
  1952. gname, 1);
  1953. }
  1954. #endif
  1955. }
  1956. }
  1957. /* print combined results for more than one thread */
  1958. XMEMSET(&cli_comb, 0, sizeof(cli_comb));
  1959. XMEMSET(&srv_comb, 0, sizeof(srv_comb));
  1960. for (i = 0; i < argThreadPairs; ++i) {
  1961. info = &theadInfo[i];
  1962. cli_comb.connCount += info->client_stats.connCount;
  1963. srv_comb.connCount += info->server_stats.connCount;
  1964. cli_comb.connTime += info->client_stats.connTime;
  1965. srv_comb.connTime += info->server_stats.connTime;
  1966. cli_comb.rxTotal += info->client_stats.rxTotal;
  1967. srv_comb.rxTotal += info->server_stats.rxTotal;
  1968. cli_comb.rxTime += info->client_stats.rxTime;
  1969. srv_comb.rxTime += info->server_stats.rxTime;
  1970. cli_comb.txTotal += info->client_stats.txTotal;
  1971. srv_comb.txTotal += info->server_stats.txTotal;
  1972. cli_comb.txTime += info->client_stats.txTime;
  1973. srv_comb.txTime += info->server_stats.txTime;
  1974. }
  1975. if (argShowVerbose) {
  1976. fprintf(stderr, "Totals for %d Threads\n", argThreadPairs);
  1977. }
  1978. else {
  1979. fprintf(stderr, "%-6s %-33s %-25s %11s %9s %9s %9s %9s %9s %17s %15s\n",
  1980. "Side", "Cipher", "Group", "Total Bytes", "Num Conns", "Rx ms", "Tx ms",
  1981. "Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms");
  1982. #ifndef NO_WOLFSSL_SERVER
  1983. if (!argClientOnly) {
  1984. print_stats(&srv_comb, "Server", theadInfo[0].cipher, gname,
  1985. 0);
  1986. }
  1987. #endif
  1988. #ifndef NO_WOLFSSL_CLIENT
  1989. if (!argServerOnly) {
  1990. print_stats(&cli_comb, "Client", theadInfo[0].cipher, gname,
  1991. 0);
  1992. }
  1993. #endif
  1994. }
  1995. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  1996. if (!argDoGroups || theadInfo[0].group == 0) {
  1997. /* We only needed to do this once because they don't want to
  1998. * benchmarks groups or this isn't a TLS 1.3 cipher. */
  1999. break;
  2000. }
  2001. }
  2002. #endif
  2003. /* target next cipher */
  2004. cipher = (next_cipher != NULL) ? (next_cipher + 1) : NULL;
  2005. } /* while */
  2006. exit:
  2007. #ifndef NO_WOLFSSL_SERVER
  2008. if (argServerOnly && !argLocalMem) {
  2009. /* Close server listener */
  2010. CloseAndCleanupListenSocket(&listenFd);
  2011. }
  2012. #endif
  2013. /* Cleanup the wolfSSL environment */
  2014. wolfSSL_Cleanup();
  2015. /* Free theadInfo array */
  2016. XFREE(theadInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2017. /* Free cipher list */
  2018. XFREE(ciphers, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2019. /* Return reporting a success */
  2020. if (args) {
  2021. ((func_args*)args)->return_code = ret;
  2022. }
  2023. return ret;
  2024. }
  2025. #endif /* (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && !WOLFCRYPT_ONLY && USE_WOLFSSL_IO */
  2026. #ifndef NO_MAIN_DRIVER
  2027. int main(int argc, char** argv)
  2028. {
  2029. func_args args;
  2030. args.argc = argc;
  2031. args.argv = argv;
  2032. args.return_code = 0;
  2033. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  2034. !defined(WOLFCRYPT_ONLY) && defined(USE_WOLFSSL_IO)
  2035. bench_tls(&args);
  2036. #endif
  2037. return args.return_code;
  2038. }
  2039. #endif /* !NO_MAIN_DRIVER */