keys.c 134 KB

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