1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 #ifndef __RTW_SECURITY_H_
5 #define __RTW_SECURITY_H_
6 
7 #include "osdep_service.h"
8 #include "drv_types.h"
9 #include <crypto/arc4.h>
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 _SMS4_				0x06
18 
19 #define _WPA_IE_ID_	0xdd
20 #define _WPA2_IE_ID_	0x30
21 
22 enum {
23 	ENCRYP_PROTOCOL_OPENSYS,   /* open system */
24 	ENCRYP_PROTOCOL_WEP,       /* WEP */
25 	ENCRYP_PROTOCOL_WPA,       /* WPA */
26 	ENCRYP_PROTOCOL_WPA2,      /* WPA2 */
27 	ENCRYP_PROTOCOL_WAPI,      /* WAPI: Not support in this version */
28 	ENCRYP_PROTOCOL_MAX
29 };
30 
31 #ifndef Ndis802_11AuthModeWPA2
32 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
33 #endif
34 
35 #ifndef Ndis802_11AuthModeWPA2PSK
36 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
37 #endif
38 
39 union pn48	{
40 	u64	val;
41 
42 #ifdef __LITTLE_ENDIAN
43 	struct {
44 		u8 TSC0;
45 		u8 TSC1;
46 		u8 TSC2;
47 		u8 TSC3;
48 		u8 TSC4;
49 		u8 TSC5;
50 		u8 TSC6;
51 		u8 TSC7;
52 	} _byte_;
53 
54 #elif defined(__BIG_ENDIAN)
55 
56 	struct {
57 		u8 TSC7;
58 		u8 TSC6;
59 		u8 TSC5;
60 		u8 TSC4;
61 		u8 TSC3;
62 		u8 TSC2;
63 		u8 TSC1;
64 		u8 TSC0;
65 	} _byte_;
66 #endif
67 };
68 
69 union Keytype {
70 	u8   skey[16];
71 	u32    lkey[4];
72 };
73 
74 struct rt_pmkid_list {
75 	u8	bUsed;
76 	u8	Bssid[6];
77 	u8	PMKID[16];
78 	u8	SsidBuf[33];
79 	u8	*ssid_octet;
80 	u16	ssid_length;
81 };
82 
83 struct security_priv {
84 	u32	  dot11AuthAlgrthm;	/*  802.11 auth, could be open,
85 					 * shared, 8021x and authswitch */
86 	u32	  dot11PrivacyAlgrthm;	/*  This specify the privacy for
87 					 * shared auth. algorithm. */
88 	/* WEP */
89 	u32	  dot11PrivacyKeyIndex;	/*  this is only valid for legendary
90 					 * wep, 0~3 for key id.(tx key index) */
91 	union Keytype dot11DefKey[4];	/*  this is only valid for def. key */
92 	u32	dot11DefKeylen[4];
93 	u32 dot118021XGrpPrivacy;	/*  This specify the privacy algthm.
94 					 * used for Grp key */
95 	u32	dot118021XGrpKeyid;	/*  key id used for Grp Key
96 					 * ( tx key index) */
97 	union Keytype	dot118021XGrpKey[4];	/*  802.1x Group Key,
98 						 * for inx0 and inx1 */
99 	union Keytype	dot118021XGrptxmickey[4];
100 	union Keytype	dot118021XGrprxmickey[4];
101 	union pn48	dot11Grptxpn;		/* PN48 used for Grp Key xmit.*/
102 	union pn48	dot11Grprxpn;		/* PN48 used for Grp Key recv.*/
103 
104 	struct arc4_ctx xmit_arc4_ctx;
105 	struct arc4_ctx recv_arc4_ctx;
106 
107 	/* extend security capabilities for AP_MODE */
108 	unsigned int dot8021xalg;/* 0:disable, 1:psk, 2:802.1x */
109 	unsigned int wpa_psk;/* 0:disable, bit(0): WPA, bit(1):WPA2 */
110 	unsigned int wpa_group_cipher;
111 	unsigned int wpa2_group_cipher;
112 	unsigned int wpa_pairwise_cipher;
113 	unsigned int wpa2_pairwise_cipher;
114 	u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */
115 	int wps_ie_len;
116 	u8	binstallGrpkey;
117 	u8	busetkipkey;
118 	u8	bcheck_grpkey;
119 	u8	bgrpkey_handshake;
120 	s32	sw_encrypt;/* from registry_priv */
121 	s32	sw_decrypt;/* from registry_priv */
122 	s32	hw_decrypted;/* if the rx packets is hw_decrypted==false,i
123 			      * it means the hw has not been ready. */
124 
125 	/* keeps the auth_type & enc_status from upper layer
126 	 * ioctl(wpa_supplicant or wzc) */
127 	u32 ndisauthtype;	/*  NDIS_802_11_AUTHENTICATION_MODE */
128 	u32 ndisencryptstatus;	/*  NDIS_802_11_ENCRYPTION_STATUS */
129 	struct wlan_bssid_ex sec_bss;  /* for joinbss (h2c buffer) usage */
130 	struct ndis_802_11_wep ndiswep;
131 	u8 assoc_info[600];
132 	u8 szofcapability[256]; /* for wpa2 usage */
133 	u8 oidassociation[512]; /* for wpa/wpa2 usage */
134 	u8 authenticator_ie[256];  /* store ap security information element */
135 	u8 supplicant_ie[256];  /* store sta security information element */
136 
137 	/* for tkip countermeasure */
138 	u32 last_mic_err_time;
139 	u8	btkip_countermeasure;
140 	u8	btkip_wait_report;
141 	u32 btkip_countermeasure_time;
142 
143 	/*  */
144 	/*  For WPA2 Pre-Authentication. */
145 	/*  */
146 	struct rt_pmkid_list PMKIDList[NUM_PMKID_CACHE];
147 	u8	PMKIDIndex;
148 	u8 bWepDefaultKeyIdxSet;
149 };
150 
151 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst)		\
152 do {									\
153 	switch (psecuritypriv->dot11AuthAlgrthm) {			\
154 	case dot11AuthAlgrthm_Open:					\
155 	case dot11AuthAlgrthm_Shared:					\
156 	case dot11AuthAlgrthm_Auto:					\
157 		encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;	\
158 		break;							\
159 	case dot11AuthAlgrthm_8021X:					\
160 		if (bmcst)						\
161 			encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\
162 		else							\
163 			encry_algo = (u8)psta->dot118021XPrivacy;	\
164 		break;							\
165 	case dot11AuthAlgrthm_WAPI:					\
166 		encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;	\
167 		break;							\
168 	}								\
169 } while (0)
170 
171 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt)			\
172 do {									\
173 	switch (encrypt) {						\
174 	case _WEP40_:							\
175 	case _WEP104_:							\
176 		iv_len = 4;						\
177 		icv_len = 4;						\
178 		break;							\
179 	case _TKIP_:							\
180 		iv_len = 8;						\
181 		icv_len = 4;						\
182 		break;							\
183 	case _AES_:							\
184 		iv_len = 8;						\
185 		icv_len = 8;						\
186 		break;							\
187 	case _SMS4_:							\
188 		iv_len = 18;						\
189 		icv_len = 16;						\
190 		break;							\
191 	default:							\
192 		iv_len = 0;						\
193 		icv_len = 0;						\
194 		break;							\
195 	}								\
196 } while (0)
197 
198 #define GET_TKIP_PN(iv, dot11txpn)					\
199 do {									\
200 	dot11txpn._byte_.TSC0 = iv[2];					\
201 	dot11txpn._byte_.TSC1 = iv[0];					\
202 	dot11txpn._byte_.TSC2 = iv[4];					\
203 	dot11txpn._byte_.TSC3 = iv[5];					\
204 	dot11txpn._byte_.TSC4 = iv[6];					\
205 	dot11txpn._byte_.TSC5 = iv[7];					\
206 } while (0)
207 
208 #define ROL32(A, n)	(((A) << (n)) | (((A)>>(32-(n)))  & ((1UL << (n)) - 1)))
209 #define ROR32(A, n)	ROL32((A), 32-(n))
210 
211 struct mic_data {
212 	u32  K0, K1;         /*  Key */
213 	u32  L, R;           /*  Current state */
214 	u32  M;              /*  Message accumulator (single word) */
215 	u32  nBytesInM;      /*  # bytes in M */
216 };
217 
218 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key);
219 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b);
220 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
221 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst);
222 void rtw_seccalctkipmic(u8 *key, u8 *header, u8 *data, u32 data_len,
223 			u8 *Miccode, u8   priority);
224 u32 rtw_aes_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe);
225 u32 rtw_tkip_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe);
226 void rtw_wep_encrypt(struct adapter *padapter, struct xmit_frame *pxmitframe);
227 u32 rtw_aes_decrypt(struct adapter *padapter, struct recv_frame *precvframe);
228 u32 rtw_tkip_decrypt(struct adapter *padapter, struct recv_frame *precvframe);
229 void rtw_wep_decrypt(struct adapter *padapter, struct recv_frame *precvframe);
230 
231 #endif	/* __RTL871X_SECURITY_H_ */
232