tls_bench.c 72 KB

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