xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/crypto/src/wlan_cfg80211_crypto.c (revision 8ddef7dd9a290d4a9b1efd5d3efacf51d78a1a0d)
1 /*
2  * Copyright (c) 2019 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: defines crypto driver functions interfacing with linux kernel
21  */
22 #include <wlan_crypto_global_def.h>
23 #include <wlan_crypto_global_api.h>
24 #include <wlan_objmgr_vdev_obj.h>
25 #include <wlan_crypto_main_i.h>
26 #include <net/cfg80211.h>
27 #include <wlan_nl_to_crypto_params.h>
28 #include "wlan_cfg80211_crypto.h"
29 #include <wlan_cfg80211.h>
30 
31 static void wlan_cfg80211_translate_key(struct wlan_objmgr_vdev *vdev,
32 					uint8_t key_index,
33 					enum wlan_crypto_key_type key_type,
34 					const u8 *mac_addr,
35 					struct key_params *params,
36 					struct wlan_crypto_key *crypto_key)
37 {
38 	qdf_mem_zero(crypto_key, sizeof(*crypto_key));
39 	crypto_key->keylen = params->key_len;
40 	crypto_key->keyix = key_index;
41 	qdf_mem_copy(&crypto_key->keyval[0], params->key, params->key_len);
42 	qdf_mem_copy(&crypto_key->keyrsc[0], params->seq, params->seq_len);
43 
44 	crypto_key->cipher_type = osif_nl_to_crypto_cipher_type(params->cipher);
45 	if (key_type == WLAN_CRYPTO_KEY_TYPE_UNICAST) {
46 		qdf_mem_copy(&crypto_key->macaddr, mac_addr, QDF_MAC_ADDR_SIZE);
47 	} else {
48 		if ((vdev->vdev_mlme.vdev_opmode == QDF_STA_MODE) ||
49 		    (vdev->vdev_mlme.vdev_opmode == QDF_P2P_CLIENT_MODE))
50 			qdf_mem_copy(&crypto_key->macaddr, mac_addr,
51 				     QDF_MAC_ADDR_SIZE);
52 		else
53 			qdf_mem_copy(&crypto_key->macaddr,
54 				     vdev->vdev_mlme.macaddr,
55 				     QDF_MAC_ADDR_SIZE);
56 	}
57 	cfg80211_debug("key_type %d, opmode %d, mac %pM", key_type,
58 		       vdev->vdev_mlme.vdev_opmode, crypto_key->macaddr);
59 }
60 
61 int wlan_cfg80211_store_key(struct wlan_objmgr_vdev *vdev,
62 			    uint8_t key_index,
63 			    enum wlan_crypto_key_type key_type,
64 			    const u8 *mac_addr, struct key_params *params)
65 {
66 	struct wlan_crypto_key *crypto_key = NULL;
67 	enum wlan_crypto_cipher_type cipher;
68 	int cipher_len;
69 	QDF_STATUS status;
70 
71 	if (!vdev) {
72 		cfg80211_err("vdev is NULL");
73 		return -EINVAL;
74 	}
75 	if (!params) {
76 		cfg80211_err("Key params is NULL");
77 		return -EINVAL;
78 	}
79 	if ((key_type == WLAN_CRYPTO_KEY_TYPE_UNICAST) && !mac_addr) {
80 		cfg80211_err("mac_addr is NULL for pairwise Key");
81 		return -EINVAL;
82 	}
83 	cipher_len = osif_nl_to_crypto_cipher_len(params->cipher);
84 	if (cipher_len < 0 || params->key_len < cipher_len) {
85 		cfg80211_err("cipher length %d less than reqd len %d",
86 			     params->key_len, cipher_len);
87 		return -EINVAL;
88 	}
89 	cipher = osif_nl_to_crypto_cipher_type(params->cipher);
90 	status = wlan_crypto_validate_key_params(cipher, key_index,
91 						 params->key_len,
92 						 params->seq_len);
93 	if (QDF_IS_STATUS_ERROR(status)) {
94 		cfg80211_err("Invalid key params");
95 		return -EINVAL;
96 	}
97 
98 	/*
99 	 * key may already exist at times and may be retrieved only to
100 	 * update it.
101 	 */
102 	crypto_key = wlan_crypto_get_key(vdev, key_index);
103 	if (!crypto_key) {
104 		crypto_key = qdf_mem_malloc(sizeof(*crypto_key));
105 		if (!crypto_key)
106 			return -EINVAL;
107 		status = wlan_crypto_save_key(vdev, key_index, crypto_key);
108 		if (QDF_IS_STATUS_ERROR(status)) {
109 			cfg80211_err("Failed to save key");
110 			qdf_mem_free(crypto_key);
111 			return -EINVAL;
112 		}
113 	}
114 
115 	wlan_cfg80211_translate_key(vdev, key_index, key_type, mac_addr,
116 				    params, crypto_key);
117 
118 	return 0;
119 }
120 
121 int wlan_cfg80211_crypto_add_key(struct wlan_objmgr_vdev *vdev,
122 				 enum wlan_crypto_key_type key_type,
123 				 uint8_t key_index)
124 {
125 	struct wlan_crypto_key *crypto_key;
126 	QDF_STATUS status;
127 
128 	crypto_key = wlan_crypto_get_key(vdev, key_index);
129 	if (!crypto_key) {
130 		cfg80211_err("Crypto KEY is NULL");
131 		return -EINVAL;
132 	}
133 	status  = ucfg_crypto_set_key_req(vdev, crypto_key, key_type);
134 
135 	return qdf_status_to_os_return(status);
136 }
137 
138 #ifdef CONFIG_CRYPTO_COMPONENT
139 int wlan_cfg80211_set_default_key(struct wlan_objmgr_vdev *vdev,
140 				  uint8_t key_index, struct qdf_mac_addr *bssid)
141 {
142 	return wlan_crypto_default_key(vdev, (uint8_t *)bssid,
143 				       key_index, true);
144 }
145 #endif
146