xref: /wlan-dirver/qca-wifi-host-cmn/target_if/crypto/src/target_if_crypto.c (revision f28396d060cff5c6519f883cb28ae0116ce479f1)
1 /*
2  * Copyright (c) 2019-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: offload lmac interface APIs definitions for crypto
21  */
22 
23 #include <qdf_mem.h>
24 #include <qdf_status.h>
25 #include <target_if_crypto.h>
26 #include <wmi_unified_priv.h>
27 #include <wmi_unified_param.h>
28 #include <wlan_objmgr_psoc_obj.h>
29 #include <target_if.h>
30 #include <wlan_crypto_global_def.h>
31 #include <wlan_crypto_global_api.h>
32 #include <wlan_objmgr_vdev_obj.h>
33 #include <cdp_txrx_cmn_struct.h>
34 #include <cds_api.h>
35 #include <cdp_txrx_cmn.h>
36 #include <wmi_unified_api.h>
37 #include <wmi_unified_crypto_api.h>
38 #include <cdp_txrx_peer_ops.h>
39 
40 #ifdef FEATURE_WLAN_WAPI
41 static void wlan_crypto_set_wapi_key(struct wlan_objmgr_vdev *vdev,
42 				     bool pairwise,
43 				     enum wlan_crypto_cipher_type cipher_type,
44 				     struct set_key_params *params)
45 {
46 	static const unsigned char tx_iv[16] = {0x36, 0x5c, 0x36, 0x5c, 0x36,
47 						0x5c, 0x36, 0x5c, 0x36, 0x5c,
48 						0x36, 0x5c, 0x36, 0x5c, 0x36,
49 						0x5c};
50 
51 	static const unsigned char rx_iv[16] = {0x5c, 0x36, 0x5c, 0x36, 0x5c,
52 						0x36, 0x5c, 0x36, 0x5c, 0x36,
53 						0x5c, 0x36, 0x5c, 0x36, 0x5c,
54 						0x37};
55 
56 	if (cipher_type != WLAN_CRYPTO_CIPHER_WAPI_SMS4 ||
57 	    cipher_type != WLAN_CRYPTO_CIPHER_WAPI_GCM4)
58 		return;
59 
60 	qdf_mem_copy(&params->rx_iv, &rx_iv,
61 		     WLAN_CRYPTO_WAPI_IV_SIZE);
62 	qdf_mem_copy(&params->tx_iv, &tx_iv,
63 		     WLAN_CRYPTO_WAPI_IV_SIZE);
64 
65 	if (vdev->vdev_mlme.vdev_opmode == QDF_SAP_MODE) {
66 		if (pairwise)
67 			params->tx_iv[0] = 0x37;
68 
69 		params->rx_iv[WLAN_CRYPTO_WAPI_IV_SIZE - 1] = 0x36;
70 	} else {
71 		if (!pairwise)
72 			params->rx_iv[WLAN_CRYPTO_WAPI_IV_SIZE - 1] = 0x36;
73 	}
74 
75 	params->key_txmic_len = WLAN_CRYPTO_MIC_LEN;
76 	params->key_rxmic_len = WLAN_CRYPTO_MIC_LEN;
77 }
78 #else
79 static inline void wlan_crypto_set_wapi_key(struct wlan_objmgr_vdev *vdev,
80 					    bool pairwise,
81 					    enum wlan_crypto_cipher_type cipher,
82 					    struct set_key_params *params)
83 {
84 }
85 #endif /* FEATURE_WLAN_WAPI */
86 
87 #ifdef BIG_ENDIAN_HOST
88 static void wlan_crypto_endianness_conversion(uint8_t *dest, uint8_t *src,
89 					      uint32_t keylen)
90 {
91 	int8_t i;
92 
93 	for (i = 0; i < roundup(keylen, sizeof(uint32_t)) / 4; i++) {
94 		*dest = le32_to_cpu(*src);
95 		dest++;
96 		src++;
97 	}
98 }
99 #else
100 static void wlan_crypto_endianness_conversion(uint8_t *dest, uint8_t *src,
101 					      uint32_t keylen)
102 {
103 	qdf_mem_copy(dest, src, keylen);
104 }
105 #endif
106 
107 QDF_STATUS target_if_crypto_set_key(struct wlan_objmgr_vdev *vdev,
108 				    struct wlan_crypto_key *req,
109 				    enum wlan_crypto_key_type key_type)
110 {
111 	struct set_key_params params = {0};
112 	struct wlan_objmgr_psoc *psoc;
113 	struct wlan_objmgr_pdev *pdev;
114 	enum cdp_sec_type sec_type = cdp_sec_type_none;
115 	void *soc = cds_get_context(QDF_MODULE_ID_SOC);
116 	uint32_t pn[4] = {0, 0, 0, 0};
117 	bool peer_exist = false;
118 	uint8_t def_tx_idx;
119 	wmi_unified_t pdev_wmi_handle;
120 	bool pairwise;
121 	QDF_STATUS status;
122 
123 	pdev = wlan_vdev_get_pdev(vdev);
124 	if (!pdev) {
125 		target_if_err("Invalid PDEV");
126 		return QDF_STATUS_E_FAILURE;
127 	}
128 	psoc = wlan_vdev_get_psoc(vdev);
129 	if (!psoc) {
130 		target_if_err("Invalid PSOC");
131 		return QDF_STATUS_E_FAILURE;
132 	}
133 	soc = wlan_psoc_get_dp_handle(psoc);
134 	if (!soc) {
135 		target_if_err("Invalid DP Handle");
136 		return QDF_STATUS_E_FAILURE;
137 	}
138 	params.vdev_id = wlan_vdev_get_id(vdev);
139 	params.key_idx = req->keyix;
140 	qdf_mem_copy(params.peer_mac, req->macaddr, QDF_MAC_ADDR_SIZE);
141 	pdev_wmi_handle = GET_WMI_HDL_FROM_PDEV(pdev);
142 	if (!pdev_wmi_handle) {
143 		target_if_err("Invalid PDEV WMI handle");
144 		return QDF_STATUS_E_FAILURE;
145 	}
146 
147 	params.key_flags = req->flags;
148 	if (key_type != WLAN_CRYPTO_KEY_TYPE_UNICAST) {
149 		pairwise = false;
150 		params.key_flags |= GROUP_USAGE;
151 
152 	} else {
153 		pairwise = true;
154 		params.key_flags |= PAIRWISE_USAGE;
155 	}
156 	qdf_mem_copy(&params.key_rsc_ctr,
157 		     &req->keyrsc[0], sizeof(uint64_t));
158 
159 	peer_exist = cdp_find_peer_exist(soc, pdev->pdev_objmgr.wlan_pdev_id,
160 					 req->macaddr);
161 	target_if_debug("key_type %d, mac: %02x:%02x:%02x:%02x:%02x:%02x",
162 			key_type, req->macaddr[0], req->macaddr[1],
163 			req->macaddr[2], req->macaddr[3], req->macaddr[4],
164 			req->macaddr[5]);
165 
166 	if ((key_type == WLAN_CRYPTO_KEY_TYPE_UNICAST) && !peer_exist) {
167 		target_if_err("Invalid peer");
168 		return QDF_STATUS_E_FAILURE;
169 	}
170 
171 	params.key_cipher = wlan_crypto_cipher_to_wmi_cipher(req->cipher_type);
172 	sec_type = wlan_crypto_cipher_to_cdp_sec_type(req->cipher_type);
173 	wlan_crypto_set_wapi_key(vdev, pairwise, req->cipher_type, &params);
174 
175 	switch (req->cipher_type) {
176 	case WLAN_CRYPTO_CIPHER_WEP:
177 	case WLAN_CRYPTO_CIPHER_WEP_40:
178 	case WLAN_CRYPTO_CIPHER_WEP_104:
179 		def_tx_idx = wlan_crypto_get_default_key_idx(vdev, false);
180 		if (pairwise && params.key_idx == def_tx_idx)
181 			params.key_flags |= TX_USAGE;
182 		else if ((vdev->vdev_mlme.vdev_opmode == QDF_SAP_MODE) &&
183 			 (params.key_idx == def_tx_idx))
184 			params.key_flags |= TX_USAGE;
185 		break;
186 	case WLAN_CRYPTO_CIPHER_TKIP:
187 		params.key_txmic_len = WLAN_CRYPTO_MIC_LEN;
188 		params.key_rxmic_len = WLAN_CRYPTO_MIC_LEN;
189 		break;
190 	default:
191 		break;
192 	}
193 
194 	wlan_crypto_endianness_conversion(&params.key_data[0],
195 					  &req->keyval[0],
196 					  req->keylen);
197 	params.key_len = req->keylen;
198 
199 	/* Set PN check & security type in data path */
200 	qdf_mem_copy(&pn[0], &params.key_rsc_ctr, sizeof(pn));
201 	cdp_set_pn_check(soc, vdev->vdev_objmgr.vdev_id, req->macaddr,
202 			 sec_type, pn);
203 	cdp_set_key(soc, vdev->vdev_objmgr.vdev_id, req->macaddr, pairwise,
204 		    (uint32_t *)(req->keyval + WLAN_CRYPTO_IV_SIZE +
205 		     WLAN_CRYPTO_MIC_LEN));
206 
207 	target_if_debug("vdev_id:%d, key: idx:%d,len:%d", params.vdev_id,
208 			params.key_idx, params.key_len);
209 	target_if_debug("peer mac %pM", params.peer_mac);
210 	QDF_TRACE_HEX_DUMP(QDF_MODULE_ID_CRYPTO, QDF_TRACE_LEVEL_DEBUG,
211 			   &params.key_rsc_ctr, sizeof(uint64_t));
212 	status = wmi_unified_setup_install_key_cmd(pdev_wmi_handle, &params);
213 
214 	/* Zero-out local key variables */
215 	qdf_mem_zero(&params, sizeof(struct set_key_params));
216 	return status;
217 }
218 
219 QDF_STATUS target_if_crypto_register_tx_ops(struct wlan_lmac_if_tx_ops *tx_ops)
220 {
221 	struct wlan_lmac_if_crypto_tx_ops *crypto;
222 
223 	if (!tx_ops) {
224 		target_if_err("txops NULL");
225 		return QDF_STATUS_E_FAILURE;
226 	}
227 	crypto = &tx_ops->crypto_tx_ops;
228 
229 	crypto->set_key = target_if_crypto_set_key;
230 
231 	return QDF_STATUS_SUCCESS;
232 }
233 
234