1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
7 #ifndef __RTW_SECURITY_H_
8 #define __RTW_SECURITY_H_
10 #include <crypto/arc4.h>
12 #define _NO_PRIVACY_ 0x0
15 #define _TKIP_WTMIC_ 0x3
18 #define _WEP_WPA_MIXED_ 0x07 /* WEP + WPA */
21 #define is_wep_enc(alg) (((alg) == _WEP40_) || ((alg) == _WEP104_))
23 const char *security_type_str(u8 value);
25 #define SHA256_MAC_LEN 32
26 #define AES_BLOCK_SIZE 16
27 #define AES_PRIV_SIZE (4 * 44)
29 #define RTW_KEK_LEN 16
30 #define RTW_KCK_LEN 16
31 #define RTW_REPLAY_CTR_LEN 8
34 ENCRYP_PROTOCOL_OPENSYS, /* open system */
35 ENCRYP_PROTOCOL_WEP, /* WEP */
36 ENCRYP_PROTOCOL_WPA, /* WPA */
37 ENCRYP_PROTOCOL_WPA2, /* WPA2 */
38 ENCRYP_PROTOCOL_WAPI, /* WAPI: Not support in this version */
43 #ifndef Ndis802_11AuthModeWPA2
44 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
47 #ifndef Ndis802_11AuthModeWPA2PSK
48 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
55 #ifdef __LITTLE_ENDIAN
88 struct rt_pmkid_list {
98 struct security_priv {
99 u32 dot11AuthAlgrthm; /* 802.11 auth, could be open, shared, 8021x and authswitch */
100 u32 dot11PrivacyAlgrthm; /* This specify the privacy for shared auth. algorithm. */
103 u32 dot11PrivacyKeyIndex; /* this is only valid for legendary wep, 0~3 for key id. (tx key index) */
104 union Keytype dot11DefKey[4]; /* this is only valid for def. key */
105 u32 dot11DefKeylen[4];
106 u8 key_mask; /* use to restore wep key after hal_init */
108 u32 dot118021XGrpPrivacy; /* This specify the privacy algthm. used for Grp key */
109 u32 dot118021XGrpKeyid; /* key id used for Grp Key (tx key index) */
110 union Keytype dot118021XGrpKey[BIP_MAX_KEYID]; /* 802.1x Group Key, for inx0 and inx1 */
111 union Keytype dot118021XGrptxmickey[BIP_MAX_KEYID];
112 union Keytype dot118021XGrprxmickey[BIP_MAX_KEYID];
113 union pn48 dot11Grptxpn; /* PN48 used for Grp Key xmit. */
114 union pn48 dot11Grprxpn; /* PN48 used for Grp Key recv. */
115 u32 dot11wBIPKeyid; /* key id used for BIP Key (tx key index) */
116 union Keytype dot11wBIPKey[6]; /* BIP Key, for index4 and index5 */
117 union pn48 dot11wBIPtxpn; /* PN48 used for Grp Key xmit. */
118 union pn48 dot11wBIPrxpn; /* PN48 used for Grp Key recv. */
120 /* extend security capabilities for AP_MODE */
121 unsigned int dot8021xalg;/* 0:disable, 1:psk, 2:802.1x */
122 unsigned int wpa_psk;/* 0:disable, bit(0): WPA, bit(1):WPA2 */
123 unsigned int wpa_group_cipher;
124 unsigned int wpa2_group_cipher;
125 unsigned int wpa_pairwise_cipher;
126 unsigned int wpa2_pairwise_cipher;
128 u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */
131 struct arc4_ctx xmit_arc4_ctx;
132 struct arc4_ctx recv_arc4_ctx;
137 /* _timer tkip_timer; */
139 u8 bgrpkey_handshake;
141 s32 sw_encrypt;/* from registry_priv */
142 s32 sw_decrypt;/* from registry_priv */
144 s32 hw_decrypted;/* if the rx packets is hw_decrypted ==false, it means the hw has not been ready. */
147 /* keeps the auth_type & enc_status from upper layer ioctl(wpa_supplicant or wzc) */
148 u32 ndisauthtype; /* enum ndis_802_11_authentication_mode */
149 u32 ndisencryptstatus; /* NDIS_802_11_ENCRYPTION_STATUS */
151 struct wlan_bssid_ex sec_bss; /* for joinbss (h2c buffer) usage */
153 struct ndis_802_11_wep ndiswep;
156 u8 szofcapability[256]; /* for wpa2 usage */
157 u8 oidassociation[512]; /* for wpa/wpa2 usage */
158 u8 authenticator_ie[256]; /* store ap security information element */
159 u8 supplicant_ie[256]; /* store sta security information element */
162 /* for tkip countermeasure */
163 unsigned long last_mic_err_time;
164 u8 btkip_countermeasure;
165 u8 btkip_wait_report;
166 u32 btkip_countermeasure_time;
168 /* For WPA2 Pre-Authentication. */
169 struct rt_pmkid_list PMKIDList[NUM_PMKID_CACHE]; /* Renamed from PreAuthKey[NUM_PRE_AUTH_KEY]. Annie, 2006-10-13. */
172 u8 bWepDefaultKeyIdxSet;
176 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst)\
178 switch (psecuritypriv->dot11AuthAlgrthm)\
180 case dot11AuthAlgrthm_Open:\
181 case dot11AuthAlgrthm_Shared:\
182 case dot11AuthAlgrthm_Auto:\
183 encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
185 case dot11AuthAlgrthm_8021X:\
187 encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\
189 encry_algo = (u8)psta->dot118021XPrivacy;\
191 case dot11AuthAlgrthm_WAPI:\
192 encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
197 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt)\
226 #define GET_TKIP_PN(iv, dot11txpn)\
228 dot11txpn._byte_.TSC0 = iv[2];\
229 dot11txpn._byte_.TSC1 = iv[0];\
230 dot11txpn._byte_.TSC2 = iv[4];\
231 dot11txpn._byte_.TSC3 = iv[5];\
232 dot11txpn._byte_.TSC4 = iv[6];\
233 dot11txpn._byte_.TSC5 = iv[7];\
237 #define ROL32(A, n) (((A) << (n)) | (((A)>>(32-(n))) & ((1UL << (n)) - 1)))
238 #define ROR32(A, n) ROL32((A), 32-(n))
241 u32 K0, K1; /* Key */
242 u32 L, R; /* Current state */
243 u32 M; /* Message accumulator (single word) */
244 u32 nBytesInM; /* # bytes in M */
247 /* ===== start - public domain SHA256 implementation ===== */
249 /* This is based on SHA256 implementation in LibTomCrypt that was released into
250 * public domain by Tom St Denis. */
252 int omac1_aes_128(u8 *key, u8 *data, size_t data_len, u8 *mac);
253 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key);
254 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b);
255 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
256 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst);
258 void rtw_seccalctkipmic(
266 u32 rtw_aes_encrypt(struct adapter *padapter, u8 *pxmitframe);
267 u32 rtw_tkip_encrypt(struct adapter *padapter, u8 *pxmitframe);
268 void rtw_wep_encrypt(struct adapter *padapter, u8 *pxmitframe);
270 u32 rtw_aes_decrypt(struct adapter *padapter, u8 *precvframe);
271 u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe);
272 void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe);
273 u32 rtw_BIP_verify(struct adapter *padapter, u8 *precvframe);
275 void rtw_sec_restore_wep_key(struct adapter *adapter);
276 u8 rtw_handle_tkip_countermeasure(struct adapter *adapter, const char *caller);
278 #endif /* __RTL871X_SECURITY_H_ */