xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/crypto/src/wlan_nl_to_crypto_params.c (revision 70a19e16789e308182f63b15c75decec7bf0b342)
1 /*
2  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: wlan_nl_to_crypto_params.c
22  *
23  * Conversion of NL param type to Crypto param type APIs implementation
24  *
25  */
26 
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/version.h>
30 #include <linux/netdevice.h>
31 #include <net/netlink.h>
32 #include <net/cfg80211.h>
33 
34 #include <qdf_types.h>
35 #include "wlan_objmgr_vdev_obj.h"
36 #include <qdf_module.h>
37 
38 #include "wlan_nl_to_crypto_params.h"
39 #include "wlan_crypto_global_def.h"
40 
41 /**
42  * struct osif_akm_crypto_mapping - mapping akm type received from
43  *                                 NL to internal crypto type
44  * @akm_suite: NL akm type
45  * @akm_type_crypto: akm crypto type
46  *
47  * mapping akm type received from NL to internal crypto type
48  */
49 struct osif_akm_type_crypto_mapping {
50 	u32 akm_suite;
51 	wlan_crypto_key_mgmt akm_type_crypto;
52 };
53 
54 /**
55  * struct osif_cipher_crypto_mapping - mapping cipher type received from NL
56  *                                    to internal crypto cipher type
57  * @cipher_suite: NL cipher type
58  * @cipher_crypto: cipher crypto type
59  * @cipher_len: Length of the cipher
60  *
61  * mapping cipher type received from NL to internal crypto cipher type
62  */
63 struct osif_cipher_crypto_mapping {
64 	u32 cipher_suite;
65 	wlan_crypto_cipher_type cipher_crypto;
66 	u32 cipher_len;
67 };
68 
69 /**
70  * mapping table for auth type received from NL and crypto auth type
71  */
72 static const wlan_crypto_auth_mode
73 	osif_auth_type_crypto_mapping[] = {
74 	[NL80211_AUTHTYPE_AUTOMATIC] = WLAN_CRYPTO_AUTH_AUTO,
75 	[NL80211_AUTHTYPE_OPEN_SYSTEM] = WLAN_CRYPTO_AUTH_OPEN,
76 	[NL80211_AUTHTYPE_FT] = WLAN_CRYPTO_AUTH_OPEN,
77 	[NL80211_AUTHTYPE_SHARED_KEY] = WLAN_CRYPTO_AUTH_SHARED,
78 	[NL80211_AUTHTYPE_NETWORK_EAP] = WLAN_CRYPTO_AUTH_8021X,
79 #if defined(WLAN_FEATURE_FILS_SK) && \
80 	(defined(CFG80211_FILS_SK_OFFLOAD_SUPPORT) || \
81 		 (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)))
82 	[NL80211_AUTHTYPE_FILS_SK] = WLAN_CRYPTO_AUTH_FILS_SK,
83 #endif
84 	[NL80211_AUTHTYPE_SAE] = WLAN_CRYPTO_AUTH_SAE,
85 };
86 
87 /* mapping table for akm type received from NL and crypto akm type */
88 static const struct osif_akm_type_crypto_mapping
89 	osif_akm_type_crypto_mapping[] = {
90 	{
91 		.akm_suite = WLAN_AKM_SUITE_8021X,
92 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X,
93 	},
94 	{
95 		.akm_suite = WLAN_AKM_SUITE_PSK,
96 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_PSK,
97 	},
98 	{
99 		.akm_suite = WLAN_AKM_SUITE_8021X_SHA256,
100 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X_SHA256,
101 	},
102 	{
103 		.akm_suite = WLAN_AKM_SUITE_PSK_SHA256,
104 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_PSK_SHA256,
105 	},
106 	{
107 		.akm_suite = WLAN_AKM_SUITE_SAE,
108 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_SAE,
109 	},
110 	{
111 		.akm_suite = WLAN_AKM_SUITE_FT_OVER_SAE,
112 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_SAE,
113 	},
114 #if defined(WLAN_AKM_SUITE_FT_8021X) || \
115 			defined(FEATURE_WLAN_FT_IEEE8021X)
116 	{
117 		.akm_suite = WLAN_AKM_SUITE_FT_8021X,
118 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_IEEE8021X,
119 	},
120 #endif
121 #if defined(WLAN_AKM_SUITE_FT_PSK) || \
122 			defined(FEATURE_WLAN_FT_PSK)
123 	{
124 		.akm_suite = WLAN_AKM_SUITE_FT_PSK,
125 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_PSK,
126 	},
127 #endif
128 #ifdef FEATURE_WLAN_ESE
129 	{
130 #ifndef WLAN_AKM_SUITE_CCKM
131 #define WLAN_AKM_SUITE_CCKM         0x00409600
132 #endif
133 		.akm_suite = WLAN_AKM_SUITE_CCKM,
134 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_CCKM,
135 	},
136 #endif
137 	{
138 #ifndef WLAN_AKM_SUITE_OSEN
139 #define WLAN_AKM_SUITE_OSEN         0x506f9a01
140 #endif
141 		.akm_suite = WLAN_AKM_SUITE_OSEN,
142 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_OSEN,
143 	},
144 #if defined(WLAN_AKM_SUITE_8021X_SUITE_B) || \
145 		defined(FEATURE_WLAN_IEEE8021X_SUITE_B)
146 	{
147 		.akm_suite = WLAN_AKM_SUITE_8021X_SUITE_B,
148 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X_SUITE_B,
149 	},
150 #endif
151 #if defined(WLAN_AKM_SUITE_8021X_SUITE_B_192) || \
152 		defined(FEATURE_WLAN_IEEE8021X_SUITE_B)
153 	{
154 		.akm_suite = WLAN_AKM_SUITE_8021X_SUITE_B_192,
155 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_IEEE8021X_SUITE_B_192,
156 	},
157 #endif
158 #if defined(WLAN_AKM_SUITE_FILS_SHA256) || \
159 				defined(FEATURE_WLAN_FILS)
160 	{
161 		.akm_suite = WLAN_AKM_SUITE_FILS_SHA256,
162 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FILS_SHA256,
163 	},
164 #endif
165 #if defined(WLAN_AKM_SUITE_FILS_SHA384) || \
166 				defined(FEATURE_WLAN_FILS)
167 	{
168 		.akm_suite = WLAN_AKM_SUITE_FILS_SHA384,
169 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FILS_SHA384,
170 	},
171 #endif
172 #if defined(WLAN_AKM_SUITE_FT_FILS_SHA256) || \
173 				defined(FEATURE_WLAN_FILS)
174 	{
175 		.akm_suite = WLAN_AKM_SUITE_FT_FILS_SHA256,
176 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_FILS_SHA256,
177 	},
178 #endif
179 #if defined(WLAN_AKM_SUITE_FT_FILS_SHA384) || \
180 				defined(FEATURE_WLAN_FILS)
181 	{
182 		.akm_suite = WLAN_AKM_SUITE_FT_FILS_SHA384,
183 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_FT_FILS_SHA384,
184 	},
185 #endif
186 	{
187 #ifndef WLAN_AKM_SUITE_OWE
188 #define WLAN_AKM_SUITE_OWE          0x000FAC12
189 #endif
190 		.akm_suite = WLAN_AKM_SUITE_OWE,
191 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_OWE,
192 	},
193 	{
194 #ifndef WLAN_AKM_SUITE_DPP
195 #define WLAN_AKM_SUITE_DPP      0x506f9a02
196 #endif
197 		.akm_suite = WLAN_AKM_SUITE_DPP,
198 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_DPP,
199 	},
200 	{
201 #ifndef WLAN_AKM_SUITE_SAE_EXT_KEY
202 #define WLAN_AKM_SUITE_SAE_EXT_KEY 0x000FAC18
203 #endif
204 		.akm_suite = WLAN_AKM_SUITE_SAE_EXT_KEY,
205 		.akm_type_crypto = WLAN_CRYPTO_KEY_MGMT_SAE_EXT_KEY,
206 	},
207 };
208 
209 /* mapping table for cipher type received from NL and crypto cipher type */
210 static const struct osif_cipher_crypto_mapping
211 	osif_cipher_crypto_mapping[] = {
212 	{
213 		.cipher_suite = IW_AUTH_CIPHER_NONE,
214 		.cipher_crypto = WLAN_CRYPTO_CIPHER_NONE,
215 		.cipher_len = 0,
216 	},
217 	{
218 		.cipher_suite = WLAN_CIPHER_SUITE_WEP40,
219 		.cipher_crypto = WLAN_CRYPTO_CIPHER_WEP_40,
220 		.cipher_len = WLAN_CRYPTO_KEY_WEP40_LEN,
221 	},
222 	{
223 		.cipher_suite = WLAN_CIPHER_SUITE_TKIP,
224 		.cipher_crypto = WLAN_CRYPTO_CIPHER_TKIP,
225 		.cipher_len = WLAN_CRYPTO_KEY_TKIP_LEN,
226 	},
227 	{
228 		.cipher_suite = WLAN_CIPHER_SUITE_CCMP,
229 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CCM,
230 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_LEN,
231 	},
232 	{
233 		.cipher_suite = WLAN_CIPHER_SUITE_WEP104,
234 		.cipher_crypto = WLAN_CRYPTO_CIPHER_WEP_104,
235 		.cipher_len = WLAN_CRYPTO_KEY_WEP104_LEN,
236 	},
237 	{
238 		.cipher_suite = WLAN_CIPHER_SUITE_GCMP,
239 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GCM,
240 		.cipher_len = WLAN_CRYPTO_KEY_GCMP_LEN,
241 	},
242 	{
243 		.cipher_suite = WLAN_CIPHER_SUITE_GCMP_256,
244 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GCM_256,
245 		.cipher_len = WLAN_CRYPTO_KEY_GCMP_256_LEN,
246 	},
247 	{
248 		.cipher_suite = WLAN_CIPHER_SUITE_CCMP_256,
249 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CCM_256,
250 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_256_LEN,
251 	},
252 	{
253 		.cipher_suite = WLAN_CIPHER_SUITE_AES_CMAC,
254 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CMAC,
255 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_LEN,
256 	},
257 #ifdef WLAN_CIPHER_SUITE_BIP_GMAC_128
258 	{
259 		.cipher_suite = WLAN_CIPHER_SUITE_BIP_GMAC_128,
260 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GMAC,
261 		.cipher_len = WLAN_CRYPTO_KEY_GMAC_LEN,
262 	},
263 #endif
264 #ifdef WLAN_CIPHER_SUITE_BIP_GMAC_256
265 	{
266 		.cipher_suite = WLAN_CIPHER_SUITE_BIP_GMAC_256,
267 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_GMAC_256,
268 		.cipher_len = WLAN_CRYPTO_KEY_GMAC_256_LEN,
269 	},
270 #endif
271 #ifdef WLAN_CIPHER_SUITE_BIP_CMAC_256
272 	{
273 		.cipher_suite = WLAN_CIPHER_SUITE_BIP_CMAC_256,
274 		.cipher_crypto = WLAN_CRYPTO_CIPHER_AES_CMAC_256,
275 		.cipher_len = WLAN_CRYPTO_KEY_CCMP_256_LEN,
276 	},
277 #endif
278 #ifdef FEATURE_WLAN_WAPI
279 	{
280 		.cipher_suite = WLAN_CIPHER_SUITE_SMS4,
281 		.cipher_crypto = WLAN_CRYPTO_CIPHER_WAPI_SMS4,
282 		.cipher_len = WLAN_CRYPTO_KEY_WAPI_LEN,
283 	},
284 #endif
285 };
286 
287 wlan_crypto_auth_mode
288 osif_nl_to_crypto_auth_type(enum nl80211_auth_type auth_type)
289 {
290 	wlan_crypto_auth_mode crypto_auth_type = WLAN_CRYPTO_AUTH_NONE;
291 
292 	if (auth_type < NL80211_AUTHTYPE_OPEN_SYSTEM ||
293 	    auth_type >= QDF_ARRAY_SIZE(osif_auth_type_crypto_mapping)) {
294 		QDF_TRACE_ERROR(QDF_MODULE_ID_OS_IF, "Unknown type: %d",
295 				auth_type);
296 		return crypto_auth_type;
297 	}
298 
299 	crypto_auth_type = osif_auth_type_crypto_mapping[auth_type];
300 	QDF_TRACE_DEBUG(QDF_MODULE_ID_OS_IF, "Auth type, NL: %d, crypto: %d",
301 			auth_type, crypto_auth_type);
302 
303 	return crypto_auth_type;
304 }
305 
306 wlan_crypto_key_mgmt osif_nl_to_crypto_akm_type(u32 key_mgmt)
307 {
308 	uint8_t index;
309 	wlan_crypto_key_mgmt crypto_akm_type = WLAN_CRYPTO_KEY_MGMT_NONE;
310 	bool akm_type_crypto_exist = false;
311 
312 	for (index = 0; index < QDF_ARRAY_SIZE(osif_akm_type_crypto_mapping);
313 	     index++) {
314 		if (osif_akm_type_crypto_mapping[index].akm_suite == key_mgmt) {
315 			crypto_akm_type = osif_akm_type_crypto_mapping[index].
316 							akm_type_crypto;
317 			akm_type_crypto_exist = true;
318 			break;
319 		}
320 	}
321 	if (!akm_type_crypto_exist)
322 		QDF_TRACE_ERROR(QDF_MODULE_ID_OS_IF, "Unknown type: %d",
323 				key_mgmt);
324 	else
325 		QDF_TRACE_DEBUG(QDF_MODULE_ID_OS_IF, "Akm suite, NL: %d, crypto: %d",
326 				key_mgmt, crypto_akm_type);
327 
328 	return crypto_akm_type;
329 }
330 
331 enum wlan_crypto_cipher_type osif_nl_to_crypto_cipher_type(u32 cipher)
332 {
333 	uint8_t index;
334 	bool cipher_crypto_exist = false;
335 	wlan_crypto_cipher_type crypto_cipher_type = WLAN_CRYPTO_CIPHER_NONE;
336 
337 	for (index = 0; index < QDF_ARRAY_SIZE(osif_cipher_crypto_mapping);
338 	     index++) {
339 		if (osif_cipher_crypto_mapping[index].cipher_suite == cipher) {
340 			crypto_cipher_type = osif_cipher_crypto_mapping[index].
341 								cipher_crypto;
342 			cipher_crypto_exist = true;
343 			break;
344 		}
345 	}
346 	if (!cipher_crypto_exist) {
347 		QDF_TRACE_ERROR(QDF_MODULE_ID_OS_IF, "Unknown type: %d",
348 				cipher);
349 		return WLAN_CRYPTO_CIPHER_INVALID;
350 	}
351 	QDF_TRACE_DEBUG(QDF_MODULE_ID_OS_IF, "Cipher suite, NL: %d, crypto: %d",
352 			cipher, crypto_cipher_type);
353 
354 	return crypto_cipher_type;
355 }
356 
357 int osif_nl_to_crypto_cipher_len(u32 cipher)
358 {
359 	uint8_t index;
360 
361 	for (index = 0; index < QDF_ARRAY_SIZE(osif_cipher_crypto_mapping);
362 	     index++) {
363 		if (osif_cipher_crypto_mapping[index].cipher_suite == cipher)
364 			return osif_cipher_crypto_mapping[index].cipher_len;
365 	}
366 
367 	return -EINVAL;
368 }
369 
370