• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2017 Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  *****************************************************************************/
15 #ifndef __RTW_SECURITY_H_
16 #define __RTW_SECURITY_H_
17 
18 enum security_type {
19 	/* TYPE */
20 	_NO_PRIVACY_	= 0x00,
21 	_WEP40_		= 0x01,
22 	_TKIP_		= 0x02,
23 	_TKIP_WTMIC_	= 0x03,
24 	_AES_		= 0x04,
25 	_WEP104_	= 0x05,
26 	_SMS4_		= 0x06,
27 	_GCMP_		= 0x07,
28 	_SEC_TYPE_MAX_,
29 
30 	/* EXT_SECTYPE=1 */
31 	_SEC_TYPE_256_	= 0x10,
32 	_CCMP_256_	= (_AES_ | _SEC_TYPE_256_),
33 	_GCMP_256_	= (_GCMP_ | _SEC_TYPE_256_),
34 
35 #ifdef CONFIG_IEEE80211W
36 	/* EXT_SECTYPE=0, MGNT=1, GK=0/1, KEYID=00/01 */
37 	_SEC_TYPE_BIT_	= 0x20,
38 	_BIP_CMAC_128_	= (_SEC_TYPE_BIT_),
39 	_BIP_GMAC_128_	= (_SEC_TYPE_BIT_ + 1),
40 	_BIP_GMAC_256_	= (_SEC_TYPE_BIT_ + 2),
41 	/* EXT_SECTYPE=1, MGNT=1, GK=1, KEYID=00/01 */
42 	_BIP_CMAC_256_	= (_SEC_TYPE_BIT_ + 3),
43 	_BIP_MAX_,
44 #endif
45 };
46 
47 /* 802.11W use wrong key */
48 #define IEEE80211W_RIGHT_KEY	0x0
49 #define IEEE80211W_WRONG_KEY	0x1
50 #define IEEE80211W_NO_KEY		0x2
51 
52 #define CCMPH_2_PN(ch)	((ch) & 0x000000000000ffff) \
53 			| (((ch) & 0xffffffff00000000) >> 16)
54 
55 #define is_wep_enc(alg) (((alg) == _WEP40_) || ((alg) == _WEP104_))
56 
57 const char *security_type_str(u8 value);
58 #ifdef CONFIG_IEEE80211W
59 u32 security_type_bip_to_gmcs(enum security_type type);
60 #endif
61 
62 #define _WPA_IE_ID_	0xdd
63 #define _WPA2_IE_ID_	0x30
64 
65 #define RTW_KEK_LEN 16
66 #define RTW_KCK_LEN 16
67 #define RTW_TKIP_MIC_LEN 8
68 #define RTW_REPLAY_CTR_LEN 8
69 
70 #define INVALID_SEC_MAC_CAM_ID	0xFF
71 
72 typedef enum {
73 	ENCRYP_PROTOCOL_OPENSYS,   /* open system */
74 	ENCRYP_PROTOCOL_WEP,       /* WEP */
75 	ENCRYP_PROTOCOL_WPA,       /* WPA */
76 	ENCRYP_PROTOCOL_WPA2,      /* WPA2 */
77 	ENCRYP_PROTOCOL_WAPI,      /* WAPI: Not support in this version */
78 	ENCRYP_PROTOCOL_MAX
79 } ENCRYP_PROTOCOL_E;
80 
81 
82 #ifndef Ndis802_11AuthModeWPA2
83 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
84 #endif
85 
86 #ifndef Ndis802_11AuthModeWPA2PSK
87 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
88 #endif
89 
90 union pn48	{
91 
92 	u64	val;
93 
94 #ifdef CONFIG_LITTLE_ENDIAN
95 
96 struct {
97 	u8 TSC0;
98 	u8 TSC1;
99 	u8 TSC2;
100 	u8 TSC3;
101 	u8 TSC4;
102 	u8 TSC5;
103 	u8 TSC6;
104 	u8 TSC7;
105 } _byte_;
106 
107 #elif defined(CONFIG_BIG_ENDIAN)
108 
109 struct {
110 	u8 TSC7;
111 	u8 TSC6;
112 	u8 TSC5;
113 	u8 TSC4;
114 	u8 TSC3;
115 	u8 TSC2;
116 	u8 TSC1;
117 	u8 TSC0;
118 } _byte_;
119 
120 #endif
121 
122 };
123 
124 union Keytype {
125 	u8 skey[32];
126 };
127 
128 typedef struct _RT_PMKID_LIST {
129 	u8						bUsed;
130 	u8						Bssid[6];
131 	u8						PMKID[16];
132 	u8						SsidBuf[33];
133 	u8						*ssid_octet;
134 	u16						ssid_length;
135 } RT_PMKID_LIST, *PRT_PMKID_LIST;
136 
137 
138 struct security_priv {
139 	u32	  dot11AuthAlgrthm;		/* 802.11 auth, could be open, shared, 8021x and authswitch */
140 	u32	  dot11PrivacyAlgrthm;	/* This specify the privacy for shared auth. algorithm. */
141 
142 	/* WEP */
143 	u32	  dot11PrivacyKeyIndex;	/* this is only valid for legendary wep, 0~3 for key id. (tx key index) */
144 	union Keytype dot11DefKey[6];			/* this is only valid for def. key	 */
145 	u32	dot11DefKeylen[6];
146 	u8	dot11Def_camid[6];
147 	u8 	key_mask; /* use to restore wep key after hal_init */
148 
149 	u32 dot118021XGrpPrivacy;	/* This specify the privacy algthm. used for Grp key */
150 	u32	dot118021XGrpKeyid;		/* key id used for Grp Key ( tx key index) */
151 	union Keytype	dot118021XGrpKey[6];	/* 802.1x Group Key, for inx0 and inx1	 */
152 	union Keytype	dot118021XGrptxmickey[6];
153 	union Keytype	dot118021XGrprxmickey[6];
154 	union pn48		dot11Grptxpn;			/* PN48 used for Grp Key xmit. */
155 	union pn48		dot11Grprxpn;			/* PN48 used for Grp Key recv. */
156 	u8				iv_seq[4][8];
157 #ifdef CONFIG_IEEE80211W
158 	enum security_type dot11wCipher;
159 	u32	dot11wBIPKeyid;						/* key id used for BIP Key ( tx key index) */
160 	union Keytype	dot11wBIPKey[6];		/* BIP Key, for index4 and index5 */
161 	union pn48		dot11wBIPtxpn;			/* PN48 used for BIP xmit. */
162 	union pn48		dot11wBIPrxpn;			/* PN48 used for BIP recv. */
163 #endif /* CONFIG_IEEE80211W */
164 #ifdef CONFIG_AP_MODE
165 	/* extend security capabilities for AP_MODE */
166 	unsigned int dot8021xalg;/* 0:disable, 1:psk, 2:802.1x */
167 	unsigned int wpa_psk;/* 0:disable, bit(0): WPA, bit(1):WPA2 */
168 	unsigned int wpa_group_cipher;
169 	unsigned int wpa2_group_cipher;
170 	unsigned int wpa_pairwise_cipher;
171 	unsigned int wpa2_pairwise_cipher;
172 	unsigned int akmp; /* An authentication and key management protocol */
173 #endif
174 	u8 mfp_opt;
175 	u8	dot118021x_bmc_cam_id;
176 	/*IEEE802.11-2012 Std. Table 8-101 AKM Suite Selectors*/
177 	u32	rsn_akm_suite_type;
178 
179 	u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */
180 	int wps_ie_len;
181 
182 	u8 owe_ie[MAX_OWE_IE_LEN];/* added in assoc req */
183 	int owe_ie_len;
184 
185 	u8 rsnx_ie[MAX_RSNX_IE_LEN];
186 	int rsnx_ie_len;
187 
188 	u8	binstallGrpkey;
189 #ifdef CONFIG_GTK_OL
190 	u8	binstallKCK_KEK;
191 #endif /* CONFIG_GTK_OL */
192 #ifdef CONFIG_IEEE80211W
193 	u8	binstallBIPkey;
194 #endif /* CONFIG_IEEE80211W */
195 	u8	busetkipkey;
196 	u8	bcheck_grpkey;
197 	u8	bgrpkey_handshake;
198 
199 	u8	auth_alg;
200 	u8	auth_type;
201 	u8	extauth_status;
202 	/* u8	packet_cnt; */ /* unused, removed */
203 
204 	s32	sw_encrypt;/* from registry_priv */
205 	s32	sw_decrypt;/* from registry_priv */
206 
207 	s32 	hw_decrypted;/* if the rx packets is hw_decrypted==_FALSE, it means the hw has not been ready. */
208 
209 
210 	/* keeps the auth_type & enc_status from upper layer ioctl(wpa_supplicant or wzc) */
211 	u32 ndisauthtype;	/* NDIS_802_11_AUTHENTICATION_MODE */
212 	u32 ndisencryptstatus;	/* NDIS_802_11_ENCRYPTION_STATUS */
213 
214 	NDIS_802_11_WEP ndiswep;
215 
216 	u8 assoc_info[600];
217 	u8 szofcapability[256]; /* for wpa2 usage */
218 	u8 oidassociation[512]; /* for wpa/wpa2 usage */
219 	u8 authenticator_ie[256];  /* store ap security information element */
220 	u8 supplicant_ie[256];  /* store sta security information element */
221 
222 
223 	/* for tkip countermeasure */
224 	systime last_mic_err_time;
225 	u8	btkip_countermeasure;
226 	u8	btkip_wait_report;
227 	systime btkip_countermeasure_time;
228 
229 	/* --------------------------------------------------------------------------- */
230 	/* For WPA2 Pre-Authentication. */
231 	/* --------------------------------------------------------------------------- */
232 	/* u8				RegEnablePreAuth;				 */ /* Default value: Pre-Authentication enabled or not, from registry "EnablePreAuth". Added by Annie, 2005-11-01. */
233 	/* u8				EnablePreAuthentication;			 */ /* Current Value: Pre-Authentication enabled or not. */
234 	RT_PMKID_LIST		PMKIDList[NUM_PMKID_CACHE];	/* Renamed from PreAuthKey[NUM_PRE_AUTH_KEY]. Annie, 2006-10-13. */
235 	u8				PMKIDIndex;
236 	/* u32				PMKIDCount;						 */ /* Added by Annie, 2006-10-13. */
237 	/* u8				szCapability[256];				 */ /* For WPA2-PSK using zero-config, by Annie, 2005-09-20. */
238 
239 	u8 bWepDefaultKeyIdxSet;
240 
241 #define DBG_SW_SEC_CNT
242 #ifdef DBG_SW_SEC_CNT
243 	u64 wep_sw_enc_cnt_bc;
244 	u64 wep_sw_enc_cnt_mc;
245 	u64 wep_sw_enc_cnt_uc;
246 	u64 wep_sw_dec_cnt_bc;
247 	u64 wep_sw_dec_cnt_mc;
248 	u64 wep_sw_dec_cnt_uc;
249 
250 	u64 tkip_sw_enc_cnt_bc;
251 	u64 tkip_sw_enc_cnt_mc;
252 	u64 tkip_sw_enc_cnt_uc;
253 	u64 tkip_sw_dec_cnt_bc;
254 	u64 tkip_sw_dec_cnt_mc;
255 	u64 tkip_sw_dec_cnt_uc;
256 
257 	u64 aes_sw_enc_cnt_bc;
258 	u64 aes_sw_enc_cnt_mc;
259 	u64 aes_sw_enc_cnt_uc;
260 	u64 aes_sw_dec_cnt_bc;
261 	u64 aes_sw_dec_cnt_mc;
262 	u64 aes_sw_dec_cnt_uc;
263 
264 	u64 gcmp_sw_enc_cnt_bc;
265 	u64 gcmp_sw_enc_cnt_mc;
266 	u64 gcmp_sw_enc_cnt_uc;
267 	u64 gcmp_sw_dec_cnt_bc;
268 	u64 gcmp_sw_dec_cnt_mc;
269 	u64 gcmp_sw_dec_cnt_uc;
270 #endif /* DBG_SW_SEC_CNT */
271 };
272 
273 #ifdef CONFIG_IEEE80211W
274 #define SEC_IS_BIP_KEY_INSTALLED(sec) ((sec)->binstallBIPkey)
275 #else
276 #define SEC_IS_BIP_KEY_INSTALLED(sec) _FALSE
277 #endif
278 
279 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst)\
280 	do {\
281 		switch (psecuritypriv->dot11AuthAlgrthm) {\
282 		case dot11AuthAlgrthm_Open:\
283 		case dot11AuthAlgrthm_Shared:\
284 		case dot11AuthAlgrthm_Auto:\
285 			encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
286 			break;\
287 		case dot11AuthAlgrthm_8021X:\
288 			if (bmcst)\
289 				encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\
290 			else\
291 				encry_algo = (u8) psta->dot118021XPrivacy;\
292 			break;\
293 		case dot11AuthAlgrthm_WAPI:\
294 			encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
295 			break;\
296 		} \
297 	} while (0)
298 
299 #define _AES_IV_LEN_ 8
300 
301 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt)\
302 	do {\
303 		switch (encrypt) {\
304 		case _WEP40_:\
305 		case _WEP104_:\
306 			iv_len = 4;\
307 			icv_len = 4;\
308 			break;\
309 		case _TKIP_:\
310 			iv_len = 8;\
311 			icv_len = 4;\
312 			break;\
313 		case _AES_:\
314 			iv_len = 8;\
315 			icv_len = 8;\
316 			break;\
317 		case _GCMP_:\
318 		case _GCMP_256_:\
319 			iv_len = 8;\
320 			icv_len = 16;\
321 			break;\
322 		case _CCMP_256_:\
323 			iv_len = 8;\
324 			icv_len = 16;\
325 			break;\
326 		case _SMS4_:\
327 			iv_len = 18;\
328 			icv_len = 16;\
329 			break;\
330 		default:\
331 			iv_len = 0;\
332 			icv_len = 0;\
333 			break;\
334 		} \
335 	} while (0)
336 
337 
338 #define GET_TKIP_PN(iv, dot11txpn)\
339 	do {\
340 		dot11txpn._byte_.TSC0 = iv[2];\
341 		dot11txpn._byte_.TSC1 = iv[0];\
342 		dot11txpn._byte_.TSC2 = iv[4];\
343 		dot11txpn._byte_.TSC3 = iv[5];\
344 		dot11txpn._byte_.TSC4 = iv[6];\
345 		dot11txpn._byte_.TSC5 = iv[7];\
346 	} while (0)
347 
348 
349 #define ROL32(A, n)	(((A) << (n)) | (((A)>>(32-(n)))  & ((1UL << (n)) - 1)))
350 #define ROR32(A, n)	ROL32((A), 32-(n))
351 
352 struct mic_data {
353 	u32  K0, K1;         /* Key */
354 	u32  L, R;           /* Current state */
355 	u32  M;              /* Message accumulator (single word) */
356 	u32     nBytesInM;      /*  # bytes in M */
357 };
358 
359 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key);
360 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b);
361 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
362 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst);
363 
364 void rtw_seccalctkipmic(
365 	u8 *key,
366 	u8 *header,
367 	u8 *data,
368 	u32 data_len,
369 	u8 *Miccode,
370 	u8   priority);
371 
372 u32 rtw_aes_encrypt(_adapter *padapter, u8 *pxmitframe);
373 u32 rtw_tkip_encrypt(_adapter *padapter, u8 *pxmitframe);
374 void rtw_wep_encrypt(_adapter *padapter, u8  *pxmitframe);
375 
376 u32 rtw_aes_decrypt(_adapter *padapter, u8  *precvframe);
377 u32 rtw_tkip_decrypt(_adapter *padapter, u8  *precvframe);
378 void rtw_wep_decrypt(_adapter *padapter, u8  *precvframe);
379 
380 u32 rtw_gcmp_encrypt(_adapter *padapter, u8 *pxmitframe);
381 u32 rtw_gcmp_decrypt(_adapter *padapter, u8 *precvframe);
382 
383 #ifdef CONFIG_RTW_MESH_AEK
384 int rtw_aes_siv_encrypt(const u8 *key, size_t key_len,
385 	const u8 *pw, size_t pwlen, size_t num_elem,
386 	const u8 *addr[], const size_t *len, u8 *out);
387 int rtw_aes_siv_decrypt(const u8 *key, size_t key_len,
388 	const u8 *iv_crypt, size_t iv_c_len, size_t num_elem,
389 	const u8 *addr[], const size_t *len, u8 *out);
390 #endif /* CONFIG_RTW_MESH_AEK */
391 
392 #ifdef CONFIG_IEEE80211W
393 u8 rtw_calculate_bip_mic(enum security_type gmcs, u8 *whdr_pos, s32 len,
394 	const u8 *key, const u8 *data, size_t data_len, u8 *mic);
395 u32 rtw_bip_verify(enum security_type gmcs, u16 pkt_len,
396 	u8 *whdr_pos, sint flen, const u8 *key, u16 keyid, u64 *ipn);
397 #endif
398 #ifdef CONFIG_TDLS
399 void wpa_tdls_generate_tpk(_adapter *padapter, void *sta);
400 int wpa_tdls_ftie_mic(u8 *kck, u8 trans_seq,
401 			u8 *lnkid, u8 *rsnie, u8 *timeoutie, u8 *ftie,
402 			u8 *mic);
403 int wpa_tdls_teardown_ftie_mic(u8 *kck, u8 *lnkid, u16 reason,
404 			u8 dialog_token, u8 trans_seq, u8 *ftie, u8 *mic);
405 int tdls_verify_mic(u8 *kck, u8 trans_seq,
406 			u8 *lnkid, u8 *rsnie, u8 *timeoutie, u8 *ftie);
407 #endif /* CONFIG_TDLS */
408 
409 void rtw_sec_restore_wep_key(_adapter *adapter);
410 u8 rtw_handle_tkip_countermeasure(_adapter *adapter, const char *caller);
411 
412 #ifdef CONFIG_WOWLAN
413 u16 rtw_calc_crc(u8  *pdata, int length);
414 #endif /*CONFIG_WOWLAN*/
415 
416 #define rtw_sec_chk_auth_alg(a, s) \
417 	((a)->securitypriv.auth_alg == (s))
418 
419 #define rtw_sec_chk_auth_type(a, s) \
420 	((a)->securitypriv.auth_type == (s))
421 
422 #endif /* __RTL871X_SECURITY_H_ */
423 
424 u32 rtw_calc_crc32(u8 *data, size_t len);
425