keys.c 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978
  1. /* keys.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. /* Name change compatibility layer no longer needs to be included here */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifndef WOLFCRYPT_ONLY
  27. #include <wolfssl/internal.h>
  28. #include <wolfssl/error-ssl.h>
  29. #if defined(SHOW_SECRETS) || defined(CHACHA_AEAD_TEST)
  30. #ifndef NO_STDIO_FILESYSTEM
  31. #include <stdio.h>
  32. #endif
  33. #endif
  34. #if defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_TSIP_TLS)
  35. #include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
  36. #endif
  37. int SetCipherSpecs(WOLFSSL* ssl)
  38. {
  39. int ret = GetCipherSpec(ssl->options.side, ssl->options.cipherSuite0,
  40. ssl->options.cipherSuite, &ssl->specs,
  41. &ssl->options);
  42. if (ret == 0) {
  43. /* set TLS if it hasn't been turned off */
  44. if (ssl->version.major == SSLv3_MAJOR &&
  45. ssl->version.minor >= TLSv1_MINOR) {
  46. #ifndef NO_TLS
  47. ssl->options.tls = 1;
  48. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  49. #if !defined(WOLFSSL_RENESAS_SCEPROTECT) && \
  50. !defined(WOLFSSL_RENESAS_TSIP_TLS)
  51. ssl->hmac = TLS_hmac;
  52. #else
  53. ssl->hmac = Renesas_cmn_TLS_hmac;
  54. #endif
  55. #endif
  56. if (ssl->version.minor >= TLSv1_1_MINOR) {
  57. ssl->options.tls1_1 = 1;
  58. if (ssl->version.minor >= TLSv1_3_MINOR)
  59. ssl->options.tls1_3 = 1;
  60. }
  61. #endif
  62. }
  63. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  64. if (IsAtLeastTLSv1_3(ssl->version) || ssl->specs.cipher_type != block)
  65. ssl->options.encThenMac = 0;
  66. #endif
  67. #if defined(WOLFSSL_DTLS)
  68. if (ssl->options.dtls && ssl->version.major == DTLS_MAJOR) {
  69. #ifndef WOLFSSL_AEAD_ONLY
  70. #if !defined(WOLFSSL_RENESAS_SCEPROTECT) && \
  71. !defined(WOLFSSL_RENESAS_TSIP_TLS)
  72. ssl->hmac = TLS_hmac;
  73. #else
  74. ssl->hmac = Renesas_cmn_TLS_hmac;
  75. #endif
  76. #endif
  77. ssl->options.tls = 1;
  78. ssl->options.tls1_1 = 1; /* DTLS 1.0 == TLS 1.1 */
  79. #ifdef WOLFSSL_DTLS13
  80. if (ssl->version.minor <= DTLSv1_3_MINOR)
  81. ssl->options.tls1_3 = 1;
  82. #endif
  83. }
  84. #endif
  85. }
  86. return ret;
  87. }
  88. /**
  89. * Populate specs with the specification of the chosen ciphersuite. If opts is
  90. * not NULL then the appropriate options will also be set.
  91. *
  92. * @param side [in] WOLFSSL_SERVER_END or WOLFSSL_CLIENT_END
  93. * @param cipherSuite0 [in]
  94. * @param cipherSuite [in]
  95. * @param specs [out] CipherSpecs
  96. * @param opts [in/out] Options can be NULL
  97. * @return
  98. */
  99. int GetCipherSpec(word16 side, byte cipherSuite0, byte cipherSuite,
  100. CipherSpecs* specs, Options* opts)
  101. {
  102. word16 havePSK = 0;
  103. (void)havePSK;
  104. (void)side;
  105. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  106. if (opts != NULL)
  107. havePSK = opts->havePSK;
  108. #endif
  109. #ifndef NO_WOLFSSL_CLIENT
  110. if (side == WOLFSSL_CLIENT_END) {
  111. /* server side verified before SetCipherSpecs call */
  112. if (VerifyClientSuite(havePSK, cipherSuite0, cipherSuite) != 1) {
  113. WOLFSSL_MSG("SetCipherSpecs() client has an unusable suite");
  114. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_SUITE);
  115. return UNSUPPORTED_SUITE;
  116. }
  117. }
  118. #endif /* NO_WOLFSSL_CLIENT */
  119. /* Chacha extensions, 0xcc */
  120. if (cipherSuite0 == CHACHA_BYTE) {
  121. switch (cipherSuite) {
  122. #ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
  123. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  124. specs->bulk_cipher_algorithm = wolfssl_chacha;
  125. specs->cipher_type = aead;
  126. specs->mac_algorithm = sha256_mac;
  127. specs->kea = ecc_diffie_hellman_kea;
  128. specs->sig_algo = rsa_sa_algo;
  129. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  130. specs->pad_size = PAD_SHA;
  131. specs->static_ecdh = 0;
  132. specs->key_size = CHACHA20_256_KEY_SIZE;
  133. specs->block_size = CHACHA20_BLOCK_SIZE;
  134. specs->iv_size = CHACHA20_IV_SIZE;
  135. specs->aead_mac_size = POLY1305_AUTH_SZ;
  136. if (opts != NULL)
  137. opts->oldPoly = 1; /* use old poly1305 padding */
  138. break;
  139. #endif
  140. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
  141. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  142. specs->bulk_cipher_algorithm = wolfssl_chacha;
  143. specs->cipher_type = aead;
  144. specs->mac_algorithm = sha256_mac;
  145. specs->kea = ecc_diffie_hellman_kea;
  146. specs->sig_algo = ecc_dsa_sa_algo;
  147. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  148. specs->pad_size = PAD_SHA;
  149. specs->static_ecdh = 0;
  150. specs->key_size = CHACHA20_256_KEY_SIZE;
  151. specs->block_size = CHACHA20_BLOCK_SIZE;
  152. specs->iv_size = CHACHA20_IV_SIZE;
  153. specs->aead_mac_size = POLY1305_AUTH_SZ;
  154. if (opts != NULL)
  155. opts->oldPoly = 1; /* use old poly1305 padding */
  156. break;
  157. #endif
  158. #ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
  159. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  160. specs->bulk_cipher_algorithm = wolfssl_chacha;
  161. specs->cipher_type = aead;
  162. specs->mac_algorithm = sha256_mac;
  163. specs->kea = diffie_hellman_kea;
  164. specs->sig_algo = rsa_sa_algo;
  165. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  166. specs->pad_size = PAD_SHA;
  167. specs->static_ecdh = 0;
  168. specs->key_size = CHACHA20_256_KEY_SIZE;
  169. specs->block_size = CHACHA20_BLOCK_SIZE;
  170. specs->iv_size = CHACHA20_IV_SIZE;
  171. specs->aead_mac_size = POLY1305_AUTH_SZ;
  172. if (opts != NULL)
  173. opts->oldPoly = 1; /* use old poly1305 padding */
  174. break;
  175. #endif
  176. #ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  177. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  178. specs->bulk_cipher_algorithm = wolfssl_chacha;
  179. specs->cipher_type = aead;
  180. specs->mac_algorithm = sha256_mac;
  181. specs->kea = ecc_diffie_hellman_kea;
  182. specs->sig_algo = rsa_sa_algo;
  183. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  184. specs->pad_size = PAD_SHA;
  185. specs->static_ecdh = 0;
  186. specs->key_size = CHACHA20_256_KEY_SIZE;
  187. specs->block_size = CHACHA20_BLOCK_SIZE;
  188. specs->iv_size = CHACHA20_IV_SIZE;
  189. specs->aead_mac_size = POLY1305_AUTH_SZ;
  190. if (opts != NULL)
  191. opts->oldPoly = 0; /* use recent padding RFC */
  192. break;
  193. #endif
  194. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  195. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
  196. specs->bulk_cipher_algorithm = wolfssl_chacha;
  197. specs->cipher_type = aead;
  198. specs->mac_algorithm = sha256_mac;
  199. specs->kea = ecc_diffie_hellman_kea;
  200. specs->sig_algo = ecc_dsa_sa_algo;
  201. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  202. specs->pad_size = PAD_SHA;
  203. specs->static_ecdh = 0;
  204. specs->key_size = CHACHA20_256_KEY_SIZE;
  205. specs->block_size = CHACHA20_BLOCK_SIZE;
  206. specs->iv_size = CHACHA20_IV_SIZE;
  207. specs->aead_mac_size = POLY1305_AUTH_SZ;
  208. if (opts != NULL)
  209. opts->oldPoly = 0; /* use recent padding RFC */
  210. break;
  211. #endif
  212. #ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
  213. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  214. specs->bulk_cipher_algorithm = wolfssl_chacha;
  215. specs->cipher_type = aead;
  216. specs->mac_algorithm = sha256_mac;
  217. specs->kea = diffie_hellman_kea;
  218. specs->sig_algo = rsa_sa_algo;
  219. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  220. specs->pad_size = PAD_SHA;
  221. specs->static_ecdh = 0;
  222. specs->key_size = CHACHA20_256_KEY_SIZE;
  223. specs->block_size = CHACHA20_BLOCK_SIZE;
  224. specs->iv_size = CHACHA20_IV_SIZE;
  225. specs->aead_mac_size = POLY1305_AUTH_SZ;
  226. if (opts != NULL)
  227. opts->oldPoly = 0; /* use recent padding RFC */
  228. break;
  229. #endif
  230. #ifdef BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
  231. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  232. specs->bulk_cipher_algorithm = wolfssl_chacha;
  233. specs->cipher_type = aead;
  234. specs->mac_algorithm = sha256_mac;
  235. specs->kea = psk_kea;
  236. specs->sig_algo = anonymous_sa_algo;
  237. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  238. specs->pad_size = PAD_SHA;
  239. specs->static_ecdh = 0;
  240. specs->key_size = CHACHA20_256_KEY_SIZE;
  241. specs->block_size = CHACHA20_BLOCK_SIZE;
  242. specs->iv_size = CHACHA20_IV_SIZE;
  243. specs->aead_mac_size = POLY1305_AUTH_SZ;
  244. if (opts != NULL) {
  245. opts->oldPoly = 0; /* use recent padding RFC */
  246. opts->usingPSK_cipher = 1;
  247. }
  248. break;
  249. #endif
  250. #ifdef BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
  251. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  252. specs->bulk_cipher_algorithm = wolfssl_chacha;
  253. specs->cipher_type = aead;
  254. specs->mac_algorithm = sha256_mac;
  255. specs->kea = ecdhe_psk_kea;
  256. specs->sig_algo = anonymous_sa_algo;
  257. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  258. specs->pad_size = PAD_SHA;
  259. specs->static_ecdh = 0;
  260. specs->key_size = CHACHA20_256_KEY_SIZE;
  261. specs->block_size = CHACHA20_BLOCK_SIZE;
  262. specs->iv_size = CHACHA20_IV_SIZE;
  263. specs->aead_mac_size = POLY1305_AUTH_SZ;
  264. if (opts != NULL) {
  265. opts->oldPoly = 0; /* use recent padding RFC */
  266. opts->usingPSK_cipher = 1;
  267. }
  268. break;
  269. #endif
  270. #ifdef BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
  271. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  272. specs->bulk_cipher_algorithm = wolfssl_chacha;
  273. specs->cipher_type = aead;
  274. specs->mac_algorithm = sha256_mac;
  275. specs->kea = dhe_psk_kea;
  276. specs->sig_algo = anonymous_sa_algo;
  277. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  278. specs->pad_size = PAD_SHA;
  279. specs->static_ecdh = 0;
  280. specs->key_size = CHACHA20_256_KEY_SIZE;
  281. specs->block_size = CHACHA20_BLOCK_SIZE;
  282. specs->iv_size = CHACHA20_IV_SIZE;
  283. specs->aead_mac_size = POLY1305_AUTH_SZ;
  284. if (opts != NULL) {
  285. opts->oldPoly = 0; /* use recent padding RFC */
  286. opts->usingPSK_cipher = 1;
  287. }
  288. break;
  289. #endif
  290. default:
  291. WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs ChaCha");
  292. return UNSUPPORTED_SUITE;
  293. }
  294. }
  295. /* ECC extensions, AES-CCM or TLS 1.3 Integrity-only */
  296. if (cipherSuite0 == ECC_BYTE) {
  297. switch (cipherSuite) {
  298. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  299. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
  300. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
  301. specs->bulk_cipher_algorithm = wolfssl_aes;
  302. specs->cipher_type = block;
  303. specs->mac_algorithm = sha256_mac;
  304. specs->kea = ecc_diffie_hellman_kea;
  305. specs->sig_algo = rsa_sa_algo;
  306. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  307. specs->pad_size = PAD_SHA;
  308. specs->static_ecdh = 0;
  309. specs->key_size = AES_128_KEY_SIZE;
  310. specs->iv_size = AES_IV_SIZE;
  311. specs->block_size = AES_BLOCK_SIZE;
  312. break;
  313. #endif
  314. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
  315. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
  316. specs->bulk_cipher_algorithm = wolfssl_aes;
  317. specs->cipher_type = block;
  318. specs->mac_algorithm = sha384_mac;
  319. specs->kea = ecc_diffie_hellman_kea;
  320. specs->sig_algo = rsa_sa_algo;
  321. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  322. specs->pad_size = PAD_SHA;
  323. specs->static_ecdh = 0;
  324. specs->key_size = AES_256_KEY_SIZE;
  325. specs->iv_size = AES_IV_SIZE;
  326. specs->block_size = AES_BLOCK_SIZE;
  327. break;
  328. #endif
  329. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
  330. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
  331. specs->bulk_cipher_algorithm = wolfssl_aes;
  332. specs->cipher_type = block;
  333. specs->mac_algorithm = sha_mac;
  334. specs->kea = ecc_diffie_hellman_kea;
  335. specs->sig_algo = rsa_sa_algo;
  336. specs->hash_size = WC_SHA_DIGEST_SIZE;
  337. specs->pad_size = PAD_SHA;
  338. specs->static_ecdh = 0;
  339. specs->key_size = AES_128_KEY_SIZE;
  340. specs->block_size = AES_BLOCK_SIZE;
  341. specs->iv_size = AES_IV_SIZE;
  342. break;
  343. #endif
  344. #ifdef BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
  345. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
  346. specs->bulk_cipher_algorithm = wolfssl_triple_des;
  347. specs->cipher_type = block;
  348. specs->mac_algorithm = sha_mac;
  349. specs->kea = ecc_diffie_hellman_kea;
  350. specs->sig_algo = rsa_sa_algo;
  351. specs->hash_size = WC_SHA_DIGEST_SIZE;
  352. specs->pad_size = PAD_SHA;
  353. specs->static_ecdh = 0;
  354. specs->key_size = DES3_KEY_SIZE;
  355. specs->block_size = DES_BLOCK_SIZE;
  356. /* DES_IV_SIZE is incorrectly 16 in FIPS v2. It should be 8, same as the
  357. * block size. */
  358. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  359. specs->iv_size = DES_BLOCK_SIZE;
  360. #else
  361. specs->iv_size = DES_IV_SIZE;
  362. #endif
  363. break;
  364. #endif
  365. #ifdef BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
  366. case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
  367. specs->bulk_cipher_algorithm = wolfssl_rc4;
  368. specs->cipher_type = stream;
  369. specs->mac_algorithm = sha_mac;
  370. specs->kea = ecc_diffie_hellman_kea;
  371. specs->sig_algo = rsa_sa_algo;
  372. specs->hash_size = WC_SHA_DIGEST_SIZE;
  373. specs->pad_size = PAD_SHA;
  374. specs->static_ecdh = 0;
  375. specs->key_size = RC4_KEY_SIZE;
  376. specs->iv_size = 0;
  377. specs->block_size = 0;
  378. break;
  379. #endif
  380. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
  381. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
  382. specs->bulk_cipher_algorithm = wolfssl_aes;
  383. specs->cipher_type = block;
  384. specs->mac_algorithm = sha_mac;
  385. specs->kea = ecc_diffie_hellman_kea;
  386. specs->sig_algo = rsa_sa_algo;
  387. specs->hash_size = WC_SHA_DIGEST_SIZE;
  388. specs->pad_size = PAD_SHA;
  389. specs->static_ecdh = 0;
  390. specs->key_size = AES_256_KEY_SIZE;
  391. specs->block_size = AES_BLOCK_SIZE;
  392. specs->iv_size = AES_IV_SIZE;
  393. break;
  394. #endif
  395. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  396. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
  397. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  398. specs->cipher_type = aead;
  399. specs->mac_algorithm = sha256_mac;
  400. specs->kea = ecc_diffie_hellman_kea;
  401. specs->sig_algo = rsa_sa_algo;
  402. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  403. specs->pad_size = PAD_SHA;
  404. specs->static_ecdh = 0;
  405. specs->key_size = AES_128_KEY_SIZE;
  406. specs->block_size = AES_BLOCK_SIZE;
  407. specs->iv_size = AESGCM_IMP_IV_SZ;
  408. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  409. break;
  410. #endif
  411. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  412. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
  413. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  414. specs->cipher_type = aead;
  415. specs->mac_algorithm = sha384_mac;
  416. specs->kea = ecc_diffie_hellman_kea;
  417. specs->sig_algo = rsa_sa_algo;
  418. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  419. specs->pad_size = PAD_SHA;
  420. specs->static_ecdh = 0;
  421. specs->key_size = AES_256_KEY_SIZE;
  422. specs->block_size = AES_BLOCK_SIZE;
  423. specs->iv_size = AESGCM_IMP_IV_SZ;
  424. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  425. break;
  426. #endif
  427. #ifdef BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
  428. case TLS_ECDHE_PSK_WITH_NULL_SHA256 :
  429. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  430. specs->cipher_type = stream;
  431. specs->mac_algorithm = sha256_mac;
  432. specs->kea = ecdhe_psk_kea;
  433. specs->sig_algo = anonymous_sa_algo;
  434. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  435. specs->pad_size = PAD_SHA;
  436. specs->static_ecdh = 0;
  437. specs->key_size = 0;
  438. specs->block_size = 0;
  439. specs->iv_size = 0;
  440. if (opts != NULL)
  441. opts->usingPSK_cipher = 1;
  442. break;
  443. #endif
  444. #ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
  445. case TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 :
  446. specs->bulk_cipher_algorithm = wolfssl_aes;
  447. specs->cipher_type = block;
  448. specs->mac_algorithm = sha256_mac;
  449. specs->kea = ecdhe_psk_kea;
  450. specs->sig_algo = anonymous_sa_algo;
  451. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  452. specs->pad_size = PAD_SHA;
  453. specs->static_ecdh = 0;
  454. specs->key_size = AES_128_KEY_SIZE;
  455. specs->block_size = AES_BLOCK_SIZE;
  456. specs->iv_size = AES_IV_SIZE;
  457. if (opts != NULL)
  458. opts->usingPSK_cipher = 1;
  459. if (opts != NULL)
  460. opts->usingPSK_cipher = 1;
  461. break;
  462. #endif
  463. #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
  464. #if defined(HAVE_ECC) || (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) \
  465. || (defined(HAVE_CURVE448) && defined(HAVE_ED448))
  466. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
  467. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
  468. specs->bulk_cipher_algorithm = wolfssl_aes;
  469. specs->cipher_type = block;
  470. specs->mac_algorithm = sha256_mac;
  471. specs->kea = ecc_diffie_hellman_kea;
  472. specs->sig_algo = ecc_dsa_sa_algo;
  473. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  474. specs->pad_size = PAD_SHA;
  475. specs->static_ecdh = 0;
  476. specs->key_size = AES_128_KEY_SIZE;
  477. specs->iv_size = AES_IV_SIZE;
  478. specs->block_size = AES_BLOCK_SIZE;
  479. break;
  480. #endif
  481. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
  482. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
  483. specs->bulk_cipher_algorithm = wolfssl_aes;
  484. specs->cipher_type = block;
  485. specs->mac_algorithm = sha384_mac;
  486. specs->kea = ecc_diffie_hellman_kea;
  487. specs->sig_algo = ecc_dsa_sa_algo;
  488. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  489. specs->pad_size = PAD_SHA;
  490. specs->static_ecdh = 0;
  491. specs->key_size = AES_256_KEY_SIZE;
  492. specs->iv_size = AES_IV_SIZE;
  493. specs->block_size = AES_BLOCK_SIZE;
  494. break;
  495. #endif
  496. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
  497. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
  498. specs->bulk_cipher_algorithm = wolfssl_triple_des;
  499. specs->cipher_type = block;
  500. specs->mac_algorithm = sha_mac;
  501. specs->kea = ecc_diffie_hellman_kea;
  502. specs->sig_algo = ecc_dsa_sa_algo;
  503. specs->hash_size = WC_SHA_DIGEST_SIZE;
  504. specs->pad_size = PAD_SHA;
  505. specs->static_ecdh = 0;
  506. specs->key_size = DES3_KEY_SIZE;
  507. specs->block_size = DES_BLOCK_SIZE;
  508. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  509. specs->iv_size = DES_BLOCK_SIZE;
  510. #else
  511. specs->iv_size = DES_IV_SIZE;
  512. #endif
  513. break;
  514. #endif
  515. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
  516. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
  517. specs->bulk_cipher_algorithm = wolfssl_rc4;
  518. specs->cipher_type = stream;
  519. specs->mac_algorithm = sha_mac;
  520. specs->kea = ecc_diffie_hellman_kea;
  521. specs->sig_algo = ecc_dsa_sa_algo;
  522. specs->hash_size = WC_SHA_DIGEST_SIZE;
  523. specs->pad_size = PAD_SHA;
  524. specs->static_ecdh = 0;
  525. specs->key_size = RC4_KEY_SIZE;
  526. specs->iv_size = 0;
  527. specs->block_size = 0;
  528. break;
  529. #endif
  530. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
  531. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
  532. specs->bulk_cipher_algorithm = wolfssl_aes;
  533. specs->cipher_type = block;
  534. specs->mac_algorithm = sha_mac;
  535. specs->kea = ecc_diffie_hellman_kea;
  536. specs->sig_algo = ecc_dsa_sa_algo;
  537. specs->hash_size = WC_SHA_DIGEST_SIZE;
  538. specs->pad_size = PAD_SHA;
  539. specs->static_ecdh = 0;
  540. specs->key_size = AES_128_KEY_SIZE;
  541. specs->block_size = AES_BLOCK_SIZE;
  542. specs->iv_size = AES_IV_SIZE;
  543. break;
  544. #endif
  545. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
  546. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
  547. specs->bulk_cipher_algorithm = wolfssl_aes;
  548. specs->cipher_type = block;
  549. specs->mac_algorithm = sha_mac;
  550. specs->kea = ecc_diffie_hellman_kea;
  551. specs->sig_algo = ecc_dsa_sa_algo;
  552. specs->hash_size = WC_SHA_DIGEST_SIZE;
  553. specs->pad_size = PAD_SHA;
  554. specs->static_ecdh = 0;
  555. specs->key_size = AES_256_KEY_SIZE;
  556. specs->block_size = AES_BLOCK_SIZE;
  557. specs->iv_size = AES_IV_SIZE;
  558. break;
  559. #endif
  560. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  561. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
  562. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  563. specs->cipher_type = aead;
  564. specs->mac_algorithm = sha256_mac;
  565. specs->kea = ecc_diffie_hellman_kea;
  566. specs->sig_algo = ecc_dsa_sa_algo;
  567. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  568. specs->pad_size = PAD_SHA;
  569. specs->static_ecdh = 0;
  570. specs->key_size = AES_128_KEY_SIZE;
  571. specs->block_size = AES_BLOCK_SIZE;
  572. specs->iv_size = AESGCM_IMP_IV_SZ;
  573. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  574. break;
  575. #endif
  576. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
  577. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
  578. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  579. specs->cipher_type = aead;
  580. specs->mac_algorithm = sha384_mac;
  581. specs->kea = ecc_diffie_hellman_kea;
  582. specs->sig_algo = ecc_dsa_sa_algo;
  583. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  584. specs->pad_size = PAD_SHA;
  585. specs->static_ecdh = 0;
  586. specs->key_size = AES_256_KEY_SIZE;
  587. specs->block_size = AES_BLOCK_SIZE;
  588. specs->iv_size = AESGCM_IMP_IV_SZ;
  589. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  590. break;
  591. #endif
  592. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
  593. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM :
  594. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  595. specs->cipher_type = aead;
  596. specs->mac_algorithm = sha256_mac;
  597. specs->kea = ecc_diffie_hellman_kea;
  598. specs->sig_algo = ecc_dsa_sa_algo;
  599. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  600. specs->pad_size = PAD_SHA;
  601. specs->static_ecdh = 0;
  602. specs->key_size = AES_128_KEY_SIZE;
  603. specs->block_size = AES_BLOCK_SIZE;
  604. specs->iv_size = AESGCM_IMP_IV_SZ;
  605. specs->aead_mac_size = AES_CCM_16_AUTH_SZ;
  606. break;
  607. #endif
  608. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
  609. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 :
  610. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  611. specs->cipher_type = aead;
  612. specs->mac_algorithm = sha256_mac;
  613. specs->kea = ecc_diffie_hellman_kea;
  614. specs->sig_algo = ecc_dsa_sa_algo;
  615. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  616. specs->pad_size = PAD_SHA;
  617. specs->static_ecdh = 0;
  618. specs->key_size = AES_128_KEY_SIZE;
  619. specs->block_size = AES_BLOCK_SIZE;
  620. specs->iv_size = AESGCM_IMP_IV_SZ;
  621. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  622. break;
  623. #endif
  624. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
  625. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 :
  626. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  627. specs->cipher_type = aead;
  628. specs->mac_algorithm = sha256_mac;
  629. specs->kea = ecc_diffie_hellman_kea;
  630. specs->sig_algo = ecc_dsa_sa_algo;
  631. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  632. specs->pad_size = PAD_SHA;
  633. specs->static_ecdh = 0;
  634. specs->key_size = AES_256_KEY_SIZE;
  635. specs->block_size = AES_BLOCK_SIZE;
  636. specs->iv_size = AESGCM_IMP_IV_SZ;
  637. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  638. break;
  639. #endif
  640. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
  641. case TLS_ECDHE_ECDSA_WITH_NULL_SHA :
  642. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  643. specs->cipher_type = stream;
  644. specs->mac_algorithm = sha_mac;
  645. specs->kea = ecc_diffie_hellman_kea;
  646. specs->sig_algo = ecc_dsa_sa_algo;
  647. specs->hash_size = WC_SHA_DIGEST_SIZE;
  648. specs->pad_size = PAD_SHA;
  649. specs->static_ecdh = 0;
  650. specs->key_size = 0;
  651. specs->block_size = 0;
  652. specs->iv_size = 0;
  653. break;
  654. #endif
  655. #endif /* HAVE_ECC || (CURVE25519 && ED25519) || (CURVE448 && ED448) */
  656. #if defined(HAVE_ECC)
  657. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
  658. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
  659. specs->bulk_cipher_algorithm = wolfssl_aes;
  660. specs->cipher_type = block;
  661. specs->mac_algorithm = sha256_mac;
  662. specs->kea = ecc_diffie_hellman_kea;
  663. specs->sig_algo = rsa_sa_algo;
  664. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  665. specs->pad_size = PAD_SHA;
  666. specs->static_ecdh = 1;
  667. specs->key_size = AES_128_KEY_SIZE;
  668. specs->iv_size = AES_IV_SIZE;
  669. specs->block_size = AES_BLOCK_SIZE;
  670. break;
  671. #endif
  672. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
  673. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
  674. specs->bulk_cipher_algorithm = wolfssl_aes;
  675. specs->cipher_type = block;
  676. specs->mac_algorithm = sha256_mac;
  677. specs->kea = ecc_diffie_hellman_kea;
  678. specs->sig_algo = ecc_dsa_sa_algo;
  679. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  680. specs->pad_size = PAD_SHA;
  681. specs->static_ecdh = 1;
  682. specs->key_size = AES_128_KEY_SIZE;
  683. specs->iv_size = AES_IV_SIZE;
  684. specs->block_size = AES_BLOCK_SIZE;
  685. break;
  686. #endif
  687. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
  688. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
  689. specs->bulk_cipher_algorithm = wolfssl_aes;
  690. specs->cipher_type = block;
  691. specs->mac_algorithm = sha384_mac;
  692. specs->kea = ecc_diffie_hellman_kea;
  693. specs->sig_algo = rsa_sa_algo;
  694. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  695. specs->pad_size = PAD_SHA;
  696. specs->static_ecdh = 1;
  697. specs->key_size = AES_256_KEY_SIZE;
  698. specs->iv_size = AES_IV_SIZE;
  699. specs->block_size = AES_BLOCK_SIZE;
  700. break;
  701. #endif
  702. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
  703. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
  704. specs->bulk_cipher_algorithm = wolfssl_aes;
  705. specs->cipher_type = block;
  706. specs->mac_algorithm = sha384_mac;
  707. specs->kea = ecc_diffie_hellman_kea;
  708. specs->sig_algo = ecc_dsa_sa_algo;
  709. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  710. specs->pad_size = PAD_SHA;
  711. specs->static_ecdh = 1;
  712. specs->key_size = AES_256_KEY_SIZE;
  713. specs->iv_size = AES_IV_SIZE;
  714. specs->block_size = AES_BLOCK_SIZE;
  715. break;
  716. #endif
  717. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
  718. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
  719. specs->bulk_cipher_algorithm = wolfssl_aes;
  720. specs->cipher_type = block;
  721. specs->mac_algorithm = sha_mac;
  722. specs->kea = ecc_diffie_hellman_kea;
  723. specs->sig_algo = rsa_sa_algo;
  724. specs->hash_size = WC_SHA_DIGEST_SIZE;
  725. specs->pad_size = PAD_SHA;
  726. specs->static_ecdh = 1;
  727. specs->key_size = AES_128_KEY_SIZE;
  728. specs->block_size = AES_BLOCK_SIZE;
  729. specs->iv_size = AES_IV_SIZE;
  730. break;
  731. #endif
  732. #ifdef BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
  733. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
  734. specs->bulk_cipher_algorithm = wolfssl_triple_des;
  735. specs->cipher_type = block;
  736. specs->mac_algorithm = sha_mac;
  737. specs->kea = ecc_diffie_hellman_kea;
  738. specs->sig_algo = rsa_sa_algo;
  739. specs->hash_size = WC_SHA_DIGEST_SIZE;
  740. specs->pad_size = PAD_SHA;
  741. specs->static_ecdh = 1;
  742. specs->key_size = DES3_KEY_SIZE;
  743. specs->block_size = DES_BLOCK_SIZE;
  744. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  745. specs->iv_size = DES_BLOCK_SIZE;
  746. #else
  747. specs->iv_size = DES_IV_SIZE;
  748. #endif
  749. break;
  750. #endif
  751. #ifdef BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
  752. case TLS_ECDH_RSA_WITH_RC4_128_SHA :
  753. specs->bulk_cipher_algorithm = wolfssl_rc4;
  754. specs->cipher_type = stream;
  755. specs->mac_algorithm = sha_mac;
  756. specs->kea = ecc_diffie_hellman_kea;
  757. specs->sig_algo = rsa_sa_algo;
  758. specs->hash_size = WC_SHA_DIGEST_SIZE;
  759. specs->pad_size = PAD_SHA;
  760. specs->static_ecdh = 1;
  761. specs->key_size = RC4_KEY_SIZE;
  762. specs->iv_size = 0;
  763. specs->block_size = 0;
  764. break;
  765. #endif
  766. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
  767. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
  768. specs->bulk_cipher_algorithm = wolfssl_triple_des;
  769. specs->cipher_type = block;
  770. specs->mac_algorithm = sha_mac;
  771. specs->kea = ecc_diffie_hellman_kea;
  772. specs->sig_algo = ecc_dsa_sa_algo;
  773. specs->hash_size = WC_SHA_DIGEST_SIZE;
  774. specs->pad_size = PAD_SHA;
  775. specs->static_ecdh = 1;
  776. specs->key_size = DES3_KEY_SIZE;
  777. specs->block_size = DES_BLOCK_SIZE;
  778. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  779. specs->iv_size = DES_BLOCK_SIZE;
  780. #else
  781. specs->iv_size = DES_IV_SIZE;
  782. #endif
  783. break;
  784. #endif
  785. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
  786. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
  787. specs->bulk_cipher_algorithm = wolfssl_rc4;
  788. specs->cipher_type = stream;
  789. specs->mac_algorithm = sha_mac;
  790. specs->kea = ecc_diffie_hellman_kea;
  791. specs->sig_algo = ecc_dsa_sa_algo;
  792. specs->hash_size = WC_SHA_DIGEST_SIZE;
  793. specs->pad_size = PAD_SHA;
  794. specs->static_ecdh = 1;
  795. specs->key_size = RC4_KEY_SIZE;
  796. specs->iv_size = 0;
  797. specs->block_size = 0;
  798. break;
  799. #endif
  800. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
  801. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
  802. specs->bulk_cipher_algorithm = wolfssl_aes;
  803. specs->cipher_type = block;
  804. specs->mac_algorithm = sha_mac;
  805. specs->kea = ecc_diffie_hellman_kea;
  806. specs->sig_algo = rsa_sa_algo;
  807. specs->hash_size = WC_SHA_DIGEST_SIZE;
  808. specs->pad_size = PAD_SHA;
  809. specs->static_ecdh = 1;
  810. specs->key_size = AES_256_KEY_SIZE;
  811. specs->block_size = AES_BLOCK_SIZE;
  812. specs->iv_size = AES_IV_SIZE;
  813. break;
  814. #endif
  815. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
  816. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
  817. specs->bulk_cipher_algorithm = wolfssl_aes;
  818. specs->cipher_type = block;
  819. specs->mac_algorithm = sha_mac;
  820. specs->kea = ecc_diffie_hellman_kea;
  821. specs->sig_algo = ecc_dsa_sa_algo;
  822. specs->hash_size = WC_SHA_DIGEST_SIZE;
  823. specs->pad_size = PAD_SHA;
  824. specs->static_ecdh = 1;
  825. specs->key_size = AES_128_KEY_SIZE;
  826. specs->block_size = AES_BLOCK_SIZE;
  827. specs->iv_size = AES_IV_SIZE;
  828. break;
  829. #endif
  830. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
  831. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
  832. specs->bulk_cipher_algorithm = wolfssl_aes;
  833. specs->cipher_type = block;
  834. specs->mac_algorithm = sha_mac;
  835. specs->kea = ecc_diffie_hellman_kea;
  836. specs->sig_algo = ecc_dsa_sa_algo;
  837. specs->hash_size = WC_SHA_DIGEST_SIZE;
  838. specs->pad_size = PAD_SHA;
  839. specs->static_ecdh = 1;
  840. specs->key_size = AES_256_KEY_SIZE;
  841. specs->block_size = AES_BLOCK_SIZE;
  842. specs->iv_size = AES_IV_SIZE;
  843. break;
  844. #endif
  845. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
  846. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
  847. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  848. specs->cipher_type = aead;
  849. specs->mac_algorithm = sha256_mac;
  850. specs->kea = ecc_diffie_hellman_kea;
  851. specs->sig_algo = rsa_sa_algo;
  852. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  853. specs->pad_size = PAD_SHA;
  854. specs->static_ecdh = 1;
  855. specs->key_size = AES_128_KEY_SIZE;
  856. specs->block_size = AES_BLOCK_SIZE;
  857. specs->iv_size = AESGCM_IMP_IV_SZ;
  858. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  859. break;
  860. #endif
  861. #ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
  862. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
  863. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  864. specs->cipher_type = aead;
  865. specs->mac_algorithm = sha384_mac;
  866. specs->kea = ecc_diffie_hellman_kea;
  867. specs->sig_algo = rsa_sa_algo;
  868. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  869. specs->pad_size = PAD_SHA;
  870. specs->static_ecdh = 1;
  871. specs->key_size = AES_256_KEY_SIZE;
  872. specs->block_size = AES_BLOCK_SIZE;
  873. specs->iv_size = AESGCM_IMP_IV_SZ;
  874. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  875. break;
  876. #endif
  877. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
  878. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
  879. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  880. specs->cipher_type = aead;
  881. specs->mac_algorithm = sha256_mac;
  882. specs->kea = ecc_diffie_hellman_kea;
  883. specs->sig_algo = ecc_dsa_sa_algo;
  884. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  885. specs->pad_size = PAD_SHA;
  886. specs->static_ecdh = 1;
  887. specs->key_size = AES_128_KEY_SIZE;
  888. specs->block_size = AES_BLOCK_SIZE;
  889. specs->iv_size = AESGCM_IMP_IV_SZ;
  890. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  891. break;
  892. #endif
  893. #ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
  894. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
  895. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  896. specs->cipher_type = aead;
  897. specs->mac_algorithm = sha384_mac;
  898. specs->kea = ecc_diffie_hellman_kea;
  899. specs->sig_algo = ecc_dsa_sa_algo;
  900. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  901. specs->pad_size = PAD_SHA;
  902. specs->static_ecdh = 1;
  903. specs->key_size = AES_256_KEY_SIZE;
  904. specs->block_size = AES_BLOCK_SIZE;
  905. specs->iv_size = AESGCM_IMP_IV_SZ;
  906. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  907. break;
  908. #endif
  909. #endif /* HAVE_ECC */
  910. #ifdef BUILD_TLS_RSA_WITH_AES_128_CCM_8
  911. case TLS_RSA_WITH_AES_128_CCM_8 :
  912. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  913. specs->cipher_type = aead;
  914. specs->mac_algorithm = sha256_mac;
  915. specs->kea = rsa_kea;
  916. specs->sig_algo = rsa_sa_algo;
  917. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  918. specs->pad_size = PAD_SHA;
  919. specs->static_ecdh = 0;
  920. specs->key_size = AES_128_KEY_SIZE;
  921. specs->block_size = AES_BLOCK_SIZE;
  922. specs->iv_size = AESGCM_IMP_IV_SZ;
  923. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  924. break;
  925. #endif
  926. #ifdef BUILD_TLS_RSA_WITH_AES_256_CCM_8
  927. case TLS_RSA_WITH_AES_256_CCM_8 :
  928. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  929. specs->cipher_type = aead;
  930. specs->mac_algorithm = sha256_mac;
  931. specs->kea = rsa_kea;
  932. specs->sig_algo = rsa_sa_algo;
  933. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  934. specs->pad_size = PAD_SHA;
  935. specs->static_ecdh = 0;
  936. specs->key_size = AES_256_KEY_SIZE;
  937. specs->block_size = AES_BLOCK_SIZE;
  938. specs->iv_size = AESGCM_IMP_IV_SZ;
  939. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  940. break;
  941. #endif
  942. #ifdef BUILD_TLS_PSK_WITH_AES_128_CCM_8
  943. case TLS_PSK_WITH_AES_128_CCM_8 :
  944. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  945. specs->cipher_type = aead;
  946. specs->mac_algorithm = sha256_mac;
  947. specs->kea = psk_kea;
  948. specs->sig_algo = anonymous_sa_algo;
  949. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  950. specs->pad_size = PAD_SHA;
  951. specs->static_ecdh = 0;
  952. specs->key_size = AES_128_KEY_SIZE;
  953. specs->block_size = AES_BLOCK_SIZE;
  954. specs->iv_size = AESGCM_IMP_IV_SZ;
  955. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  956. if (opts != NULL)
  957. opts->usingPSK_cipher = 1;
  958. break;
  959. #endif
  960. #ifdef BUILD_TLS_PSK_WITH_AES_256_CCM_8
  961. case TLS_PSK_WITH_AES_256_CCM_8 :
  962. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  963. specs->cipher_type = aead;
  964. specs->mac_algorithm = sha256_mac;
  965. specs->kea = psk_kea;
  966. specs->sig_algo = anonymous_sa_algo;
  967. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  968. specs->pad_size = PAD_SHA;
  969. specs->static_ecdh = 0;
  970. specs->key_size = AES_256_KEY_SIZE;
  971. specs->block_size = AES_BLOCK_SIZE;
  972. specs->iv_size = AESGCM_IMP_IV_SZ;
  973. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  974. if (opts != NULL)
  975. opts->usingPSK_cipher = 1;
  976. break;
  977. #endif
  978. #ifdef BUILD_TLS_PSK_WITH_AES_128_CCM
  979. case TLS_PSK_WITH_AES_128_CCM :
  980. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  981. specs->cipher_type = aead;
  982. specs->mac_algorithm = sha256_mac;
  983. specs->kea = psk_kea;
  984. specs->sig_algo = anonymous_sa_algo;
  985. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  986. specs->pad_size = PAD_SHA;
  987. specs->static_ecdh = 0;
  988. specs->key_size = AES_128_KEY_SIZE;
  989. specs->block_size = AES_BLOCK_SIZE;
  990. specs->iv_size = AESGCM_IMP_IV_SZ;
  991. specs->aead_mac_size = AES_CCM_16_AUTH_SZ;
  992. if (opts != NULL)
  993. opts->usingPSK_cipher = 1;
  994. break;
  995. #endif
  996. #ifdef BUILD_TLS_PSK_WITH_AES_256_CCM
  997. case TLS_PSK_WITH_AES_256_CCM :
  998. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  999. specs->cipher_type = aead;
  1000. specs->mac_algorithm = sha256_mac;
  1001. specs->kea = psk_kea;
  1002. specs->sig_algo = anonymous_sa_algo;
  1003. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1004. specs->pad_size = PAD_SHA;
  1005. specs->static_ecdh = 0;
  1006. specs->key_size = AES_256_KEY_SIZE;
  1007. specs->block_size = AES_BLOCK_SIZE;
  1008. specs->iv_size = AESGCM_IMP_IV_SZ;
  1009. specs->aead_mac_size = AES_CCM_16_AUTH_SZ;
  1010. if (opts != NULL)
  1011. opts->usingPSK_cipher = 1;
  1012. break;
  1013. #endif
  1014. #ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
  1015. case TLS_DHE_PSK_WITH_AES_128_CCM :
  1016. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  1017. specs->cipher_type = aead;
  1018. specs->mac_algorithm = sha256_mac;
  1019. specs->kea = dhe_psk_kea;
  1020. specs->sig_algo = anonymous_sa_algo;
  1021. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1022. specs->pad_size = PAD_SHA;
  1023. specs->static_ecdh = 0;
  1024. specs->key_size = AES_128_KEY_SIZE;
  1025. specs->block_size = AES_BLOCK_SIZE;
  1026. specs->iv_size = AESGCM_IMP_IV_SZ;
  1027. specs->aead_mac_size = AES_CCM_16_AUTH_SZ;
  1028. if (opts != NULL)
  1029. opts->usingPSK_cipher = 1;
  1030. break;
  1031. #endif
  1032. #ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
  1033. case TLS_DHE_PSK_WITH_AES_256_CCM :
  1034. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  1035. specs->cipher_type = aead;
  1036. specs->mac_algorithm = sha256_mac;
  1037. specs->kea = dhe_psk_kea;
  1038. specs->sig_algo = anonymous_sa_algo;
  1039. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1040. specs->pad_size = PAD_SHA;
  1041. specs->static_ecdh = 0;
  1042. specs->key_size = AES_256_KEY_SIZE;
  1043. specs->block_size = AES_BLOCK_SIZE;
  1044. specs->iv_size = AESGCM_IMP_IV_SZ;
  1045. specs->aead_mac_size = AES_CCM_16_AUTH_SZ;
  1046. if (opts != NULL)
  1047. opts->usingPSK_cipher = 1;
  1048. break;
  1049. #endif
  1050. #if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER)
  1051. #ifdef BUILD_TLS_SHA256_SHA256
  1052. case TLS_SHA256_SHA256 :
  1053. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1054. specs->cipher_type = aead;
  1055. specs->mac_algorithm = sha256_mac;
  1056. specs->kea = 0;
  1057. specs->sig_algo = 0;
  1058. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1059. specs->pad_size = PAD_SHA;
  1060. specs->static_ecdh = 0;
  1061. specs->key_size = WC_SHA256_DIGEST_SIZE;
  1062. specs->block_size = 0;
  1063. specs->iv_size = HMAC_NONCE_SZ;
  1064. specs->aead_mac_size = WC_SHA256_DIGEST_SIZE;
  1065. break;
  1066. #endif
  1067. #ifdef BUILD_TLS_SHA384_SHA384
  1068. case TLS_SHA384_SHA384 :
  1069. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1070. specs->cipher_type = aead;
  1071. specs->mac_algorithm = sha384_mac;
  1072. specs->kea = 0;
  1073. specs->sig_algo = 0;
  1074. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1075. specs->pad_size = PAD_SHA;
  1076. specs->static_ecdh = 0;
  1077. specs->key_size = WC_SHA384_DIGEST_SIZE;
  1078. specs->block_size = 0;
  1079. specs->iv_size = HMAC_NONCE_SZ;
  1080. specs->aead_mac_size = WC_SHA384_DIGEST_SIZE;
  1081. break;
  1082. #endif
  1083. #endif
  1084. default:
  1085. WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs ECC");
  1086. return UNSUPPORTED_SUITE;
  1087. } /* switch */
  1088. } /* if */
  1089. /* TLSi v1.3 cipher suites, 0x13 */
  1090. if (cipherSuite0 == TLS13_BYTE) {
  1091. switch (cipherSuite) {
  1092. #ifdef WOLFSSL_TLS13
  1093. #ifdef BUILD_TLS_AES_128_GCM_SHA256
  1094. case TLS_AES_128_GCM_SHA256 :
  1095. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1096. specs->cipher_type = aead;
  1097. specs->mac_algorithm = sha256_mac;
  1098. specs->kea = 0;
  1099. specs->sig_algo = 0;
  1100. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1101. specs->pad_size = PAD_SHA;
  1102. specs->static_ecdh = 0;
  1103. specs->key_size = AES_128_KEY_SIZE;
  1104. specs->block_size = AES_BLOCK_SIZE;
  1105. specs->iv_size = AESGCM_NONCE_SZ;
  1106. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1107. break;
  1108. #endif
  1109. #ifdef BUILD_TLS_AES_256_GCM_SHA384
  1110. case TLS_AES_256_GCM_SHA384 :
  1111. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1112. specs->cipher_type = aead;
  1113. specs->mac_algorithm = sha384_mac;
  1114. specs->kea = 0;
  1115. specs->sig_algo = 0;
  1116. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1117. specs->pad_size = PAD_SHA;
  1118. specs->static_ecdh = 0;
  1119. specs->key_size = AES_256_KEY_SIZE;
  1120. specs->block_size = AES_BLOCK_SIZE;
  1121. specs->iv_size = AESGCM_NONCE_SZ;
  1122. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1123. break;
  1124. #endif
  1125. #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256
  1126. case TLS_CHACHA20_POLY1305_SHA256 :
  1127. specs->bulk_cipher_algorithm = wolfssl_chacha;
  1128. specs->cipher_type = aead;
  1129. specs->mac_algorithm = sha256_mac;
  1130. specs->kea = 0;
  1131. specs->sig_algo = 0;
  1132. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1133. specs->pad_size = PAD_SHA;
  1134. specs->static_ecdh = 0;
  1135. specs->key_size = CHACHA20_256_KEY_SIZE;
  1136. specs->block_size = CHACHA20_BLOCK_SIZE;
  1137. specs->iv_size = CHACHA20_IV_SIZE;
  1138. specs->aead_mac_size = POLY1305_AUTH_SZ;
  1139. if (opts != NULL)
  1140. opts->oldPoly = 0; /* use recent padding RFC */
  1141. break;
  1142. #endif
  1143. #ifdef BUILD_TLS_AES_128_CCM_SHA256
  1144. case TLS_AES_128_CCM_SHA256 :
  1145. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  1146. specs->cipher_type = aead;
  1147. specs->mac_algorithm = sha256_mac;
  1148. specs->kea = 0;
  1149. specs->sig_algo = 0;
  1150. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1151. specs->pad_size = PAD_SHA;
  1152. specs->static_ecdh = 0;
  1153. specs->key_size = AES_128_KEY_SIZE;
  1154. specs->block_size = AES_BLOCK_SIZE;
  1155. specs->iv_size = AESGCM_NONCE_SZ;
  1156. specs->aead_mac_size = AES_CCM_16_AUTH_SZ;
  1157. break;
  1158. #endif
  1159. #ifdef BUILD_TLS_AES_128_CCM_8_SHA256
  1160. case TLS_AES_128_CCM_8_SHA256 :
  1161. specs->bulk_cipher_algorithm = wolfssl_aes_ccm;
  1162. specs->cipher_type = aead;
  1163. specs->mac_algorithm = sha256_mac;
  1164. specs->kea = 0;
  1165. specs->sig_algo = 0;
  1166. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1167. specs->pad_size = PAD_SHA;
  1168. specs->static_ecdh = 0;
  1169. specs->key_size = AES_128_KEY_SIZE;
  1170. specs->block_size = AES_BLOCK_SIZE;
  1171. specs->iv_size = AESGCM_NONCE_SZ;
  1172. specs->aead_mac_size = AES_CCM_8_AUTH_SZ;
  1173. break;
  1174. #endif
  1175. #endif /* WOLFSSL_TLS13 */
  1176. default:
  1177. break;
  1178. }
  1179. }
  1180. if (cipherSuite0 == ECDHE_PSK_BYTE) {
  1181. switch (cipherSuite) {
  1182. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  1183. #ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256
  1184. case TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 :
  1185. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1186. specs->cipher_type = aead;
  1187. specs->mac_algorithm = sha256_mac;
  1188. specs->kea = ecdhe_psk_kea;
  1189. specs->sig_algo = anonymous_sa_algo;
  1190. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1191. specs->pad_size = PAD_SHA;
  1192. specs->static_ecdh = 0;
  1193. specs->key_size = AES_128_KEY_SIZE;
  1194. specs->block_size = AES_BLOCK_SIZE;
  1195. specs->iv_size = AESGCM_IMP_IV_SZ;
  1196. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1197. if (opts != NULL)
  1198. opts->usingPSK_cipher = 1;
  1199. break;
  1200. #endif
  1201. #endif
  1202. default:
  1203. break;
  1204. }
  1205. }
  1206. if (cipherSuite0 == SM_BYTE) {
  1207. switch (cipherSuite) {
  1208. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3
  1209. case TLS_ECDHE_ECDSA_WITH_SM4_CBC_SM3 :
  1210. specs->bulk_cipher_algorithm = wolfssl_sm4_cbc;
  1211. specs->cipher_type = block;
  1212. specs->mac_algorithm = sm3_mac;
  1213. specs->kea = ecc_diffie_hellman_kea;
  1214. specs->sig_algo = sm2_sa_algo;
  1215. specs->hash_size = WC_SM3_DIGEST_SIZE;
  1216. specs->pad_size = PAD_SHA;
  1217. specs->static_ecdh = 0;
  1218. specs->key_size = SM4_KEY_SIZE;
  1219. specs->iv_size = SM4_IV_SIZE;
  1220. specs->block_size = SM4_BLOCK_SIZE;
  1221. break;
  1222. #endif
  1223. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3
  1224. case TLS_ECDHE_ECDSA_WITH_SM4_GCM_SM3 :
  1225. specs->bulk_cipher_algorithm = wolfssl_sm4_gcm;
  1226. specs->cipher_type = aead;
  1227. specs->mac_algorithm = sm3_mac;
  1228. specs->kea = ecc_diffie_hellman_kea;
  1229. specs->sig_algo = sm2_sa_algo;
  1230. specs->hash_size = WC_SM3_DIGEST_SIZE;
  1231. specs->pad_size = PAD_SHA;
  1232. specs->static_ecdh = 0;
  1233. specs->key_size = SM4_KEY_SIZE;
  1234. specs->block_size = SM4_BLOCK_SIZE;
  1235. specs->iv_size = GCM_IMP_IV_SZ;
  1236. specs->aead_mac_size = SM4_GCM_AUTH_SZ;
  1237. break;
  1238. #endif
  1239. #ifdef BUILD_TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3
  1240. case TLS_ECDHE_ECDSA_WITH_SM4_CCM_SM3 :
  1241. specs->bulk_cipher_algorithm = wolfssl_sm4_ccm;
  1242. specs->cipher_type = aead;
  1243. specs->mac_algorithm = sm3_mac;
  1244. specs->kea = ecc_diffie_hellman_kea;
  1245. specs->sig_algo = sm2_sa_algo;
  1246. specs->hash_size = WC_SM3_DIGEST_SIZE;
  1247. specs->pad_size = PAD_SHA;
  1248. specs->static_ecdh = 0;
  1249. specs->key_size = SM4_KEY_SIZE;
  1250. specs->block_size = SM4_BLOCK_SIZE;
  1251. specs->iv_size = GCM_IMP_IV_SZ;
  1252. specs->aead_mac_size = SM4_CCM_AUTH_SZ;
  1253. break;
  1254. #endif
  1255. default:
  1256. break;
  1257. }
  1258. }
  1259. if (cipherSuite0 != ECC_BYTE &&
  1260. cipherSuite0 != ECDHE_PSK_BYTE &&
  1261. cipherSuite0 != CHACHA_BYTE &&
  1262. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3) && \
  1263. (defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_GCM) || \
  1264. defined(WOLFSSL_SM4_CCM))
  1265. cipherSuite0 != SM_BYTE &&
  1266. #endif
  1267. cipherSuite0 != TLS13_BYTE) { /* normal suites */
  1268. switch (cipherSuite) {
  1269. #ifdef BUILD_TLS_SM4_GCM_SM3
  1270. case TLS_SM4_GCM_SM3 :
  1271. specs->bulk_cipher_algorithm = wolfssl_sm4_gcm;
  1272. specs->cipher_type = aead;
  1273. specs->mac_algorithm = sm3_mac;
  1274. specs->kea = 0;
  1275. specs->sig_algo = 0;
  1276. specs->hash_size = WC_SM3_DIGEST_SIZE;
  1277. specs->pad_size = PAD_SHA;
  1278. specs->static_ecdh = 0;
  1279. specs->key_size = SM4_KEY_SIZE;
  1280. specs->block_size = SM4_BLOCK_SIZE;
  1281. specs->iv_size = SM4_GCM_NONCE_SZ;
  1282. specs->aead_mac_size = SM4_GCM_AUTH_SZ;
  1283. break;
  1284. #endif
  1285. #ifdef BUILD_TLS_SM4_CCM_SM3
  1286. case TLS_SM4_CCM_SM3 :
  1287. specs->bulk_cipher_algorithm = wolfssl_sm4_ccm;
  1288. specs->cipher_type = aead;
  1289. specs->mac_algorithm = sm3_mac;
  1290. specs->kea = 0;
  1291. specs->sig_algo = 0;
  1292. specs->hash_size = WC_SM3_DIGEST_SIZE;
  1293. specs->pad_size = PAD_SHA;
  1294. specs->static_ecdh = 0;
  1295. specs->key_size = SM4_KEY_SIZE;
  1296. specs->block_size = SM4_BLOCK_SIZE;
  1297. specs->iv_size = SM4_CCM_NONCE_SZ;
  1298. specs->aead_mac_size = SM4_CCM_AUTH_SZ;
  1299. break;
  1300. #endif
  1301. #ifdef BUILD_SSL_RSA_WITH_RC4_128_SHA
  1302. case SSL_RSA_WITH_RC4_128_SHA :
  1303. specs->bulk_cipher_algorithm = wolfssl_rc4;
  1304. specs->cipher_type = stream;
  1305. specs->mac_algorithm = sha_mac;
  1306. specs->kea = rsa_kea;
  1307. specs->sig_algo = rsa_sa_algo;
  1308. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1309. specs->pad_size = PAD_SHA;
  1310. specs->static_ecdh = 0;
  1311. specs->key_size = RC4_KEY_SIZE;
  1312. specs->iv_size = 0;
  1313. specs->block_size = 0;
  1314. break;
  1315. #endif
  1316. #ifdef BUILD_SSL_RSA_WITH_RC4_128_MD5
  1317. case SSL_RSA_WITH_RC4_128_MD5 :
  1318. specs->bulk_cipher_algorithm = wolfssl_rc4;
  1319. specs->cipher_type = stream;
  1320. specs->mac_algorithm = md5_mac;
  1321. specs->kea = rsa_kea;
  1322. specs->sig_algo = rsa_sa_algo;
  1323. specs->hash_size = WC_MD5_DIGEST_SIZE;
  1324. specs->pad_size = PAD_MD5;
  1325. specs->static_ecdh = 0;
  1326. specs->key_size = RC4_KEY_SIZE;
  1327. specs->iv_size = 0;
  1328. specs->block_size = 0;
  1329. break;
  1330. #endif
  1331. #ifdef BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
  1332. case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
  1333. specs->bulk_cipher_algorithm = wolfssl_triple_des;
  1334. specs->cipher_type = block;
  1335. specs->mac_algorithm = sha_mac;
  1336. specs->kea = rsa_kea;
  1337. specs->sig_algo = rsa_sa_algo;
  1338. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1339. specs->pad_size = PAD_SHA;
  1340. specs->static_ecdh = 0;
  1341. specs->key_size = DES3_KEY_SIZE;
  1342. specs->block_size = DES_BLOCK_SIZE;
  1343. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2)
  1344. specs->iv_size = DES_BLOCK_SIZE;
  1345. #else
  1346. specs->iv_size = DES_IV_SIZE;
  1347. #endif
  1348. break;
  1349. #endif
  1350. #ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
  1351. case TLS_RSA_WITH_AES_128_CBC_SHA :
  1352. specs->bulk_cipher_algorithm = wolfssl_aes;
  1353. specs->cipher_type = block;
  1354. specs->mac_algorithm = sha_mac;
  1355. specs->kea = rsa_kea;
  1356. specs->sig_algo = rsa_sa_algo;
  1357. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1358. specs->pad_size = PAD_SHA;
  1359. specs->static_ecdh = 0;
  1360. specs->key_size = AES_128_KEY_SIZE;
  1361. specs->block_size = AES_BLOCK_SIZE;
  1362. specs->iv_size = AES_IV_SIZE;
  1363. break;
  1364. #endif
  1365. #ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
  1366. case TLS_RSA_WITH_AES_128_CBC_SHA256 :
  1367. specs->bulk_cipher_algorithm = wolfssl_aes;
  1368. specs->cipher_type = block;
  1369. specs->mac_algorithm = sha256_mac;
  1370. specs->kea = rsa_kea;
  1371. specs->sig_algo = rsa_sa_algo;
  1372. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1373. specs->pad_size = PAD_SHA;
  1374. specs->static_ecdh = 0;
  1375. specs->key_size = AES_128_KEY_SIZE;
  1376. specs->block_size = AES_BLOCK_SIZE;
  1377. specs->iv_size = AES_IV_SIZE;
  1378. break;
  1379. #endif
  1380. #ifdef BUILD_TLS_RSA_WITH_NULL_MD5
  1381. case TLS_RSA_WITH_NULL_MD5 :
  1382. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1383. specs->cipher_type = stream;
  1384. specs->mac_algorithm = md5_mac;
  1385. specs->kea = rsa_kea;
  1386. specs->sig_algo = rsa_sa_algo;
  1387. specs->hash_size = WC_MD5_DIGEST_SIZE;
  1388. specs->pad_size = PAD_MD5;
  1389. specs->static_ecdh = 0;
  1390. specs->key_size = 0;
  1391. specs->block_size = 0;
  1392. specs->iv_size = 0;
  1393. break;
  1394. #endif
  1395. #ifdef BUILD_TLS_RSA_WITH_NULL_SHA
  1396. case TLS_RSA_WITH_NULL_SHA :
  1397. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1398. specs->cipher_type = stream;
  1399. specs->mac_algorithm = sha_mac;
  1400. specs->kea = rsa_kea;
  1401. specs->sig_algo = rsa_sa_algo;
  1402. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1403. specs->pad_size = PAD_SHA;
  1404. specs->static_ecdh = 0;
  1405. specs->key_size = 0;
  1406. specs->block_size = 0;
  1407. specs->iv_size = 0;
  1408. break;
  1409. #endif
  1410. #ifdef BUILD_TLS_RSA_WITH_NULL_SHA256
  1411. case TLS_RSA_WITH_NULL_SHA256 :
  1412. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1413. specs->cipher_type = stream;
  1414. specs->mac_algorithm = sha256_mac;
  1415. specs->kea = rsa_kea;
  1416. specs->sig_algo = rsa_sa_algo;
  1417. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1418. specs->pad_size = PAD_SHA;
  1419. specs->static_ecdh = 0;
  1420. specs->key_size = 0;
  1421. specs->block_size = 0;
  1422. specs->iv_size = 0;
  1423. break;
  1424. #endif
  1425. #ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
  1426. case TLS_RSA_WITH_AES_256_CBC_SHA :
  1427. specs->bulk_cipher_algorithm = wolfssl_aes;
  1428. specs->cipher_type = block;
  1429. specs->mac_algorithm = sha_mac;
  1430. specs->kea = rsa_kea;
  1431. specs->sig_algo = rsa_sa_algo;
  1432. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1433. specs->pad_size = PAD_SHA;
  1434. specs->static_ecdh = 0;
  1435. specs->key_size = AES_256_KEY_SIZE;
  1436. specs->block_size = AES_BLOCK_SIZE;
  1437. specs->iv_size = AES_IV_SIZE;
  1438. break;
  1439. #endif
  1440. #ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
  1441. case TLS_RSA_WITH_AES_256_CBC_SHA256 :
  1442. specs->bulk_cipher_algorithm = wolfssl_aes;
  1443. specs->cipher_type = block;
  1444. specs->mac_algorithm = sha256_mac;
  1445. specs->kea = rsa_kea;
  1446. specs->sig_algo = rsa_sa_algo;
  1447. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1448. specs->pad_size = PAD_SHA;
  1449. specs->static_ecdh = 0;
  1450. specs->key_size = AES_256_KEY_SIZE;
  1451. specs->block_size = AES_BLOCK_SIZE;
  1452. specs->iv_size = AES_IV_SIZE;
  1453. break;
  1454. #endif
  1455. #ifdef BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
  1456. case TLS_PSK_WITH_AES_128_GCM_SHA256 :
  1457. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1458. specs->cipher_type = aead;
  1459. specs->mac_algorithm = sha256_mac;
  1460. specs->kea = psk_kea;
  1461. specs->sig_algo = anonymous_sa_algo;
  1462. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1463. specs->pad_size = PAD_SHA;
  1464. specs->static_ecdh = 0;
  1465. specs->key_size = AES_128_KEY_SIZE;
  1466. specs->block_size = AES_BLOCK_SIZE;
  1467. specs->iv_size = AESGCM_IMP_IV_SZ;
  1468. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1469. if (opts != NULL)
  1470. opts->usingPSK_cipher = 1;
  1471. break;
  1472. #endif
  1473. #ifdef BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
  1474. case TLS_PSK_WITH_AES_256_GCM_SHA384 :
  1475. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1476. specs->cipher_type = aead;
  1477. specs->mac_algorithm = sha384_mac;
  1478. specs->kea = psk_kea;
  1479. specs->sig_algo = anonymous_sa_algo;
  1480. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1481. specs->pad_size = PAD_SHA;
  1482. specs->static_ecdh = 0;
  1483. specs->key_size = AES_256_KEY_SIZE;
  1484. specs->block_size = AES_BLOCK_SIZE;
  1485. specs->iv_size = AESGCM_IMP_IV_SZ;
  1486. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1487. if (opts != NULL)
  1488. opts->usingPSK_cipher = 1;
  1489. break;
  1490. #endif
  1491. #ifdef BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
  1492. case TLS_DH_anon_WITH_AES_256_GCM_SHA384:
  1493. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1494. specs->cipher_type = aead;
  1495. specs->mac_algorithm = sha384_mac;
  1496. specs->kea = diffie_hellman_kea;
  1497. specs->sig_algo = anonymous_sa_algo;
  1498. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1499. specs->pad_size = PAD_SHA;
  1500. specs->static_ecdh = 0;
  1501. specs->key_size = AES_256_KEY_SIZE;
  1502. specs->block_size = AES_BLOCK_SIZE;
  1503. specs->iv_size = AESGCM_IMP_IV_SZ;
  1504. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1505. if (opts != NULL)
  1506. opts->usingAnon_cipher = 1;
  1507. break;
  1508. #endif
  1509. #ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
  1510. case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
  1511. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1512. specs->cipher_type = aead;
  1513. specs->mac_algorithm = sha256_mac;
  1514. specs->kea = dhe_psk_kea;
  1515. specs->sig_algo = anonymous_sa_algo;
  1516. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1517. specs->pad_size = PAD_SHA;
  1518. specs->static_ecdh = 0;
  1519. specs->key_size = AES_128_KEY_SIZE;
  1520. specs->block_size = AES_BLOCK_SIZE;
  1521. specs->iv_size = AESGCM_IMP_IV_SZ;
  1522. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1523. if (opts != NULL)
  1524. opts->usingPSK_cipher = 1;
  1525. break;
  1526. #endif
  1527. #ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
  1528. case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
  1529. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1530. specs->cipher_type = aead;
  1531. specs->mac_algorithm = sha384_mac;
  1532. specs->kea = dhe_psk_kea;
  1533. specs->sig_algo = anonymous_sa_algo;
  1534. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1535. specs->pad_size = PAD_SHA;
  1536. specs->static_ecdh = 0;
  1537. specs->key_size = AES_256_KEY_SIZE;
  1538. specs->block_size = AES_BLOCK_SIZE;
  1539. specs->iv_size = AESGCM_IMP_IV_SZ;
  1540. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1541. if (opts != NULL)
  1542. opts->usingPSK_cipher = 1;
  1543. break;
  1544. #endif
  1545. #ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
  1546. case TLS_PSK_WITH_AES_128_CBC_SHA256 :
  1547. specs->bulk_cipher_algorithm = wolfssl_aes;
  1548. specs->cipher_type = block;
  1549. specs->mac_algorithm = sha256_mac;
  1550. specs->kea = psk_kea;
  1551. specs->sig_algo = anonymous_sa_algo;
  1552. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1553. specs->pad_size = PAD_SHA;
  1554. specs->static_ecdh = 0;
  1555. specs->key_size = AES_128_KEY_SIZE;
  1556. specs->block_size = AES_BLOCK_SIZE;
  1557. specs->iv_size = AES_IV_SIZE;
  1558. if (opts != NULL)
  1559. opts->usingPSK_cipher = 1;
  1560. break;
  1561. #endif
  1562. #ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
  1563. case TLS_PSK_WITH_AES_256_CBC_SHA384 :
  1564. specs->bulk_cipher_algorithm = wolfssl_aes;
  1565. specs->cipher_type = block;
  1566. specs->mac_algorithm = sha384_mac;
  1567. specs->kea = psk_kea;
  1568. specs->sig_algo = anonymous_sa_algo;
  1569. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1570. specs->pad_size = PAD_SHA;
  1571. specs->static_ecdh = 0;
  1572. specs->key_size = AES_256_KEY_SIZE;
  1573. specs->block_size = AES_BLOCK_SIZE;
  1574. specs->iv_size = AES_IV_SIZE;
  1575. if (opts != NULL)
  1576. opts->usingPSK_cipher = 1;
  1577. break;
  1578. #endif
  1579. #ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
  1580. case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
  1581. specs->bulk_cipher_algorithm = wolfssl_aes;
  1582. specs->cipher_type = block;
  1583. specs->mac_algorithm = sha256_mac;
  1584. specs->kea = dhe_psk_kea;
  1585. specs->sig_algo = anonymous_sa_algo;
  1586. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1587. specs->pad_size = PAD_SHA;
  1588. specs->static_ecdh = 0;
  1589. specs->key_size = AES_128_KEY_SIZE;
  1590. specs->block_size = AES_BLOCK_SIZE;
  1591. specs->iv_size = AES_IV_SIZE;
  1592. if (opts != NULL)
  1593. opts->usingPSK_cipher = 1;
  1594. break;
  1595. #endif
  1596. #ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
  1597. case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
  1598. specs->bulk_cipher_algorithm = wolfssl_aes;
  1599. specs->cipher_type = block;
  1600. specs->mac_algorithm = sha384_mac;
  1601. specs->kea = dhe_psk_kea;
  1602. specs->sig_algo = anonymous_sa_algo;
  1603. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1604. specs->pad_size = PAD_SHA;
  1605. specs->static_ecdh = 0;
  1606. specs->key_size = AES_256_KEY_SIZE;
  1607. specs->block_size = AES_BLOCK_SIZE;
  1608. specs->iv_size = AES_IV_SIZE;
  1609. if (opts != NULL)
  1610. opts->usingPSK_cipher = 1;
  1611. break;
  1612. #endif
  1613. #ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
  1614. case TLS_PSK_WITH_AES_128_CBC_SHA :
  1615. specs->bulk_cipher_algorithm = wolfssl_aes;
  1616. specs->cipher_type = block;
  1617. specs->mac_algorithm = sha_mac;
  1618. specs->kea = psk_kea;
  1619. specs->sig_algo = anonymous_sa_algo;
  1620. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1621. specs->pad_size = PAD_SHA;
  1622. specs->static_ecdh = 0;
  1623. specs->key_size = AES_128_KEY_SIZE;
  1624. specs->block_size = AES_BLOCK_SIZE;
  1625. specs->iv_size = AES_IV_SIZE;
  1626. if (opts != NULL)
  1627. opts->usingPSK_cipher = 1;
  1628. break;
  1629. #endif
  1630. #ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
  1631. case TLS_PSK_WITH_AES_256_CBC_SHA :
  1632. specs->bulk_cipher_algorithm = wolfssl_aes;
  1633. specs->cipher_type = block;
  1634. specs->mac_algorithm = sha_mac;
  1635. specs->kea = psk_kea;
  1636. specs->sig_algo = anonymous_sa_algo;
  1637. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1638. specs->pad_size = PAD_SHA;
  1639. specs->static_ecdh = 0;
  1640. specs->key_size = AES_256_KEY_SIZE;
  1641. specs->block_size = AES_BLOCK_SIZE;
  1642. specs->iv_size = AES_IV_SIZE;
  1643. if (opts != NULL)
  1644. opts->usingPSK_cipher = 1;
  1645. break;
  1646. #endif
  1647. #ifdef BUILD_TLS_PSK_WITH_NULL_SHA256
  1648. case TLS_PSK_WITH_NULL_SHA256 :
  1649. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1650. specs->cipher_type = stream;
  1651. specs->mac_algorithm = sha256_mac;
  1652. specs->kea = psk_kea;
  1653. specs->sig_algo = anonymous_sa_algo;
  1654. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1655. specs->pad_size = PAD_SHA;
  1656. specs->static_ecdh = 0;
  1657. specs->key_size = 0;
  1658. specs->block_size = 0;
  1659. specs->iv_size = 0;
  1660. if (opts != NULL)
  1661. opts->usingPSK_cipher = 1;
  1662. break;
  1663. #endif
  1664. #ifdef BUILD_TLS_PSK_WITH_NULL_SHA384
  1665. case TLS_PSK_WITH_NULL_SHA384 :
  1666. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1667. specs->cipher_type = stream;
  1668. specs->mac_algorithm = sha384_mac;
  1669. specs->kea = psk_kea;
  1670. specs->sig_algo = anonymous_sa_algo;
  1671. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1672. specs->pad_size = PAD_SHA;
  1673. specs->static_ecdh = 0;
  1674. specs->key_size = 0;
  1675. specs->block_size = 0;
  1676. specs->iv_size = 0;
  1677. if (opts != NULL)
  1678. opts->usingPSK_cipher = 1;
  1679. break;
  1680. #endif
  1681. #ifdef BUILD_TLS_PSK_WITH_NULL_SHA
  1682. case TLS_PSK_WITH_NULL_SHA :
  1683. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1684. specs->cipher_type = stream;
  1685. specs->mac_algorithm = sha_mac;
  1686. specs->kea = psk_kea;
  1687. specs->sig_algo = anonymous_sa_algo;
  1688. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1689. specs->pad_size = PAD_SHA;
  1690. specs->static_ecdh = 0;
  1691. specs->key_size = 0;
  1692. specs->block_size = 0;
  1693. specs->iv_size = 0;
  1694. if (opts != NULL)
  1695. opts->usingPSK_cipher = 1;
  1696. break;
  1697. #endif
  1698. #ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
  1699. case TLS_DHE_PSK_WITH_NULL_SHA256 :
  1700. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1701. specs->cipher_type = stream;
  1702. specs->mac_algorithm = sha256_mac;
  1703. specs->kea = dhe_psk_kea;
  1704. specs->sig_algo = anonymous_sa_algo;
  1705. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1706. specs->pad_size = PAD_SHA;
  1707. specs->static_ecdh = 0;
  1708. specs->key_size = 0;
  1709. specs->block_size = 0;
  1710. specs->iv_size = 0;
  1711. if (opts != NULL)
  1712. opts->usingPSK_cipher = 1;
  1713. break;
  1714. #endif
  1715. #ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
  1716. case TLS_DHE_PSK_WITH_NULL_SHA384 :
  1717. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  1718. specs->cipher_type = stream;
  1719. specs->mac_algorithm = sha384_mac;
  1720. specs->kea = dhe_psk_kea;
  1721. specs->sig_algo = anonymous_sa_algo;
  1722. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1723. specs->pad_size = PAD_SHA;
  1724. specs->static_ecdh = 0;
  1725. specs->key_size = 0;
  1726. specs->block_size = 0;
  1727. specs->iv_size = 0;
  1728. if (opts != NULL)
  1729. opts->usingPSK_cipher = 1;
  1730. break;
  1731. #endif
  1732. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
  1733. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
  1734. specs->bulk_cipher_algorithm = wolfssl_aes;
  1735. specs->cipher_type = block;
  1736. specs->mac_algorithm = sha256_mac;
  1737. specs->kea = diffie_hellman_kea;
  1738. specs->sig_algo = rsa_sa_algo;
  1739. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1740. specs->pad_size = PAD_SHA;
  1741. specs->static_ecdh = 0;
  1742. specs->key_size = AES_128_KEY_SIZE;
  1743. specs->block_size = AES_BLOCK_SIZE;
  1744. specs->iv_size = AES_IV_SIZE;
  1745. break;
  1746. #endif
  1747. #ifdef BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
  1748. case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA :
  1749. specs->bulk_cipher_algorithm = wolfssl_triple_des;
  1750. specs->cipher_type = block;
  1751. specs->mac_algorithm = sha_mac;
  1752. specs->kea = diffie_hellman_kea;
  1753. specs->sig_algo = rsa_sa_algo;
  1754. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1755. specs->pad_size = PAD_SHA;
  1756. specs->static_ecdh = 0;
  1757. specs->key_size = DES3_KEY_SIZE;
  1758. specs->block_size = DES_BLOCK_SIZE;
  1759. specs->iv_size = DES_IV_SIZE;
  1760. break;
  1761. #endif
  1762. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
  1763. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
  1764. specs->bulk_cipher_algorithm = wolfssl_aes;
  1765. specs->cipher_type = block;
  1766. specs->mac_algorithm = sha256_mac;
  1767. specs->kea = diffie_hellman_kea;
  1768. specs->sig_algo = rsa_sa_algo;
  1769. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1770. specs->pad_size = PAD_SHA;
  1771. specs->static_ecdh = 0;
  1772. specs->key_size = AES_256_KEY_SIZE;
  1773. specs->block_size = AES_BLOCK_SIZE;
  1774. specs->iv_size = AES_IV_SIZE;
  1775. break;
  1776. #endif
  1777. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  1778. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
  1779. specs->bulk_cipher_algorithm = wolfssl_aes;
  1780. specs->cipher_type = block;
  1781. specs->mac_algorithm = sha_mac;
  1782. specs->kea = diffie_hellman_kea;
  1783. specs->sig_algo = rsa_sa_algo;
  1784. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1785. specs->pad_size = PAD_SHA;
  1786. specs->static_ecdh = 0;
  1787. specs->key_size = AES_128_KEY_SIZE;
  1788. specs->block_size = AES_BLOCK_SIZE;
  1789. specs->iv_size = AES_IV_SIZE;
  1790. break;
  1791. #endif
  1792. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
  1793. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
  1794. specs->bulk_cipher_algorithm = wolfssl_aes;
  1795. specs->cipher_type = block;
  1796. specs->mac_algorithm = sha_mac;
  1797. specs->kea = diffie_hellman_kea;
  1798. specs->sig_algo = rsa_sa_algo;
  1799. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1800. specs->pad_size = PAD_SHA;
  1801. specs->static_ecdh = 0;
  1802. specs->key_size = AES_256_KEY_SIZE;
  1803. specs->block_size = AES_BLOCK_SIZE;
  1804. specs->iv_size = AES_IV_SIZE;
  1805. break;
  1806. #endif
  1807. #ifdef BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
  1808. case TLS_RSA_WITH_AES_128_GCM_SHA256 :
  1809. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1810. specs->cipher_type = aead;
  1811. specs->mac_algorithm = sha256_mac;
  1812. specs->kea = rsa_kea;
  1813. specs->sig_algo = rsa_sa_algo;
  1814. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1815. specs->pad_size = PAD_SHA;
  1816. specs->static_ecdh = 0;
  1817. specs->key_size = AES_128_KEY_SIZE;
  1818. specs->block_size = AES_BLOCK_SIZE;
  1819. specs->iv_size = AESGCM_IMP_IV_SZ;
  1820. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1821. break;
  1822. #endif
  1823. #ifdef BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
  1824. case TLS_RSA_WITH_AES_256_GCM_SHA384 :
  1825. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1826. specs->cipher_type = aead;
  1827. specs->mac_algorithm = sha384_mac;
  1828. specs->kea = rsa_kea;
  1829. specs->sig_algo = rsa_sa_algo;
  1830. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1831. specs->pad_size = PAD_SHA;
  1832. specs->static_ecdh = 0;
  1833. specs->key_size = AES_256_KEY_SIZE;
  1834. specs->block_size = AES_BLOCK_SIZE;
  1835. specs->iv_size = AESGCM_IMP_IV_SZ;
  1836. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1837. break;
  1838. #endif
  1839. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
  1840. case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
  1841. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1842. specs->cipher_type = aead;
  1843. specs->mac_algorithm = sha256_mac;
  1844. specs->kea = diffie_hellman_kea;
  1845. specs->sig_algo = rsa_sa_algo;
  1846. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1847. specs->pad_size = PAD_SHA;
  1848. specs->static_ecdh = 0;
  1849. specs->key_size = AES_128_KEY_SIZE;
  1850. specs->block_size = AES_BLOCK_SIZE;
  1851. specs->iv_size = AESGCM_IMP_IV_SZ;
  1852. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1853. break;
  1854. #endif
  1855. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
  1856. case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
  1857. specs->bulk_cipher_algorithm = wolfssl_aes_gcm;
  1858. specs->cipher_type = aead;
  1859. specs->mac_algorithm = sha384_mac;
  1860. specs->kea = diffie_hellman_kea;
  1861. specs->sig_algo = rsa_sa_algo;
  1862. specs->hash_size = WC_SHA384_DIGEST_SIZE;
  1863. specs->pad_size = PAD_SHA;
  1864. specs->static_ecdh = 0;
  1865. specs->key_size = AES_256_KEY_SIZE;
  1866. specs->block_size = AES_BLOCK_SIZE;
  1867. specs->iv_size = AESGCM_IMP_IV_SZ;
  1868. specs->aead_mac_size = AES_GCM_AUTH_SZ;
  1869. break;
  1870. #endif
  1871. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
  1872. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
  1873. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1874. specs->cipher_type = block;
  1875. specs->mac_algorithm = sha_mac;
  1876. specs->kea = rsa_kea;
  1877. specs->sig_algo = rsa_sa_algo;
  1878. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1879. specs->pad_size = PAD_SHA;
  1880. specs->static_ecdh = 0;
  1881. specs->key_size = CAMELLIA_128_KEY_SIZE;
  1882. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1883. specs->iv_size = CAMELLIA_IV_SIZE;
  1884. break;
  1885. #endif
  1886. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
  1887. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
  1888. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1889. specs->cipher_type = block;
  1890. specs->mac_algorithm = sha_mac;
  1891. specs->kea = rsa_kea;
  1892. specs->sig_algo = rsa_sa_algo;
  1893. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1894. specs->pad_size = PAD_SHA;
  1895. specs->static_ecdh = 0;
  1896. specs->key_size = CAMELLIA_256_KEY_SIZE;
  1897. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1898. specs->iv_size = CAMELLIA_IV_SIZE;
  1899. break;
  1900. #endif
  1901. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
  1902. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  1903. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1904. specs->cipher_type = block;
  1905. specs->mac_algorithm = sha256_mac;
  1906. specs->kea = rsa_kea;
  1907. specs->sig_algo = rsa_sa_algo;
  1908. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1909. specs->pad_size = PAD_SHA;
  1910. specs->static_ecdh = 0;
  1911. specs->key_size = CAMELLIA_128_KEY_SIZE;
  1912. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1913. specs->iv_size = CAMELLIA_IV_SIZE;
  1914. break;
  1915. #endif
  1916. #ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
  1917. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  1918. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1919. specs->cipher_type = block;
  1920. specs->mac_algorithm = sha256_mac;
  1921. specs->kea = rsa_kea;
  1922. specs->sig_algo = rsa_sa_algo;
  1923. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1924. specs->pad_size = PAD_SHA;
  1925. specs->static_ecdh = 0;
  1926. specs->key_size = CAMELLIA_256_KEY_SIZE;
  1927. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1928. specs->iv_size = CAMELLIA_IV_SIZE;
  1929. break;
  1930. #endif
  1931. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
  1932. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
  1933. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1934. specs->cipher_type = block;
  1935. specs->mac_algorithm = sha_mac;
  1936. specs->kea = diffie_hellman_kea;
  1937. specs->sig_algo = rsa_sa_algo;
  1938. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1939. specs->pad_size = PAD_SHA;
  1940. specs->static_ecdh = 0;
  1941. specs->key_size = CAMELLIA_128_KEY_SIZE;
  1942. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1943. specs->iv_size = CAMELLIA_IV_SIZE;
  1944. break;
  1945. #endif
  1946. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
  1947. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
  1948. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1949. specs->cipher_type = block;
  1950. specs->mac_algorithm = sha_mac;
  1951. specs->kea = diffie_hellman_kea;
  1952. specs->sig_algo = rsa_sa_algo;
  1953. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1954. specs->pad_size = PAD_SHA;
  1955. specs->static_ecdh = 0;
  1956. specs->key_size = CAMELLIA_256_KEY_SIZE;
  1957. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1958. specs->iv_size = CAMELLIA_IV_SIZE;
  1959. break;
  1960. #endif
  1961. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
  1962. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  1963. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1964. specs->cipher_type = block;
  1965. specs->mac_algorithm = sha256_mac;
  1966. specs->kea = diffie_hellman_kea;
  1967. specs->sig_algo = rsa_sa_algo;
  1968. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1969. specs->pad_size = PAD_SHA;
  1970. specs->static_ecdh = 0;
  1971. specs->key_size = CAMELLIA_128_KEY_SIZE;
  1972. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1973. specs->iv_size = CAMELLIA_IV_SIZE;
  1974. break;
  1975. #endif
  1976. #ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
  1977. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  1978. specs->bulk_cipher_algorithm = wolfssl_camellia;
  1979. specs->cipher_type = block;
  1980. specs->mac_algorithm = sha256_mac;
  1981. specs->kea = diffie_hellman_kea;
  1982. specs->sig_algo = rsa_sa_algo;
  1983. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  1984. specs->pad_size = PAD_SHA;
  1985. specs->static_ecdh = 0;
  1986. specs->key_size = CAMELLIA_256_KEY_SIZE;
  1987. specs->block_size = CAMELLIA_BLOCK_SIZE;
  1988. specs->iv_size = CAMELLIA_IV_SIZE;
  1989. break;
  1990. #endif
  1991. #ifdef BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
  1992. case TLS_DH_anon_WITH_AES_128_CBC_SHA :
  1993. specs->bulk_cipher_algorithm = wolfssl_aes;
  1994. specs->cipher_type = block;
  1995. specs->mac_algorithm = sha_mac;
  1996. specs->kea = diffie_hellman_kea;
  1997. specs->sig_algo = anonymous_sa_algo;
  1998. specs->hash_size = WC_SHA_DIGEST_SIZE;
  1999. specs->pad_size = PAD_SHA;
  2000. specs->static_ecdh = 0;
  2001. specs->key_size = AES_128_KEY_SIZE;
  2002. specs->block_size = AES_BLOCK_SIZE;
  2003. specs->iv_size = AES_IV_SIZE;
  2004. if (opts != NULL)
  2005. opts->usingAnon_cipher = 1;
  2006. break;
  2007. #endif
  2008. #ifdef BUILD_WDM_WITH_NULL_SHA256
  2009. case WDM_WITH_NULL_SHA256 :
  2010. specs->bulk_cipher_algorithm = wolfssl_cipher_null;
  2011. specs->cipher_type = stream;
  2012. specs->mac_algorithm = sha256_mac;
  2013. specs->kea = no_kea;
  2014. specs->sig_algo = anonymous_sa_algo;
  2015. specs->hash_size = WC_SHA256_DIGEST_SIZE;
  2016. specs->pad_size = PAD_SHA;
  2017. break;
  2018. #endif
  2019. default:
  2020. WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs");
  2021. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_SUITE);
  2022. return UNSUPPORTED_SUITE;
  2023. } /* switch */
  2024. } /* if ECC / Normal suites else */
  2025. if (specs->sig_algo == anonymous_sa_algo && opts != NULL) {
  2026. /* CLIENT/SERVER: No peer authentication to be performed. */
  2027. opts->peerAuthGood = 1;
  2028. }
  2029. return 0;
  2030. }
  2031. enum KeyStuff {
  2032. MASTER_ROUNDS = 3,
  2033. PREFIX = 3, /* up to three letters for master prefix */
  2034. KEY_PREFIX = 9 /* up to 9 prefix letters for key rounds */
  2035. };
  2036. #ifndef NO_OLD_TLS
  2037. /* true or false, zero for error */
  2038. static int SetPrefix(byte* sha_input, int idx)
  2039. {
  2040. switch (idx) {
  2041. case 0:
  2042. XMEMCPY(sha_input, "A", 1);
  2043. break;
  2044. case 1:
  2045. XMEMCPY(sha_input, "BB", 2);
  2046. break;
  2047. case 2:
  2048. XMEMCPY(sha_input, "CCC", 3);
  2049. break;
  2050. case 3:
  2051. XMEMCPY(sha_input, "DDDD", 4);
  2052. break;
  2053. case 4:
  2054. XMEMCPY(sha_input, "EEEEE", 5);
  2055. break;
  2056. case 5:
  2057. XMEMCPY(sha_input, "FFFFFF", 6);
  2058. break;
  2059. case 6:
  2060. XMEMCPY(sha_input, "GGGGGGG", 7);
  2061. break;
  2062. case 7:
  2063. XMEMCPY(sha_input, "HHHHHHHH", 8);
  2064. break;
  2065. case 8:
  2066. XMEMCPY(sha_input, "IIIIIIIII", 9);
  2067. break;
  2068. default:
  2069. WOLFSSL_MSG("Set Prefix error, bad input");
  2070. return 0;
  2071. }
  2072. return 1;
  2073. }
  2074. #endif
  2075. static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
  2076. int side, void* heap, int devId, WC_RNG* rng, int tls13)
  2077. {
  2078. (void)rng;
  2079. (void)tls13;
  2080. #ifdef BUILD_ARC4
  2081. if (specs->bulk_cipher_algorithm == wolfssl_rc4) {
  2082. word32 sz = specs->key_size;
  2083. if (enc && enc->arc4 == NULL) {
  2084. enc->arc4 = (Arc4*)XMALLOC(sizeof(Arc4), heap, DYNAMIC_TYPE_CIPHER);
  2085. if (enc->arc4 == NULL)
  2086. return MEMORY_E;
  2087. }
  2088. if (dec && dec->arc4 == NULL) {
  2089. dec->arc4 = (Arc4*)XMALLOC(sizeof(Arc4), heap, DYNAMIC_TYPE_CIPHER);
  2090. if (dec->arc4 == NULL)
  2091. return MEMORY_E;
  2092. }
  2093. if (enc) {
  2094. if (wc_Arc4Init(enc->arc4, heap, devId) != 0) {
  2095. WOLFSSL_MSG("Arc4Init failed in SetKeys");
  2096. return ASYNC_INIT_E;
  2097. }
  2098. }
  2099. if (dec) {
  2100. if (wc_Arc4Init(dec->arc4, heap, devId) != 0) {
  2101. WOLFSSL_MSG("Arc4Init failed in SetKeys");
  2102. return ASYNC_INIT_E;
  2103. }
  2104. }
  2105. if (side == WOLFSSL_CLIENT_END) {
  2106. if (enc)
  2107. wc_Arc4SetKey(enc->arc4, keys->client_write_key, sz);
  2108. if (dec)
  2109. wc_Arc4SetKey(dec->arc4, keys->server_write_key, sz);
  2110. }
  2111. else {
  2112. if (enc)
  2113. wc_Arc4SetKey(enc->arc4, keys->server_write_key, sz);
  2114. if (dec)
  2115. wc_Arc4SetKey(dec->arc4, keys->client_write_key, sz);
  2116. }
  2117. if (enc)
  2118. enc->setup = 1;
  2119. if (dec)
  2120. dec->setup = 1;
  2121. }
  2122. #endif /* BUILD_ARC4 */
  2123. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_CHAPOL_AEAD)
  2124. /* Check that the max implicit iv size is sufficient */
  2125. #if (AEAD_MAX_IMP_SZ < 12) /* CHACHA20_IMP_IV_SZ */
  2126. #error AEAD_MAX_IMP_SZ is too small for ChaCha20
  2127. #endif
  2128. #if (MAX_WRITE_IV_SZ < 12) /* CHACHA20_IMP_IV_SZ */
  2129. #error MAX_WRITE_IV_SZ is too small for ChaCha20
  2130. #endif
  2131. if (specs->bulk_cipher_algorithm == wolfssl_chacha) {
  2132. int chachaRet;
  2133. if (enc && enc->chacha == NULL)
  2134. enc->chacha =
  2135. (ChaCha*)XMALLOC(sizeof(ChaCha), heap, DYNAMIC_TYPE_CIPHER);
  2136. if (enc && enc->chacha == NULL)
  2137. return MEMORY_E;
  2138. #ifdef WOLFSSL_CHECK_MEM_ZERO
  2139. if (enc) {
  2140. wc_MemZero_Add("SSL keys enc chacha", enc->chacha, sizeof(ChaCha));
  2141. }
  2142. #endif
  2143. if (dec && dec->chacha == NULL)
  2144. dec->chacha =
  2145. (ChaCha*)XMALLOC(sizeof(ChaCha), heap, DYNAMIC_TYPE_CIPHER);
  2146. if (dec && dec->chacha == NULL)
  2147. return MEMORY_E;
  2148. #ifdef WOLFSSL_CHECK_MEM_ZERO
  2149. if (dec) {
  2150. wc_MemZero_Add("SSL keys dec chacha", dec->chacha, sizeof(ChaCha));
  2151. }
  2152. #endif
  2153. if (side == WOLFSSL_CLIENT_END) {
  2154. if (enc) {
  2155. chachaRet = wc_Chacha_SetKey(enc->chacha, keys->client_write_key,
  2156. specs->key_size);
  2157. XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
  2158. CHACHA20_IMP_IV_SZ);
  2159. if (chachaRet != 0) return chachaRet;
  2160. }
  2161. if (dec) {
  2162. chachaRet = wc_Chacha_SetKey(dec->chacha, keys->server_write_key,
  2163. specs->key_size);
  2164. XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
  2165. CHACHA20_IMP_IV_SZ);
  2166. if (chachaRet != 0) return chachaRet;
  2167. }
  2168. }
  2169. else {
  2170. if (enc) {
  2171. chachaRet = wc_Chacha_SetKey(enc->chacha, keys->server_write_key,
  2172. specs->key_size);
  2173. XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
  2174. CHACHA20_IMP_IV_SZ);
  2175. if (chachaRet != 0) return chachaRet;
  2176. }
  2177. if (dec) {
  2178. chachaRet = wc_Chacha_SetKey(dec->chacha, keys->client_write_key,
  2179. specs->key_size);
  2180. XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
  2181. CHACHA20_IMP_IV_SZ);
  2182. if (chachaRet != 0) return chachaRet;
  2183. }
  2184. }
  2185. if (enc)
  2186. enc->setup = 1;
  2187. if (dec)
  2188. dec->setup = 1;
  2189. }
  2190. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  2191. #ifdef BUILD_DES3
  2192. /* check that buffer sizes are sufficient */
  2193. #if (MAX_WRITE_IV_SZ < 8) /* DES_IV_SIZE */
  2194. #error MAX_WRITE_IV_SZ too small for 3DES
  2195. #endif
  2196. if (specs->bulk_cipher_algorithm == wolfssl_triple_des) {
  2197. int desRet = 0;
  2198. if (enc) {
  2199. if (enc->des3 == NULL)
  2200. enc->des3 = (Des3*)XMALLOC(sizeof(Des3), heap, DYNAMIC_TYPE_CIPHER);
  2201. if (enc->des3 == NULL)
  2202. return MEMORY_E;
  2203. XMEMSET(enc->des3, 0, sizeof(Des3));
  2204. }
  2205. if (dec) {
  2206. if (dec->des3 == NULL)
  2207. dec->des3 = (Des3*)XMALLOC(sizeof(Des3), heap, DYNAMIC_TYPE_CIPHER);
  2208. if (dec->des3 == NULL)
  2209. return MEMORY_E;
  2210. XMEMSET(dec->des3, 0, sizeof(Des3));
  2211. }
  2212. if (enc) {
  2213. if (wc_Des3Init(enc->des3, heap, devId) != 0) {
  2214. WOLFSSL_MSG("Des3Init failed in SetKeys");
  2215. return ASYNC_INIT_E;
  2216. }
  2217. }
  2218. if (dec) {
  2219. if (wc_Des3Init(dec->des3, heap, devId) != 0) {
  2220. WOLFSSL_MSG("Des3Init failed in SetKeys");
  2221. return ASYNC_INIT_E;
  2222. }
  2223. }
  2224. if (side == WOLFSSL_CLIENT_END) {
  2225. if (enc) {
  2226. desRet = wc_Des3_SetKey(enc->des3, keys->client_write_key,
  2227. keys->client_write_IV, DES_ENCRYPTION);
  2228. if (desRet != 0) return desRet;
  2229. }
  2230. if (dec) {
  2231. desRet = wc_Des3_SetKey(dec->des3, keys->server_write_key,
  2232. keys->server_write_IV, DES_DECRYPTION);
  2233. if (desRet != 0) return desRet;
  2234. }
  2235. }
  2236. else {
  2237. if (enc) {
  2238. desRet = wc_Des3_SetKey(enc->des3, keys->server_write_key,
  2239. keys->server_write_IV, DES_ENCRYPTION);
  2240. if (desRet != 0) return desRet;
  2241. }
  2242. if (dec) {
  2243. desRet = wc_Des3_SetKey(dec->des3, keys->client_write_key,
  2244. keys->client_write_IV, DES_DECRYPTION);
  2245. if (desRet != 0) return desRet;
  2246. }
  2247. }
  2248. if (enc)
  2249. enc->setup = 1;
  2250. if (dec)
  2251. dec->setup = 1;
  2252. }
  2253. #endif /* BUILD_DES3 */
  2254. #ifdef BUILD_AES
  2255. /* check that buffer sizes are sufficient */
  2256. #if (MAX_WRITE_IV_SZ < 16) /* AES_IV_SIZE */
  2257. #error MAX_WRITE_IV_SZ too small for AES
  2258. #endif
  2259. if (specs->bulk_cipher_algorithm == wolfssl_aes) {
  2260. int aesRet = 0;
  2261. if (enc) {
  2262. if (enc->aes == NULL) {
  2263. enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
  2264. if (enc->aes == NULL)
  2265. return MEMORY_E;
  2266. } else {
  2267. wc_AesFree(enc->aes);
  2268. }
  2269. XMEMSET(enc->aes, 0, sizeof(Aes));
  2270. }
  2271. if (dec) {
  2272. if (dec->aes == NULL) {
  2273. dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
  2274. if (dec->aes == NULL)
  2275. return MEMORY_E;
  2276. } else {
  2277. wc_AesFree(dec->aes);
  2278. }
  2279. XMEMSET(dec->aes, 0, sizeof(Aes));
  2280. }
  2281. if (enc) {
  2282. if (wc_AesInit(enc->aes, heap, devId) != 0) {
  2283. WOLFSSL_MSG("AesInit failed in SetKeys");
  2284. return ASYNC_INIT_E;
  2285. }
  2286. }
  2287. if (dec) {
  2288. if (wc_AesInit(dec->aes, heap, devId) != 0) {
  2289. WOLFSSL_MSG("AesInit failed in SetKeys");
  2290. return ASYNC_INIT_E;
  2291. }
  2292. }
  2293. if (side == WOLFSSL_CLIENT_END) {
  2294. if (enc) {
  2295. aesRet = wc_AesSetKey(enc->aes, keys->client_write_key,
  2296. specs->key_size, keys->client_write_IV,
  2297. AES_ENCRYPTION);
  2298. if (aesRet != 0) return aesRet;
  2299. }
  2300. if (dec) {
  2301. aesRet = wc_AesSetKey(dec->aes, keys->server_write_key,
  2302. specs->key_size, keys->server_write_IV,
  2303. AES_DECRYPTION);
  2304. if (aesRet != 0) return aesRet;
  2305. }
  2306. }
  2307. else {
  2308. if (enc) {
  2309. aesRet = wc_AesSetKey(enc->aes, keys->server_write_key,
  2310. specs->key_size, keys->server_write_IV,
  2311. AES_ENCRYPTION);
  2312. if (aesRet != 0) return aesRet;
  2313. }
  2314. if (dec) {
  2315. aesRet = wc_AesSetKey(dec->aes, keys->client_write_key,
  2316. specs->key_size, keys->client_write_IV,
  2317. AES_DECRYPTION);
  2318. if (aesRet != 0) return aesRet;
  2319. }
  2320. }
  2321. if (enc)
  2322. enc->setup = 1;
  2323. if (dec)
  2324. dec->setup = 1;
  2325. }
  2326. #endif /* BUILD_AES */
  2327. #ifdef BUILD_AESGCM
  2328. /* check that buffer sizes are sufficient */
  2329. #if (AEAD_MAX_IMP_SZ < 4) /* AESGCM_IMP_IV_SZ */
  2330. #error AEAD_MAX_IMP_SZ too small for AESGCM
  2331. #endif
  2332. #if (AEAD_MAX_EXP_SZ < 8) /* AESGCM_EXP_IV_SZ */
  2333. #error AEAD_MAX_EXP_SZ too small for AESGCM
  2334. #endif
  2335. #if (MAX_WRITE_IV_SZ < 4) /* AESGCM_IMP_IV_SZ */
  2336. #error MAX_WRITE_IV_SZ too small for AESGCM
  2337. #endif
  2338. if (specs->bulk_cipher_algorithm == wolfssl_aes_gcm) {
  2339. int gcmRet;
  2340. if (enc) {
  2341. if (enc->aes == NULL) {
  2342. enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
  2343. if (enc->aes == NULL)
  2344. return MEMORY_E;
  2345. } else {
  2346. wc_AesFree(enc->aes);
  2347. }
  2348. XMEMSET(enc->aes, 0, sizeof(Aes));
  2349. }
  2350. if (dec) {
  2351. if (dec->aes == NULL) {
  2352. dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
  2353. if (dec->aes == NULL)
  2354. return MEMORY_E;
  2355. } else {
  2356. wc_AesFree(dec->aes);
  2357. }
  2358. XMEMSET(dec->aes, 0, sizeof(Aes));
  2359. }
  2360. if (enc) {
  2361. if (wc_AesInit(enc->aes, heap, devId) != 0) {
  2362. WOLFSSL_MSG("AesInit failed in SetKeys");
  2363. return ASYNC_INIT_E;
  2364. }
  2365. }
  2366. if (dec) {
  2367. if (wc_AesInit(dec->aes, heap, devId) != 0) {
  2368. WOLFSSL_MSG("AesInit failed in SetKeys");
  2369. return ASYNC_INIT_E;
  2370. }
  2371. }
  2372. if (side == WOLFSSL_CLIENT_END) {
  2373. if (enc) {
  2374. gcmRet = wc_AesGcmSetKey(enc->aes, keys->client_write_key,
  2375. specs->key_size);
  2376. if (gcmRet != 0) return gcmRet;
  2377. XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
  2378. AEAD_MAX_IMP_SZ);
  2379. #if !defined(NO_PUBLIC_GCM_SET_IV) && \
  2380. ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  2381. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  2382. if (!tls13) {
  2383. gcmRet = wc_AesGcmSetIV(enc->aes, AESGCM_NONCE_SZ,
  2384. keys->client_write_IV, AESGCM_IMP_IV_SZ, rng);
  2385. if (gcmRet != 0) return gcmRet;
  2386. }
  2387. #endif
  2388. }
  2389. if (dec) {
  2390. gcmRet = wc_AesGcmSetKey(dec->aes, keys->server_write_key,
  2391. specs->key_size);
  2392. if (gcmRet != 0) return gcmRet;
  2393. XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
  2394. AEAD_MAX_IMP_SZ);
  2395. }
  2396. }
  2397. else {
  2398. if (enc) {
  2399. gcmRet = wc_AesGcmSetKey(enc->aes, keys->server_write_key,
  2400. specs->key_size);
  2401. if (gcmRet != 0) return gcmRet;
  2402. XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
  2403. AEAD_MAX_IMP_SZ);
  2404. #if !defined(NO_PUBLIC_GCM_SET_IV) && \
  2405. ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  2406. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  2407. if (!tls13) {
  2408. gcmRet = wc_AesGcmSetIV(enc->aes, AESGCM_NONCE_SZ,
  2409. keys->server_write_IV, AESGCM_IMP_IV_SZ, rng);
  2410. if (gcmRet != 0) return gcmRet;
  2411. }
  2412. #endif
  2413. }
  2414. if (dec) {
  2415. gcmRet = wc_AesGcmSetKey(dec->aes, keys->client_write_key,
  2416. specs->key_size);
  2417. if (gcmRet != 0) return gcmRet;
  2418. XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
  2419. AEAD_MAX_IMP_SZ);
  2420. }
  2421. }
  2422. if (enc)
  2423. enc->setup = 1;
  2424. if (dec)
  2425. dec->setup = 1;
  2426. }
  2427. #endif /* BUILD_AESGCM */
  2428. #ifdef HAVE_AESCCM
  2429. /* check that buffer sizes are sufficient (CCM is same size as GCM) */
  2430. #if (AEAD_MAX_IMP_SZ < 4) /* AESGCM_IMP_IV_SZ */
  2431. #error AEAD_MAX_IMP_SZ too small for AESCCM
  2432. #endif
  2433. #if (AEAD_MAX_EXP_SZ < 8) /* AESGCM_EXP_IV_SZ */
  2434. #error AEAD_MAX_EXP_SZ too small for AESCCM
  2435. #endif
  2436. #if (MAX_WRITE_IV_SZ < 4) /* AESGCM_IMP_IV_SZ */
  2437. #error MAX_WRITE_IV_SZ too small for AESCCM
  2438. #endif
  2439. if (specs->bulk_cipher_algorithm == wolfssl_aes_ccm) {
  2440. int CcmRet;
  2441. if (enc) {
  2442. if (enc->aes == NULL) {
  2443. enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
  2444. if (enc->aes == NULL)
  2445. return MEMORY_E;
  2446. } else {
  2447. wc_AesFree(enc->aes);
  2448. }
  2449. XMEMSET(enc->aes, 0, sizeof(Aes));
  2450. }
  2451. if (dec) {
  2452. if (dec->aes == NULL) {
  2453. dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
  2454. if (dec->aes == NULL)
  2455. return MEMORY_E;
  2456. } else {
  2457. wc_AesFree(dec->aes);
  2458. }
  2459. XMEMSET(dec->aes, 0, sizeof(Aes));
  2460. }
  2461. if (enc) {
  2462. if (wc_AesInit(enc->aes, heap, devId) != 0) {
  2463. WOLFSSL_MSG("AesInit failed in SetKeys");
  2464. return ASYNC_INIT_E;
  2465. }
  2466. }
  2467. if (dec) {
  2468. if (wc_AesInit(dec->aes, heap, devId) != 0) {
  2469. WOLFSSL_MSG("AesInit failed in SetKeys");
  2470. return ASYNC_INIT_E;
  2471. }
  2472. }
  2473. if (side == WOLFSSL_CLIENT_END) {
  2474. if (enc) {
  2475. CcmRet = wc_AesCcmSetKey(enc->aes, keys->client_write_key,
  2476. specs->key_size);
  2477. if (CcmRet != 0) {
  2478. return CcmRet;
  2479. }
  2480. XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
  2481. AEAD_MAX_IMP_SZ);
  2482. #if !defined(NO_PUBLIC_CCM_SET_NONCE) && \
  2483. ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  2484. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  2485. if (!tls13) {
  2486. CcmRet = wc_AesCcmSetNonce(enc->aes, keys->client_write_IV,
  2487. AEAD_MAX_IMP_SZ);
  2488. if (CcmRet != 0) return CcmRet;
  2489. }
  2490. #endif
  2491. }
  2492. if (dec) {
  2493. CcmRet = wc_AesCcmSetKey(dec->aes, keys->server_write_key,
  2494. specs->key_size);
  2495. if (CcmRet != 0) {
  2496. return CcmRet;
  2497. }
  2498. XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
  2499. AEAD_MAX_IMP_SZ);
  2500. }
  2501. }
  2502. else {
  2503. if (enc) {
  2504. CcmRet = wc_AesCcmSetKey(enc->aes, keys->server_write_key,
  2505. specs->key_size);
  2506. if (CcmRet != 0) {
  2507. return CcmRet;
  2508. }
  2509. XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
  2510. AEAD_MAX_IMP_SZ);
  2511. #if !defined(NO_PUBLIC_CCM_SET_NONCE) && \
  2512. ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  2513. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  2514. if (!tls13) {
  2515. CcmRet = wc_AesCcmSetNonce(enc->aes, keys->server_write_IV,
  2516. AEAD_MAX_IMP_SZ);
  2517. if (CcmRet != 0) return CcmRet;
  2518. }
  2519. #endif
  2520. }
  2521. if (dec) {
  2522. CcmRet = wc_AesCcmSetKey(dec->aes, keys->client_write_key,
  2523. specs->key_size);
  2524. if (CcmRet != 0) {
  2525. return CcmRet;
  2526. }
  2527. XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
  2528. AEAD_MAX_IMP_SZ);
  2529. }
  2530. }
  2531. if (enc)
  2532. enc->setup = 1;
  2533. if (dec)
  2534. dec->setup = 1;
  2535. }
  2536. #endif /* HAVE_AESCCM */
  2537. #ifdef HAVE_CAMELLIA
  2538. /* check that buffer sizes are sufficient */
  2539. #if (MAX_WRITE_IV_SZ < 16) /* CAMELLIA_IV_SIZE */
  2540. #error MAX_WRITE_IV_SZ too small for CAMELLIA
  2541. #endif
  2542. if (specs->bulk_cipher_algorithm == wolfssl_camellia) {
  2543. int camRet;
  2544. if (enc && enc->cam == NULL)
  2545. enc->cam =
  2546. (Camellia*)XMALLOC(sizeof(Camellia), heap, DYNAMIC_TYPE_CIPHER);
  2547. if (enc && enc->cam == NULL)
  2548. return MEMORY_E;
  2549. if (dec && dec->cam == NULL)
  2550. dec->cam =
  2551. (Camellia*)XMALLOC(sizeof(Camellia), heap, DYNAMIC_TYPE_CIPHER);
  2552. if (dec && dec->cam == NULL)
  2553. return MEMORY_E;
  2554. if (side == WOLFSSL_CLIENT_END) {
  2555. if (enc) {
  2556. camRet = wc_CamelliaSetKey(enc->cam, keys->client_write_key,
  2557. specs->key_size, keys->client_write_IV);
  2558. if (camRet != 0) return camRet;
  2559. }
  2560. if (dec) {
  2561. camRet = wc_CamelliaSetKey(dec->cam, keys->server_write_key,
  2562. specs->key_size, keys->server_write_IV);
  2563. if (camRet != 0) return camRet;
  2564. }
  2565. }
  2566. else {
  2567. if (enc) {
  2568. camRet = wc_CamelliaSetKey(enc->cam, keys->server_write_key,
  2569. specs->key_size, keys->server_write_IV);
  2570. if (camRet != 0) return camRet;
  2571. }
  2572. if (dec) {
  2573. camRet = wc_CamelliaSetKey(dec->cam, keys->client_write_key,
  2574. specs->key_size, keys->client_write_IV);
  2575. if (camRet != 0) return camRet;
  2576. }
  2577. }
  2578. if (enc)
  2579. enc->setup = 1;
  2580. if (dec)
  2581. dec->setup = 1;
  2582. }
  2583. #endif /* HAVE_CAMELLIA */
  2584. #ifdef WOLFSSL_SM4_CBC
  2585. /* check that buffer sizes are sufficient */
  2586. #if (MAX_WRITE_IV_SZ < 16) /* AES_IV_SIZE */
  2587. #error MAX_WRITE_IV_SZ too small for SM4_CBC
  2588. #endif
  2589. if (specs->bulk_cipher_algorithm == wolfssl_sm4_cbc) {
  2590. int sm4Ret = 0;
  2591. if (enc) {
  2592. if (enc->sm4 == NULL) {
  2593. enc->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
  2594. DYNAMIC_TYPE_CIPHER);
  2595. if (enc->sm4 == NULL)
  2596. return MEMORY_E;
  2597. }
  2598. else {
  2599. wc_Sm4Free(enc->sm4);
  2600. }
  2601. XMEMSET(enc->sm4, 0, sizeof(wc_Sm4));
  2602. }
  2603. if (dec) {
  2604. if (dec->sm4 == NULL) {
  2605. dec->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
  2606. DYNAMIC_TYPE_CIPHER);
  2607. if (dec->sm4 == NULL)
  2608. return MEMORY_E;
  2609. }
  2610. else {
  2611. wc_Sm4Free(dec->sm4);
  2612. }
  2613. XMEMSET(dec->sm4, 0, sizeof(wc_Sm4));
  2614. }
  2615. if (enc) {
  2616. if (wc_Sm4Init(enc->sm4, heap, devId) != 0) {
  2617. WOLFSSL_MSG("Sm4Init failed in SetKeys");
  2618. return ASYNC_INIT_E;
  2619. }
  2620. }
  2621. if (dec) {
  2622. if (wc_Sm4Init(dec->sm4, heap, devId) != 0) {
  2623. WOLFSSL_MSG("Sm4Init failed in SetKeys");
  2624. return ASYNC_INIT_E;
  2625. }
  2626. }
  2627. if (side == WOLFSSL_CLIENT_END) {
  2628. if (enc) {
  2629. sm4Ret = wc_Sm4SetKey(enc->sm4, keys->client_write_key,
  2630. specs->key_size);
  2631. if (sm4Ret != 0) return sm4Ret;
  2632. sm4Ret = wc_Sm4SetIV(enc->sm4, keys->client_write_IV);
  2633. if (sm4Ret != 0) return sm4Ret;
  2634. }
  2635. if (dec) {
  2636. sm4Ret = wc_Sm4SetKey(dec->sm4, keys->server_write_key,
  2637. specs->key_size);
  2638. if (sm4Ret != 0) return sm4Ret;
  2639. sm4Ret = wc_Sm4SetIV(dec->sm4, keys->server_write_IV);
  2640. if (sm4Ret != 0) return sm4Ret;
  2641. }
  2642. }
  2643. else {
  2644. if (enc) {
  2645. sm4Ret = wc_Sm4SetKey(enc->sm4, keys->server_write_key,
  2646. specs->key_size);
  2647. if (sm4Ret != 0) return sm4Ret;
  2648. sm4Ret = wc_Sm4SetIV(enc->sm4, keys->server_write_IV);
  2649. if (sm4Ret != 0) return sm4Ret;
  2650. }
  2651. if (dec) {
  2652. sm4Ret = wc_Sm4SetKey(dec->sm4, keys->client_write_key,
  2653. specs->key_size);
  2654. if (sm4Ret != 0) return sm4Ret;
  2655. sm4Ret = wc_Sm4SetIV(dec->sm4, keys->client_write_IV);
  2656. if (sm4Ret != 0) return sm4Ret;
  2657. }
  2658. }
  2659. if (enc)
  2660. enc->setup = 1;
  2661. if (dec)
  2662. dec->setup = 1;
  2663. }
  2664. #endif /* WOLFSSL_SM4_CBC */
  2665. #ifdef WOLFSSL_SM4_GCM
  2666. /* check that buffer sizes are sufficient */
  2667. #if (AEAD_MAX_IMP_SZ < 4) /* SM4-GCM_IMP_IV_SZ */
  2668. #error AEAD_MAX_IMP_SZ too small for SM4-GCM
  2669. #endif
  2670. #if (AEAD_MAX_EXP_SZ < 8) /* SM4-GCM_EXP_IV_SZ */
  2671. #error AEAD_MAX_EXP_SZ too small for SM4-GCM
  2672. #endif
  2673. #if (MAX_WRITE_IV_SZ < 4) /* SM4-GCM_IMP_IV_SZ */
  2674. #error MAX_WRITE_IV_SZ too small for SM4-GCM
  2675. #endif
  2676. if (specs->bulk_cipher_algorithm == wolfssl_sm4_gcm) {
  2677. int gcmRet;
  2678. if (enc) {
  2679. if (enc->sm4 == NULL) {
  2680. enc->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
  2681. DYNAMIC_TYPE_CIPHER);
  2682. if (enc->sm4 == NULL)
  2683. return MEMORY_E;
  2684. } else {
  2685. wc_Sm4Free(enc->sm4);
  2686. }
  2687. XMEMSET(enc->sm4, 0, sizeof(wc_Sm4));
  2688. }
  2689. if (dec) {
  2690. if (dec->sm4 == NULL) {
  2691. dec->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
  2692. DYNAMIC_TYPE_CIPHER);
  2693. if (dec->sm4 == NULL)
  2694. return MEMORY_E;
  2695. } else {
  2696. wc_Sm4Free(dec->sm4);
  2697. }
  2698. XMEMSET(dec->sm4, 0, sizeof(wc_Sm4));
  2699. }
  2700. if (enc) {
  2701. if (wc_Sm4Init(enc->sm4, heap, devId) != 0) {
  2702. WOLFSSL_MSG("Sm4Init failed in SetKeys");
  2703. return ASYNC_INIT_E;
  2704. }
  2705. }
  2706. if (dec) {
  2707. if (wc_Sm4Init(dec->sm4, heap, devId) != 0) {
  2708. WOLFSSL_MSG("Sm4Init failed in SetKeys");
  2709. return ASYNC_INIT_E;
  2710. }
  2711. }
  2712. if (side == WOLFSSL_CLIENT_END) {
  2713. if (enc) {
  2714. gcmRet = wc_Sm4GcmSetKey(enc->sm4, keys->client_write_key,
  2715. specs->key_size);
  2716. if (gcmRet != 0) return gcmRet;
  2717. XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
  2718. AEAD_MAX_IMP_SZ);
  2719. }
  2720. if (dec) {
  2721. gcmRet = wc_Sm4GcmSetKey(dec->sm4, keys->server_write_key,
  2722. specs->key_size);
  2723. if (gcmRet != 0) return gcmRet;
  2724. XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
  2725. AEAD_MAX_IMP_SZ);
  2726. }
  2727. }
  2728. else {
  2729. if (enc) {
  2730. gcmRet = wc_Sm4GcmSetKey(enc->sm4, keys->server_write_key,
  2731. specs->key_size);
  2732. if (gcmRet != 0) return gcmRet;
  2733. XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
  2734. AEAD_MAX_IMP_SZ);
  2735. }
  2736. if (dec) {
  2737. gcmRet = wc_Sm4GcmSetKey(dec->sm4, keys->client_write_key,
  2738. specs->key_size);
  2739. if (gcmRet != 0) return gcmRet;
  2740. XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
  2741. AEAD_MAX_IMP_SZ);
  2742. }
  2743. }
  2744. if (enc)
  2745. enc->setup = 1;
  2746. if (dec)
  2747. dec->setup = 1;
  2748. }
  2749. #endif /* WOLFSSL_SM4_GCM */
  2750. #ifdef WOLFSSL_SM4_CCM
  2751. /* check that buffer sizes are sufficient (CCM is same size as GCM) */
  2752. #if (AEAD_MAX_IMP_SZ < 4) /* SM4-CCM_IMP_IV_SZ */
  2753. #error AEAD_MAX_IMP_SZ too small for SM4-CCM
  2754. #endif
  2755. #if (AEAD_MAX_EXP_SZ < 8) /* SM4-CCM_EXP_IV_SZ */
  2756. #error AEAD_MAX_EXP_SZ too small for SM4-CCM
  2757. #endif
  2758. #if (MAX_WRITE_IV_SZ < 4) /* SM4-CCM_IMP_IV_SZ */
  2759. #error MAX_WRITE_IV_SZ too small for SM4-CCM
  2760. #endif
  2761. if (specs->bulk_cipher_algorithm == wolfssl_sm4_ccm) {
  2762. int CcmRet;
  2763. if (enc) {
  2764. if (enc->sm4 == NULL) {
  2765. enc->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
  2766. DYNAMIC_TYPE_CIPHER);
  2767. if (enc->sm4 == NULL)
  2768. return MEMORY_E;
  2769. } else {
  2770. wc_Sm4Free(enc->sm4);
  2771. }
  2772. XMEMSET(enc->sm4, 0, sizeof(wc_Sm4));
  2773. }
  2774. if (dec) {
  2775. if (dec->sm4 == NULL) {
  2776. dec->sm4 = (wc_Sm4*)XMALLOC(sizeof(wc_Sm4), heap,
  2777. DYNAMIC_TYPE_CIPHER);
  2778. if (dec->sm4 == NULL)
  2779. return MEMORY_E;
  2780. } else {
  2781. wc_Sm4Free(dec->sm4);
  2782. }
  2783. XMEMSET(dec->sm4, 0, sizeof(wc_Sm4));
  2784. }
  2785. if (enc) {
  2786. if (wc_Sm4Init(enc->sm4, heap, devId) != 0) {
  2787. WOLFSSL_MSG("Sm4Init failed in SetKeys");
  2788. return ASYNC_INIT_E;
  2789. }
  2790. }
  2791. if (dec) {
  2792. if (wc_Sm4Init(dec->sm4, heap, devId) != 0) {
  2793. WOLFSSL_MSG("Sm4Init failed in SetKeys");
  2794. return ASYNC_INIT_E;
  2795. }
  2796. }
  2797. if (side == WOLFSSL_CLIENT_END) {
  2798. if (enc) {
  2799. CcmRet = wc_Sm4SetKey(enc->sm4, keys->client_write_key,
  2800. specs->key_size);
  2801. if (CcmRet != 0) {
  2802. return CcmRet;
  2803. }
  2804. XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
  2805. AEAD_MAX_IMP_SZ);
  2806. }
  2807. if (dec) {
  2808. CcmRet = wc_Sm4SetKey(dec->sm4, keys->server_write_key,
  2809. specs->key_size);
  2810. if (CcmRet != 0) {
  2811. return CcmRet;
  2812. }
  2813. XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
  2814. AEAD_MAX_IMP_SZ);
  2815. }
  2816. }
  2817. else {
  2818. if (enc) {
  2819. CcmRet = wc_Sm4SetKey(enc->sm4, keys->server_write_key,
  2820. specs->key_size);
  2821. if (CcmRet != 0) {
  2822. return CcmRet;
  2823. }
  2824. XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
  2825. AEAD_MAX_IMP_SZ);
  2826. }
  2827. if (dec) {
  2828. CcmRet = wc_Sm4SetKey(dec->sm4, keys->client_write_key,
  2829. specs->key_size);
  2830. if (CcmRet != 0) {
  2831. return CcmRet;
  2832. }
  2833. XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
  2834. AEAD_MAX_IMP_SZ);
  2835. }
  2836. }
  2837. if (enc)
  2838. enc->setup = 1;
  2839. if (dec)
  2840. dec->setup = 1;
  2841. }
  2842. #endif /* WOLFSSL_SM4_CCM */
  2843. #ifdef HAVE_NULL_CIPHER
  2844. if (specs->bulk_cipher_algorithm == wolfssl_cipher_null) {
  2845. #ifdef WOLFSSL_TLS13
  2846. if (tls13) {
  2847. int hmacRet;
  2848. int hashType = WC_HASH_TYPE_NONE;
  2849. switch (specs->mac_algorithm) {
  2850. case sha256_mac:
  2851. hashType = WC_SHA256;
  2852. break;
  2853. case sha384_mac:
  2854. hashType = WC_SHA384;
  2855. break;
  2856. default:
  2857. break;
  2858. }
  2859. if (enc && enc->hmac == NULL) {
  2860. enc->hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap,
  2861. DYNAMIC_TYPE_CIPHER);
  2862. if (enc->hmac == NULL)
  2863. return MEMORY_E;
  2864. }
  2865. if (enc) {
  2866. if (wc_HmacInit(enc->hmac, heap, devId) != 0) {
  2867. WOLFSSL_MSG("HmacInit failed in SetKeys");
  2868. XFREE(enc->hmac, heap, DYNAMIC_TYPE_CIPHER);
  2869. enc->hmac = NULL;
  2870. return ASYNC_INIT_E;
  2871. }
  2872. }
  2873. if (dec && dec->hmac == NULL) {
  2874. dec->hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap,
  2875. DYNAMIC_TYPE_CIPHER);
  2876. if (dec->hmac == NULL)
  2877. return MEMORY_E;
  2878. }
  2879. if (dec) {
  2880. if (wc_HmacInit(dec->hmac, heap, devId) != 0) {
  2881. WOLFSSL_MSG("HmacInit failed in SetKeys");
  2882. XFREE(dec->hmac, heap, DYNAMIC_TYPE_CIPHER);
  2883. dec->hmac = NULL;
  2884. return ASYNC_INIT_E;
  2885. }
  2886. }
  2887. if (side == WOLFSSL_CLIENT_END) {
  2888. if (enc) {
  2889. XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
  2890. HMAC_NONCE_SZ);
  2891. hmacRet = wc_HmacSetKey(enc->hmac, hashType,
  2892. keys->client_write_key, specs->key_size);
  2893. if (hmacRet != 0) return hmacRet;
  2894. }
  2895. if (dec) {
  2896. XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
  2897. HMAC_NONCE_SZ);
  2898. hmacRet = wc_HmacSetKey(dec->hmac, hashType,
  2899. keys->server_write_key, specs->key_size);
  2900. if (hmacRet != 0) return hmacRet;
  2901. }
  2902. }
  2903. else {
  2904. if (enc) {
  2905. XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
  2906. HMAC_NONCE_SZ);
  2907. hmacRet = wc_HmacSetKey(enc->hmac, hashType,
  2908. keys->server_write_key, specs->key_size);
  2909. if (hmacRet != 0) return hmacRet;
  2910. }
  2911. if (dec) {
  2912. XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
  2913. HMAC_NONCE_SZ);
  2914. hmacRet = wc_HmacSetKey(dec->hmac, hashType,
  2915. keys->client_write_key, specs->key_size);
  2916. if (hmacRet != 0) return hmacRet;
  2917. }
  2918. }
  2919. }
  2920. #endif
  2921. if (enc)
  2922. enc->setup = 1;
  2923. if (dec)
  2924. dec->setup = 1;
  2925. }
  2926. #endif
  2927. if (enc) {
  2928. keys->sequence_number_hi = 0;
  2929. keys->sequence_number_lo = 0;
  2930. }
  2931. if (dec) {
  2932. keys->peer_sequence_number_hi = 0;
  2933. keys->peer_sequence_number_lo = 0;
  2934. }
  2935. (void)side;
  2936. (void)heap;
  2937. (void)enc;
  2938. (void)dec;
  2939. (void)specs;
  2940. (void)devId;
  2941. return 0;
  2942. }
  2943. #ifdef HAVE_ONE_TIME_AUTH
  2944. /* set one time authentication keys */
  2945. static int SetAuthKeys(OneTimeAuth* authentication, Keys* keys,
  2946. CipherSpecs* specs, void* heap, int devId)
  2947. {
  2948. #ifdef HAVE_POLY1305
  2949. /* set up memory space for poly1305 */
  2950. if (authentication && authentication->poly1305 == NULL)
  2951. authentication->poly1305 =
  2952. (Poly1305*)XMALLOC(sizeof(Poly1305), heap, DYNAMIC_TYPE_CIPHER);
  2953. if (authentication && authentication->poly1305 == NULL)
  2954. return MEMORY_E;
  2955. #ifdef WOLFSSL_CHECK_MEM_ZERO
  2956. wc_MemZero_Add("SSL auth keys poly1305", authentication->poly1305,
  2957. sizeof(Poly1305));
  2958. #endif
  2959. if (authentication)
  2960. authentication->setup = 1;
  2961. #endif
  2962. (void)authentication;
  2963. (void)heap;
  2964. (void)keys;
  2965. (void)specs;
  2966. (void)devId;
  2967. return 0;
  2968. }
  2969. #endif /* HAVE_ONE_TIME_AUTH */
  2970. #ifdef HAVE_SECURE_RENEGOTIATION
  2971. /* function name is for cache_status++
  2972. * This function was added because of error incrementing enum type when
  2973. * compiling with a C++ compiler.
  2974. */
  2975. static void CacheStatusPP(SecureRenegotiation* cache)
  2976. {
  2977. switch (cache->cache_status) {
  2978. case SCR_CACHE_NULL:
  2979. cache->cache_status = SCR_CACHE_NEEDED;
  2980. break;
  2981. case SCR_CACHE_NEEDED:
  2982. cache->cache_status = SCR_CACHE_COPY;
  2983. break;
  2984. case SCR_CACHE_COPY:
  2985. cache->cache_status = SCR_CACHE_PARTIAL;
  2986. break;
  2987. case SCR_CACHE_PARTIAL:
  2988. cache->cache_status = SCR_CACHE_COMPLETE;
  2989. break;
  2990. case SCR_CACHE_COMPLETE:
  2991. WOLFSSL_MSG("SCR Cache state Complete");
  2992. break;
  2993. default:
  2994. WOLFSSL_MSG("Unknown cache state!!");
  2995. }
  2996. }
  2997. #endif /* HAVE_SECURE_RENEGOTIATION */
  2998. /* Set wc_encrypt/wc_decrypt or both sides of key setup
  2999. * note: use wc_encrypt to avoid shadowing global encrypt
  3000. * declared in unistd.h
  3001. */
  3002. int SetKeysSide(WOLFSSL* ssl, enum encrypt_side side)
  3003. {
  3004. int ret, copy = 0;
  3005. Ciphers* wc_encrypt = NULL;
  3006. Ciphers* wc_decrypt = NULL;
  3007. Keys* keys = &ssl->keys;
  3008. (void)copy;
  3009. #ifdef HAVE_SECURE_RENEGOTIATION
  3010. if (ssl->secure_renegotiation &&
  3011. ssl->secure_renegotiation->cache_status != SCR_CACHE_NULL) {
  3012. keys = &ssl->secure_renegotiation->tmp_keys;
  3013. #ifdef WOLFSSL_DTLS
  3014. /* For DTLS, copy is done in StoreKeys */
  3015. if (!ssl->options.dtls)
  3016. #endif
  3017. copy = 1;
  3018. }
  3019. #endif /* HAVE_SECURE_RENEGOTIATION */
  3020. switch (side) {
  3021. case ENCRYPT_SIDE_ONLY:
  3022. #ifdef WOLFSSL_DEBUG_TLS
  3023. WOLFSSL_MSG("Provisioning ENCRYPT key");
  3024. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  3025. WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
  3026. }
  3027. else {
  3028. WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
  3029. }
  3030. #endif
  3031. wc_encrypt = &ssl->encrypt;
  3032. break;
  3033. case DECRYPT_SIDE_ONLY:
  3034. #ifdef WOLFSSL_DEBUG_TLS
  3035. WOLFSSL_MSG("Provisioning DECRYPT key");
  3036. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  3037. WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
  3038. }
  3039. else {
  3040. WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
  3041. }
  3042. #endif
  3043. wc_decrypt = &ssl->decrypt;
  3044. break;
  3045. case ENCRYPT_AND_DECRYPT_SIDE:
  3046. #ifdef WOLFSSL_DEBUG_TLS
  3047. WOLFSSL_MSG("Provisioning ENCRYPT key");
  3048. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  3049. WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
  3050. }
  3051. else {
  3052. WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
  3053. }
  3054. WOLFSSL_MSG("Provisioning DECRYPT key");
  3055. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  3056. WOLFSSL_BUFFER(keys->server_write_key, ssl->specs.key_size);
  3057. }
  3058. else {
  3059. WOLFSSL_BUFFER(keys->client_write_key, ssl->specs.key_size);
  3060. }
  3061. #endif
  3062. wc_encrypt = &ssl->encrypt;
  3063. wc_decrypt = &ssl->decrypt;
  3064. break;
  3065. default:
  3066. return BAD_FUNC_ARG;
  3067. }
  3068. #ifdef HAVE_ONE_TIME_AUTH
  3069. if (!ssl->auth.setup && ssl->specs.bulk_cipher_algorithm == wolfssl_chacha){
  3070. ret = SetAuthKeys(&ssl->auth, keys, &ssl->specs, ssl->heap, ssl->devId);
  3071. if (ret != 0)
  3072. return ret;
  3073. }
  3074. #endif
  3075. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  3076. ret = PROTOCOLCB_UNAVAILABLE;
  3077. if (ssl->ctx->EncryptKeysCb) {
  3078. void* ctx = wolfSSL_GetEncryptKeysCtx(ssl);
  3079. ret = ssl->ctx->EncryptKeysCb(ssl, ctx);
  3080. }
  3081. if (!ssl->ctx->EncryptKeysCb || ret == PROTOCOLCB_UNAVAILABLE)
  3082. #endif
  3083. {
  3084. ret = SetKeys(wc_encrypt, wc_decrypt, keys, &ssl->specs, ssl->options.side,
  3085. ssl->heap, ssl->devId, ssl->rng, ssl->options.tls1_3);
  3086. }
  3087. #ifdef WOLFSSL_DTLS13
  3088. if (ret == 0 && ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  3089. ret = Dtls13SetRecordNumberKeys(ssl, side);
  3090. #endif /* WOLFSSL_DTLS13 */
  3091. #ifdef WOLFSSL_QUIC
  3092. if (ret == 0 && WOLFSSL_IS_QUIC(ssl)) {
  3093. ret = wolfSSL_quic_keys_active(ssl, side);
  3094. }
  3095. #endif /* WOLFSSL_QUIC */
  3096. #ifdef HAVE_SECURE_RENEGOTIATION
  3097. #ifdef WOLFSSL_DTLS
  3098. if (ret == 0 && ssl->options.dtls && !ssl->options.tls1_3) {
  3099. if (wc_encrypt)
  3100. wc_encrypt->src = keys == &ssl->keys ? KEYS : SCR;
  3101. if (wc_decrypt)
  3102. wc_decrypt->src = keys == &ssl->keys ? KEYS : SCR;
  3103. }
  3104. #endif
  3105. if (copy) {
  3106. int clientCopy = 0;
  3107. /* Sanity check that keys == ssl->secure_renegotiation->tmp_keys.
  3108. * Otherwise the memcpy calls would copy overlapping memory
  3109. * and cause UB. Fail early. */
  3110. if (keys == &ssl->keys)
  3111. return BAD_FUNC_ARG;
  3112. if (ssl->options.side == WOLFSSL_CLIENT_END && wc_encrypt)
  3113. clientCopy = 1;
  3114. else if (ssl->options.side == WOLFSSL_SERVER_END && wc_decrypt)
  3115. clientCopy = 1;
  3116. if (clientCopy) {
  3117. #ifndef WOLFSSL_AEAD_ONLY
  3118. XMEMCPY(ssl->keys.client_write_MAC_secret,
  3119. keys->client_write_MAC_secret, WC_MAX_DIGEST_SIZE);
  3120. #endif
  3121. XMEMCPY(ssl->keys.client_write_key,
  3122. keys->client_write_key, AES_256_KEY_SIZE);
  3123. XMEMCPY(ssl->keys.client_write_IV,
  3124. keys->client_write_IV, MAX_WRITE_IV_SZ);
  3125. } else {
  3126. #ifndef WOLFSSL_AEAD_ONLY
  3127. XMEMCPY(ssl->keys.server_write_MAC_secret,
  3128. keys->server_write_MAC_secret, WC_MAX_DIGEST_SIZE);
  3129. #endif
  3130. XMEMCPY(ssl->keys.server_write_key,
  3131. keys->server_write_key, AES_256_KEY_SIZE);
  3132. XMEMCPY(ssl->keys.server_write_IV,
  3133. keys->server_write_IV, MAX_WRITE_IV_SZ);
  3134. }
  3135. if (wc_encrypt) {
  3136. ssl->keys.sequence_number_hi = keys->sequence_number_hi;
  3137. ssl->keys.sequence_number_lo = keys->sequence_number_lo;
  3138. #ifdef HAVE_AEAD
  3139. if (ssl->specs.cipher_type == aead) {
  3140. /* Initialize the AES-GCM/CCM explicit IV to a zero. */
  3141. XMEMCPY(ssl->keys.aead_exp_IV, keys->aead_exp_IV,
  3142. AEAD_MAX_EXP_SZ);
  3143. /* Initialize encrypt implicit IV by encrypt side */
  3144. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  3145. XMEMCPY(ssl->keys.aead_enc_imp_IV,
  3146. keys->client_write_IV, AEAD_MAX_IMP_SZ);
  3147. } else {
  3148. XMEMCPY(ssl->keys.aead_enc_imp_IV,
  3149. keys->server_write_IV, AEAD_MAX_IMP_SZ);
  3150. }
  3151. }
  3152. #endif
  3153. }
  3154. if (wc_decrypt) {
  3155. ssl->keys.peer_sequence_number_hi = keys->peer_sequence_number_hi;
  3156. ssl->keys.peer_sequence_number_lo = keys->peer_sequence_number_lo;
  3157. #ifdef HAVE_AEAD
  3158. if (ssl->specs.cipher_type == aead) {
  3159. /* Initialize decrypt implicit IV by decrypt side */
  3160. if (ssl->options.side == WOLFSSL_SERVER_END) {
  3161. XMEMCPY(ssl->keys.aead_dec_imp_IV,
  3162. keys->client_write_IV, AEAD_MAX_IMP_SZ);
  3163. } else {
  3164. XMEMCPY(ssl->keys.aead_dec_imp_IV,
  3165. keys->server_write_IV, AEAD_MAX_IMP_SZ);
  3166. }
  3167. }
  3168. #endif
  3169. }
  3170. CacheStatusPP(ssl->secure_renegotiation);
  3171. }
  3172. #endif /* HAVE_SECURE_RENEGOTIATION */
  3173. return ret;
  3174. }
  3175. /* TLS can call too */
  3176. int StoreKeys(WOLFSSL* ssl, const byte* keyData, int side)
  3177. {
  3178. int sz, i = 0;
  3179. Keys* keys = &ssl->keys;
  3180. #ifdef WOLFSSL_DTLS
  3181. /* In case of DTLS, ssl->keys is updated here */
  3182. int scr_copy = 0;
  3183. #endif
  3184. #ifdef HAVE_SECURE_RENEGOTIATION
  3185. if (ssl->secure_renegotiation &&
  3186. ssl->secure_renegotiation->cache_status == SCR_CACHE_NEEDED) {
  3187. keys = &ssl->secure_renegotiation->tmp_keys;
  3188. #ifdef WOLFSSL_DTLS
  3189. if (ssl->options.dtls) {
  3190. /* epoch is incremented after StoreKeys is called */
  3191. ssl->secure_renegotiation->tmp_keys.dtls_epoch = ssl->keys.dtls_epoch + 1;
  3192. /* we only need to copy keys on second and future renegotiations */
  3193. if (ssl->keys.dtls_epoch > 1)
  3194. scr_copy = 1;
  3195. ssl->encrypt.src = KEYS_NOT_SET;
  3196. ssl->decrypt.src = KEYS_NOT_SET;
  3197. }
  3198. #endif
  3199. CacheStatusPP(ssl->secure_renegotiation);
  3200. }
  3201. #endif /* HAVE_SECURE_RENEGOTIATION */
  3202. #ifdef WOLFSSL_MULTICAST
  3203. if (ssl->options.haveMcast) {
  3204. /* Use the same keys for encrypt and decrypt. */
  3205. if (ssl->specs.cipher_type != aead) {
  3206. sz = ssl->specs.hash_size;
  3207. #ifndef WOLFSSL_AEAD_ONLY
  3208. #ifdef WOLFSSL_DTLS
  3209. if (scr_copy) {
  3210. XMEMCPY(ssl->keys.client_write_MAC_secret,
  3211. keys->client_write_MAC_secret, sz);
  3212. XMEMCPY(ssl->keys.server_write_MAC_secret,
  3213. keys->server_write_MAC_secret, sz);
  3214. }
  3215. #endif
  3216. XMEMCPY(keys->client_write_MAC_secret,&keyData[i], sz);
  3217. XMEMCPY(keys->server_write_MAC_secret,&keyData[i], sz);
  3218. #endif
  3219. i += sz;
  3220. }
  3221. sz = ssl->specs.key_size;
  3222. #ifdef WOLFSSL_DTLS
  3223. if (scr_copy) {
  3224. XMEMCPY(ssl->keys.client_write_key,
  3225. keys->client_write_key, sz);
  3226. XMEMCPY(ssl->keys.server_write_key,
  3227. keys->server_write_key, sz);
  3228. }
  3229. #endif
  3230. XMEMCPY(keys->client_write_key, &keyData[i], sz);
  3231. XMEMCPY(keys->server_write_key, &keyData[i], sz);
  3232. i += sz;
  3233. sz = ssl->specs.iv_size;
  3234. #ifdef WOLFSSL_DTLS
  3235. if (scr_copy) {
  3236. XMEMCPY(ssl->keys.client_write_IV,
  3237. keys->client_write_IV, sz);
  3238. XMEMCPY(ssl->keys.server_write_IV,
  3239. keys->server_write_IV, sz);
  3240. }
  3241. #endif
  3242. XMEMCPY(keys->client_write_IV, &keyData[i], sz);
  3243. XMEMCPY(keys->server_write_IV, &keyData[i], sz);
  3244. #ifdef HAVE_AEAD
  3245. if (ssl->specs.cipher_type == aead) {
  3246. /* Initialize the AES-GCM/CCM explicit IV to a zero. */
  3247. #ifdef WOLFSSL_DTLS
  3248. if (scr_copy) {
  3249. XMEMCPY(ssl->keys.aead_exp_IV,
  3250. keys->aead_exp_IV, AEAD_MAX_EXP_SZ);
  3251. }
  3252. #endif
  3253. XMEMSET(keys->aead_exp_IV, 0, AEAD_MAX_EXP_SZ);
  3254. }
  3255. #endif /* HAVE_AEAD */
  3256. return 0;
  3257. }
  3258. #endif /* WOLFSSL_MULTICAST */
  3259. if (ssl->specs.cipher_type != aead) {
  3260. sz = ssl->specs.hash_size;
  3261. if (side & PROVISION_CLIENT) {
  3262. #ifndef WOLFSSL_AEAD_ONLY
  3263. #ifdef WOLFSSL_DTLS
  3264. if (scr_copy)
  3265. XMEMCPY(ssl->keys.client_write_MAC_secret,
  3266. keys->client_write_MAC_secret, sz);
  3267. #endif
  3268. XMEMCPY(keys->client_write_MAC_secret,&keyData[i], sz);
  3269. #endif
  3270. i += sz;
  3271. }
  3272. if (side & PROVISION_SERVER) {
  3273. #ifndef WOLFSSL_AEAD_ONLY
  3274. #ifdef WOLFSSL_DTLS
  3275. if (scr_copy)
  3276. XMEMCPY(ssl->keys.server_write_MAC_secret,
  3277. keys->server_write_MAC_secret, sz);
  3278. #endif
  3279. XMEMCPY(keys->server_write_MAC_secret,&keyData[i], sz);
  3280. #endif
  3281. i += sz;
  3282. }
  3283. }
  3284. sz = ssl->specs.key_size;
  3285. if (side & PROVISION_CLIENT) {
  3286. #ifdef WOLFSSL_DTLS
  3287. if (scr_copy)
  3288. XMEMCPY(ssl->keys.client_write_key,
  3289. keys->client_write_key, sz);
  3290. #endif
  3291. XMEMCPY(keys->client_write_key, &keyData[i], sz);
  3292. i += sz;
  3293. }
  3294. if (side & PROVISION_SERVER) {
  3295. #ifdef WOLFSSL_DTLS
  3296. if (scr_copy)
  3297. XMEMCPY(ssl->keys.server_write_key,
  3298. keys->server_write_key, sz);
  3299. #endif
  3300. XMEMCPY(keys->server_write_key, &keyData[i], sz);
  3301. i += sz;
  3302. }
  3303. sz = ssl->specs.iv_size;
  3304. if (side & PROVISION_CLIENT) {
  3305. #ifdef WOLFSSL_DTLS
  3306. if (scr_copy)
  3307. XMEMCPY(ssl->keys.client_write_IV,
  3308. keys->client_write_IV, sz);
  3309. #endif
  3310. XMEMCPY(keys->client_write_IV, &keyData[i], sz);
  3311. i += sz;
  3312. }
  3313. if (side & PROVISION_SERVER) {
  3314. #ifdef WOLFSSL_DTLS
  3315. if (scr_copy)
  3316. XMEMCPY(ssl->keys.server_write_IV,
  3317. keys->server_write_IV, sz);
  3318. #endif
  3319. XMEMCPY(keys->server_write_IV, &keyData[i], sz);
  3320. }
  3321. #ifdef HAVE_AEAD
  3322. if (ssl->specs.cipher_type == aead) {
  3323. /* Initialize the AES-GCM/CCM explicit IV to a zero. */
  3324. #ifdef WOLFSSL_DTLS
  3325. if (scr_copy)
  3326. XMEMMOVE(ssl->keys.aead_exp_IV,
  3327. keys->aead_exp_IV, AEAD_MAX_EXP_SZ);
  3328. #endif
  3329. XMEMSET(keys->aead_exp_IV, 0, AEAD_MAX_EXP_SZ);
  3330. }
  3331. #endif
  3332. return 0;
  3333. }
  3334. #ifndef NO_OLD_TLS
  3335. int DeriveKeys(WOLFSSL* ssl)
  3336. {
  3337. int length = 2 * ssl->specs.hash_size +
  3338. 2 * ssl->specs.key_size +
  3339. 2 * ssl->specs.iv_size;
  3340. int rounds = (length + WC_MD5_DIGEST_SIZE - 1 ) / WC_MD5_DIGEST_SIZE;
  3341. int ret = 0;
  3342. #ifdef WOLFSSL_SMALL_STACK
  3343. byte* shaOutput;
  3344. byte* md5Input;
  3345. byte* shaInput;
  3346. byte* keyData;
  3347. wc_Md5* md5;
  3348. wc_Sha* sha;
  3349. #else
  3350. byte shaOutput[WC_SHA_DIGEST_SIZE];
  3351. byte md5Input[SECRET_LEN + WC_SHA_DIGEST_SIZE];
  3352. byte shaInput[KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN];
  3353. byte keyData[KEY_PREFIX * WC_MD5_DIGEST_SIZE];
  3354. wc_Md5 md5[1];
  3355. wc_Sha sha[1];
  3356. #endif
  3357. #ifdef WOLFSSL_SMALL_STACK
  3358. shaOutput = (byte*)XMALLOC(WC_SHA_DIGEST_SIZE,
  3359. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3360. md5Input = (byte*)XMALLOC(SECRET_LEN + WC_SHA_DIGEST_SIZE,
  3361. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3362. shaInput = (byte*)XMALLOC(KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN,
  3363. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3364. keyData = (byte*)XMALLOC(KEY_PREFIX * WC_MD5_DIGEST_SIZE,
  3365. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3366. md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3367. sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3368. if (shaOutput == NULL || md5Input == NULL || shaInput == NULL ||
  3369. keyData == NULL || md5 == NULL || sha == NULL) {
  3370. if (shaOutput) XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3371. if (md5Input) XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3372. if (shaInput) XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3373. if (keyData) XFREE(keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3374. if (md5) XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3375. if (sha) XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3376. return MEMORY_E;
  3377. }
  3378. #endif
  3379. XMEMSET(shaOutput, 0, WC_SHA_DIGEST_SIZE);
  3380. ret = wc_InitMd5(md5);
  3381. if (ret == 0) {
  3382. ret = wc_InitSha(sha);
  3383. }
  3384. if (ret == 0) {
  3385. int i;
  3386. XMEMCPY(md5Input, ssl->arrays->masterSecret, SECRET_LEN);
  3387. for (i = 0; i < rounds; ++i) {
  3388. int j = i + 1;
  3389. int idx = j;
  3390. if (!SetPrefix(shaInput, i)) {
  3391. ret = PREFIX_ERROR;
  3392. break;
  3393. }
  3394. XMEMCPY(shaInput + idx, ssl->arrays->masterSecret, SECRET_LEN);
  3395. idx += SECRET_LEN;
  3396. XMEMCPY(shaInput + idx, ssl->arrays->serverRandom, RAN_LEN);
  3397. idx += RAN_LEN;
  3398. XMEMCPY(shaInput + idx, ssl->arrays->clientRandom, RAN_LEN);
  3399. if (ret == 0) {
  3400. ret = wc_ShaUpdate(sha, shaInput,
  3401. (KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN) - KEY_PREFIX + j);
  3402. }
  3403. if (ret == 0) {
  3404. ret = wc_ShaFinal(sha, shaOutput);
  3405. }
  3406. XMEMCPY(md5Input + SECRET_LEN, shaOutput, WC_SHA_DIGEST_SIZE);
  3407. if (ret == 0) {
  3408. ret = wc_Md5Update(md5, md5Input, SECRET_LEN + WC_SHA_DIGEST_SIZE);
  3409. }
  3410. if (ret == 0) {
  3411. ret = wc_Md5Final(md5, keyData + i * WC_MD5_DIGEST_SIZE);
  3412. }
  3413. }
  3414. if (ret == 0)
  3415. ret = StoreKeys(ssl, keyData, PROVISION_CLIENT_SERVER);
  3416. }
  3417. #ifdef WOLFSSL_SMALL_STACK
  3418. XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3419. XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3420. XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3421. XFREE(keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3422. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3423. XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3424. #endif
  3425. return ret;
  3426. }
  3427. static int CleanPreMaster(WOLFSSL* ssl)
  3428. {
  3429. int i, ret, sz = ssl->arrays->preMasterSz;
  3430. for (i = 0; i < sz; i++)
  3431. ssl->arrays->preMasterSecret[i] = 0;
  3432. ret = wc_RNG_GenerateBlock(ssl->rng, ssl->arrays->preMasterSecret, sz);
  3433. if (ret != 0)
  3434. return ret;
  3435. for (i = 0; i < sz; i++)
  3436. ssl->arrays->preMasterSecret[i] = 0;
  3437. XFREE(ssl->arrays->preMasterSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  3438. ssl->arrays->preMasterSecret = NULL;
  3439. ssl->arrays->preMasterSz = 0;
  3440. return 0;
  3441. }
  3442. /* Create and store the master secret see page 32, 6.1 */
  3443. static int MakeSslMasterSecret(WOLFSSL* ssl)
  3444. {
  3445. int i, ret;
  3446. word32 idx;
  3447. word32 pmsSz = ssl->arrays->preMasterSz;
  3448. #ifdef WOLFSSL_SMALL_STACK
  3449. byte* shaOutput;
  3450. byte* md5Input;
  3451. byte* shaInput;
  3452. wc_Md5* md5;
  3453. wc_Sha* sha;
  3454. #else
  3455. byte shaOutput[WC_SHA_DIGEST_SIZE];
  3456. byte md5Input[ENCRYPT_LEN + WC_SHA_DIGEST_SIZE];
  3457. byte shaInput[PREFIX + ENCRYPT_LEN + 2 * RAN_LEN];
  3458. wc_Md5 md5[1];
  3459. wc_Sha sha[1];
  3460. #endif
  3461. if (ssl->arrays->preMasterSecret == NULL) {
  3462. return BAD_FUNC_ARG;
  3463. }
  3464. #ifdef SHOW_SECRETS
  3465. {
  3466. word32 j;
  3467. printf("pre master secret: ");
  3468. for (j = 0; j < pmsSz; j++)
  3469. printf("%02x", ssl->arrays->preMasterSecret[j]);
  3470. printf("\n");
  3471. }
  3472. #endif
  3473. #ifdef WOLFSSL_SMALL_STACK
  3474. shaOutput = (byte*)XMALLOC(WC_SHA_DIGEST_SIZE,
  3475. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3476. md5Input = (byte*)XMALLOC(ENCRYPT_LEN + WC_SHA_DIGEST_SIZE,
  3477. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3478. shaInput = (byte*)XMALLOC(PREFIX + ENCRYPT_LEN + 2 * RAN_LEN,
  3479. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3480. md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3481. sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3482. if (shaOutput == NULL || md5Input == NULL || shaInput == NULL ||
  3483. md5 == NULL || sha == NULL) {
  3484. if (shaOutput) XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3485. if (md5Input) XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3486. if (shaInput) XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3487. if (md5) XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3488. if (sha) XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3489. return MEMORY_E;
  3490. }
  3491. #endif
  3492. XMEMSET(shaOutput, 0, WC_SHA_DIGEST_SIZE);
  3493. ret = wc_InitMd5(md5);
  3494. if (ret == 0) {
  3495. ret = wc_InitSha(sha);
  3496. }
  3497. if (ret == 0) {
  3498. XMEMCPY(md5Input, ssl->arrays->preMasterSecret, pmsSz);
  3499. for (i = 0; i < MASTER_ROUNDS; ++i) {
  3500. byte prefix[KEY_PREFIX]; /* only need PREFIX bytes but static */
  3501. if (!SetPrefix(prefix, i)) { /* analysis thinks will overrun */
  3502. ret = PREFIX_ERROR;
  3503. break;
  3504. }
  3505. idx = 0;
  3506. XMEMCPY(shaInput, prefix, i + 1);
  3507. idx += i + 1;
  3508. XMEMCPY(shaInput + idx, ssl->arrays->preMasterSecret, pmsSz);
  3509. idx += pmsSz;
  3510. XMEMCPY(shaInput + idx, ssl->arrays->clientRandom, RAN_LEN);
  3511. idx += RAN_LEN;
  3512. XMEMCPY(shaInput + idx, ssl->arrays->serverRandom, RAN_LEN);
  3513. idx += RAN_LEN;
  3514. if (ret == 0) {
  3515. ret = wc_ShaUpdate(sha, shaInput, idx);
  3516. }
  3517. if (ret == 0) {
  3518. ret = wc_ShaFinal(sha, shaOutput);
  3519. }
  3520. idx = pmsSz; /* preSz */
  3521. XMEMCPY(md5Input + idx, shaOutput, WC_SHA_DIGEST_SIZE);
  3522. idx += WC_SHA_DIGEST_SIZE;
  3523. if (ret == 0) {
  3524. ret = wc_Md5Update(md5, md5Input, idx);
  3525. }
  3526. if (ret == 0) {
  3527. ret = wc_Md5Final(md5,
  3528. &ssl->arrays->masterSecret[i * WC_MD5_DIGEST_SIZE]);
  3529. }
  3530. }
  3531. #ifdef SHOW_SECRETS
  3532. {
  3533. word32 j;
  3534. printf("master secret: ");
  3535. for (j = 0; j < SECRET_LEN; j++)
  3536. printf("%02x", ssl->arrays->masterSecret[j]);
  3537. printf("\n");
  3538. }
  3539. #endif
  3540. if (ret == 0)
  3541. ret = DeriveKeys(ssl);
  3542. }
  3543. #ifdef WOLFSSL_SMALL_STACK
  3544. XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3545. XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3546. XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3547. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3548. XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3549. #endif
  3550. if (ret == 0)
  3551. ret = CleanPreMaster(ssl);
  3552. else
  3553. CleanPreMaster(ssl);
  3554. return ret;
  3555. }
  3556. #endif
  3557. /* Master wrapper, doesn't use SSL stack space in TLS mode */
  3558. int MakeMasterSecret(WOLFSSL* ssl)
  3559. {
  3560. /* append secret to premaster : premaster | SerSi | CliSi */
  3561. #ifndef NO_OLD_TLS
  3562. if (ssl->options.tls) return MakeTlsMasterSecret(ssl);
  3563. return MakeSslMasterSecret(ssl);
  3564. #elif !defined(WOLFSSL_NO_TLS12) && !defined(NO_TLS)
  3565. return MakeTlsMasterSecret(ssl);
  3566. #else
  3567. (void)ssl;
  3568. return 0;
  3569. #endif
  3570. }
  3571. #endif /* WOLFCRYPT_ONLY */