1
0

wolfssl_example.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  1. /* wolfssl_example.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include "wolfssl_example.h"
  22. /* force certificate test buffers to be included via headers */
  23. #undef USE_CERT_BUFFERS_2048
  24. #define USE_CERT_BUFFERS_2048
  25. #undef USE_CERT_BUFFERS_256
  26. #define USE_CERT_BUFFERS_256
  27. #include <wolfssl/certs_test.h>
  28. #include <wolfssl/wolfcrypt/hash.h> /* WC_MAX_DIGEST_SIZE */
  29. #ifndef SINGLE_THREADED
  30. #include <cmsis_os.h>
  31. #ifdef WOLFSSL_DEBUG_MEMORY
  32. /* for memory debugging */
  33. #include <task.h>
  34. #endif
  35. #endif
  36. #include <stdio.h>
  37. #include <string.h>
  38. /*****************************************************************************
  39. * Configuration
  40. ****************************************************************************/
  41. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  42. !defined(WOLFCRYPT_ONLY) && !defined(SINGLE_THREADED)
  43. #define ENABLE_TLS_BENCH
  44. #endif
  45. #if !defined(WOLFCRYPT_ONLY) && defined(WOLFSSL_TLS13) && !defined(NO_TLS_UART_TEST)
  46. #define ENABLE_TLS_UART
  47. #endif
  48. /* Defaults for configuration parameters */
  49. #define BENCH_DEFAULT_HOST "localhost"
  50. #define BENCH_DEFAULT_PORT 11112
  51. #define BENCH_RUNTIME_SEC 20
  52. #define BENCH_SHOW_PEER_INFO 1
  53. #define TEST_PACKET_SIZE (2 * 1024) /* TLS packet size */
  54. #ifdef BENCH_EMBEDDED
  55. #define TEST_MAX_SIZE (4 * 1024)
  56. #else
  57. #define TEST_MAX_SIZE (32 * 1024) /* Total bytes to benchmark */
  58. #endif
  59. /* Must be large enough to handle max packet size - TLS header MAX_MSG_EXTRA + MAX DIGEST */
  60. #define MEM_BUFFER_SZ (TEST_PACKET_SIZE + 38 + WC_MAX_DIGEST_SIZE)
  61. /* make sure memory buffer size is large enough */
  62. #if MEM_BUFFER_SZ < 2048
  63. #undef MEM_BUFFER_SZ
  64. #define MEM_BUFFER_SZ 2048
  65. #endif
  66. #define SHOW_VERBOSE 0 /* 0=tab del (minimal), 1=info, 2=debug, 3=debug w/wolf logs */
  67. #ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE
  68. #define WOLFSSL_CIPHER_LIST_MAX_SIZE 2048
  69. #endif
  70. #if 0
  71. /* define this to test only a specific cipher suite(s) (colon separated) */
  72. #define TEST_CIPHER_SUITE "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256"
  73. #endif
  74. #if 0
  75. /* use non-blocking mode for read/write IO */
  76. #define BENCH_USE_NONBLOCK
  77. #endif
  78. #ifndef RECV_WAIT_TIMEOUT
  79. #define RECV_WAIT_TIMEOUT 10000
  80. #endif
  81. /*****************************************************************************
  82. * Private types/enumerations/variables
  83. ****************************************************************************/
  84. #ifdef WOLFSSL_STATIC_MEMORY
  85. #if 1 /* on-chip RAM */
  86. #define RAM_STATIC
  87. #else /* external RAM */
  88. /* requires .ld to be updated with ".extram" section */
  89. #define RAM_STATIC __attribute__ ((section (".extram")))
  90. #endif
  91. #define WOLF_GEN_MEM (20*1024)
  92. #define WOLF_TLS_GEN_MEM (90*1024)
  93. #define WOLF_TLS_IO_POOL_MEM (35*1024)
  94. RAM_STATIC static byte gWolfMem[WOLF_GEN_MEM];
  95. RAM_STATIC static byte gWolfCTXCli[WOLF_TLS_GEN_MEM];
  96. RAM_STATIC static byte gWolfIOCli[WOLF_TLS_IO_POOL_MEM];
  97. RAM_STATIC static byte gWolfCTXSrv[WOLF_TLS_GEN_MEM];
  98. RAM_STATIC static byte gWolfIOSrv[WOLF_TLS_IO_POOL_MEM];
  99. WOLFSSL_HEAP_HINT* HEAP_HINT = NULL;
  100. #endif /* WOLFSSL_STATIC_MEMORY */
  101. /* This sets which UART to use for the console. It is something you will have
  102. * to configure in STMCubeIDE and then change here. */
  103. #ifndef HAL_CONSOLE_UART
  104. #define HAL_CONSOLE_UART huart4
  105. #endif
  106. extern UART_HandleTypeDef HAL_CONSOLE_UART;
  107. /*****************************************************************************
  108. * Public types/enumerations/variables
  109. ****************************************************************************/
  110. typedef struct func_args {
  111. int argc;
  112. char** argv;
  113. int return_code;
  114. } func_args;
  115. const char menu1[] = "\n"
  116. "\tt. wolfCrypt Test\n"
  117. "\tb. wolfCrypt Benchmark\n"
  118. #ifdef ENABLE_TLS_BENCH
  119. "\tl. wolfSSL TLS Bench\n"
  120. #endif
  121. "\te. Show Cipher List\n"
  122. #ifdef ENABLE_TLS_UART
  123. "\ts. Run TLS 1.3 Server over UART\n"
  124. "\tc. Run TLS 1.3 Client over UART\n"
  125. #endif
  126. ;
  127. static void PrintMemStats(void);
  128. double current_time(void);
  129. #ifdef ENABLE_TLS_BENCH
  130. static const char* kShutdown = "shutdown";
  131. static const char* kTestStr =
  132. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  133. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  134. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  135. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  136. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  137. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  138. "\n"
  139. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  140. "four loko you probably haven't heard of them high life. Messenger bag\n"
  141. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  142. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  143. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  144. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  145. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  146. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  147. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  148. "four loko whatever street art yr farm-to-table.\n"
  149. "\n"
  150. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  151. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  152. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  153. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  154. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  155. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  156. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  157. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  158. "locavore.\n"
  159. "\n"
  160. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  161. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  162. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  163. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  164. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  165. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  166. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  167. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  168. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  169. "neutra PBR selvage.\n"
  170. "\n"
  171. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  172. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  173. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  174. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  175. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  176. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  177. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  178. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  179. "mollit magna, sriracha sartorial helvetica.\n"
  180. "\n"
  181. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  182. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  183. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  184. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  185. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  186. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  187. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  188. "\n"
  189. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  190. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  191. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  192. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  193. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  194. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  195. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  196. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  197. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  198. "\n"
  199. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  200. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  201. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  202. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  203. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  204. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  205. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  206. "bag dolor terry richardson sapiente.\n";
  207. #if !defined(NO_DH)
  208. #define MIN_DHKEY_BITS 1024
  209. /* dh2048 p */
  210. static const unsigned char p[] =
  211. {
  212. 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13, 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30,
  213. 0xd5, 0xf5, 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a, 0xc6, 0x25, 0x27,
  214. 0x1c, 0xe8, 0xd2, 0x96, 0x53, 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  215. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1, 0x8a, 0xc5, 0xbe, 0x72, 0x5c,
  216. 0xa7, 0xe7, 0x91, 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66, 0x48, 0xc7,
  217. 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9, 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46,
  218. 0x3e, 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d, 0x9f, 0x2b, 0x96, 0x17,
  219. 0x39, 0x6d, 0x30, 0x8d, 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75, 0xe6,
  220. 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa, 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda,
  221. 0x76, 0x93, 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c, 0xe5, 0xb7, 0xc8,
  222. 0x09, 0xae, 0x6f, 0x50, 0x35, 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  223. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76, 0xcc, 0x16, 0x30, 0xdb, 0x0c,
  224. 0xfc, 0xc5, 0x62, 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0, 0x36, 0xf6,
  225. 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40, 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3,
  226. 0x1a, 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4, 0x48, 0x4e, 0x22, 0x73,
  227. 0x6f, 0xc3, 0x5f, 0xd4, 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90, 0xab,
  228. 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58, 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf,
  229. 0x68, 0x1f, 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6, 0xb6, 0x06, 0x65,
  230. 0xf7, 0xa6, 0xdc, 0x00, 0x67, 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  231. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f, 0x93
  232. };
  233. /* dh2048 g */
  234. static const unsigned char g[] =
  235. {
  236. 0x02,
  237. };
  238. #endif /* !NO_DH */
  239. typedef struct {
  240. unsigned char buf[MEM_BUFFER_SZ];
  241. int write_bytes;
  242. int write_idx;
  243. int read_bytes;
  244. int read_idx;
  245. } memBuf_t;
  246. typedef struct {
  247. double connTime;
  248. double rxTime;
  249. double txTime;
  250. int connCount;
  251. int rxTotal;
  252. int txTotal;
  253. } stats_t;
  254. typedef struct {
  255. int ret;
  256. osThreadId threadId;
  257. #ifdef CMSIS_OS2_H_
  258. osSemaphoreId_t mutex;
  259. #else
  260. osThreadDef_t threadDef;
  261. osSemaphoreDef_t mutexDef;
  262. osSemaphoreId mutex;
  263. #endif
  264. byte shutdown:1;
  265. byte done:1;
  266. } side_t;
  267. typedef struct {
  268. const char* cipher;
  269. const char* host;
  270. word32 port;
  271. int packetSize; /* The data payload size in the packet */
  272. int maxSize;
  273. int runTimeSec;
  274. int showPeerInfo;
  275. int showVerbose;
  276. int doShutdown;
  277. side_t client;
  278. side_t server;
  279. /* client messages to server in memory */
  280. memBuf_t to_server;
  281. /* server messages to client in memory */
  282. memBuf_t to_client;
  283. /* server */
  284. stats_t server_stats;
  285. /* client */
  286. stats_t client_stats;
  287. } info_t;
  288. /*****************************************************************************
  289. * Private functions
  290. ****************************************************************************/
  291. static double gettime_secs(int reset)
  292. {
  293. return current_time();
  294. }
  295. static void PrintTlsStats(stats_t* wcStat, const char* desc, const char* cipher, int verbose)
  296. {
  297. const char* formatStr;
  298. if (verbose) {
  299. formatStr = "wolfSSL %s Benchmark on %s:\n"
  300. "\tTotal : %9d bytes\n"
  301. "\tNum Conns : %9d\n"
  302. "\tRx Total : %9.3f ms\n"
  303. "\tTx Total : %9.3f ms\n"
  304. "\tRx : %9.3f MB/s\n"
  305. "\tTx : %9.3f MB/s\n"
  306. "\tConnect : %9.3f ms\n"
  307. "\tConnect Avg : %9.3f ms\n";
  308. }
  309. else {
  310. formatStr = "%-6s %-33s %11d %9d %9.3f %9.3f %9.3f %9.3f %17.3f %15.3f\n";
  311. }
  312. printf(formatStr,
  313. desc,
  314. cipher,
  315. wcStat->txTotal + wcStat->rxTotal,
  316. wcStat->connCount,
  317. wcStat->rxTime * 1000,
  318. wcStat->txTime * 1000,
  319. wcStat->rxTotal / wcStat->rxTime / 1024 / 1024,
  320. wcStat->txTotal / wcStat->txTime / 1024 / 1024,
  321. wcStat->connTime * 1000,
  322. wcStat->connTime * 1000 / wcStat->connCount);
  323. }
  324. #endif /* ENABLE_TLS_BENCH */
  325. #if defined(ENABLE_TLS_BENCH) || defined(ENABLE_TLS_UART)
  326. #if defined(KEEP_PEER_CERT) || defined(KEEP_OUR_CERT)
  327. static const char* client_showx509_msg[] = {
  328. "issuer",
  329. "subject",
  330. "altname",
  331. "serial number",
  332. NULL
  333. };
  334. static void ShowX509(WOLFSSL_X509* x509, const char* hdr)
  335. {
  336. char* altName;
  337. char* issuer;
  338. char* subject;
  339. byte serial[32];
  340. int ret;
  341. int sz = sizeof(serial);
  342. const char** words = client_showx509_msg;
  343. if (x509 == NULL) {
  344. printf("%s No Cert\n", hdr);
  345. return;
  346. }
  347. issuer = wolfSSL_X509_NAME_oneline(
  348. wolfSSL_X509_get_issuer_name(x509), 0, 0);
  349. subject = wolfSSL_X509_NAME_oneline(
  350. wolfSSL_X509_get_subject_name(x509), 0, 0);
  351. printf("%s\n %s : %s\n %s: %s\n", hdr, words[0], issuer, words[1], subject);
  352. while ( (altName = wolfSSL_X509_get_next_altname(x509)) != NULL)
  353. printf(" %s = %s\n", words[2], altName);
  354. ret = wolfSSL_X509_get_serial_number(x509, serial, &sz);
  355. if (ret == WOLFSSL_SUCCESS) {
  356. int i;
  357. int strLen;
  358. char serialMsg[80];
  359. /* testsuite has multiple threads writing to stdout, get output
  360. message ready to write once */
  361. strLen = sprintf(serialMsg, " %s", words[3]);
  362. for (i = 0; i < sz; i++)
  363. sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
  364. printf("%s\n", serialMsg);
  365. }
  366. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  367. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  368. #if defined(OPENSSL_EXTRA)
  369. {
  370. WOLFSSL_BIO* bio;
  371. char buf[256]; /* should be size of ASN_NAME_MAX */
  372. int textSz;
  373. /* print out domain component if certificate has it */
  374. textSz = wolfSSL_X509_NAME_get_text_by_NID(
  375. wolfSSL_X509_get_subject_name(x509), NID_domainComponent,
  376. buf, sizeof(buf));
  377. if (textSz > 0) {
  378. printf("Domain Component = %s\n", buf);
  379. }
  380. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  381. if (bio != NULL) {
  382. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  383. wolfSSL_X509_print(bio, x509);
  384. wolfSSL_BIO_free(bio);
  385. }
  386. }
  387. #endif /* OPENSSL_EXTRA */
  388. }
  389. #endif /* KEEP_PEER_CERT || KEEP_OUR_CERT */
  390. static const char* client_showpeer_msg[] = {
  391. "SSL version is",
  392. "SSL cipher suite is",
  393. "SSL curve name is",
  394. "SSL DH size is",
  395. "SSL reused session",
  396. "Alternate cert chain used",
  397. "peer's cert info:",
  398. NULL
  399. };
  400. static void ShowPeer(WOLFSSL* ssl)
  401. {
  402. WOLFSSL_CIPHER* cipher;
  403. const char** words = client_showpeer_msg;
  404. #if defined(HAVE_ECC) || !defined(NO_DH)
  405. const char *name;
  406. #endif
  407. #ifndef NO_DH
  408. int bits;
  409. #endif
  410. #ifdef KEEP_PEER_CERT
  411. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  412. if (peer)
  413. ShowX509(peer, words[6]);
  414. else
  415. printf("peer has no cert!\n");
  416. wolfSSL_FreeX509(peer);
  417. #endif
  418. #if defined(KEEP_OUR_CERT) && \
  419. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  420. ShowX509(wolfSSL_get_certificate(ssl), "our cert info:");
  421. printf("Peer verify result = %lu\n", wolfSSL_get_verify_result(ssl));
  422. #endif /* SHOW_CERTS && KEEP_OUR_CERT */
  423. printf("%s %s\n", words[0], wolfSSL_get_version(ssl));
  424. cipher = wolfSSL_get_current_cipher(ssl);
  425. printf("%s %s\n", words[1], wolfSSL_CIPHER_get_name(cipher));
  426. #if defined(HAVE_ECC) || !defined(NO_DH)
  427. if ((name = wolfSSL_get_curve_name(ssl)) != NULL)
  428. printf("%s %s\n", words[2], name);
  429. #endif
  430. #ifndef NO_DH
  431. else if ((bits = wolfSSL_GetDhKey_Sz(ssl)) > 0)
  432. printf("%s %d bits\n", words[3], bits);
  433. #endif
  434. if (wolfSSL_session_reused(ssl))
  435. printf("%s\n", words[4]);
  436. #ifdef WOLFSSL_ALT_CERT_CHAINS
  437. if (wolfSSL_is_peer_alt_cert_chain(ssl))
  438. printf("%s\n", words[5]);
  439. #endif
  440. (void)ssl;
  441. }
  442. #endif /* ENABLE_TLS_BENCH || ENABLE_TLS_UART */
  443. #ifdef ENABLE_TLS_BENCH
  444. /* server send callback */
  445. static int ServerMemSend(info_t* info, char* buf, int sz)
  446. {
  447. #ifdef CMSIS_OS2_H_
  448. osSemaphoreAcquire(info->client.mutex, osWaitForever);
  449. #else
  450. osSemaphoreWait(info->client.mutex, osWaitForever);
  451. #endif
  452. #ifndef BENCH_USE_NONBLOCK
  453. /* check for overflow */
  454. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ) {
  455. osSemaphoreRelease(info->client.mutex);
  456. printf("ServerMemSend overflow\n");
  457. return -1;
  458. }
  459. #else
  460. if (info->to_client.write_idx + sz > MEM_BUFFER_SZ)
  461. sz = MEM_BUFFER_SZ - info->to_client.write_idx;
  462. #endif
  463. if (info->showVerbose >= 2)
  464. printf("Server Send: %d\n", sz);
  465. XMEMCPY(&info->to_client.buf[info->to_client.write_idx], buf, sz);
  466. info->to_client.write_idx += sz;
  467. info->to_client.write_bytes += sz;
  468. #ifdef CMSIS_OS2_H_
  469. osThreadFlagsSet(info->client.threadId, 1);
  470. #else
  471. osSignalSet(info->client.threadId, 1);
  472. #endif
  473. osSemaphoreRelease(info->client.mutex);
  474. #ifdef BENCH_USE_NONBLOCK
  475. if (sz == 0)
  476. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  477. #endif
  478. return sz;
  479. }
  480. /* server recv callback */
  481. static int ServerMemRecv(info_t* info, char* buf, int sz)
  482. {
  483. #ifdef CMSIS_OS2_H_
  484. osSemaphoreAcquire(info->server.mutex, osWaitForever);
  485. #else
  486. osSemaphoreWait(info->server.mutex, osWaitForever);
  487. #endif
  488. #ifndef BENCH_USE_NONBLOCK
  489. while (info->to_server.write_idx - info->to_server.read_idx < sz &&
  490. !info->client.done) {
  491. osSemaphoreRelease(info->server.mutex);
  492. #ifdef CMSIS_OS2_H_
  493. if (osThreadFlagsWait(1, osFlagsWaitAny, RECV_WAIT_TIMEOUT) == osFlagsErrorTimeout) {
  494. printf("Server Recv: Timeout!\n");
  495. return WOLFSSL_CBIO_ERR_TIMEOUT;
  496. }
  497. osSemaphoreAcquire(info->server.mutex, osWaitForever);
  498. #else
  499. if (osSignalWait(1, RECV_WAIT_TIMEOUT).status == osEventTimeout) {
  500. printf("Server Recv: Timeout!\n");
  501. return WOLFSSL_CBIO_ERR_TIMEOUT;
  502. }
  503. osSemaphoreWait(info->server.mutex, osWaitForever);
  504. #endif
  505. }
  506. #else
  507. if (info->to_server.write_idx - info->to_server.read_idx < sz)
  508. sz = info->to_server.write_idx - info->to_server.read_idx;
  509. #endif
  510. XMEMCPY(buf, &info->to_server.buf[info->to_server.read_idx], sz);
  511. info->to_server.read_idx += sz;
  512. info->to_server.read_bytes += sz;
  513. /* if the rx has caught up with pending then reset buffer positions */
  514. if (info->to_server.read_bytes == info->to_server.write_bytes) {
  515. info->to_server.read_bytes = info->to_server.read_idx = 0;
  516. info->to_server.write_bytes = info->to_server.write_idx = 0;
  517. }
  518. if (info->showVerbose >= 2)
  519. printf("Server Recv: %d\n", sz);
  520. osSemaphoreRelease(info->server.mutex);
  521. #ifdef BENCH_USE_NONBLOCK
  522. if (sz == 0)
  523. return WOLFSSL_CBIO_ERR_WANT_READ;
  524. #endif
  525. return sz;
  526. }
  527. /* client send callback */
  528. static int ClientMemSend(info_t* info, char* buf, int sz)
  529. {
  530. #ifdef CMSIS_OS2_H_
  531. osSemaphoreAcquire(info->server.mutex, osWaitForever);
  532. #else
  533. osSemaphoreWait(info->server.mutex, osWaitForever);
  534. #endif
  535. #ifndef BENCH_USE_NONBLOCK
  536. /* check for overflow */
  537. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ) {
  538. printf("ClientMemSend overflow %d %d %d\n",
  539. info->to_server.write_idx, sz, MEM_BUFFER_SZ);
  540. osSemaphoreRelease(info->server.mutex);
  541. return -1;
  542. }
  543. #else
  544. if (info->to_server.write_idx + sz > MEM_BUFFER_SZ)
  545. sz = MEM_BUFFER_SZ - info->to_server.write_idx;
  546. #endif
  547. if (info->showVerbose >= 2)
  548. printf("Client Send: %d\n", sz);
  549. XMEMCPY(&info->to_server.buf[info->to_server.write_idx], buf, sz);
  550. info->to_server.write_idx += sz;
  551. info->to_server.write_bytes += sz;
  552. #ifdef CMSIS_OS2_H_
  553. osThreadFlagsSet(info->server.threadId, 1);
  554. #else
  555. osSignalSet(info->server.threadId, 1);
  556. #endif
  557. osSemaphoreRelease(info->server.mutex);
  558. #ifdef BENCH_USE_NONBLOCK
  559. if (sz == 0)
  560. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  561. #endif
  562. return sz;
  563. }
  564. /* client recv callback */
  565. static int ClientMemRecv(info_t* info, char* buf, int sz)
  566. {
  567. #ifdef CMSIS_OS2_H_
  568. osSemaphoreAcquire(info->client.mutex, osWaitForever);
  569. #else
  570. osSemaphoreWait(info->client.mutex, osWaitForever);
  571. #endif
  572. #ifndef BENCH_USE_NONBLOCK
  573. while (info->to_client.write_idx - info->to_client.read_idx < sz &&
  574. !info->server.done) {
  575. osSemaphoreRelease(info->client.mutex);
  576. #ifdef CMSIS_OS2_H_
  577. if (osThreadFlagsWait(1, osFlagsWaitAny, RECV_WAIT_TIMEOUT) == osFlagsErrorTimeout) {
  578. printf("Client Recv: Timeout!\n");
  579. return WOLFSSL_CBIO_ERR_TIMEOUT;
  580. }
  581. osSemaphoreAcquire(info->client.mutex, osWaitForever);
  582. #else
  583. if (osSignalWait(1, RECV_WAIT_TIMEOUT).status == osEventTimeout) {
  584. printf("Client Recv: Timeout!\n");
  585. return WOLFSSL_CBIO_ERR_TIMEOUT;
  586. }
  587. osSemaphoreWait(info->client.mutex, osWaitForever);
  588. #endif
  589. }
  590. #else
  591. if (info->to_client.write_idx - info->to_client.read_idx < sz)
  592. sz = info->to_client.write_idx - info->to_client.read_idx;
  593. #endif
  594. XMEMCPY(buf, &info->to_client.buf[info->to_client.read_idx], sz);
  595. info->to_client.read_idx += sz;
  596. info->to_client.read_bytes += sz;
  597. /* if the rx has caught up with pending then reset buffer positions */
  598. if (info->to_client.read_bytes == info->to_client.write_bytes) {
  599. info->to_client.read_bytes = info->to_client.read_idx = 0;
  600. info->to_client.write_bytes = info->to_client.write_idx = 0;
  601. }
  602. if (info->showVerbose >= 2)
  603. printf("Client Recv: %d\n", sz);
  604. osSemaphoreRelease(info->client.mutex);
  605. #ifdef BENCH_USE_NONBLOCK
  606. if (sz == 0)
  607. return WOLFSSL_CBIO_ERR_WANT_READ;
  608. #endif
  609. return sz;
  610. }
  611. static int ServerSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  612. {
  613. info_t* info = (info_t*)ctx;
  614. (void)ssl;
  615. return ServerMemSend(info, buf, sz);
  616. }
  617. static int ServerRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  618. {
  619. info_t* info = (info_t*)ctx;
  620. (void)ssl;
  621. return ServerMemRecv(info, buf, sz);
  622. }
  623. static int ClientSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  624. {
  625. info_t* info = (info_t*)ctx;
  626. (void)ssl;
  627. return ClientMemSend(info, buf, sz);
  628. }
  629. static int ClientRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  630. {
  631. info_t* info = (info_t*)ctx;
  632. (void)ssl;
  633. return ClientMemRecv(info, buf, sz);
  634. }
  635. static int bench_tls_client(info_t* info)
  636. {
  637. byte *writeBuf = NULL, *readBuf = NULL;
  638. double start, total = 0;
  639. int ret = 0, readBufSz;
  640. WOLFSSL_CTX* cli_ctx = NULL;
  641. WOLFSSL* cli_ssl = NULL;
  642. int haveShownPeerInfo = 0;
  643. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  644. int total_sz;
  645. total = gettime_secs(0);
  646. /* set up client */
  647. #ifdef WOLFSSL_TLS13
  648. if (tls13) {
  649. #ifdef WOLFSSL_STATIC_MEMORY
  650. ret = wolfSSL_CTX_load_static_memory(&cli_ctx, wolfTLSv1_3_client_method_ex,
  651. gWolfCTXCli, sizeof(gWolfCTXCli), WOLFMEM_GENERAL , 10);
  652. #else
  653. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  654. #endif
  655. }
  656. #endif
  657. if (!tls13) {
  658. #if !defined(WOLFSSL_TLS13)
  659. #ifdef WOLFSSL_STATIC_MEMORY
  660. ret = wolfSSL_CTX_load_static_memory(&cli_ctx, wolfSSLv23_client_method_ex,
  661. gWolfCTXCli, sizeof(gWolfCTXCli), WOLFMEM_GENERAL , 10);
  662. #else
  663. cli_ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  664. #endif
  665. #elif !defined(WOLFSSL_NO_TLS12)
  666. #ifdef WOLFSSL_STATIC_MEMORY
  667. ret = wolfSSL_CTX_load_static_memory(&cli_ctx, wolfTLSv1_2_client_method_ex,
  668. gWolfCTXCli, sizeof(gWolfCTXCli), WOLFMEM_GENERAL , 10);
  669. #else
  670. cli_ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  671. #endif
  672. #endif
  673. }
  674. if (cli_ctx == NULL || ret != 0) {
  675. printf("error creating ctx: ret %d\n", ret);
  676. ret = MEMORY_E; goto exit;
  677. }
  678. #ifdef WOLFSSL_STATIC_MEMORY
  679. ret = wolfSSL_CTX_load_static_memory(&cli_ctx, 0, gWolfIOCli, sizeof(gWolfIOCli),
  680. WOLFMEM_IO_POOL, 10 );
  681. #endif
  682. #ifndef NO_CERTS
  683. #ifdef HAVE_ECC
  684. if (XSTRSTR(info->cipher, "ECDSA")) {
  685. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_ecc_cert_der_256,
  686. sizeof_ca_ecc_cert_der_256, WOLFSSL_FILETYPE_ASN1);
  687. }
  688. else
  689. #endif
  690. {
  691. ret = wolfSSL_CTX_load_verify_buffer(cli_ctx, ca_cert_der_2048,
  692. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  693. }
  694. if (ret != WOLFSSL_SUCCESS) {
  695. printf("error loading CA\n");
  696. goto exit;
  697. }
  698. #endif /* !NO_CERTS */
  699. wolfSSL_CTX_SetIOSend(cli_ctx, ClientSend);
  700. wolfSSL_CTX_SetIORecv(cli_ctx, ClientRecv);
  701. /* set cipher suite */
  702. ret = wolfSSL_CTX_set_cipher_list(cli_ctx, info->cipher);
  703. if (ret != WOLFSSL_SUCCESS) {
  704. printf("error setting cipher suite\n");
  705. goto exit;
  706. }
  707. #ifndef NO_DH
  708. ret = wolfSSL_CTX_SetMinDhKey_Sz(cli_ctx, MIN_DHKEY_BITS);
  709. if (ret != WOLFSSL_SUCCESS) {
  710. printf("Error setting minimum DH key size\n");
  711. goto exit;
  712. }
  713. #endif /* !NO_DH */
  714. /* Allocate and initialize a packet sized buffer */
  715. writeBuf = (unsigned char*)XMALLOC(info->packetSize, NULL,
  716. DYNAMIC_TYPE_TMP_BUFFER);
  717. if (writeBuf == NULL) {
  718. printf("failed to allocate write memory\n");
  719. ret = MEMORY_E; goto exit;
  720. }
  721. /* Allocate read buffer */
  722. readBufSz = info->packetSize;
  723. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  724. if (readBuf == NULL) {
  725. printf("failed to allocate read memory\n");
  726. ret = MEMORY_E; goto exit;
  727. }
  728. /* BENCHMARK CONNECTIONS LOOP */
  729. while (!info->client.shutdown) {
  730. int writeSz = info->packetSize;
  731. #ifdef BENCH_USE_NONBLOCK
  732. int err;
  733. #endif
  734. cli_ssl = wolfSSL_new(cli_ctx);
  735. if (cli_ssl == NULL) {
  736. printf("error creating client object\n");
  737. ret = MEMORY_E; goto exit;
  738. }
  739. wolfSSL_SetIOReadCtx(cli_ssl, info);
  740. wolfSSL_SetIOWriteCtx(cli_ssl, info);
  741. /* perform connect */
  742. start = gettime_secs(1);
  743. #ifndef BENCH_USE_NONBLOCK
  744. ret = wolfSSL_connect(cli_ssl);
  745. #else
  746. do
  747. {
  748. ret = wolfSSL_connect(cli_ssl);
  749. err = wolfSSL_get_error(cli_ssl, ret);
  750. }
  751. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  752. #endif
  753. start = gettime_secs(0) - start;
  754. if (ret != WOLFSSL_SUCCESS) {
  755. ret = wolfSSL_get_error(cli_ssl, ret);
  756. printf("error %d connecting client\n", ret);
  757. goto exit;
  758. }
  759. info->client_stats.connTime += start;
  760. info->client_stats.connCount++;
  761. if ((info->showPeerInfo) && (!haveShownPeerInfo)) {
  762. haveShownPeerInfo = 1;
  763. ShowPeer(cli_ssl);
  764. }
  765. /* check for run time completion and issue shutdown */
  766. if (gettime_secs(0) - total >= info->runTimeSec) {
  767. info->client.shutdown = 1;
  768. writeSz = (int)XSTRLEN(kShutdown) + 1;
  769. XMEMCPY(writeBuf, kShutdown, writeSz); /* include null term */
  770. if (info->showVerbose) {
  771. printf("Sending shutdown\n");
  772. }
  773. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  774. if (ret < 0) {
  775. ret = wolfSSL_get_error(cli_ssl, ret);
  776. printf("error %d on client write\n", ret);
  777. goto exit;
  778. }
  779. }
  780. else {
  781. XMEMSET(writeBuf, 0, info->packetSize);
  782. XSTRNCPY((char*)writeBuf, kTestStr, info->packetSize);
  783. }
  784. /* write / read echo loop */
  785. ret = 0;
  786. total_sz = 0;
  787. while (ret == 0 && total_sz < info->maxSize && !info->client.shutdown) {
  788. /* write test message to server */
  789. start = gettime_secs(1);
  790. #ifndef BENCH_USE_NONBLOCK
  791. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  792. #else
  793. do {
  794. ret = wolfSSL_write(cli_ssl, writeBuf, writeSz);
  795. err = wolfSSL_get_error(cli_ssl, ret);
  796. }
  797. while (err == WOLFSSL_ERROR_WANT_WRITE);
  798. #endif
  799. info->client_stats.txTime += gettime_secs(0) - start;
  800. if (ret < 0) {
  801. ret = wolfSSL_get_error(cli_ssl, ret);
  802. printf("error %d on client write\n", ret);
  803. goto exit;
  804. }
  805. info->client_stats.txTotal += ret;
  806. total_sz += ret;
  807. /* read echo of message from server */
  808. XMEMSET(readBuf, 0, readBufSz);
  809. start = gettime_secs(1);
  810. #ifndef BENCH_USE_NONBLOCK
  811. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  812. #else
  813. do {
  814. ret = wolfSSL_read(cli_ssl, readBuf, readBufSz);
  815. err = wolfSSL_get_error(cli_ssl, ret);
  816. }
  817. while (err == WOLFSSL_ERROR_WANT_READ);
  818. #endif
  819. info->client_stats.rxTime += gettime_secs(0) - start;
  820. if (ret < 0) {
  821. ret = wolfSSL_get_error(cli_ssl, ret);
  822. printf("error %d on client read\n", ret);
  823. goto exit;
  824. }
  825. info->client_stats.rxTotal += ret;
  826. ret = 0; /* reset return code */
  827. /* validate echo */
  828. if (XMEMCMP((char*)writeBuf, (char*)readBuf, writeSz) != 0) {
  829. printf("echo check failed!\n");
  830. goto exit;
  831. }
  832. }
  833. wolfSSL_free(cli_ssl);
  834. cli_ssl = NULL;
  835. }
  836. exit:
  837. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  838. info->doShutdown = 1;
  839. printf("Client Error: %d (%s)\n", ret,
  840. wolfSSL_ERR_reason_error_string(ret));
  841. }
  842. /* cleanup */
  843. if (cli_ssl != NULL)
  844. wolfSSL_free(cli_ssl);
  845. if (cli_ctx != NULL)
  846. wolfSSL_CTX_free(cli_ctx);
  847. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  848. XFREE(writeBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  849. info->client.ret = ret;
  850. return ret;
  851. }
  852. #ifdef CMSIS_OS2_H_
  853. static void client_thread(void* args)
  854. #else
  855. static void client_thread(const void* args)
  856. #endif
  857. {
  858. int ret;
  859. info_t* info = (info_t*)args;
  860. #ifdef CMSIS_OS2_H_
  861. info->client.threadId = osThreadGetId();
  862. #endif
  863. do {
  864. ret = bench_tls_client(info);
  865. /* signal server */
  866. if (!info->server.done && info->server.threadId != 0) {
  867. #ifdef CMSIS_OS2_H_
  868. osThreadFlagsSet(info->server.threadId, 1);
  869. #else
  870. osSignalSet(info->server.threadId, 1);
  871. #endif
  872. }
  873. info->client.ret = ret;
  874. info->client.done = 1;
  875. osThreadSuspend(info->client.threadId);
  876. if (info->doShutdown)
  877. info->server.done = 1;
  878. } while (!info->doShutdown);
  879. osThreadTerminate(info->client.threadId);
  880. info->client.threadId = NULL;
  881. }
  882. static int bench_tls_server(info_t* info)
  883. {
  884. byte *readBuf = NULL;
  885. double start;
  886. int ret = 0, len = 0, readBufSz;
  887. WOLFSSL_CTX* srv_ctx = NULL;
  888. WOLFSSL* srv_ssl = NULL;
  889. int tls13 = XSTRNCMP(info->cipher, "TLS13", 5) == 0;
  890. int total_sz;
  891. /* set up server */
  892. #ifdef WOLFSSL_TLS13
  893. if (tls13) {
  894. #ifdef WOLFSSL_STATIC_MEMORY
  895. ret = wolfSSL_CTX_load_static_memory(&srv_ctx, wolfTLSv1_3_server_method_ex,
  896. gWolfCTXSrv, sizeof(gWolfCTXSrv), WOLFMEM_GENERAL , 10);
  897. #else
  898. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  899. #endif
  900. }
  901. #endif
  902. if (!tls13) {
  903. #if !defined(WOLFSSL_TLS13)
  904. #ifdef WOLFSSL_STATIC_MEMORY
  905. ret = wolfSSL_CTX_load_static_memory(&srv_ctx, wolfSSLv23_server_method_ex,
  906. gWolfCTXSrv, sizeof(gWolfCTXSrv), WOLFMEM_GENERAL , 10);
  907. #else
  908. srv_ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  909. #endif
  910. #elif !defined(WOLFSSL_NO_TLS12)
  911. #ifdef WOLFSSL_STATIC_MEMORY
  912. ret = wolfSSL_CTX_load_static_memory(&srv_ctx, wolfTLSv1_2_server_method_ex,
  913. gWolfCTXSrv, sizeof(gWolfCTXSrv), WOLFMEM_GENERAL , 10);
  914. #else
  915. srv_ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  916. #endif
  917. #endif
  918. }
  919. if (srv_ctx == NULL || ret != 0) {
  920. printf("error creating server ctx: ret %d\n", ret);
  921. ret = MEMORY_E; goto exit;
  922. }
  923. #ifdef WOLFSSL_STATIC_MEMORY
  924. ret = wolfSSL_CTX_load_static_memory(&srv_ctx, 0, gWolfIOSrv, sizeof(gWolfIOSrv),
  925. WOLFMEM_IO_POOL, 10 );
  926. #endif
  927. #ifndef NO_CERTS
  928. #ifdef HAVE_ECC
  929. if (XSTRSTR(info->cipher, "ECDSA")) {
  930. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, ecc_key_der_256,
  931. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  932. }
  933. else
  934. #endif
  935. {
  936. ret = wolfSSL_CTX_use_PrivateKey_buffer(srv_ctx, server_key_der_2048,
  937. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1);
  938. }
  939. if (ret != WOLFSSL_SUCCESS) {
  940. printf("error loading server key\n");
  941. goto exit;
  942. }
  943. #ifdef HAVE_ECC
  944. if (XSTRSTR(info->cipher, "ECDSA")) {
  945. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, serv_ecc_der_256,
  946. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  947. }
  948. else
  949. #endif
  950. {
  951. ret = wolfSSL_CTX_use_certificate_buffer(srv_ctx, server_cert_der_2048,
  952. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  953. }
  954. if (ret != WOLFSSL_SUCCESS) {
  955. printf("error loading server cert\n");
  956. goto exit;
  957. }
  958. #endif /* !NO_CERTS */
  959. wolfSSL_CTX_SetIOSend(srv_ctx, ServerSend);
  960. wolfSSL_CTX_SetIORecv(srv_ctx, ServerRecv);
  961. /* set cipher suite */
  962. ret = wolfSSL_CTX_set_cipher_list(srv_ctx, info->cipher);
  963. if (ret != WOLFSSL_SUCCESS) {
  964. printf("error setting cipher suite\n");
  965. goto exit;
  966. }
  967. #ifndef NO_DH
  968. ret = wolfSSL_CTX_SetMinDhKey_Sz(srv_ctx, MIN_DHKEY_BITS);
  969. if (ret != WOLFSSL_SUCCESS) {
  970. printf("Error setting minimum DH key size\n");
  971. goto exit;
  972. }
  973. #endif
  974. /* Allocate read buffer */
  975. readBufSz = info->packetSize;
  976. readBuf = (unsigned char*)XMALLOC(readBufSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  977. if (readBuf == NULL) {
  978. printf("failed to allocate read memory\n");
  979. ret = MEMORY_E; goto exit;
  980. }
  981. /* BENCHMARK CONNECTIONS LOOP */
  982. while (!info->server.shutdown) {
  983. #ifdef BENCH_USE_NONBLOCK
  984. int err;
  985. #endif
  986. #ifdef BENCH_USE_NONBLOCK
  987. if (ret == -2) {
  988. osDelay(0);
  989. continue;
  990. }
  991. #endif
  992. srv_ssl = wolfSSL_new(srv_ctx);
  993. if (srv_ssl == NULL) {
  994. printf("error creating server object\n");
  995. ret = MEMORY_E; goto exit;
  996. }
  997. wolfSSL_SetIOReadCtx(srv_ssl, info);
  998. wolfSSL_SetIOWriteCtx(srv_ssl, info);
  999. #ifndef NO_DH
  1000. wolfSSL_SetTmpDH(srv_ssl, p, sizeof(p), g, sizeof(g));
  1001. #endif
  1002. /* accept TLS connection */
  1003. start = gettime_secs(1);
  1004. #ifndef BENCH_USE_NONBLOCK
  1005. ret = wolfSSL_accept(srv_ssl);
  1006. #else
  1007. do {
  1008. ret = wolfSSL_accept(srv_ssl);
  1009. err = wolfSSL_get_error(srv_ssl, ret);
  1010. }
  1011. while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1012. #endif
  1013. start = gettime_secs(0) - start;
  1014. if (ret != WOLFSSL_SUCCESS) {
  1015. ret = wolfSSL_get_error(srv_ssl, ret);
  1016. printf("error %d on server accept\n", ret);
  1017. goto exit;
  1018. }
  1019. info->server_stats.connTime += start;
  1020. info->server_stats.connCount++;
  1021. /* echo loop */
  1022. ret = 0;
  1023. total_sz = 0;
  1024. while (ret == 0 && total_sz < info->maxSize) {
  1025. double rxTime;
  1026. /* read message from client */
  1027. XMEMSET(readBuf, 0, readBufSz);
  1028. start = gettime_secs(1);
  1029. #ifndef BENCH_USE_NONBLOCK
  1030. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1031. #else
  1032. do {
  1033. ret = wolfSSL_read(srv_ssl, readBuf, readBufSz);
  1034. err = wolfSSL_get_error(srv_ssl, ret);
  1035. }
  1036. while (err == WOLFSSL_ERROR_WANT_READ);
  1037. #endif
  1038. rxTime = gettime_secs(0) - start;
  1039. /* shutdown signals, no more connections for this cipher */
  1040. if (XSTRSTR((const char*)readBuf, kShutdown) != NULL) {
  1041. info->server.shutdown = 1;
  1042. if (info->showVerbose) {
  1043. printf("Server shutdown done\n");
  1044. }
  1045. ret = 0; /* success */
  1046. break;
  1047. }
  1048. info->server_stats.rxTime += rxTime;
  1049. if (ret < 0) {
  1050. ret = wolfSSL_get_error(srv_ssl, ret);
  1051. printf("error %d on server read\n", ret);
  1052. goto exit;
  1053. }
  1054. info->server_stats.rxTotal += ret;
  1055. len = ret;
  1056. total_sz += ret;
  1057. /* write message back to client */
  1058. start = gettime_secs(1);
  1059. #ifndef BENCH_USE_NONBLOCK
  1060. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1061. #else
  1062. do {
  1063. ret = wolfSSL_write(srv_ssl, readBuf, len);
  1064. err = wolfSSL_get_error(srv_ssl, ret);
  1065. }
  1066. while (err == WOLFSSL_ERROR_WANT_WRITE);
  1067. #endif
  1068. info->server_stats.txTime += gettime_secs(0) - start;
  1069. if (ret < 0) {
  1070. ret = wolfSSL_get_error(srv_ssl, ret);
  1071. printf("error %d on server write\n", ret);
  1072. goto exit;
  1073. }
  1074. info->server_stats.txTotal += ret;
  1075. ret = 0; /* reset return code */
  1076. }
  1077. wolfSSL_free(srv_ssl);
  1078. srv_ssl = NULL;
  1079. }
  1080. exit:
  1081. if (ret != 0 && ret != WOLFSSL_SUCCESS) {
  1082. info->doShutdown = 1;
  1083. printf("Server Error: %d (%s)\n", ret,
  1084. wolfSSL_ERR_reason_error_string(ret));
  1085. }
  1086. /* clean up */
  1087. if (srv_ssl != NULL)
  1088. wolfSSL_free(srv_ssl);
  1089. if (srv_ctx != NULL)
  1090. wolfSSL_CTX_free(srv_ctx);
  1091. XFREE(readBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1092. info->server.ret = ret;
  1093. return ret;
  1094. }
  1095. #ifdef CMSIS_OS2_H_
  1096. static void server_thread(void* args)
  1097. #else
  1098. static void server_thread(const void* args)
  1099. #endif
  1100. {
  1101. int ret;
  1102. info_t* info = (info_t*)args;
  1103. #ifdef CMSIS_OS2_H_
  1104. info->server.threadId = osThreadGetId();
  1105. #endif
  1106. do {
  1107. ret = bench_tls_server(info);
  1108. /* signal client */
  1109. if (!info->client.done && info->client.threadId != 0) {
  1110. #ifdef CMSIS_OS2_H_
  1111. osThreadFlagsSet(info->client.threadId, 1);
  1112. #else
  1113. osSignalSet(info->client.threadId, 1);
  1114. #endif
  1115. }
  1116. info->server.ret = ret;
  1117. info->server.done = 1;
  1118. osThreadSuspend(info->server.threadId);
  1119. if (info->doShutdown)
  1120. info->client.done = 1;
  1121. } while (!info->doShutdown);
  1122. osThreadTerminate(info->server.threadId);
  1123. info->server.threadId = NULL;
  1124. }
  1125. #ifdef CMSIS_OS2_H_
  1126. static const osThreadAttr_t server_thread_attributes = {
  1127. .name = "server_thread",
  1128. .priority = (osPriority_t) osPriorityNormal,
  1129. .stack_size = WOLF_EXAMPLES_STACK
  1130. };
  1131. static const osThreadAttr_t client_thread_attributes = {
  1132. .name = "client_thread",
  1133. .priority = (osPriority_t) osPriorityNormal,
  1134. .stack_size = WOLF_EXAMPLES_STACK
  1135. };
  1136. #endif
  1137. int bench_tls(void* args)
  1138. {
  1139. int ret = 0;
  1140. info_t *info = NULL;
  1141. char *cipher, *next_cipher, *ciphers;
  1142. /* Runtime variables */
  1143. int argRuntimeSec = BENCH_RUNTIME_SEC;
  1144. int argTestPacketSize = TEST_PACKET_SIZE;
  1145. int argTestMaxSize = TEST_MAX_SIZE;
  1146. int argShowVerbose = SHOW_VERBOSE;
  1147. const char* argHost = BENCH_DEFAULT_HOST;
  1148. int argPort = BENCH_DEFAULT_PORT;
  1149. int argShowPeerInfo = BENCH_SHOW_PEER_INFO;
  1150. #ifdef DEBUG_WOLFSSL
  1151. if (argShowVerbose >= 3) {
  1152. wolfSSL_Debugging_ON();
  1153. }
  1154. else {
  1155. wolfSSL_Debugging_OFF();
  1156. }
  1157. #endif
  1158. #ifdef TEST_CIPHER_SUITE
  1159. ciphers = TEST_CIPHER_SUITE;
  1160. #else
  1161. /* Get cipher suite list */
  1162. ciphers = (char*)XMALLOC(WOLFSSL_CIPHER_LIST_MAX_SIZE, NULL,
  1163. DYNAMIC_TYPE_TMP_BUFFER);
  1164. if (ciphers == NULL) {
  1165. ret = MEMORY_E; goto exit;
  1166. }
  1167. wolfSSL_get_ciphers(ciphers, WOLFSSL_CIPHER_LIST_MAX_SIZE);
  1168. #endif
  1169. cipher = ciphers;
  1170. /* Allocate test info */
  1171. info = (info_t*)XMALLOC(sizeof(info_t), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1172. if (info == NULL) {
  1173. ret = MEMORY_E; goto exit;
  1174. }
  1175. XMEMSET(info, 0, sizeof(info_t));
  1176. info->host = argHost;
  1177. info->port = argPort;
  1178. info->packetSize = argTestPacketSize;
  1179. info->runTimeSec = argRuntimeSec;
  1180. info->maxSize = argTestMaxSize;
  1181. info->showPeerInfo = argShowPeerInfo;
  1182. info->showVerbose = argShowVerbose;
  1183. #ifdef CMSIS_OS2_H_
  1184. info->server.mutex = osSemaphoreNew(1, 0, NULL);
  1185. info->client.mutex = osSemaphoreNew(1, 0, NULL);
  1186. #else
  1187. info->server.mutex = osSemaphoreCreate(&info->server.mutexDef, 1);
  1188. info->client.mutex = osSemaphoreCreate(&info->client.mutexDef, 1);
  1189. /* threads */
  1190. info->server.threadDef.name = "server_thread";
  1191. info->server.threadDef.pthread = server_thread;
  1192. info->server.threadDef.tpriority = osPriorityNormal;
  1193. info->server.threadDef.stacksize = WOLF_EXAMPLES_STACK;
  1194. info->client.threadDef.name = "client_thread";
  1195. info->client.threadDef.pthread = client_thread;
  1196. info->client.threadDef.tpriority = osPriorityNormal;
  1197. info->client.threadDef.stacksize = WOLF_EXAMPLES_STACK;
  1198. #endif
  1199. /* parse by : */
  1200. while ((cipher != NULL) && (cipher[0] != '\0')) {
  1201. next_cipher = strchr(cipher, ':');
  1202. if (next_cipher != NULL) {
  1203. cipher[next_cipher - cipher] = '\0';
  1204. }
  1205. if (argShowVerbose) {
  1206. printf("Cipher: %s\n", cipher);
  1207. }
  1208. /* set cipher suite */
  1209. info->cipher = cipher;
  1210. /* reset thread info */
  1211. info->server.ret = info->client.ret = 0;
  1212. info->server.done = info->client.done = 0;
  1213. info->server.shutdown = info->client.shutdown = 0;
  1214. XMEMSET(&info->server_stats, 0, sizeof(info->server_stats));
  1215. XMEMSET(&info->client_stats, 0, sizeof(info->client_stats));
  1216. XMEMSET(&info->to_server, 0, sizeof(info->to_server));
  1217. XMEMSET(&info->to_client, 0, sizeof(info->to_client));
  1218. /* make sure nothing is locking it */
  1219. osSemaphoreRelease(info->server.mutex);
  1220. osSemaphoreRelease(info->client.mutex);
  1221. /* start threads */
  1222. if (info->server.threadId == 0) {
  1223. #ifdef CMSIS_OS2_H_
  1224. osThreadNew(server_thread, info, &server_thread_attributes);
  1225. #else
  1226. info->server.threadId = osThreadCreate(&info->server.threadDef, info);
  1227. #endif
  1228. }
  1229. else {
  1230. osThreadResume(info->server.threadId);
  1231. }
  1232. if (info->client.threadId == 0) {
  1233. #ifdef CMSIS_OS2_H_
  1234. osThreadNew(client_thread, info, &client_thread_attributes);
  1235. #else
  1236. info->client.threadId = osThreadCreate(&info->client.threadDef, info);
  1237. #endif
  1238. }
  1239. else {
  1240. osThreadResume(info->client.threadId);
  1241. }
  1242. /* Wait until threads are marked done */
  1243. while (!info->client.done || !info->server.done) {
  1244. osThreadYield(); /* Allow other threads to run */
  1245. }
  1246. if (argShowVerbose) {
  1247. /* print results */
  1248. PrintTlsStats(&info->server_stats, "Server", info->cipher, 1);
  1249. PrintTlsStats(&info->client_stats, "Client", info->cipher, 1);
  1250. }
  1251. printf("%-6s %-33s %11s %9s %9s %9s %9s %9s %17s %15s\n",
  1252. "Side", "Cipher", "Total Bytes", "Num Conns", "Rx ms", "Tx ms",
  1253. "Rx MB/s", "Tx MB/s", "Connect Total ms", "Connect Avg ms");
  1254. PrintTlsStats(&info->server_stats, "Server", info->cipher, 0);
  1255. PrintTlsStats(&info->client_stats, "Client", info->cipher, 0);
  1256. PrintMemStats();
  1257. /* target next cipher */
  1258. cipher = (next_cipher != NULL) ? (next_cipher + 1) : NULL;
  1259. } /* while */
  1260. /* do thread shutdown */
  1261. info->doShutdown = 1;
  1262. info->server.done = 0;
  1263. info->client.done = 0;
  1264. osThreadResume(info->server.threadId);
  1265. osThreadResume(info->client.threadId);
  1266. /* Wait until threads are marked done */
  1267. while (!info->client.done || !info->server.done) {
  1268. osThreadYield(); /* Allow other threads to run */
  1269. }
  1270. exit:
  1271. /* cleanup thread info */
  1272. osSemaphoreDelete(info->server.mutex);
  1273. osSemaphoreDelete(info->client.mutex);
  1274. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1275. #ifndef TEST_CIPHER_SUITE
  1276. /* Free cipher list */
  1277. XFREE(ciphers, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1278. #endif
  1279. /* set return code */
  1280. if (args)
  1281. ((func_args*)args)->return_code = ret;
  1282. return ret;
  1283. }
  1284. #endif /* ENABLE_TLS_BENCH */
  1285. #ifndef WOLFCRYPT_ONLY
  1286. static void ShowCiphers(void)
  1287. {
  1288. int ret;
  1289. char* ciphers = (char*)XMALLOC(WOLFSSL_CIPHER_LIST_MAX_SIZE, NULL,
  1290. DYNAMIC_TYPE_TMP_BUFFER);
  1291. if (ciphers) {
  1292. ret = wolfSSL_get_ciphers(ciphers, WOLFSSL_CIPHER_LIST_MAX_SIZE);
  1293. if (ret == WOLFSSL_SUCCESS)
  1294. printf("%s\n", ciphers);
  1295. XFREE(ciphers, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1296. }
  1297. }
  1298. #endif
  1299. static void PrintMemStats(void)
  1300. {
  1301. #ifdef WOLFSSL_DEBUG_MEMORY
  1302. printf("\nHeap MinEver %d, Free %d, Stack %lu\n",
  1303. xPortGetMinimumEverFreeHeapSize(),
  1304. xPortGetFreeHeapSize(),
  1305. uxTaskGetStackHighWaterMark(NULL));
  1306. #endif
  1307. }
  1308. #ifdef ENABLE_TLS_UART
  1309. /* UART DMA IO Routines */
  1310. #ifndef B115200
  1311. #define B115200 115200
  1312. #endif
  1313. /* Max buffer for a single TLS frame */
  1314. #ifndef MAX_RECORD_SIZE
  1315. #define MAX_RECORD_SIZE (16 * 1024)
  1316. #endif
  1317. typedef struct {
  1318. int curr_index;
  1319. int data_len;
  1320. char buf[MAX_RECORD_SIZE];
  1321. } tls13_buf;
  1322. /* This sets which UART to do the TLS 1.3 connection over. It is something you
  1323. * will have to configure in STMCubeIDE and then change here. */
  1324. #ifndef TLS_UART
  1325. #define TLS_UART huart2
  1326. #endif
  1327. /* If you get an undefined error here you can optionally disable the TLS
  1328. * over UART test using NO_TLS_UART_TEST */
  1329. extern UART_HandleTypeDef TLS_UART;
  1330. static int msg_length = 0;
  1331. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
  1332. if (huart->Instance == TLS_UART.Instance) {
  1333. msg_length = Size;
  1334. }
  1335. }
  1336. static int uartIORx(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  1337. {
  1338. HAL_StatusTypeDef status;
  1339. tls13_buf *tb = ctx;
  1340. #ifdef DEBUG_UART_IO
  1341. printf("UART Read: In %d\n", sz);
  1342. #endif
  1343. if (tb->curr_index + sz <= tb->data_len) {
  1344. XMEMCPY(buf, tb->buf + tb->curr_index, sz);
  1345. tb->curr_index += sz;
  1346. #ifdef DEBUG_UART_IO
  1347. printf("UART Read1: Out %d\n", sz);
  1348. #endif
  1349. return sz;
  1350. }
  1351. msg_length = 0;
  1352. XMEMSET(tb, 0, sizeof(*tb));
  1353. /* Now setup the DMA RX */
  1354. /* This requires enabling the UART RX DMA in the STM32Cube tool
  1355. * Under Connectivity click on your TLS UART (USART2) and goto DMA Settings
  1356. * and Add one for USART2_RX with default options */
  1357. status = HAL_UARTEx_ReceiveToIdle_DMA(&TLS_UART, (uint8_t *)tb->buf, MAX_RECORD_SIZE);
  1358. if (status != HAL_OK) {
  1359. return WOLFSSL_CBIO_ERR_WANT_READ;
  1360. }
  1361. else {
  1362. /* We now go into an infinite loop waiting for msg_length to be set to a
  1363. * value other than 0. This will be done when the other side writes to
  1364. * UART and then idles. That will trigger HAL_UARTEx_RxEventCallback()
  1365. * which will set msg_length to the length of data written.
  1366. *
  1367. * If you mistakenly get stuck here, please simply reset the board.
  1368. */
  1369. while (msg_length == 0) {
  1370. HAL_Delay(10);
  1371. }
  1372. #ifdef DEBUG_UART_IO
  1373. printf("Message received! length = %d\n", msg_length);
  1374. #endif
  1375. }
  1376. /* now return the number of bytes requested. */
  1377. XMEMCPY(buf, tb->buf, sz);
  1378. tb->data_len = msg_length;
  1379. tb->curr_index = sz;
  1380. #ifdef DEBUG_UART_IO
  1381. printf("UART Read2: Out %d\n", tb->data_len);
  1382. #endif
  1383. return sz;
  1384. }
  1385. static int uartIOTx(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  1386. {
  1387. HAL_StatusTypeDef status;
  1388. int ret = sz;
  1389. (void)ctx;
  1390. #ifdef DEBUG_UART_IO
  1391. printf("UART Write: In %d\n", sz);
  1392. #endif
  1393. status = HAL_UART_Transmit(&TLS_UART, (uint8_t *)buf, sz, 0xFFFF);
  1394. if (status != HAL_OK) {
  1395. ret = WOLFSSL_CBIO_ERR_WANT_WRITE;
  1396. }
  1397. #ifdef DEBUG_UART_IO
  1398. printf("UART Write: Out %d\n", ret);
  1399. #endif
  1400. return ret;
  1401. }
  1402. static void uartReset(void)
  1403. {
  1404. HAL_UART_Abort_IT(&TLS_UART);
  1405. }
  1406. /* UART TLS 1.3 client and server */
  1407. #ifndef NO_WOLFSSL_SERVER
  1408. static int tls13_uart_server(void)
  1409. {
  1410. int ret = -1, err;
  1411. WOLFSSL_CTX* ctx = NULL;
  1412. WOLFSSL* ssl = NULL;
  1413. byte echoBuffer[100];
  1414. #ifdef WOLFSSL_SMALL_STACK
  1415. tls13_buf* tbuf = (tls13_buf*)XMALLOC(sizeof(*tbuf), NULL,
  1416. DYNAMIC_TYPE_TMP_BUFFER);
  1417. if (tbuf == NULL) {
  1418. printf("Memory allocation error\n");
  1419. goto done;
  1420. }
  1421. #else
  1422. tls13_buf tbuf[1];
  1423. #endif
  1424. XMEMSET(tbuf, 0, sizeof(tls13_buf));
  1425. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  1426. if (ctx == NULL) {
  1427. printf("Error creating WOLFSSL_CTX\n");
  1428. goto done;
  1429. }
  1430. /* Register wolfSSL send/recv callbacks */
  1431. uartReset();
  1432. wolfSSL_CTX_SetIOSend(ctx, uartIOTx);
  1433. wolfSSL_CTX_SetIORecv(ctx, uartIORx);
  1434. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, ecc_key_der_256,
  1435. sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
  1436. if (ret != WOLFSSL_SUCCESS) {
  1437. printf("error loading server private key\n");
  1438. goto done;
  1439. }
  1440. ret = wolfSSL_CTX_use_certificate_buffer(ctx, serv_ecc_der_256,
  1441. sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
  1442. if (ret != WOLFSSL_SUCCESS) {
  1443. printf("error loading server certificate\n");
  1444. goto done;
  1445. }
  1446. ssl = wolfSSL_new(ctx);
  1447. if (ssl == NULL) {
  1448. printf("Error creating WOLFSSL\n");
  1449. goto done;
  1450. }
  1451. wolfSSL_SetIOReadCtx(ssl, tbuf);
  1452. printf("Waiting for client\n");
  1453. do {
  1454. ret = wolfSSL_accept(ssl);
  1455. err = wolfSSL_get_error(ssl, ret);
  1456. } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1457. if (ret != WOLFSSL_SUCCESS) {
  1458. printf("TLS accept error %d\n", err);
  1459. goto done;
  1460. }
  1461. printf("TLS Accept handshake done\n");
  1462. /* Waiting for data to echo */
  1463. XMEMSET(echoBuffer, 0, sizeof(echoBuffer));
  1464. do {
  1465. ret = wolfSSL_read(ssl, echoBuffer, sizeof(echoBuffer)-1);
  1466. err = wolfSSL_get_error(ssl, ret);
  1467. } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1468. printf("Read (%d): %s\n", err, echoBuffer);
  1469. do {
  1470. ret = wolfSSL_write(ssl, echoBuffer, XSTRLEN((char*)echoBuffer));
  1471. err = wolfSSL_get_error(ssl, ret);
  1472. } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1473. printf("Sent (%d): %s\n", err, echoBuffer);
  1474. ret = 0; /* Success */
  1475. done:
  1476. if (ssl) {
  1477. wolfSSL_shutdown(ssl);
  1478. wolfSSL_free(ssl);
  1479. }
  1480. if (ctx) {
  1481. wolfSSL_CTX_free(ctx);
  1482. }
  1483. #ifdef WOLFSSL_SMALL_STACK
  1484. if (tbuf != NULL) {
  1485. XFREE(tbuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1486. }
  1487. #endif
  1488. return ret;
  1489. }
  1490. #endif
  1491. #ifdef ENABLE_TLS_UART
  1492. static int tls13_uart_client(void)
  1493. {
  1494. int ret = -1, err;
  1495. WOLFSSL_CTX* ctx = NULL;
  1496. WOLFSSL* ssl = NULL;
  1497. const char testStr[] = "Testing 1, 2 and 3\r\n";
  1498. byte readBuf[100];
  1499. #ifdef WOLFSSL_SMALL_STACK
  1500. tls13_buf* tbuf = (tls13_buf*)XMALLOC(sizeof(*tbuf), NULL,
  1501. DYNAMIC_TYPE_TMP_BUFFER);
  1502. if (tbuf == NULL) {
  1503. printf("Memory allocation error\n");
  1504. goto done;
  1505. }
  1506. #else
  1507. tls13_buf tbuf[1];
  1508. #endif
  1509. XMEMSET(tbuf, 0, sizeof(tls13_buf));
  1510. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  1511. if (ctx == NULL) {
  1512. printf("Error creating WOLFSSL_CTX\n");
  1513. goto done;
  1514. }
  1515. /* Register wolfSSL send/recv callbacks */
  1516. uartReset();
  1517. wolfSSL_CTX_SetIOSend(ctx, uartIOTx);
  1518. wolfSSL_CTX_SetIORecv(ctx, uartIORx);
  1519. /* Load the root certificate. */
  1520. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
  1521. ssl = wolfSSL_new(ctx);
  1522. if (ssl == NULL) {
  1523. printf("Error creating WOLFSSL\n");
  1524. goto done;
  1525. }
  1526. wolfSSL_SetIOReadCtx(ssl, tbuf);
  1527. #ifdef HAVE_PQC
  1528. if (wolfSSL_UseKeyShare(ssl, WOLFSSL_KYBER_LEVEL1) != WOLFSSL_SUCCESS) {
  1529. printf("wolfSSL_UseKeyShare Error!!");
  1530. }
  1531. #endif
  1532. do {
  1533. ret = wolfSSL_connect(ssl);
  1534. err = wolfSSL_get_error(ssl, ret);
  1535. } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1536. if (ret != WOLFSSL_SUCCESS) {
  1537. printf("TLS connect error %d\n", err);
  1538. goto done;
  1539. }
  1540. ShowPeer(ssl);
  1541. printf("TLS Connect handshake done\n");
  1542. printf("Sending test string\n");
  1543. do {
  1544. ret = wolfSSL_write(ssl, testStr, XSTRLEN(testStr));
  1545. err = wolfSSL_get_error(ssl, ret);
  1546. } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1547. printf("Sent (%d): %s\n", err, testStr);
  1548. XMEMSET(readBuf, 0, sizeof(readBuf));
  1549. do {
  1550. ret = wolfSSL_read(ssl, readBuf, sizeof(readBuf)-1);
  1551. err = wolfSSL_get_error(ssl, ret);
  1552. } while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
  1553. printf("Read (%d): %s\n", err, readBuf);
  1554. ret = 0; /* Success */
  1555. done:
  1556. if (ssl) {
  1557. wolfSSL_shutdown(ssl);
  1558. wolfSSL_free(ssl);
  1559. }
  1560. if (ctx) {
  1561. wolfSSL_CTX_free(ctx);
  1562. }
  1563. #ifdef WOLFSSL_SMALL_STACK
  1564. if (tbuf != NULL) {
  1565. XFREE(tbuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1566. }
  1567. #endif
  1568. return ret;
  1569. }
  1570. #endif
  1571. #endif /* ENABLE_TLS_UART */
  1572. /*****************************************************************************
  1573. * Public functions
  1574. ****************************************************************************/
  1575. #ifdef HAL_RTC_MODULE_ENABLED
  1576. extern RTC_HandleTypeDef hrtc;
  1577. double current_time(void)
  1578. {
  1579. RTC_TimeTypeDef time;
  1580. RTC_DateTypeDef date;
  1581. uint32_t subsec = 0;
  1582. /* must get time and date here due to STM32 HW bug */
  1583. HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN);
  1584. HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN);
  1585. /* Not all STM32 RTCs have subseconds in the struct */
  1586. #ifdef RTC_ALARMSUBSECONDMASK_ALL
  1587. subsec = (255 - time.SubSeconds) * 1000 / 255;
  1588. #endif
  1589. (void) date;
  1590. /* return seconds.milliseconds */
  1591. return ((double) time.Hours * 24) + ((double) time.Minutes * 60)
  1592. + (double) time.Seconds + ((double) subsec / 1000);
  1593. }
  1594. #endif /* HAL_RTC_MODULE_ENABLED */
  1595. #ifdef CMSIS_OS2_H_
  1596. void wolfCryptDemo(void* argument)
  1597. #else
  1598. void wolfCryptDemo(const void* argument)
  1599. #endif
  1600. {
  1601. HAL_StatusTypeDef halRet;
  1602. uint8_t buffer[2];
  1603. func_args args;
  1604. #if 0
  1605. wolfSSL_Debugging_ON();
  1606. #endif
  1607. /* initialize wolfSSL */
  1608. #ifdef WOLFCRYPT_ONLY
  1609. wolfCrypt_Init();
  1610. #else
  1611. wolfSSL_Init();
  1612. #endif
  1613. #ifdef WOLFSSL_STATIC_MEMORY
  1614. if (wc_LoadStaticMemory(&HEAP_HINT, gWolfMem, sizeof(gWolfMem),
  1615. WOLFMEM_GENERAL, 10) != 0) {
  1616. printf("unable to load static memory");
  1617. }
  1618. #endif
  1619. //wolfSSL_SetAllocators(wolfMallocCb, wolfFreeCb, wolfReallocCb);
  1620. while (1) {
  1621. memset(&args, 0, sizeof(args));
  1622. args.return_code = NOT_COMPILED_IN; /* default */
  1623. printf("\n\t\t\t\tMENU\n");
  1624. printf(menu1);
  1625. printf("Please select one of the above options:\n");
  1626. do {
  1627. halRet = HAL_UART_Receive(&HAL_CONSOLE_UART, buffer, sizeof(buffer), 100);
  1628. } while (halRet != HAL_OK || buffer[0] == '\n' || buffer[0] == '\r');
  1629. switch (buffer[0]) {
  1630. case 't':
  1631. printf("Running wolfCrypt Tests...\n");
  1632. #ifndef NO_CRYPT_TEST
  1633. args.return_code = 0;
  1634. wolfcrypt_test(&args);
  1635. #else
  1636. args.return_code = NOT_COMPILED_IN;
  1637. #endif
  1638. printf("Crypt Test: Return code %d\n", args.return_code);
  1639. break;
  1640. case 'b':
  1641. printf("Running wolfCrypt Benchmarks...\n");
  1642. #ifndef NO_CRYPT_BENCHMARK
  1643. args.return_code = 0;
  1644. benchmark_test(&args);
  1645. #else
  1646. args.return_code = NOT_COMPILED_IN;
  1647. #endif
  1648. printf("Benchmark Test: Return code %d\n", args.return_code);
  1649. break;
  1650. case 'l':
  1651. printf("Running TLS Benchmarks...\n");
  1652. #ifdef ENABLE_TLS_BENCH
  1653. bench_tls(&args);
  1654. #else
  1655. args.return_code = NOT_COMPILED_IN;
  1656. #endif
  1657. printf("TLS Benchmarks: Return code %d\n", args.return_code);
  1658. break;
  1659. case 'e':
  1660. #ifndef WOLFCRYPT_ONLY
  1661. ShowCiphers();
  1662. #else
  1663. printf("Not compiled in\n");
  1664. #endif
  1665. break;
  1666. #ifdef ENABLE_TLS_UART
  1667. case 's':
  1668. #ifndef NO_WOLFSSL_SERVER
  1669. printf("Running TLS 1.3 server...\n");
  1670. args.return_code = tls13_uart_server();
  1671. #else
  1672. args.return_code = NOT_COMPILED_IN;
  1673. #endif
  1674. printf("TLS 1.3 Server: Return code %d\n", args.return_code);
  1675. break;
  1676. case 'c':
  1677. #ifndef NO_WOLFSSL_CLIENT
  1678. printf("Running TLS 1.3 client...\n");
  1679. args.return_code = tls13_uart_client();
  1680. #else
  1681. args.return_code = NOT_COMPILED_IN;
  1682. #endif
  1683. printf("TLS 1.3 Client: Return code %d\n", args.return_code);
  1684. break;
  1685. #endif /* ENABLE_TLS_UART */
  1686. /* All other cases go here */
  1687. default:
  1688. printf("\nSelection out of range\n");
  1689. break;
  1690. }
  1691. PrintMemStats();
  1692. }
  1693. #ifdef WOLFCRYPT_ONLY
  1694. wolfCrypt_Cleanup();
  1695. #else
  1696. wolfSSL_Cleanup();
  1697. #endif
  1698. }