tls_bench.c 71 KB

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