rtw_security.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #define _RTW_SECURITY_C_
  16. #include <drv_types.h>
  17. static const char *_security_type_str[] = {
  18. "N/A",
  19. "WEP40",
  20. "TKIP",
  21. "TKIP_WM",
  22. "AES",
  23. "WEP104",
  24. "SMS4",
  25. "WEP_WPA",
  26. "BIP",
  27. };
  28. const char *security_type_str(u8 value)
  29. {
  30. #ifdef CONFIG_IEEE80211W
  31. if (value <= _BIP_)
  32. #else
  33. if (value <= _WEP_WPA_MIXED_)
  34. #endif
  35. return _security_type_str[value];
  36. return NULL;
  37. }
  38. #ifdef DBG_SW_SEC_CNT
  39. #define WEP_SW_ENC_CNT_INC(sec, ra) do {\
  40. if (is_broadcast_mac_addr(ra)) \
  41. sec->wep_sw_enc_cnt_bc++; \
  42. else if (is_multicast_mac_addr(ra)) \
  43. sec->wep_sw_enc_cnt_mc++; \
  44. else \
  45. sec->wep_sw_enc_cnt_uc++; \
  46. } while (0)
  47. #define WEP_SW_DEC_CNT_INC(sec, ra) do {\
  48. if (is_broadcast_mac_addr(ra)) \
  49. sec->wep_sw_dec_cnt_bc++; \
  50. else if (is_multicast_mac_addr(ra)) \
  51. sec->wep_sw_dec_cnt_mc++; \
  52. else \
  53. sec->wep_sw_dec_cnt_uc++; \
  54. } while (0)
  55. #define TKIP_SW_ENC_CNT_INC(sec, ra) do {\
  56. if (is_broadcast_mac_addr(ra)) \
  57. sec->tkip_sw_enc_cnt_bc++; \
  58. else if (is_multicast_mac_addr(ra)) \
  59. sec->tkip_sw_enc_cnt_mc++; \
  60. else \
  61. sec->tkip_sw_enc_cnt_uc++; \
  62. } while (0)
  63. #define TKIP_SW_DEC_CNT_INC(sec, ra) do {\
  64. if (is_broadcast_mac_addr(ra)) \
  65. sec->tkip_sw_dec_cnt_bc++; \
  66. else if (is_multicast_mac_addr(ra)) \
  67. sec->tkip_sw_dec_cnt_mc++; \
  68. else \
  69. sec->tkip_sw_dec_cnt_uc++; \
  70. } while (0)
  71. #define AES_SW_ENC_CNT_INC(sec, ra) do {\
  72. if (is_broadcast_mac_addr(ra)) \
  73. sec->aes_sw_enc_cnt_bc++; \
  74. else if (is_multicast_mac_addr(ra)) \
  75. sec->aes_sw_enc_cnt_mc++; \
  76. else \
  77. sec->aes_sw_enc_cnt_uc++; \
  78. } while (0)
  79. #define AES_SW_DEC_CNT_INC(sec, ra) do {\
  80. if (is_broadcast_mac_addr(ra)) \
  81. sec->aes_sw_dec_cnt_bc++; \
  82. else if (is_multicast_mac_addr(ra)) \
  83. sec->aes_sw_dec_cnt_mc++; \
  84. else \
  85. sec->aes_sw_dec_cnt_uc++; \
  86. } while (0)
  87. #else
  88. #define WEP_SW_ENC_CNT_INC(sec, ra)
  89. #define WEP_SW_DEC_CNT_INC(sec, ra)
  90. #define TKIP_SW_ENC_CNT_INC(sec, ra)
  91. #define TKIP_SW_DEC_CNT_INC(sec, ra)
  92. #define AES_SW_ENC_CNT_INC(sec, ra)
  93. #define AES_SW_DEC_CNT_INC(sec, ra)
  94. #endif /* DBG_SW_SEC_CNT */
  95. /* *****WEP related***** */
  96. #define CRC32_POLY 0x04c11db7
  97. struct arc4context {
  98. u32 x;
  99. u32 y;
  100. u8 state[256];
  101. };
  102. static void arcfour_init(struct arc4context *parc4ctx, u8 *key, u32 key_len)
  103. {
  104. u32 t, u;
  105. u32 keyindex;
  106. u32 stateindex;
  107. u8 *state;
  108. u32 counter;
  109. state = parc4ctx->state;
  110. parc4ctx->x = 0;
  111. parc4ctx->y = 0;
  112. for (counter = 0; counter < 256; counter++)
  113. state[counter] = (u8)counter;
  114. keyindex = 0;
  115. stateindex = 0;
  116. for (counter = 0; counter < 256; counter++) {
  117. t = state[counter];
  118. stateindex = (stateindex + key[keyindex] + t) & 0xff;
  119. u = state[stateindex];
  120. state[stateindex] = (u8)t;
  121. state[counter] = (u8)u;
  122. if (++keyindex >= key_len)
  123. keyindex = 0;
  124. }
  125. }
  126. static u32 arcfour_byte(struct arc4context *parc4ctx)
  127. {
  128. u32 x;
  129. u32 y;
  130. u32 sx, sy;
  131. u8 *state;
  132. state = parc4ctx->state;
  133. x = (parc4ctx->x + 1) & 0xff;
  134. sx = state[x];
  135. y = (sx + parc4ctx->y) & 0xff;
  136. sy = state[y];
  137. parc4ctx->x = x;
  138. parc4ctx->y = y;
  139. state[y] = (u8)sx;
  140. state[x] = (u8)sy;
  141. return state[(sx + sy) & 0xff];
  142. }
  143. static void arcfour_encrypt(struct arc4context *parc4ctx,
  144. u8 *dest,
  145. u8 *src,
  146. u32 len)
  147. {
  148. u32 i;
  149. for (i = 0; i < len; i++)
  150. dest[i] = src[i] ^ (unsigned char)arcfour_byte(parc4ctx);
  151. }
  152. static sint bcrc32initialized = 0;
  153. static u32 crc32_table[256];
  154. static u8 crc32_reverseBit(u8 data)
  155. {
  156. return (u8)((data << 7) & 0x80) | ((data << 5) & 0x40) | ((data << 3) & 0x20) | ((data << 1) & 0x10) | ((data >> 1) & 0x08) | ((data >> 3) & 0x04) | ((data >> 5) & 0x02) | ((
  157. data >> 7) & 0x01) ;
  158. }
  159. static void crc32_init(void)
  160. {
  161. if (bcrc32initialized == 1)
  162. goto exit;
  163. else {
  164. sint i, j;
  165. u32 c;
  166. u8 *p = (u8 *)&c, *p1;
  167. u8 k;
  168. c = 0x12340000;
  169. for (i = 0; i < 256; ++i) {
  170. k = crc32_reverseBit((u8)i);
  171. for (c = ((u32)k) << 24, j = 8; j > 0; --j)
  172. c = c & 0x80000000 ? (c << 1) ^ CRC32_POLY : (c << 1);
  173. p1 = (u8 *)&crc32_table[i];
  174. p1[0] = crc32_reverseBit(p[3]);
  175. p1[1] = crc32_reverseBit(p[2]);
  176. p1[2] = crc32_reverseBit(p[1]);
  177. p1[3] = crc32_reverseBit(p[0]);
  178. }
  179. bcrc32initialized = 1;
  180. }
  181. exit:
  182. return;
  183. }
  184. static u32 getcrc32(u8 *buf, sint len)
  185. {
  186. u8 *p;
  187. u32 crc;
  188. if (bcrc32initialized == 0)
  189. crc32_init();
  190. crc = 0xffffffff; /* preload shift register, per CRC-32 spec */
  191. for (p = buf; len > 0; ++p, --len)
  192. crc = crc32_table[(crc ^ *p) & 0xff] ^ (crc >> 8);
  193. return ~crc; /* transmit complement, per CRC-32 spec */
  194. }
  195. /*
  196. Need to consider the fragment situation
  197. */
  198. void rtw_wep_encrypt(_adapter *padapter, u8 *pxmitframe)
  199. {
  200. /* exclude ICV */
  201. unsigned char crc[4];
  202. struct arc4context mycontext;
  203. sint curfragnum, length;
  204. u32 keylength;
  205. u8 *pframe, *payload, *iv; /* ,*wepkey */
  206. u8 wepkey[16];
  207. u8 hw_hdr_offset = 0;
  208. struct pkt_attrib *pattrib = &((struct xmit_frame *)pxmitframe)->attrib;
  209. struct security_priv *psecuritypriv = &padapter->securitypriv;
  210. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  211. if (((struct xmit_frame *)pxmitframe)->buf_addr == NULL)
  212. return;
  213. #ifdef CONFIG_USB_TX_AGGREGATION
  214. hw_hdr_offset = TXDESC_SIZE +
  215. (((struct xmit_frame *)pxmitframe)->pkt_offset * PACKET_OFFSET_SZ);
  216. #else
  217. #ifdef CONFIG_TX_EARLY_MODE
  218. hw_hdr_offset = TXDESC_OFFSET + EARLY_MODE_INFO_SIZE;
  219. #else
  220. hw_hdr_offset = TXDESC_OFFSET;
  221. #endif
  222. #endif
  223. pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + hw_hdr_offset;
  224. /* start to encrypt each fragment */
  225. if ((pattrib->encrypt == _WEP40_) || (pattrib->encrypt == _WEP104_)) {
  226. keylength = psecuritypriv->dot11DefKeylen[psecuritypriv->dot11PrivacyKeyIndex];
  227. for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
  228. iv = pframe + pattrib->hdrlen;
  229. _rtw_memcpy(&wepkey[0], iv, 3);
  230. _rtw_memcpy(&wepkey[3], &psecuritypriv->dot11DefKey[psecuritypriv->dot11PrivacyKeyIndex].skey[0], keylength);
  231. payload = pframe + pattrib->iv_len + pattrib->hdrlen;
  232. if ((curfragnum + 1) == pattrib->nr_frags) {
  233. /* the last fragment */
  234. length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
  235. *((u32 *)crc) = cpu_to_le32(getcrc32(payload, length));
  236. arcfour_init(&mycontext, wepkey, 3 + keylength);
  237. arcfour_encrypt(&mycontext, payload, payload, length);
  238. arcfour_encrypt(&mycontext, payload + length, crc, 4);
  239. } else {
  240. length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len ;
  241. *((u32 *)crc) = cpu_to_le32(getcrc32(payload, length));
  242. arcfour_init(&mycontext, wepkey, 3 + keylength);
  243. arcfour_encrypt(&mycontext, payload, payload, length);
  244. arcfour_encrypt(&mycontext, payload + length, crc, 4);
  245. pframe += pxmitpriv->frag_len;
  246. pframe = (u8 *)RND4((SIZE_PTR)(pframe));
  247. }
  248. }
  249. WEP_SW_ENC_CNT_INC(psecuritypriv, pattrib->ra);
  250. }
  251. }
  252. void rtw_wep_decrypt(_adapter *padapter, u8 *precvframe)
  253. {
  254. /* exclude ICV */
  255. u8 crc[4];
  256. struct arc4context mycontext;
  257. sint length;
  258. u32 keylength;
  259. u8 *pframe, *payload, *iv, wepkey[16];
  260. u8 keyindex;
  261. struct rx_pkt_attrib *prxattrib = &(((union recv_frame *)precvframe)->u.hdr.attrib);
  262. struct security_priv *psecuritypriv = &padapter->securitypriv;
  263. pframe = (unsigned char *)((union recv_frame *)precvframe)->u.hdr.rx_data;
  264. /* start to decrypt recvframe */
  265. if ((prxattrib->encrypt == _WEP40_) || (prxattrib->encrypt == _WEP104_)) {
  266. iv = pframe + prxattrib->hdrlen;
  267. /* keyindex=(iv[3]&0x3); */
  268. keyindex = prxattrib->key_index;
  269. keylength = psecuritypriv->dot11DefKeylen[keyindex];
  270. _rtw_memcpy(&wepkey[0], iv, 3);
  271. /* _rtw_memcpy(&wepkey[3], &psecuritypriv->dot11DefKey[psecuritypriv->dot11PrivacyKeyIndex].skey[0],keylength); */
  272. _rtw_memcpy(&wepkey[3], &psecuritypriv->dot11DefKey[keyindex].skey[0], keylength);
  273. length = ((union recv_frame *)precvframe)->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len;
  274. payload = pframe + prxattrib->iv_len + prxattrib->hdrlen;
  275. /* decrypt payload include icv */
  276. arcfour_init(&mycontext, wepkey, 3 + keylength);
  277. arcfour_encrypt(&mycontext, payload, payload, length);
  278. /* calculate icv and compare the icv */
  279. *((u32 *)crc) = le32_to_cpu(getcrc32(payload, length - 4));
  280. WEP_SW_DEC_CNT_INC(psecuritypriv, prxattrib->ra);
  281. }
  282. return;
  283. }
  284. /* 3 =====TKIP related===== */
  285. static u32 secmicgetuint32(u8 *p)
  286. /* Convert from Byte[] to Us4Byte32 in a portable way */
  287. {
  288. s32 i;
  289. u32 res = 0;
  290. for (i = 0; i < 4; i++)
  291. res |= ((u32)(*p++)) << (8 * i);
  292. return res;
  293. }
  294. static void secmicputuint32(u8 *p, u32 val)
  295. /* Convert from Us4Byte32 to Byte[] in a portable way */
  296. {
  297. long i;
  298. for (i = 0; i < 4; i++) {
  299. *p++ = (u8)(val & 0xff);
  300. val >>= 8;
  301. }
  302. }
  303. static void secmicclear(struct mic_data *pmicdata)
  304. {
  305. /* Reset the state to the empty message. */
  306. pmicdata->L = pmicdata->K0;
  307. pmicdata->R = pmicdata->K1;
  308. pmicdata->nBytesInM = 0;
  309. pmicdata->M = 0;
  310. }
  311. void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key)
  312. {
  313. /* Set the key */
  314. pmicdata->K0 = secmicgetuint32(key);
  315. pmicdata->K1 = secmicgetuint32(key + 4);
  316. /* and reset the message */
  317. secmicclear(pmicdata);
  318. }
  319. void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b)
  320. {
  321. /* Append the byte to our word-sized buffer */
  322. pmicdata->M |= ((unsigned long)b) << (8 * pmicdata->nBytesInM);
  323. pmicdata->nBytesInM++;
  324. /* Process the word if it is full. */
  325. if (pmicdata->nBytesInM >= 4) {
  326. pmicdata->L ^= pmicdata->M;
  327. pmicdata->R ^= ROL32(pmicdata->L, 17);
  328. pmicdata->L += pmicdata->R;
  329. pmicdata->R ^= ((pmicdata->L & 0xff00ff00) >> 8) | ((pmicdata->L & 0x00ff00ff) << 8);
  330. pmicdata->L += pmicdata->R;
  331. pmicdata->R ^= ROL32(pmicdata->L, 3);
  332. pmicdata->L += pmicdata->R;
  333. pmicdata->R ^= ROR32(pmicdata->L, 2);
  334. pmicdata->L += pmicdata->R;
  335. /* Clear the buffer */
  336. pmicdata->M = 0;
  337. pmicdata->nBytesInM = 0;
  338. }
  339. }
  340. void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nbytes)
  341. {
  342. /* This is simple */
  343. while (nbytes > 0) {
  344. rtw_secmicappendbyte(pmicdata, *src++);
  345. nbytes--;
  346. }
  347. }
  348. void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst)
  349. {
  350. /* Append the minimum padding */
  351. rtw_secmicappendbyte(pmicdata, 0x5a);
  352. rtw_secmicappendbyte(pmicdata, 0);
  353. rtw_secmicappendbyte(pmicdata, 0);
  354. rtw_secmicappendbyte(pmicdata, 0);
  355. rtw_secmicappendbyte(pmicdata, 0);
  356. /* and then zeroes until the length is a multiple of 4 */
  357. while (pmicdata->nBytesInM != 0)
  358. rtw_secmicappendbyte(pmicdata, 0);
  359. /* The appendByte function has already computed the result. */
  360. secmicputuint32(dst, pmicdata->L);
  361. secmicputuint32(dst + 4, pmicdata->R);
  362. /* Reset to the empty message. */
  363. secmicclear(pmicdata);
  364. }
  365. void rtw_seccalctkipmic(u8 *key, u8 *header, u8 *data, u32 data_len, u8 *mic_code, u8 pri)
  366. {
  367. struct mic_data micdata;
  368. u8 priority[4] = {0x0, 0x0, 0x0, 0x0};
  369. rtw_secmicsetkey(&micdata, key);
  370. priority[0] = pri;
  371. /* Michael MIC pseudo header: DA, SA, 3 x 0, Priority */
  372. if (header[1] & 1) { /* ToDS==1 */
  373. rtw_secmicappend(&micdata, &header[16], 6); /* DA */
  374. if (header[1] & 2) /* From Ds==1 */
  375. rtw_secmicappend(&micdata, &header[24], 6);
  376. else
  377. rtw_secmicappend(&micdata, &header[10], 6);
  378. } else { /* ToDS==0 */
  379. rtw_secmicappend(&micdata, &header[4], 6); /* DA */
  380. if (header[1] & 2) /* From Ds==1 */
  381. rtw_secmicappend(&micdata, &header[16], 6);
  382. else
  383. rtw_secmicappend(&micdata, &header[10], 6);
  384. }
  385. rtw_secmicappend(&micdata, &priority[0], 4);
  386. rtw_secmicappend(&micdata, data, data_len);
  387. rtw_secgetmic(&micdata, mic_code);
  388. }
  389. /* macros for extraction/creation of unsigned char/unsigned short values */
  390. #define RotR1(v16) ((((v16) >> 1) & 0x7FFF) ^ (((v16) & 1) << 15))
  391. #define Lo8(v16) ((u8)((v16) & 0x00FF))
  392. #define Hi8(v16) ((u8)(((v16) >> 8) & 0x00FF))
  393. #define Lo16(v32) ((u16)((v32) & 0xFFFF))
  394. #define Hi16(v32) ((u16)(((v32) >> 16) & 0xFFFF))
  395. #define Mk16(hi, lo) ((lo) ^ (((u16)(hi)) << 8))
  396. /* select the Nth 16-bit word of the temporal key unsigned char array TK[] */
  397. #define TK16(N) Mk16(tk[2*(N)+1], tk[2*(N)])
  398. /* S-box lookup: 16 bits --> 16 bits */
  399. #define _S_(v16) (Sbox1[0][Lo8(v16)] ^ Sbox1[1][Hi8(v16)])
  400. /* fixed algorithm "parameters" */
  401. #define PHASE1_LOOP_CNT 8 /* this needs to be "big enough" */
  402. #define TA_SIZE 6 /* 48-bit transmitter address */
  403. #define TK_SIZE 16 /* 128-bit temporal key */
  404. #define P1K_SIZE 10 /* 80-bit Phase1 key */
  405. #define RC4_KEY_SIZE 16 /* 128-bit RC4KEY (104 bits unknown) */
  406. /* 2-unsigned char by 2-unsigned char subset of the full AES S-box table */
  407. static const unsigned short Sbox1[2][256] = /* Sbox for hash (can be in ROM) */
  408. { {
  409. 0xC6A5, 0xF884, 0xEE99, 0xF68D, 0xFF0D, 0xD6BD, 0xDEB1, 0x9154,
  410. 0x6050, 0x0203, 0xCEA9, 0x567D, 0xE719, 0xB562, 0x4DE6, 0xEC9A,
  411. 0x8F45, 0x1F9D, 0x8940, 0xFA87, 0xEF15, 0xB2EB, 0x8EC9, 0xFB0B,
  412. 0x41EC, 0xB367, 0x5FFD, 0x45EA, 0x23BF, 0x53F7, 0xE496, 0x9B5B,
  413. 0x75C2, 0xE11C, 0x3DAE, 0x4C6A, 0x6C5A, 0x7E41, 0xF502, 0x834F,
  414. 0x685C, 0x51F4, 0xD134, 0xF908, 0xE293, 0xAB73, 0x6253, 0x2A3F,
  415. 0x080C, 0x9552, 0x4665, 0x9D5E, 0x3028, 0x37A1, 0x0A0F, 0x2FB5,
  416. 0x0E09, 0x2436, 0x1B9B, 0xDF3D, 0xCD26, 0x4E69, 0x7FCD, 0xEA9F,
  417. 0x121B, 0x1D9E, 0x5874, 0x342E, 0x362D, 0xDCB2, 0xB4EE, 0x5BFB,
  418. 0xA4F6, 0x764D, 0xB761, 0x7DCE, 0x527B, 0xDD3E, 0x5E71, 0x1397,
  419. 0xA6F5, 0xB968, 0x0000, 0xC12C, 0x4060, 0xE31F, 0x79C8, 0xB6ED,
  420. 0xD4BE, 0x8D46, 0x67D9, 0x724B, 0x94DE, 0x98D4, 0xB0E8, 0x854A,
  421. 0xBB6B, 0xC52A, 0x4FE5, 0xED16, 0x86C5, 0x9AD7, 0x6655, 0x1194,
  422. 0x8ACF, 0xE910, 0x0406, 0xFE81, 0xA0F0, 0x7844, 0x25BA, 0x4BE3,
  423. 0xA2F3, 0x5DFE, 0x80C0, 0x058A, 0x3FAD, 0x21BC, 0x7048, 0xF104,
  424. 0x63DF, 0x77C1, 0xAF75, 0x4263, 0x2030, 0xE51A, 0xFD0E, 0xBF6D,
  425. 0x814C, 0x1814, 0x2635, 0xC32F, 0xBEE1, 0x35A2, 0x88CC, 0x2E39,
  426. 0x9357, 0x55F2, 0xFC82, 0x7A47, 0xC8AC, 0xBAE7, 0x322B, 0xE695,
  427. 0xC0A0, 0x1998, 0x9ED1, 0xA37F, 0x4466, 0x547E, 0x3BAB, 0x0B83,
  428. 0x8CCA, 0xC729, 0x6BD3, 0x283C, 0xA779, 0xBCE2, 0x161D, 0xAD76,
  429. 0xDB3B, 0x6456, 0x744E, 0x141E, 0x92DB, 0x0C0A, 0x486C, 0xB8E4,
  430. 0x9F5D, 0xBD6E, 0x43EF, 0xC4A6, 0x39A8, 0x31A4, 0xD337, 0xF28B,
  431. 0xD532, 0x8B43, 0x6E59, 0xDAB7, 0x018C, 0xB164, 0x9CD2, 0x49E0,
  432. 0xD8B4, 0xACFA, 0xF307, 0xCF25, 0xCAAF, 0xF48E, 0x47E9, 0x1018,
  433. 0x6FD5, 0xF088, 0x4A6F, 0x5C72, 0x3824, 0x57F1, 0x73C7, 0x9751,
  434. 0xCB23, 0xA17C, 0xE89C, 0x3E21, 0x96DD, 0x61DC, 0x0D86, 0x0F85,
  435. 0xE090, 0x7C42, 0x71C4, 0xCCAA, 0x90D8, 0x0605, 0xF701, 0x1C12,
  436. 0xC2A3, 0x6A5F, 0xAEF9, 0x69D0, 0x1791, 0x9958, 0x3A27, 0x27B9,
  437. 0xD938, 0xEB13, 0x2BB3, 0x2233, 0xD2BB, 0xA970, 0x0789, 0x33A7,
  438. 0x2DB6, 0x3C22, 0x1592, 0xC920, 0x8749, 0xAAFF, 0x5078, 0xA57A,
  439. 0x038F, 0x59F8, 0x0980, 0x1A17, 0x65DA, 0xD731, 0x84C6, 0xD0B8,
  440. 0x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A,
  441. },
  442. { /* second half of table is unsigned char-reversed version of first! */
  443. 0xA5C6, 0x84F8, 0x99EE, 0x8DF6, 0x0DFF, 0xBDD6, 0xB1DE, 0x5491,
  444. 0x5060, 0x0302, 0xA9CE, 0x7D56, 0x19E7, 0x62B5, 0xE64D, 0x9AEC,
  445. 0x458F, 0x9D1F, 0x4089, 0x87FA, 0x15EF, 0xEBB2, 0xC98E, 0x0BFB,
  446. 0xEC41, 0x67B3, 0xFD5F, 0xEA45, 0xBF23, 0xF753, 0x96E4, 0x5B9B,
  447. 0xC275, 0x1CE1, 0xAE3D, 0x6A4C, 0x5A6C, 0x417E, 0x02F5, 0x4F83,
  448. 0x5C68, 0xF451, 0x34D1, 0x08F9, 0x93E2, 0x73AB, 0x5362, 0x3F2A,
  449. 0x0C08, 0x5295, 0x6546, 0x5E9D, 0x2830, 0xA137, 0x0F0A, 0xB52F,
  450. 0x090E, 0x3624, 0x9B1B, 0x3DDF, 0x26CD, 0x694E, 0xCD7F, 0x9FEA,
  451. 0x1B12, 0x9E1D, 0x7458, 0x2E34, 0x2D36, 0xB2DC, 0xEEB4, 0xFB5B,
  452. 0xF6A4, 0x4D76, 0x61B7, 0xCE7D, 0x7B52, 0x3EDD, 0x715E, 0x9713,
  453. 0xF5A6, 0x68B9, 0x0000, 0x2CC1, 0x6040, 0x1FE3, 0xC879, 0xEDB6,
  454. 0xBED4, 0x468D, 0xD967, 0x4B72, 0xDE94, 0xD498, 0xE8B0, 0x4A85,
  455. 0x6BBB, 0x2AC5, 0xE54F, 0x16ED, 0xC586, 0xD79A, 0x5566, 0x9411,
  456. 0xCF8A, 0x10E9, 0x0604, 0x81FE, 0xF0A0, 0x4478, 0xBA25, 0xE34B,
  457. 0xF3A2, 0xFE5D, 0xC080, 0x8A05, 0xAD3F, 0xBC21, 0x4870, 0x04F1,
  458. 0xDF63, 0xC177, 0x75AF, 0x6342, 0x3020, 0x1AE5, 0x0EFD, 0x6DBF,
  459. 0x4C81, 0x1418, 0x3526, 0x2FC3, 0xE1BE, 0xA235, 0xCC88, 0x392E,
  460. 0x5793, 0xF255, 0x82FC, 0x477A, 0xACC8, 0xE7BA, 0x2B32, 0x95E6,
  461. 0xA0C0, 0x9819, 0xD19E, 0x7FA3, 0x6644, 0x7E54, 0xAB3B, 0x830B,
  462. 0xCA8C, 0x29C7, 0xD36B, 0x3C28, 0x79A7, 0xE2BC, 0x1D16, 0x76AD,
  463. 0x3BDB, 0x5664, 0x4E74, 0x1E14, 0xDB92, 0x0A0C, 0x6C48, 0xE4B8,
  464. 0x5D9F, 0x6EBD, 0xEF43, 0xA6C4, 0xA839, 0xA431, 0x37D3, 0x8BF2,
  465. 0x32D5, 0x438B, 0x596E, 0xB7DA, 0x8C01, 0x64B1, 0xD29C, 0xE049,
  466. 0xB4D8, 0xFAAC, 0x07F3, 0x25CF, 0xAFCA, 0x8EF4, 0xE947, 0x1810,
  467. 0xD56F, 0x88F0, 0x6F4A, 0x725C, 0x2438, 0xF157, 0xC773, 0x5197,
  468. 0x23CB, 0x7CA1, 0x9CE8, 0x213E, 0xDD96, 0xDC61, 0x860D, 0x850F,
  469. 0x90E0, 0x427C, 0xC471, 0xAACC, 0xD890, 0x0506, 0x01F7, 0x121C,
  470. 0xA3C2, 0x5F6A, 0xF9AE, 0xD069, 0x9117, 0x5899, 0x273A, 0xB927,
  471. 0x38D9, 0x13EB, 0xB32B, 0x3322, 0xBBD2, 0x70A9, 0x8907, 0xA733,
  472. 0xB62D, 0x223C, 0x9215, 0x20C9, 0x4987, 0xFFAA, 0x7850, 0x7AA5,
  473. 0x8F03, 0xF859, 0x8009, 0x171A, 0xDA65, 0x31D7, 0xC684, 0xB8D0,
  474. 0xC382, 0xB029, 0x775A, 0x111E, 0xCB7B, 0xFCA8, 0xD66D, 0x3A2C,
  475. }
  476. };
  477. /*
  478. **********************************************************************
  479. * Routine: Phase 1 -- generate P1K, given TA, TK, IV32
  480. *
  481. * Inputs:
  482. * tk[] = temporal key [128 bits]
  483. * ta[] = transmitter's MAC address [ 48 bits]
  484. * iv32 = upper 32 bits of IV [ 32 bits]
  485. * Output:
  486. * p1k[] = Phase 1 key [ 80 bits]
  487. *
  488. * Note:
  489. * This function only needs to be called every 2**16 packets,
  490. * although in theory it could be called every packet.
  491. *
  492. **********************************************************************
  493. */
  494. static void phase1(u16 *p1k, const u8 *tk, const u8 *ta, u32 iv32)
  495. {
  496. sint i;
  497. /* Initialize the 80 bits of P1K[] from IV32 and TA[0..5] */
  498. p1k[0] = Lo16(iv32);
  499. p1k[1] = Hi16(iv32);
  500. p1k[2] = Mk16(ta[1], ta[0]); /* use TA[] as little-endian */
  501. p1k[3] = Mk16(ta[3], ta[2]);
  502. p1k[4] = Mk16(ta[5], ta[4]);
  503. /* Now compute an unbalanced Feistel cipher with 80-bit block */
  504. /* size on the 80-bit block P1K[], using the 128-bit key TK[] */
  505. for (i = 0; i < PHASE1_LOOP_CNT ; i++) {
  506. /* Each add operation here is mod 2**16 */
  507. p1k[0] += _S_(p1k[4] ^ TK16((i & 1) + 0));
  508. p1k[1] += _S_(p1k[0] ^ TK16((i & 1) + 2));
  509. p1k[2] += _S_(p1k[1] ^ TK16((i & 1) + 4));
  510. p1k[3] += _S_(p1k[2] ^ TK16((i & 1) + 6));
  511. p1k[4] += _S_(p1k[3] ^ TK16((i & 1) + 0));
  512. p1k[4] += (unsigned short)i; /* avoid "slide attacks" */
  513. }
  514. }
  515. /*
  516. **********************************************************************
  517. * Routine: Phase 2 -- generate RC4KEY, given TK, P1K, IV16
  518. *
  519. * Inputs:
  520. * tk[] = Temporal key [128 bits]
  521. * p1k[] = Phase 1 output key [ 80 bits]
  522. * iv16 = low 16 bits of IV counter [ 16 bits]
  523. * Output:
  524. * rc4key[] = the key used to encrypt the packet [128 bits]
  525. *
  526. * Note:
  527. * The value {TA,IV32,IV16} for Phase1/Phase2 must be unique
  528. * across all packets using the same key TK value. Then, for a
  529. * given value of TK[], this TKIP48 construction guarantees that
  530. * the final RC4KEY value is unique across all packets.
  531. *
  532. * Suggested implementation optimization: if PPK[] is "overlaid"
  533. * appropriately on RC4KEY[], there is no need for the final
  534. * for loop below that copies the PPK[] result into RC4KEY[].
  535. *
  536. **********************************************************************
  537. */
  538. static void phase2(u8 *rc4key, const u8 *tk, const u16 *p1k, u16 iv16)
  539. {
  540. sint i;
  541. u16 PPK[6]; /* temporary key for mixing */
  542. /* Note: all adds in the PPK[] equations below are mod 2**16 */
  543. for (i = 0; i < 5; i++)
  544. PPK[i] = p1k[i]; /* first, copy P1K to PPK */
  545. PPK[5] = p1k[4] + iv16; /* next, add in IV16 */
  546. /* Bijective non-linear mixing of the 96 bits of PPK[0..5] */
  547. PPK[0] += _S_(PPK[5] ^ TK16(0)); /* Mix key in each "round" */
  548. PPK[1] += _S_(PPK[0] ^ TK16(1));
  549. PPK[2] += _S_(PPK[1] ^ TK16(2));
  550. PPK[3] += _S_(PPK[2] ^ TK16(3));
  551. PPK[4] += _S_(PPK[3] ^ TK16(4));
  552. PPK[5] += _S_(PPK[4] ^ TK16(5)); /* Total # S-box lookups == 6 */
  553. /* Final sweep: bijective, "linear". Rotates kill LSB correlations */
  554. PPK[0] += RotR1(PPK[5] ^ TK16(6));
  555. PPK[1] += RotR1(PPK[0] ^ TK16(7)); /* Use all of TK[] in Phase2 */
  556. PPK[2] += RotR1(PPK[1]);
  557. PPK[3] += RotR1(PPK[2]);
  558. PPK[4] += RotR1(PPK[3]);
  559. PPK[5] += RotR1(PPK[4]);
  560. /* Note: At this point, for a given key TK[0..15], the 96-bit output */
  561. /* value PPK[0..5] is guaranteed to be unique, as a function */
  562. /* of the 96-bit "input" value {TA,IV32,IV16}. That is, P1K */
  563. /* is now a keyed permutation of {TA,IV32,IV16}. */
  564. /* Set RC4KEY[0..3], which includes "cleartext" portion of RC4 key */
  565. rc4key[0] = Hi8(iv16); /* RC4KEY[0..2] is the WEP IV */
  566. rc4key[1] = (Hi8(iv16) | 0x20) & 0x7F; /* Help avoid weak (FMS) keys */
  567. rc4key[2] = Lo8(iv16);
  568. rc4key[3] = Lo8((PPK[5] ^ TK16(0)) >> 1);
  569. /* Copy 96 bits of PPK[0..5] to RC4KEY[4..15] (little-endian) */
  570. for (i = 0; i < 6; i++) {
  571. rc4key[4 + 2 * i] = Lo8(PPK[i]);
  572. rc4key[5 + 2 * i] = Hi8(PPK[i]);
  573. }
  574. }
  575. /* The hlen isn't include the IV */
  576. u32 rtw_tkip_encrypt(_adapter *padapter, u8 *pxmitframe)
  577. {
  578. /* exclude ICV */
  579. u16 pnl;
  580. u32 pnh;
  581. u8 rc4key[16];
  582. u8 ttkey[16];
  583. u8 crc[4];
  584. u8 hw_hdr_offset = 0;
  585. struct arc4context mycontext;
  586. sint curfragnum, length;
  587. u32 prwskeylen;
  588. u8 *pframe, *payload, *iv, *prwskey;
  589. union pn48 dot11txpn;
  590. /* struct sta_info *stainfo; */
  591. struct pkt_attrib *pattrib = &((struct xmit_frame *)pxmitframe)->attrib;
  592. struct security_priv *psecuritypriv = &padapter->securitypriv;
  593. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  594. u32 res = _SUCCESS;
  595. if (((struct xmit_frame *)pxmitframe)->buf_addr == NULL)
  596. return _FAIL;
  597. #ifdef CONFIG_USB_TX_AGGREGATION
  598. hw_hdr_offset = TXDESC_SIZE +
  599. (((struct xmit_frame *)pxmitframe)->pkt_offset * PACKET_OFFSET_SZ);
  600. #else
  601. #ifdef CONFIG_TX_EARLY_MODE
  602. hw_hdr_offset = TXDESC_OFFSET + EARLY_MODE_INFO_SIZE;
  603. #else
  604. hw_hdr_offset = TXDESC_OFFSET;
  605. #endif
  606. #endif
  607. pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + hw_hdr_offset;
  608. /* 4 start to encrypt each fragment */
  609. if (pattrib->encrypt == _TKIP_) {
  610. /*
  611. if(pattrib->psta)
  612. {
  613. stainfo = pattrib->psta;
  614. }
  615. else
  616. {
  617. RTW_INFO("%s, call rtw_get_stainfo()\n", __func__);
  618. stainfo=rtw_get_stainfo(&padapter->stapriv ,&pattrib->ra[0] );
  619. }
  620. */
  621. /* if (stainfo!=NULL) */
  622. {
  623. /*
  624. if(!(stainfo->state &_FW_LINKED))
  625. {
  626. RTW_INFO("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, stainfo->state);
  627. return _FAIL;
  628. }
  629. */
  630. if (IS_MCAST(pattrib->ra))
  631. prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey;
  632. else {
  633. /* prwskey=&stainfo->dot118021x_UncstKey.skey[0]; */
  634. prwskey = pattrib->dot118021x_UncstKey.skey;
  635. }
  636. prwskeylen = 16;
  637. for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
  638. iv = pframe + pattrib->hdrlen;
  639. payload = pframe + pattrib->iv_len + pattrib->hdrlen;
  640. GET_TKIP_PN(iv, dot11txpn);
  641. pnl = (u16)(dot11txpn.val);
  642. pnh = (u32)(dot11txpn.val >> 16);
  643. phase1((u16 *)&ttkey[0], prwskey, &pattrib->ta[0], pnh);
  644. phase2(&rc4key[0], prwskey, (u16 *)&ttkey[0], pnl);
  645. if ((curfragnum + 1) == pattrib->nr_frags) { /* 4 the last fragment */
  646. length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
  647. *((u32 *)crc) = cpu_to_le32(getcrc32(payload, length)); /* modified by Amy*/
  648. arcfour_init(&mycontext, rc4key, 16);
  649. arcfour_encrypt(&mycontext, payload, payload, length);
  650. arcfour_encrypt(&mycontext, payload + length, crc, 4);
  651. } else {
  652. length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len ;
  653. *((u32 *)crc) = cpu_to_le32(getcrc32(payload, length)); /* modified by Amy*/
  654. arcfour_init(&mycontext, rc4key, 16);
  655. arcfour_encrypt(&mycontext, payload, payload, length);
  656. arcfour_encrypt(&mycontext, payload + length, crc, 4);
  657. pframe += pxmitpriv->frag_len;
  658. pframe = (u8 *)RND4((SIZE_PTR)(pframe));
  659. }
  660. }
  661. TKIP_SW_ENC_CNT_INC(psecuritypriv, pattrib->ra);
  662. }
  663. /*
  664. else{
  665. RTW_INFO("%s, psta==NUL\n", __func__);
  666. res=_FAIL;
  667. }
  668. */
  669. }
  670. return res;
  671. }
  672. /* The hlen isn't include the IV */
  673. u32 rtw_tkip_decrypt(_adapter *padapter, u8 *precvframe)
  674. {
  675. /* exclude ICV */
  676. u16 pnl;
  677. u32 pnh;
  678. u8 rc4key[16];
  679. u8 ttkey[16];
  680. u8 crc[4];
  681. struct arc4context mycontext;
  682. sint length;
  683. u32 prwskeylen;
  684. u8 *pframe, *payload, *iv, *prwskey;
  685. union pn48 dot11txpn;
  686. struct sta_info *stainfo;
  687. struct rx_pkt_attrib *prxattrib = &((union recv_frame *)precvframe)->u.hdr.attrib;
  688. struct security_priv *psecuritypriv = &padapter->securitypriv;
  689. /* struct recv_priv *precvpriv=&padapter->recvpriv; */
  690. u32 res = _SUCCESS;
  691. pframe = (unsigned char *)((union recv_frame *)precvframe)->u.hdr.rx_data;
  692. /* 4 start to decrypt recvframe */
  693. if (prxattrib->encrypt == _TKIP_) {
  694. stainfo = rtw_get_stainfo(&padapter->stapriv , &prxattrib->ta[0]);
  695. if (stainfo != NULL) {
  696. if (IS_MCAST(prxattrib->ra)) {
  697. static systime start = 0;
  698. static u32 no_gkey_bc_cnt = 0;
  699. static u32 no_gkey_mc_cnt = 0;
  700. if (psecuritypriv->binstallGrpkey == _FALSE) {
  701. res = _FAIL;
  702. if (start == 0)
  703. start = rtw_get_current_time();
  704. if (is_broadcast_mac_addr(prxattrib->ra))
  705. no_gkey_bc_cnt++;
  706. else
  707. no_gkey_mc_cnt++;
  708. if (rtw_get_passing_time_ms(start) > 1000) {
  709. if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
  710. RTW_PRINT(FUNC_ADPT_FMT" no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
  711. FUNC_ADPT_ARG(padapter), no_gkey_bc_cnt, no_gkey_mc_cnt);
  712. }
  713. start = rtw_get_current_time();
  714. no_gkey_bc_cnt = 0;
  715. no_gkey_mc_cnt = 0;
  716. }
  717. goto exit;
  718. }
  719. if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
  720. RTW_PRINT(FUNC_ADPT_FMT" gkey installed. no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
  721. FUNC_ADPT_ARG(padapter), no_gkey_bc_cnt, no_gkey_mc_cnt);
  722. }
  723. start = 0;
  724. no_gkey_bc_cnt = 0;
  725. no_gkey_mc_cnt = 0;
  726. /* RTW_INFO("rx bc/mc packets, to perform sw rtw_tkip_decrypt\n"); */
  727. /* prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey; */
  728. prwskey = psecuritypriv->dot118021XGrpKey[prxattrib->key_index].skey;
  729. prwskeylen = 16;
  730. } else {
  731. prwskey = &stainfo->dot118021x_UncstKey.skey[0];
  732. prwskeylen = 16;
  733. }
  734. iv = pframe + prxattrib->hdrlen;
  735. payload = pframe + prxattrib->iv_len + prxattrib->hdrlen;
  736. length = ((union recv_frame *)precvframe)->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len;
  737. GET_TKIP_PN(iv, dot11txpn);
  738. pnl = (u16)(dot11txpn.val);
  739. pnh = (u32)(dot11txpn.val >> 16);
  740. phase1((u16 *)&ttkey[0], prwskey, &prxattrib->ta[0], pnh);
  741. phase2(&rc4key[0], prwskey, (unsigned short *)&ttkey[0], pnl);
  742. /* 4 decrypt payload include icv */
  743. arcfour_init(&mycontext, rc4key, 16);
  744. arcfour_encrypt(&mycontext, payload, payload, length);
  745. *((u32 *)crc) = le32_to_cpu(getcrc32(payload, length - 4));
  746. if (crc[3] != payload[length - 1] || crc[2] != payload[length - 2] || crc[1] != payload[length - 3] || crc[0] != payload[length - 4]) {
  747. res = _FAIL;
  748. }
  749. TKIP_SW_DEC_CNT_INC(psecuritypriv, prxattrib->ra);
  750. } else {
  751. res = _FAIL;
  752. }
  753. }
  754. exit:
  755. return res;
  756. }
  757. /* 3 =====AES related===== */
  758. #define MAX_MSG_SIZE 2048
  759. /*****************************/
  760. /******** SBOX Table *********/
  761. /*****************************/
  762. static u8 sbox_table[256] = {
  763. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  764. 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  765. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  766. 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  767. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  768. 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  769. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  770. 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  771. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  772. 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  773. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  774. 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  775. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  776. 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  777. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  778. 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  779. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  780. 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  781. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  782. 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  783. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  784. 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  785. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  786. 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  787. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  788. 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  789. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  790. 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  791. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  792. 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  793. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  794. 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  795. };
  796. /*****************************/
  797. /**** Function Prototypes ****/
  798. /*****************************/
  799. static void bitwise_xor(u8 *ina, u8 *inb, u8 *out);
  800. static void construct_mic_iv(
  801. u8 *mic_header1,
  802. sint qc_exists,
  803. sint a4_exists,
  804. u8 *mpdu,
  805. uint payload_length,
  806. u8 *pn_vector,
  807. uint frtype);/* add for CONFIG_IEEE80211W, none 11w also can use */
  808. static void construct_mic_header1(
  809. u8 *mic_header1,
  810. sint header_length,
  811. u8 *mpdu,
  812. uint frtype);/* add for CONFIG_IEEE80211W, none 11w also can use */
  813. static void construct_mic_header2(
  814. u8 *mic_header2,
  815. u8 *mpdu,
  816. sint a4_exists,
  817. sint qc_exists);
  818. static void construct_ctr_preload(
  819. u8 *ctr_preload,
  820. sint a4_exists,
  821. sint qc_exists,
  822. u8 *mpdu,
  823. u8 *pn_vector,
  824. sint c,
  825. uint frtype);/* add for CONFIG_IEEE80211W, none 11w also can use */
  826. static void xor_128(u8 *a, u8 *b, u8 *out);
  827. static void xor_32(u8 *a, u8 *b, u8 *out);
  828. static u8 sbox(u8 a);
  829. static void next_key(u8 *key, sint round);
  830. static void byte_sub(u8 *in, u8 *out);
  831. static void shift_row(u8 *in, u8 *out);
  832. static void mix_column(u8 *in, u8 *out);
  833. static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext);
  834. /****************************************/
  835. /* aes128k128d() */
  836. /* Performs a 128 bit AES encrypt with */
  837. /* 128 bit data. */
  838. /****************************************/
  839. static void xor_128(u8 *a, u8 *b, u8 *out)
  840. {
  841. sint i;
  842. for (i = 0; i < 16; i++)
  843. out[i] = a[i] ^ b[i];
  844. }
  845. static void xor_32(u8 *a, u8 *b, u8 *out)
  846. {
  847. sint i;
  848. for (i = 0; i < 4; i++)
  849. out[i] = a[i] ^ b[i];
  850. }
  851. static u8 sbox(u8 a)
  852. {
  853. return sbox_table[(sint)a];
  854. }
  855. static void next_key(u8 *key, sint round)
  856. {
  857. u8 rcon;
  858. u8 sbox_key[4];
  859. u8 rcon_table[12] = {
  860. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  861. 0x1b, 0x36, 0x36, 0x36
  862. };
  863. sbox_key[0] = sbox(key[13]);
  864. sbox_key[1] = sbox(key[14]);
  865. sbox_key[2] = sbox(key[15]);
  866. sbox_key[3] = sbox(key[12]);
  867. rcon = rcon_table[round];
  868. xor_32(&key[0], sbox_key, &key[0]);
  869. key[0] = key[0] ^ rcon;
  870. xor_32(&key[4], &key[0], &key[4]);
  871. xor_32(&key[8], &key[4], &key[8]);
  872. xor_32(&key[12], &key[8], &key[12]);
  873. }
  874. static void byte_sub(u8 *in, u8 *out)
  875. {
  876. sint i;
  877. for (i = 0; i < 16; i++)
  878. out[i] = sbox(in[i]);
  879. }
  880. static void shift_row(u8 *in, u8 *out)
  881. {
  882. out[0] = in[0];
  883. out[1] = in[5];
  884. out[2] = in[10];
  885. out[3] = in[15];
  886. out[4] = in[4];
  887. out[5] = in[9];
  888. out[6] = in[14];
  889. out[7] = in[3];
  890. out[8] = in[8];
  891. out[9] = in[13];
  892. out[10] = in[2];
  893. out[11] = in[7];
  894. out[12] = in[12];
  895. out[13] = in[1];
  896. out[14] = in[6];
  897. out[15] = in[11];
  898. }
  899. static void mix_column(u8 *in, u8 *out)
  900. {
  901. sint i;
  902. u8 add1b[4];
  903. u8 add1bf7[4];
  904. u8 rotl[4];
  905. u8 swap_halfs[4];
  906. u8 andf7[4];
  907. u8 rotr[4];
  908. u8 temp[4];
  909. u8 tempb[4];
  910. for (i = 0 ; i < 4; i++) {
  911. if ((in[i] & 0x80) == 0x80)
  912. add1b[i] = 0x1b;
  913. else
  914. add1b[i] = 0x00;
  915. }
  916. swap_halfs[0] = in[2]; /* Swap halfs */
  917. swap_halfs[1] = in[3];
  918. swap_halfs[2] = in[0];
  919. swap_halfs[3] = in[1];
  920. rotl[0] = in[3]; /* Rotate left 8 bits */
  921. rotl[1] = in[0];
  922. rotl[2] = in[1];
  923. rotl[3] = in[2];
  924. andf7[0] = in[0] & 0x7f;
  925. andf7[1] = in[1] & 0x7f;
  926. andf7[2] = in[2] & 0x7f;
  927. andf7[3] = in[3] & 0x7f;
  928. for (i = 3; i > 0; i--) { /* logical shift left 1 bit */
  929. andf7[i] = andf7[i] << 1;
  930. if ((andf7[i - 1] & 0x80) == 0x80)
  931. andf7[i] = (andf7[i] | 0x01);
  932. }
  933. andf7[0] = andf7[0] << 1;
  934. andf7[0] = andf7[0] & 0xfe;
  935. xor_32(add1b, andf7, add1bf7);
  936. xor_32(in, add1bf7, rotr);
  937. temp[0] = rotr[0]; /* Rotate right 8 bits */
  938. rotr[0] = rotr[1];
  939. rotr[1] = rotr[2];
  940. rotr[2] = rotr[3];
  941. rotr[3] = temp[0];
  942. xor_32(add1bf7, rotr, temp);
  943. xor_32(swap_halfs, rotl, tempb);
  944. xor_32(temp, tempb, out);
  945. }
  946. static void aes128k128d(u8 *key, u8 *data, u8 *ciphertext)
  947. {
  948. sint round;
  949. sint i;
  950. u8 intermediatea[16];
  951. u8 intermediateb[16];
  952. u8 round_key[16];
  953. for (i = 0; i < 16; i++)
  954. round_key[i] = key[i];
  955. for (round = 0; round < 11; round++) {
  956. if (round == 0) {
  957. xor_128(round_key, data, ciphertext);
  958. next_key(round_key, round);
  959. } else if (round == 10) {
  960. byte_sub(ciphertext, intermediatea);
  961. shift_row(intermediatea, intermediateb);
  962. xor_128(intermediateb, round_key, ciphertext);
  963. } else { /* 1 - 9 */
  964. byte_sub(ciphertext, intermediatea);
  965. shift_row(intermediatea, intermediateb);
  966. mix_column(&intermediateb[0], &intermediatea[0]);
  967. mix_column(&intermediateb[4], &intermediatea[4]);
  968. mix_column(&intermediateb[8], &intermediatea[8]);
  969. mix_column(&intermediateb[12], &intermediatea[12]);
  970. xor_128(intermediatea, round_key, ciphertext);
  971. next_key(round_key, round);
  972. }
  973. }
  974. }
  975. /************************************************/
  976. /* construct_mic_iv() */
  977. /* Builds the MIC IV from header fields and PN */
  978. /* Baron think the function is construct CCM */
  979. /* nonce */
  980. /************************************************/
  981. static void construct_mic_iv(
  982. u8 *mic_iv,
  983. sint qc_exists,
  984. sint a4_exists,
  985. u8 *mpdu,
  986. uint payload_length,
  987. u8 *pn_vector,
  988. uint frtype/* add for CONFIG_IEEE80211W, none 11w also can use */
  989. )
  990. {
  991. sint i;
  992. mic_iv[0] = 0x59;
  993. if (qc_exists && a4_exists)
  994. mic_iv[1] = mpdu[30] & 0x0f; /* QoS_TC */
  995. if (qc_exists && !a4_exists)
  996. mic_iv[1] = mpdu[24] & 0x0f; /* mute bits 7-4 */
  997. if (!qc_exists)
  998. mic_iv[1] = 0x00;
  999. #if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH)
  1000. /* 802.11w management frame should set management bit(4) */
  1001. if (frtype == WIFI_MGT_TYPE)
  1002. mic_iv[1] |= BIT(4);
  1003. #endif
  1004. for (i = 2; i < 8; i++)
  1005. mic_iv[i] = mpdu[i + 8]; /* mic_iv[2:7] = A2[0:5] = mpdu[10:15] */
  1006. #ifdef CONSISTENT_PN_ORDER
  1007. for (i = 8; i < 14; i++)
  1008. mic_iv[i] = pn_vector[i - 8]; /* mic_iv[8:13] = PN[0:5] */
  1009. #else
  1010. for (i = 8; i < 14; i++)
  1011. mic_iv[i] = pn_vector[13 - i]; /* mic_iv[8:13] = PN[5:0] */
  1012. #endif
  1013. mic_iv[14] = (unsigned char)(payload_length / 256);
  1014. mic_iv[15] = (unsigned char)(payload_length % 256);
  1015. }
  1016. /************************************************/
  1017. /* construct_mic_header1() */
  1018. /* Builds the first MIC header block from */
  1019. /* header fields. */
  1020. /* Build AAD SC,A1,A2 */
  1021. /************************************************/
  1022. static void construct_mic_header1(
  1023. u8 *mic_header1,
  1024. sint header_length,
  1025. u8 *mpdu,
  1026. uint frtype/* add for CONFIG_IEEE80211W, none 11w also can use */
  1027. )
  1028. {
  1029. mic_header1[0] = (u8)((header_length - 2) / 256);
  1030. mic_header1[1] = (u8)((header_length - 2) % 256);
  1031. #if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH)
  1032. /* 802.11w management frame don't AND subtype bits 4,5,6 of frame control field */
  1033. if (frtype == WIFI_MGT_TYPE)
  1034. mic_header1[2] = mpdu[0];
  1035. else
  1036. #endif
  1037. mic_header1[2] = mpdu[0] & 0xcf; /* Mute CF poll & CF ack bits */
  1038. mic_header1[3] = mpdu[1] & 0xc7; /* Mute retry, more data and pwr mgt bits */
  1039. mic_header1[4] = mpdu[4]; /* A1 */
  1040. mic_header1[5] = mpdu[5];
  1041. mic_header1[6] = mpdu[6];
  1042. mic_header1[7] = mpdu[7];
  1043. mic_header1[8] = mpdu[8];
  1044. mic_header1[9] = mpdu[9];
  1045. mic_header1[10] = mpdu[10]; /* A2 */
  1046. mic_header1[11] = mpdu[11];
  1047. mic_header1[12] = mpdu[12];
  1048. mic_header1[13] = mpdu[13];
  1049. mic_header1[14] = mpdu[14];
  1050. mic_header1[15] = mpdu[15];
  1051. }
  1052. /************************************************/
  1053. /* construct_mic_header2() */
  1054. /* Builds the last MIC header block from */
  1055. /* header fields. */
  1056. /************************************************/
  1057. static void construct_mic_header2(
  1058. u8 *mic_header2,
  1059. u8 *mpdu,
  1060. sint a4_exists,
  1061. sint qc_exists
  1062. )
  1063. {
  1064. sint i;
  1065. for (i = 0; i < 16; i++)
  1066. mic_header2[i] = 0x00;
  1067. mic_header2[0] = mpdu[16]; /* A3 */
  1068. mic_header2[1] = mpdu[17];
  1069. mic_header2[2] = mpdu[18];
  1070. mic_header2[3] = mpdu[19];
  1071. mic_header2[4] = mpdu[20];
  1072. mic_header2[5] = mpdu[21];
  1073. /* mic_header2[6] = mpdu[22] & 0xf0; SC */
  1074. mic_header2[6] = 0x00;
  1075. mic_header2[7] = 0x00; /* mpdu[23]; */
  1076. if (!qc_exists && a4_exists) {
  1077. for (i = 0; i < 6; i++)
  1078. mic_header2[8 + i] = mpdu[24 + i]; /* A4 */
  1079. }
  1080. if (qc_exists && !a4_exists) {
  1081. mic_header2[8] = mpdu[24] & 0x0f; /* mute bits 15 - 4 */
  1082. mic_header2[9] = mpdu[25] & 0x00;
  1083. }
  1084. if (qc_exists && a4_exists) {
  1085. for (i = 0; i < 6; i++)
  1086. mic_header2[8 + i] = mpdu[24 + i]; /* A4 */
  1087. mic_header2[14] = mpdu[30] & 0x0f;
  1088. mic_header2[15] = mpdu[31] & 0x00;
  1089. }
  1090. }
  1091. /************************************************/
  1092. /* construct_mic_header2() */
  1093. /* Builds the last MIC header block from */
  1094. /* header fields. */
  1095. /* Baron think the function is construct CCM */
  1096. /* nonce */
  1097. /************************************************/
  1098. static void construct_ctr_preload(
  1099. u8 *ctr_preload,
  1100. sint a4_exists,
  1101. sint qc_exists,
  1102. u8 *mpdu,
  1103. u8 *pn_vector,
  1104. sint c,
  1105. uint frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1106. )
  1107. {
  1108. sint i = 0;
  1109. for (i = 0; i < 16; i++)
  1110. ctr_preload[i] = 0x00;
  1111. i = 0;
  1112. ctr_preload[0] = 0x01; /* flag */
  1113. if (qc_exists && a4_exists)
  1114. ctr_preload[1] = mpdu[30] & 0x0f; /* QoC_Control */
  1115. if (qc_exists && !a4_exists)
  1116. ctr_preload[1] = mpdu[24] & 0x0f;
  1117. #if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH)
  1118. /* 802.11w management frame should set management bit(4) */
  1119. if (frtype == WIFI_MGT_TYPE)
  1120. ctr_preload[1] |= BIT(4);
  1121. #endif
  1122. for (i = 2; i < 8; i++)
  1123. ctr_preload[i] = mpdu[i + 8]; /* ctr_preload[2:7] = A2[0:5] = mpdu[10:15] */
  1124. #ifdef CONSISTENT_PN_ORDER
  1125. for (i = 8; i < 14; i++)
  1126. ctr_preload[i] = pn_vector[i - 8]; /* ctr_preload[8:13] = PN[0:5] */
  1127. #else
  1128. for (i = 8; i < 14; i++)
  1129. ctr_preload[i] = pn_vector[13 - i]; /* ctr_preload[8:13] = PN[5:0] */
  1130. #endif
  1131. ctr_preload[14] = (unsigned char)(c / 256); /* Ctr */
  1132. ctr_preload[15] = (unsigned char)(c % 256);
  1133. }
  1134. /************************************/
  1135. /* bitwise_xor() */
  1136. /* A 128 bit, bitwise exclusive or */
  1137. /************************************/
  1138. static void bitwise_xor(u8 *ina, u8 *inb, u8 *out)
  1139. {
  1140. sint i;
  1141. for (i = 0; i < 16; i++)
  1142. out[i] = ina[i] ^ inb[i];
  1143. }
  1144. static sint aes_cipher(u8 *key, uint hdrlen,
  1145. u8 *pframe, uint plen)
  1146. {
  1147. /* static unsigned char message[MAX_MSG_SIZE]; */
  1148. uint qc_exists, a4_exists, i, j, payload_remainder,
  1149. num_blocks, payload_index;
  1150. u8 pn_vector[6];
  1151. u8 mic_iv[16];
  1152. u8 mic_header1[16];
  1153. u8 mic_header2[16];
  1154. u8 ctr_preload[16];
  1155. /* Intermediate Buffers */
  1156. u8 chain_buffer[16];
  1157. u8 aes_out[16];
  1158. u8 padded_buffer[16];
  1159. u8 mic[8];
  1160. /* uint offset = 0; */
  1161. uint frtype = GetFrameType(pframe);
  1162. uint frsubtype = get_frame_sub_type(pframe);
  1163. frsubtype = frsubtype >> 4;
  1164. _rtw_memset((void *)mic_iv, 0, 16);
  1165. _rtw_memset((void *)mic_header1, 0, 16);
  1166. _rtw_memset((void *)mic_header2, 0, 16);
  1167. _rtw_memset((void *)ctr_preload, 0, 16);
  1168. _rtw_memset((void *)chain_buffer, 0, 16);
  1169. _rtw_memset((void *)aes_out, 0, 16);
  1170. _rtw_memset((void *)padded_buffer, 0, 16);
  1171. if ((hdrlen == WLAN_HDR_A3_LEN) || (hdrlen == WLAN_HDR_A3_QOS_LEN))
  1172. a4_exists = 0;
  1173. else
  1174. a4_exists = 1;
  1175. if (
  1176. ((frtype | frsubtype) == WIFI_DATA_CFACK) ||
  1177. ((frtype | frsubtype) == WIFI_DATA_CFPOLL) ||
  1178. ((frtype | frsubtype) == WIFI_DATA_CFACKPOLL)) {
  1179. qc_exists = 1;
  1180. if (hdrlen != WLAN_HDR_A3_QOS_LEN && hdrlen != WLAN_HDR_A4_QOS_LEN)
  1181. hdrlen += 2;
  1182. }
  1183. /* add for CONFIG_IEEE80211W, none 11w also can use */
  1184. else if ((frtype == WIFI_DATA) &&
  1185. ((frsubtype == 0x08) ||
  1186. (frsubtype == 0x09) ||
  1187. (frsubtype == 0x0a) ||
  1188. (frsubtype == 0x0b))) {
  1189. if (hdrlen != WLAN_HDR_A3_QOS_LEN && hdrlen != WLAN_HDR_A4_QOS_LEN)
  1190. hdrlen += 2;
  1191. qc_exists = 1;
  1192. } else
  1193. qc_exists = 0;
  1194. pn_vector[0] = pframe[hdrlen];
  1195. pn_vector[1] = pframe[hdrlen + 1];
  1196. pn_vector[2] = pframe[hdrlen + 4];
  1197. pn_vector[3] = pframe[hdrlen + 5];
  1198. pn_vector[4] = pframe[hdrlen + 6];
  1199. pn_vector[5] = pframe[hdrlen + 7];
  1200. construct_mic_iv(
  1201. mic_iv,
  1202. qc_exists,
  1203. a4_exists,
  1204. pframe, /* message, */
  1205. plen,
  1206. pn_vector,
  1207. frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1208. );
  1209. construct_mic_header1(
  1210. mic_header1,
  1211. hdrlen,
  1212. pframe, /* message */
  1213. frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1214. );
  1215. construct_mic_header2(
  1216. mic_header2,
  1217. pframe, /* message, */
  1218. a4_exists,
  1219. qc_exists
  1220. );
  1221. payload_remainder = plen % 16;
  1222. num_blocks = plen / 16;
  1223. /* Find start of payload */
  1224. payload_index = (hdrlen + 8);
  1225. /* Calculate MIC */
  1226. aes128k128d(key, mic_iv, aes_out);
  1227. bitwise_xor(aes_out, mic_header1, chain_buffer);
  1228. aes128k128d(key, chain_buffer, aes_out);
  1229. bitwise_xor(aes_out, mic_header2, chain_buffer);
  1230. aes128k128d(key, chain_buffer, aes_out);
  1231. for (i = 0; i < num_blocks; i++) {
  1232. bitwise_xor(aes_out, &pframe[payload_index], chain_buffer);/* bitwise_xor(aes_out, &message[payload_index], chain_buffer); */
  1233. payload_index += 16;
  1234. aes128k128d(key, chain_buffer, aes_out);
  1235. }
  1236. /* Add on the final payload block if it needs padding */
  1237. if (payload_remainder > 0) {
  1238. for (j = 0; j < 16; j++)
  1239. padded_buffer[j] = 0x00;
  1240. for (j = 0; j < payload_remainder; j++) {
  1241. padded_buffer[j] = pframe[payload_index++];/* padded_buffer[j] = message[payload_index++]; */
  1242. }
  1243. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1244. aes128k128d(key, chain_buffer, aes_out);
  1245. }
  1246. for (j = 0 ; j < 8; j++)
  1247. mic[j] = aes_out[j];
  1248. /* Insert MIC into payload */
  1249. for (j = 0; j < 8; j++)
  1250. pframe[payload_index + j] = mic[j]; /* message[payload_index+j] = mic[j]; */
  1251. payload_index = hdrlen + 8;
  1252. for (i = 0; i < num_blocks; i++) {
  1253. construct_ctr_preload(
  1254. ctr_preload,
  1255. a4_exists,
  1256. qc_exists,
  1257. pframe, /* message, */
  1258. pn_vector,
  1259. i + 1,
  1260. frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */
  1261. aes128k128d(key, ctr_preload, aes_out);
  1262. bitwise_xor(aes_out, &pframe[payload_index], chain_buffer);/* bitwise_xor(aes_out, &message[payload_index], chain_buffer); */
  1263. for (j = 0; j < 16; j++)
  1264. pframe[payload_index++] = chain_buffer[j];/* for (j=0; j<16;j++) message[payload_index++] = chain_buffer[j]; */
  1265. }
  1266. if (payload_remainder > 0) { /* If there is a short final block, then pad it,*/
  1267. /* encrypt it and copy the unpadded part back */
  1268. construct_ctr_preload(
  1269. ctr_preload,
  1270. a4_exists,
  1271. qc_exists,
  1272. pframe, /* message, */
  1273. pn_vector,
  1274. num_blocks + 1,
  1275. frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */
  1276. for (j = 0; j < 16; j++)
  1277. padded_buffer[j] = 0x00;
  1278. for (j = 0; j < payload_remainder; j++) {
  1279. padded_buffer[j] = pframe[payload_index + j]; /* padded_buffer[j] = message[payload_index+j]; */
  1280. }
  1281. aes128k128d(key, ctr_preload, aes_out);
  1282. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1283. for (j = 0; j < payload_remainder; j++)
  1284. pframe[payload_index++] = chain_buffer[j];/* for (j=0; j<payload_remainder;j++) message[payload_index++] = chain_buffer[j]; */
  1285. }
  1286. /* Encrypt the MIC */
  1287. construct_ctr_preload(
  1288. ctr_preload,
  1289. a4_exists,
  1290. qc_exists,
  1291. pframe, /* message, */
  1292. pn_vector,
  1293. 0,
  1294. frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */
  1295. for (j = 0; j < 16; j++)
  1296. padded_buffer[j] = 0x00;
  1297. for (j = 0; j < 8; j++) {
  1298. padded_buffer[j] = pframe[j + hdrlen + 8 + plen]; /* padded_buffer[j] = message[j+hdrlen+8+plen]; */
  1299. }
  1300. aes128k128d(key, ctr_preload, aes_out);
  1301. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1302. for (j = 0; j < 8; j++)
  1303. pframe[payload_index++] = chain_buffer[j];/* for (j=0; j<8;j++) message[payload_index++] = chain_buffer[j]; */
  1304. return _SUCCESS;
  1305. }
  1306. u32 rtw_aes_encrypt(_adapter *padapter, u8 *pxmitframe)
  1307. {
  1308. /* exclude ICV */
  1309. /*static*/
  1310. /* unsigned char message[MAX_MSG_SIZE]; */
  1311. /* Intermediate Buffers */
  1312. sint curfragnum, length;
  1313. u32 prwskeylen;
  1314. u8 *pframe, *prwskey; /* , *payload,*iv */
  1315. u8 hw_hdr_offset = 0;
  1316. /* struct sta_info *stainfo=NULL; */
  1317. struct pkt_attrib *pattrib = &((struct xmit_frame *)pxmitframe)->attrib;
  1318. struct security_priv *psecuritypriv = &padapter->securitypriv;
  1319. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  1320. /* uint offset = 0; */
  1321. u32 res = _SUCCESS;
  1322. if (((struct xmit_frame *)pxmitframe)->buf_addr == NULL)
  1323. return _FAIL;
  1324. #ifdef CONFIG_USB_TX_AGGREGATION
  1325. hw_hdr_offset = TXDESC_SIZE +
  1326. (((struct xmit_frame *)pxmitframe)->pkt_offset * PACKET_OFFSET_SZ);
  1327. #else
  1328. #ifdef CONFIG_TX_EARLY_MODE
  1329. hw_hdr_offset = TXDESC_OFFSET + EARLY_MODE_INFO_SIZE;
  1330. #else
  1331. hw_hdr_offset = TXDESC_OFFSET;
  1332. #endif
  1333. #endif
  1334. pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + hw_hdr_offset;
  1335. /* 4 start to encrypt each fragment */
  1336. if ((pattrib->encrypt == _AES_)) {
  1337. /*
  1338. if(pattrib->psta)
  1339. {
  1340. stainfo = pattrib->psta;
  1341. }
  1342. else
  1343. {
  1344. RTW_INFO("%s, call rtw_get_stainfo()\n", __func__);
  1345. stainfo=rtw_get_stainfo(&padapter->stapriv ,&pattrib->ra[0] );
  1346. }
  1347. */
  1348. /* if (stainfo!=NULL) */
  1349. {
  1350. /*
  1351. if(!(stainfo->state &_FW_LINKED))
  1352. {
  1353. RTW_INFO("%s, psta->state(0x%x) != _FW_LINKED\n", __func__, stainfo->state);
  1354. return _FAIL;
  1355. }
  1356. */
  1357. if (IS_MCAST(pattrib->ra))
  1358. prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey;
  1359. else {
  1360. /* prwskey=&stainfo->dot118021x_UncstKey.skey[0]; */
  1361. prwskey = pattrib->dot118021x_UncstKey.skey;
  1362. }
  1363. #ifdef CONFIG_TDLS
  1364. {
  1365. /* Swencryption */
  1366. struct sta_info *ptdls_sta;
  1367. ptdls_sta = rtw_get_stainfo(&padapter->stapriv , &pattrib->dst[0]);
  1368. if ((ptdls_sta != NULL) && (ptdls_sta->tdls_sta_state & TDLS_LINKED_STATE)) {
  1369. RTW_INFO("[%s] for tdls link\n", __FUNCTION__);
  1370. prwskey = &ptdls_sta->tpk.tk[0];
  1371. }
  1372. }
  1373. #endif /* CONFIG_TDLS */
  1374. prwskeylen = 16;
  1375. for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
  1376. if ((curfragnum + 1) == pattrib->nr_frags) { /* 4 the last fragment */
  1377. length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
  1378. aes_cipher(prwskey, pattrib->hdrlen, pframe, length);
  1379. } else {
  1380. length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len ;
  1381. aes_cipher(prwskey, pattrib->hdrlen, pframe, length);
  1382. pframe += pxmitpriv->frag_len;
  1383. pframe = (u8 *)RND4((SIZE_PTR)(pframe));
  1384. }
  1385. }
  1386. AES_SW_ENC_CNT_INC(psecuritypriv, pattrib->ra);
  1387. }
  1388. /*
  1389. else{
  1390. RTW_INFO("%s, psta==NUL\n", __func__);
  1391. res=_FAIL;
  1392. }
  1393. */
  1394. }
  1395. return res;
  1396. }
  1397. static sint aes_decipher(u8 *key, uint hdrlen,
  1398. u8 *pframe, uint plen)
  1399. {
  1400. static u8 message[MAX_MSG_SIZE];
  1401. uint qc_exists, a4_exists, i, j, payload_remainder,
  1402. num_blocks, payload_index;
  1403. sint res = _SUCCESS;
  1404. u8 pn_vector[6];
  1405. u8 mic_iv[16];
  1406. u8 mic_header1[16];
  1407. u8 mic_header2[16];
  1408. u8 ctr_preload[16];
  1409. /* Intermediate Buffers */
  1410. u8 chain_buffer[16];
  1411. u8 aes_out[16];
  1412. u8 padded_buffer[16];
  1413. u8 mic[8];
  1414. /* uint offset = 0; */
  1415. uint frtype = GetFrameType(pframe);
  1416. uint frsubtype = get_frame_sub_type(pframe);
  1417. frsubtype = frsubtype >> 4;
  1418. _rtw_memset((void *)mic_iv, 0, 16);
  1419. _rtw_memset((void *)mic_header1, 0, 16);
  1420. _rtw_memset((void *)mic_header2, 0, 16);
  1421. _rtw_memset((void *)ctr_preload, 0, 16);
  1422. _rtw_memset((void *)chain_buffer, 0, 16);
  1423. _rtw_memset((void *)aes_out, 0, 16);
  1424. _rtw_memset((void *)padded_buffer, 0, 16);
  1425. /* start to decrypt the payload */
  1426. num_blocks = (plen - 8) / 16; /* (plen including LLC, payload_length and mic ) */
  1427. payload_remainder = (plen - 8) % 16;
  1428. pn_vector[0] = pframe[hdrlen];
  1429. pn_vector[1] = pframe[hdrlen + 1];
  1430. pn_vector[2] = pframe[hdrlen + 4];
  1431. pn_vector[3] = pframe[hdrlen + 5];
  1432. pn_vector[4] = pframe[hdrlen + 6];
  1433. pn_vector[5] = pframe[hdrlen + 7];
  1434. if ((hdrlen == WLAN_HDR_A3_LEN) || (hdrlen == WLAN_HDR_A3_QOS_LEN))
  1435. a4_exists = 0;
  1436. else
  1437. a4_exists = 1;
  1438. if (
  1439. ((frtype | frsubtype) == WIFI_DATA_CFACK) ||
  1440. ((frtype | frsubtype) == WIFI_DATA_CFPOLL) ||
  1441. ((frtype | frsubtype) == WIFI_DATA_CFACKPOLL)) {
  1442. qc_exists = 1;
  1443. if (hdrlen != WLAN_HDR_A3_QOS_LEN && hdrlen != WLAN_HDR_A4_QOS_LEN)
  1444. hdrlen += 2;
  1445. } /* only for data packet . add for CONFIG_IEEE80211W, none 11w also can use */
  1446. else if ((frtype == WIFI_DATA) &&
  1447. ((frsubtype == 0x08) ||
  1448. (frsubtype == 0x09) ||
  1449. (frsubtype == 0x0a) ||
  1450. (frsubtype == 0x0b))) {
  1451. if (hdrlen != WLAN_HDR_A3_QOS_LEN && hdrlen != WLAN_HDR_A4_QOS_LEN)
  1452. hdrlen += 2;
  1453. qc_exists = 1;
  1454. } else
  1455. qc_exists = 0;
  1456. /* now, decrypt pframe with hdrlen offset and plen long */
  1457. payload_index = hdrlen + 8; /* 8 is for extiv */
  1458. for (i = 0; i < num_blocks; i++) {
  1459. construct_ctr_preload(
  1460. ctr_preload,
  1461. a4_exists,
  1462. qc_exists,
  1463. pframe,
  1464. pn_vector,
  1465. i + 1,
  1466. frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1467. );
  1468. aes128k128d(key, ctr_preload, aes_out);
  1469. bitwise_xor(aes_out, &pframe[payload_index], chain_buffer);
  1470. for (j = 0; j < 16; j++)
  1471. pframe[payload_index++] = chain_buffer[j];
  1472. }
  1473. if (payload_remainder > 0) { /* If there is a short final block, then pad it,*/
  1474. /* encrypt it and copy the unpadded part back */
  1475. construct_ctr_preload(
  1476. ctr_preload,
  1477. a4_exists,
  1478. qc_exists,
  1479. pframe,
  1480. pn_vector,
  1481. num_blocks + 1,
  1482. frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1483. );
  1484. for (j = 0; j < 16; j++)
  1485. padded_buffer[j] = 0x00;
  1486. for (j = 0; j < payload_remainder; j++)
  1487. padded_buffer[j] = pframe[payload_index + j];
  1488. aes128k128d(key, ctr_preload, aes_out);
  1489. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1490. for (j = 0; j < payload_remainder; j++)
  1491. pframe[payload_index++] = chain_buffer[j];
  1492. }
  1493. /* start to calculate the mic */
  1494. if ((hdrlen + plen + 8) <= MAX_MSG_SIZE)
  1495. _rtw_memcpy((void *)message, pframe, (hdrlen + plen + 8)); /* 8 is for ext iv len */
  1496. pn_vector[0] = pframe[hdrlen];
  1497. pn_vector[1] = pframe[hdrlen + 1];
  1498. pn_vector[2] = pframe[hdrlen + 4];
  1499. pn_vector[3] = pframe[hdrlen + 5];
  1500. pn_vector[4] = pframe[hdrlen + 6];
  1501. pn_vector[5] = pframe[hdrlen + 7];
  1502. construct_mic_iv(
  1503. mic_iv,
  1504. qc_exists,
  1505. a4_exists,
  1506. message,
  1507. plen - 8,
  1508. pn_vector,
  1509. frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1510. );
  1511. construct_mic_header1(
  1512. mic_header1,
  1513. hdrlen,
  1514. message,
  1515. frtype /* add for CONFIG_IEEE80211W, none 11w also can use */
  1516. );
  1517. construct_mic_header2(
  1518. mic_header2,
  1519. message,
  1520. a4_exists,
  1521. qc_exists
  1522. );
  1523. payload_remainder = (plen - 8) % 16;
  1524. num_blocks = (plen - 8) / 16;
  1525. /* Find start of payload */
  1526. payload_index = (hdrlen + 8);
  1527. /* Calculate MIC */
  1528. aes128k128d(key, mic_iv, aes_out);
  1529. bitwise_xor(aes_out, mic_header1, chain_buffer);
  1530. aes128k128d(key, chain_buffer, aes_out);
  1531. bitwise_xor(aes_out, mic_header2, chain_buffer);
  1532. aes128k128d(key, chain_buffer, aes_out);
  1533. for (i = 0; i < num_blocks; i++) {
  1534. bitwise_xor(aes_out, &message[payload_index], chain_buffer);
  1535. payload_index += 16;
  1536. aes128k128d(key, chain_buffer, aes_out);
  1537. }
  1538. /* Add on the final payload block if it needs padding */
  1539. if (payload_remainder > 0) {
  1540. for (j = 0; j < 16; j++)
  1541. padded_buffer[j] = 0x00;
  1542. for (j = 0; j < payload_remainder; j++)
  1543. padded_buffer[j] = message[payload_index++];
  1544. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1545. aes128k128d(key, chain_buffer, aes_out);
  1546. }
  1547. for (j = 0 ; j < 8; j++)
  1548. mic[j] = aes_out[j];
  1549. /* Insert MIC into payload */
  1550. for (j = 0; j < 8; j++)
  1551. message[payload_index + j] = mic[j];
  1552. payload_index = hdrlen + 8;
  1553. for (i = 0; i < num_blocks; i++) {
  1554. construct_ctr_preload(
  1555. ctr_preload,
  1556. a4_exists,
  1557. qc_exists,
  1558. message,
  1559. pn_vector,
  1560. i + 1,
  1561. frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */
  1562. aes128k128d(key, ctr_preload, aes_out);
  1563. bitwise_xor(aes_out, &message[payload_index], chain_buffer);
  1564. for (j = 0; j < 16; j++)
  1565. message[payload_index++] = chain_buffer[j];
  1566. }
  1567. if (payload_remainder > 0) { /* If there is a short final block, then pad it,*/
  1568. /* encrypt it and copy the unpadded part back */
  1569. construct_ctr_preload(
  1570. ctr_preload,
  1571. a4_exists,
  1572. qc_exists,
  1573. message,
  1574. pn_vector,
  1575. num_blocks + 1,
  1576. frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */
  1577. for (j = 0; j < 16; j++)
  1578. padded_buffer[j] = 0x00;
  1579. for (j = 0; j < payload_remainder; j++)
  1580. padded_buffer[j] = message[payload_index + j];
  1581. aes128k128d(key, ctr_preload, aes_out);
  1582. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1583. for (j = 0; j < payload_remainder; j++)
  1584. message[payload_index++] = chain_buffer[j];
  1585. }
  1586. /* Encrypt the MIC */
  1587. construct_ctr_preload(
  1588. ctr_preload,
  1589. a4_exists,
  1590. qc_exists,
  1591. message,
  1592. pn_vector,
  1593. 0,
  1594. frtype); /* add for CONFIG_IEEE80211W, none 11w also can use */
  1595. for (j = 0; j < 16; j++)
  1596. padded_buffer[j] = 0x00;
  1597. for (j = 0; j < 8; j++)
  1598. padded_buffer[j] = message[j + hdrlen + 8 + plen - 8];
  1599. aes128k128d(key, ctr_preload, aes_out);
  1600. bitwise_xor(aes_out, padded_buffer, chain_buffer);
  1601. for (j = 0; j < 8; j++)
  1602. message[payload_index++] = chain_buffer[j];
  1603. /* compare the mic */
  1604. for (i = 0; i < 8; i++) {
  1605. if (pframe[hdrlen + 8 + plen - 8 + i] != message[hdrlen + 8 + plen - 8 + i]) {
  1606. RTW_INFO("aes_decipher:mic check error mic[%d]: pframe(%x) != message(%x)\n",
  1607. i, pframe[hdrlen + 8 + plen - 8 + i], message[hdrlen + 8 + plen - 8 + i]);
  1608. res = _FAIL;
  1609. }
  1610. }
  1611. return res;
  1612. }
  1613. u32 rtw_aes_decrypt(_adapter *padapter, u8 *precvframe)
  1614. {
  1615. /* exclude ICV */
  1616. /*static*/
  1617. /* unsigned char message[MAX_MSG_SIZE]; */
  1618. /* Intermediate Buffers */
  1619. sint length;
  1620. u8 *pframe, *prwskey; /* , *payload,*iv */
  1621. struct sta_info *stainfo;
  1622. struct rx_pkt_attrib *prxattrib = &((union recv_frame *)precvframe)->u.hdr.attrib;
  1623. struct security_priv *psecuritypriv = &padapter->securitypriv;
  1624. /* struct recv_priv *precvpriv=&padapter->recvpriv; */
  1625. u32 res = _SUCCESS;
  1626. pframe = (unsigned char *)((union recv_frame *)precvframe)->u.hdr.rx_data;
  1627. /* 4 start to encrypt each fragment */
  1628. if ((prxattrib->encrypt == _AES_)) {
  1629. stainfo = rtw_get_stainfo(&padapter->stapriv , &prxattrib->ta[0]);
  1630. if (stainfo != NULL) {
  1631. if (IS_MCAST(prxattrib->ra)) {
  1632. static systime start = 0;
  1633. static u32 no_gkey_bc_cnt = 0;
  1634. static u32 no_gkey_mc_cnt = 0;
  1635. /* RTW_INFO("rx bc/mc packets, to perform sw rtw_aes_decrypt\n"); */
  1636. /* prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey; */
  1637. if ((!MLME_IS_MESH(padapter) && psecuritypriv->binstallGrpkey == _FALSE)
  1638. #ifdef CONFIG_RTW_MESH
  1639. || !(stainfo->gtk_bmp | BIT(prxattrib->key_index))
  1640. #endif
  1641. ) {
  1642. res = _FAIL;
  1643. if (start == 0)
  1644. start = rtw_get_current_time();
  1645. if (is_broadcast_mac_addr(prxattrib->ra))
  1646. no_gkey_bc_cnt++;
  1647. else
  1648. no_gkey_mc_cnt++;
  1649. if (rtw_get_passing_time_ms(start) > 1000) {
  1650. if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
  1651. RTW_PRINT(FUNC_ADPT_FMT" no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
  1652. FUNC_ADPT_ARG(padapter), no_gkey_bc_cnt, no_gkey_mc_cnt);
  1653. }
  1654. start = rtw_get_current_time();
  1655. no_gkey_bc_cnt = 0;
  1656. no_gkey_mc_cnt = 0;
  1657. }
  1658. goto exit;
  1659. }
  1660. if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
  1661. RTW_PRINT(FUNC_ADPT_FMT" gkey installed. no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
  1662. FUNC_ADPT_ARG(padapter), no_gkey_bc_cnt, no_gkey_mc_cnt);
  1663. }
  1664. start = 0;
  1665. no_gkey_bc_cnt = 0;
  1666. no_gkey_mc_cnt = 0;
  1667. #ifdef CONFIG_RTW_MESH
  1668. if (MLME_IS_MESH(padapter)) {
  1669. /* TODO: multiple GK? */
  1670. prwskey = &stainfo->gtk.skey[0];
  1671. } else
  1672. #endif
  1673. {
  1674. prwskey = psecuritypriv->dot118021XGrpKey[prxattrib->key_index].skey;
  1675. if (psecuritypriv->dot118021XGrpKeyid != prxattrib->key_index) {
  1676. RTW_DBG("not match packet_index=%d, install_index=%d\n"
  1677. , prxattrib->key_index, psecuritypriv->dot118021XGrpKeyid);
  1678. res = _FAIL;
  1679. goto exit;
  1680. }
  1681. }
  1682. } else
  1683. prwskey = &stainfo->dot118021x_UncstKey.skey[0];
  1684. length = ((union recv_frame *)precvframe)->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len;
  1685. #if 0
  1686. /* add for CONFIG_IEEE80211W, debug */
  1687. if (0)
  1688. printk("@@@@@@@@@@@@@@@@@@ length=%d, prxattrib->hdrlen=%d, prxattrib->pkt_len=%d\n"
  1689. , length, prxattrib->hdrlen, prxattrib->pkt_len);
  1690. if (0) {
  1691. int no;
  1692. /* test print PSK */
  1693. printk("PSK key below:\n");
  1694. for (no = 0; no < 16; no++)
  1695. printk(" %02x ", prwskey[no]);
  1696. printk("\n");
  1697. }
  1698. if (0) {
  1699. int no;
  1700. /* test print PSK */
  1701. printk("frame:\n");
  1702. for (no = 0; no < prxattrib->pkt_len; no++)
  1703. printk(" %02x ", pframe[no]);
  1704. printk("\n");
  1705. }
  1706. #endif
  1707. res = aes_decipher(prwskey, prxattrib->hdrlen, pframe, length);
  1708. AES_SW_DEC_CNT_INC(psecuritypriv, prxattrib->ra);
  1709. } else {
  1710. res = _FAIL;
  1711. }
  1712. }
  1713. exit:
  1714. return res;
  1715. }
  1716. #ifdef CONFIG_IEEE80211W
  1717. u32 rtw_BIP_verify(_adapter *padapter, u8 *whdr_pos, sint flen
  1718. , const u8 *key, u16 keyid, u64* ipn)
  1719. {
  1720. u8 *BIP_AAD, *mme;
  1721. u32 res = _FAIL;
  1722. uint len, ori_len;
  1723. u16 pkt_keyid = 0;
  1724. u64 pkt_ipn = 0;
  1725. struct rtw_ieee80211_hdr *pwlanhdr;
  1726. u8 mic[16];
  1727. mme = whdr_pos + flen - 18;
  1728. if (*mme != _MME_IE_)
  1729. return RTW_RX_HANDLED;
  1730. /* copy key index */
  1731. _rtw_memcpy(&pkt_keyid, mme + 2, 2);
  1732. pkt_keyid = le16_to_cpu(pkt_keyid);
  1733. if (pkt_keyid != keyid) {
  1734. RTW_INFO("BIP key index error!\n");
  1735. return _FAIL;
  1736. }
  1737. /* save packet number */
  1738. _rtw_memcpy(&pkt_ipn, mme + 4, 6);
  1739. pkt_ipn = le64_to_cpu(pkt_ipn);
  1740. /* BIP packet number should bigger than previous BIP packet */
  1741. if (pkt_ipn <= *ipn) { /* wrap around? */
  1742. RTW_INFO("replay BIP packet\n");
  1743. return _FAIL;
  1744. }
  1745. ori_len = flen - WLAN_HDR_A3_LEN + BIP_AAD_SIZE;
  1746. BIP_AAD = rtw_zmalloc(ori_len);
  1747. if (BIP_AAD == NULL) {
  1748. RTW_INFO("BIP AAD allocate fail\n");
  1749. return _FAIL;
  1750. }
  1751. /* mapping to wlan header */
  1752. pwlanhdr = (struct rtw_ieee80211_hdr *)whdr_pos;
  1753. /* save the frame body + MME */
  1754. _rtw_memcpy(BIP_AAD + BIP_AAD_SIZE, whdr_pos + WLAN_HDR_A3_LEN, flen - WLAN_HDR_A3_LEN);
  1755. /* point mme to the copy */
  1756. mme = BIP_AAD + ori_len - 18;
  1757. /* clear the MIC field of MME to zero */
  1758. _rtw_memset(mme + 10, 0, 8);
  1759. /* conscruct AAD, copy frame control field */
  1760. _rtw_memcpy(BIP_AAD, &pwlanhdr->frame_ctl, 2);
  1761. ClearRetry(BIP_AAD);
  1762. ClearPwrMgt(BIP_AAD);
  1763. ClearMData(BIP_AAD);
  1764. /* conscruct AAD, copy address 1 to address 3 */
  1765. _rtw_memcpy(BIP_AAD + 2, pwlanhdr->addr1, 18);
  1766. if (omac1_aes_128(key, BIP_AAD, ori_len, mic))
  1767. goto BIP_exit;
  1768. #if 0
  1769. /* management packet content */
  1770. {
  1771. int pp;
  1772. RTW_INFO("pkt: ");
  1773. for (pp = 0; pp < flen; pp++)
  1774. printk(" %02x ", whdr_pos[pp]);
  1775. RTW_INFO("\n");
  1776. /* BIP AAD + management frame body + MME(MIC is zero) */
  1777. RTW_INFO("AAD+PKT: ");
  1778. for (pp = 0; pp < ori_len; pp++)
  1779. RTW_INFO(" %02x ", BIP_AAD[pp]);
  1780. RTW_INFO("\n");
  1781. /* show the MIC result */
  1782. RTW_INFO("mic: ");
  1783. for (pp = 0; pp < 16; pp++)
  1784. RTW_INFO(" %02x ", mic[pp]);
  1785. RTW_INFO("\n");
  1786. }
  1787. #endif
  1788. /* MIC field should be last 8 bytes of packet (packet without FCS) */
  1789. if (_rtw_memcmp(mic, whdr_pos + flen - 8, 8)) {
  1790. *ipn = pkt_ipn;
  1791. res = _SUCCESS;
  1792. } else
  1793. RTW_INFO("BIP MIC error!\n");
  1794. BIP_exit:
  1795. rtw_mfree(BIP_AAD, ori_len);
  1796. return res;
  1797. }
  1798. #endif /* CONFIG_IEEE80211W */
  1799. #ifndef PLATFORM_FREEBSD
  1800. #if defined(CONFIG_TDLS)
  1801. /* compress 512-bits */
  1802. static int sha256_compress(struct sha256_state *md, unsigned char *buf)
  1803. {
  1804. u32 S[8], W[64], t0, t1;
  1805. u32 t;
  1806. int i;
  1807. /* copy state into S */
  1808. for (i = 0; i < 8; i++)
  1809. S[i] = md->state[i];
  1810. /* copy the state into 512-bits into W[0..15] */
  1811. for (i = 0; i < 16; i++)
  1812. W[i] = WPA_GET_BE32(buf + (4 * i));
  1813. /* fill W[16..63] */
  1814. for (i = 16; i < 64; i++) {
  1815. W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
  1816. W[i - 16];
  1817. }
  1818. /* Compress */
  1819. #define RND(a, b, c, d, e, f, g, h, i) do {\
  1820. t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
  1821. t1 = Sigma0(a) + Maj(a, b, c); \
  1822. d += t0; \
  1823. h = t0 + t1; \
  1824. } while (0)
  1825. for (i = 0; i < 64; ++i) {
  1826. RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
  1827. t = S[7];
  1828. S[7] = S[6];
  1829. S[6] = S[5];
  1830. S[5] = S[4];
  1831. S[4] = S[3];
  1832. S[3] = S[2];
  1833. S[2] = S[1];
  1834. S[1] = S[0];
  1835. S[0] = t;
  1836. }
  1837. /* feedback */
  1838. for (i = 0; i < 8; i++)
  1839. md->state[i] = md->state[i] + S[i];
  1840. return 0;
  1841. }
  1842. /* Initialize the hash state */
  1843. static void sha256_init(struct sha256_state *md)
  1844. {
  1845. md->curlen = 0;
  1846. md->length = 0;
  1847. md->state[0] = 0x6A09E667UL;
  1848. md->state[1] = 0xBB67AE85UL;
  1849. md->state[2] = 0x3C6EF372UL;
  1850. md->state[3] = 0xA54FF53AUL;
  1851. md->state[4] = 0x510E527FUL;
  1852. md->state[5] = 0x9B05688CUL;
  1853. md->state[6] = 0x1F83D9ABUL;
  1854. md->state[7] = 0x5BE0CD19UL;
  1855. }
  1856. /**
  1857. Process a block of memory though the hash
  1858. @param md The hash state
  1859. @param in The data to hash
  1860. @param inlen The length of the data (octets)
  1861. @return CRYPT_OK if successful
  1862. */
  1863. static int sha256_process(struct sha256_state *md, unsigned char *in,
  1864. unsigned long inlen)
  1865. {
  1866. unsigned long n;
  1867. #define block_size 64
  1868. if (md->curlen >= sizeof(md->buf))
  1869. return -1;
  1870. while (inlen > 0) {
  1871. if (md->curlen == 0 && inlen >= block_size) {
  1872. if (sha256_compress(md, (unsigned char *) in) < 0)
  1873. return -1;
  1874. md->length += block_size * 8;
  1875. in += block_size;
  1876. inlen -= block_size;
  1877. } else {
  1878. n = MIN(inlen, (block_size - md->curlen));
  1879. _rtw_memcpy(md->buf + md->curlen, in, n);
  1880. md->curlen += n;
  1881. in += n;
  1882. inlen -= n;
  1883. if (md->curlen == block_size) {
  1884. if (sha256_compress(md, md->buf) < 0)
  1885. return -1;
  1886. md->length += 8 * block_size;
  1887. md->curlen = 0;
  1888. }
  1889. }
  1890. }
  1891. return 0;
  1892. }
  1893. /**
  1894. Terminate the hash to get the digest
  1895. @param md The hash state
  1896. @param out [out] The destination of the hash (32 bytes)
  1897. @return CRYPT_OK if successful
  1898. */
  1899. static int sha256_done(struct sha256_state *md, unsigned char *out)
  1900. {
  1901. int i;
  1902. if (md->curlen >= sizeof(md->buf))
  1903. return -1;
  1904. /* increase the length of the message */
  1905. md->length += md->curlen * 8;
  1906. /* append the '1' bit */
  1907. md->buf[md->curlen++] = (unsigned char) 0x80;
  1908. /* if the length is currently above 56 bytes we append zeros
  1909. * then compress. Then we can fall back to padding zeros and length
  1910. * encoding like normal.
  1911. */
  1912. if (md->curlen > 56) {
  1913. while (md->curlen < 64)
  1914. md->buf[md->curlen++] = (unsigned char) 0;
  1915. sha256_compress(md, md->buf);
  1916. md->curlen = 0;
  1917. }
  1918. /* pad upto 56 bytes of zeroes */
  1919. while (md->curlen < 56)
  1920. md->buf[md->curlen++] = (unsigned char) 0;
  1921. /* store length */
  1922. WPA_PUT_BE64(md->buf + 56, md->length);
  1923. sha256_compress(md, md->buf);
  1924. /* copy output */
  1925. for (i = 0; i < 8; i++)
  1926. WPA_PUT_BE32(out + (4 * i), md->state[i]);
  1927. return 0;
  1928. }
  1929. /**
  1930. * sha256_vector - SHA256 hash for data vector
  1931. * @num_elem: Number of elements in the data vector
  1932. * @addr: Pointers to the data areas
  1933. * @len: Lengths of the data blocks
  1934. * @mac: Buffer for the hash
  1935. * Returns: 0 on success, -1 of failure
  1936. */
  1937. static int sha256_vector(size_t num_elem, u8 *addr[], size_t *len,
  1938. u8 *mac)
  1939. {
  1940. struct sha256_state ctx;
  1941. size_t i;
  1942. sha256_init(&ctx);
  1943. for (i = 0; i < num_elem; i++)
  1944. if (sha256_process(&ctx, addr[i], len[i]))
  1945. return -1;
  1946. if (sha256_done(&ctx, mac))
  1947. return -1;
  1948. return 0;
  1949. }
  1950. static u8 os_strlen(const char *s)
  1951. {
  1952. const char *p = s;
  1953. while (*p)
  1954. p++;
  1955. return p - s;
  1956. }
  1957. #endif
  1958. #if defined(CONFIG_TDLS) || defined(CONFIG_RTW_MESH_AEK)
  1959. static int os_memcmp(const void *s1, const void *s2, u8 n)
  1960. {
  1961. const unsigned char *p1 = s1, *p2 = s2;
  1962. if (n == 0)
  1963. return 0;
  1964. while (*p1 == *p2) {
  1965. p1++;
  1966. p2++;
  1967. n--;
  1968. if (n == 0)
  1969. return 0;
  1970. }
  1971. return *p1 - *p2;
  1972. }
  1973. #endif
  1974. /**
  1975. * hmac_sha256_vector - HMAC-SHA256 over data vector (RFC 2104)
  1976. * @key: Key for HMAC operations
  1977. * @key_len: Length of the key in bytes
  1978. * @num_elem: Number of elements in the data vector
  1979. * @addr: Pointers to the data areas
  1980. * @len: Lengths of the data blocks
  1981. * @mac: Buffer for the hash (32 bytes)
  1982. */
  1983. #if defined(CONFIG_TDLS)
  1984. static void hmac_sha256_vector(u8 *key, size_t key_len, size_t num_elem,
  1985. u8 *addr[], size_t *len, u8 *mac)
  1986. {
  1987. unsigned char k_pad[64]; /* padding - key XORd with ipad/opad */
  1988. unsigned char tk[32];
  1989. u8 *_addr[6];
  1990. size_t _len[6], i;
  1991. if (num_elem > 5) {
  1992. /*
  1993. * Fixed limit on the number of fragments to avoid having to
  1994. * allocate memory (which could fail).
  1995. */
  1996. return;
  1997. }
  1998. /* if key is longer than 64 bytes reset it to key = SHA256(key) */
  1999. if (key_len > 64) {
  2000. sha256_vector(1, &key, &key_len, tk);
  2001. key = tk;
  2002. key_len = 32;
  2003. }
  2004. /* the HMAC_SHA256 transform looks like:
  2005. *
  2006. * SHA256(K XOR opad, SHA256(K XOR ipad, text))
  2007. *
  2008. * where K is an n byte key
  2009. * ipad is the byte 0x36 repeated 64 times
  2010. * opad is the byte 0x5c repeated 64 times
  2011. * and text is the data being protected */
  2012. /* start out by storing key in ipad */
  2013. _rtw_memset(k_pad, 0, sizeof(k_pad));
  2014. _rtw_memcpy(k_pad, key, key_len);
  2015. /* XOR key with ipad values */
  2016. for (i = 0; i < 64; i++)
  2017. k_pad[i] ^= 0x36;
  2018. /* perform inner SHA256 */
  2019. _addr[0] = k_pad;
  2020. _len[0] = 64;
  2021. for (i = 0; i < num_elem; i++) {
  2022. _addr[i + 1] = addr[i];
  2023. _len[i + 1] = len[i];
  2024. }
  2025. sha256_vector(1 + num_elem, _addr, _len, mac);
  2026. _rtw_memset(k_pad, 0, sizeof(k_pad));
  2027. _rtw_memcpy(k_pad, key, key_len);
  2028. /* XOR key with opad values */
  2029. for (i = 0; i < 64; i++)
  2030. k_pad[i] ^= 0x5c;
  2031. /* perform outer SHA256 */
  2032. _addr[0] = k_pad;
  2033. _len[0] = 64;
  2034. _addr[1] = mac;
  2035. _len[1] = 32;
  2036. sha256_vector(2, _addr, _len, mac);
  2037. }
  2038. #endif /* CONFIG_TDLS */
  2039. #endif /* PLATFORM_FREEBSD */
  2040. /**
  2041. * sha256_prf - SHA256-based Pseudo-Random Function (IEEE 802.11r, 8.5.1.5.2)
  2042. * @key: Key for PRF
  2043. * @key_len: Length of the key in bytes
  2044. * @label: A unique label for each purpose of the PRF
  2045. * @data: Extra data to bind into the key
  2046. * @data_len: Length of the data
  2047. * @buf: Buffer for the generated pseudo-random key
  2048. * @buf_len: Number of bytes of key to generate
  2049. *
  2050. * This function is used to derive new, cryptographically separate keys from a
  2051. * given key.
  2052. */
  2053. #ifndef PLATFORM_FREEBSD /* Baron */
  2054. #if defined(CONFIG_TDLS)
  2055. static void sha256_prf(u8 *key, size_t key_len, char *label,
  2056. u8 *data, size_t data_len, u8 *buf, size_t buf_len)
  2057. {
  2058. u16 counter = 1;
  2059. size_t pos, plen;
  2060. u8 hash[SHA256_MAC_LEN];
  2061. u8 *addr[4];
  2062. size_t len[4];
  2063. u8 counter_le[2], length_le[2];
  2064. addr[0] = counter_le;
  2065. len[0] = 2;
  2066. addr[1] = (u8 *) label;
  2067. len[1] = os_strlen(label);
  2068. addr[2] = data;
  2069. len[2] = data_len;
  2070. addr[3] = length_le;
  2071. len[3] = sizeof(length_le);
  2072. WPA_PUT_LE16(length_le, buf_len * 8);
  2073. pos = 0;
  2074. while (pos < buf_len) {
  2075. plen = buf_len - pos;
  2076. WPA_PUT_LE16(counter_le, counter);
  2077. if (plen >= SHA256_MAC_LEN) {
  2078. hmac_sha256_vector(key, key_len, 4, addr, len,
  2079. &buf[pos]);
  2080. pos += SHA256_MAC_LEN;
  2081. } else {
  2082. hmac_sha256_vector(key, key_len, 4, addr, len, hash);
  2083. _rtw_memcpy(&buf[pos], hash, plen);
  2084. break;
  2085. }
  2086. counter++;
  2087. }
  2088. }
  2089. #endif
  2090. #endif /* PLATFORM_FREEBSD Baron */
  2091. /* AES tables*/
  2092. const u32 Te0[256] = {
  2093. 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
  2094. 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
  2095. 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
  2096. 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
  2097. 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
  2098. 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
  2099. 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
  2100. 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
  2101. 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
  2102. 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
  2103. 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
  2104. 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
  2105. 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
  2106. 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
  2107. 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
  2108. 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
  2109. 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
  2110. 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
  2111. 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
  2112. 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
  2113. 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
  2114. 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
  2115. 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
  2116. 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
  2117. 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
  2118. 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
  2119. 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
  2120. 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
  2121. 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
  2122. 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
  2123. 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
  2124. 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
  2125. 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
  2126. 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
  2127. 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
  2128. 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
  2129. 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
  2130. 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
  2131. 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
  2132. 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
  2133. 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
  2134. 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
  2135. 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
  2136. 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
  2137. 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
  2138. 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
  2139. 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
  2140. 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
  2141. 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
  2142. 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
  2143. 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
  2144. 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
  2145. 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
  2146. 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
  2147. 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
  2148. 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
  2149. 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
  2150. 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
  2151. 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
  2152. 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
  2153. 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
  2154. 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
  2155. 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
  2156. 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
  2157. };
  2158. const u32 Td0[256] = {
  2159. 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
  2160. 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
  2161. 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
  2162. 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
  2163. 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
  2164. 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
  2165. 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
  2166. 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
  2167. 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
  2168. 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
  2169. 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
  2170. 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
  2171. 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
  2172. 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
  2173. 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
  2174. 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
  2175. 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
  2176. 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
  2177. 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
  2178. 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
  2179. 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
  2180. 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
  2181. 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
  2182. 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
  2183. 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
  2184. 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
  2185. 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
  2186. 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
  2187. 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
  2188. 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
  2189. 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
  2190. 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
  2191. 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
  2192. 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
  2193. 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
  2194. 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
  2195. 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
  2196. 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
  2197. 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
  2198. 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
  2199. 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
  2200. 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
  2201. 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
  2202. 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
  2203. 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
  2204. 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
  2205. 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
  2206. 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
  2207. 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
  2208. 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
  2209. 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
  2210. 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
  2211. 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
  2212. 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
  2213. 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
  2214. 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
  2215. 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
  2216. 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
  2217. 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
  2218. 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
  2219. 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
  2220. 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
  2221. 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
  2222. 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
  2223. };
  2224. const u8 Td4s[256] = {
  2225. 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
  2226. 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
  2227. 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
  2228. 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU,
  2229. 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU,
  2230. 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU,
  2231. 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U,
  2232. 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U,
  2233. 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U,
  2234. 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U,
  2235. 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU,
  2236. 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U,
  2237. 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU,
  2238. 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U,
  2239. 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U,
  2240. 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU,
  2241. 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU,
  2242. 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U,
  2243. 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U,
  2244. 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU,
  2245. 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U,
  2246. 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU,
  2247. 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U,
  2248. 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U,
  2249. 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U,
  2250. 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU,
  2251. 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU,
  2252. 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU,
  2253. 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U,
  2254. 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U,
  2255. 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U,
  2256. 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
  2257. };
  2258. const u8 rcons[] = {
  2259. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
  2260. /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
  2261. };
  2262. /**
  2263. * Expand the cipher key into the encryption key schedule.
  2264. *
  2265. * @return the number of rounds for the given cipher key size.
  2266. */
  2267. #ifndef PLATFORM_FREEBSD /* Baron */
  2268. static void rijndaelKeySetupEnc(u32 rk[/*44*/], const u8 cipherKey[])
  2269. {
  2270. int i;
  2271. u32 temp;
  2272. rk[0] = GETU32(cipherKey);
  2273. rk[1] = GETU32(cipherKey + 4);
  2274. rk[2] = GETU32(cipherKey + 8);
  2275. rk[3] = GETU32(cipherKey + 12);
  2276. for (i = 0; i < 10; i++) {
  2277. temp = rk[3];
  2278. rk[4] = rk[0] ^
  2279. TE421(temp) ^ TE432(temp) ^ TE443(temp) ^ TE414(temp) ^
  2280. RCON(i);
  2281. rk[5] = rk[1] ^ rk[4];
  2282. rk[6] = rk[2] ^ rk[5];
  2283. rk[7] = rk[3] ^ rk[6];
  2284. rk += 4;
  2285. }
  2286. }
  2287. static void rijndaelEncrypt(u32 rk[/*44*/], u8 pt[16], u8 ct[16])
  2288. {
  2289. u32 s0, s1, s2, s3, t0, t1, t2, t3;
  2290. int Nr = 10;
  2291. #ifndef FULL_UNROLL
  2292. int r;
  2293. #endif /* ?FULL_UNROLL */
  2294. /*
  2295. * map byte array block to cipher state
  2296. * and add initial round key:
  2297. */
  2298. s0 = GETU32(pt) ^ rk[0];
  2299. s1 = GETU32(pt + 4) ^ rk[1];
  2300. s2 = GETU32(pt + 8) ^ rk[2];
  2301. s3 = GETU32(pt + 12) ^ rk[3];
  2302. #define ROUND(i, d, s) do {\
  2303. d##0 = TE0(s##0) ^ TE1(s##1) ^ TE2(s##2) ^ TE3(s##3) ^ rk[4 * i]; \
  2304. d##1 = TE0(s##1) ^ TE1(s##2) ^ TE2(s##3) ^ TE3(s##0) ^ rk[4 * i + 1]; \
  2305. d##2 = TE0(s##2) ^ TE1(s##3) ^ TE2(s##0) ^ TE3(s##1) ^ rk[4 * i + 2]; \
  2306. d##3 = TE0(s##3) ^ TE1(s##0) ^ TE2(s##1) ^ TE3(s##2) ^ rk[4 * i + 3]; \
  2307. } while (0)
  2308. #ifdef FULL_UNROLL
  2309. ROUND(1, t, s);
  2310. ROUND(2, s, t);
  2311. ROUND(3, t, s);
  2312. ROUND(4, s, t);
  2313. ROUND(5, t, s);
  2314. ROUND(6, s, t);
  2315. ROUND(7, t, s);
  2316. ROUND(8, s, t);
  2317. ROUND(9, t, s);
  2318. rk += Nr << 2;
  2319. #else /* !FULL_UNROLL */
  2320. /* Nr - 1 full rounds: */
  2321. r = Nr >> 1;
  2322. for (;;) {
  2323. ROUND(1, t, s);
  2324. rk += 8;
  2325. if (--r == 0)
  2326. break;
  2327. ROUND(0, s, t);
  2328. }
  2329. #endif /* ?FULL_UNROLL */
  2330. #undef ROUND
  2331. /*
  2332. * apply last round and
  2333. * map cipher state to byte array block:
  2334. */
  2335. s0 = TE41(t0) ^ TE42(t1) ^ TE43(t2) ^ TE44(t3) ^ rk[0];
  2336. PUTU32(ct , s0);
  2337. s1 = TE41(t1) ^ TE42(t2) ^ TE43(t3) ^ TE44(t0) ^ rk[1];
  2338. PUTU32(ct + 4, s1);
  2339. s2 = TE41(t2) ^ TE42(t3) ^ TE43(t0) ^ TE44(t1) ^ rk[2];
  2340. PUTU32(ct + 8, s2);
  2341. s3 = TE41(t3) ^ TE42(t0) ^ TE43(t1) ^ TE44(t2) ^ rk[3];
  2342. PUTU32(ct + 12, s3);
  2343. }
  2344. static void *aes_encrypt_init(const u8 *key, size_t len)
  2345. {
  2346. u32 *rk;
  2347. if (len != 16)
  2348. return NULL;
  2349. rk = (u32 *)rtw_malloc(AES_PRIV_SIZE);
  2350. if (rk == NULL)
  2351. return NULL;
  2352. rijndaelKeySetupEnc(rk, key);
  2353. return rk;
  2354. }
  2355. static void aes_128_encrypt(void *ctx, u8 *plain, u8 *crypt)
  2356. {
  2357. rijndaelEncrypt(ctx, plain, crypt);
  2358. }
  2359. static void gf_mulx(u8 *pad)
  2360. {
  2361. int i, carry;
  2362. carry = pad[0] & 0x80;
  2363. for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
  2364. pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
  2365. pad[AES_BLOCK_SIZE - 1] <<= 1;
  2366. if (carry)
  2367. pad[AES_BLOCK_SIZE - 1] ^= 0x87;
  2368. }
  2369. static void aes_encrypt_deinit(void *ctx)
  2370. {
  2371. _rtw_memset(ctx, 0, AES_PRIV_SIZE);
  2372. rtw_mfree(ctx, AES_PRIV_SIZE);
  2373. }
  2374. /**
  2375. * omac1_aes_128_vector - One-Key CBC MAC (OMAC1) hash with AES-128
  2376. * @key: 128-bit key for the hash operation
  2377. * @num_elem: Number of elements in the data vector
  2378. * @addr: Pointers to the data areas
  2379. * @len: Lengths of the data blocks
  2380. * @mac: Buffer for MAC (128 bits, i.e., 16 bytes)
  2381. * Returns: 0 on success, -1 on failure
  2382. *
  2383. * This is a mode for using block cipher (AES in this case) for authentication.
  2384. * OMAC1 was standardized with the name CMAC by NIST in a Special Publication
  2385. * (SP) 800-38B.
  2386. */
  2387. static int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  2388. const u8 *addr[], const size_t *len, u8 *mac)
  2389. {
  2390. void *ctx;
  2391. u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
  2392. const u8 *pos, *end;
  2393. size_t i, e, left, total_len;
  2394. ctx = aes_encrypt_init(key, 16);
  2395. if (ctx == NULL)
  2396. return -1;
  2397. _rtw_memset(cbc, 0, AES_BLOCK_SIZE);
  2398. total_len = 0;
  2399. for (e = 0; e < num_elem; e++)
  2400. total_len += len[e];
  2401. left = total_len;
  2402. e = 0;
  2403. pos = addr[0];
  2404. end = pos + len[0];
  2405. while (left >= AES_BLOCK_SIZE) {
  2406. for (i = 0; i < AES_BLOCK_SIZE; i++) {
  2407. cbc[i] ^= *pos++;
  2408. if (pos >= end) {
  2409. e++;
  2410. pos = addr[e];
  2411. end = pos + len[e];
  2412. }
  2413. }
  2414. if (left > AES_BLOCK_SIZE)
  2415. aes_128_encrypt(ctx, cbc, cbc);
  2416. left -= AES_BLOCK_SIZE;
  2417. }
  2418. _rtw_memset(pad, 0, AES_BLOCK_SIZE);
  2419. aes_128_encrypt(ctx, pad, pad);
  2420. gf_mulx(pad);
  2421. if (left || total_len == 0) {
  2422. for (i = 0; i < left; i++) {
  2423. cbc[i] ^= *pos++;
  2424. if (pos >= end) {
  2425. e++;
  2426. pos = addr[e];
  2427. end = pos + len[e];
  2428. }
  2429. }
  2430. cbc[left] ^= 0x80;
  2431. gf_mulx(pad);
  2432. }
  2433. for (i = 0; i < AES_BLOCK_SIZE; i++)
  2434. pad[i] ^= cbc[i];
  2435. aes_128_encrypt(ctx, pad, mac);
  2436. aes_encrypt_deinit(ctx);
  2437. return 0;
  2438. }
  2439. /**
  2440. * omac1_aes_128 - One-Key CBC MAC (OMAC1) hash with AES-128 (aka AES-CMAC)
  2441. * @key: 128-bit key for the hash operation
  2442. * @data: Data buffer for which a MAC is determined
  2443. * @data_len: Length of data buffer in bytes
  2444. * @mac: Buffer for MAC (128 bits, i.e., 16 bytes)
  2445. * Returns: 0 on success, -1 on failure
  2446. *
  2447. * This is a mode for using block cipher (AES in this case) for authentication.
  2448. * OMAC1 was standardized with the name CMAC by NIST in a Special Publication
  2449. * (SP) 800-38B.
  2450. */ /* modify for CONFIG_IEEE80211W */
  2451. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  2452. {
  2453. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  2454. }
  2455. #endif /* PLATFORM_FREEBSD Baron */
  2456. #ifdef CONFIG_RTW_MESH_AEK
  2457. /* for AES-SIV */
  2458. #define os_memset _rtw_memset
  2459. #define os_memcpy _rtw_memcpy
  2460. #define os_malloc rtw_malloc
  2461. #define bin_clear_free(bin, len) \
  2462. do { \
  2463. if (bin) { \
  2464. os_memset(bin, 0, len); \
  2465. rtw_mfree(bin, len); \
  2466. } \
  2467. } while (0)
  2468. static const u8 zero[AES_BLOCK_SIZE];
  2469. static void dbl(u8 *pad)
  2470. {
  2471. int i, carry;
  2472. carry = pad[0] & 0x80;
  2473. for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
  2474. pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
  2475. pad[AES_BLOCK_SIZE - 1] <<= 1;
  2476. if (carry)
  2477. pad[AES_BLOCK_SIZE - 1] ^= 0x87;
  2478. }
  2479. static void xor(u8 *a, const u8 *b)
  2480. {
  2481. int i;
  2482. for (i = 0; i < AES_BLOCK_SIZE; i++)
  2483. *a++ ^= *b++;
  2484. }
  2485. static void xorend(u8 *a, int alen, const u8 *b, int blen)
  2486. {
  2487. int i;
  2488. if (alen < blen)
  2489. return;
  2490. for (i = 0; i < blen; i++)
  2491. a[alen - blen + i] ^= b[i];
  2492. }
  2493. static void pad_block(u8 *pad, const u8 *addr, size_t len)
  2494. {
  2495. os_memset(pad, 0, AES_BLOCK_SIZE);
  2496. os_memcpy(pad, addr, len);
  2497. if (len < AES_BLOCK_SIZE)
  2498. pad[len] = 0x80;
  2499. }
  2500. static int aes_s2v(const u8 *key, size_t num_elem, const u8 *addr[],
  2501. size_t *len, u8 *mac)
  2502. {
  2503. u8 tmp[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
  2504. u8 *buf = NULL;
  2505. int ret;
  2506. size_t i;
  2507. if (!num_elem) {
  2508. os_memcpy(tmp, zero, sizeof(zero));
  2509. tmp[AES_BLOCK_SIZE - 1] = 1;
  2510. return omac1_aes_128(key, tmp, sizeof(tmp), mac);
  2511. }
  2512. ret = omac1_aes_128(key, zero, sizeof(zero), tmp);
  2513. if (ret)
  2514. return ret;
  2515. for (i = 0; i < num_elem - 1; i++) {
  2516. ret = omac1_aes_128(key, addr[i], len[i], tmp2);
  2517. if (ret)
  2518. return ret;
  2519. dbl(tmp);
  2520. xor(tmp, tmp2);
  2521. }
  2522. if (len[i] >= AES_BLOCK_SIZE) {
  2523. buf = os_malloc(len[i]);
  2524. if (!buf)
  2525. return -ENOMEM;
  2526. os_memcpy(buf, addr[i], len[i]);
  2527. xorend(buf, len[i], tmp, AES_BLOCK_SIZE);
  2528. ret = omac1_aes_128(key, buf, len[i], mac);
  2529. bin_clear_free(buf, len[i]);
  2530. return ret;
  2531. }
  2532. dbl(tmp);
  2533. pad_block(tmp2, addr[i], len[i]);
  2534. xor(tmp, tmp2);
  2535. return omac1_aes_128(key, tmp, sizeof(tmp), mac);
  2536. }
  2537. /**
  2538. * aes_128_ctr_encrypt - AES-128 CTR mode encryption
  2539. * @key: Key for encryption (16 bytes)
  2540. * @nonce: Nonce for counter mode (16 bytes)
  2541. * @data: Data to encrypt in-place
  2542. * @data_len: Length of data in bytes
  2543. * Returns: 0 on success, -1 on failure
  2544. */
  2545. int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
  2546. u8 *data, size_t data_len)
  2547. {
  2548. void *ctx;
  2549. size_t j, len, left = data_len;
  2550. int i;
  2551. u8 *pos = data;
  2552. u8 counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
  2553. ctx = aes_encrypt_init(key, 16);
  2554. if (ctx == NULL)
  2555. return -1;
  2556. os_memcpy(counter, nonce, AES_BLOCK_SIZE);
  2557. while (left > 0) {
  2558. #if 0
  2559. aes_encrypt(ctx, counter, buf);
  2560. #else
  2561. aes_128_encrypt(ctx, counter, buf);
  2562. #endif
  2563. len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE;
  2564. for (j = 0; j < len; j++)
  2565. pos[j] ^= buf[j];
  2566. pos += len;
  2567. left -= len;
  2568. for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
  2569. counter[i]++;
  2570. if (counter[i])
  2571. break;
  2572. }
  2573. }
  2574. aes_encrypt_deinit(ctx);
  2575. return 0;
  2576. }
  2577. int aes_siv_encrypt(const u8 *key, const u8 *pw,
  2578. size_t pwlen, size_t num_elem,
  2579. const u8 *addr[], const size_t *len, u8 *out)
  2580. {
  2581. const u8 *_addr[6];
  2582. size_t _len[6];
  2583. const u8 *k1 = key, *k2 = key + 16;
  2584. u8 v[AES_BLOCK_SIZE];
  2585. size_t i;
  2586. u8 *iv, *crypt_pw;
  2587. if (num_elem > ARRAY_SIZE(_addr) - 1)
  2588. return -1;
  2589. for (i = 0; i < num_elem; i++) {
  2590. _addr[i] = addr[i];
  2591. _len[i] = len[i];
  2592. }
  2593. _addr[num_elem] = pw;
  2594. _len[num_elem] = pwlen;
  2595. if (aes_s2v(k1, num_elem + 1, _addr, _len, v))
  2596. return -1;
  2597. iv = out;
  2598. crypt_pw = out + AES_BLOCK_SIZE;
  2599. os_memcpy(iv, v, AES_BLOCK_SIZE);
  2600. os_memcpy(crypt_pw, pw, pwlen);
  2601. /* zero out 63rd and 31st bits of ctr (from right) */
  2602. v[8] &= 0x7f;
  2603. v[12] &= 0x7f;
  2604. return aes_128_ctr_encrypt(k2, v, crypt_pw, pwlen);
  2605. }
  2606. int aes_siv_decrypt(const u8 *key, const u8 *iv_crypt, size_t iv_c_len,
  2607. size_t num_elem, const u8 *addr[], const size_t *len,
  2608. u8 *out)
  2609. {
  2610. const u8 *_addr[6];
  2611. size_t _len[6];
  2612. const u8 *k1 = key, *k2 = key + 16;
  2613. size_t crypt_len;
  2614. size_t i;
  2615. int ret;
  2616. u8 iv[AES_BLOCK_SIZE];
  2617. u8 check[AES_BLOCK_SIZE];
  2618. if (iv_c_len < AES_BLOCK_SIZE || num_elem > ARRAY_SIZE(_addr) - 1)
  2619. return -1;
  2620. crypt_len = iv_c_len - AES_BLOCK_SIZE;
  2621. for (i = 0; i < num_elem; i++) {
  2622. _addr[i] = addr[i];
  2623. _len[i] = len[i];
  2624. }
  2625. _addr[num_elem] = out;
  2626. _len[num_elem] = crypt_len;
  2627. os_memcpy(iv, iv_crypt, AES_BLOCK_SIZE);
  2628. os_memcpy(out, iv_crypt + AES_BLOCK_SIZE, crypt_len);
  2629. iv[8] &= 0x7f;
  2630. iv[12] &= 0x7f;
  2631. ret = aes_128_ctr_encrypt(k2, iv, out, crypt_len);
  2632. if (ret)
  2633. return ret;
  2634. ret = aes_s2v(k1, num_elem + 1, _addr, _len, check);
  2635. if (ret)
  2636. return ret;
  2637. if (os_memcmp(check, iv_crypt, AES_BLOCK_SIZE) == 0)
  2638. return 0;
  2639. return -1;
  2640. }
  2641. #endif /* CONFIG_RTW_MESH_AEK */
  2642. #ifdef CONFIG_TDLS
  2643. void wpa_tdls_generate_tpk(_adapter *padapter, PVOID sta)
  2644. {
  2645. struct sta_info *psta = (struct sta_info *)sta;
  2646. struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
  2647. u8 *SNonce = psta->SNonce;
  2648. u8 *ANonce = psta->ANonce;
  2649. u8 key_input[SHA256_MAC_LEN];
  2650. u8 *nonce[2];
  2651. size_t len[2];
  2652. u8 data[3 * ETH_ALEN];
  2653. /* IEEE Std 802.11z-2010 8.5.9.1:
  2654. * TPK-Key-Input = SHA-256(min(SNonce, ANonce) || max(SNonce, ANonce))
  2655. */
  2656. len[0] = 32;
  2657. len[1] = 32;
  2658. if (os_memcmp(SNonce, ANonce, 32) < 0) {
  2659. nonce[0] = SNonce;
  2660. nonce[1] = ANonce;
  2661. } else {
  2662. nonce[0] = ANonce;
  2663. nonce[1] = SNonce;
  2664. }
  2665. sha256_vector(2, nonce, len, key_input);
  2666. /*
  2667. * TPK-Key-Data = KDF-N_KEY(TPK-Key-Input, "TDLS PMK",
  2668. * min(MAC_I, MAC_R) || max(MAC_I, MAC_R) || BSSID || N_KEY)
  2669. * TODO: is N_KEY really included in KDF Context and if so, in which
  2670. * presentation format (little endian 16-bit?) is it used? It gets
  2671. * added by the KDF anyway..
  2672. */
  2673. if (os_memcmp(adapter_mac_addr(padapter), psta->cmn.mac_addr, ETH_ALEN) < 0) {
  2674. _rtw_memcpy(data, adapter_mac_addr(padapter), ETH_ALEN);
  2675. _rtw_memcpy(data + ETH_ALEN, psta->cmn.mac_addr, ETH_ALEN);
  2676. } else {
  2677. _rtw_memcpy(data, psta->cmn.mac_addr, ETH_ALEN);
  2678. _rtw_memcpy(data + ETH_ALEN, adapter_mac_addr(padapter), ETH_ALEN);
  2679. }
  2680. _rtw_memcpy(data + 2 * ETH_ALEN, get_bssid(pmlmepriv), ETH_ALEN);
  2681. sha256_prf(key_input, SHA256_MAC_LEN, "TDLS PMK", data, sizeof(data), (u8 *) &psta->tpk, sizeof(psta->tpk));
  2682. }
  2683. /**
  2684. * wpa_tdls_ftie_mic - Calculate TDLS FTIE MIC
  2685. * @kck: TPK-KCK
  2686. * @lnkid: Pointer to the beginning of Link Identifier IE
  2687. * @rsnie: Pointer to the beginning of RSN IE used for handshake
  2688. * @timeoutie: Pointer to the beginning of Timeout IE used for handshake
  2689. * @ftie: Pointer to the beginning of FT IE
  2690. * @mic: Pointer for writing MIC
  2691. *
  2692. * Calculate MIC for TDLS frame.
  2693. */
  2694. int wpa_tdls_ftie_mic(u8 *kck, u8 trans_seq,
  2695. u8 *lnkid, u8 *rsnie, u8 *timeoutie, u8 *ftie,
  2696. u8 *mic)
  2697. {
  2698. u8 *buf, *pos;
  2699. struct wpa_tdls_ftie *_ftie;
  2700. struct wpa_tdls_lnkid *_lnkid;
  2701. int ret;
  2702. int len = 2 * ETH_ALEN + 1 + 2 + lnkid[1] + 2 + rsnie[1] +
  2703. 2 + timeoutie[1] + 2 + ftie[1];
  2704. buf = rtw_zmalloc(len);
  2705. if (!buf) {
  2706. RTW_INFO("TDLS: No memory for MIC calculation\n");
  2707. return -1;
  2708. }
  2709. pos = buf;
  2710. _lnkid = (struct wpa_tdls_lnkid *) lnkid;
  2711. /* 1) TDLS initiator STA MAC address */
  2712. _rtw_memcpy(pos, _lnkid->init_sta, ETH_ALEN);
  2713. pos += ETH_ALEN;
  2714. /* 2) TDLS responder STA MAC address */
  2715. _rtw_memcpy(pos, _lnkid->resp_sta, ETH_ALEN);
  2716. pos += ETH_ALEN;
  2717. /* 3) Transaction Sequence number */
  2718. *pos++ = trans_seq;
  2719. /* 4) Link Identifier IE */
  2720. _rtw_memcpy(pos, lnkid, 2 + lnkid[1]);
  2721. pos += 2 + lnkid[1];
  2722. /* 5) RSN IE */
  2723. _rtw_memcpy(pos, rsnie, 2 + rsnie[1]);
  2724. pos += 2 + rsnie[1];
  2725. /* 6) Timeout Interval IE */
  2726. _rtw_memcpy(pos, timeoutie, 2 + timeoutie[1]);
  2727. pos += 2 + timeoutie[1];
  2728. /* 7) FTIE, with the MIC field of the FTIE set to 0 */
  2729. _rtw_memcpy(pos, ftie, 2 + ftie[1]);
  2730. _ftie = (struct wpa_tdls_ftie *) pos;
  2731. _rtw_memset(_ftie->mic, 0, TDLS_MIC_LEN);
  2732. pos += 2 + ftie[1];
  2733. ret = omac1_aes_128(kck, buf, pos - buf, mic);
  2734. rtw_mfree(buf, len);
  2735. return ret;
  2736. }
  2737. /**
  2738. * wpa_tdls_teardown_ftie_mic - Calculate TDLS TEARDOWN FTIE MIC
  2739. * @kck: TPK-KCK
  2740. * @lnkid: Pointer to the beginning of Link Identifier IE
  2741. * @reason: Reason code of TDLS Teardown
  2742. * @dialog_token: Dialog token that was used in the MIC calculation for TPK Handshake Message 3
  2743. * @trans_seq: Transaction Sequence number (1 octet) which shall be set to the value 4
  2744. * @ftie: Pointer to the beginning of FT IE
  2745. * @mic: Pointer for writing MIC
  2746. *
  2747. * Calculate MIC for TDLS TEARDOWN frame according to Section 10.22.5 in IEEE 802.11 - 2012.
  2748. */
  2749. int wpa_tdls_teardown_ftie_mic(u8 *kck, u8 *lnkid, u16 reason,
  2750. u8 dialog_token, u8 trans_seq, u8 *ftie, u8 *mic)
  2751. {
  2752. u8 *buf, *pos;
  2753. struct wpa_tdls_ftie *_ftie;
  2754. int ret;
  2755. int len = 2 + lnkid[1] + 2 + 1 + 1 + 2 + ftie[1];
  2756. buf = rtw_zmalloc(len);
  2757. if (!buf) {
  2758. RTW_INFO("TDLS: No memory for MIC calculation\n");
  2759. return -1;
  2760. }
  2761. pos = buf;
  2762. /* 1) Link Identifier IE */
  2763. _rtw_memcpy(pos, lnkid, 2 + lnkid[1]);
  2764. pos += 2 + lnkid[1];
  2765. /* 2) Reason Code */
  2766. _rtw_memcpy(pos, (u8 *)&reason, 2);
  2767. pos += 2;
  2768. /* 3) Dialog Token */
  2769. *pos++ = dialog_token;
  2770. /* 4) Transaction Sequence number */
  2771. *pos++ = trans_seq;
  2772. /* 5) FTIE, with the MIC field of the FTIE set to 0 */
  2773. _rtw_memcpy(pos, ftie, 2 + ftie[1]);
  2774. _ftie = (struct wpa_tdls_ftie *) pos;
  2775. _rtw_memset(_ftie->mic, 0, TDLS_MIC_LEN);
  2776. pos += 2 + ftie[1];
  2777. ret = omac1_aes_128(kck, buf, pos - buf, mic);
  2778. rtw_mfree(buf, len);
  2779. return ret;
  2780. }
  2781. int tdls_verify_mic(u8 *kck, u8 trans_seq,
  2782. u8 *lnkid, u8 *rsnie, u8 *timeoutie, u8 *ftie)
  2783. {
  2784. u8 *buf, *pos;
  2785. int len;
  2786. u8 mic[16];
  2787. int ret;
  2788. u8 *rx_ftie, *tmp_ftie;
  2789. if (lnkid == NULL || rsnie == NULL ||
  2790. timeoutie == NULL || ftie == NULL)
  2791. return _FAIL;
  2792. len = 2 * ETH_ALEN + 1 + 2 + 18 + 2 + *(rsnie + 1) + 2 + *(timeoutie + 1) + 2 + *(ftie + 1);
  2793. buf = rtw_zmalloc(len);
  2794. if (buf == NULL)
  2795. return _FAIL;
  2796. pos = buf;
  2797. /* 1) TDLS initiator STA MAC address */
  2798. _rtw_memcpy(pos, lnkid + ETH_ALEN + 2, ETH_ALEN);
  2799. pos += ETH_ALEN;
  2800. /* 2) TDLS responder STA MAC address */
  2801. _rtw_memcpy(pos, lnkid + 2 * ETH_ALEN + 2, ETH_ALEN);
  2802. pos += ETH_ALEN;
  2803. /* 3) Transaction Sequence number */
  2804. *pos++ = trans_seq;
  2805. /* 4) Link Identifier IE */
  2806. _rtw_memcpy(pos, lnkid, 2 + 18);
  2807. pos += 2 + 18;
  2808. /* 5) RSN IE */
  2809. _rtw_memcpy(pos, rsnie, 2 + *(rsnie + 1));
  2810. pos += 2 + *(rsnie + 1);
  2811. /* 6) Timeout Interval IE */
  2812. _rtw_memcpy(pos, timeoutie, 2 + *(timeoutie + 1));
  2813. pos += 2 + *(timeoutie + 1);
  2814. /* 7) FTIE, with the MIC field of the FTIE set to 0 */
  2815. _rtw_memcpy(pos, ftie, 2 + *(ftie + 1));
  2816. pos += 2;
  2817. tmp_ftie = (u8 *)(pos + 2);
  2818. _rtw_memset(tmp_ftie, 0, 16);
  2819. pos += *(ftie + 1);
  2820. ret = omac1_aes_128(kck, buf, pos - buf, mic);
  2821. rtw_mfree(buf, len);
  2822. if (ret)
  2823. return _FAIL;
  2824. rx_ftie = ftie + 4;
  2825. if (os_memcmp(mic, rx_ftie, 16) == 0) {
  2826. /* Valid MIC */
  2827. return _SUCCESS;
  2828. }
  2829. /* Invalid MIC */
  2830. RTW_INFO("[%s] Invalid MIC\n", __FUNCTION__);
  2831. return _FAIL;
  2832. }
  2833. #endif /* CONFIG_TDLS */
  2834. /* Restore HW wep key setting according to key_mask */
  2835. void rtw_sec_restore_wep_key(_adapter *adapter)
  2836. {
  2837. struct security_priv *securitypriv = &(adapter->securitypriv);
  2838. sint keyid;
  2839. if ((_WEP40_ == securitypriv->dot11PrivacyAlgrthm) || (_WEP104_ == securitypriv->dot11PrivacyAlgrthm)) {
  2840. for (keyid = 0; keyid < 4; keyid++) {
  2841. if (securitypriv->key_mask & BIT(keyid)) {
  2842. if (keyid == securitypriv->dot11PrivacyKeyIndex)
  2843. rtw_set_key(adapter, securitypriv, keyid, 1, _FALSE);
  2844. else
  2845. rtw_set_key(adapter, securitypriv, keyid, 0, _FALSE);
  2846. }
  2847. }
  2848. }
  2849. }
  2850. u8 rtw_handle_tkip_countermeasure(_adapter *adapter, const char *caller)
  2851. {
  2852. struct security_priv *securitypriv = &(adapter->securitypriv);
  2853. u8 status = _SUCCESS;
  2854. if (securitypriv->btkip_countermeasure == _TRUE) {
  2855. u32 passing_ms = rtw_get_passing_time_ms(securitypriv->btkip_countermeasure_time);
  2856. if (passing_ms > 60 * 1000) {
  2857. RTW_PRINT("%s("ADPT_FMT") countermeasure time:%ds > 60s\n",
  2858. caller, ADPT_ARG(adapter), passing_ms / 1000);
  2859. securitypriv->btkip_countermeasure = _FALSE;
  2860. securitypriv->btkip_countermeasure_time = 0;
  2861. } else {
  2862. RTW_PRINT("%s("ADPT_FMT") countermeasure time:%ds < 60s\n",
  2863. caller, ADPT_ARG(adapter), passing_ms / 1000);
  2864. status = _FAIL;
  2865. }
  2866. }
  2867. return status;
  2868. }
  2869. #ifdef CONFIG_WOWLAN
  2870. u16 rtw_cal_crc16(u8 data, u16 crc)
  2871. {
  2872. u8 shift_in, data_bit;
  2873. u8 crc_bit4, crc_bit11, crc_bit15;
  2874. u16 crc_result;
  2875. int index;
  2876. for (index = 0; index < 8; index++) {
  2877. crc_bit15 = ((crc & BIT15) ? 1 : 0);
  2878. data_bit = (data & (BIT0 << index) ? 1 : 0);
  2879. shift_in = crc_bit15 ^ data_bit;
  2880. /*printf("crc_bit15=%d, DataBit=%d, shift_in=%d\n",
  2881. * crc_bit15, data_bit, shift_in);*/
  2882. crc_result = crc << 1;
  2883. if (shift_in == 0)
  2884. crc_result &= (~BIT0);
  2885. else
  2886. crc_result |= BIT0;
  2887. /*printf("CRC =%x\n",CRC_Result);*/
  2888. crc_bit11 = ((crc & BIT11) ? 1 : 0) ^ shift_in;
  2889. if (crc_bit11 == 0)
  2890. crc_result &= (~BIT12);
  2891. else
  2892. crc_result |= BIT12;
  2893. /*printf("bit12 CRC =%x\n",CRC_Result);*/
  2894. crc_bit4 = ((crc & BIT4) ? 1 : 0) ^ shift_in;
  2895. if (crc_bit4 == 0)
  2896. crc_result &= (~BIT5);
  2897. else
  2898. crc_result |= BIT5;
  2899. /* printf("bit5 CRC =%x\n",CRC_Result); */
  2900. /* repeat using the last result*/
  2901. crc = crc_result;
  2902. }
  2903. return crc;
  2904. }
  2905. /*
  2906. * function name :rtw_calc_crc
  2907. *
  2908. * input: char* pattern , pattern size
  2909. *
  2910. */
  2911. u16 rtw_calc_crc(u8 *pdata, int length)
  2912. {
  2913. u16 crc = 0xffff;
  2914. int i;
  2915. for (i = 0; i < length; i++)
  2916. crc = rtw_cal_crc16(pdata[i], crc);
  2917. /* get 1' complement */
  2918. crc = ~crc;
  2919. return crc;
  2920. }
  2921. #endif /*CONFIG_WOWLAN*/