1 /*
2  * Copyright (c) 2017-2018, 2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 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: qdf_crypto.h
22  * This file provides OS abstraction for crypto APIs.
23  */
24 
25 #if !defined(__QDF_CRYPTO_H)
26 #define __QDF_CRYPTO_H
27 
28 /* Include Files */
29 #include "qdf_status.h"
30 #include <qdf_types.h>
31 #include <qdf_trace.h>
32 
33 /* Preprocessor definitions and constants */
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37 
38 #define AES_BLOCK_SIZE 16
39 #define HMAC_SHA256_CRYPTO_TYPE "hmac(sha256)"
40 #define HMAC_SHA386_CRYPTO_TYPE "hmac(sha384)"
41 
42 #define SHA256_CRYPTO_TYPE "sha256"
43 #define SHA386_CRYPTO_TYPE "sha384"
44 
45 #define SHA256_DIGEST_SIZE 32
46 #define SHA384_DIGEST_SIZE 48
47 
48 #define FIXED_PARAM_OFFSET_ASSOC_REQ 4
49 #define FIXED_PARAM_OFFSET_ASSOC_RSP 6
50 
51 #define CMAC_TLEN 8             /* CMAC TLen = 64 bits (8 octets) */
52 #define AAD_LEN 20
53 #define IEEE80211_MMIE_GMAC_MICLEN  16
54 
55 #define IS_VALID_CTR_KEY_LEN(len) ((((len) == 16) || ((len) == 32) || \
56 	((len) == 48)) ? 1 : 0)
57 
58 #define WLAN_MAX_PRF_INTERATIONS_COUNT 255
59 
60 /* Function declarations and documentation */
61 
62 /**
63  * qdf_get_hash: API to get hash using specific crypto and scatterlist
64  * @type: crypto type
65  * @element_cnt: scatterlist element count
66  * @addr: scatterlist element array
67  * @addr_len: element length array
68  * @hash: new hash
69  *
70  * Return: 0 if success else error code
71  */
72 int qdf_get_hash(uint8_t *type, uint8_t element_cnt,
73 		uint8_t *addr[], uint32_t *addr_len,
74 		int8_t *hash);
75 
76 /**
77  * qdf_get_hmac_hash: API to get hmac hash using specific crypto and
78  * scatterlist elements.
79  * @type: crypto type
80  * @key: key needs to be used for hmac api
81  * @keylen: length of key
82  * @element_cnt: scatterlist element count
83  * @addr: scatterlist element array
84  * @addr_len: element length array
85  * @hash: new hash
86  *
87  * Return: 0 if success else error code
88  */
89 int qdf_get_hmac_hash(uint8_t *type, uint8_t *key,
90 		uint32_t keylen, uint8_t element_cnt,
91 		uint8_t *addr[], uint32_t *addr_len, int8_t *hash);
92 
93 /**
94  * qdf_default_hmac_sha256_kdf()- This API calculates key data using default kdf
95  * defined in RFC4306.
96  * @secret: key which needs to be used in crypto
97  * @secret_len: key_len of secret
98  * @label: PRF label
99  * @optional_data: Data used for hash
100  * @optional_data_len: data length
101  * @key: key data output
102  * @keylen: key data length
103  *
104  * This API creates default KDF as defined in RFC4306
105  * PRF+ (K,S) = T1 | T2 | T3 | T4 | ...
106  * T1 = PRF (K, S | 0x01)
107  * T2 = PRF (K, T1 | S | 0x02)
108  * T3 = PRF (K, T2 | S | 0x03)
109  * T4 = PRF (K, T3 | S | 0x04)
110  *
111  * for every iteration its creates 32 bit of hash
112  *
113  * Return: QDF_STATUS
114  */
115 QDF_STATUS
116 qdf_default_hmac_sha256_kdf(uint8_t *secret, uint32_t secret_len,
117 			    uint8_t *label, uint8_t *optional_data,
118 			    uint32_t optional_data_len, uint8_t *key,
119 			    uint32_t keylen);
120 
121 /**
122  * qdf_get_keyed_hash: API to get hash using specific crypto and
123  * scatterlist elements.
124  * @alg: crypto type
125  * @key: key needs to be used for hmac api
126  * @key_len: length of key
127  * @src: scatterlist element array
128  * @src_len: scatterlist element length array
129  * @num_elements: scatterlist element count
130  * @out: calculated hash
131  *
132  * Return: 0 if success else error code
133  */
134 int qdf_get_keyed_hash(const char *alg, const uint8_t *key,
135 			unsigned int key_len, const uint8_t *src[],
136 			size_t *src_len, size_t num_elements, uint8_t *out);
137 
138 /**
139  * qdf_update_dbl: This API does the doubling operation as defined in RFC5297
140  * @d: input for doubling
141  *
142  * Return: None
143  */
144 void qdf_update_dbl(uint8_t *d);
145 
146 /**
147  * qdf_aes_s2v: This API gets vector from AES string as defined in RFC5297
148  * output length will be AES_BLOCK_SIZE.
149  * @key: key used for operation
150  * @key_len: key len
151  * @s: addresses of elements to be used
152  * @s_len: array of element length
153  * @num_s: number of elements
154  * @out: pointer to output vector
155  *
156  * Return: 0 if success else Error number
157  */
158 int qdf_aes_s2v(const uint8_t *key, unsigned int key_len, const uint8_t *s[],
159 		   size_t s_len[], size_t num_s, uint8_t *out);
160 
161 /**
162  * qdf_aes_ctr: This API defines AES Counter Mode
163  * @key: key used for operation
164  * @key_len: key len
165  * @siv: Initialization vector
166  * @src: input
167  * @src_len: input len
168  * @dest: output
169  * @enc: if encryption needs to be done or decryption
170  *
171  * Return: 0 if success else Error number
172  */
173 int qdf_aes_ctr(const uint8_t *key, unsigned int key_len, uint8_t *siv,
174 		const uint8_t *src, size_t src_len, uint8_t *dest, bool enc);
175 
176 /**
177  * qdf_crypto_aes_gmac: This API calculates MIC for GMAC
178  * @key: key used for operation
179  * @key_length: key length
180  * @iv: Initialization vector
181  * @aad: Additional authentication data
182  * @data: Pointer to data
183  * @data_len: Length of data
184  * @mic: Pointer to MIC
185  *
186  * Return: 0 if success else Error number
187  */
188 int qdf_crypto_aes_gmac(const uint8_t *key, uint16_t key_length,
189 			uint8_t *iv, const uint8_t *aad,
190 			const uint8_t *data, uint16_t data_len, uint8_t *mic);
191 
192 /**
193  * qdf_crypto_aes_128_cmac: This API calculates MIC for AES 128 CMAC
194  * @key: key used for operation
195  * @data: Pointer to data
196  * @len: Length of data
197  * @mic: Pointer to MIC
198  *
199  * Return: 0 if success else Error number
200  */
201 int qdf_crypto_aes_128_cmac(const uint8_t *key, const uint8_t *data,
202 			    uint16_t len, uint8_t *mic);
203 
204 #ifdef __cplusplus
205 }
206 #endif /* __cplusplus */
207 #endif /* __QDF_CRYPTO_H */
208