rtw_wapi_sms4.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #ifdef CONFIG_WAPI_SUPPORT
  2. #include <linux/unistd.h>
  3. #include <linux/etherdevice.h>
  4. #include <drv_types.h>
  5. #include <rtw_wapi.h>
  6. #ifdef CONFIG_WAPI_SW_SMS4
  7. #define WAPI_LITTLE_ENDIAN
  8. //#define BIG_ENDIAN
  9. #define ENCRYPT 0
  10. #define DECRYPT 1
  11. /**********************************************************
  12. **********************************************************/
  13. const u8 Sbox[256] = {
  14. 0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05,
  15. 0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99,
  16. 0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62,
  17. 0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6,
  18. 0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8,
  19. 0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35,
  20. 0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87,
  21. 0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e,
  22. 0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1,
  23. 0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3,
  24. 0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,0xc0,0x29,0x23,0xab,0x0d,0x53,0x4e,0x6f,
  25. 0xd5,0xdb,0x37,0x45,0xde,0xfd,0x8e,0x2f,0x03,0xff,0x6a,0x72,0x6d,0x6c,0x5b,0x51,
  26. 0x8d,0x1b,0xaf,0x92,0xbb,0xdd,0xbc,0x7f,0x11,0xd9,0x5c,0x41,0x1f,0x10,0x5a,0xd8,
  27. 0x0a,0xc1,0x31,0x88,0xa5,0xcd,0x7b,0xbd,0x2d,0x74,0xd0,0x12,0xb8,0xe5,0xb4,0xb0,
  28. 0x89,0x69,0x97,0x4a,0x0c,0x96,0x77,0x7e,0x65,0xb9,0xf1,0x09,0xc5,0x6e,0xc6,0x84,
  29. 0x18,0xf0,0x7d,0xec,0x3a,0xdc,0x4d,0x20,0x79,0xee,0x5f,0x3e,0xd7,0xcb,0x39,0x48
  30. };
  31. const u32 CK[32] = {
  32. 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
  33. 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
  34. 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
  35. 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
  36. 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,
  37. 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
  38. 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
  39. 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279 };
  40. #define Rotl(_x, _y) (((_x) << (_y)) | ((_x) >> (32 - (_y))))
  41. #define ByteSub(_A) (Sbox[(_A) >> 24 & 0xFF] << 24 | \
  42. Sbox[(_A) >> 16 & 0xFF] << 16 | \
  43. Sbox[(_A) >> 8 & 0xFF] << 8 | \
  44. Sbox[(_A) & 0xFF])
  45. #define L1(_B) ((_B) ^ Rotl(_B, 2) ^ Rotl(_B, 10) ^ Rotl(_B, 18) ^ Rotl(_B, 24))
  46. #define L2(_B) ((_B) ^ Rotl(_B, 13) ^ Rotl(_B, 23))
  47. static void
  48. xor_block(void *dst, void *src1, void *src2)
  49. /* 128-bit xor: *dst = *src1 xor *src2. Pointers must be 32-bit aligned */
  50. {
  51. ((u32 *)dst)[0] = ((u32 *)src1)[0] ^ ((u32 *)src2)[0];
  52. ((u32 *)dst)[1] = ((u32 *)src1)[1] ^ ((u32 *)src2)[1];
  53. ((u32 *)dst)[2] = ((u32 *)src1)[2] ^ ((u32 *)src2)[2];
  54. ((u32 *)dst)[3] = ((u32 *)src1)[3] ^ ((u32 *)src2)[3];
  55. }
  56. void SMS4Crypt(u8 *Input, u8 *Output, u32 *rk)
  57. {
  58. u32 r, mid, x0, x1, x2, x3, *p;
  59. p = (u32 *)Input;
  60. x0 = p[0];
  61. x1 = p[1];
  62. x2 = p[2];
  63. x3 = p[3];
  64. #ifdef WAPI_LITTLE_ENDIAN
  65. x0 = Rotl(x0, 16); x0 = ((x0 & 0x00FF00FF) << 8) | ((x0 & 0xFF00FF00) >> 8);
  66. x1 = Rotl(x1, 16); x1 = ((x1 & 0x00FF00FF) << 8) | ((x1 & 0xFF00FF00) >> 8);
  67. x2 = Rotl(x2, 16); x2 = ((x2 & 0x00FF00FF) << 8) | ((x2 & 0xFF00FF00) >> 8);
  68. x3 = Rotl(x3, 16); x3 = ((x3 & 0x00FF00FF) << 8) | ((x3 & 0xFF00FF00) >> 8);
  69. #endif
  70. for (r = 0; r < 32; r += 4)
  71. {
  72. mid = x1 ^ x2 ^ x3 ^ rk[r + 0];
  73. mid = ByteSub(mid);
  74. x0 ^= L1(mid);
  75. mid = x2 ^ x3 ^ x0 ^ rk[r + 1];
  76. mid = ByteSub(mid);
  77. x1 ^= L1(mid);
  78. mid = x3 ^ x0 ^ x1 ^ rk[r + 2];
  79. mid = ByteSub(mid);
  80. x2 ^= L1(mid);
  81. mid = x0 ^ x1 ^ x2 ^ rk[r + 3];
  82. mid = ByteSub(mid);
  83. x3 ^= L1(mid);
  84. }
  85. #ifdef WAPI_LITTLE_ENDIAN
  86. x0 = Rotl(x0, 16); x0 = ((x0 & 0x00FF00FF) << 8) | ((x0 & 0xFF00FF00) >> 8);
  87. x1 = Rotl(x1, 16); x1 = ((x1 & 0x00FF00FF) << 8) | ((x1 & 0xFF00FF00) >> 8);
  88. x2 = Rotl(x2, 16); x2 = ((x2 & 0x00FF00FF) << 8) | ((x2 & 0xFF00FF00) >> 8);
  89. x3 = Rotl(x3, 16); x3 = ((x3 & 0x00FF00FF) << 8) | ((x3 & 0xFF00FF00) >> 8);
  90. #endif
  91. p = (u32 *)Output;
  92. p[0] = x3;
  93. p[1] = x2;
  94. p[2] = x1;
  95. p[3] = x0;
  96. }
  97. void SMS4KeyExt(u8 *Key, u32 *rk, u32 CryptFlag)
  98. {
  99. u32 r, mid, x0, x1, x2, x3, *p;
  100. p = (u32 *)Key;
  101. x0 = p[0];
  102. x1 = p[1];
  103. x2 = p[2];
  104. x3 = p[3];
  105. #ifdef WAPI_LITTLE_ENDIAN
  106. x0 = Rotl(x0, 16); x0 = ((x0 & 0xFF00FF) << 8) | ((x0 & 0xFF00FF00) >> 8);
  107. x1 = Rotl(x1, 16); x1 = ((x1 & 0xFF00FF) << 8) | ((x1 & 0xFF00FF00) >> 8);
  108. x2 = Rotl(x2, 16); x2 = ((x2 & 0xFF00FF) << 8) | ((x2 & 0xFF00FF00) >> 8);
  109. x3 = Rotl(x3, 16); x3 = ((x3 & 0xFF00FF) << 8) | ((x3 & 0xFF00FF00) >> 8);
  110. #endif
  111. x0 ^= 0xa3b1bac6;
  112. x1 ^= 0x56aa3350;
  113. x2 ^= 0x677d9197;
  114. x3 ^= 0xb27022dc;
  115. for (r = 0; r < 32; r += 4)
  116. {
  117. mid = x1 ^ x2 ^ x3 ^ CK[r + 0];
  118. mid = ByteSub(mid);
  119. rk[r + 0] = x0 ^= L2(mid);
  120. mid = x2 ^ x3 ^ x0 ^ CK[r + 1];
  121. mid = ByteSub(mid);
  122. rk[r + 1] = x1 ^= L2(mid);
  123. mid = x3 ^ x0 ^ x1 ^ CK[r + 2];
  124. mid = ByteSub(mid);
  125. rk[r + 2] = x2 ^= L2(mid);
  126. mid = x0 ^ x1 ^ x2 ^ CK[r + 3];
  127. mid = ByteSub(mid);
  128. rk[r + 3] = x3 ^= L2(mid);
  129. }
  130. if (CryptFlag == DECRYPT)
  131. {
  132. for (r = 0; r < 16; r++)
  133. mid = rk[r], rk[r] = rk[31 - r], rk[31 - r] = mid;
  134. }
  135. }
  136. void WapiSMS4Cryption(u8 *Key, u8 *IV, u8 *Input, u16 InputLength,
  137. u8 *Output, u16 *OutputLength, u32 CryptFlag)
  138. {
  139. u32 blockNum,i,j, rk[32];
  140. u16 remainder;
  141. u8 blockIn[16],blockOut[16], tempIV[16], k;
  142. *OutputLength = 0;
  143. remainder = InputLength & 0x0F;
  144. blockNum = InputLength >> 4;
  145. if(remainder !=0)
  146. blockNum++;
  147. else
  148. remainder = 16;
  149. for(k=0;k<16;k++)
  150. tempIV[k] = IV[15-k];
  151. memcpy(blockIn, tempIV, 16);
  152. SMS4KeyExt((u8 *)Key, rk,CryptFlag);
  153. for(i=0; i<blockNum-1; i++)
  154. {
  155. SMS4Crypt((u8 *)blockIn, blockOut, rk);
  156. xor_block(&Output[i*16], &Input[i*16], blockOut);
  157. memcpy(blockIn,blockOut,16);
  158. }
  159. *OutputLength = i*16;
  160. SMS4Crypt((u8 *)blockIn, blockOut, rk);
  161. for(j=0; j<remainder; j++)
  162. {
  163. Output[i*16+j] = Input[i*16+j] ^ blockOut[j];
  164. }
  165. *OutputLength += remainder;
  166. }
  167. void WapiSMS4Encryption(u8 *Key, u8 *IV, u8 *Input, u16 InputLength,
  168. u8 *Output, u16 *OutputLength)
  169. {
  170. WapiSMS4Cryption(Key, IV, Input, InputLength, Output, OutputLength, ENCRYPT);
  171. }
  172. void WapiSMS4Decryption(u8 *Key, u8 *IV, u8 *Input, u16 InputLength,
  173. u8 *Output, u16 *OutputLength)
  174. {
  175. // OFB mode: is also ENCRYPT flag
  176. WapiSMS4Cryption(Key, IV, Input, InputLength, Output, OutputLength, ENCRYPT);
  177. }
  178. void WapiSMS4CalculateMic(u8 *Key, u8 *IV, u8 *Input1, u8 Input1Length,
  179. u8 *Input2, u16 Input2Length, u8 *Output, u8 *OutputLength)
  180. {
  181. u32 blockNum, i, remainder, rk[32];
  182. u8 BlockIn[16], BlockOut[16], TempBlock[16], tempIV[16], k;
  183. *OutputLength = 0;
  184. remainder = Input1Length & 0x0F;
  185. blockNum = Input1Length >> 4;
  186. for(k=0;k<16;k++)
  187. tempIV[k] = IV[15-k];
  188. memcpy(BlockIn, tempIV, 16);
  189. SMS4KeyExt((u8 *)Key, rk, ENCRYPT);
  190. SMS4Crypt((u8 *)BlockIn, BlockOut, rk);
  191. for(i=0; i<blockNum; i++){
  192. xor_block(BlockIn, (Input1+i*16), BlockOut);
  193. SMS4Crypt((u8 *)BlockIn, BlockOut, rk);
  194. }
  195. if(remainder !=0){
  196. memset(TempBlock, 0, 16);
  197. memcpy(TempBlock, (Input1+blockNum*16), remainder);
  198. xor_block(BlockIn, TempBlock, BlockOut);
  199. SMS4Crypt((u8 *)BlockIn, BlockOut, rk);
  200. }
  201. remainder = Input2Length & 0x0F;
  202. blockNum = Input2Length >> 4;
  203. for(i=0; i<blockNum; i++){
  204. xor_block(BlockIn, (Input2+i*16), BlockOut);
  205. SMS4Crypt((u8 *)BlockIn, BlockOut, rk);
  206. }
  207. if(remainder !=0){
  208. memset(TempBlock, 0, 16);
  209. memcpy(TempBlock, (Input2+blockNum*16), remainder);
  210. xor_block(BlockIn, TempBlock, BlockOut);
  211. SMS4Crypt((u8 *)BlockIn, BlockOut, rk);
  212. }
  213. memcpy(Output, BlockOut, 16);
  214. *OutputLength = 16;
  215. }
  216. void SecCalculateMicSMS4(
  217. u8 KeyIdx,
  218. u8 *MicKey,
  219. u8 *pHeader,
  220. u8 *pData,
  221. u16 DataLen,
  222. u8 *MicBuffer
  223. )
  224. {
  225. #if 0
  226. struct ieee80211_hdr_3addr_qos *header;
  227. u8 TempBuf[34], TempLen = 32, MicLen, QosOffset, *IV;
  228. u16 *pTemp, fc;
  229. WAPI_TRACE(WAPI_TX|WAPI_RX, "=========>%s\n", __FUNCTION__);
  230. header = (struct ieee80211_hdr_3addr_qos *)pHeader;
  231. memset(TempBuf, 0, 34);
  232. memcpy(TempBuf, pHeader, 2); //FrameCtrl
  233. pTemp = (u16*)TempBuf;
  234. *pTemp &= 0xc78f; //bit4,5,6,11,12,13
  235. memcpy((TempBuf+2), (pHeader+4), 12); //Addr1, Addr2
  236. memcpy((TempBuf+14), (pHeader+22), 2); // SeqCtrl
  237. pTemp = (u16*)(TempBuf + 14);
  238. *pTemp &= 0x000f;
  239. memcpy((TempBuf+16), (pHeader+16), 6); //Addr3
  240. fc = le16_to_cpu(header->frame_ctl);
  241. if (GetFrDs((u16*)&fc) && GetToDs((u16 *)&fc))
  242. {
  243. memcpy((TempBuf+22), (pHeader+24), 6);
  244. QosOffset = 30;
  245. }else{
  246. memset((TempBuf+22), 0, 6);
  247. QosOffset = 24;
  248. }
  249. if((fc & 0x0088) == 0x0088){
  250. memcpy((TempBuf+28), (pHeader+QosOffset), 2);
  251. TempLen += 2;
  252. //IV = pHeader + QosOffset + 2 + SNAP_SIZE + sizeof(u16) + 2;
  253. IV = pHeader + QosOffset + 2 + 2;
  254. }else{
  255. IV = pHeader + QosOffset + 2;
  256. //IV = pHeader + QosOffset + SNAP_SIZE + sizeof(u16) + 2;
  257. }
  258. TempBuf[TempLen-1] = (u8)(DataLen & 0xff);
  259. TempBuf[TempLen-2] = (u8)((DataLen & 0xff00)>>8);
  260. TempBuf[TempLen-4] = KeyIdx;
  261. WAPI_DATA(WAPI_TX, "CalculateMic - KEY", MicKey, 16);
  262. WAPI_DATA(WAPI_TX, "CalculateMic - IV", IV, 16);
  263. WAPI_DATA(WAPI_TX, "CalculateMic - TempBuf", TempBuf, TempLen);
  264. WAPI_DATA(WAPI_TX, "CalculateMic - pData", pData, DataLen);
  265. WapiSMS4CalculateMic(MicKey, IV, TempBuf, TempLen,
  266. pData, DataLen, MicBuffer, &MicLen);
  267. if (MicLen != 16)
  268. WAPI_TRACE(WAPI_ERR,"%s: MIC Length Error!!\n",__FUNCTION__);
  269. WAPI_TRACE(WAPI_TX|WAPI_RX, "<=========%s\n", __FUNCTION__);
  270. #endif
  271. }
  272. /* AddCount: 1 or 2.
  273. * If overflow, return 1,
  274. * else return 0.
  275. */
  276. u8 WapiIncreasePN(u8 *PN, u8 AddCount)
  277. {
  278. u8 i;
  279. if (NULL == PN)
  280. return 1;
  281. //YJ,test,091102
  282. /*
  283. if(AddCount == 2){
  284. DBG_8192C("############################%s(): PN[0]=0x%x\n", __FUNCTION__, PN[0]);
  285. if(PN[0] == 0x48){
  286. PN[0] += AddCount;
  287. return 1;
  288. }else{
  289. PN[0] += AddCount;
  290. return 0;
  291. }
  292. }
  293. */
  294. //YJ,test,091102,end
  295. for (i=0; i<16; i++)
  296. {
  297. if (PN[i] + AddCount <= 0xff)
  298. {
  299. PN[i] += AddCount;
  300. return 0;
  301. }
  302. else
  303. {
  304. PN[i] += AddCount;
  305. AddCount = 1;
  306. }
  307. }
  308. return 1;
  309. }
  310. void WapiGetLastRxUnicastPNForQoSData(
  311. u8 UserPriority,
  312. PRT_WAPI_STA_INFO pWapiStaInfo,
  313. u8 *PNOut
  314. )
  315. {
  316. WAPI_TRACE(WAPI_RX, "===========> %s\n", __FUNCTION__);
  317. switch(UserPriority)
  318. {
  319. case 0:
  320. case 3:
  321. memcpy(PNOut,pWapiStaInfo->lastRxUnicastPNBEQueue,16);
  322. break;
  323. case 1:
  324. case 2:
  325. memcpy(PNOut,pWapiStaInfo->lastRxUnicastPNBKQueue,16);
  326. break;
  327. case 4:
  328. case 5:
  329. memcpy(PNOut,pWapiStaInfo->lastRxUnicastPNVIQueue,16);
  330. break;
  331. case 6:
  332. case 7:
  333. memcpy(PNOut,pWapiStaInfo->lastRxUnicastPNVOQueue,16);
  334. break;
  335. default:
  336. WAPI_TRACE(WAPI_ERR, "%s: Unknown TID \n", __FUNCTION__);
  337. break;
  338. }
  339. WAPI_TRACE(WAPI_RX, "<=========== %s\n", __FUNCTION__);
  340. }
  341. void WapiSetLastRxUnicastPNForQoSData(
  342. u8 UserPriority,
  343. u8 *PNIn,
  344. PRT_WAPI_STA_INFO pWapiStaInfo
  345. )
  346. {
  347. WAPI_TRACE(WAPI_RX, "===========> %s\n", __FUNCTION__);
  348. switch(UserPriority)
  349. {
  350. case 0:
  351. case 3:
  352. memcpy(pWapiStaInfo->lastRxUnicastPNBEQueue,PNIn,16);
  353. break;
  354. case 1:
  355. case 2:
  356. memcpy(pWapiStaInfo->lastRxUnicastPNBKQueue,PNIn,16);
  357. break;
  358. case 4:
  359. case 5:
  360. memcpy(pWapiStaInfo->lastRxUnicastPNVIQueue,PNIn,16);
  361. break;
  362. case 6:
  363. case 7:
  364. memcpy(pWapiStaInfo->lastRxUnicastPNVOQueue,PNIn,16);
  365. break;
  366. default:
  367. WAPI_TRACE(WAPI_ERR, "%s: Unknown TID \n", __FUNCTION__);
  368. break;
  369. }
  370. WAPI_TRACE(WAPI_RX, "<=========== %s\n", __FUNCTION__);
  371. }
  372. /****************************************************************************
  373. FALSE not RX-Reorder
  374. TRUE do RX Reorder
  375. add to support WAPI to N-mode
  376. *****************************************************************************/
  377. u8 WapiCheckPnInSwDecrypt(
  378. _adapter *padapter,
  379. struct sk_buff *pskb
  380. )
  381. {
  382. u8 ret = false;
  383. #if 0
  384. struct ieee80211_hdr_3addr_qos *header;
  385. u16 fc;
  386. u8 *pDaddr, *pTaddr, *pRaddr;
  387. header = (struct ieee80211_hdr_3addr_qos *)pskb->data;
  388. pTaddr = header->addr2;
  389. pRaddr = header->addr1;
  390. fc = le16_to_cpu(header->frame_ctl);
  391. if(GetToDs(&fc))
  392. pDaddr = header->addr3;
  393. else
  394. pDaddr = header->addr1;
  395. if ((_rtw_memcmp(pRaddr, padapter->pnetdev->dev_addr, ETH_ALEN) == 0)
  396. && ! (pDaddr)
  397. && (GetFrameType(&fc) == WIFI_QOS_DATA_TYPE))
  398. //&& ieee->pHTInfo->bCurrentHTSupport &&
  399. //ieee->pHTInfo->bCurRxReorderEnable)
  400. ret = false;
  401. else
  402. ret = true;
  403. #endif
  404. WAPI_TRACE(WAPI_RX, "%s: return %d\n", __FUNCTION__, ret);
  405. return ret;
  406. }
  407. int SecSMS4HeaderFillIV(_adapter *padapter, u8 *pxmitframe)
  408. {
  409. struct pkt_attrib *pattrib = &((struct xmit_frame*)pxmitframe)->attrib;
  410. u8 * frame = ((struct xmit_frame *)pxmitframe)->buf_addr + TXDESC_OFFSET;
  411. u8 *pSecHeader = NULL, *pos = NULL, *pRA = NULL;
  412. u8 bPNOverflow = false, bFindMatchPeer = false, hdr_len = 0;
  413. PWLAN_HEADER_WAPI_EXTENSION pWapiExt = NULL;
  414. PRT_WAPI_T pWapiInfo = &padapter->wapiInfo;
  415. PRT_WAPI_STA_INFO pWapiSta = NULL;
  416. int ret = 0;
  417. WAPI_TRACE(WAPI_TX, "=========>%s\n", __FUNCTION__);
  418. return ret;
  419. #if 0
  420. hdr_len = sMacHdrLng;
  421. if (GetFrameType(pskb->data) == WIFI_QOS_DATA_TYPE)
  422. {
  423. hdr_len += 2;
  424. }
  425. //hdr_len += SNAP_SIZE + sizeof(u16);
  426. pos = skb_push(pskb, padapter->wapiInfo.extra_prefix_len);
  427. memmove(pos, pos+padapter->wapiInfo.extra_prefix_len, hdr_len);
  428. pSecHeader = pskb->data + hdr_len;
  429. pWapiExt = (PWLAN_HEADER_WAPI_EXTENSION)pSecHeader;
  430. pRA = pskb->data + 4;
  431. WAPI_DATA(WAPI_TX, "FillIV - Before Fill IV", pskb->data, pskb->len);
  432. //Address 1 is always receiver's address
  433. if( IS_MCAST(pRA) ){
  434. if(!pWapiInfo->wapiTxMsk.bTxEnable){
  435. WAPI_TRACE(WAPI_ERR,"%s: bTxEnable = 0!!\n",__FUNCTION__);
  436. return -2;
  437. }
  438. if(pWapiInfo->wapiTxMsk.keyId <= 1){
  439. pWapiExt->KeyIdx = pWapiInfo->wapiTxMsk.keyId;
  440. pWapiExt->Reserved = 0;
  441. bPNOverflow = WapiIncreasePN(pWapiInfo->lastTxMulticastPN, 1);
  442. memcpy(pWapiExt->PN, pWapiInfo->lastTxMulticastPN, 16);
  443. if (bPNOverflow){
  444. // Update MSK Notification.
  445. WAPI_TRACE(WAPI_ERR,"===============>%s():multicast PN overflow\n",__FUNCTION__);
  446. rtw_wapi_app_event_handler(padapter,NULL,0,pRA, false, false, true, 0, false);
  447. }
  448. }else{
  449. WAPI_TRACE(WAPI_ERR,"%s: Invalid Wapi Multicast KeyIdx!!\n",__FUNCTION__);
  450. ret = -3;
  451. }
  452. }
  453. else{
  454. list_for_each_entry(pWapiSta, &pWapiInfo->wapiSTAUsedList, list) {
  455. if(!memcmp(pWapiSta->PeerMacAddr,pRA,6)){
  456. bFindMatchPeer = true;
  457. break;
  458. }
  459. }
  460. if (bFindMatchPeer){
  461. if((!pWapiSta->wapiUskUpdate.bTxEnable) && (!pWapiSta->wapiUsk.bTxEnable)){
  462. WAPI_TRACE(WAPI_ERR,"%s: bTxEnable = 0!!\n",__FUNCTION__);
  463. return -4;
  464. }
  465. if (pWapiSta->wapiUsk.keyId <= 1){
  466. if(pWapiSta->wapiUskUpdate.bTxEnable)
  467. pWapiExt->KeyIdx = pWapiSta->wapiUskUpdate.keyId;
  468. else
  469. pWapiExt->KeyIdx = pWapiSta->wapiUsk.keyId;
  470. pWapiExt->Reserved = 0;
  471. bPNOverflow = WapiIncreasePN(pWapiSta->lastTxUnicastPN, 2);
  472. memcpy(pWapiExt->PN, pWapiSta->lastTxUnicastPN, 16);
  473. if (bPNOverflow){
  474. // Update USK Notification.
  475. WAPI_TRACE(WAPI_ERR,"===============>%s():unicast PN overflow\n",__FUNCTION__);
  476. rtw_wapi_app_event_handler(padapter,NULL,0,pWapiSta->PeerMacAddr, false, true, false, 0, false);
  477. }
  478. }else{
  479. WAPI_TRACE(WAPI_ERR,"%s: Invalid Wapi Unicast KeyIdx!!\n",__FUNCTION__);
  480. ret = -5;
  481. }
  482. }
  483. else{
  484. WAPI_TRACE(WAPI_ERR,"%s: Can not find Peer Sta "MAC_FMT"!!\n",__FUNCTION__, MAC_ARG(pRA));
  485. ret = -6;
  486. }
  487. }
  488. WAPI_DATA(WAPI_TX, "FillIV - After Fill IV", pskb->data, pskb->len);
  489. WAPI_TRACE(WAPI_TX, "<=========%s\n", __FUNCTION__);
  490. return ret;
  491. #endif
  492. }
  493. // WAPI SW Enc: must have done Coalesce!
  494. void SecSWSMS4Encryption(
  495. _adapter *padapter,
  496. u8 * pxmitframe
  497. )
  498. {
  499. PRT_WAPI_T pWapiInfo = &padapter->wapiInfo;
  500. PRT_WAPI_STA_INFO pWapiSta = NULL;
  501. u8 *pframe = ((struct xmit_frame*)pxmitframe)->buf_addr + TXDESC_SIZE;
  502. struct pkt_attrib *pattrib = &((struct xmit_frame*)pxmitframe)->attrib;
  503. u8 *SecPtr = NULL, *pRA, *pMicKey = NULL, *pDataKey = NULL, *pIV = NULL;
  504. u8 IVOffset, DataOffset, bFindMatchPeer = false, KeyIdx = 0, MicBuffer[16];
  505. u16 OutputLength;
  506. WAPI_TRACE(WAPI_TX, "=========>%s\n", __FUNCTION__);
  507. WAPI_TRACE(WAPI_TX,"hdrlen: %d \n",pattrib->hdrlen);
  508. return;
  509. DataOffset = pattrib->hdrlen + pattrib->iv_len;
  510. pRA = pframe + 4;
  511. if( IS_MCAST(pRA) ){
  512. KeyIdx = pWapiInfo->wapiTxMsk.keyId;
  513. pIV = pWapiInfo->lastTxMulticastPN;
  514. pMicKey = pWapiInfo->wapiTxMsk.micKey;
  515. pDataKey = pWapiInfo->wapiTxMsk.dataKey;
  516. }else{
  517. if (!list_empty(&(pWapiInfo->wapiSTAUsedList))){
  518. list_for_each_entry(pWapiSta, &pWapiInfo->wapiSTAUsedList, list) {
  519. if (0 == memcmp(pWapiSta->PeerMacAddr, pRA, 6)){
  520. bFindMatchPeer = true;
  521. break;
  522. }
  523. }
  524. if (bFindMatchPeer){
  525. if (pWapiSta->wapiUskUpdate.bTxEnable){
  526. KeyIdx = pWapiSta->wapiUskUpdate.keyId;
  527. WAPI_TRACE(WAPI_TX, "%s(): Use update USK!! KeyIdx=%d\n", __FUNCTION__, KeyIdx);
  528. pIV = pWapiSta->lastTxUnicastPN;
  529. pMicKey = pWapiSta->wapiUskUpdate.micKey;
  530. pDataKey = pWapiSta->wapiUskUpdate.dataKey;
  531. }else{
  532. KeyIdx = pWapiSta->wapiUsk.keyId;
  533. WAPI_TRACE(WAPI_TX, "%s(): Use USK!! KeyIdx=%d\n", __FUNCTION__, KeyIdx);
  534. pIV = pWapiSta->lastTxUnicastPN;
  535. pMicKey = pWapiSta->wapiUsk.micKey;
  536. pDataKey = pWapiSta->wapiUsk.dataKey;
  537. }
  538. }else{
  539. WAPI_TRACE(WAPI_ERR,"%s: Can not find Peer Sta!!\n",__FUNCTION__);
  540. return;
  541. }
  542. }else{
  543. WAPI_TRACE(WAPI_ERR,"%s: wapiSTAUsedList is empty!!\n",__FUNCTION__);
  544. return;
  545. }
  546. }
  547. SecPtr = pframe;
  548. SecCalculateMicSMS4(KeyIdx, pMicKey, SecPtr, (SecPtr+DataOffset), pattrib->pktlen, MicBuffer);
  549. WAPI_DATA(WAPI_TX, "Encryption - MIC", MicBuffer, padapter->wapiInfo.extra_postfix_len);
  550. memcpy(pframe+pattrib->hdrlen+pattrib->iv_len+pattrib->pktlen-pattrib->icv_len,
  551. (u8 *)MicBuffer,
  552. padapter->wapiInfo.extra_postfix_len
  553. );
  554. WapiSMS4Encryption(pDataKey, pIV, (SecPtr+DataOffset),pattrib->pktlen+pattrib->icv_len, (SecPtr+DataOffset), &OutputLength);
  555. WAPI_DATA(WAPI_TX, "Encryption - After SMS4 encryption",pframe,pattrib->hdrlen+pattrib->iv_len+pattrib->pktlen);
  556. WAPI_TRACE(WAPI_TX, "<=========%s\n", __FUNCTION__);
  557. }
  558. u8 SecSWSMS4Decryption(
  559. _adapter *padapter,
  560. u8 *precv_frame,
  561. struct recv_priv *precv_priv
  562. )
  563. {
  564. PRT_WAPI_T pWapiInfo = &padapter->wapiInfo;
  565. struct recv_frame_hdr *precv_hdr;
  566. PRT_WAPI_STA_INFO pWapiSta = NULL;
  567. u8 IVOffset, DataOffset, bFindMatchPeer = false, bUseUpdatedKey = false;
  568. u8 KeyIdx, MicBuffer[16], lastRxPNforQoS[16];
  569. u8 *pRA, *pTA, *pMicKey, *pDataKey, *pLastRxPN, *pRecvPN, *pSecData, *pRecvMic, *pos;
  570. u8 TID = 0;
  571. u16 OutputLength, DataLen;
  572. u8 bQosData;
  573. struct sk_buff * pskb;
  574. WAPI_TRACE(WAPI_RX, "=========>%s\n", __FUNCTION__);
  575. return 0;
  576. precv_hdr = &((union recv_frame*)precv_frame)->u.hdr;
  577. pskb = (struct sk_buff *)(precv_hdr->rx_data);
  578. precv_hdr->bWapiCheckPNInDecrypt = WapiCheckPnInSwDecrypt(padapter, pskb);
  579. WAPI_TRACE(WAPI_RX, "=========>%s: check PN %d\n", __FUNCTION__,precv_hdr->bWapiCheckPNInDecrypt);
  580. WAPI_DATA(WAPI_RX, "Decryption - Before decryption", pskb->data, pskb->len);
  581. IVOffset = sMacHdrLng;
  582. bQosData = GetFrameType(pskb->data) == WIFI_QOS_DATA_TYPE;
  583. if (bQosData){
  584. IVOffset += 2;
  585. }
  586. //if(GetHTC())
  587. // IVOffset += 4;
  588. //IVOffset += SNAP_SIZE + sizeof(u16);
  589. DataOffset = IVOffset + padapter->wapiInfo.extra_prefix_len;
  590. pRA = pskb->data + 4;
  591. pTA = pskb->data + 10;
  592. KeyIdx = *(pskb->data + IVOffset);
  593. pRecvPN = pskb->data + IVOffset + 2;
  594. pSecData = pskb->data + DataOffset;
  595. DataLen = pskb->len - DataOffset;
  596. pRecvMic = pskb->data + pskb->len - padapter->wapiInfo.extra_postfix_len;
  597. TID = GetTid(pskb->data);
  598. if (!list_empty(&(pWapiInfo->wapiSTAUsedList))){
  599. list_for_each_entry(pWapiSta, &pWapiInfo->wapiSTAUsedList, list) {
  600. if (0 == memcmp(pWapiSta->PeerMacAddr, pTA, 6)){
  601. bFindMatchPeer = true;
  602. break;
  603. }
  604. }
  605. }
  606. if (!bFindMatchPeer){
  607. WAPI_TRACE(WAPI_ERR, "%s: Can not find Peer Sta "MAC_FMT" for Key Info!!!\n", __FUNCTION__, MAC_ARG(pTA));
  608. return false;
  609. }
  610. if( IS_MCAST(pRA) ){
  611. WAPI_TRACE(WAPI_RX, "%s: Multicast decryption !!!\n", __FUNCTION__);
  612. if (pWapiSta->wapiMsk.keyId == KeyIdx && pWapiSta->wapiMsk.bSet){
  613. pLastRxPN = pWapiSta->lastRxMulticastPN;
  614. if (!WapiComparePN(pRecvPN, pLastRxPN)){
  615. WAPI_TRACE(WAPI_ERR, "%s: MSK PN is not larger than last, Dropped!!!\n", __FUNCTION__);
  616. WAPI_DATA(WAPI_ERR, "pRecvPN:", pRecvPN, 16);
  617. WAPI_DATA(WAPI_ERR, "pLastRxPN:", pLastRxPN, 16);
  618. return false;
  619. }
  620. memcpy(pLastRxPN, pRecvPN, 16);
  621. pMicKey = pWapiSta->wapiMsk.micKey;
  622. pDataKey = pWapiSta->wapiMsk.dataKey;
  623. }else if (pWapiSta->wapiMskUpdate.keyId == KeyIdx && pWapiSta->wapiMskUpdate.bSet){
  624. WAPI_TRACE(WAPI_RX, "%s: Use Updated MSK for Decryption !!!\n", __FUNCTION__);
  625. bUseUpdatedKey = true;
  626. memcpy(pWapiSta->lastRxMulticastPN, pRecvPN, 16);
  627. pMicKey = pWapiSta->wapiMskUpdate.micKey;
  628. pDataKey = pWapiSta->wapiMskUpdate.dataKey;
  629. }else{
  630. WAPI_TRACE(WAPI_ERR, "%s: Can not find MSK with matched KeyIdx(%d), Dropped !!!\n", __FUNCTION__,KeyIdx);
  631. return false;
  632. }
  633. }
  634. else{
  635. WAPI_TRACE(WAPI_RX, "%s: Unicast decryption !!!\n", __FUNCTION__);
  636. if (pWapiSta->wapiUsk.keyId == KeyIdx && pWapiSta->wapiUsk.bSet){
  637. WAPI_TRACE(WAPI_RX, "%s: Use USK for Decryption!!!\n", __FUNCTION__);
  638. if(precv_hdr->bWapiCheckPNInDecrypt){
  639. if(GetFrameType(pskb->data) == WIFI_QOS_DATA_TYPE){
  640. WapiGetLastRxUnicastPNForQoSData(TID, pWapiSta, lastRxPNforQoS);
  641. pLastRxPN = lastRxPNforQoS;
  642. }else{
  643. pLastRxPN = pWapiSta->lastRxUnicastPN;
  644. }
  645. if (!WapiComparePN(pRecvPN, pLastRxPN)){
  646. return false;
  647. }
  648. if(bQosData){
  649. WapiSetLastRxUnicastPNForQoSData(TID, pRecvPN, pWapiSta);
  650. }else{
  651. memcpy(pWapiSta->lastRxUnicastPN, pRecvPN, 16);
  652. }
  653. }else{
  654. memcpy(precv_hdr->WapiTempPN,pRecvPN,16);
  655. }
  656. if (check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE))
  657. {
  658. if ((pRecvPN[0] & 0x1) == 0){
  659. WAPI_TRACE(WAPI_ERR, "%s: Rx USK PN is not odd when Infra STA mode, Dropped !!!\n", __FUNCTION__);
  660. return false;
  661. }
  662. }
  663. pMicKey = pWapiSta->wapiUsk.micKey;
  664. pDataKey = pWapiSta->wapiUsk.dataKey;
  665. }
  666. else if (pWapiSta->wapiUskUpdate.keyId == KeyIdx && pWapiSta->wapiUskUpdate.bSet ){
  667. WAPI_TRACE(WAPI_RX, "%s: Use Updated USK for Decryption!!!\n", __FUNCTION__);
  668. if(pWapiSta->bAuthenticatorInUpdata)
  669. bUseUpdatedKey = true;
  670. else
  671. bUseUpdatedKey = false;
  672. if(bQosData){
  673. WapiSetLastRxUnicastPNForQoSData(TID, pRecvPN, pWapiSta);
  674. }else{
  675. memcpy(pWapiSta->lastRxUnicastPN, pRecvPN, 16);
  676. }
  677. pMicKey = pWapiSta->wapiUskUpdate.micKey;
  678. pDataKey = pWapiSta->wapiUskUpdate.dataKey;
  679. }else{
  680. WAPI_TRACE(WAPI_ERR, "%s: No valid USK!!!KeyIdx=%d pWapiSta->wapiUsk.keyId=%d pWapiSta->wapiUskUpdate.keyId=%d\n", __FUNCTION__, KeyIdx, pWapiSta->wapiUsk.keyId, pWapiSta->wapiUskUpdate.keyId);
  681. //dump_buf(pskb->data,pskb->len);
  682. return false;
  683. }
  684. }
  685. WAPI_DATA(WAPI_RX, "Decryption - DataKey", pDataKey, 16);
  686. WAPI_DATA(WAPI_RX, "Decryption - IV", pRecvPN, 16);
  687. WapiSMS4Decryption(pDataKey, pRecvPN, pSecData, DataLen, pSecData, &OutputLength);
  688. if (OutputLength != DataLen)
  689. WAPI_TRACE(WAPI_ERR, "%s: Output Length Error!!!!\n", __FUNCTION__);
  690. WAPI_DATA(WAPI_RX, "Decryption - After decryption", pskb->data, pskb->len);
  691. DataLen -= padapter->wapiInfo.extra_postfix_len;
  692. SecCalculateMicSMS4(KeyIdx, pMicKey, pskb->data, pSecData, DataLen, MicBuffer);
  693. WAPI_DATA(WAPI_RX, "Decryption - MIC received", pRecvMic, SMS4_MIC_LEN);
  694. WAPI_DATA(WAPI_RX, "Decryption - MIC calculated", MicBuffer, SMS4_MIC_LEN);
  695. if (0 == memcmp(MicBuffer, pRecvMic, padapter->wapiInfo.extra_postfix_len)){
  696. WAPI_TRACE(WAPI_RX, "%s: Check MIC OK!!\n", __FUNCTION__);
  697. if (bUseUpdatedKey){
  698. // delete the old key
  699. if ( IS_MCAST(pRA) ){
  700. WAPI_TRACE(WAPI_API, "%s(): AE use new update MSK!!\n", __FUNCTION__);
  701. pWapiSta->wapiMsk.keyId = pWapiSta->wapiMskUpdate.keyId;
  702. memcpy(pWapiSta->wapiMsk.dataKey, pWapiSta->wapiMskUpdate.dataKey, 16);
  703. memcpy(pWapiSta->wapiMsk.micKey, pWapiSta->wapiMskUpdate.micKey, 16);
  704. pWapiSta->wapiMskUpdate.bTxEnable = pWapiSta->wapiMskUpdate.bSet = false;
  705. }else{
  706. WAPI_TRACE(WAPI_API, "%s(): AE use new update USK!!\n", __FUNCTION__);
  707. pWapiSta->wapiUsk.keyId = pWapiSta->wapiUskUpdate.keyId;
  708. memcpy(pWapiSta->wapiUsk.dataKey, pWapiSta->wapiUskUpdate.dataKey, 16);
  709. memcpy(pWapiSta->wapiUsk.micKey, pWapiSta->wapiUskUpdate.micKey, 16);
  710. pWapiSta->wapiUskUpdate.bTxEnable = pWapiSta->wapiUskUpdate.bSet = false;
  711. }
  712. }
  713. }else{
  714. WAPI_TRACE(WAPI_ERR, "%s: Check MIC Error, Dropped !!!!\n", __FUNCTION__);
  715. return false;
  716. }
  717. pos = pskb->data;
  718. memmove(pos+padapter->wapiInfo.extra_prefix_len, pos, IVOffset);
  719. skb_pull(pskb, padapter->wapiInfo.extra_prefix_len);
  720. WAPI_TRACE(WAPI_RX, "<=========%s\n", __FUNCTION__);
  721. return true;
  722. }
  723. u32 rtw_sms4_encrypt(_adapter *padapter, u8 *pxmitframe)
  724. {
  725. u8 *pframe;
  726. u32 res = _SUCCESS;
  727. WAPI_TRACE(WAPI_TX, "=========>%s\n", __FUNCTION__);
  728. if ((!padapter->WapiSupport) || (!padapter->wapiInfo.bWapiEnable))
  729. {
  730. WAPI_TRACE(WAPI_TX, "<========== %s, WAPI not supported or enabled!\n", __FUNCTION__);
  731. return _FAIL;
  732. }
  733. if(((struct xmit_frame*)pxmitframe)->buf_addr==NULL)
  734. return _FAIL;
  735. pframe = ((struct xmit_frame*)pxmitframe)->buf_addr + TXDESC_OFFSET;
  736. SecSWSMS4Encryption(padapter, pxmitframe);
  737. WAPI_TRACE(WAPI_TX, "<=========%s\n", __FUNCTION__);
  738. return res;
  739. }
  740. u32 rtw_sms4_decrypt(_adapter *padapter, u8 *precvframe)
  741. {
  742. u8 *pframe;
  743. u32 res = _SUCCESS;
  744. WAPI_TRACE(WAPI_RX, "=========>%s\n", __FUNCTION__);
  745. if ((!padapter->WapiSupport) || (!padapter->wapiInfo.bWapiEnable))
  746. {
  747. WAPI_TRACE(WAPI_RX, "<========== %s, WAPI not supported or enabled!\n", __FUNCTION__);
  748. return _FAIL;
  749. }
  750. //drop packet when hw decrypt fail
  751. //return tempraily
  752. return _FAIL;
  753. //pframe=(unsigned char *)((union recv_frame*)precvframe)->u.hdr.rx_data;
  754. if (false == SecSWSMS4Decryption(padapter, precvframe, &padapter->recvpriv))
  755. {
  756. WAPI_TRACE(WAPI_ERR, "%s():SMS4 decrypt frame error\n",__FUNCTION__);
  757. return _FAIL;
  758. }
  759. WAPI_TRACE(WAPI_RX, "<=========%s\n", __FUNCTION__);
  760. return res;
  761. }
  762. #else
  763. u32 rtw_sms4_encrypt(_adapter *padapter, u8 *pxmitframe)
  764. {
  765. WAPI_TRACE(WAPI_TX, "=========>Dummy %s\n", __FUNCTION__);
  766. WAPI_TRACE(WAPI_TX, "<=========Dummy %s\n", __FUNCTION__);
  767. return _SUCCESS;
  768. }
  769. u32 rtw_sms4_decrypt(_adapter *padapter, u8 *precvframe)
  770. {
  771. WAPI_TRACE(WAPI_RX, "=========>Dummy %s\n", __FUNCTION__);
  772. WAPI_TRACE(WAPI_RX, "<=========Dummy %s\n", __FUNCTION__);
  773. return _SUCCESS;
  774. }
  775. #endif
  776. #endif