GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / rtl8723bs / include / rtw_security.h
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 #include <crypto/arc4.h>
11
12 #define _NO_PRIVACY_            0x0
13 #define _WEP40_                         0x1
14 #define _TKIP_                          0x2
15 #define _TKIP_WTMIC_            0x3
16 #define _AES_                           0x4
17 #define _WEP104_                        0x5
18 #define _WEP_WPA_MIXED_ 0x07  /*  WEP + WPA */
19 #define _SMS4_                          0x06
20 #define _BIP_                           0x8
21 #define is_wep_enc(alg) (((alg) == _WEP40_) || ((alg) == _WEP104_))
22
23 const char *security_type_str(u8 value);
24
25 #define SHA256_MAC_LEN 32
26 #define AES_BLOCK_SIZE 16
27 #define AES_PRIV_SIZE (4 * 44)
28
29 #define RTW_KEK_LEN 16
30 #define RTW_KCK_LEN 16
31 #define RTW_REPLAY_CTR_LEN 8
32
33 enum {
34         ENCRYP_PROTOCOL_OPENSYS,   /* open system */
35         ENCRYP_PROTOCOL_WEP,       /* WEP */
36         ENCRYP_PROTOCOL_WPA,       /* WPA */
37         ENCRYP_PROTOCOL_WPA2,      /* WPA2 */
38         ENCRYP_PROTOCOL_WAPI,      /* WAPI: Not support in this version */
39         ENCRYP_PROTOCOL_MAX
40 };
41
42
43 #ifndef Ndis802_11AuthModeWPA2
44 #define Ndis802_11AuthModeWPA2 (Ndis802_11AuthModeWPANone + 1)
45 #endif
46
47 #ifndef Ndis802_11AuthModeWPA2PSK
48 #define Ndis802_11AuthModeWPA2PSK (Ndis802_11AuthModeWPANone + 2)
49 #endif
50
51 union pn48      {
52
53         u64     val;
54
55 #ifdef __LITTLE_ENDIAN
56
57 struct {
58   u8 TSC0;
59   u8 TSC1;
60   u8 TSC2;
61   u8 TSC3;
62   u8 TSC4;
63   u8 TSC5;
64   u8 TSC6;
65   u8 TSC7;
66 } _byte_;
67 #else
68 struct {
69   u8 TSC7;
70   u8 TSC6;
71   u8 TSC5;
72   u8 TSC4;
73   u8 TSC3;
74   u8 TSC2;
75   u8 TSC1;
76   u8 TSC0;
77 } _byte_;
78 #endif
79
80 };
81
82 union Keytype {
83         u8   skey[16];
84         u32    lkey[4];
85 };
86
87
88 struct rt_pmkid_list {
89         u8                              bUsed;
90         u8                              Bssid[6];
91         u8                              PMKID[16];
92         u8                              SsidBuf[33];
93         u8 *ssid_octet;
94         u16                                     ssid_length;
95 };
96
97
98 struct security_priv {
99         u32   dot11AuthAlgrthm;         /*  802.11 auth, could be open, shared, 8021x and authswitch */
100         u32   dot11PrivacyAlgrthm;      /*  This specify the privacy for shared auth. algorithm. */
101
102         /* WEP */
103         u32   dot11PrivacyKeyIndex;     /*  this is only valid for legendary wep, 0~3 for key id. (tx key index) */
104         union Keytype dot11DefKey[4];   /*  this is only valid for def. key */
105         u32 dot11DefKeylen[4];
106         u8 key_mask; /* use to restore wep key after hal_init */
107
108         u32 dot118021XGrpPrivacy;       /*  This specify the privacy algthm. used for Grp key */
109         u32 dot118021XGrpKeyid;         /*  key id used for Grp Key (tx key index) */
110         union Keytype   dot118021XGrpKey[BIP_MAX_KEYID];        /*  802.1x Group Key, for inx0 and inx1 */
111         union Keytype   dot118021XGrptxmickey[BIP_MAX_KEYID];
112         union Keytype   dot118021XGrprxmickey[BIP_MAX_KEYID];
113         union pn48              dot11Grptxpn;                   /*  PN48 used for Grp Key xmit. */
114         union pn48              dot11Grprxpn;                   /*  PN48 used for Grp Key recv. */
115         u32 dot11wBIPKeyid;                                             /*  key id used for BIP Key (tx key index) */
116         union Keytype   dot11wBIPKey[6];                /*  BIP Key, for index4 and index5 */
117         union pn48              dot11wBIPtxpn;                  /*  PN48 used for Grp Key xmit. */
118         union pn48              dot11wBIPrxpn;                  /*  PN48 used for Grp Key recv. */
119
120         /* extend security capabilities for AP_MODE */
121         unsigned int dot8021xalg;/* 0:disable, 1:psk, 2:802.1x */
122         unsigned int wpa_psk;/* 0:disable, bit(0): WPA, bit(1):WPA2 */
123         unsigned int wpa_group_cipher;
124         unsigned int wpa2_group_cipher;
125         unsigned int wpa_pairwise_cipher;
126         unsigned int wpa2_pairwise_cipher;
127
128         u8 wps_ie[MAX_WPS_IE_LEN];/* added in assoc req */
129         int wps_ie_len;
130
131         struct arc4_ctx xmit_arc4_ctx;
132         struct arc4_ctx recv_arc4_ctx;
133
134         u8 binstallGrpkey;
135         u8 binstallBIPkey;
136         u8 busetkipkey;
137         /* _timer tkip_timer; */
138         u8 bcheck_grpkey;
139         u8 bgrpkey_handshake;
140
141         s32     sw_encrypt;/* from registry_priv */
142         s32     sw_decrypt;/* from registry_priv */
143
144         s32     hw_decrypted;/* if the rx packets is hw_decrypted ==false, it means the hw has not been ready. */
145
146
147         /* keeps the auth_type & enc_status from upper layer ioctl(wpa_supplicant or wzc) */
148         u32 ndisauthtype;       /*  enum ndis_802_11_authentication_mode */
149         u32 ndisencryptstatus;  /*  NDIS_802_11_ENCRYPTION_STATUS */
150
151         struct wlan_bssid_ex sec_bss;  /* for joinbss (h2c buffer) usage */
152
153         struct ndis_802_11_wep ndiswep;
154
155         u8 assoc_info[600];
156         u8 szofcapability[256]; /* for wpa2 usage */
157         u8 oidassociation[512]; /* for wpa/wpa2 usage */
158         u8 authenticator_ie[256];  /* store ap security information element */
159         u8 supplicant_ie[256];  /* store sta security information element */
160
161
162         /* for tkip countermeasure */
163         unsigned long last_mic_err_time;
164         u8 btkip_countermeasure;
165         u8 btkip_wait_report;
166         u32 btkip_countermeasure_time;
167
168         /*  For WPA2 Pre-Authentication. */
169         struct rt_pmkid_list            PMKIDList[NUM_PMKID_CACHE];     /*  Renamed from PreAuthKey[NUM_PRE_AUTH_KEY]. Annie, 2006-10-13. */
170         u8              PMKIDIndex;
171
172         u8 bWepDefaultKeyIdxSet;
173
174 };
175
176 #define GET_ENCRY_ALGO(psecuritypriv, psta, encry_algo, bmcst)\
177 do {\
178         switch (psecuritypriv->dot11AuthAlgrthm)\
179         {\
180                 case dot11AuthAlgrthm_Open:\
181                 case dot11AuthAlgrthm_Shared:\
182                 case dot11AuthAlgrthm_Auto:\
183                         encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
184                         break;\
185                 case dot11AuthAlgrthm_8021X:\
186                         if (bmcst)\
187                                 encry_algo = (u8)psecuritypriv->dot118021XGrpPrivacy;\
188                         else\
189                                 encry_algo = (u8)psta->dot118021XPrivacy;\
190                         break;\
191              case dot11AuthAlgrthm_WAPI:\
192                      encry_algo = (u8)psecuritypriv->dot11PrivacyAlgrthm;\
193                      break;\
194         } \
195 } while (0)
196
197 #define SET_ICE_IV_LEN(iv_len, icv_len, encrypt)\
198 do {\
199         switch (encrypt)\
200         {\
201                 case _WEP40_:\
202                 case _WEP104_:\
203                         iv_len = 4;\
204                         icv_len = 4;\
205                         break;\
206                 case _TKIP_:\
207                         iv_len = 8;\
208                         icv_len = 4;\
209                         break;\
210                 case _AES_:\
211                         iv_len = 8;\
212                         icv_len = 8;\
213                         break;\
214                 case _SMS4_:\
215                         iv_len = 18;\
216                         icv_len = 16;\
217                         break;\
218                 default:\
219                         iv_len = 0;\
220                         icv_len = 0;\
221                         break;\
222         } \
223 } while (0)
224
225
226 #define GET_TKIP_PN(iv, dot11txpn)\
227 do {\
228         dot11txpn._byte_.TSC0 = iv[2];\
229         dot11txpn._byte_.TSC1 = iv[0];\
230         dot11txpn._byte_.TSC2 = iv[4];\
231         dot11txpn._byte_.TSC3 = iv[5];\
232         dot11txpn._byte_.TSC4 = iv[6];\
233         dot11txpn._byte_.TSC5 = iv[7];\
234 } while (0)
235
236
237 #define ROL32(A, n)     (((A) << (n)) | (((A)>>(32-(n)))  & ((1UL << (n)) - 1)))
238 #define ROR32(A, n)     ROL32((A), 32-(n))
239
240 struct mic_data {
241         u32  K0, K1;         /*  Key */
242         u32  L, R;           /*  Current state */
243         u32  M;              /*  Message accumulator (single word) */
244         u32     nBytesInM;      /*  # bytes in M */
245 };
246
247 /* ===== start - public domain SHA256 implementation ===== */
248
249 /* This is based on SHA256 implementation in LibTomCrypt that was released into
250  * public domain by Tom St Denis. */
251
252 int omac1_aes_128(u8 *key, u8 *data, size_t data_len, u8 *mac);
253 void rtw_secmicsetkey(struct mic_data *pmicdata, u8 *key);
254 void rtw_secmicappendbyte(struct mic_data *pmicdata, u8 b);
255 void rtw_secmicappend(struct mic_data *pmicdata, u8 *src, u32 nBytes);
256 void rtw_secgetmic(struct mic_data *pmicdata, u8 *dst);
257
258 void rtw_seccalctkipmic(
259         u8 *key,
260         u8 *header,
261         u8 *data,
262         u32 data_len,
263         u8 *Miccode,
264         u8   priority);
265
266 u32 rtw_aes_encrypt(struct adapter *padapter, u8 *pxmitframe);
267 u32 rtw_tkip_encrypt(struct adapter *padapter, u8 *pxmitframe);
268 void rtw_wep_encrypt(struct adapter *padapter, u8  *pxmitframe);
269
270 u32 rtw_aes_decrypt(struct adapter *padapter, u8  *precvframe);
271 u32 rtw_tkip_decrypt(struct adapter *padapter, u8  *precvframe);
272 void rtw_wep_decrypt(struct adapter *padapter, u8  *precvframe);
273 u32 rtw_BIP_verify(struct adapter *padapter, u8 *precvframe);
274
275 void rtw_sec_restore_wep_key(struct adapter *adapter);
276 u8 rtw_handle_tkip_countermeasure(struct adapter *adapter, const char *caller);
277
278 #endif  /* __RTL871X_SECURITY_H_ */