xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_crypto.h (revision 6d768494e5ce14eb1603a695c86739d12ecc6ec2)
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 /* Function declarations and documenation */
58 
59 /**
60  * qdf_get_hash: API to get hash using specific crypto and scatterlist
61  * @type: crypto type
62  * @element_cnt: scatterlist element count
63  * @addr: scatterlist element array
64  * @addr_len: element length array
65  * @hash: new hash
66  *
67  * Return: 0 if success else error code
68  */
69 int qdf_get_hash(uint8_t *type, uint8_t element_cnt,
70 		uint8_t *addr[], uint32_t *addr_len,
71 		int8_t *hash);
72 
73 /**
74  * qdf_get_hmac_hash: API to get hmac hash using specific crypto and
75  * scatterlist elements.
76  * @type: crypto type
77  * @key: key needs to be used for hmac api
78  * @keylen: length of key
79  * @element_cnt: scatterlist element count
80  * @addr: scatterlist element array
81  * @addr_len: element length array
82  * @hash: new hash
83  *
84  * Return: 0 if success else error code
85  */
86 int qdf_get_hmac_hash(uint8_t *type, uint8_t *key,
87 		uint32_t keylen, uint8_t element_cnt,
88 		uint8_t *addr[], uint32_t *addr_len, int8_t *hash);
89 
90 /**
91  * qdf_get_keyed_hash: API to get hash using specific crypto and
92  * scatterlist elements.
93  * @type: crypto type
94  * @key: key needs to be used for hmac api
95  * @keylen: length of key
96  * @element_cnt: scatterlist element count
97  * @addr: scatterlist element array
98  * @addr_len: element length array
99  * @hash: new hash
100  *
101  * Return: 0 if success else error code
102  */
103 int qdf_get_keyed_hash(const char *alg, const uint8_t *key,
104 			unsigned int key_len, const uint8_t *src[],
105 			size_t *src_len, size_t num_elements, uint8_t *out);
106 /**
107  * qdf_update_dbl: This API does the doubling operation as defined in RFC5297
108  * @d: input for doubling
109  *
110  * Return: None
111  */
112 void qdf_update_dbl(uint8_t *d);
113 
114 /**
115  * qdf_aes_s2v: This API gets vector from AES string as defined in RFC5297
116  * output length will be AES_BLOCK_SIZE.
117  * @key: key used for operation
118  * @key_len: key len
119  * @s: addresses of elements to be used
120  * @s_len: array of element length
121  * @num_s: number of elements
122  * @out: pointer to output vector
123  *
124  * Return: 0 if success else Error number
125  */
126 int qdf_aes_s2v(const uint8_t *key, unsigned int key_len, const uint8_t *s[],
127 		   size_t s_len[], size_t num_s, uint8_t *out);
128 
129 /**
130  * qdf_aes_ctr: This API defines AES Counter Mode
131  * @key: key used for operation
132  * @key_len: key len
133  * @siv: Initialization vector
134  * @src: input
135  * @src_len: input len
136  * @dest: output
137  * @enc: if encryption needs to be done or decryption
138  *
139  * Return: 0 if success else Error number
140  */
141 int qdf_aes_ctr(const uint8_t *key, unsigned int key_len, uint8_t *siv,
142 		const uint8_t *src, size_t src_len, uint8_t *dest, bool enc);
143 
144 /**
145  * qdf_crypto_aes_gmac: This API calculates MIC for GMAC
146  * @key: key used for operation
147  * @key_length: key length
148  * @iv: Initialization vector
149  * @aad: Additional authentication data
150  * @data: Pointer to data
151  * @data_len: Length of data
152  * @mic: Pointer to MIC
153  *
154  * Return: 0 if success else Error number
155  */
156 int qdf_crypto_aes_gmac(const uint8_t *key, uint16_t key_length,
157 			uint8_t *iv, const uint8_t *aad,
158 			const uint8_t *data, uint16_t data_len, uint8_t *mic);
159 
160 /**
161  * qdf_crypto_aes_128_cmac: This API calculates MIC for AES 128 CMAC
162  * @key: key used for operation
163  * @data: Pointer to data
164  * @len: Length of data
165  * @mic: Pointer to MIC
166  *
167  * Return: 0 if success else Error number
168  */
169 int qdf_crypto_aes_128_cmac(const uint8_t *key, const uint8_t *data,
170 			    uint16_t len, uint8_t *mic);
171 
172 #ifdef __cplusplus
173 }
174 #endif /* __cplusplus */
175 #endif /* __QDF_CRYPTO_H */
176