keys.c 149 KB

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