rtw_security.c 84 KB

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