net/sfc: check mbufs allocated using mempool API for Rx
[dpdk.git] / drivers / crypto / aesni_mb / rte_aesni_mb_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2017 Intel Corporation
3  */
4
5 #include <intel-ipsec-mb.h>
6
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_cryptodev.h>
10 #include <rte_cryptodev_pmd.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_cpuflags.h>
14
15 #include "rte_aesni_mb_pmd_private.h"
16
17 #define AES_CCM_DIGEST_MIN_LEN 4
18 #define AES_CCM_DIGEST_MAX_LEN 16
19 #define HMAC_MAX_BLOCK_SIZE 128
20 static uint8_t cryptodev_driver_id;
21
22 typedef void (*hash_one_block_t)(const void *data, void *digest);
23 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
24
25 /**
26  * Calculate the authentication pre-computes
27  *
28  * @param one_block_hash        Function pointer to calculate digest on ipad/opad
29  * @param ipad                  Inner pad output byte array
30  * @param opad                  Outer pad output byte array
31  * @param hkey                  Authentication key
32  * @param hkey_len              Authentication key length
33  * @param blocksize             Block size of selected hash algo
34  */
35 static void
36 calculate_auth_precomputes(hash_one_block_t one_block_hash,
37                 uint8_t *ipad, uint8_t *opad,
38                 uint8_t *hkey, uint16_t hkey_len,
39                 uint16_t blocksize)
40 {
41         unsigned i, length;
42
43         uint8_t ipad_buf[blocksize] __rte_aligned(16);
44         uint8_t opad_buf[blocksize] __rte_aligned(16);
45
46         /* Setup inner and outer pads */
47         memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
48         memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
49
50         /* XOR hash key with inner and outer pads */
51         length = hkey_len > blocksize ? blocksize : hkey_len;
52
53         for (i = 0; i < length; i++) {
54                 ipad_buf[i] ^= hkey[i];
55                 opad_buf[i] ^= hkey[i];
56         }
57
58         /* Compute partial hashes */
59         (*one_block_hash)(ipad_buf, ipad);
60         (*one_block_hash)(opad_buf, opad);
61
62         /* Clean up stack */
63         memset(ipad_buf, 0, blocksize);
64         memset(opad_buf, 0, blocksize);
65 }
66
67 /** Get xform chain order */
68 static enum aesni_mb_operation
69 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
70 {
71         if (xform == NULL)
72                 return AESNI_MB_OP_NOT_SUPPORTED;
73
74         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
75                 if (xform->next == NULL)
76                         return AESNI_MB_OP_CIPHER_ONLY;
77                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
78                         return AESNI_MB_OP_CIPHER_HASH;
79         }
80
81         if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
82                 if (xform->next == NULL)
83                         return AESNI_MB_OP_HASH_ONLY;
84                 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
85                         return AESNI_MB_OP_HASH_CIPHER;
86         }
87
88         if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
89                 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM) {
90                         if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
91                                 return AESNI_MB_OP_AEAD_CIPHER_HASH;
92                         else
93                                 return AESNI_MB_OP_AEAD_HASH_CIPHER;
94                 }
95         }
96
97         return AESNI_MB_OP_NOT_SUPPORTED;
98 }
99
100 /** Set session authentication parameters */
101 static int
102 aesni_mb_set_session_auth_parameters(const struct aesni_mb_op_fns *mb_ops,
103                 struct aesni_mb_session *sess,
104                 const struct rte_crypto_sym_xform *xform)
105 {
106         hash_one_block_t hash_oneblock_fn;
107         unsigned int key_larger_block_size = 0;
108         uint8_t hashed_key[HMAC_MAX_BLOCK_SIZE] = { 0 };
109
110         if (xform == NULL) {
111                 sess->auth.algo = NULL_HASH;
112                 return 0;
113         }
114
115         if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
116                 AESNI_MB_LOG(ERR, "Crypto xform struct not of type auth");
117                 return -1;
118         }
119
120         /* Set the request digest size */
121         sess->auth.req_digest_len = xform->auth.digest_length;
122
123         /* Select auth generate/verify */
124         sess->auth.operation = xform->auth.op;
125
126         /* Set Authentication Parameters */
127         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
128                 sess->auth.algo = AES_XCBC;
129
130                 uint16_t xcbc_mac_digest_len =
131                         get_truncated_digest_byte_length(AES_XCBC);
132                 if (sess->auth.req_digest_len != xcbc_mac_digest_len) {
133                         AESNI_MB_LOG(ERR, "Invalid digest size\n");
134                         return -EINVAL;
135                 }
136                 sess->auth.gen_digest_len = sess->auth.req_digest_len;
137                 (*mb_ops->aux.keyexp.aes_xcbc)(xform->auth.key.data,
138                                 sess->auth.xcbc.k1_expanded,
139                                 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
140                 return 0;
141         }
142
143         if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_CMAC) {
144                 sess->auth.algo = AES_CMAC;
145
146                 uint16_t cmac_digest_len = get_digest_byte_length(AES_CMAC);
147
148                 if (sess->auth.req_digest_len > cmac_digest_len) {
149                         AESNI_MB_LOG(ERR, "Invalid digest size\n");
150                         return -EINVAL;
151                 }
152                 /*
153                  * Multi-buffer lib supports digest sizes from 4 to 16 bytes
154                  * in version 0.50 and sizes of 12 and 16 bytes,
155                  * in version 0.49.
156                  * If size requested is different, generate the full digest
157                  * (16 bytes) in a temporary location and then memcpy
158                  * the requested number of bytes.
159                  */
160 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
161                 if (sess->auth.req_digest_len < 4)
162 #else
163                 uint16_t cmac_trunc_digest_len =
164                                 get_truncated_digest_byte_length(AES_CMAC);
165                 if (sess->auth.req_digest_len != cmac_digest_len &&
166                                 sess->auth.req_digest_len != cmac_trunc_digest_len)
167 #endif
168                         sess->auth.gen_digest_len = cmac_digest_len;
169                 else
170                         sess->auth.gen_digest_len = sess->auth.req_digest_len;
171                 (*mb_ops->aux.keyexp.aes_cmac_expkey)(xform->auth.key.data,
172                                 sess->auth.cmac.expkey);
173
174                 (*mb_ops->aux.keyexp.aes_cmac_subkey)(sess->auth.cmac.expkey,
175                                 sess->auth.cmac.skey1, sess->auth.cmac.skey2);
176                 return 0;
177         }
178
179         switch (xform->auth.algo) {
180         case RTE_CRYPTO_AUTH_MD5_HMAC:
181                 sess->auth.algo = MD5;
182                 hash_oneblock_fn = mb_ops->aux.one_block.md5;
183                 break;
184         case RTE_CRYPTO_AUTH_SHA1_HMAC:
185                 sess->auth.algo = SHA1;
186                 hash_oneblock_fn = mb_ops->aux.one_block.sha1;
187 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
188                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA1)) {
189                         mb_ops->aux.multi_block.sha1(
190                                 xform->auth.key.data,
191                                 xform->auth.key.length,
192                                 hashed_key);
193                         key_larger_block_size = 1;
194                 }
195 #endif
196                 break;
197         case RTE_CRYPTO_AUTH_SHA224_HMAC:
198                 sess->auth.algo = SHA_224;
199                 hash_oneblock_fn = mb_ops->aux.one_block.sha224;
200 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
201                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_224)) {
202                         mb_ops->aux.multi_block.sha224(
203                                 xform->auth.key.data,
204                                 xform->auth.key.length,
205                                 hashed_key);
206                         key_larger_block_size = 1;
207                 }
208 #endif
209                 break;
210         case RTE_CRYPTO_AUTH_SHA256_HMAC:
211                 sess->auth.algo = SHA_256;
212                 hash_oneblock_fn = mb_ops->aux.one_block.sha256;
213 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
214                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_256)) {
215                         mb_ops->aux.multi_block.sha256(
216                                 xform->auth.key.data,
217                                 xform->auth.key.length,
218                                 hashed_key);
219                         key_larger_block_size = 1;
220                 }
221 #endif
222                 break;
223         case RTE_CRYPTO_AUTH_SHA384_HMAC:
224                 sess->auth.algo = SHA_384;
225                 hash_oneblock_fn = mb_ops->aux.one_block.sha384;
226 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
227                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_384)) {
228                         mb_ops->aux.multi_block.sha384(
229                                 xform->auth.key.data,
230                                 xform->auth.key.length,
231                                 hashed_key);
232                         key_larger_block_size = 1;
233                 }
234 #endif
235                 break;
236         case RTE_CRYPTO_AUTH_SHA512_HMAC:
237                 sess->auth.algo = SHA_512;
238                 hash_oneblock_fn = mb_ops->aux.one_block.sha512;
239 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
240                 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_512)) {
241                         mb_ops->aux.multi_block.sha512(
242                                 xform->auth.key.data,
243                                 xform->auth.key.length,
244                                 hashed_key);
245                         key_larger_block_size = 1;
246                 }
247 #endif
248                 break;
249         default:
250                 AESNI_MB_LOG(ERR, "Unsupported authentication algorithm selection");
251                 return -ENOTSUP;
252         }
253         uint16_t trunc_digest_size =
254                         get_truncated_digest_byte_length(sess->auth.algo);
255         uint16_t full_digest_size =
256                         get_digest_byte_length(sess->auth.algo);
257
258 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
259         if (sess->auth.req_digest_len > full_digest_size ||
260                         sess->auth.req_digest_len == 0) {
261 #else
262         if (sess->auth.req_digest_len != trunc_digest_size) {
263 #endif
264                 AESNI_MB_LOG(ERR, "Invalid digest size\n");
265                 return -EINVAL;
266         }
267
268         if (sess->auth.req_digest_len != trunc_digest_size &&
269                         sess->auth.req_digest_len != full_digest_size)
270                 sess->auth.gen_digest_len = full_digest_size;
271         else
272                 sess->auth.gen_digest_len = sess->auth.req_digest_len;
273
274         /* Calculate Authentication precomputes */
275         if (key_larger_block_size) {
276                 calculate_auth_precomputes(hash_oneblock_fn,
277                         sess->auth.pads.inner, sess->auth.pads.outer,
278                         hashed_key,
279                         xform->auth.key.length,
280                         get_auth_algo_blocksize(sess->auth.algo));
281         } else {
282                 calculate_auth_precomputes(hash_oneblock_fn,
283                         sess->auth.pads.inner, sess->auth.pads.outer,
284                         xform->auth.key.data,
285                         xform->auth.key.length,
286                         get_auth_algo_blocksize(sess->auth.algo));
287         }
288
289         return 0;
290 }
291
292 /** Set session cipher parameters */
293 static int
294 aesni_mb_set_session_cipher_parameters(const struct aesni_mb_op_fns *mb_ops,
295                 struct aesni_mb_session *sess,
296                 const struct rte_crypto_sym_xform *xform)
297 {
298         uint8_t is_aes = 0;
299         uint8_t is_3DES = 0;
300         aes_keyexp_t aes_keyexp_fn;
301
302         if (xform == NULL) {
303                 sess->cipher.mode = NULL_CIPHER;
304                 return 0;
305         }
306
307         if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
308                 AESNI_MB_LOG(ERR, "Crypto xform struct not of type cipher");
309                 return -EINVAL;
310         }
311
312         /* Select cipher direction */
313         switch (xform->cipher.op) {
314         case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
315                 sess->cipher.direction = ENCRYPT;
316                 break;
317         case RTE_CRYPTO_CIPHER_OP_DECRYPT:
318                 sess->cipher.direction = DECRYPT;
319                 break;
320         default:
321                 AESNI_MB_LOG(ERR, "Invalid cipher operation parameter");
322                 return -EINVAL;
323         }
324
325         /* Select cipher mode */
326         switch (xform->cipher.algo) {
327         case RTE_CRYPTO_CIPHER_AES_CBC:
328                 sess->cipher.mode = CBC;
329                 is_aes = 1;
330                 break;
331         case RTE_CRYPTO_CIPHER_AES_CTR:
332                 sess->cipher.mode = CNTR;
333                 is_aes = 1;
334                 break;
335         case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
336                 sess->cipher.mode = DOCSIS_SEC_BPI;
337                 is_aes = 1;
338                 break;
339         case RTE_CRYPTO_CIPHER_DES_CBC:
340                 sess->cipher.mode = DES;
341                 break;
342         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
343                 sess->cipher.mode = DOCSIS_DES;
344                 break;
345         case RTE_CRYPTO_CIPHER_3DES_CBC:
346                 sess->cipher.mode = DES3;
347                 is_3DES = 1;
348                 break;
349         default:
350                 AESNI_MB_LOG(ERR, "Unsupported cipher mode parameter");
351                 return -ENOTSUP;
352         }
353
354         /* Set IV parameters */
355         sess->iv.offset = xform->cipher.iv.offset;
356         sess->iv.length = xform->cipher.iv.length;
357
358         /* Check key length and choose key expansion function for AES */
359         if (is_aes) {
360                 switch (xform->cipher.key.length) {
361                 case AES_128_BYTES:
362                         sess->cipher.key_length_in_bytes = AES_128_BYTES;
363                         aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
364                         break;
365                 case AES_192_BYTES:
366                         sess->cipher.key_length_in_bytes = AES_192_BYTES;
367                         aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
368                         break;
369                 case AES_256_BYTES:
370                         sess->cipher.key_length_in_bytes = AES_256_BYTES;
371                         aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
372                         break;
373                 default:
374                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
375                         return -EINVAL;
376                 }
377
378                 /* Expanded cipher keys */
379                 (*aes_keyexp_fn)(xform->cipher.key.data,
380                                 sess->cipher.expanded_aes_keys.encode,
381                                 sess->cipher.expanded_aes_keys.decode);
382
383         } else if (is_3DES) {
384                 uint64_t *keys[3] = {sess->cipher.exp_3des_keys.key[0],
385                                 sess->cipher.exp_3des_keys.key[1],
386                                 sess->cipher.exp_3des_keys.key[2]};
387
388                 switch (xform->cipher.key.length) {
389                 case  24:
390                         des_key_schedule(keys[0], xform->cipher.key.data);
391                         des_key_schedule(keys[1], xform->cipher.key.data+8);
392                         des_key_schedule(keys[2], xform->cipher.key.data+16);
393
394                         /* Initialize keys - 24 bytes: [K1-K2-K3] */
395                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
396                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
397                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[2];
398                         break;
399                 case 16:
400                         des_key_schedule(keys[0], xform->cipher.key.data);
401                         des_key_schedule(keys[1], xform->cipher.key.data+8);
402
403                         /* Initialize keys - 16 bytes: [K1=K1,K2=K2,K3=K1] */
404                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
405                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
406                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
407                         break;
408                 case 8:
409                         des_key_schedule(keys[0], xform->cipher.key.data);
410
411                         /* Initialize keys - 8 bytes: [K1 = K2 = K3] */
412                         sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
413                         sess->cipher.exp_3des_keys.ks_ptr[1] = keys[0];
414                         sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
415                         break;
416                 default:
417                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
418                         return -EINVAL;
419                 }
420
421 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
422                 sess->cipher.key_length_in_bytes = 24;
423 #else
424                 sess->cipher.key_length_in_bytes = 8;
425 #endif
426         } else {
427                 if (xform->cipher.key.length != 8) {
428                         AESNI_MB_LOG(ERR, "Invalid cipher key length");
429                         return -EINVAL;
430                 }
431                 sess->cipher.key_length_in_bytes = 8;
432
433                 des_key_schedule((uint64_t *)sess->cipher.expanded_aes_keys.encode,
434                                 xform->cipher.key.data);
435                 des_key_schedule((uint64_t *)sess->cipher.expanded_aes_keys.decode,
436                                 xform->cipher.key.data);
437         }
438
439         return 0;
440 }
441
442 static int
443 aesni_mb_set_session_aead_parameters(const struct aesni_mb_op_fns *mb_ops,
444                 struct aesni_mb_session *sess,
445                 const struct rte_crypto_sym_xform *xform)
446 {
447         aes_keyexp_t aes_keyexp_fn;
448
449         switch (xform->aead.op) {
450         case RTE_CRYPTO_AEAD_OP_ENCRYPT:
451                 sess->cipher.direction = ENCRYPT;
452                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
453                 break;
454         case RTE_CRYPTO_AEAD_OP_DECRYPT:
455                 sess->cipher.direction = DECRYPT;
456                 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
457                 break;
458         default:
459                 AESNI_MB_LOG(ERR, "Invalid aead operation parameter");
460                 return -EINVAL;
461         }
462
463         switch (xform->aead.algo) {
464         case RTE_CRYPTO_AEAD_AES_CCM:
465                 sess->cipher.mode = CCM;
466                 sess->auth.algo = AES_CCM;
467                 break;
468         default:
469                 AESNI_MB_LOG(ERR, "Unsupported aead mode parameter");
470                 return -ENOTSUP;
471         }
472
473         /* Set IV parameters */
474         sess->iv.offset = xform->aead.iv.offset;
475         sess->iv.length = xform->aead.iv.length;
476
477         sess->auth.req_digest_len = xform->aead.digest_length;
478         /* CCM digests must be between 4 and 16 and an even number */
479         if (sess->auth.req_digest_len < AES_CCM_DIGEST_MIN_LEN ||
480                         sess->auth.req_digest_len > AES_CCM_DIGEST_MAX_LEN ||
481                         (sess->auth.req_digest_len & 1) == 1) {
482                 AESNI_MB_LOG(ERR, "Invalid digest size\n");
483                 return -EINVAL;
484         }
485         sess->auth.gen_digest_len = sess->auth.req_digest_len;
486
487         /* Check key length and choose key expansion function for AES */
488
489         switch (xform->aead.key.length) {
490         case AES_128_BYTES:
491                 sess->cipher.key_length_in_bytes = AES_128_BYTES;
492                 aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
493                 break;
494         default:
495                 AESNI_MB_LOG(ERR, "Invalid cipher key length");
496                 return -EINVAL;
497         }
498
499         /* Expanded cipher keys */
500         (*aes_keyexp_fn)(xform->aead.key.data,
501                         sess->cipher.expanded_aes_keys.encode,
502                         sess->cipher.expanded_aes_keys.decode);
503
504         return 0;
505 }
506
507 /** Parse crypto xform chain and set private session parameters */
508 int
509 aesni_mb_set_session_parameters(const struct aesni_mb_op_fns *mb_ops,
510                 struct aesni_mb_session *sess,
511                 const struct rte_crypto_sym_xform *xform)
512 {
513         const struct rte_crypto_sym_xform *auth_xform = NULL;
514         const struct rte_crypto_sym_xform *cipher_xform = NULL;
515         const struct rte_crypto_sym_xform *aead_xform = NULL;
516         int ret;
517
518         /* Select Crypto operation - hash then cipher / cipher then hash */
519         switch (aesni_mb_get_chain_order(xform)) {
520         case AESNI_MB_OP_HASH_CIPHER:
521                 sess->chain_order = HASH_CIPHER;
522                 auth_xform = xform;
523                 cipher_xform = xform->next;
524                 break;
525         case AESNI_MB_OP_CIPHER_HASH:
526                 sess->chain_order = CIPHER_HASH;
527                 auth_xform = xform->next;
528                 cipher_xform = xform;
529                 break;
530         case AESNI_MB_OP_HASH_ONLY:
531                 sess->chain_order = HASH_CIPHER;
532                 auth_xform = xform;
533                 cipher_xform = NULL;
534                 break;
535         case AESNI_MB_OP_CIPHER_ONLY:
536                 /*
537                  * Multi buffer library operates only at two modes,
538                  * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
539                  * chain order depends on cipher operation: encryption is always
540                  * the first operation and decryption the last one.
541                  */
542                 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
543                         sess->chain_order = CIPHER_HASH;
544                 else
545                         sess->chain_order = HASH_CIPHER;
546                 auth_xform = NULL;
547                 cipher_xform = xform;
548                 break;
549         case AESNI_MB_OP_AEAD_CIPHER_HASH:
550                 sess->chain_order = CIPHER_HASH;
551                 sess->aead.aad_len = xform->aead.aad_length;
552                 aead_xform = xform;
553                 break;
554         case AESNI_MB_OP_AEAD_HASH_CIPHER:
555                 sess->chain_order = HASH_CIPHER;
556                 sess->aead.aad_len = xform->aead.aad_length;
557                 aead_xform = xform;
558                 break;
559         case AESNI_MB_OP_NOT_SUPPORTED:
560         default:
561                 AESNI_MB_LOG(ERR, "Unsupported operation chain order parameter");
562                 return -ENOTSUP;
563         }
564
565         /* Default IV length = 0 */
566         sess->iv.length = 0;
567
568         ret = aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform);
569         if (ret != 0) {
570                 AESNI_MB_LOG(ERR, "Invalid/unsupported authentication parameters");
571                 return ret;
572         }
573
574         ret = aesni_mb_set_session_cipher_parameters(mb_ops, sess,
575                         cipher_xform);
576         if (ret != 0) {
577                 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
578                 return ret;
579         }
580
581         if (aead_xform) {
582                 ret = aesni_mb_set_session_aead_parameters(mb_ops, sess,
583                                 aead_xform);
584                 if (ret != 0) {
585                         AESNI_MB_LOG(ERR, "Invalid/unsupported aead parameters");
586                         return ret;
587                 }
588         }
589
590         return 0;
591 }
592
593 /**
594  * burst enqueue, place crypto operations on ingress queue for processing.
595  *
596  * @param __qp         Queue Pair to process
597  * @param ops          Crypto operations for processing
598  * @param nb_ops       Number of crypto operations for processing
599  *
600  * @return
601  * - Number of crypto operations enqueued
602  */
603 static uint16_t
604 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
605                 uint16_t nb_ops)
606 {
607         struct aesni_mb_qp *qp = __qp;
608
609         unsigned int nb_enqueued;
610
611         nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
612                         (void **)ops, nb_ops, NULL);
613
614         qp->stats.enqueued_count += nb_enqueued;
615
616         return nb_enqueued;
617 }
618
619 /** Get multi buffer session */
620 static inline struct aesni_mb_session *
621 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
622 {
623         struct aesni_mb_session *sess = NULL;
624
625         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
626                 if (likely(op->sym->session != NULL))
627                         sess = (struct aesni_mb_session *)
628                                         get_sym_session_private_data(
629                                         op->sym->session,
630                                         cryptodev_driver_id);
631         } else {
632                 void *_sess = NULL;
633                 void *_sess_private_data = NULL;
634
635                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
636                         return NULL;
637
638                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
639                         return NULL;
640
641                 sess = (struct aesni_mb_session *)_sess_private_data;
642
643                 if (unlikely(aesni_mb_set_session_parameters(qp->op_fns,
644                                 sess, op->sym->xform) != 0)) {
645                         rte_mempool_put(qp->sess_mp, _sess);
646                         rte_mempool_put(qp->sess_mp, _sess_private_data);
647                         sess = NULL;
648                 }
649                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
650                 set_sym_session_private_data(op->sym->session,
651                                 cryptodev_driver_id, _sess_private_data);
652         }
653
654         if (unlikely(sess == NULL))
655                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
656
657         return sess;
658 }
659
660 /**
661  * Process a crypto operation and complete a JOB_AES_HMAC job structure for
662  * submission to the multi buffer library for processing.
663  *
664  * @param       qp      queue pair
665  * @param       job     JOB_AES_HMAC structure to fill
666  * @param       m       mbuf to process
667  *
668  * @return
669  * - Completed JOB_AES_HMAC structure pointer on success
670  * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
671  */
672 static inline int
673 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
674                 struct rte_crypto_op *op, uint8_t *digest_idx)
675 {
676         struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
677         struct aesni_mb_session *session;
678         uint16_t m_offset = 0;
679
680         session = get_session(qp, op);
681         if (session == NULL) {
682                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
683                 return -1;
684         }
685
686         /* Set crypto operation */
687         job->chain_order = session->chain_order;
688
689         /* Set cipher parameters */
690         job->cipher_direction = session->cipher.direction;
691         job->cipher_mode = session->cipher.mode;
692
693         job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
694
695         if (job->cipher_mode == DES3) {
696                 job->aes_enc_key_expanded =
697                         session->cipher.exp_3des_keys.ks_ptr;
698                 job->aes_dec_key_expanded =
699                         session->cipher.exp_3des_keys.ks_ptr;
700         } else {
701                 job->aes_enc_key_expanded =
702                         session->cipher.expanded_aes_keys.encode;
703                 job->aes_dec_key_expanded =
704                         session->cipher.expanded_aes_keys.decode;
705         }
706
707
708
709
710         /* Set authentication parameters */
711         job->hash_alg = session->auth.algo;
712         if (job->hash_alg == AES_XCBC) {
713                 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
714                 job->u.XCBC._k2 = session->auth.xcbc.k2;
715                 job->u.XCBC._k3 = session->auth.xcbc.k3;
716         } else if (job->hash_alg == AES_CCM) {
717                 job->u.CCM.aad = op->sym->aead.aad.data + 18;
718                 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
719         } else if (job->hash_alg == AES_CMAC) {
720                 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
721                 job->u.CMAC._skey1 = session->auth.cmac.skey1;
722                 job->u.CMAC._skey2 = session->auth.cmac.skey2;
723
724         } else {
725                 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
726                 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
727         }
728
729         /* Mutable crypto operation parameters */
730         if (op->sym->m_dst) {
731                 m_src = m_dst = op->sym->m_dst;
732
733                 /* append space for output data to mbuf */
734                 char *odata = rte_pktmbuf_append(m_dst,
735                                 rte_pktmbuf_data_len(op->sym->m_src));
736                 if (odata == NULL) {
737                         AESNI_MB_LOG(ERR, "failed to allocate space in destination "
738                                         "mbuf for source data");
739                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
740                         return -1;
741                 }
742
743                 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
744                                 rte_pktmbuf_data_len(op->sym->m_src));
745         } else {
746                 m_dst = m_src;
747                 if (job->hash_alg == AES_CCM)
748                         m_offset = op->sym->aead.data.offset;
749                 else
750                         m_offset = op->sym->cipher.data.offset;
751         }
752
753         /* Set digest output location */
754         if (job->hash_alg != NULL_HASH &&
755                         session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
756                 job->auth_tag_output = qp->temp_digests[*digest_idx];
757                 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
758         } else {
759                 if (job->hash_alg == AES_CCM)
760                         job->auth_tag_output = op->sym->aead.digest.data;
761                 else
762                         job->auth_tag_output = op->sym->auth.digest.data;
763
764                 if (session->auth.req_digest_len != session->auth.gen_digest_len) {
765                         job->auth_tag_output = qp->temp_digests[*digest_idx];
766                         *digest_idx = (*digest_idx + 1) % MAX_JOBS;
767                 }
768         }
769
770         /* Set digest length */
771         job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
772
773         /* Set IV parameters */
774         job->iv_len_in_bytes = session->iv.length;
775
776         /* Data  Parameter */
777         job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
778         job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
779
780         if (job->hash_alg == AES_CCM) {
781                 job->cipher_start_src_offset_in_bytes =
782                                 op->sym->aead.data.offset;
783                 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
784                 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
785                 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
786
787                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
788                         session->iv.offset + 1);
789         } else {
790                 job->cipher_start_src_offset_in_bytes =
791                                 op->sym->cipher.data.offset;
792                 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
793
794                 job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
795                 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
796
797                 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
798                         session->iv.offset);
799         }
800
801         /* Set user data to be crypto operation data struct */
802         job->user_data = op;
803
804         return 0;
805 }
806
807 static inline void
808 verify_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
809                 struct aesni_mb_session *sess)
810 {
811         /* Verify digest if required */
812         if (job->hash_alg == AES_CCM) {
813                 if (memcmp(job->auth_tag_output, op->sym->aead.digest.data,
814                                 sess->auth.req_digest_len) != 0)
815                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
816         } else {
817                 if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
818                                 sess->auth.req_digest_len) != 0)
819                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
820         }
821 }
822
823 static inline void
824 generate_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
825                 struct aesni_mb_session *sess)
826 {
827         /* No extra copy neeed */
828         if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
829                 return;
830
831         /*
832          * This can only happen for HMAC, so only digest
833          * for authentication algos is required
834          */
835         memcpy(op->sym->auth.digest.data, job->auth_tag_output,
836                         sess->auth.req_digest_len);
837 }
838
839 /**
840  * Process a completed job and return rte_mbuf which job processed
841  *
842  * @param qp            Queue Pair to process
843  * @param job   JOB_AES_HMAC job to process
844  *
845  * @return
846  * - Returns processed crypto operation.
847  * - Returns NULL on invalid job
848  */
849 static inline struct rte_crypto_op *
850 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
851 {
852         struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
853         struct aesni_mb_session *sess = get_sym_session_private_data(
854                                                         op->sym->session,
855                                                         cryptodev_driver_id);
856
857         if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
858                 switch (job->status) {
859                 case STS_COMPLETED:
860                         op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
861
862                         if (job->hash_alg != NULL_HASH) {
863                                 if (sess->auth.operation ==
864                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
865                                         verify_digest(job, op, sess);
866                                 else
867                                         generate_digest(job, op, sess);
868                         }
869                         break;
870                 default:
871                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
872                 }
873         }
874
875         /* Free session if a session-less crypto op */
876         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
877                 memset(sess, 0, sizeof(struct aesni_mb_session));
878                 memset(op->sym->session, 0,
879                                 rte_cryptodev_sym_get_header_session_size());
880                 rte_mempool_put(qp->sess_mp, sess);
881                 rte_mempool_put(qp->sess_mp, op->sym->session);
882                 op->sym->session = NULL;
883         }
884
885         return op;
886 }
887
888 /**
889  * Process a completed JOB_AES_HMAC job and keep processing jobs until
890  * get_completed_job return NULL
891  *
892  * @param qp            Queue Pair to process
893  * @param job           JOB_AES_HMAC job
894  *
895  * @return
896  * - Number of processed jobs
897  */
898 static unsigned
899 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
900                 struct rte_crypto_op **ops, uint16_t nb_ops)
901 {
902         struct rte_crypto_op *op = NULL;
903         unsigned processed_jobs = 0;
904
905         while (job != NULL) {
906                 op = post_process_mb_job(qp, job);
907
908                 if (op) {
909                         ops[processed_jobs++] = op;
910                         qp->stats.dequeued_count++;
911                 } else {
912                         qp->stats.dequeue_err_count++;
913                         break;
914                 }
915                 if (processed_jobs == nb_ops)
916                         break;
917
918                 job = (*qp->op_fns->job.get_completed_job)(qp->mb_mgr);
919         }
920
921         return processed_jobs;
922 }
923
924 static inline uint16_t
925 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
926                 uint16_t nb_ops)
927 {
928         int processed_ops = 0;
929
930         /* Flush the remaining jobs */
931         JOB_AES_HMAC *job = (*qp->op_fns->job.flush_job)(qp->mb_mgr);
932
933         if (job)
934                 processed_ops += handle_completed_jobs(qp, job,
935                                 &ops[processed_ops], nb_ops - processed_ops);
936
937         return processed_ops;
938 }
939
940 static inline JOB_AES_HMAC *
941 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
942 {
943         job->chain_order = HASH_CIPHER;
944         job->cipher_mode = NULL_CIPHER;
945         job->hash_alg = NULL_HASH;
946         job->cipher_direction = DECRYPT;
947
948         /* Set user data to be crypto operation data struct */
949         job->user_data = op;
950
951         return job;
952 }
953
954 static uint16_t
955 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
956                 uint16_t nb_ops)
957 {
958         struct aesni_mb_qp *qp = queue_pair;
959
960         struct rte_crypto_op *op;
961         JOB_AES_HMAC *job;
962
963         int retval, processed_jobs = 0;
964
965         if (unlikely(nb_ops == 0))
966                 return 0;
967
968         uint8_t digest_idx = qp->digest_idx;
969         do {
970                 /* Get next free mb job struct from mb manager */
971                 job = (*qp->op_fns->job.get_next)(qp->mb_mgr);
972                 if (unlikely(job == NULL)) {
973                         /* if no free mb job structs we need to flush mb_mgr */
974                         processed_jobs += flush_mb_mgr(qp,
975                                         &ops[processed_jobs],
976                                         nb_ops - processed_jobs);
977
978                         if (nb_ops == processed_jobs)
979                                 break;
980
981                         job = (*qp->op_fns->job.get_next)(qp->mb_mgr);
982                 }
983
984                 /*
985                  * Get next operation to process from ingress queue.
986                  * There is no need to return the job to the MB_MGR
987                  * if there are no more operations to process, since the MB_MGR
988                  * can use that pointer again in next get_next calls.
989                  */
990                 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
991                 if (retval < 0)
992                         break;
993
994                 retval = set_mb_job_params(job, qp, op, &digest_idx);
995                 if (unlikely(retval != 0)) {
996                         qp->stats.dequeue_err_count++;
997                         set_job_null_op(job, op);
998                 }
999
1000                 /* Submit job to multi-buffer for processing */
1001                 job = (*qp->op_fns->job.submit)(qp->mb_mgr);
1002
1003                 /*
1004                  * If submit returns a processed job then handle it,
1005                  * before submitting subsequent jobs
1006                  */
1007                 if (job)
1008                         processed_jobs += handle_completed_jobs(qp, job,
1009                                         &ops[processed_jobs],
1010                                         nb_ops - processed_jobs);
1011
1012         } while (processed_jobs < nb_ops);
1013
1014         qp->digest_idx = digest_idx;
1015
1016         if (processed_jobs < 1)
1017                 processed_jobs += flush_mb_mgr(qp,
1018                                 &ops[processed_jobs],
1019                                 nb_ops - processed_jobs);
1020
1021         return processed_jobs;
1022 }
1023
1024 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
1025
1026 static int
1027 cryptodev_aesni_mb_create(const char *name,
1028                         struct rte_vdev_device *vdev,
1029                         struct rte_cryptodev_pmd_init_params *init_params)
1030 {
1031         struct rte_cryptodev *dev;
1032         struct aesni_mb_private *internals;
1033         enum aesni_mb_vector_mode vector_mode;
1034
1035         /* Check CPU for support for AES instruction set */
1036         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
1037                 AESNI_MB_LOG(ERR, "AES instructions not supported by CPU");
1038                 return -EFAULT;
1039         }
1040
1041         dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
1042         if (dev == NULL) {
1043                 AESNI_MB_LOG(ERR, "failed to create cryptodev vdev");
1044                 return -ENODEV;
1045         }
1046
1047         /* Check CPU for supported vector instruction set */
1048         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
1049                 vector_mode = RTE_AESNI_MB_AVX512;
1050         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
1051                 vector_mode = RTE_AESNI_MB_AVX2;
1052         else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
1053                 vector_mode = RTE_AESNI_MB_AVX;
1054         else
1055                 vector_mode = RTE_AESNI_MB_SSE;
1056
1057         dev->driver_id = cryptodev_driver_id;
1058         dev->dev_ops = rte_aesni_mb_pmd_ops;
1059
1060         /* register rx/tx burst functions for data path */
1061         dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
1062         dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
1063
1064         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1065                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1066                         RTE_CRYPTODEV_FF_CPU_AESNI;
1067
1068         switch (vector_mode) {
1069         case RTE_AESNI_MB_SSE:
1070                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
1071                 break;
1072         case RTE_AESNI_MB_AVX:
1073                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
1074                 break;
1075         case RTE_AESNI_MB_AVX2:
1076                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
1077                 break;
1078         case RTE_AESNI_MB_AVX512:
1079                 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX512;
1080                 break;
1081         default:
1082                 break;
1083         }
1084
1085         /* Set vector instructions mode supported */
1086         internals = dev->data->dev_private;
1087
1088         internals->vector_mode = vector_mode;
1089         internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
1090
1091 #if IMB_VERSION_NUM >= IMB_VERSION(0, 50, 0)
1092         AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: %s\n",
1093                         imb_get_version_str());
1094 #else
1095         AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: 0.49.0\n");
1096 #endif
1097
1098         return 0;
1099 }
1100
1101 static int
1102 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
1103 {
1104         struct rte_cryptodev_pmd_init_params init_params = {
1105                 "",
1106                 sizeof(struct aesni_mb_private),
1107                 rte_socket_id(),
1108                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
1109         };
1110         const char *name, *args;
1111         int retval;
1112
1113         name = rte_vdev_device_name(vdev);
1114         if (name == NULL)
1115                 return -EINVAL;
1116
1117         args = rte_vdev_device_args(vdev);
1118
1119         retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
1120         if (retval) {
1121                 AESNI_MB_LOG(ERR, "Failed to parse initialisation arguments[%s]",
1122                                 args);
1123                 return -EINVAL;
1124         }
1125
1126         return cryptodev_aesni_mb_create(name, vdev, &init_params);
1127 }
1128
1129 static int
1130 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
1131 {
1132         struct rte_cryptodev *cryptodev;
1133         const char *name;
1134
1135         name = rte_vdev_device_name(vdev);
1136         if (name == NULL)
1137                 return -EINVAL;
1138
1139         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
1140         if (cryptodev == NULL)
1141                 return -ENODEV;
1142
1143         return rte_cryptodev_pmd_destroy(cryptodev);
1144 }
1145
1146 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
1147         .probe = cryptodev_aesni_mb_probe,
1148         .remove = cryptodev_aesni_mb_remove
1149 };
1150
1151 static struct cryptodev_driver aesni_mb_crypto_drv;
1152
1153 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
1154 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
1155 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
1156         "max_nb_queue_pairs=<int> "
1157         "socket_id=<int>");
1158 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
1159                 cryptodev_aesni_mb_pmd_drv.driver,
1160                 cryptodev_driver_id);
1161
1162 RTE_INIT(aesni_mb_init_log)
1163 {
1164         aesni_mb_logtype_driver = rte_log_register("pmd.crypto.aesni_mb");
1165 }