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