xref: /freebsd/sys/dev/qat/qat_api/common/crypto/sym/include/lac_sym_cipher_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_cipher_defs.h
7  *
8  * @ingroup LacCipher
9  *
10  * @description
11  *      This file defines constants for the cipher operations.
12  *
13  *****************************************************************************/
14 
15 /***************************************************************************/
16 
17 #ifndef LAC_SYM_CIPHER_DEFS_H
18 #define LAC_SYM_CIPHER_DEFS_H
19 
20 /*
21 ******************************************************************************
22 * Include public/global header files
23 ******************************************************************************
24 */
25 
26 #include "cpa.h"
27 #include "cpa_cy_sym.h"
28 
29 /*
30 *******************************************************************************
31 * Include private header files
32 *******************************************************************************
33 */
34 
35 /***************************************************************************/
36 
37 /*
38  * Constants value for ARC4 algorithm
39  */
40 /* ARC4 algorithm block size */
41 #define LAC_CIPHER_ARC4_BLOCK_LEN_BYTES 8
42 /* ARC4 key matrix size (bytes) */
43 #define LAC_CIPHER_ARC4_KEY_MATRIX_LEN_BYTES 256
44 /* ARC4 256 bytes for key matrix, 2 for i and j and 6 bytes for padding */
45 #define LAC_CIPHER_ARC4_STATE_LEN_BYTES 264
46 
47 /*
48  * Constant values for CCM AAD buffer
49  */
50 #define LAC_CIPHER_CCM_B0_SIZE 16
51 #define LAC_CIPHER_CCM_ENCODED_AAD_LEN_SIZE 2
52 #define LAC_CIPHER_CCM_AAD_OFFSET                                              \
53 	(LAC_CIPHER_CCM_B0_SIZE + LAC_CIPHER_CCM_ENCODED_AAD_LEN_SIZE)
54 
55 #define LAC_SYM_SNOW3G_CIPHER_CONFIG_FOR_HASH_SZ 40
56 /* Snow3g cipher config required for performing a Snow3g hash operation.
57  * It contains 8 Bytes of config for hardware, 16 Bytes of Key and requires
58  * 16 Bytes for the IV.
59  */
60 
61 /* Key Modifier (KM) 4 bytes used in Kasumi algorithm in F8 mode to XOR
62  * Cipher Key (CK) */
63 #define LAC_CIPHER_KASUMI_F8_KEY_MODIFIER_4_BYTES 0x55555555
64 
65 /* The IV length for Kasumi Kgcore is 8 bytes */
66 #define LAC_CIPHER_KASUMI_F8_IV_LENGTH 8
67 
68 /* The Counter length for Kasumi Kgcore is 8 bytes */
69 #define LAC_CIPHER_KASUMI_F8_COUNTER_LENGTH 8
70 
71 /* The IV length for AES F8 is 16 bytes */
72 #define LAC_CIPHER_AES_F8_IV_LENGTH 16
73 
74 /* The max key length for AES XTS 32 is bytes*/
75 #define LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH 32
76 
77 /* For Snow3G UEA2, need to make sure last 8 Bytes of IV buffer are
78  * zero. */
79 #define LAC_CIPHER_SNOW3G_UEA2_IV_BUFFER_ZERO_LENGTH 8
80 
81 /* Reserve enough space for max length cipher state
82  * (can be IV , counter or ARC4 state)  */
83 #define LAC_CIPHER_STATE_SIZE_MAX LAC_CIPHER_ARC4_STATE_LEN_BYTES
84 
85 /* Reserve enough space for max length cipher IV
86  * (can be A value for Kasumi(passed in as IV), IV or counter,
87  * but not ARC4 state)  */
88 #define LAC_CIPHER_IV_SIZE_MAX ICP_QAT_HW_SNOW_3G_UEA2_IV_SZ
89 
90 /* 96-bit case of IV for GCM algorithm */
91 #define LAC_CIPHER_IV_SIZE_GCM_12 12
92 
93 /* 96-bit case of IV for CCP/GCM single pass algorithm */
94 #define LAC_CIPHER_SPC_IV_SIZE 12
95 /*
96  * Constants value for NULL algorithm
97  */
98 /* NULL algorithm block size */
99 #define LAC_CIPHER_NULL_BLOCK_LEN_BYTES 8
100 
101 /* Macro to check if the Algorithm is SM4 */
102 #define LAC_CIPHER_IS_SM4(algo)                                                \
103 	((algo == CPA_CY_SYM_CIPHER_SM4_ECB) ||                                \
104 	 (algo == CPA_CY_SYM_CIPHER_SM4_CBC) ||                                \
105 	 (algo == CPA_CY_SYM_CIPHER_SM4_CTR))
106 
107 /* Macro to check if the Algorithm is CHACHA */
108 #define LAC_CIPHER_IS_CHACHA(algo) (algo == CPA_CY_SYM_CIPHER_CHACHA)
109 /* Macro to check if the Algorithm is AES */
110 #define LAC_CIPHER_IS_AES(algo)                                                \
111 	((algo == CPA_CY_SYM_CIPHER_AES_ECB) ||                                \
112 	 (algo == CPA_CY_SYM_CIPHER_AES_CBC) ||                                \
113 	 (algo == CPA_CY_SYM_CIPHER_AES_CTR) ||                                \
114 	 (algo == CPA_CY_SYM_CIPHER_AES_CCM) ||                                \
115 	 (algo == CPA_CY_SYM_CIPHER_AES_GCM) ||                                \
116 	 (algo == CPA_CY_SYM_CIPHER_AES_XTS))
117 
118 /* Macro to check if the Algorithm is DES */
119 #define LAC_CIPHER_IS_DES(algo)                                                \
120 	((algo == CPA_CY_SYM_CIPHER_DES_ECB) ||                                \
121 	 (algo == CPA_CY_SYM_CIPHER_DES_CBC))
122 
123 /* Macro to check if the Algorithm is Triple DES */
124 #define LAC_CIPHER_IS_TRIPLE_DES(algo)                                         \
125 	((algo == CPA_CY_SYM_CIPHER_3DES_ECB) ||                               \
126 	 (algo == CPA_CY_SYM_CIPHER_3DES_CBC) ||                               \
127 	 (algo == CPA_CY_SYM_CIPHER_3DES_CTR))
128 
129 /* Macro to check if the Algorithm is Kasumi */
130 #define LAC_CIPHER_IS_KASUMI(algo) (algo == CPA_CY_SYM_CIPHER_KASUMI_F8)
131 
132 /* Macro to check if the Algorithm is Snow3G UEA2 */
133 #define LAC_CIPHER_IS_SNOW3G_UEA2(algo) (algo == CPA_CY_SYM_CIPHER_SNOW3G_UEA2)
134 
135 /* Macro to check if the Algorithm is ARC4 */
136 #define LAC_CIPHER_IS_ARC4(algo) (algo == CPA_CY_SYM_CIPHER_ARC4)
137 
138 /* Macro to check if the Algorithm is ZUC EEA3 */
139 #define LAC_CIPHER_IS_ZUC_EEA3(algo) (algo == CPA_CY_SYM_CIPHER_ZUC_EEA3)
140 
141 /* Macro to check if the Algorithm is NULL */
142 #define LAC_CIPHER_IS_NULL(algo) (algo == CPA_CY_SYM_CIPHER_NULL)
143 
144 /* Macro to check if the Mode is CTR */
145 #define LAC_CIPHER_IS_CTR_MODE(algo)                                           \
146 	((algo == CPA_CY_SYM_CIPHER_AES_CTR) ||                                \
147 	 (algo == CPA_CY_SYM_CIPHER_3DES_CTR) || (LAC_CIPHER_IS_CCM(algo)) ||  \
148 	 (LAC_CIPHER_IS_GCM(algo)) || (LAC_CIPHER_IS_CHACHA(algo)) ||          \
149 	 (algo == CPA_CY_SYM_CIPHER_SM4_CTR))
150 
151 /* Macro to check if the Algorithm is ECB */
152 #define LAC_CIPHER_IS_ECB_MODE(algo)                                           \
153 	((algo == CPA_CY_SYM_CIPHER_AES_ECB) ||                                \
154 	 (algo == CPA_CY_SYM_CIPHER_DES_ECB) ||                                \
155 	 (algo == CPA_CY_SYM_CIPHER_3DES_ECB) ||                               \
156 	 (algo == CPA_CY_SYM_CIPHER_NULL) ||                                   \
157 	 (algo == CPA_CY_SYM_CIPHER_SNOW3G_UEA2) ||                            \
158 	 (algo == CPA_CY_SYM_CIPHER_SM4_ECB))
159 
160 /* Macro to check if the Algorithm Mode is F8 */
161 #define LAC_CIPHER_IS_F8_MODE(algo)                                            \
162 	((algo == CPA_CY_SYM_CIPHER_KASUMI_F8) ||                              \
163 	 (algo == CPA_CY_SYM_CIPHER_AES_F8))
164 
165 /* Macro to check if the Algorithm is CBC */
166 #define LAC_CIPHER_IS_CBC_MODE(algo)                                           \
167 	((algo == CPA_CY_SYM_CIPHER_AES_CBC) ||                                \
168 	 (algo == CPA_CY_SYM_CIPHER_DES_CBC) ||                                \
169 	 (algo == CPA_CY_SYM_CIPHER_3DES_CBC) ||                               \
170 	 (algo == CPA_CY_SYM_CIPHER_SM4_CBC))
171 
172 /* Macro to check if the Algorithm is CCM */
173 #define LAC_CIPHER_IS_CCM(algo) (algo == CPA_CY_SYM_CIPHER_AES_CCM)
174 
175 /* Macro to check if the Algorithm is GCM */
176 #define LAC_CIPHER_IS_GCM(algo) (algo == CPA_CY_SYM_CIPHER_AES_GCM)
177 
178 /* Macro to check if the Algorithm is AES-F8 */
179 #define LAC_CIPHER_IS_AES_F8(algo) (algo == CPA_CY_SYM_CIPHER_AES_F8)
180 
181 /* Macro to check if the Algorithm Mode is XTS */
182 #define LAC_CIPHER_IS_XTS_MODE(algo) (algo == CPA_CY_SYM_CIPHER_AES_XTS)
183 
184 /* Macro to check if the accelerator has AES V2 capability */
185 #define LAC_CIPHER_AES_V2(mask) ((mask)&ICP_ACCEL_CAPABILITIES_AES_V2)
186 
187 /* Macro to check if the Algorithm is single pass */
188 #define LAC_CIPHER_IS_SPC(cipher, hash, mask)                                  \
189 	(((mask)&ICP_ACCEL_CAPABILITIES_CHACHA_POLY &&                         \
190 	  LAC_CIPHER_IS_CHACHA(cipher) && (CPA_CY_SYM_HASH_POLY == hash)) ||   \
191 	 (((mask)&ICP_ACCEL_CAPABILITIES_AESGCM_SPC) &&                        \
192 	  LAC_CIPHER_IS_GCM(cipher) &&                                         \
193 	  ((CPA_CY_SYM_HASH_AES_GCM == hash) ||                                \
194 	   (CPA_CY_SYM_HASH_AES_GMAC == hash))) ||                             \
195 	 (LAC_CIPHER_IS_CCM(cipher) && LAC_CIPHER_AES_V2(mask)))
196 
197 #endif /* LAC_CIPHER_DEFS_H */
198