1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
4 #ifndef __RTW_SECURITY_H_
5 #define __RTW_SECURITY_H_
7 #include "osdep_service.h"
9 #include <crypto/arc4.h>
11 #define _NO_PRIVACY_ 0x0
14 #define _TKIP_WTMIC_ 0x3
17 #define _WEP_WPA_MIXED_ 0x07 /* WEP + WPA */
20 #define is_wep_enc(alg) (((alg) == _WEP40_) || ((alg) == _WEP104_))
22 #define _WPA_IE_ID_ 0xdd
23 #define _WPA2_IE_ID_ 0x30
25 #define SHA256_MAC_LEN 32
26 #define AES_BLOCK_SIZE 16
27 #define AES_PRIV_SIZE (4 * 44)
30 ENCRYP_PROTOCOL_OPENSYS, /* open system */
31 ENCRYP_PROTOCOL_WEP, /* WEP */
32 ENCRYP_PROTOCOL_WPA, /* WPA */
33 ENCRYP_PROTOCOL_WPA2, /* WPA2 */
34 ENCRYP_PROTOCOL_WAPI, /* WAPI: Not support in this version */
38 #ifndef Ndis802_11AuthModeWPA2
39 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
42 #ifndef Ndis802_11AuthModeWPA2PSK
43 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
49 #ifdef __LITTLE_ENDIAN
61 #elif defined(__BIG_ENDIAN)
81 struct rt_pmkid_list {
90 struct security_priv {
91 u32 dot11AuthAlgrthm; /* 802.11 auth, could be open,
92 * shared, 8021x and authswitch */
93 u32 dot11PrivacyAlgrthm; /* This specify the privacy for
94 * shared auth. algorithm. */
96 u32 dot11PrivacyKeyIndex; /* this is only valid for legendary
97 * wep, 0~3 for key id.(tx key index) */
98 union Keytype dot11DefKey[4]; /* this is only valid for def. key */
99 u32 dot11DefKeylen[4];
100 u32 dot118021XGrpPrivacy; /* This specify the privacy algthm.
101 * used for Grp key */
102 u32 dot118021XGrpKeyid; /* key id used for Grp Key
104 union Keytype dot118021XGrpKey[4]; /* 802.1x Group Key,
105 * for inx0 and inx1 */
106 union Keytype dot118021XGrptxmickey[4];
107 union Keytype dot118021XGrprxmickey[4];
108 union pn48 dot11Grptxpn; /* PN48 used for Grp Key xmit.*/
109 union pn48 dot11Grprxpn; /* PN48 used for Grp Key recv.*/
111 struct arc4_ctx xmit_arc4_ctx;
112 struct arc4_ctx recv_arc4_ctx;
114 /* extend security capabilities for AP_MODE */
115 unsigned int dot8021xalg;/* 0:disable, 1:psk, 2:802.1x */
116 unsigned int wpa_psk;/* 0:disable, bit(0): WPA, bit(1):WPA2 */
117 unsigned int wpa_group_cipher;
118 unsigned int wpa2_group_cipher;
119 unsigned int wpa_pairwise_cipher;
120 unsigned int wpa2_pairwise_cipher;
121 u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */
126 u8 bgrpkey_handshake;
127 s32 sw_encrypt;/* from registry_priv */
128 s32 sw_decrypt;/* from registry_priv */
129 s32 hw_decrypted;/* if the rx packets is hw_decrypted==false,i
130 * it means the hw has not been ready. */
132 /* keeps the auth_type & enc_status from upper layer
133 * ioctl(wpa_supplicant or wzc) */
134 u32 ndisauthtype; /* NDIS_802_11_AUTHENTICATION_MODE */
135 u32 ndisencryptstatus; /* NDIS_802_11_ENCRYPTION_STATUS */
136 struct wlan_bssid_ex sec_bss; /* for joinbss (h2c buffer) usage */
137 struct ndis_802_11_wep ndiswep;
139 u8 szofcapability[256]; /* for wpa2 usage */
140 u8 oidassociation[512]; /* for wpa/wpa2 usage */
141 u8 authenticator_ie[256]; /* store ap security information element */
142 u8 supplicant_ie[256]; /* store sta security information element */
144 /* for tkip countermeasure */
145 u32 last_mic_err_time;
146 u8 btkip_countermeasure;
147 u8 btkip_wait_report;
148 u32 btkip_countermeasure_time;
151 /* For WPA2 Pre-Authentication. */
153 struct rt_pmkid_list PMKIDList[NUM_PMKID_CACHE];
155 u8 bWepDefaultKeyIdxSet;
158 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst) \
160 switch (psecuritypriv->dot11AuthAlgrthm) { \
161 case dot11AuthAlgrthm_Open: \
162 case dot11AuthAlgrthm_Shared: \
163 case dot11AuthAlgrthm_Auto: \
164 encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm; \
166 case dot11AuthAlgrthm_8021X: \
168 encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\
170 encry_algo = (u8)psta->dot118021XPrivacy; \
172 case dot11AuthAlgrthm_WAPI: \
173 encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm; \
178 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt) \
205 #define GET_TKIP_PN(iv, dot11txpn) \
207 dot11txpn._byte_.TSC0 = iv[2]; \
208 dot11txpn._byte_.TSC1 = iv[0]; \
209 dot11txpn._byte_.TSC2 = iv[4]; \
210 dot11txpn._byte_.TSC3 = iv[5]; \
211 dot11txpn._byte_.TSC4 = iv[6]; \
212 dot11txpn._byte_.TSC5 = iv[7]; \
215 #define ROL32(A, n) (((A) << (n)) | (((A)>>(32-(n))) & ((1UL << (n)) - 1)))
216 #define ROR32(A, n) ROL32((A), 32-(n))
219 u32 K0, K1; /* Key */
220 u32 L, R; /* Current state */
221 u32 M; /* Message accumulator (single word) */
222 u32 nBytesInM; /* # bytes in M */
225 extern const u32 Te0[256];
226 extern const u32 Te1[256];
227 extern const u32 Te2[256];
228 extern const u32 Te3[256];
229 extern const u32 Te4[256];
230 extern const u32 Td0[256];
231 extern const u32 Td1[256];
232 extern const u32 Td2[256];
233 extern const u32 Td3[256];
234 extern const u32 Td4[256];
235 extern const u32 rcon[10];
236 extern const u8 Td4s[256];
237 extern const u8 rcons[10];
239 #define RCON(i) (rcons[(i)] << 24)
241 static inline u32 rotr(u32 val, int bits)
243 return (val >> bits) | (val << (32 - bits));
246 #define TE0(i) Te0[((i) >> 24) & 0xff]
247 #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
248 #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
249 #define TE3(i) rotr(Te0[(i) & 0xff], 24)
250 #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
251 #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
252 #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
253 #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
254 #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
255 #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
256 #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
257 #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
258 #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
260 #define TD0(i) Td0[((i) >> 24) & 0xff]
261 #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
262 #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
263 #define TD3(i) rotr(Td0[(i) & 0xff], 24)
264 #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24)
265 #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16)
266 #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8)
267 #define TD44(i) (Td4s[(i) & 0xff])
268 #define TD0_(i) Td0[(i) & 0xff]
269 #define TD1_(i) rotr(Td0[(i) & 0xff], 8)
270 #define TD2_(i) rotr(Td0[(i) & 0xff], 16)
271 #define TD3_(i) rotr(Td0[(i) & 0xff], 24)
273 #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
274 ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
276 #define PUTU32(ct, st) { \
277 (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
278 (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
280 #define WPA_GET_BE32(a) ((((u32)(a)[0]) << 24) | (((u32)(a)[1]) << 16) | \
281 (((u32)(a)[2]) << 8) | ((u32)(a)[3]))
283 #define WPA_PUT_LE16(a, val) \
285 (a)[1] = ((u16)(val)) >> 8; \
286 (a)[0] = ((u16)(val)) & 0xff; \
289 #define WPA_PUT_BE32(a, val) \
291 (a)[0] = (u8)((((u32)(val)) >> 24) & 0xff); \
292 (a)[1] = (u8)((((u32)(val)) >> 16) & 0xff); \
293 (a)[2] = (u8)((((u32)(val)) >> 8) & 0xff); \
294 (a)[3] = (u8)(((u32)(val)) & 0xff); \
297 #define WPA_PUT_BE64(a, val) \
299 (a)[0] = (u8)(((u64)(val)) >> 56); \
300 (a)[1] = (u8)(((u64)(val)) >> 48); \
301 (a)[2] = (u8)(((u64)(val)) >> 40); \
302 (a)[3] = (u8)(((u64)(val)) >> 32); \
303 (a)[4] = (u8)(((u64)(val)) >> 24); \
304 (a)[5] = (u8)(((u64)(val)) >> 16); \
305 (a)[6] = (u8)(((u64)(val)) >> 8); \
306 (a)[7] = (u8)(((u64)(val)) & 0xff); \
309 /* ===== start - public domain SHA256 implementation ===== */
311 /* This is based on SHA256 implementation in LibTomCrypt that was released into
312 * public domain by Tom St Denis. */
314 /* Various logical functions */
316 (((((unsigned long)(x) & 0xFFFFFFFFUL) >> (unsigned long)((y)&31)) | \
317 ((unsigned long)(x) << (unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
318 #define Ch(x, y ,z) (z ^ (x & (y ^ z)))
319 #define Maj(x, y, z) (((x | y) & z) | (x & y))
320 #define S(x, n) RORc((x), (n))
321 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
322 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
323 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
324 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
325 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
327 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
330 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key);
331 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b);
332 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
333 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst);
334 void rtw_seccalctkipmic(u8 *key, u8 *header, u8 *data, u32 data_len,
335 u8 *Miccode, u8 priority);
336 u32 rtw_aes_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe);
337 u32 rtw_tkip_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe);
338 void rtw_wep_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe);
339 u32 rtw_aes_decrypt(struct adapter *padapter, struct recv_frame *precvframe);
340 u32 rtw_tkip_decrypt(struct adapter *padapter, struct recv_frame *precvframe);
341 void rtw_wep_decrypt(struct adapter *padapter, struct recv_frame *precvframe);
343 #endif /* __RTL871X_SECURITY_H_ */