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