xref: /freebsd/sys/dev/qat/qat_api/common/crypto/sym/include/lac_sym_hash_defs.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 
4 /**
5  ***************************************************************************
6  * @file lac_sym_hash_defs.h
7  *
8  * @defgroup LacHashDefs Hash Definitions
9  *
10  * @ingroup  LacHash
11  *
12  * Constants for hash algorithms
13  *
14  ***************************************************************************/
15 
16 #ifndef LAC_SYM_HASH_DEFS_H
17 #define LAC_SYM_HASH_DEFS_H
18 
19 /* Constant for MD5 algorithm  */
20 #define LAC_HASH_MD5_BLOCK_SIZE 64
21 /**< @ingroup LacHashDefs
22  * MD5 block size in bytes */
23 #define LAC_HASH_MD5_DIGEST_SIZE 16
24 /**< @ingroup LacHashDefs
25  * MD5 digest length in bytes */
26 #define LAC_HASH_MD5_STATE_SIZE 16
27 /**< @ingroup LacHashDefs
28  * MD5 state size */
29 
30 /* Constants for SHA1 algorithm  */
31 #define LAC_HASH_SHA1_BLOCK_SIZE 64
32 /**< @ingroup LacHashDefs
33  * SHA1 Block size in bytes */
34 #define LAC_HASH_SHA1_DIGEST_SIZE 20
35 /**< @ingroup LacHashDefs
36  *  SHA1 digest length in bytes */
37 #define LAC_HASH_SHA1_STATE_SIZE 20
38 /**< @ingroup LacHashDefs
39  *  SHA1 state size */
40 
41 /* Constants for SHA224 algorithm  */
42 #define LAC_HASH_SHA224_BLOCK_SIZE 64
43 /**< @ingroup LacHashDefs
44  *  SHA224 block size in bytes */
45 #define LAC_HASH_SHA224_DIGEST_SIZE 28
46 /**< @ingroup LacHashDefs
47  *  SHA224 digest length in bytes */
48 #define LAC_HASH_SHA224_STATE_SIZE 32
49 /**< @ingroup LacHashDefs
50  * SHA224 state size */
51 
52 /* Constants for SHA256 algorithm  */
53 #define LAC_HASH_SHA256_BLOCK_SIZE 64
54 /**< @ingroup LacHashDefs
55  *  SHA256 block size in bytes */
56 #define LAC_HASH_SHA256_DIGEST_SIZE 32
57 /**< @ingroup LacHashDefs
58  *  SHA256 digest length */
59 #define LAC_HASH_SHA256_STATE_SIZE 32
60 /**< @ingroup LacHashDefs
61  *  SHA256 state size */
62 
63 /* Constants for SHA384 algorithm  */
64 #define LAC_HASH_SHA384_BLOCK_SIZE 128
65 /**< @ingroup LacHashDefs
66  *  SHA384 block size in bytes */
67 #define LAC_HASH_SHA384_DIGEST_SIZE 48
68 /**< @ingroup LacHashDefs
69  *  SHA384 digest length in bytes */
70 #define LAC_HASH_SHA384_STATE_SIZE 64
71 /**< @ingroup LacHashDefs
72  *  SHA384 state size */
73 
74 /* Constants for SHA512 algorithm  */
75 #define LAC_HASH_SHA512_BLOCK_SIZE 128
76 /**< @ingroup LacHashDefs
77  *  SHA512 block size in bytes */
78 #define LAC_HASH_SHA512_DIGEST_SIZE 64
79 /**< @ingroup LacHashDefs
80  *  SHA512 digest length in bytes */
81 #define LAC_HASH_SHA512_STATE_SIZE 64
82 /**< @ingroup LacHashDefs
83  *  SHA512 state size */
84 
85 /* Constants for SHA3_224 algorithm  */
86 #define LAC_HASH_SHA3_224_BLOCK_SIZE 144
87 /**< @ingroup LacHashDefs
88  *  SHA3_224 block size in bytes */
89 #define LAC_HASH_SHA3_224_DIGEST_SIZE 28
90 /**< @ingroup LacHashDefs
91  *  SHA3_224 digest length in bytes */
92 #define LAC_HASH_SHA3_224_STATE_SIZE 28
93 /**< @ingroup LacHashDefs
94  *  SHA3_224 state size */
95 
96 /* Constants for SHA3_256 algorithm  */
97 #define LAC_HASH_SHA3_256_BLOCK_SIZE 136
98 /**< @ingroup LacHashDefs
99  *  SHA3_256 block size in bytes */
100 #define LAC_HASH_SHA3_256_DIGEST_SIZE 32
101 /**< @ingroup LacHashDefs
102  *  SHA3_256 digest length in bytes */
103 #define LAC_HASH_SHA3_256_STATE_SIZE 32
104 /**< @ingroup LacHashDefs
105  *  SHA3_256 state size */
106 
107 /* Constants for SHA3_384 algorithm  */
108 #define LAC_HASH_SHA3_384_BLOCK_SIZE 104
109 /**< @ingroup LacHashDefs
110  *  SHA3_384 block size in bytes */
111 #define LAC_HASH_SHA3_384_DIGEST_SIZE 48
112 /**< @ingroup LacHashDefs
113  *  SHA3_384 digest length in bytes */
114 #define LAC_HASH_SHA3_384_STATE_SIZE 48
115 /**< @ingroup LacHashDefs
116  *  SHA3_384 state size */
117 
118 /* Constants for SHA3_512 algorithm  */
119 #define LAC_HASH_SHA3_512_BLOCK_SIZE 72
120 /**< @ingroup LacHashDefs
121  *  SHA3_512 block size in bytes */
122 #define LAC_HASH_SHA3_512_DIGEST_SIZE 64
123 /**< @ingroup LacHashDefs
124  *  SHA3_512 digest length in bytes */
125 #define LAC_HASH_SHA3_512_STATE_SIZE 64
126 /**< @ingroup LacHashDefs
127  *  SHA3_512 state size */
128 
129 #define LAC_HASH_SHA3_STATEFUL_STATE_SIZE 200
130 
131 /* Constants for SM3 algorithm  */
132 #define LAC_HASH_SM3_BLOCK_SIZE 64
133 /**< @ingroup LacHashDefs
134  *  SM3 block size in bytes */
135 #define LAC_HASH_SM3_DIGEST_SIZE 32
136 /**< @ingroup LacHashDefs
137  *  SM3 digest length */
138 #define LAC_HASH_SM3_STATE_SIZE 32
139 /**< @ingroup LacHashDefs
140  *  SM3 state size */
141 
142 /* Constants for POLY algorithm  */
143 #define LAC_HASH_POLY_BLOCK_SIZE 64
144 /**< @ingroup LacHashDefs
145  *  POLY block size in bytes */
146 #define LAC_HASH_POLY_DIGEST_SIZE 16
147 /**< @ingroup LacHashDefs
148  *  POLY digest length */
149 #define LAC_HASH_POLY_STATE_SIZE 0
150 /**< @ingroup LacHashDefs
151  *  POLY state size */
152 
153 /* Constants for XCBC precompute algorithm  */
154 #define LAC_HASH_XCBC_PRECOMP_KEY_NUM 3
155 /**< @ingroup LacHashDefs
156  *  The Pre-compute operation involves deriving 3 128-bit
157  *  keys (K1, K2 and K3) */
158 
159 /* Constants for XCBC MAC algorithm  */
160 #define LAC_HASH_XCBC_MAC_BLOCK_SIZE 16
161 /**< @ingroup LacHashDefs
162  *  XCBC_MAC block size in bytes */
163 #define LAC_HASH_XCBC_MAC_128_DIGEST_SIZE 16
164 /**< @ingroup LacHashDefs
165  *  XCBC_MAC_PRF_128 digest length in bytes */
166 
167 /* Constants for AES CMAC algorithm  */
168 #define LAC_HASH_CMAC_BLOCK_SIZE 16
169 /**< @ingroup LacHashDefs
170  *  AES CMAC block size in bytes */
171 #define LAC_HASH_CMAC_128_DIGEST_SIZE 16
172 /**< @ingroup LacHashDefs
173  *  AES CMAC digest length in bytes */
174 
175 /* constants for AES CCM */
176 #define LAC_HASH_AES_CCM_BLOCK_SIZE 16
177 /**< @ingroup LacHashDefs
178  *  block size for CBC-MAC part of CCM */
179 #define LAC_HASH_AES_CCM_DIGEST_SIZE 16
180 /**< @ingroup LacHashDefs
181  *  untruncated size of authentication field */
182 
183 /* constants for AES GCM */
184 #define LAC_HASH_AES_GCM_BLOCK_SIZE 16
185 /**< @ingroup LacHashDefs
186  *  block size for Galois Hash 128 part of CCM */
187 #define LAC_HASH_AES_GCM_DIGEST_SIZE 16
188 /**< @ingroup LacHashDefs
189  *  untruncated size of authentication field */
190 
191 /* constants for KASUMI F9 */
192 #define LAC_HASH_KASUMI_F9_BLOCK_SIZE 8
193 /**< @ingroup LacHashDefs
194  *  KASUMI_F9 block size in bytes */
195 #define LAC_HASH_KASUMI_F9_DIGEST_SIZE 4
196 /**< @ingroup LacHashDefs
197  *  KASUMI_F9 digest size in bytes */
198 
199 /* constants for SNOW3G UIA2 */
200 #define LAC_HASH_SNOW3G_UIA2_BLOCK_SIZE 8
201 /**< @ingroup LacHashDefs
202  *  SNOW3G UIA2 block size in bytes */
203 #define LAC_HASH_SNOW3G_UIA2_DIGEST_SIZE 4
204 /**< @ingroup LacHashDefs
205  *  SNOW3G UIA2 digest size in bytes */
206 
207 /* constants for AES CBC MAC */
208 #define LAC_HASH_AES_CBC_MAC_BLOCK_SIZE 16
209 /**< @ingroup LacHashDefs
210  *  AES CBC MAC block size in bytes */
211 #define LAC_HASH_AES_CBC_MAC_DIGEST_SIZE 16
212 /**< @ingroup LacHashDefs
213  *  AES CBC MAC digest size in bytes */
214 
215 #define LAC_HASH_ZUC_EIA3_BLOCK_SIZE 4
216 /**< @ingroup LacHashDefs
217  *  ZUC EIA3 block size in bytes */
218 #define LAC_HASH_ZUC_EIA3_DIGEST_SIZE 4
219 /**< @ingroup LacHashDefs
220  *  ZUC EIA3 digest size in bytes */
221 
222 /* constants for AES GCM ICV allowed sizes */
223 #define LAC_HASH_AES_GCM_ICV_SIZE_8 8
224 #define LAC_HASH_AES_GCM_ICV_SIZE_12 12
225 #define LAC_HASH_AES_GCM_ICV_SIZE_16 16
226 
227 /* constants for AES CCM ICV allowed sizes */
228 #define LAC_HASH_AES_CCM_ICV_SIZE_MIN 4
229 #define LAC_HASH_AES_CCM_ICV_SIZE_MAX 16
230 
231 /* constants for authentication algorithms */
232 #define LAC_HASH_IPAD_BYTE 0x36
233 /**< @ingroup LacHashDefs
234  *  Ipad Byte */
235 #define LAC_HASH_OPAD_BYTE 0x5c
236 /**< @ingroup LacHashDefs
237  *  Opad Byte */
238 
239 #define LAC_HASH_IPAD_4_BYTES 0x36363636
240 /**< @ingroup LacHashDefs
241  *  Ipad for 4 Bytes */
242 #define LAC_HASH_OPAD_4_BYTES 0x5c5c5c5c
243 /**< @ingroup LacHashDefs
244  *  Opad for 4 Bytes */
245 
246 /* Key Modifier (KM) value used in Kasumi algorithm in F9 mode to XOR
247  * Integrity Key (IK) */
248 #define LAC_HASH_KASUMI_F9_KEY_MODIFIER_4_BYTES 0xAAAAAAAA
249 /**< @ingroup LacHashDefs
250  *  Kasumi F9 Key Modifier for 4 bytes */
251 
252 #define LAC_SYM_QAT_HASH_IV_REQ_MAX_SIZE_QW 2
253 /**< @ingroup LacSymQatHash
254  * Maximum size of IV embedded in the request.
255  * This is set to 2, namely 4 LONGWORDS. */
256 
257 #define LAC_SYM_QAT_HASH_STATE1_MAX_SIZE_BYTES LAC_HASH_SHA512_BLOCK_SIZE
258 /**< @ingroup LacSymQatHash
259  * Maximum size of state1 in the hash setup block of the content descriptor.
260  * This is set to the block size of SHA512. */
261 
262 #define LAC_SYM_QAT_HASH_STATE2_MAX_SIZE_BYTES LAC_HASH_SHA512_BLOCK_SIZE
263 /**< @ingroup LacSymQatHash
264  * Maximum size of state2 in the hash setup block of the content descriptor.
265  * This is set to the block size of SHA512. */
266 
267 #define LAC_MAX_INNER_OUTER_PREFIX_SIZE_BYTES 255
268 /**< Maximum size of the inner and outer prefix for nested hashing operations.
269  * This is got from the maximum size supported by the accelerator which stores
270  * the size in an 8bit field */
271 
272 #define LAC_MAX_HASH_STATE_STORAGE_SIZE                                        \
273 	(sizeof(icp_qat_hw_auth_counter_t) + LAC_HASH_SHA3_STATEFUL_STATE_SIZE)
274 /**< Maximum size of the hash state storage section of the hash state prefix
275  * buffer */
276 
277 #define LAC_MAX_HASH_STATE_BUFFER_SIZE_BYTES                                   \
278 	LAC_MAX_HASH_STATE_STORAGE_SIZE +                                      \
279 	    (LAC_ALIGN_POW2_ROUNDUP(LAC_MAX_INNER_OUTER_PREFIX_SIZE_BYTES,     \
280 				    LAC_QUAD_WORD_IN_BYTES) *                  \
281 	     2)
282 /**< Maximum size of the hash state prefix buffer will be for nested hash when
283  * there is the maximum sized inner prefix and outer prefix */
284 
285 #define LAC_MAX_AAD_SIZE_BYTES 256
286 /**< Maximum size of AAD in bytes */
287 
288 #define IS_HMAC_ALG(algorithm)                                                 \
289 	((algorithm == CPA_CY_SYM_HASH_MD5) ||                                 \
290 	 (algorithm == CPA_CY_SYM_HASH_SHA1) ||                                \
291 	 (algorithm == CPA_CY_SYM_HASH_SHA224) ||                              \
292 	 (algorithm == CPA_CY_SYM_HASH_SHA256) ||                              \
293 	 (algorithm == CPA_CY_SYM_HASH_SHA384) ||                              \
294 	 (algorithm == CPA_CY_SYM_HASH_SHA512) ||                              \
295 	 (algorithm == CPA_CY_SYM_HASH_SM3)) ||                                \
296 	    (LAC_HASH_IS_SHA3(algorithm))
297 /**< @ingroup LacSymQatHash
298  * Macro to detect if the hash algorithm is a HMAC algorithm */
299 
300 #define IS_HASH_MODE_1(qatHashMode) (ICP_QAT_HW_AUTH_MODE1 == qatHashMode)
301 /**< @ingroup LacSymQatHash
302  * Macro to detect is qat hash mode is set to 1 (precompute mode)
303  * only used with algorithms in hash mode CPA_CY_SYM_HASH_MODE_AUTH */
304 
305 #define IS_HASH_MODE_2(qatHashMode) (ICP_QAT_HW_AUTH_MODE2 == qatHashMode)
306 /**< @ingroup LacSymQatHash
307  * Macro to detect is qat hash mode is set to 2. This is used for TLS and
308  * mode 2 HMAC (no preompute mode) */
309 
310 #define IS_HASH_MODE_2_AUTH(qatHashMode, hashMode)                             \
311 	((IS_HASH_MODE_2(qatHashMode)) &&                                      \
312 	 (CPA_CY_SYM_HASH_MODE_AUTH == hashMode))
313 /**< @ingroup LacSymQatHash
314  * Macro to check for qat hash mode is set to 2 and the hash mode is
315  * Auth. This applies to HMAC algorithms (no pre compute). This is used
316  * to differntiate between TLS and HMAC */
317 
318 #define IS_HASH_MODE_2_NESTED(qatHashMode, hashMode)                           \
319 	((IS_HASH_MODE_2(qatHashMode)) &&                                      \
320 	 (CPA_CY_SYM_HASH_MODE_NESTED == hashMode))
321 /**< @ingroup LacSymQatHash
322  * Macro to check for qat hash mode is set to 2 and the LAC hash mode is
323  * Nested. This applies to TLS. This is used to differentiate between
324  * TLS and HMAC */
325 
326 #define LAC_HASH_IS_SHA3(algo)                                                 \
327 	((algo == CPA_CY_SYM_HASH_SHA3_224) ||                                 \
328 	 (algo == CPA_CY_SYM_HASH_SHA3_256) ||                                 \
329 	 (algo == CPA_CY_SYM_HASH_SHA3_384) ||                                 \
330 	 (algo == CPA_CY_SYM_HASH_SHA3_512))
331 /**< @ingroup LacSymQatHash
332  * Macro to check if the hash algorithm is SHA3 */
333 
334 #endif /* LAC_SYM_HASH_DEFS_H */
335