cdbe7f520b97476dc40edeb5b8085a4cb3e44768
[dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd_private.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2016 Intel Corporation
3  */
4
5 #ifndef _RTE_AESNI_MB_PMD_PRIVATE_H_
6 #define _RTE_AESNI_MB_PMD_PRIVATE_H_
7
8 #include <intel-ipsec-mb.h>
9
10
11 /*
12  * IMB_VERSION_NUM macro was introduced in version Multi-buffer 0.50,
13  * so if macro is not defined, it means that the version is 0.49.
14  */
15 #if !defined(IMB_VERSION_NUM)
16 #define IMB_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
17 #define IMB_VERSION_NUM IMB_VERSION(0, 49, 0)
18 #endif
19
20 #if IMB_VERSION_NUM < IMB_VERSION(0, 52, 0)
21 #include "aesni_mb_ops.h"
22 #endif
23
24 #if IMB_VERSION_NUM >= IMB_VERSION(0, 52, 0)
25 enum aesni_mb_vector_mode {
26         RTE_AESNI_MB_NOT_SUPPORTED = 0,
27         RTE_AESNI_MB_SSE,
28         RTE_AESNI_MB_AVX,
29         RTE_AESNI_MB_AVX2,
30         RTE_AESNI_MB_AVX512
31 };
32 #endif
33
34
35 #define CRYPTODEV_NAME_AESNI_MB_PMD     crypto_aesni_mb
36 /**< AES-NI Multi buffer PMD device name */
37
38 /** AESNI_MB PMD LOGTYPE DRIVER */
39 int aesni_mb_logtype_driver;
40
41 #define AESNI_MB_LOG(level, fmt, ...)  \
42         rte_log(RTE_LOG_ ## level, aesni_mb_logtype_driver,  \
43                         "%s() line %u: " fmt "\n", __func__, __LINE__,  \
44                                         ## __VA_ARGS__)
45
46
47 #define HMAC_IPAD_VALUE                 (0x36)
48 #define HMAC_OPAD_VALUE                 (0x5C)
49
50 /* Maximum length for digest */
51 #define DIGEST_LENGTH_MAX 64
52 static const unsigned auth_blocksize[] = {
53                 [MD5]           = 64,
54                 [SHA1]          = 64,
55                 [SHA_224]       = 64,
56                 [SHA_256]       = 64,
57                 [SHA_384]       = 128,
58                 [SHA_512]       = 128,
59                 [AES_XCBC]      = 16,
60                 [AES_CCM]       = 16,
61 };
62
63 /**
64  * Get the blocksize in bytes for a specified authentication algorithm
65  *
66  * @Note: this function will not return a valid value for a non-valid
67  * authentication algorithm
68  */
69 static inline unsigned
70 get_auth_algo_blocksize(JOB_HASH_ALG algo)
71 {
72         return auth_blocksize[algo];
73 }
74
75 static const unsigned auth_truncated_digest_byte_lengths[] = {
76                 [MD5]           = 12,
77                 [SHA1]          = 12,
78                 [SHA_224]       = 14,
79                 [SHA_256]       = 16,
80                 [SHA_384]       = 24,
81                 [SHA_512]       = 32,
82                 [AES_XCBC]      = 12,
83                 [AES_CMAC]      = 12,
84                 [AES_CCM]       = 8,
85                 [NULL_HASH]     = 0
86 };
87
88 /**
89  * Get the IPsec specified truncated length in bytes of the HMAC digest for a
90  * specified authentication algorithm
91  *
92  * @Note: this function will not return a valid value for a non-valid
93  * authentication algorithm
94  */
95 static inline unsigned
96 get_truncated_digest_byte_length(JOB_HASH_ALG algo)
97 {
98         return auth_truncated_digest_byte_lengths[algo];
99 }
100
101 static const unsigned auth_digest_byte_lengths[] = {
102                 [MD5]           = 16,
103                 [SHA1]          = 20,
104                 [SHA_224]       = 28,
105                 [SHA_256]       = 32,
106                 [SHA_384]       = 48,
107                 [SHA_512]       = 64,
108                 [AES_XCBC]      = 16,
109                 [AES_CMAC]      = 16,
110                 [AES_GMAC]      = 12,
111                 [NULL_HASH]     = 0,
112 #if IMB_VERSION_NUM >= IMB_VERSION(0, 52, 0)
113                 [PLAIN_SHA1]    = 20,
114                 [PLAIN_SHA_224] = 28,
115                 [PLAIN_SHA_256] = 32,
116                 [PLAIN_SHA_384] = 48,
117                 [PLAIN_SHA_512] = 64
118 #endif
119         /**< Vector mode dependent pointer table of the multi-buffer APIs */
120
121 };
122
123 /**
124  * Get the full digest size in bytes for a specified authentication algorithm
125  * (if available in the Multi-buffer library)
126  *
127  * @Note: this function will not return a valid value for a non-valid
128  * authentication algorithm
129  */
130 static inline unsigned
131 get_digest_byte_length(JOB_HASH_ALG algo)
132 {
133         return auth_digest_byte_lengths[algo];
134 }
135
136 enum aesni_mb_operation {
137         AESNI_MB_OP_HASH_CIPHER,
138         AESNI_MB_OP_CIPHER_HASH,
139         AESNI_MB_OP_HASH_ONLY,
140         AESNI_MB_OP_CIPHER_ONLY,
141         AESNI_MB_OP_AEAD_HASH_CIPHER,
142         AESNI_MB_OP_AEAD_CIPHER_HASH,
143         AESNI_MB_OP_NOT_SUPPORTED
144 };
145
146 /** private data structure for each virtual AESNI device */
147 struct aesni_mb_private {
148         enum aesni_mb_vector_mode vector_mode;
149         /**< CPU vector instruction set mode */
150         unsigned max_nb_queue_pairs;
151         /**< Max number of queue pairs supported by device */
152 #if IMB_VERSION_NUM >= IMB_VERSION(0, 52, 0)
153         MB_MGR *mb_mgr;
154         /**< Multi-buffer instance */
155 #endif
156 };
157
158 /** AESNI Multi buffer queue pair */
159 struct aesni_mb_qp {
160         uint16_t id;
161         /**< Queue Pair Identifier */
162         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
163 #if IMB_VERSION_NUM < IMB_VERSION(0, 52, 0)
164         /**< Unique Queue Pair Name */
165         const struct aesni_mb_op_fns *op_fns;
166 #endif
167         /**< Unique Queue Pair Name */
168         MB_MGR *mb_mgr;
169         /**< Multi-buffer instance */
170         struct rte_ring *ingress_queue;
171         /**< Ring for placing operations ready for processing */
172         struct rte_mempool *sess_mp;
173         /**< Session Mempool */
174         struct rte_cryptodev_stats stats;
175         /**< Queue pair statistics */
176         uint8_t digest_idx;
177         /**< Index of the next slot to be used in temp_digests,
178          * to store the digest for a given operation
179          */
180         uint8_t temp_digests[MAX_JOBS][DIGEST_LENGTH_MAX];
181         /**< Buffers used to store the digest generated
182          * by the driver when verifying a digest provided
183          * by the user (using authentication verify operation)
184          */
185 } __rte_cache_aligned;
186
187 /** AES-NI multi-buffer private session structure */
188 struct aesni_mb_session {
189         JOB_CHAIN_ORDER chain_order;
190         struct {
191                 uint16_t length;
192                 uint16_t offset;
193         } iv;
194         /**< IV parameters */
195
196         /** Cipher Parameters */const struct aesni_mb_op_fns *op_fns;
197         /**< Vector mode dependent pointer table of the multi-buffer APIs */
198
199         struct {
200                 /** Cipher direction - encrypt / decrypt */
201                 JOB_CIPHER_DIRECTION direction;
202                 /** Cipher mode - CBC / Counter */
203                 JOB_CIPHER_MODE mode;
204
205                 uint64_t key_length_in_bytes;
206
207                 union {
208                         struct {
209                                 uint32_t encode[60] __rte_aligned(16);
210                                 /**< encode key */
211                                 uint32_t decode[60] __rte_aligned(16);
212                                 /**< decode key */
213                         } expanded_aes_keys;
214                         struct {
215                                 const void *ks_ptr[3];
216                                 uint64_t key[3][16];
217                         } exp_3des_keys;
218
219                         struct gcm_key_data gcm_key;
220                 };
221                 /**< Expanded AES keys - Allocating space to
222                  * contain the maximum expanded key size which
223                  * is 240 bytes for 256 bit AES, calculate by:
224                  * ((key size (bytes)) *
225                  * ((number of rounds) + 1))
226                  */
227         } cipher;
228
229         /** Authentication Parameters */
230         struct {
231                 JOB_HASH_ALG algo; /**< Authentication Algorithm */
232                 enum rte_crypto_auth_operation operation;
233                 /**< auth operation generate or verify */
234                 union {
235                         struct {
236                                 uint8_t inner[128] __rte_aligned(16);
237                                 /**< inner pad */
238                                 uint8_t outer[128] __rte_aligned(16);
239                                 /**< outer pad */
240                         } pads;
241                         /**< HMAC Authentication pads -
242                          * allocating space for the maximum pad
243                          * size supported which is 128 bytes for
244                          * SHA512
245                          */
246
247                         struct {
248                             uint32_t k1_expanded[44] __rte_aligned(16);
249                             /**< k1 (expanded key). */
250                             uint8_t k2[16] __rte_aligned(16);
251                             /**< k2. */
252                             uint8_t k3[16] __rte_aligned(16);
253                             /**< k3. */
254                         } xcbc;
255
256                         struct {
257                                 uint32_t expkey[60] __rte_aligned(16);
258                                                     /**< k1 (expanded key). */
259                                 uint32_t skey1[4] __rte_aligned(16);
260                                                     /**< k2. */
261                                 uint32_t skey2[4] __rte_aligned(16);
262                                                     /**< k3. */
263                         } cmac;
264                         /**< Expanded XCBC authentication keys */
265                 };
266         /** Generated digest size by the Multi-buffer library */
267         uint16_t gen_digest_len;
268         /** Requested digest size from Cryptodev */
269         uint16_t req_digest_len;
270
271         } auth;
272         struct {
273                 /** AAD data length */
274                 uint16_t aad_len;
275         } aead;
276 } __rte_cache_aligned;
277
278
279
280 #if IMB_VERSION_NUM >= IMB_VERSION(0, 52, 0)
281 /**
282  *
283  */
284 extern int
285 aesni_mb_set_session_parameters(const MB_MGR *mb_mgr,
286                 struct aesni_mb_session *sess,
287                 const struct rte_crypto_sym_xform *xform);
288 #else
289 extern int
290 aesni_mb_set_session_parameters(const struct aesni_mb_op_fns *mb_ops,
291                 struct aesni_mb_session *sess,
292                 const struct rte_crypto_sym_xform *xform);
293 #endif
294
295 /** device specific operations function pointer structure */
296 extern struct rte_cryptodev_ops *rte_aesni_mb_pmd_ops;
297
298
299
300 #endif /* _RTE_AESNI_MB_PMD_PRIVATE_H_ */