tls_bench.c 70 KB

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