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