1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 
5 /**
6  ***************************************************************************
7  * @file lac_sym_hash_precomputes.h
8  *
9  * @defgroup LacHashDefs Hash Definitions
10  *
11  * @ingroup  LacHash
12  *
13  * Constants for hash algorithms
14  *
15  ***************************************************************************/
16 #ifndef LAC_SYM_HASH_PRECOMPUTES_H
17 #define LAC_SYM_HASH_PRECOMPUTES_H
18 
19 #include "lac_sym_hash.h"
20 
21 #define LAC_SYM_AES_CMAC_RB_128 0x87 /* constant used for   */
22 				     /* CMAC calculation    */
23 
24 #define LAC_SYM_HASH_MSBIT_MASK 0x80 /* Mask to check MSB top bit */
25 				     /* zero or one */
26 
27 #define LAC_SINGLE_BUFFER_HW_META_SIZE                                         \
28 	(sizeof(icp_buffer_list_desc_t) + sizeof(icp_flat_buffer_desc_t))
29 /**< size of memory to allocate for the HW buffer list that is sent to the
30  * QAT */
31 
32 #define LAC_SYM_HASH_PRECOMP_MAX_WORKING_BUFFER                                \
33 	((sizeof(lac_sym_hash_precomp_op_data_t) * 2) +                        \
34 	 sizeof(lac_sym_hash_precomp_op_t))
35 /**< maximum size of the working data for the HMAC precompute operations
36  *
37  * Maximum size of lac_sym_hash_precomp_op_data_t is 264 bytes. For hash
38  * precomputes there are 2 of these structrues and a further
39  * lac_sym_hash_precomp_op_t structure required. This comes to a total of 536
40  * bytes.
41  * For the asynchronous version of the precomputes, the memory for the hash
42  * state prefix buffer is used as the working memory. There are 584 bytes
43  * which are alloacted for the hash state prefix buffer which is enough to
44  * carve up for the precomputes.
45  */
46 
47 #define LAC_SYM_HASH_PRECOMP_MAX_AES_ECB_DATA                                  \
48 	((ICP_QAT_HW_AES_128_KEY_SZ) * (3))
49 /**< Maximum size for the data that an AES ECB precompute is generated on */
50 
51 /**
52  *****************************************************************************
53  * @ingroup LacHashDefs
54  *      Precompute type enum
55  * @description
56  *      Enum used to distinguish between precompute types
57  *
58  *****************************************************************************/
59 typedef enum {
60 	LAC_SYM_HASH_PRECOMP_HMAC = 1,
61 	/**< Hmac precompute operation. Copy state from hash state buffer */
62 	LAC_SYM_HASH_PRECOMP_AES_ECB,
63 	/**< XCBC/CGM precompute, Copy state from data buffer */
64 } lac_sym_hash_precomp_type_t;
65 
66 /**
67  *****************************************************************************
68  * @ingroup LacHashDefs
69  *      overall precompute management structure
70  * @description
71  *      structure used to manage the precompute operations for a session
72  *
73  *****************************************************************************/
74 typedef struct lac_sym_hash_precomp_op_s {
75 	lac_hash_precompute_done_cb_t callbackFn;
76 	/**< Callback function to be invoked when the final precompute completes
77 	 */
78 
79 	void *pCallbackTag;
80 	/**< Opaque data to be passed back as a parameter in the callback */
81 
82 	QatUtilsAtomic opsPending;
83 	/**< counter used to determine if the current precompute is the
84 	 * final one. */
85 
86 } lac_sym_hash_precomp_op_t;
87 
88 /**
89  *****************************************************************************
90  * @ingroup LacHashDefs
91  *      hmac precompute structure as used by the QAT
92  * @description
93  *      data used by the QAT for HMAC precomputes
94  *
95  *      Must be allocated on an 8-byte aligned memory address.
96  *
97  *****************************************************************************/
98 typedef struct lac_sym_hash_hmac_precomp_qat_s {
99 	Cpa8U data[LAC_HASH_SHA512_BLOCK_SIZE];
100 	/**< data to be hashed - block size of data for the algorithm */
101 	/* NOTE: to save space we could have got the QAT to overwrite
102 	 * this with the hash state storage */
103 	icp_qat_fw_la_auth_req_params_t hashReqParams;
104 	/**< Request parameters as read in by the QAT */
105 	Cpa8U bufferDesc[LAC_SINGLE_BUFFER_HW_META_SIZE];
106 	/**< Buffer descriptor structure */
107 	Cpa8U hashStateStorage[LAC_MAX_HASH_STATE_STORAGE_SIZE];
108 	/**< Internal buffer where QAT writes the intermediate partial
109 	 * state that is used in the precompute */
110 } lac_sym_hash_hmac_precomp_qat_t;
111 
112 /**
113  *****************************************************************************
114  * @ingroup LacHashDefs
115  *      AES ECB precompute structure as used by the QAT
116  * @description
117  *      data used by the QAT for AES ECB precomptes
118  *
119  *      Must be allocated on an 8-byte aligned memory address.
120  *
121  *****************************************************************************/
122 typedef struct lac_sym_hash_aes_precomp_qat_s {
123 	Cpa8U contentDesc[LAC_SYM_QAT_MAX_CIPHER_SETUP_BLK_SZ];
124 	/**< Content descriptor for a cipher operation */
125 	Cpa8U data[LAC_SYM_HASH_PRECOMP_MAX_AES_ECB_DATA];
126 	/**< The data to be ciphered is conatined here and the result is
127 	 * written in place back into this buffer */
128 	icp_qat_fw_la_cipher_req_params_t cipherReqParams;
129 	/**< Request parameters as read in by the QAT */
130 	Cpa8U bufferDesc[LAC_SINGLE_BUFFER_HW_META_SIZE];
131 	/**< Buffer descriptor structure */
132 } lac_sym_hash_aes_precomp_qat_t;
133 
134 /**
135  *****************************************************************************
136  * @ingroup LacHashDefs
137  *      overall structure for managing a single precompute operation
138  * @description
139  *      overall structure for managing a single precompute operation
140  *
141  *      Must be allocated on an 8-byte aligned memory address.
142  *
143  *****************************************************************************/
144 typedef struct lac_sym_hash_precomp_op_data_s {
145 	sal_crypto_service_t *pInstance;
146 	/**< Instance handle for the operation */
147 	Cpa8U reserved[4];
148 	/**< padding to align later structures on minimum 8-Byte address */
149 	lac_sym_hash_precomp_type_t opType;
150 	/**< operation type to determine the precompute type in the callback */
151 	lac_sym_hash_precomp_op_t *pOpStatus;
152 	/**< structure containing the counter and the condition for the overall
153 	 * precompute operation. This is a pointer because the memory structure
154 	 * may be shared between precomputes when there are more than 1 as in
155 	 * the
156 	 * case of HMAC */
157 	union {
158 		lac_sym_hash_hmac_precomp_qat_t hmacQatData;
159 		/**< Data sent to the QAT for hmac precomputes */
160 		lac_sym_hash_aes_precomp_qat_t aesQatData;
161 		/**< Data sent to the QAT for AES ECB precomputes */
162 	} u;
163 
164 	/**< ASSUMPTION: The above structures are 8 byte aligned if the overall
165 	 * struct is 8 byte aligned, as there are two 4 byte fields before this
166 	 * union */
167 	Cpa32U stateSize;
168 	/**< Size of the state to be copied into the state pointer in the
169 	 * content
170 	 * descriptor */
171 	Cpa8U *pState;
172 	/**< pointer to the state in the content descriptor where the result of
173 	 * the precompute should be copied to */
174 } lac_sym_hash_precomp_op_data_t;
175 
176 #endif /* LAC_SYM_HASH_PRECOMPUTES_H */
177