mirror of
https://github.com/torvalds/linux.git
synced 2026-07-27 17:47:41 +02:00
staging: rtl8723bs: reduce nesting in rtw_security.c
Improve readability of functions: rtw_tkip_encrypt, rtw_tkip_decrypt, rtw_aes_encrypt in rtw_security.c by adding early returns for the encryption type check and the stainfo null check. Signed-off-by: Maksym Pikhotskyi <mpikhotskyi@gmail.com> Reviewed-by: Luka Gejak <luka.gejak@linux.dev> Link: https://patch.msgid.link/20260417095452.23440-2-mpikhotskyi@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
9a3f9b3c47
commit
13d883fa23
|
|
@ -449,47 +449,45 @@ u32 rtw_tkip_encrypt(struct adapter *padapter, u8 *pxmitframe)
|
|||
pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + hw_hdr_offset;
|
||||
|
||||
/* 4 start to encrypt each fragment */
|
||||
if (pattrib->encrypt == _TKIP_) {
|
||||
if (pattrib->encrypt != _TKIP_)
|
||||
return _SUCCESS;
|
||||
|
||||
{
|
||||
if (is_multicast_ether_addr(pattrib->ra))
|
||||
prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey;
|
||||
else
|
||||
prwskey = pattrib->dot118021x_UncstKey.skey;
|
||||
if (is_multicast_ether_addr(pattrib->ra))
|
||||
prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey;
|
||||
else
|
||||
prwskey = pattrib->dot118021x_UncstKey.skey;
|
||||
|
||||
for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
|
||||
iv = pframe + pattrib->hdrlen;
|
||||
payload = pframe + pattrib->iv_len + pattrib->hdrlen;
|
||||
for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
|
||||
iv = pframe + pattrib->hdrlen;
|
||||
payload = pframe + pattrib->iv_len + pattrib->hdrlen;
|
||||
|
||||
GET_TKIP_PN(iv, dot11txpn);
|
||||
GET_TKIP_PN(iv, dot11txpn);
|
||||
|
||||
pnl = (u16)(dot11txpn.val);
|
||||
pnh = (u32)(dot11txpn.val >> 16);
|
||||
pnl = (u16)(dot11txpn.val);
|
||||
pnh = (u32)(dot11txpn.val >> 16);
|
||||
|
||||
phase1((u16 *)&ttkey[0], prwskey, &pattrib->ta[0], pnh);
|
||||
phase1((u16 *)&ttkey[0], prwskey, &pattrib->ta[0], pnh);
|
||||
|
||||
phase2(&rc4key[0], prwskey, (u16 *)&ttkey[0], pnl);
|
||||
phase2(&rc4key[0], prwskey, (u16 *)&ttkey[0], pnl);
|
||||
|
||||
if ((curfragnum + 1) == pattrib->nr_frags) { /* 4 the last fragment */
|
||||
length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
crc.f0 = cpu_to_le32(~crc32_le(~0, payload, length));
|
||||
if ((curfragnum + 1) == pattrib->nr_frags) { /* 4 the last fragment */
|
||||
length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
crc.f0 = cpu_to_le32(~crc32_le(~0, payload, length));
|
||||
|
||||
arc4_setkey(ctx, rc4key, 16);
|
||||
arc4_crypt(ctx, payload, payload, length);
|
||||
arc4_crypt(ctx, payload + length, crc.f1, 4);
|
||||
arc4_setkey(ctx, rc4key, 16);
|
||||
arc4_crypt(ctx, payload, payload, length);
|
||||
arc4_crypt(ctx, payload + length, crc.f1, 4);
|
||||
|
||||
} else {
|
||||
length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
crc.f0 = cpu_to_le32(~crc32_le(~0, payload, length));
|
||||
} else {
|
||||
length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
crc.f0 = cpu_to_le32(~crc32_le(~0, payload, length));
|
||||
|
||||
arc4_setkey(ctx, rc4key, 16);
|
||||
arc4_crypt(ctx, payload, payload, length);
|
||||
arc4_crypt(ctx, payload + length, crc.f1, 4);
|
||||
arc4_setkey(ctx, rc4key, 16);
|
||||
arc4_crypt(ctx, payload, payload, length);
|
||||
arc4_crypt(ctx, payload + length, crc.f1, 4);
|
||||
|
||||
pframe += pxmitpriv->frag_len;
|
||||
pframe = (u8 *)round_up((SIZE_PTR)(pframe), 4);
|
||||
}
|
||||
}
|
||||
pframe += pxmitpriv->frag_len;
|
||||
pframe = (u8 *)round_up((SIZE_PTR)(pframe), 4);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
|
@ -517,82 +515,82 @@ u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe)
|
|||
pframe = (unsigned char *)((union recv_frame *)precvframe)->u.hdr.rx_data;
|
||||
|
||||
/* 4 start to decrypt recvframe */
|
||||
if (prxattrib->encrypt == _TKIP_) {
|
||||
stainfo = rtw_get_stainfo(&padapter->stapriv, &prxattrib->ta[0]);
|
||||
if (stainfo) {
|
||||
if (is_multicast_ether_addr(prxattrib->ra)) {
|
||||
static unsigned long start;
|
||||
static u32 no_gkey_bc_cnt;
|
||||
static u32 no_gkey_mc_cnt;
|
||||
if (prxattrib->encrypt != _TKIP_)
|
||||
return _SUCCESS;
|
||||
|
||||
if (!psecuritypriv->binstallGrpkey) {
|
||||
res = _FAIL;
|
||||
stainfo = rtw_get_stainfo(&padapter->stapriv, &prxattrib->ta[0]);
|
||||
if (!stainfo)
|
||||
return _FAIL;
|
||||
|
||||
if (start == 0)
|
||||
start = jiffies;
|
||||
if (is_multicast_ether_addr(prxattrib->ra)) {
|
||||
static unsigned long start;
|
||||
static u32 no_gkey_bc_cnt;
|
||||
static u32 no_gkey_mc_cnt;
|
||||
|
||||
if (is_broadcast_ether_addr(prxattrib->ra))
|
||||
no_gkey_bc_cnt++;
|
||||
else
|
||||
no_gkey_mc_cnt++;
|
||||
if (!psecuritypriv->binstallGrpkey) {
|
||||
res = _FAIL;
|
||||
|
||||
if (jiffies_to_msecs(jiffies - start) > 1000) {
|
||||
if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
|
||||
netdev_dbg(padapter->pnetdev,
|
||||
FUNC_ADPT_FMT " no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
|
||||
FUNC_ADPT_ARG(padapter),
|
||||
no_gkey_bc_cnt,
|
||||
no_gkey_mc_cnt);
|
||||
}
|
||||
start = jiffies;
|
||||
no_gkey_bc_cnt = 0;
|
||||
no_gkey_mc_cnt = 0;
|
||||
}
|
||||
goto exit;
|
||||
}
|
||||
if (start == 0)
|
||||
start = jiffies;
|
||||
|
||||
if (is_broadcast_ether_addr(prxattrib->ra))
|
||||
no_gkey_bc_cnt++;
|
||||
else
|
||||
no_gkey_mc_cnt++;
|
||||
|
||||
if (jiffies_to_msecs(jiffies - start) > 1000) {
|
||||
if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
|
||||
netdev_dbg(padapter->pnetdev,
|
||||
FUNC_ADPT_FMT " gkey installed. no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
|
||||
FUNC_ADPT_FMT " no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
|
||||
FUNC_ADPT_ARG(padapter),
|
||||
no_gkey_bc_cnt,
|
||||
no_gkey_mc_cnt);
|
||||
}
|
||||
start = 0;
|
||||
start = jiffies;
|
||||
no_gkey_bc_cnt = 0;
|
||||
no_gkey_mc_cnt = 0;
|
||||
|
||||
prwskey = psecuritypriv->dot118021XGrpKey[prxattrib->key_index].skey;
|
||||
} else {
|
||||
prwskey = &stainfo->dot118021x_UncstKey.skey[0];
|
||||
}
|
||||
|
||||
iv = pframe + prxattrib->hdrlen;
|
||||
payload = pframe + prxattrib->iv_len + prxattrib->hdrlen;
|
||||
length = ((union recv_frame *)precvframe)->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len;
|
||||
|
||||
GET_TKIP_PN(iv, dot11txpn);
|
||||
|
||||
pnl = (u16)(dot11txpn.val);
|
||||
pnh = (u32)(dot11txpn.val >> 16);
|
||||
|
||||
phase1((u16 *)&ttkey[0], prwskey, &prxattrib->ta[0], pnh);
|
||||
phase2(&rc4key[0], prwskey, (unsigned short *)&ttkey[0], pnl);
|
||||
|
||||
/* 4 decrypt payload include icv */
|
||||
|
||||
arc4_setkey(ctx, rc4key, 16);
|
||||
arc4_crypt(ctx, payload, payload, length);
|
||||
|
||||
*((u32 *)crc) = ~crc32_le(~0, payload, length - 4);
|
||||
|
||||
if (crc[3] != payload[length - 1] || crc[2] != payload[length - 2] ||
|
||||
crc[1] != payload[length - 3] || crc[0] != payload[length - 4])
|
||||
res = _FAIL;
|
||||
} else {
|
||||
res = _FAIL;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (no_gkey_bc_cnt || no_gkey_mc_cnt) {
|
||||
netdev_dbg(padapter->pnetdev,
|
||||
FUNC_ADPT_FMT " gkey installed. no_gkey_bc_cnt:%u, no_gkey_mc_cnt:%u\n",
|
||||
FUNC_ADPT_ARG(padapter),
|
||||
no_gkey_bc_cnt,
|
||||
no_gkey_mc_cnt);
|
||||
}
|
||||
start = 0;
|
||||
no_gkey_bc_cnt = 0;
|
||||
no_gkey_mc_cnt = 0;
|
||||
|
||||
prwskey = psecuritypriv->dot118021XGrpKey[prxattrib->key_index].skey;
|
||||
} else {
|
||||
prwskey = &stainfo->dot118021x_UncstKey.skey[0];
|
||||
}
|
||||
|
||||
iv = pframe + prxattrib->hdrlen;
|
||||
payload = pframe + prxattrib->iv_len + prxattrib->hdrlen;
|
||||
length = ((union recv_frame *)precvframe)->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len;
|
||||
|
||||
GET_TKIP_PN(iv, dot11txpn);
|
||||
|
||||
pnl = (u16)(dot11txpn.val);
|
||||
pnh = (u32)(dot11txpn.val >> 16);
|
||||
|
||||
phase1((u16 *)&ttkey[0], prwskey, &prxattrib->ta[0], pnh);
|
||||
phase2(&rc4key[0], prwskey, (unsigned short *)&ttkey[0], pnl);
|
||||
|
||||
/* 4 decrypt payload include icv */
|
||||
|
||||
arc4_setkey(ctx, rc4key, 16);
|
||||
arc4_crypt(ctx, payload, payload, length);
|
||||
|
||||
*((u32 *)crc) = ~crc32_le(~0, payload, length - 4);
|
||||
|
||||
if (crc[3] != payload[length - 1] || crc[2] != payload[length - 2] ||
|
||||
crc[1] != payload[length - 3] || crc[0] != payload[length - 4])
|
||||
res = _FAIL;
|
||||
exit:
|
||||
return res;
|
||||
}
|
||||
|
|
@ -965,24 +963,25 @@ u32 rtw_aes_encrypt(struct adapter *padapter, u8 *pxmitframe)
|
|||
pframe = ((struct xmit_frame *)pxmitframe)->buf_addr + hw_hdr_offset;
|
||||
|
||||
/* 4 start to encrypt each fragment */
|
||||
if (pattrib->encrypt == _AES_) {
|
||||
if (is_multicast_ether_addr(pattrib->ra))
|
||||
prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey;
|
||||
else
|
||||
prwskey = pattrib->dot118021x_UncstKey.skey;
|
||||
if (pattrib->encrypt != _AES_)
|
||||
return _SUCCESS;
|
||||
|
||||
for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
|
||||
if ((curfragnum + 1) == pattrib->nr_frags) { /* 4 the last fragment */
|
||||
length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
if (is_multicast_ether_addr(pattrib->ra))
|
||||
prwskey = psecuritypriv->dot118021XGrpKey[psecuritypriv->dot118021XGrpKeyid].skey;
|
||||
else
|
||||
prwskey = pattrib->dot118021x_UncstKey.skey;
|
||||
|
||||
aes_cipher(prwskey, pattrib->hdrlen, pframe, length);
|
||||
} else {
|
||||
length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
for (curfragnum = 0; curfragnum < pattrib->nr_frags; curfragnum++) {
|
||||
if ((curfragnum + 1) == pattrib->nr_frags) { /* 4 the last fragment */
|
||||
length = pattrib->last_txcmdsz - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
|
||||
aes_cipher(prwskey, pattrib->hdrlen, pframe, length);
|
||||
pframe += pxmitpriv->frag_len;
|
||||
pframe = (u8 *)round_up((SIZE_PTR)(pframe), 4);
|
||||
}
|
||||
aes_cipher(prwskey, pattrib->hdrlen, pframe, length);
|
||||
} else {
|
||||
length = pxmitpriv->frag_len - pattrib->hdrlen - pattrib->iv_len - pattrib->icv_len;
|
||||
|
||||
aes_cipher(prwskey, pattrib->hdrlen, pframe, length);
|
||||
pframe += pxmitpriv->frag_len;
|
||||
pframe = (u8 *)round_up((SIZE_PTR)(pframe), 4);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user