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 
11 #define _NO_PRIVACY_		0x0
12 #define _WEP40_				0x1
13 #define _TKIP_				0x2
14 #define _TKIP_WTMIC_		0x3
15 #define _AES_				0x4
16 #define _WEP104_			0x5
17 #define _WEP_WPA_MIXED_	0x07  /*  WEP + WPA */
18 #define _SMS4_				0x06
19 #define _BIP_				0x8
20 #define is_wep_enc(alg) (((alg) == _WEP40_) || ((alg) == _WEP104_))
21 
22 const char *security_type_str(u8 value);
23 
24 #define SHA256_MAC_LEN 32
25 #define AES_BLOCK_SIZE 16
26 #define AES_PRIV_SIZE (4 * 44)
27 
28 #define RTW_KEK_LEN 16
29 #define RTW_KCK_LEN 16
30 #define RTW_REPLAY_CTR_LEN 8
31 
32 enum {
33 	ENCRYP_PROTOCOL_OPENSYS,   /* open system */
34 	ENCRYP_PROTOCOL_WEP,       /* WEP */
35 	ENCRYP_PROTOCOL_WPA,       /* WPA */
36 	ENCRYP_PROTOCOL_WPA2,      /* WPA2 */
37 	ENCRYP_PROTOCOL_WAPI,      /* WAPI: Not support in this version */
38 	ENCRYP_PROTOCOL_MAX
39 };
40 
41 
42 #ifndef Ndis802_11AuthModeWPA2
43 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
44 #endif
45 
46 #ifndef Ndis802_11AuthModeWPA2PSK
47 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
48 #endif
49 
50 union pn48	{
51 
52 	u64	val;
53 
54 #ifdef __LITTLE_ENDIAN
55 
56 struct {
57   u8 TSC0;
58   u8 TSC1;
59   u8 TSC2;
60   u8 TSC3;
61   u8 TSC4;
62   u8 TSC5;
63   u8 TSC6;
64   u8 TSC7;
65 } _byte_;
66 #else
67 struct {
68   u8 TSC7;
69   u8 TSC6;
70   u8 TSC5;
71   u8 TSC4;
72   u8 TSC3;
73   u8 TSC2;
74   u8 TSC1;
75   u8 TSC0;
76 } _byte_;
77 #endif
78 
79 };
80 
81 union Keytype {
82         u8   skey[16];
83         u32    lkey[4];
84 };
85 
86 
87 struct rt_pmkid_list {
88 	u8 				bUsed;
89 	u8 				Bssid[6];
90 	u8 				PMKID[16];
91 	u8 				SsidBuf[33];
92 	u8 *ssid_octet;
93 	u16 					ssid_length;
94 };
95 
96 
97 struct security_priv {
98 	u32   dot11AuthAlgrthm;		/*  802.11 auth, could be open, shared, 8021x and authswitch */
99 	u32   dot11PrivacyAlgrthm;	/*  This specify the privacy for shared auth. algorithm. */
100 
101 	/* WEP */
102 	u32   dot11PrivacyKeyIndex;	/*  this is only valid for legendary wep, 0~3 for key id. (tx key index) */
103 	union Keytype dot11DefKey[4];	/*  this is only valid for def. key */
104 	u32 dot11DefKeylen[4];
105 	u8 key_mask; /* use to restore wep key after hal_init */
106 
107 	u32 dot118021XGrpPrivacy;	/*  This specify the privacy algthm. used for Grp key */
108 	u32 dot118021XGrpKeyid;		/*  key id used for Grp Key (tx key index) */
109 	union Keytype	dot118021XGrpKey[BIP_MAX_KEYID];	/*  802.1x Group Key, for inx0 and inx1 */
110 	union Keytype	dot118021XGrptxmickey[BIP_MAX_KEYID];
111 	union Keytype	dot118021XGrprxmickey[BIP_MAX_KEYID];
112 	union pn48		dot11Grptxpn;			/*  PN48 used for Grp Key xmit. */
113 	union pn48		dot11Grprxpn;			/*  PN48 used for Grp Key recv. */
114 	u32 dot11wBIPKeyid;						/*  key id used for BIP Key (tx key index) */
115 	union Keytype	dot11wBIPKey[6];		/*  BIP Key, for index4 and index5 */
116 	union pn48		dot11wBIPtxpn;			/*  PN48 used for Grp Key xmit. */
117 	union pn48		dot11wBIPrxpn;			/*  PN48 used for Grp Key recv. */
118 
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 
127 	u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */
128 	int wps_ie_len;
129 
130 
131 	u8 binstallGrpkey;
132 	u8 binstallBIPkey;
133 	u8 busetkipkey;
134 	/* _timer tkip_timer; */
135 	u8 bcheck_grpkey;
136 	u8 bgrpkey_handshake;
137 
138 	s32	sw_encrypt;/* from registry_priv */
139 	s32	sw_decrypt;/* from registry_priv */
140 
141 	s32	hw_decrypted;/* if the rx packets is hw_decrypted ==false, it means the hw has not been ready. */
142 
143 
144 	/* keeps the auth_type & enc_status from upper layer ioctl(wpa_supplicant or wzc) */
145 	u32 ndisauthtype;	/*  enum ndis_802_11_authentication_mode */
146 	u32 ndisencryptstatus;	/*  NDIS_802_11_ENCRYPTION_STATUS */
147 
148 	struct wlan_bssid_ex sec_bss;  /* for joinbss (h2c buffer) usage */
149 
150 	struct ndis_802_11_wep ndiswep;
151 
152 	u8 assoc_info[600];
153 	u8 szofcapability[256]; /* for wpa2 usage */
154 	u8 oidassociation[512]; /* for wpa/wpa2 usage */
155 	u8 authenticator_ie[256];  /* store ap security information element */
156 	u8 supplicant_ie[256];  /* store sta security information element */
157 
158 
159 	/* for tkip countermeasure */
160 	unsigned long last_mic_err_time;
161 	u8 btkip_countermeasure;
162 	u8 btkip_wait_report;
163 	u32 btkip_countermeasure_time;
164 
165 	/*  For WPA2 Pre-Authentication. */
166 	struct rt_pmkid_list		PMKIDList[NUM_PMKID_CACHE];	/*  Renamed from PreAuthKey[NUM_PRE_AUTH_KEY]. Annie, 2006-10-13. */
167 	u8 		PMKIDIndex;
168 
169 	u8 bWepDefaultKeyIdxSet;
170 
171 };
172 
173 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst)\
174 do {\
175 	switch (psecuritypriv->dot11AuthAlgrthm)\
176 	{\
177 		case dot11AuthAlgrthm_Open:\
178 		case dot11AuthAlgrthm_Shared:\
179 		case dot11AuthAlgrthm_Auto:\
180 			encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
181 			break;\
182 		case dot11AuthAlgrthm_8021X:\
183 			if (bmcst)\
184 				encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\
185 			else\
186 				encry_algo = (u8)psta->dot118021XPrivacy;\
187 			break;\
188 	     case dot11AuthAlgrthm_WAPI:\
189 		     encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
190 		     break;\
191 	} \
192 } while (0)
193 
194 #define _AES_IV_LEN_ 8
195 
196 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt)\
197 do {\
198 	switch (encrypt)\
199 	{\
200 		case _WEP40_:\
201 		case _WEP104_:\
202 			iv_len = 4;\
203 			icv_len = 4;\
204 			break;\
205 		case _TKIP_:\
206 			iv_len = 8;\
207 			icv_len = 4;\
208 			break;\
209 		case _AES_:\
210 			iv_len = 8;\
211 			icv_len = 8;\
212 			break;\
213 		case _SMS4_:\
214 			iv_len = 18;\
215 			icv_len = 16;\
216 			break;\
217 		default:\
218 			iv_len = 0;\
219 			icv_len = 0;\
220 			break;\
221 	} \
222 } while (0)
223 
224 
225 #define GET_TKIP_PN(iv, dot11txpn)\
226 do {\
227 	dot11txpn._byte_.TSC0 = iv[2];\
228 	dot11txpn._byte_.TSC1 = iv[0];\
229 	dot11txpn._byte_.TSC2 = iv[4];\
230 	dot11txpn._byte_.TSC3 = iv[5];\
231 	dot11txpn._byte_.TSC4 = iv[6];\
232 	dot11txpn._byte_.TSC5 = iv[7];\
233 } while (0)
234 
235 
236 #define ROL32(A, n)	(((A) << (n)) | (((A)>>(32-(n)))  & ((1UL << (n)) - 1)))
237 #define ROR32(A, n)	ROL32((A), 32-(n))
238 
239 struct mic_data {
240 	u32  K0, K1;         /*  Key */
241 	u32  L, R;           /*  Current state */
242 	u32  M;              /*  Message accumulator (single word) */
243 	u32     nBytesInM;      /*  # bytes in M */
244 };
245 
246 extern const u32 Te0[256];
247 extern const u32 Te1[256];
248 extern const u32 Te2[256];
249 extern const u32 Te3[256];
250 extern const u32 Te4[256];
251 extern const u32 Td0[256];
252 extern const u32 Td1[256];
253 extern const u32 Td2[256];
254 extern const u32 Td3[256];
255 extern const u32 Td4[256];
256 extern const u32 rcon[10];
257 extern const u8 Td4s[256];
258 extern const u8 rcons[10];
259 
260 #define RCON(i) (rcons[(i)] << 24)
261 
262 static inline u32 rotr(u32 val, int bits)
263 {
264 	return (val >> bits) | (val << (32 - bits));
265 }
266 
267 #define TE0(i) Te0[((i) >> 24) & 0xff]
268 #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
269 #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
270 #define TE3(i) rotr(Te0[(i) & 0xff], 24)
271 #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
272 #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
273 #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
274 #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
275 #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
276 #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
277 #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
278 #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
279 #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
280 
281 #define TD0(i) Td0[((i) >> 24) & 0xff]
282 #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
283 #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
284 #define TD3(i) rotr(Td0[(i) & 0xff], 24)
285 #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24)
286 #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16)
287 #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8)
288 #define TD44(i) (Td4s[(i) & 0xff])
289 #define TD0_(i) Td0[(i) & 0xff]
290 #define TD1_(i) rotr(Td0[(i) & 0xff], 8)
291 #define TD2_(i) rotr(Td0[(i) & 0xff], 16)
292 #define TD3_(i) rotr(Td0[(i) & 0xff], 24)
293 
294 #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
295 			((u32)(pt)[2] <<  8) ^ ((u32)(pt)[3]))
296 
297 #define PUTU32(ct, st) { \
298 (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
299 (ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }
300 
301 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
302 			 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
303 
304 #define WPA_PUT_LE16(a, val)			\
305 	do {					\
306 		(a)[1] = ((u16) (val)) >> 8;	\
307 		(a)[0] = ((u16) (val)) & 0xff;	\
308 	} while (0)
309 
310 #define WPA_PUT_BE32(a, val)					\
311 	do {							\
312 		(a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);	\
313 		(a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);	\
314 		(a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);	\
315 		(a)[3] = (u8) (((u32) (val)) & 0xff);		\
316 	} while (0)
317 
318 #define WPA_PUT_BE64(a, val)				\
319 	do {						\
320 		(a)[0] = (u8) (((u64) (val)) >> 56);	\
321 		(a)[1] = (u8) (((u64) (val)) >> 48);	\
322 		(a)[2] = (u8) (((u64) (val)) >> 40);	\
323 		(a)[3] = (u8) (((u64) (val)) >> 32);	\
324 		(a)[4] = (u8) (((u64) (val)) >> 24);	\
325 		(a)[5] = (u8) (((u64) (val)) >> 16);	\
326 		(a)[6] = (u8) (((u64) (val)) >> 8);	\
327 		(a)[7] = (u8) (((u64) (val)) & 0xff);	\
328 	} while (0)
329 
330 /* ===== start - public domain SHA256 implementation ===== */
331 
332 /* This is based on SHA256 implementation in LibTomCrypt that was released into
333  * public domain by Tom St Denis. */
334 
335 /* Various logical functions */
336 #define RORc(x, y) \
337 (((((unsigned long) (x) & 0xFFFFFFFFUL) >> (unsigned long) ((y) & 31)) | \
338    ((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL)
339 #define Ch(x, y, z)       (z ^ (x & (y ^ z)))
340 #define Maj(x, y, z)      (((x | y) & z) | (x & y))
341 #define S(x, n)         RORc((x), (n))
342 #define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
343 #define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
344 #define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
345 #define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
346 #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
347 #ifndef MIN
348 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
349 #endif
350 int omac1_aes_128(u8 *key, u8 *data, size_t data_len, u8 *mac);
351 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key);
352 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b);
353 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
354 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst);
355 
356 void rtw_seccalctkipmic(
357 	u8 *key,
358 	u8 *header,
359 	u8 *data,
360 	u32 data_len,
361 	u8 *Miccode,
362 	u8   priority);
363 
364 u32 rtw_aes_encrypt(struct adapter *padapter, u8 *pxmitframe);
365 u32 rtw_tkip_encrypt(struct adapter *padapter, u8 *pxmitframe);
366 void rtw_wep_encrypt(struct adapter *padapter, u8  *pxmitframe);
367 
368 u32 rtw_aes_decrypt(struct adapter *padapter, u8  *precvframe);
369 u32 rtw_tkip_decrypt(struct adapter *padapter, u8  *precvframe);
370 void rtw_wep_decrypt(struct adapter *padapter, u8  *precvframe);
371 u32 rtw_BIP_verify(struct adapter *padapter, u8 *precvframe);
372 
373 void rtw_sec_restore_wep_key(struct adapter *adapter);
374 u8 rtw_handle_tkip_countermeasure(struct adapter *adapter, const char *caller);
375 
376 #endif	/* __RTL871X_SECURITY_H_ */
377