1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  *
6  * Modifications for inclusion into the Linux staging tree are
7  * Copyright(c) 2010 Larry Finger. All rights reserved.
8  *
9  * Contact information:
10  * WLAN FAE <wlanfae@realtek.com>
11  * Larry Finger <Larry.Finger@lwfinger.net>
12  *
13  ******************************************************************************/
14 #ifndef __RTL871X_SECURITY_H_
15 #define __RTL871X_SECURITY_H_
16 
17 #include "osdep_service.h"
18 #include "drv_types.h"
19 
20 #define _NO_PRIVACY_	0x0
21 #define _WEP40_		0x1
22 #define _TKIP_		0x2
23 #define _TKIP_WTMIC_	0x3
24 #define _AES_		0x4
25 #define _WEP104_	0x5
26 
27 #define _WPA_IE_ID_	0xdd
28 #define _WPA2_IE_ID_	0x30
29 
30 #ifndef Ndis802_11AuthModeWPA2
31 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
32 #endif
33 
34 #ifndef Ndis802_11AuthModeWPA2PSK
35 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
36 #endif
37 
38 union pn48 {
39 	u64 val;
40 #if defined(__BIG_ENDIAN)
41 	struct {
42 		u8 TSC7;
43 		u8 TSC6;
44 		u8 TSC5;
45 		u8 TSC4;
46 		u8 TSC3;
47 		u8 TSC2;
48 		u8 TSC1;
49 		u8 TSC0;
50 	} _byte_;
51 #else
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 #endif
63 };
64 
65 union Keytype {
66 	u8 skey[16];
67 	u32 lkey[4];
68 };
69 
70 struct RT_PMKID_LIST {
71 	u8 bUsed;
72 	u8 Bssid[6];
73 	u8 PMKID[16];
74 	u8 SsidBuf[33];
75 	u8 *ssid_octet;
76 	u16 ssid_length;
77 };
78 
79 struct security_priv {
80 	u32 AuthAlgrthm;		/* 802.11 auth, could be open, shared,
81 					 * 8021x and authswitch
82 					 */
83 	u32 PrivacyAlgrthm;		/* This specify the privacy for shared
84 					 * auth. algorithm.
85 					 */
86 	u32 PrivacyKeyIndex;		/* this is only valid for legendary
87 					 * wep, 0~3 for key id.
88 					 */
89 	union Keytype DefKey[4];	/* this is only valid for def. key */
90 	u32 DefKeylen[4];
91 	u32 XGrpPrivacy;		/* This specify the privacy algthm.
92 					 * used for Grp key
93 					 */
94 	u32 XGrpKeyid;			/* key id used for Grp Key */
95 	union Keytype	XGrpKey[2];	/* 802.1x Group Key, for
96 					 * inx0 and inx1
97 					 */
98 	union Keytype	XGrptxmickey[2];
99 	union Keytype	XGrprxmickey[2];
100 	union pn48 Grptxpn;		/* PN48 used for Grp Key xmit. */
101 	union pn48 Grprxpn;		/* PN48 used for Grp Key recv. */
102 	u8 wps_hw_pbc_pressed;/*for hw pbc pressed*/
103 	u8 wps_phase;/*for wps*/
104 	u8 wps_ie[MAX_WPA_IE_LEN<<2];
105 	int wps_ie_len;
106 	u8	binstallGrpkey;
107 	u8	busetkipkey;
108 	struct timer_list tkip_timer;
109 	u8	bcheck_grpkey;
110 	u8	bgrpkey_handshake;
111 	s32	sw_encrypt;	/* from registry_priv */
112 	s32	sw_decrypt;	/* from registry_priv */
113 	s32	hw_decrypted;	/* if the rx packets is hw_decrypted==false,
114 				 * it means the hw has not been ready.
115 				 */
116 	u32 ndisauthtype;	/* keeps the auth_type & enc_status from upper
117 				 * layer ioctl(wpa_supplicant or wzc)
118 				 */
119 	u32 ndisencryptstatus;
120 	struct wlan_bssid_ex sec_bss;  /* for joinbss (h2c buffer) usage */
121 	struct NDIS_802_11_WEP ndiswep;
122 	u8 assoc_info[600];
123 	u8 szofcapability[256]; /* for wpa2 usage */
124 	u8 oidassociation[512]; /* for wpa/wpa2 usage */
125 	u8 authenticator_ie[256];  /* store ap security information element */
126 	u8 supplicant_ie[256];  /* store sta security information element */
127 	/* for tkip countermeasure */
128 	u32 last_mic_err_time;
129 	u8	btkip_countermeasure;
130 	u8	btkip_wait_report;
131 	u32 btkip_countermeasure_time;
132 	/*-------------------------------------------------------------------
133 	 * For WPA2 Pre-Authentication.
134 	 *------------------------------------------------------------------
135 	 **/
136 	struct RT_PMKID_LIST		PMKIDList[NUM_PMKID_CACHE];
137 	u8				PMKIDIndex;
138 };
139 
140 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst) \
141 do { \
142 	switch (psecuritypriv->AuthAlgrthm) { \
143 	case 0: \
144 	case 1: \
145 	case 3: \
146 		encry_algo = (u8)psecuritypriv->PrivacyAlgrthm; \
147 		break; \
148 	case 2: \
149 		if (bmcst) \
150 			encry_algo = (u8)psecuritypriv->XGrpPrivacy; \
151 		else \
152 			encry_algo = (u8)psta->XPrivacy; \
153 		break; \
154 	} \
155 } while (0)
156 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt)\
157 do {\
158 	switch (encrypt) { \
159 	case _WEP40_: \
160 	case _WEP104_: \
161 		iv_len = 4; \
162 		icv_len = 4; \
163 		break; \
164 	case _TKIP_: \
165 		iv_len = 8; \
166 		icv_len = 4; \
167 		break; \
168 	case _AES_: \
169 		iv_len = 8; \
170 		icv_len = 8; \
171 		break; \
172 	default: \
173 		iv_len = 0; \
174 		icv_len = 0; \
175 		break; \
176 	} \
177 } while (0)
178 #define GET_TKIP_PN(iv, txpn) \
179 do {\
180 	txpn._byte_.TSC0 = iv[2];\
181 	txpn._byte_.TSC1 = iv[0];\
182 	txpn._byte_.TSC2 = iv[4];\
183 	txpn._byte_.TSC3 = iv[5];\
184 	txpn._byte_.TSC4 = iv[6];\
185 	txpn._byte_.TSC5 = iv[7];\
186 } while (0)
187 
188 #define ROL32(A, n) (((A) << (n)) | (((A)>>(32-(n)))  & ((1UL << (n)) - 1)))
189 #define ROR32(A, n) ROL32((A), 32 - (n))
190 
191 struct mic_data {
192 	u32  K0, K1;         /* Key */
193 	u32  L, R;           /* Current state */
194 	u32  M;              /* Message accumulator (single word) */
195 	u32  nBytesInM;      /* # bytes in M */
196 };
197 
198 void seccalctkipmic(
199 	u8  *key,
200 	u8  *header,
201 	u8  *data,
202 	u32  data_len,
203 	u8  *Miccode,
204 	u8   priority);
205 
206 void r8712_secmicsetkey(struct mic_data *pmicdata, u8 *key);
207 void r8712_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
208 void r8712_secgetmic(struct mic_data *pmicdata, u8 *dst);
209 u32 r8712_aes_encrypt(struct _adapter *padapter, u8 *pxmitframe);
210 u32 r8712_tkip_encrypt(struct _adapter *padapter, u8 *pxmitframe);
211 void r8712_wep_encrypt(struct _adapter *padapter, u8  *pxmitframe);
212 void r8712_aes_decrypt(struct _adapter *padapter, u8  *precvframe);
213 void r8712_tkip_decrypt(struct _adapter *padapter, u8  *precvframe);
214 void r8712_wep_decrypt(struct _adapter *padapter, u8  *precvframe);
215 void r8712_use_tkipkey_handler(struct timer_list *t);
216 
217 #endif	/*__RTL871X_SECURITY_H_ */
218 
219