tls_bench.c 70 KB

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