1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /****************************************************************************** 3 * 4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. 5 * 6 ******************************************************************************/ 7 #ifndef __RTW_SECURITY_H_ 8 #define __RTW_SECURITY_H_ 9 10 #include <osdep_service.h> 11 #include <drv_types.h> 12 13 #define _NO_PRIVACY_ 0x0 14 #define _WEP40_ 0x1 15 #define _TKIP_ 0x2 16 #define _TKIP_WTMIC_ 0x3 17 #define _AES_ 0x4 18 #define _WEP104_ 0x5 19 #define _WEP_WPA_MIXED_ 0x07 /* WEP + WPA */ 20 #define _SMS4_ 0x06 21 22 #define is_wep_enc(alg) (((alg) == _WEP40_) || ((alg) == _WEP104_)) 23 24 #define _WPA_IE_ID_ 0xdd 25 #define _WPA2_IE_ID_ 0x30 26 27 #define SHA256_MAC_LEN 32 28 #define AES_BLOCK_SIZE 16 29 #define AES_PRIV_SIZE (4 * 44) 30 31 enum { 32 ENCRYP_PROTOCOL_OPENSYS, /* open system */ 33 ENCRYP_PROTOCOL_WEP, /* WEP */ 34 ENCRYP_PROTOCOL_WPA, /* WPA */ 35 ENCRYP_PROTOCOL_WPA2, /* WPA2 */ 36 ENCRYP_PROTOCOL_WAPI, /* WAPI: Not support in this version */ 37 ENCRYP_PROTOCOL_MAX 38 }; 39 40 #ifndef Ndis802_11AuthModeWPA2 41 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1) 42 #endif 43 44 #ifndef Ndis802_11AuthModeWPA2PSK 45 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2) 46 #endif 47 48 union pn48 { 49 u64 val; 50 51 #ifdef __LITTLE_ENDIAN 52 struct { 53 u8 TSC0; 54 u8 TSC1; 55 u8 TSC2; 56 u8 TSC3; 57 u8 TSC4; 58 u8 TSC5; 59 u8 TSC6; 60 u8 TSC7; 61 } _byte_; 62 63 #elif defined(__BIG_ENDIAN) 64 65 struct { 66 u8 TSC7; 67 u8 TSC6; 68 u8 TSC5; 69 u8 TSC4; 70 u8 TSC3; 71 u8 TSC2; 72 u8 TSC1; 73 u8 TSC0; 74 } _byte_; 75 #endif 76 }; 77 78 union Keytype { 79 u8 skey[16]; 80 u32 lkey[4]; 81 }; 82 83 struct rt_pmkid_list { 84 u8 used; 85 u8 bssid[ETH_ALEN]; 86 u8 PMKID[16]; 87 u8 SsidBuf[33]; 88 u8 *ssid_octet; 89 u16 ssid_length; 90 }; 91 92 struct security_priv { 93 u32 dot11AuthAlgrthm; /* 802.11 auth, could be open, 94 * shared, 8021x and authswitch 95 */ 96 u32 dot11PrivacyAlgrthm; /* This specify the privacy for 97 * shared auth. algorithm. 98 */ 99 /* WEP */ 100 u32 dot11PrivacyKeyIndex; /* this is only valid for legendary 101 * wep, 0~3 for key id.(tx key index) 102 */ 103 union Keytype dot11DefKey[4]; /* this is only valid for def. key */ 104 u32 dot11DefKeylen[4]; 105 u32 dot118021XGrpPrivacy; /* This specify the privacy algthm. 106 * used for Grp key 107 */ 108 u32 dot118021XGrpKeyid; /* key id used for Grp Key 109 * ( tx key index) 110 */ 111 union Keytype dot118021XGrpKey[4]; /* 802.1x Group Key, 112 * for inx0 and inx1 113 */ 114 union Keytype dot118021XGrptxmickey[4]; 115 union Keytype dot118021XGrprxmickey[4]; 116 union pn48 dot11Grptxpn; /* PN48 used for Grp Key xmit.*/ 117 union pn48 dot11Grprxpn; /* PN48 used for Grp Key recv.*/ 118 #ifdef CONFIG_88EU_AP_MODE 119 /* extend security capabilities for AP_MODE */ 120 unsigned int dot8021xalg;/* 0:disable, 1:psk, 2:802.1x */ 121 unsigned int wpa_psk;/* 0:disable, bit(0): WPA, bit(1):WPA2 */ 122 unsigned int wpa_group_cipher; 123 unsigned int wpa2_group_cipher; 124 unsigned int wpa_pairwise_cipher; 125 unsigned int wpa2_pairwise_cipher; 126 #endif 127 u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */ 128 int wps_ie_len; 129 u8 binstallGrpkey; 130 u8 busetkipkey; 131 u8 bcheck_grpkey; 132 u8 bgrpkey_handshake; 133 s32 hw_decrypted;/* if the rx packets is hw_decrypted==false,i 134 * it means the hw has not been ready. 135 */ 136 137 /* keeps the auth_type & enc_status from upper layer 138 * ioctl(wpa_supplicant or wzc) 139 */ 140 u32 ndisauthtype; /* NDIS_802_11_AUTHENTICATION_MODE */ 141 u32 ndisencryptstatus; /* NDIS_802_11_ENCRYPTION_STATUS */ 142 struct wlan_bssid_ex sec_bss; /* for joinbss (h2c buffer) usage */ 143 struct ndis_802_11_wep ndiswep; 144 u8 assoc_info[600]; 145 u8 szofcapability[256]; /* for wpa2 usage */ 146 u8 oidassociation[512]; /* for wpa/wpa2 usage */ 147 u8 authenticator_ie[256]; /* store ap security information element */ 148 u8 supplicant_ie[256]; /* store sta security information element */ 149 150 /* for tkip countermeasure */ 151 u32 last_mic_err_time; 152 u8 btkip_countermeasure; 153 u8 btkip_wait_report; 154 u32 btkip_countermeasure_time; 155 156 /* */ 157 /* For WPA2 Pre-Authentication. */ 158 /* */ 159 struct rt_pmkid_list PMKIDList[NUM_PMKID_CACHE]; 160 u8 PMKIDIndex; 161 u8 bWepDefaultKeyIdxSet; 162 }; 163 164 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst) \ 165 do { \ 166 switch (psecuritypriv->dot11AuthAlgrthm) { \ 167 case dot11AuthAlgrthm_Open: \ 168 case dot11AuthAlgrthm_Shared: \ 169 case dot11AuthAlgrthm_Auto: \ 170 encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm; \ 171 break; \ 172 case dot11AuthAlgrthm_8021X: \ 173 if (bmcst) \ 174 encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\ 175 else \ 176 encry_algo = (u8)psta->dot118021XPrivacy; \ 177 break; \ 178 case dot11AuthAlgrthm_WAPI: \ 179 encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm; \ 180 break; \ 181 } \ 182 } while (0) 183 184 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt) \ 185 do { \ 186 switch (encrypt) { \ 187 case _WEP40_: \ 188 case _WEP104_: \ 189 iv_len = 4; \ 190 icv_len = 4; \ 191 break; \ 192 case _TKIP_: \ 193 iv_len = 8; \ 194 icv_len = 4; \ 195 break; \ 196 case _AES_: \ 197 iv_len = 8; \ 198 icv_len = 8; \ 199 break; \ 200 case _SMS4_: \ 201 iv_len = 18; \ 202 icv_len = 16; \ 203 break; \ 204 default: \ 205 iv_len = 0; \ 206 icv_len = 0; \ 207 break; \ 208 } \ 209 } while (0) 210 211 #define GET_TKIP_PN(iv, dot11txpn) \ 212 do { \ 213 dot11txpn._byte_.TSC0 = iv[2]; \ 214 dot11txpn._byte_.TSC1 = iv[0]; \ 215 dot11txpn._byte_.TSC2 = iv[4]; \ 216 dot11txpn._byte_.TSC3 = iv[5]; \ 217 dot11txpn._byte_.TSC4 = iv[6]; \ 218 dot11txpn._byte_.TSC5 = iv[7]; \ 219 } while (0) 220 221 #define ROL32(A, n) (((A) << (n)) | (((A) >> (32 - (n))) & ((1UL << (n)) - 1))) 222 #define ROR32(A, n) ROL32((A), 32 - (n)) 223 224 struct mic_data { 225 u32 K0, K1; /* Key */ 226 u32 L, R; /* Current state */ 227 u32 M; /* Message accumulator (single word) */ 228 u32 nBytesInM; /* # bytes in M */ 229 }; 230 231 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key); 232 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b); 233 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes); 234 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst); 235 void rtw_seccalctkipmic(u8 *key, u8 *header, u8 *data, u32 data_len, 236 u8 *Miccode, u8 priority); 237 u32 rtw_aes_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe); 238 u32 rtw_tkip_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe); 239 void rtw_wep_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe); 240 u32 rtw_aes_decrypt(struct adapter *padapter, struct recv_frame *precvframe); 241 u32 rtw_tkip_decrypt(struct adapter *padapter, struct recv_frame *precvframe); 242 int rtw_wep_decrypt(struct adapter *padapter, struct recv_frame *precvframe); 243 244 #endif /* __RTL871X_SECURITY_H_ */ 245