xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_crypto.h (revision 901120c066e139c7f8a2c8e4820561fdd83c67ef)
1 /*
2  * Copyright (c) 2017-2018, 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: 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  * @type: crypto type
125  * @key: key needs to be used for hmac api
126  * @keylen: length of key
127  * @element_cnt: scatterlist element count
128  * @addr: scatterlist element array
129  * @addr_len: element length array
130  * @hash: new 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  * qdf_update_dbl: This API does the doubling operation as defined in RFC5297
139  * @d: input for doubling
140  *
141  * Return: None
142  */
143 void qdf_update_dbl(uint8_t *d);
144 
145 /**
146  * qdf_aes_s2v: This API gets vector from AES string as defined in RFC5297
147  * output length will be AES_BLOCK_SIZE.
148  * @key: key used for operation
149  * @key_len: key len
150  * @s: addresses of elements to be used
151  * @s_len: array of element length
152  * @num_s: number of elements
153  * @out: pointer to output vector
154  *
155  * Return: 0 if success else Error number
156  */
157 int qdf_aes_s2v(const uint8_t *key, unsigned int key_len, const uint8_t *s[],
158 		   size_t s_len[], size_t num_s, uint8_t *out);
159 
160 /**
161  * qdf_aes_ctr: This API defines AES Counter Mode
162  * @key: key used for operation
163  * @key_len: key len
164  * @siv: Initialization vector
165  * @src: input
166  * @src_len: input len
167  * @dest: output
168  * @enc: if encryption needs to be done or decryption
169  *
170  * Return: 0 if success else Error number
171  */
172 int qdf_aes_ctr(const uint8_t *key, unsigned int key_len, uint8_t *siv,
173 		const uint8_t *src, size_t src_len, uint8_t *dest, bool enc);
174 
175 /**
176  * qdf_crypto_aes_gmac: This API calculates MIC for GMAC
177  * @key: key used for operation
178  * @key_length: key length
179  * @iv: Initialization vector
180  * @aad: Additional authentication data
181  * @data: Pointer to data
182  * @data_len: Length of data
183  * @mic: Pointer to MIC
184  *
185  * Return: 0 if success else Error number
186  */
187 int qdf_crypto_aes_gmac(const uint8_t *key, uint16_t key_length,
188 			uint8_t *iv, const uint8_t *aad,
189 			const uint8_t *data, uint16_t data_len, uint8_t *mic);
190 
191 /**
192  * qdf_crypto_aes_128_cmac: This API calculates MIC for AES 128 CMAC
193  * @key: key used for operation
194  * @data: Pointer to data
195  * @len: Length of data
196  * @mic: Pointer to MIC
197  *
198  * Return: 0 if success else Error number
199  */
200 int qdf_crypto_aes_128_cmac(const uint8_t *key, const uint8_t *data,
201 			    uint16_t len, uint8_t *mic);
202 
203 #ifdef __cplusplus
204 }
205 #endif /* __cplusplus */
206 #endif /* __QDF_CRYPTO_H */
207