Path: blob/master/ALFA-W1F1/RTL8814AU/core/rtw_wapi_sms4.c
1307 views
/******************************************************************************1*2* Copyright(c) 2016 - 2017 Realtek Corporation.3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of version 2 of the GNU General Public License as6* published by the Free Software Foundation.7*8* This program is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for11* more details.12*13*****************************************************************************/14#ifdef CONFIG_WAPI_SUPPORT1516#include <linux/unistd.h>17#include <linux/etherdevice.h>18#include <drv_types.h>19#include <rtw_wapi.h>202122#ifdef CONFIG_WAPI_SW_SMS42324#define WAPI_LITTLE_ENDIAN25/* #define BIG_ENDIAN */26#define ENCRYPT 027#define DECRYPT 1282930/**********************************************************31**********************************************************/32const u8 Sbox[256] = {330xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,340x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,350x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62,360xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6,370x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8,380x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35,390x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87,400xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e,410xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1,420xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,430x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,440xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,450x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,460x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,470x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,480x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x4849};5051const u32 CK[32] = {520x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,530x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,540xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,550x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,560xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,570x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,580xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,590x10171e25, 0x2c333a41, 0x484f565d, 0x646b727960};6162#define Rotl(_x, _y) (((_x) << (_y)) | ((_x) >> (32 - (_y))))6364#define ByteSub(_A) (Sbox[(_A) >> 24 & 0xFF] << 24 | \65Sbox[(_A) >> 16 & 0xFF] << 16 | \66Sbox[(_A) >> 8 & 0xFF] << 8 | \67Sbox[(_A) & 0xFF])6869#define L1(_B) ((_B) ^ Rotl(_B, 2) ^ Rotl(_B, 10) ^ Rotl(_B, 18) ^ Rotl(_B, 24))70#define L2(_B) ((_B) ^ Rotl(_B, 13) ^ Rotl(_B, 23))7172static void73xor_block(void *dst, void *src1, void *src2)74/* 128-bit xor: *dst = *src1 xor *src2. Pointers must be 32-bit aligned */75{76((u32 *)dst)[0] = ((u32 *)src1)[0] ^ ((u32 *)src2)[0];77((u32 *)dst)[1] = ((u32 *)src1)[1] ^ ((u32 *)src2)[1];78((u32 *)dst)[2] = ((u32 *)src1)[2] ^ ((u32 *)src2)[2];79((u32 *)dst)[3] = ((u32 *)src1)[3] ^ ((u32 *)src2)[3];80}818283void SMS4Crypt(u8 *Input, u8 *Output, u32 *rk)84{85u32 r, mid, x0, x1, x2, x3, *p;86p = (u32 *)Input;87x0 = p[0];88x1 = p[1];89x2 = p[2];90x3 = p[3];91#ifdef WAPI_LITTLE_ENDIAN92x0 = Rotl(x0, 16);93x0 = ((x0 & 0x00FF00FF) << 8) | ((x0 & 0xFF00FF00) >> 8);94x1 = Rotl(x1, 16);95x1 = ((x1 & 0x00FF00FF) << 8) | ((x1 & 0xFF00FF00) >> 8);96x2 = Rotl(x2, 16);97x2 = ((x2 & 0x00FF00FF) << 8) | ((x2 & 0xFF00FF00) >> 8);98x3 = Rotl(x3, 16);99x3 = ((x3 & 0x00FF00FF) << 8) | ((x3 & 0xFF00FF00) >> 8);100#endif101for (r = 0; r < 32; r += 4) {102mid = x1 ^ x2 ^ x3 ^ rk[r + 0];103mid = ByteSub(mid);104x0 ^= L1(mid);105mid = x2 ^ x3 ^ x0 ^ rk[r + 1];106mid = ByteSub(mid);107x1 ^= L1(mid);108mid = x3 ^ x0 ^ x1 ^ rk[r + 2];109mid = ByteSub(mid);110x2 ^= L1(mid);111mid = x0 ^ x1 ^ x2 ^ rk[r + 3];112mid = ByteSub(mid);113x3 ^= L1(mid);114}115#ifdef WAPI_LITTLE_ENDIAN116x0 = Rotl(x0, 16);117x0 = ((x0 & 0x00FF00FF) << 8) | ((x0 & 0xFF00FF00) >> 8);118x1 = Rotl(x1, 16);119x1 = ((x1 & 0x00FF00FF) << 8) | ((x1 & 0xFF00FF00) >> 8);120x2 = Rotl(x2, 16);121x2 = ((x2 & 0x00FF00FF) << 8) | ((x2 & 0xFF00FF00) >> 8);122x3 = Rotl(x3, 16);123x3 = ((x3 & 0x00FF00FF) << 8) | ((x3 & 0xFF00FF00) >> 8);124#endif125p = (u32 *)Output;126p[0] = x3;127p[1] = x2;128p[2] = x1;129p[3] = x0;130}131132133134void SMS4KeyExt(u8 *Key, u32 *rk, u32 CryptFlag)135{136u32 r, mid, x0, x1, x2, x3, *p;137138p = (u32 *)Key;139x0 = p[0];140x1 = p[1];141x2 = p[2];142x3 = p[3];143#ifdef WAPI_LITTLE_ENDIAN144x0 = Rotl(x0, 16);145x0 = ((x0 & 0xFF00FF) << 8) | ((x0 & 0xFF00FF00) >> 8);146x1 = Rotl(x1, 16);147x1 = ((x1 & 0xFF00FF) << 8) | ((x1 & 0xFF00FF00) >> 8);148x2 = Rotl(x2, 16);149x2 = ((x2 & 0xFF00FF) << 8) | ((x2 & 0xFF00FF00) >> 8);150x3 = Rotl(x3, 16);151x3 = ((x3 & 0xFF00FF) << 8) | ((x3 & 0xFF00FF00) >> 8);152#endif153154x0 ^= 0xa3b1bac6;155x1 ^= 0x56aa3350;156x2 ^= 0x677d9197;157x3 ^= 0xb27022dc;158for (r = 0; r < 32; r += 4) {159mid = x1 ^ x2 ^ x3 ^ CK[r + 0];160mid = ByteSub(mid);161rk[r + 0] = x0 ^= L2(mid);162mid = x2 ^ x3 ^ x0 ^ CK[r + 1];163mid = ByteSub(mid);164rk[r + 1] = x1 ^= L2(mid);165mid = x3 ^ x0 ^ x1 ^ CK[r + 2];166mid = ByteSub(mid);167rk[r + 2] = x2 ^= L2(mid);168mid = x0 ^ x1 ^ x2 ^ CK[r + 3];169mid = ByteSub(mid);170rk[r + 3] = x3 ^= L2(mid);171}172if (CryptFlag == DECRYPT) {173for (r = 0; r < 16; r++)174mid = rk[r], rk[r] = rk[31 - r], rk[31 - r] = mid;175}176}177178179void WapiSMS4Cryption(u8 *Key, u8 *IV, u8 *Input, u16 InputLength,180u8 *Output, u16 *OutputLength, u32 CryptFlag)181{182u32 blockNum, i, j, rk[32];183u16 remainder;184u8 blockIn[16], blockOut[16], tempIV[16], k;185186*OutputLength = 0;187remainder = InputLength & 0x0F;188blockNum = InputLength >> 4;189if (remainder != 0)190blockNum++;191else192remainder = 16;193194for (k = 0; k < 16; k++)195tempIV[k] = IV[15 - k];196197memcpy(blockIn, tempIV, 16);198199SMS4KeyExt((u8 *)Key, rk, CryptFlag);200201for (i = 0; i < blockNum - 1; i++) {202SMS4Crypt((u8 *)blockIn, blockOut, rk);203xor_block(&Output[i * 16], &Input[i * 16], blockOut);204memcpy(blockIn, blockOut, 16);205}206207*OutputLength = i * 16;208209SMS4Crypt((u8 *)blockIn, blockOut, rk);210211for (j = 0; j < remainder; j++)212Output[i * 16 + j] = Input[i * 16 + j] ^ blockOut[j];213*OutputLength += remainder;214215}216217void WapiSMS4Encryption(u8 *Key, u8 *IV, u8 *Input, u16 InputLength,218u8 *Output, u16 *OutputLength)219{220221WapiSMS4Cryption(Key, IV, Input, InputLength, Output, OutputLength, ENCRYPT);222}223224void WapiSMS4Decryption(u8 *Key, u8 *IV, u8 *Input, u16 InputLength,225u8 *Output, u16 *OutputLength)226{227/* OFB mode: is also ENCRYPT flag */228WapiSMS4Cryption(Key, IV, Input, InputLength, Output, OutputLength, ENCRYPT);229}230231void WapiSMS4CalculateMic(u8 *Key, u8 *IV, u8 *Input1, u8 Input1Length,232u8 *Input2, u16 Input2Length, u8 *Output, u8 *OutputLength)233{234u32 blockNum, i, remainder, rk[32];235u8 BlockIn[16], BlockOut[16], TempBlock[16], tempIV[16], k;236237*OutputLength = 0;238remainder = Input1Length & 0x0F;239blockNum = Input1Length >> 4;240241for (k = 0; k < 16; k++)242tempIV[k] = IV[15 - k];243244memcpy(BlockIn, tempIV, 16);245246SMS4KeyExt((u8 *)Key, rk, ENCRYPT);247248SMS4Crypt((u8 *)BlockIn, BlockOut, rk);249250for (i = 0; i < blockNum; i++) {251xor_block(BlockIn, (Input1 + i * 16), BlockOut);252SMS4Crypt((u8 *)BlockIn, BlockOut, rk);253}254255if (remainder != 0) {256memset(TempBlock, 0, 16);257memcpy(TempBlock, (Input1 + blockNum * 16), remainder);258259xor_block(BlockIn, TempBlock, BlockOut);260SMS4Crypt((u8 *)BlockIn, BlockOut, rk);261}262263remainder = Input2Length & 0x0F;264blockNum = Input2Length >> 4;265266for (i = 0; i < blockNum; i++) {267xor_block(BlockIn, (Input2 + i * 16), BlockOut);268SMS4Crypt((u8 *)BlockIn, BlockOut, rk);269}270271if (remainder != 0) {272memset(TempBlock, 0, 16);273memcpy(TempBlock, (Input2 + blockNum * 16), remainder);274275xor_block(BlockIn, TempBlock, BlockOut);276SMS4Crypt((u8 *)BlockIn, BlockOut, rk);277}278279memcpy(Output, BlockOut, 16);280*OutputLength = 16;281}282283void SecCalculateMicSMS4(284u8 KeyIdx,285u8 *MicKey,286u8 *pHeader,287u8 *pData,288u16 DataLen,289u8 *MicBuffer290)291{292#if 0293struct ieee80211_hdr_3addr_qos *header;294u8 TempBuf[34], TempLen = 32, MicLen, QosOffset, *IV;295u16 *pTemp, fc;296297WAPI_TRACE(WAPI_TX | WAPI_RX, "=========>%s\n", __FUNCTION__);298299header = (struct ieee80211_hdr_3addr_qos *)pHeader;300memset(TempBuf, 0, 34);301memcpy(TempBuf, pHeader, 2); /* FrameCtrl */302pTemp = (u16 *)TempBuf;303*pTemp &= 0xc78f; /* bit4,5,6,11,12,13 */304305memcpy((TempBuf + 2), (pHeader + 4), 12); /* Addr1, Addr2 */306memcpy((TempBuf + 14), (pHeader + 22), 2); /* SeqCtrl */307pTemp = (u16 *)(TempBuf + 14);308*pTemp &= 0x000f;309310memcpy((TempBuf + 16), (pHeader + 16), 6); /* Addr3 */311312fc = le16_to_cpu(header->frame_ctl);313314315316if (GetFrDs((u16 *)&fc) && GetToDs((u16 *)&fc)) {317memcpy((TempBuf + 22), (pHeader + 24), 6);318QosOffset = 30;319} else {320memset((TempBuf + 22), 0, 6);321QosOffset = 24;322}323324if ((fc & 0x0088) == 0x0088) {325memcpy((TempBuf + 28), (pHeader + QosOffset), 2);326TempLen += 2;327/* IV = pHeader + QosOffset + 2 + SNAP_SIZE + sizeof(u16) + 2; */328IV = pHeader + QosOffset + 2 + 2;329} else {330IV = pHeader + QosOffset + 2;331/* IV = pHeader + QosOffset + SNAP_SIZE + sizeof(u16) + 2; */332}333334TempBuf[TempLen - 1] = (u8)(DataLen & 0xff);335TempBuf[TempLen - 2] = (u8)((DataLen & 0xff00) >> 8);336TempBuf[TempLen - 4] = KeyIdx;337338WAPI_DATA(WAPI_TX, "CalculateMic - KEY", MicKey, 16);339WAPI_DATA(WAPI_TX, "CalculateMic - IV", IV, 16);340WAPI_DATA(WAPI_TX, "CalculateMic - TempBuf", TempBuf, TempLen);341WAPI_DATA(WAPI_TX, "CalculateMic - pData", pData, DataLen);342343WapiSMS4CalculateMic(MicKey, IV, TempBuf, TempLen,344pData, DataLen, MicBuffer, &MicLen);345346if (MicLen != 16)347WAPI_TRACE(WAPI_ERR, "%s: MIC Length Error!!\n", __FUNCTION__);348349WAPI_TRACE(WAPI_TX | WAPI_RX, "<=========%s\n", __FUNCTION__);350#endif351}352353/* AddCount: 1 or 2.354* If overflow, return 1,355* else return 0.356*/357u8 WapiIncreasePN(u8 *PN, u8 AddCount)358{359u8 i;360361if (NULL == PN)362return 1;363/* YJ,test,091102 */364/*365if(AddCount == 2){366RTW_INFO("############################%s(): PN[0]=0x%x\n", __FUNCTION__, PN[0]);367if(PN[0] == 0x48){368PN[0] += AddCount;369return 1;370}else{371PN[0] += AddCount;372return 0;373}374}375*/376/* YJ,test,091102,end */377378for (i = 0; i < 16; i++) {379if (PN[i] + AddCount <= 0xff) {380PN[i] += AddCount;381return 0;382} else {383PN[i] += AddCount;384AddCount = 1;385}386}387return 1;388}389390391void WapiGetLastRxUnicastPNForQoSData(392u8 UserPriority,393PRT_WAPI_STA_INFO pWapiStaInfo,394u8 *PNOut395)396{397WAPI_TRACE(WAPI_RX, "===========> %s\n", __FUNCTION__);398switch (UserPriority) {399case 0:400case 3:401memcpy(PNOut, pWapiStaInfo->lastRxUnicastPNBEQueue, 16);402break;403case 1:404case 2:405memcpy(PNOut, pWapiStaInfo->lastRxUnicastPNBKQueue, 16);406break;407case 4:408case 5:409memcpy(PNOut, pWapiStaInfo->lastRxUnicastPNVIQueue, 16);410break;411case 6:412case 7:413memcpy(PNOut, pWapiStaInfo->lastRxUnicastPNVOQueue, 16);414break;415default:416WAPI_TRACE(WAPI_ERR, "%s: Unknown TID\n", __FUNCTION__);417break;418}419WAPI_TRACE(WAPI_RX, "<=========== %s\n", __FUNCTION__);420}421422423void WapiSetLastRxUnicastPNForQoSData(424u8 UserPriority,425u8 *PNIn,426PRT_WAPI_STA_INFO pWapiStaInfo427)428{429WAPI_TRACE(WAPI_RX, "===========> %s\n", __FUNCTION__);430switch (UserPriority) {431case 0:432case 3:433memcpy(pWapiStaInfo->lastRxUnicastPNBEQueue, PNIn, 16);434break;435case 1:436case 2:437memcpy(pWapiStaInfo->lastRxUnicastPNBKQueue, PNIn, 16);438break;439case 4:440case 5:441memcpy(pWapiStaInfo->lastRxUnicastPNVIQueue, PNIn, 16);442break;443case 6:444case 7:445memcpy(pWapiStaInfo->lastRxUnicastPNVOQueue, PNIn, 16);446break;447default:448WAPI_TRACE(WAPI_ERR, "%s: Unknown TID\n", __FUNCTION__);449break;450}451WAPI_TRACE(WAPI_RX, "<=========== %s\n", __FUNCTION__);452}453454455/****************************************************************************456FALSE not RX-Reorder457TRUE do RX Reorder458add to support WAPI to N-mode459*****************************************************************************/460u8 WapiCheckPnInSwDecrypt(461_adapter *padapter,462struct sk_buff *pskb463)464{465u8 ret = false;466467#if 0468struct ieee80211_hdr_3addr_qos *header;469u16 fc;470u8 *pDaddr, *pTaddr, *pRaddr;471472header = (struct ieee80211_hdr_3addr_qos *)pskb->data;473pTaddr = header->addr2;474pRaddr = header->addr1;475fc = le16_to_cpu(header->frame_ctl);476477if (GetToDs(&fc))478pDaddr = header->addr3;479else480pDaddr = header->addr1;481482if ((_rtw_memcmp(pRaddr, padapter->pnetdev->dev_addr, ETH_ALEN) == 0)483&& !(pDaddr)484&& (GetFrameType(&fc) == WIFI_QOS_DATA_TYPE))485/* && ieee->pHTInfo->bCurrentHTSupport && */486/* ieee->pHTInfo->bCurRxReorderEnable) */487ret = false;488else489ret = true;490#endif491WAPI_TRACE(WAPI_RX, "%s: return %d\n", __FUNCTION__, ret);492return ret;493}494495int SecSMS4HeaderFillIV(_adapter *padapter, u8 *pxmitframe)496{497struct pkt_attrib *pattrib = &((struct xmit_frame *)pxmitframe)->attrib;498u8 *frame = ((struct xmit_frame *)pxmitframe)->buf_addr + TXDESC_OFFSET;499u8 *pSecHeader = NULL, *pos = NULL, *pRA = NULL;500u8 bPNOverflow = false, bFindMatchPeer = false, hdr_len = 0;501PWLAN_HEADER_WAPI_EXTENSION pWapiExt = NULL;502PRT_WAPI_T pWapiInfo = &padapter->wapiInfo;503PRT_WAPI_STA_INFO pWapiSta = NULL;504int ret = 0;505506WAPI_TRACE(WAPI_TX, "=========>%s\n", __FUNCTION__);507508return ret;509#if 0510hdr_len = sMacHdrLng;511if (GetFrameType(pskb->data) == WIFI_QOS_DATA_TYPE)512hdr_len += 2;513/* hdr_len += SNAP_SIZE + sizeof(u16); */514515pos = skb_push(pskb, padapter->wapiInfo.extra_prefix_len);516memmove(pos, pos + padapter->wapiInfo.extra_prefix_len, hdr_len);517518pSecHeader = pskb->data + hdr_len;519pWapiExt = (PWLAN_HEADER_WAPI_EXTENSION)pSecHeader;520pRA = pskb->data + 4;521522WAPI_DATA(WAPI_TX, "FillIV - Before Fill IV", pskb->data, pskb->len);523524/* Address 1 is always receiver's address */525if (IS_MCAST(pRA)) {526if (!pWapiInfo->wapiTxMsk.bTxEnable) {527WAPI_TRACE(WAPI_ERR, "%s: bTxEnable = 0!!\n", __FUNCTION__);528return -2;529}530if (pWapiInfo->wapiTxMsk.keyId <= 1) {531pWapiExt->KeyIdx = pWapiInfo->wapiTxMsk.keyId;532pWapiExt->Reserved = 0;533bPNOverflow = WapiIncreasePN(pWapiInfo->lastTxMulticastPN, 1);534memcpy(pWapiExt->PN, pWapiInfo->lastTxMulticastPN, 16);535if (bPNOverflow) {536/* Update MSK Notification. */537WAPI_TRACE(WAPI_ERR, "===============>%s():multicast PN overflow\n", __FUNCTION__);538rtw_wapi_app_event_handler(padapter, NULL, 0, pRA, false, false, true, 0, false);539}540} else {541WAPI_TRACE(WAPI_ERR, "%s: Invalid Wapi Multicast KeyIdx!!\n", __FUNCTION__);542ret = -3;543}544} else {545list_for_each_entry(pWapiSta, &pWapiInfo->wapiSTAUsedList, list) {546if (!memcmp(pWapiSta->PeerMacAddr, pRA, 6)) {547bFindMatchPeer = true;548break;549}550}551if (bFindMatchPeer) {552if ((!pWapiSta->wapiUskUpdate.bTxEnable) && (!pWapiSta->wapiUsk.bTxEnable)) {553WAPI_TRACE(WAPI_ERR, "%s: bTxEnable = 0!!\n", __FUNCTION__);554return -4;555}556if (pWapiSta->wapiUsk.keyId <= 1) {557if (pWapiSta->wapiUskUpdate.bTxEnable)558pWapiExt->KeyIdx = pWapiSta->wapiUskUpdate.keyId;559else560pWapiExt->KeyIdx = pWapiSta->wapiUsk.keyId;561562pWapiExt->Reserved = 0;563bPNOverflow = WapiIncreasePN(pWapiSta->lastTxUnicastPN, 2);564memcpy(pWapiExt->PN, pWapiSta->lastTxUnicastPN, 16);565if (bPNOverflow) {566/* Update USK Notification. */567WAPI_TRACE(WAPI_ERR, "===============>%s():unicast PN overflow\n", __FUNCTION__);568rtw_wapi_app_event_handler(padapter, NULL, 0, pWapiSta->PeerMacAddr, false, true, false, 0, false);569}570} else {571WAPI_TRACE(WAPI_ERR, "%s: Invalid Wapi Unicast KeyIdx!!\n", __FUNCTION__);572ret = -5;573}574} else {575WAPI_TRACE(WAPI_ERR, "%s: Can not find Peer Sta "MAC_FMT"!!\n", __FUNCTION__, MAC_ARG(pRA));576ret = -6;577}578}579580WAPI_DATA(WAPI_TX, "FillIV - After Fill IV", pskb->data, pskb->len);581WAPI_TRACE(WAPI_TX, "<=========%s\n", __FUNCTION__);582return ret;583#endif584}585586/* WAPI SW Enc: must have done Coalesce! */587void SecSWSMS4Encryption(588_adapter *padapter,589u8 *pxmitframe590)591{592PRT_WAPI_T pWapiInfo = &padapter->wapiInfo;593PRT_WAPI_STA_INFO pWapiSta = NULL;594u8 *pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + TXDESC_SIZE;595struct pkt_attrib *pattrib = &((struct xmit_frame *)pxmitframe)->attrib;596597u8 *SecPtr = NULL, *pRA, *pMicKey = NULL, *pDataKey = NULL, *pIV = NULL;598u8 IVOffset, DataOffset, bFindMatchPeer = false, KeyIdx = 0, MicBuffer[16];599u16 OutputLength;600601WAPI_TRACE(WAPI_TX, "=========>%s\n", __FUNCTION__);602603WAPI_TRACE(WAPI_TX, "hdrlen: %d\n", pattrib->hdrlen);604605return;606607DataOffset = pattrib->hdrlen + pattrib->iv_len;608609pRA = pframe + 4;610611612if (IS_MCAST(pRA)) {613KeyIdx = pWapiInfo->wapiTxMsk.keyId;614pIV = pWapiInfo->lastTxMulticastPN;615pMicKey = pWapiInfo->wapiTxMsk.micKey;616pDataKey = pWapiInfo->wapiTxMsk.dataKey;617} else {618if (!list_empty(&(pWapiInfo->wapiSTAUsedList))) {619list_for_each_entry(pWapiSta, &pWapiInfo->wapiSTAUsedList, list) {620if (0 == memcmp(pWapiSta->PeerMacAddr, pRA, 6)) {621bFindMatchPeer = true;622break;623}624}625626if (bFindMatchPeer) {627if (pWapiSta->wapiUskUpdate.bTxEnable) {628KeyIdx = pWapiSta->wapiUskUpdate.keyId;629WAPI_TRACE(WAPI_TX, "%s(): Use update USK!! KeyIdx=%d\n", __FUNCTION__, KeyIdx);630pIV = pWapiSta->lastTxUnicastPN;631pMicKey = pWapiSta->wapiUskUpdate.micKey;632pDataKey = pWapiSta->wapiUskUpdate.dataKey;633} else {634KeyIdx = pWapiSta->wapiUsk.keyId;635WAPI_TRACE(WAPI_TX, "%s(): Use USK!! KeyIdx=%d\n", __FUNCTION__, KeyIdx);636pIV = pWapiSta->lastTxUnicastPN;637pMicKey = pWapiSta->wapiUsk.micKey;638pDataKey = pWapiSta->wapiUsk.dataKey;639}640} else {641WAPI_TRACE(WAPI_ERR, "%s: Can not find Peer Sta!!\n", __FUNCTION__);642return;643}644} else {645WAPI_TRACE(WAPI_ERR, "%s: wapiSTAUsedList is empty!!\n", __FUNCTION__);646return;647}648}649650SecPtr = pframe;651SecCalculateMicSMS4(KeyIdx, pMicKey, SecPtr, (SecPtr + DataOffset), pattrib->pktlen, MicBuffer);652653WAPI_DATA(WAPI_TX, "Encryption - MIC", MicBuffer, padapter->wapiInfo.extra_postfix_len);654655memcpy(pframe + pattrib->hdrlen + pattrib->iv_len + pattrib->pktlen - pattrib->icv_len,656(u8 *)MicBuffer,657padapter->wapiInfo.extra_postfix_len658);659660661WapiSMS4Encryption(pDataKey, pIV, (SecPtr + DataOffset), pattrib->pktlen + pattrib->icv_len, (SecPtr + DataOffset), &OutputLength);662663WAPI_DATA(WAPI_TX, "Encryption - After SMS4 encryption", pframe, pattrib->hdrlen + pattrib->iv_len + pattrib->pktlen);664665WAPI_TRACE(WAPI_TX, "<=========%s\n", __FUNCTION__);666}667668u8 SecSWSMS4Decryption(669_adapter *padapter,670u8 *precv_frame,671struct recv_priv *precv_priv672)673{674PRT_WAPI_T pWapiInfo = &padapter->wapiInfo;675struct recv_frame_hdr *precv_hdr;676PRT_WAPI_STA_INFO pWapiSta = NULL;677u8 IVOffset, DataOffset, bFindMatchPeer = false, bUseUpdatedKey = false;678u8 KeyIdx, MicBuffer[16], lastRxPNforQoS[16];679u8 *pRA, *pTA, *pMicKey, *pDataKey, *pLastRxPN, *pRecvPN, *pSecData, *pRecvMic, *pos;680u8 TID = 0;681u16 OutputLength, DataLen;682u8 bQosData;683struct sk_buff *pskb;684685WAPI_TRACE(WAPI_RX, "=========>%s\n", __FUNCTION__);686687return 0;688689precv_hdr = &((union recv_frame *)precv_frame)->u.hdr;690pskb = (struct sk_buff *)(precv_hdr->rx_data);691precv_hdr->bWapiCheckPNInDecrypt = WapiCheckPnInSwDecrypt(padapter, pskb);692WAPI_TRACE(WAPI_RX, "=========>%s: check PN %d\n", __FUNCTION__, precv_hdr->bWapiCheckPNInDecrypt);693WAPI_DATA(WAPI_RX, "Decryption - Before decryption", pskb->data, pskb->len);694695IVOffset = sMacHdrLng;696bQosData = GetFrameType(pskb->data) == WIFI_QOS_DATA_TYPE;697if (bQosData)698IVOffset += 2;699700/* if(GetHTC()) */701/* IVOffset += 4; */702703/* IVOffset += SNAP_SIZE + sizeof(u16); */704705DataOffset = IVOffset + padapter->wapiInfo.extra_prefix_len;706707pRA = pskb->data + 4;708pTA = pskb->data + 10;709KeyIdx = *(pskb->data + IVOffset);710pRecvPN = pskb->data + IVOffset + 2;711pSecData = pskb->data + DataOffset;712DataLen = pskb->len - DataOffset;713pRecvMic = pskb->data + pskb->len - padapter->wapiInfo.extra_postfix_len;714TID = GetTid(pskb->data);715716if (!list_empty(&(pWapiInfo->wapiSTAUsedList))) {717list_for_each_entry(pWapiSta, &pWapiInfo->wapiSTAUsedList, list) {718if (0 == memcmp(pWapiSta->PeerMacAddr, pTA, 6)) {719bFindMatchPeer = true;720break;721}722}723}724725if (!bFindMatchPeer) {726WAPI_TRACE(WAPI_ERR, "%s: Can not find Peer Sta "MAC_FMT" for Key Info!!!\n", __FUNCTION__, MAC_ARG(pTA));727return false;728}729730if (IS_MCAST(pRA)) {731WAPI_TRACE(WAPI_RX, "%s: Multicast decryption !!!\n", __FUNCTION__);732if (pWapiSta->wapiMsk.keyId == KeyIdx && pWapiSta->wapiMsk.bSet) {733pLastRxPN = pWapiSta->lastRxMulticastPN;734if (!WapiComparePN(pRecvPN, pLastRxPN)) {735WAPI_TRACE(WAPI_ERR, "%s: MSK PN is not larger than last, Dropped!!!\n", __FUNCTION__);736WAPI_DATA(WAPI_ERR, "pRecvPN:", pRecvPN, 16);737WAPI_DATA(WAPI_ERR, "pLastRxPN:", pLastRxPN, 16);738return false;739}740741memcpy(pLastRxPN, pRecvPN, 16);742pMicKey = pWapiSta->wapiMsk.micKey;743pDataKey = pWapiSta->wapiMsk.dataKey;744} else if (pWapiSta->wapiMskUpdate.keyId == KeyIdx && pWapiSta->wapiMskUpdate.bSet) {745WAPI_TRACE(WAPI_RX, "%s: Use Updated MSK for Decryption !!!\n", __FUNCTION__);746bUseUpdatedKey = true;747memcpy(pWapiSta->lastRxMulticastPN, pRecvPN, 16);748pMicKey = pWapiSta->wapiMskUpdate.micKey;749pDataKey = pWapiSta->wapiMskUpdate.dataKey;750} else {751WAPI_TRACE(WAPI_ERR, "%s: Can not find MSK with matched KeyIdx(%d), Dropped !!!\n", __FUNCTION__, KeyIdx);752return false;753}754} else {755WAPI_TRACE(WAPI_RX, "%s: Unicast decryption !!!\n", __FUNCTION__);756if (pWapiSta->wapiUsk.keyId == KeyIdx && pWapiSta->wapiUsk.bSet) {757WAPI_TRACE(WAPI_RX, "%s: Use USK for Decryption!!!\n", __FUNCTION__);758if (precv_hdr->bWapiCheckPNInDecrypt) {759if (GetFrameType(pskb->data) == WIFI_QOS_DATA_TYPE) {760WapiGetLastRxUnicastPNForQoSData(TID, pWapiSta, lastRxPNforQoS);761pLastRxPN = lastRxPNforQoS;762} else763pLastRxPN = pWapiSta->lastRxUnicastPN;764if (!WapiComparePN(pRecvPN, pLastRxPN))765return false;766if (bQosData)767WapiSetLastRxUnicastPNForQoSData(TID, pRecvPN, pWapiSta);768else769memcpy(pWapiSta->lastRxUnicastPN, pRecvPN, 16);770} else771memcpy(precv_hdr->WapiTempPN, pRecvPN, 16);772773if (check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE)) {774if ((pRecvPN[0] & 0x1) == 0) {775WAPI_TRACE(WAPI_ERR, "%s: Rx USK PN is not odd when Infra STA mode, Dropped !!!\n", __FUNCTION__);776return false;777}778}779780pMicKey = pWapiSta->wapiUsk.micKey;781pDataKey = pWapiSta->wapiUsk.dataKey;782} else if (pWapiSta->wapiUskUpdate.keyId == KeyIdx && pWapiSta->wapiUskUpdate.bSet) {783WAPI_TRACE(WAPI_RX, "%s: Use Updated USK for Decryption!!!\n", __FUNCTION__);784if (pWapiSta->bAuthenticatorInUpdata)785bUseUpdatedKey = true;786else787bUseUpdatedKey = false;788789if (bQosData)790WapiSetLastRxUnicastPNForQoSData(TID, pRecvPN, pWapiSta);791else792memcpy(pWapiSta->lastRxUnicastPN, pRecvPN, 16);793pMicKey = pWapiSta->wapiUskUpdate.micKey;794pDataKey = pWapiSta->wapiUskUpdate.dataKey;795} else {796WAPI_TRACE(WAPI_ERR, "%s: No valid USK!!!KeyIdx=%d pWapiSta->wapiUsk.keyId=%d pWapiSta->wapiUskUpdate.keyId=%d\n", __FUNCTION__, KeyIdx, pWapiSta->wapiUsk.keyId,797pWapiSta->wapiUskUpdate.keyId);798/* dump_buf(pskb->data,pskb->len); */799return false;800}801}802803WAPI_DATA(WAPI_RX, "Decryption - DataKey", pDataKey, 16);804WAPI_DATA(WAPI_RX, "Decryption - IV", pRecvPN, 16);805WapiSMS4Decryption(pDataKey, pRecvPN, pSecData, DataLen, pSecData, &OutputLength);806807if (OutputLength != DataLen)808WAPI_TRACE(WAPI_ERR, "%s: Output Length Error!!!!\n", __FUNCTION__);809810WAPI_DATA(WAPI_RX, "Decryption - After decryption", pskb->data, pskb->len);811812DataLen -= padapter->wapiInfo.extra_postfix_len;813814SecCalculateMicSMS4(KeyIdx, pMicKey, pskb->data, pSecData, DataLen, MicBuffer);815816WAPI_DATA(WAPI_RX, "Decryption - MIC received", pRecvMic, SMS4_MIC_LEN);817WAPI_DATA(WAPI_RX, "Decryption - MIC calculated", MicBuffer, SMS4_MIC_LEN);818819if (0 == memcmp(MicBuffer, pRecvMic, padapter->wapiInfo.extra_postfix_len)) {820WAPI_TRACE(WAPI_RX, "%s: Check MIC OK!!\n", __FUNCTION__);821if (bUseUpdatedKey) {822/* delete the old key */823if (IS_MCAST(pRA)) {824WAPI_TRACE(WAPI_API, "%s(): AE use new update MSK!!\n", __FUNCTION__);825pWapiSta->wapiMsk.keyId = pWapiSta->wapiMskUpdate.keyId;826memcpy(pWapiSta->wapiMsk.dataKey, pWapiSta->wapiMskUpdate.dataKey, 16);827memcpy(pWapiSta->wapiMsk.micKey, pWapiSta->wapiMskUpdate.micKey, 16);828pWapiSta->wapiMskUpdate.bTxEnable = pWapiSta->wapiMskUpdate.bSet = false;829} else {830WAPI_TRACE(WAPI_API, "%s(): AE use new update USK!!\n", __FUNCTION__);831pWapiSta->wapiUsk.keyId = pWapiSta->wapiUskUpdate.keyId;832memcpy(pWapiSta->wapiUsk.dataKey, pWapiSta->wapiUskUpdate.dataKey, 16);833memcpy(pWapiSta->wapiUsk.micKey, pWapiSta->wapiUskUpdate.micKey, 16);834pWapiSta->wapiUskUpdate.bTxEnable = pWapiSta->wapiUskUpdate.bSet = false;835}836}837} else {838WAPI_TRACE(WAPI_ERR, "%s: Check MIC Error, Dropped !!!!\n", __FUNCTION__);839return false;840}841842pos = pskb->data;843memmove(pos + padapter->wapiInfo.extra_prefix_len, pos, IVOffset);844skb_pull(pskb, padapter->wapiInfo.extra_prefix_len);845846WAPI_TRACE(WAPI_RX, "<=========%s\n", __FUNCTION__);847848return true;849}850851u32 rtw_sms4_encrypt(_adapter *padapter, u8 *pxmitframe)852{853854u8 *pframe;855u32 res = _SUCCESS;856857WAPI_TRACE(WAPI_TX, "=========>%s\n", __FUNCTION__);858859if ((!padapter->WapiSupport) || (!padapter->wapiInfo.bWapiEnable)) {860WAPI_TRACE(WAPI_TX, "<========== %s, WAPI not supported or enabled!\n", __FUNCTION__);861return _FAIL;862}863864if (((struct xmit_frame *)pxmitframe)->buf_addr == NULL)865return _FAIL;866867pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + TXDESC_OFFSET;868869SecSWSMS4Encryption(padapter, pxmitframe);870871WAPI_TRACE(WAPI_TX, "<=========%s\n", __FUNCTION__);872return res;873}874875u32 rtw_sms4_decrypt(_adapter *padapter, u8 *precvframe)876{877u8 *pframe;878u32 res = _SUCCESS;879880WAPI_TRACE(WAPI_RX, "=========>%s\n", __FUNCTION__);881882if ((!padapter->WapiSupport) || (!padapter->wapiInfo.bWapiEnable)) {883WAPI_TRACE(WAPI_RX, "<========== %s, WAPI not supported or enabled!\n", __FUNCTION__);884return _FAIL;885}886887888/* drop packet when hw decrypt fail889* return tempraily */890return _FAIL;891892/* pframe=(unsigned char *)((union recv_frame*)precvframe)->u.hdr.rx_data; */893894if (false == SecSWSMS4Decryption(padapter, precvframe, &padapter->recvpriv)) {895WAPI_TRACE(WAPI_ERR, "%s():SMS4 decrypt frame error\n", __FUNCTION__);896return _FAIL;897}898899WAPI_TRACE(WAPI_RX, "<=========%s\n", __FUNCTION__);900return res;901}902903#else904905u32 rtw_sms4_encrypt(_adapter *padapter, u8 *pxmitframe)906{907WAPI_TRACE(WAPI_TX, "=========>Dummy %s\n", __FUNCTION__);908WAPI_TRACE(WAPI_TX, "<=========Dummy %s\n", __FUNCTION__);909return _SUCCESS;910}911912u32 rtw_sms4_decrypt(_adapter *padapter, u8 *precvframe)913{914WAPI_TRACE(WAPI_RX, "=========>Dummy %s\n", __FUNCTION__);915WAPI_TRACE(WAPI_RX, "<=========Dummy %s\n", __FUNCTION__);916return _SUCCESS;917}918919#endif920921#endif922923924