1 /*
2  * Copyright (c) 2014-2021 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    FILE:         cds_utils.c
21 
22    OVERVIEW:     This source file contains definitions for CDS crypto APIs
23    The four APIs mentioned in this file are used for
24    initializing, and de-initializing a crypto context, and
25    obtaining truly random data (for keys), as well as
26    SHA1 HMAC, and AES encrypt and decrypt routines.
27 
28    The routines include:
29    cds_crypto_init() - Initializes Crypto module
30    cds_crypto_deinit() - De-initializes Crypto module
31    cds_rand_get_bytes() - Generates random byte
32 
33    DEPENDENCIES:
34    ============================================================================*/
35 
36 /*----------------------------------------------------------------------------
37  * Include Files
38  * -------------------------------------------------------------------------*/
39 
40 #include "qdf_trace.h"
41 #include "cds_utils.h"
42 #include "qdf_mem.h"
43 #include <linux/err.h>
44 #include <linux/random.h>
45 #include <linux/crypto.h>
46 #include <linux/scatterlist.h>
47 #include <linux/completion.h>
48 #include <linux/ieee80211.h>
49 #include <crypto/hash.h>
50 #include <crypto/aes.h>
51 
52 #include "cds_ieee80211_common.h"
53 #include <qdf_crypto.h>
54 
55 /*----------------------------------------------------------------------------
56  * Preprocessor Definitions and Constants
57  * -------------------------------------------------------------------------*/
58 
59 /*----------------------------------------------------------------------------
60  * Type Declarations
61  * -------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
63  * Global Data Definitions
64  * -------------------------------------------------------------------------*/
65 /*----------------------------------------------------------------------------
66  * Static Variable Definitions
67  * -------------------------------------------------------------------------*/
68 
69 /*----------------------------------------------------------------------------
70    Function Definitions and Documentation
71  * -------------------------------------------------------------------------*/
72 
cds_get_mmie_size(void)73 uint8_t cds_get_mmie_size(void)
74 {
75 	return sizeof(struct ieee80211_mmie);
76 }
77 
78 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
cds_get_gmac_mmie_size(void)79 uint8_t cds_get_gmac_mmie_size(void)
80 {
81 	return sizeof(struct ieee80211_mmie_16);
82 }
83 #else
cds_get_gmac_mmie_size(void)84 uint8_t cds_get_gmac_mmie_size(void)
85 {
86 	return 0;
87 }
88 #endif
89 
cds_chan_to_freq(uint8_t chan)90 uint32_t cds_chan_to_freq(uint8_t chan)
91 {
92 	if (chan < CDS_24_GHZ_CHANNEL_14)       /* ch 0 - ch 13 */
93 		return CDS_24_GHZ_BASE_FREQ + chan * CDS_CHAN_SPACING_5MHZ;
94 	else if (chan == CDS_24_GHZ_CHANNEL_14) /* ch 14 */
95 		return CDS_CHAN_14_FREQ;
96 	else if (chan < CDS_24_GHZ_CHANNEL_27)  /* ch 15 - ch 26 */
97 		return CDS_CHAN_15_FREQ +
98 		       (chan - CDS_24_GHZ_CHANNEL_15) * CDS_CHAN_SPACING_20MHZ;
99 	else if (chan == CDS_5_GHZ_CHANNEL_170)
100 		return CDS_CHAN_170_FREQ;
101 	else
102 		return CDS_5_GHZ_BASE_FREQ + chan * CDS_CHAN_SPACING_5MHZ;
103 }
104 
cds_freq_to_chan(uint32_t freq)105 uint8_t cds_freq_to_chan(uint32_t freq)
106 {
107 	uint8_t chan;
108 
109 	if (freq > CDS_24_GHZ_BASE_FREQ && freq < CDS_CHAN_14_FREQ)
110 		chan = ((freq - CDS_24_GHZ_BASE_FREQ) / CDS_CHAN_SPACING_5MHZ);
111 	else if (freq == CDS_CHAN_14_FREQ)
112 		chan = CDS_24_GHZ_CHANNEL_14;
113 	else if ((freq > CDS_24_GHZ_BASE_FREQ) && (freq < CDS_5_GHZ_BASE_FREQ))
114 		chan = (((freq - CDS_CHAN_15_FREQ) / CDS_CHAN_SPACING_20MHZ) +
115 			CDS_24_GHZ_CHANNEL_15);
116 	else
117 		chan = (freq - CDS_5_GHZ_BASE_FREQ) / CDS_CHAN_SPACING_5MHZ;
118 	return chan;
119 }
120 
cds_chan_to_band(uint32_t chan)121 enum cds_band_type cds_chan_to_band(uint32_t chan)
122 {
123 	if (chan <= CDS_24_GHZ_CHANNEL_14)
124 		return CDS_BAND_2GHZ;
125 
126 	return CDS_BAND_5GHZ;
127 }
128 
cds_copy_hlp_info(struct qdf_mac_addr * input_dst_mac,struct qdf_mac_addr * input_src_mac,uint16_t input_hlp_data_len,uint8_t * input_hlp_data,struct qdf_mac_addr * output_dst_mac,struct qdf_mac_addr * output_src_mac,uint16_t * output_hlp_data_len,uint8_t * output_hlp_data)129 void cds_copy_hlp_info(struct qdf_mac_addr *input_dst_mac,
130 		       struct qdf_mac_addr *input_src_mac,
131 		       uint16_t input_hlp_data_len,
132 		       uint8_t *input_hlp_data,
133 		       struct qdf_mac_addr *output_dst_mac,
134 		       struct qdf_mac_addr *output_src_mac,
135 		       uint16_t *output_hlp_data_len,
136 		       uint8_t *output_hlp_data)
137 {
138 	if (!input_hlp_data_len) {
139 		cds_debug("Input HLP data len zero\n");
140 		return;
141 	}
142 
143 	if (!input_dst_mac) {
144 		cds_debug("HLP destination mac NULL");
145 		return;
146 	}
147 
148 	if (!input_src_mac) {
149 		cds_debug("HLP source mac NULL");
150 		return;
151 	}
152 	qdf_copy_macaddr(output_dst_mac, input_dst_mac);
153 	qdf_copy_macaddr(output_src_mac, input_src_mac);
154 	*output_hlp_data_len = input_hlp_data_len;
155 	qdf_mem_copy(output_hlp_data, input_hlp_data, input_hlp_data_len);
156 }
157