cryptodev: remove digest length from crypto op
[dpdk.git] / drivers / crypto / openssl / rte_openssl_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
37 #include <rte_cryptodev_vdev.h>
38 #include <rte_vdev.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
41
42 #include <openssl/evp.h>
43
44 #include "rte_openssl_pmd_private.h"
45
46 #define DES_BLOCK_SIZE 8
47
48 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
49
50 /*----------------------------------------------------------------------------*/
51
52 /**
53  * Increment counter by 1
54  * Counter is 64 bit array, big-endian
55  */
56 static void
57 ctr_inc(uint8_t *ctr)
58 {
59         uint64_t *ctr64 = (uint64_t *)ctr;
60
61         *ctr64 = __builtin_bswap64(*ctr64);
62         (*ctr64)++;
63         *ctr64 = __builtin_bswap64(*ctr64);
64 }
65
66 /*
67  *------------------------------------------------------------------------------
68  * Session Prepare
69  *------------------------------------------------------------------------------
70  */
71
72 /** Get xform chain order */
73 static enum openssl_chain_order
74 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
75 {
76         enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
77
78         if (xform != NULL) {
79                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
80                         if (xform->next == NULL)
81                                 res =  OPENSSL_CHAIN_ONLY_AUTH;
82                         else if (xform->next->type ==
83                                         RTE_CRYPTO_SYM_XFORM_CIPHER)
84                                 res =  OPENSSL_CHAIN_AUTH_CIPHER;
85                 }
86                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
87                         if (xform->next == NULL)
88                                 res =  OPENSSL_CHAIN_ONLY_CIPHER;
89                         else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
90                                 res =  OPENSSL_CHAIN_CIPHER_AUTH;
91                 }
92         }
93
94         return res;
95 }
96
97 /** Get session cipher key from input cipher key */
98 static void
99 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
100 {
101         memcpy(session_key, input_key, keylen);
102 }
103
104 /** Get key ede 24 bytes standard from input key */
105 static int
106 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
107 {
108         int res = 0;
109
110         /* Initialize keys - 24 bytes: [key1-key2-key3] */
111         switch (keylen) {
112         case 24:
113                 memcpy(key_ede, key, 24);
114                 break;
115         case 16:
116                 /* K3 = K1 */
117                 memcpy(key_ede, key, 16);
118                 memcpy(key_ede + 16, key, 8);
119                 break;
120         case 8:
121                 /* K1 = K2 = K3 (DES compatibility) */
122                 memcpy(key_ede, key, 8);
123                 memcpy(key_ede + 8, key, 8);
124                 memcpy(key_ede + 16, key, 8);
125                 break;
126         default:
127                 OPENSSL_LOG_ERR("Unsupported key size");
128                 res = -EINVAL;
129         }
130
131         return res;
132 }
133
134 /** Get adequate openssl function for input cipher algorithm */
135 static uint8_t
136 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
137                 const EVP_CIPHER **algo)
138 {
139         int res = 0;
140
141         if (algo != NULL) {
142                 switch (sess_algo) {
143                 case RTE_CRYPTO_CIPHER_3DES_CBC:
144                         switch (keylen) {
145                         case 16:
146                                 *algo = EVP_des_ede_cbc();
147                                 break;
148                         case 24:
149                                 *algo = EVP_des_ede3_cbc();
150                                 break;
151                         default:
152                                 res = -EINVAL;
153                         }
154                         break;
155                 case RTE_CRYPTO_CIPHER_3DES_CTR:
156                         break;
157                 case RTE_CRYPTO_CIPHER_AES_CBC:
158                         switch (keylen) {
159                         case 16:
160                                 *algo = EVP_aes_128_cbc();
161                                 break;
162                         case 24:
163                                 *algo = EVP_aes_192_cbc();
164                                 break;
165                         case 32:
166                                 *algo = EVP_aes_256_cbc();
167                                 break;
168                         default:
169                                 res = -EINVAL;
170                         }
171                         break;
172                 case RTE_CRYPTO_CIPHER_AES_CTR:
173                         switch (keylen) {
174                         case 16:
175                                 *algo = EVP_aes_128_ctr();
176                                 break;
177                         case 24:
178                                 *algo = EVP_aes_192_ctr();
179                                 break;
180                         case 32:
181                                 *algo = EVP_aes_256_ctr();
182                                 break;
183                         default:
184                                 res = -EINVAL;
185                         }
186                         break;
187                 case RTE_CRYPTO_CIPHER_AES_GCM:
188                         switch (keylen) {
189                         case 16:
190                                 *algo = EVP_aes_128_gcm();
191                                 break;
192                         case 24:
193                                 *algo = EVP_aes_192_gcm();
194                                 break;
195                         case 32:
196                                 *algo = EVP_aes_256_gcm();
197                                 break;
198                         default:
199                                 res = -EINVAL;
200                         }
201                         break;
202                 default:
203                         res = -EINVAL;
204                         break;
205                 }
206         } else {
207                 res = -EINVAL;
208         }
209
210         return res;
211 }
212
213 /** Get adequate openssl function for input auth algorithm */
214 static uint8_t
215 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
216                 const EVP_MD **algo)
217 {
218         int res = 0;
219
220         if (algo != NULL) {
221                 switch (sessalgo) {
222                 case RTE_CRYPTO_AUTH_MD5:
223                 case RTE_CRYPTO_AUTH_MD5_HMAC:
224                         *algo = EVP_md5();
225                         break;
226                 case RTE_CRYPTO_AUTH_SHA1:
227                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
228                         *algo = EVP_sha1();
229                         break;
230                 case RTE_CRYPTO_AUTH_SHA224:
231                 case RTE_CRYPTO_AUTH_SHA224_HMAC:
232                         *algo = EVP_sha224();
233                         break;
234                 case RTE_CRYPTO_AUTH_SHA256:
235                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
236                         *algo = EVP_sha256();
237                         break;
238                 case RTE_CRYPTO_AUTH_SHA384:
239                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
240                         *algo = EVP_sha384();
241                         break;
242                 case RTE_CRYPTO_AUTH_SHA512:
243                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
244                         *algo = EVP_sha512();
245                         break;
246                 default:
247                         res = -EINVAL;
248                         break;
249                 }
250         } else {
251                 res = -EINVAL;
252         }
253
254         return res;
255 }
256
257 /** Set session cipher parameters */
258 static int
259 openssl_set_session_cipher_parameters(struct openssl_session *sess,
260                 const struct rte_crypto_sym_xform *xform)
261 {
262         /* Select cipher direction */
263         sess->cipher.direction = xform->cipher.op;
264         /* Select cipher key */
265         sess->cipher.key.length = xform->cipher.key.length;
266
267         /* Set IV parameters */
268         sess->iv.offset = xform->cipher.iv.offset;
269         sess->iv.length = xform->cipher.iv.length;
270
271         /* Select cipher algo */
272         switch (xform->cipher.algo) {
273         case RTE_CRYPTO_CIPHER_3DES_CBC:
274         case RTE_CRYPTO_CIPHER_AES_CBC:
275         case RTE_CRYPTO_CIPHER_AES_CTR:
276         case RTE_CRYPTO_CIPHER_AES_GCM:
277                 sess->cipher.mode = OPENSSL_CIPHER_LIB;
278                 sess->cipher.algo = xform->cipher.algo;
279                 sess->cipher.ctx = EVP_CIPHER_CTX_new();
280
281                 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
282                                 &sess->cipher.evp_algo) != 0)
283                         return -EINVAL;
284
285                 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
286                         sess->cipher.key.data);
287
288                 break;
289
290         case RTE_CRYPTO_CIPHER_3DES_CTR:
291                 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
292                 sess->cipher.ctx = EVP_CIPHER_CTX_new();
293
294                 if (get_cipher_key_ede(xform->cipher.key.data,
295                                 sess->cipher.key.length,
296                                 sess->cipher.key.data) != 0)
297                         return -EINVAL;
298                 break;
299         case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
300                 sess->cipher.algo = xform->cipher.algo;
301                 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
302                 sess->cipher.ctx = EVP_CIPHER_CTX_new();
303                 sess->cipher.evp_algo = EVP_des_cbc();
304
305                 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
306                 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
307                 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
308                                 NULL, xform->cipher.key.data, 0) != 1)
309                         return -EINVAL;
310
311                 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
312                         sess->cipher.key.data);
313                 break;
314         default:
315                 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
316                 return -EINVAL;
317         }
318
319         return 0;
320 }
321
322 /* Set session auth parameters */
323 static int
324 openssl_set_session_auth_parameters(struct openssl_session *sess,
325                 const struct rte_crypto_sym_xform *xform)
326 {
327         /* Select auth generate/verify */
328         sess->auth.operation = xform->auth.op;
329         sess->auth.algo = xform->auth.algo;
330
331         /* Select auth algo */
332         switch (xform->auth.algo) {
333         case RTE_CRYPTO_AUTH_AES_GMAC:
334         case RTE_CRYPTO_AUTH_AES_GCM:
335                 /* Check additional condition for AES_GMAC/GCM */
336                 if (sess->cipher.algo != RTE_CRYPTO_CIPHER_AES_GCM)
337                         return -EINVAL;
338                 sess->chain_order = OPENSSL_CHAIN_COMBINED;
339                 break;
340
341         case RTE_CRYPTO_AUTH_MD5:
342         case RTE_CRYPTO_AUTH_SHA1:
343         case RTE_CRYPTO_AUTH_SHA224:
344         case RTE_CRYPTO_AUTH_SHA256:
345         case RTE_CRYPTO_AUTH_SHA384:
346         case RTE_CRYPTO_AUTH_SHA512:
347                 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
348                 if (get_auth_algo(xform->auth.algo,
349                                 &sess->auth.auth.evp_algo) != 0)
350                         return -EINVAL;
351                 sess->auth.auth.ctx = EVP_MD_CTX_create();
352                 break;
353
354         case RTE_CRYPTO_AUTH_MD5_HMAC:
355         case RTE_CRYPTO_AUTH_SHA1_HMAC:
356         case RTE_CRYPTO_AUTH_SHA224_HMAC:
357         case RTE_CRYPTO_AUTH_SHA256_HMAC:
358         case RTE_CRYPTO_AUTH_SHA384_HMAC:
359         case RTE_CRYPTO_AUTH_SHA512_HMAC:
360                 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
361                 sess->auth.hmac.ctx = EVP_MD_CTX_create();
362                 if (get_auth_algo(xform->auth.algo,
363                                 &sess->auth.hmac.evp_algo) != 0)
364                         return -EINVAL;
365                 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
366                                 xform->auth.key.data, xform->auth.key.length);
367                 break;
368
369         default:
370                 return -EINVAL;
371         }
372
373         sess->auth.aad_length = xform->auth.add_auth_data_length;
374         sess->auth.digest_length = xform->auth.digest_length;
375
376         return 0;
377 }
378
379 /** Parse crypto xform chain and set private session parameters */
380 int
381 openssl_set_session_parameters(struct openssl_session *sess,
382                 const struct rte_crypto_sym_xform *xform)
383 {
384         const struct rte_crypto_sym_xform *cipher_xform = NULL;
385         const struct rte_crypto_sym_xform *auth_xform = NULL;
386
387         sess->chain_order = openssl_get_chain_order(xform);
388         switch (sess->chain_order) {
389         case OPENSSL_CHAIN_ONLY_CIPHER:
390                 cipher_xform = xform;
391                 break;
392         case OPENSSL_CHAIN_ONLY_AUTH:
393                 auth_xform = xform;
394                 break;
395         case OPENSSL_CHAIN_CIPHER_AUTH:
396                 cipher_xform = xform;
397                 auth_xform = xform->next;
398                 break;
399         case OPENSSL_CHAIN_AUTH_CIPHER:
400                 auth_xform = xform;
401                 cipher_xform = xform->next;
402                 break;
403         default:
404                 return -EINVAL;
405         }
406
407         /* Default IV length = 0 */
408         sess->iv.length = 0;
409
410         /* cipher_xform must be check before auth_xform */
411         if (cipher_xform) {
412                 if (openssl_set_session_cipher_parameters(
413                                 sess, cipher_xform)) {
414                         OPENSSL_LOG_ERR(
415                                 "Invalid/unsupported cipher parameters");
416                         return -EINVAL;
417                 }
418         }
419
420         if (auth_xform) {
421                 if (openssl_set_session_auth_parameters(sess, auth_xform)) {
422                         OPENSSL_LOG_ERR(
423                                 "Invalid/unsupported auth parameters");
424                         return -EINVAL;
425                 }
426         }
427
428         return 0;
429 }
430
431 /** Reset private session parameters */
432 void
433 openssl_reset_session(struct openssl_session *sess)
434 {
435         EVP_CIPHER_CTX_free(sess->cipher.ctx);
436
437         if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
438                 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
439
440         switch (sess->auth.mode) {
441         case OPENSSL_AUTH_AS_AUTH:
442                 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
443                 break;
444         case OPENSSL_AUTH_AS_HMAC:
445                 EVP_PKEY_free(sess->auth.hmac.pkey);
446                 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
447                 break;
448         default:
449                 break;
450         }
451 }
452
453 /** Provide session for operation */
454 static struct openssl_session *
455 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
456 {
457         struct openssl_session *sess = NULL;
458
459         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
460                 /* get existing session */
461                 if (likely(op->sym->session != NULL &&
462                                 op->sym->session->dev_type ==
463                                 RTE_CRYPTODEV_OPENSSL_PMD))
464                         sess = (struct openssl_session *)
465                                 op->sym->session->_private;
466         } else  {
467                 /* provide internal session */
468                 void *_sess = NULL;
469
470                 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
471                         sess = (struct openssl_session *)
472                                 ((struct rte_cryptodev_sym_session *)_sess)
473                                 ->_private;
474
475                         if (unlikely(openssl_set_session_parameters(
476                                         sess, op->sym->xform) != 0)) {
477                                 rte_mempool_put(qp->sess_mp, _sess);
478                                 sess = NULL;
479                         } else
480                                 op->sym->session = _sess;
481                 }
482         }
483
484         if (sess == NULL)
485                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
486
487         return sess;
488 }
489
490 /*
491  *------------------------------------------------------------------------------
492  * Process Operations
493  *------------------------------------------------------------------------------
494  */
495 static inline int
496 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
497                 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
498 {
499         struct rte_mbuf *m;
500         int dstlen;
501         int l, n = srclen;
502         uint8_t *src;
503
504         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
505                         m = m->next)
506                 offset -= rte_pktmbuf_data_len(m);
507
508         if (m == 0)
509                 return -1;
510
511         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
512
513         l = rte_pktmbuf_data_len(m) - offset;
514         if (srclen <= l) {
515                 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
516                         return -1;
517                 *dst += l;
518                 return 0;
519         }
520
521         if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
522                 return -1;
523
524         *dst += dstlen;
525         n -= l;
526
527         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
528                 src = rte_pktmbuf_mtod(m, uint8_t *);
529                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
530                 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
531                         return -1;
532                 *dst += dstlen;
533                 n -= l;
534         }
535
536         return 0;
537 }
538
539 static inline int
540 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
541                 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
542 {
543         struct rte_mbuf *m;
544         int dstlen;
545         int l, n = srclen;
546         uint8_t *src;
547
548         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
549                         m = m->next)
550                 offset -= rte_pktmbuf_data_len(m);
551
552         if (m == 0)
553                 return -1;
554
555         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
556
557         l = rte_pktmbuf_data_len(m) - offset;
558         if (srclen <= l) {
559                 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
560                         return -1;
561                 *dst += l;
562                 return 0;
563         }
564
565         if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
566                 return -1;
567
568         *dst += dstlen;
569         n -= l;
570
571         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
572                 src = rte_pktmbuf_mtod(m, uint8_t *);
573                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
574                 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
575                         return -1;
576                 *dst += dstlen;
577                 n -= l;
578         }
579
580         return 0;
581 }
582
583 /** Process standard openssl cipher encryption */
584 static int
585 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
586                 int offset, uint8_t *iv, uint8_t *key, int srclen,
587                 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
588 {
589         int totlen;
590
591         if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
592                 goto process_cipher_encrypt_err;
593
594         EVP_CIPHER_CTX_set_padding(ctx, 0);
595
596         if (process_openssl_encryption_update(mbuf_src, offset, &dst,
597                         srclen, ctx))
598                 goto process_cipher_encrypt_err;
599
600         if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
601                 goto process_cipher_encrypt_err;
602
603         return 0;
604
605 process_cipher_encrypt_err:
606         OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
607         return -EINVAL;
608 }
609
610 /** Process standard openssl cipher encryption */
611 static int
612 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
613                 uint8_t *iv, int srclen,
614                 EVP_CIPHER_CTX *ctx)
615 {
616         uint8_t i;
617         uint8_t encrypted_iv[DES_BLOCK_SIZE];
618         int encrypted_ivlen;
619
620         if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
621                         iv, DES_BLOCK_SIZE) <= 0)
622                 goto process_cipher_encrypt_err;
623
624         for (i = 0; i < srclen; i++)
625                 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
626
627         return 0;
628
629 process_cipher_encrypt_err:
630         OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
631         return -EINVAL;
632 }
633 /** Process standard openssl cipher decryption */
634 static int
635 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
636                 int offset, uint8_t *iv, uint8_t *key, int srclen,
637                 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
638 {
639         int totlen;
640
641         if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
642                 goto process_cipher_decrypt_err;
643
644         EVP_CIPHER_CTX_set_padding(ctx, 0);
645
646         if (process_openssl_decryption_update(mbuf_src, offset, &dst,
647                         srclen, ctx))
648                 goto process_cipher_decrypt_err;
649
650         if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
651                 goto process_cipher_decrypt_err;
652         return 0;
653
654 process_cipher_decrypt_err:
655         OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
656         return -EINVAL;
657 }
658
659 /** Process cipher des 3 ctr encryption, decryption algorithm */
660 static int
661 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
662                 int offset, uint8_t *iv, uint8_t *key, int srclen,
663                 EVP_CIPHER_CTX *ctx)
664 {
665         uint8_t ebuf[8], ctr[8];
666         int unused, n;
667         struct rte_mbuf *m;
668         uint8_t *src;
669         int l;
670
671         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
672                         m = m->next)
673                 offset -= rte_pktmbuf_data_len(m);
674
675         if (m == 0)
676                 goto process_cipher_des3ctr_err;
677
678         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
679         l = rte_pktmbuf_data_len(m) - offset;
680
681         /* We use 3DES encryption also for decryption.
682          * IV is not important for 3DES ecb
683          */
684         if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
685                 goto process_cipher_des3ctr_err;
686
687         memcpy(ctr, iv, 8);
688
689         for (n = 0; n < srclen; n++) {
690                 if (n % 8 == 0) {
691                         if (EVP_EncryptUpdate(ctx,
692                                         (unsigned char *)&ebuf, &unused,
693                                         (const unsigned char *)&ctr, 8) <= 0)
694                                 goto process_cipher_des3ctr_err;
695                         ctr_inc(ctr);
696                 }
697                 dst[n] = *(src++) ^ ebuf[n % 8];
698
699                 l--;
700                 if (!l) {
701                         m = m->next;
702                         if (m) {
703                                 src = rte_pktmbuf_mtod(m, uint8_t *);
704                                 l = rte_pktmbuf_data_len(m);
705                         }
706                 }
707         }
708
709         return 0;
710
711 process_cipher_des3ctr_err:
712         OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
713         return -EINVAL;
714 }
715
716 /** Process auth/encription aes-gcm algorithm */
717 static int
718 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
719                 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
720                 uint8_t *key, uint8_t *dst, uint8_t *tag,
721                 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
722 {
723         int len = 0, unused = 0;
724         uint8_t empty[] = {};
725
726         if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
727                 goto process_auth_encryption_gcm_err;
728
729         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
730                 goto process_auth_encryption_gcm_err;
731
732         if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
733                 goto process_auth_encryption_gcm_err;
734
735         if (aadlen > 0)
736                 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
737                         goto process_auth_encryption_gcm_err;
738
739         if (srclen > 0)
740                 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
741                                 srclen, ctx))
742                         goto process_auth_encryption_gcm_err;
743
744         /* Workaround open ssl bug in version less then 1.0.1f */
745         if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
746                 goto process_auth_encryption_gcm_err;
747
748         if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
749                 goto process_auth_encryption_gcm_err;
750
751         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
752                 goto process_auth_encryption_gcm_err;
753
754         return 0;
755
756 process_auth_encryption_gcm_err:
757         OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
758         return -EINVAL;
759 }
760
761 static int
762 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
763                 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
764                 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
765                 const EVP_CIPHER *algo)
766 {
767         int len = 0, unused = 0;
768         uint8_t empty[] = {};
769
770         if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
771                 goto process_auth_decryption_gcm_err;
772
773         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
774                 goto process_auth_decryption_gcm_err;
775
776         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
777                 goto process_auth_decryption_gcm_err;
778
779         if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
780                 goto process_auth_decryption_gcm_err;
781
782         if (aadlen > 0)
783                 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
784                         goto process_auth_decryption_gcm_err;
785
786         if (srclen > 0)
787                 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
788                                 srclen, ctx))
789                         goto process_auth_decryption_gcm_err;
790
791         /* Workaround open ssl bug in version less then 1.0.1f */
792         if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
793                 goto process_auth_decryption_gcm_err;
794
795         if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
796                 goto process_auth_decryption_gcm_final_err;
797
798         return 0;
799
800 process_auth_decryption_gcm_err:
801         OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
802         return -EINVAL;
803
804 process_auth_decryption_gcm_final_err:
805         return -EFAULT;
806 }
807
808 /** Process standard openssl auth algorithms */
809 static int
810 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
811                 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
812                 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
813 {
814         size_t dstlen;
815         struct rte_mbuf *m;
816         int l, n = srclen;
817         uint8_t *src;
818
819         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
820                         m = m->next)
821                 offset -= rte_pktmbuf_data_len(m);
822
823         if (m == 0)
824                 goto process_auth_err;
825
826         if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
827                 goto process_auth_err;
828
829         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
830
831         l = rte_pktmbuf_data_len(m) - offset;
832         if (srclen <= l) {
833                 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
834                         goto process_auth_err;
835                 goto process_auth_final;
836         }
837
838         if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
839                 goto process_auth_err;
840
841         n -= l;
842
843         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
844                 src = rte_pktmbuf_mtod(m, uint8_t *);
845                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
846                 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
847                         goto process_auth_err;
848                 n -= l;
849         }
850
851 process_auth_final:
852         if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
853                 goto process_auth_err;
854         return 0;
855
856 process_auth_err:
857         OPENSSL_LOG_ERR("Process openssl auth failed");
858         return -EINVAL;
859 }
860
861 /** Process standard openssl auth algorithms with hmac */
862 static int
863 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
864                 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
865                 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
866 {
867         size_t dstlen;
868         struct rte_mbuf *m;
869         int l, n = srclen;
870         uint8_t *src;
871
872         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
873                         m = m->next)
874                 offset -= rte_pktmbuf_data_len(m);
875
876         if (m == 0)
877                 goto process_auth_err;
878
879         if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
880                 goto process_auth_err;
881
882         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
883
884         l = rte_pktmbuf_data_len(m) - offset;
885         if (srclen <= l) {
886                 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
887                         goto process_auth_err;
888                 goto process_auth_final;
889         }
890
891         if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
892                 goto process_auth_err;
893
894         n -= l;
895
896         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
897                 src = rte_pktmbuf_mtod(m, uint8_t *);
898                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
899                 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
900                         goto process_auth_err;
901                 n -= l;
902         }
903
904 process_auth_final:
905         if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
906                 goto process_auth_err;
907
908         return 0;
909
910 process_auth_err:
911         OPENSSL_LOG_ERR("Process openssl auth failed");
912         return -EINVAL;
913 }
914
915 /*----------------------------------------------------------------------------*/
916
917 /** Process auth/cipher combined operation */
918 static void
919 process_openssl_combined_op
920                 (struct rte_crypto_op *op, struct openssl_session *sess,
921                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
922 {
923         /* cipher */
924         uint8_t *dst = NULL, *iv, *tag, *aad;
925         int srclen, ivlen, aadlen, status = -1;
926
927         /*
928          * Segmented destination buffer is not supported for
929          * encryption/decryption
930          */
931         if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
932                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
933                 return;
934         }
935
936         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
937                         sess->iv.offset);
938         ivlen = sess->iv.length;
939         aad = op->sym->auth.aad.data;
940         aadlen = sess->auth.aad_length;
941
942         tag = op->sym->auth.digest.data;
943         if (tag == NULL)
944                 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
945                                 op->sym->cipher.data.offset +
946                                 op->sym->cipher.data.length);
947
948         if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
949                 srclen = 0;
950         else {
951                 srclen = op->sym->cipher.data.length;
952                 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
953                                 op->sym->cipher.data.offset);
954         }
955
956         if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
957                 status = process_openssl_auth_encryption_gcm(
958                                 mbuf_src, op->sym->cipher.data.offset, srclen,
959                                 aad, aadlen, iv, ivlen, sess->cipher.key.data,
960                                 dst, tag, sess->cipher.ctx,
961                                 sess->cipher.evp_algo);
962         else
963                 status = process_openssl_auth_decryption_gcm(
964                                 mbuf_src, op->sym->cipher.data.offset, srclen,
965                                 aad, aadlen, iv, ivlen, sess->cipher.key.data,
966                                 dst, tag, sess->cipher.ctx,
967                                 sess->cipher.evp_algo);
968
969         if (status != 0) {
970                 if (status == (-EFAULT) &&
971                                 sess->auth.operation ==
972                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
973                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
974                 else
975                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
976         }
977 }
978
979 /** Process cipher operation */
980 static void
981 process_openssl_cipher_op
982                 (struct rte_crypto_op *op, struct openssl_session *sess,
983                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
984 {
985         uint8_t *dst, *iv;
986         int srclen, status;
987
988         /*
989          * Segmented destination buffer is not supported for
990          * encryption/decryption
991          */
992         if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
993                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
994                 return;
995         }
996
997         srclen = op->sym->cipher.data.length;
998         dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
999                         op->sym->cipher.data.offset);
1000
1001         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1002                         sess->iv.offset);
1003
1004         if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1005                 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1006                         status = process_openssl_cipher_encrypt(mbuf_src, dst,
1007                                         op->sym->cipher.data.offset, iv,
1008                                         sess->cipher.key.data, srclen,
1009                                         sess->cipher.ctx,
1010                                         sess->cipher.evp_algo);
1011                 else
1012                         status = process_openssl_cipher_decrypt(mbuf_src, dst,
1013                                         op->sym->cipher.data.offset, iv,
1014                                         sess->cipher.key.data, srclen,
1015                                         sess->cipher.ctx,
1016                                         sess->cipher.evp_algo);
1017         else
1018                 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1019                                 op->sym->cipher.data.offset, iv,
1020                                 sess->cipher.key.data, srclen,
1021                                 sess->cipher.ctx);
1022
1023         if (status != 0)
1024                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1025 }
1026
1027 /** Process cipher operation */
1028 static void
1029 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1030                 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1031                 struct rte_mbuf *mbuf_dst)
1032 {
1033         uint8_t *src, *dst, *iv;
1034         uint8_t block_size, last_block_len;
1035         int srclen, status = 0;
1036
1037         srclen = op->sym->cipher.data.length;
1038         src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1039                         op->sym->cipher.data.offset);
1040         dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1041                         op->sym->cipher.data.offset);
1042
1043         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1044                         sess->iv.offset);
1045
1046         block_size = DES_BLOCK_SIZE;
1047
1048         last_block_len = srclen % block_size;
1049         if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1050                 /* Encrypt only with ECB mode XOR IV */
1051                 if (srclen < block_size) {
1052                         status = process_openssl_cipher_bpi_encrypt(src, dst,
1053                                         iv, srclen,
1054                                         sess->cipher.bpi_ctx);
1055                 } else {
1056                         srclen -= last_block_len;
1057                         /* Encrypt with the block aligned stream with CBC mode */
1058                         status = process_openssl_cipher_encrypt(mbuf_src, dst,
1059                                         op->sym->cipher.data.offset, iv,
1060                                         sess->cipher.key.data, srclen,
1061                                         sess->cipher.ctx, sess->cipher.evp_algo);
1062                         if (last_block_len) {
1063                                 /* Point at last block */
1064                                 dst += srclen;
1065                                 /*
1066                                  * IV is the last encrypted block from
1067                                  * the previous operation
1068                                  */
1069                                 iv = dst - block_size;
1070                                 src += srclen;
1071                                 srclen = last_block_len;
1072                                 /* Encrypt the last frame with ECB mode */
1073                                 status |= process_openssl_cipher_bpi_encrypt(src,
1074                                                 dst, iv,
1075                                                 srclen, sess->cipher.bpi_ctx);
1076                         }
1077                 }
1078         } else {
1079                 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1080                 if (srclen < block_size) {
1081                         status = process_openssl_cipher_bpi_encrypt(src, dst,
1082                                         iv,
1083                                         srclen,
1084                                         sess->cipher.bpi_ctx);
1085                 } else {
1086                         if (last_block_len) {
1087                                 /* Point at last block */
1088                                 dst += srclen - last_block_len;
1089                                 src += srclen - last_block_len;
1090                                 /*
1091                                  * IV is the last full block
1092                                  */
1093                                 iv = src - block_size;
1094                                 /*
1095                                  * Decrypt the last frame with ECB mode
1096                                  * (encrypt, as it is the same operation)
1097                                  */
1098                                 status = process_openssl_cipher_bpi_encrypt(src,
1099                                                 dst, iv,
1100                                                 last_block_len, sess->cipher.bpi_ctx);
1101                                 /* Prepare parameters for CBC mode op */
1102                                 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1103                                                 sess->iv.offset);
1104                                 dst += last_block_len - srclen;
1105                                 srclen -= last_block_len;
1106                         }
1107
1108                         /* Decrypt with CBC mode */
1109                         status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1110                                         op->sym->cipher.data.offset, iv,
1111                                         sess->cipher.key.data, srclen,
1112                                         sess->cipher.ctx,
1113                                         sess->cipher.evp_algo);
1114                 }
1115         }
1116
1117         if (status != 0)
1118                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1119 }
1120
1121 /** Process auth operation */
1122 static void
1123 process_openssl_auth_op
1124                 (struct rte_crypto_op *op, struct openssl_session *sess,
1125                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1126 {
1127         uint8_t *dst;
1128         int srclen, status;
1129
1130         srclen = op->sym->auth.data.length;
1131
1132         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1133                 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1134                                 sess->auth.digest_length);
1135         else {
1136                 dst = op->sym->auth.digest.data;
1137                 if (dst == NULL)
1138                         dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1139                                         op->sym->auth.data.offset +
1140                                         op->sym->auth.data.length);
1141         }
1142
1143         switch (sess->auth.mode) {
1144         case OPENSSL_AUTH_AS_AUTH:
1145                 status = process_openssl_auth(mbuf_src, dst,
1146                                 op->sym->auth.data.offset, NULL, NULL, srclen,
1147                                 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1148                 break;
1149         case OPENSSL_AUTH_AS_HMAC:
1150                 status = process_openssl_auth_hmac(mbuf_src, dst,
1151                                 op->sym->auth.data.offset, NULL,
1152                                 sess->auth.hmac.pkey, srclen,
1153                                 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1154                 break;
1155         default:
1156                 status = -1;
1157                 break;
1158         }
1159
1160         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1161                 if (memcmp(dst, op->sym->auth.digest.data,
1162                                 sess->auth.digest_length) != 0) {
1163                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1164                 }
1165                 /* Trim area used for digest from mbuf. */
1166                 rte_pktmbuf_trim(mbuf_src, sess->auth.digest_length);
1167         }
1168
1169         if (status != 0)
1170                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1171 }
1172
1173 /** Process crypto operation for mbuf */
1174 static int
1175 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1176                 struct openssl_session *sess)
1177 {
1178         struct rte_mbuf *msrc, *mdst;
1179         int retval;
1180
1181         msrc = op->sym->m_src;
1182         mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1183
1184         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1185
1186         switch (sess->chain_order) {
1187         case OPENSSL_CHAIN_ONLY_CIPHER:
1188                 process_openssl_cipher_op(op, sess, msrc, mdst);
1189                 break;
1190         case OPENSSL_CHAIN_ONLY_AUTH:
1191                 process_openssl_auth_op(op, sess, msrc, mdst);
1192                 break;
1193         case OPENSSL_CHAIN_CIPHER_AUTH:
1194                 process_openssl_cipher_op(op, sess, msrc, mdst);
1195                 process_openssl_auth_op(op, sess, mdst, mdst);
1196                 break;
1197         case OPENSSL_CHAIN_AUTH_CIPHER:
1198                 process_openssl_auth_op(op, sess, msrc, mdst);
1199                 process_openssl_cipher_op(op, sess, msrc, mdst);
1200                 break;
1201         case OPENSSL_CHAIN_COMBINED:
1202                 process_openssl_combined_op(op, sess, msrc, mdst);
1203                 break;
1204         case OPENSSL_CHAIN_CIPHER_BPI:
1205                 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1206                 break;
1207         default:
1208                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1209                 break;
1210         }
1211
1212         /* Free session if a session-less crypto op */
1213         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1214                 openssl_reset_session(sess);
1215                 memset(sess, 0, sizeof(struct openssl_session));
1216                 rte_mempool_put(qp->sess_mp, op->sym->session);
1217                 op->sym->session = NULL;
1218         }
1219
1220         if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1221                 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1222
1223         if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1224                 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1225         else
1226                 retval = -1;
1227
1228         return retval;
1229 }
1230
1231 /*
1232  *------------------------------------------------------------------------------
1233  * PMD Framework
1234  *------------------------------------------------------------------------------
1235  */
1236
1237 /** Enqueue burst */
1238 static uint16_t
1239 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1240                 uint16_t nb_ops)
1241 {
1242         struct openssl_session *sess;
1243         struct openssl_qp *qp = queue_pair;
1244         int i, retval;
1245
1246         for (i = 0; i < nb_ops; i++) {
1247                 sess = get_session(qp, ops[i]);
1248                 if (unlikely(sess == NULL))
1249                         goto enqueue_err;
1250
1251                 retval = process_op(qp, ops[i], sess);
1252                 if (unlikely(retval < 0))
1253                         goto enqueue_err;
1254         }
1255
1256         qp->stats.enqueued_count += i;
1257         return i;
1258
1259 enqueue_err:
1260         qp->stats.enqueue_err_count++;
1261         return i;
1262 }
1263
1264 /** Dequeue burst */
1265 static uint16_t
1266 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1267                 uint16_t nb_ops)
1268 {
1269         struct openssl_qp *qp = queue_pair;
1270
1271         unsigned int nb_dequeued = 0;
1272
1273         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1274                         (void **)ops, nb_ops, NULL);
1275         qp->stats.dequeued_count += nb_dequeued;
1276
1277         return nb_dequeued;
1278 }
1279
1280 /** Create OPENSSL crypto device */
1281 static int
1282 cryptodev_openssl_create(const char *name,
1283                         struct rte_vdev_device *vdev,
1284                         struct rte_crypto_vdev_init_params *init_params)
1285 {
1286         struct rte_cryptodev *dev;
1287         struct openssl_private *internals;
1288
1289         if (init_params->name[0] == '\0')
1290                 snprintf(init_params->name, sizeof(init_params->name),
1291                                 "%s", name);
1292
1293         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1294                         sizeof(struct openssl_private),
1295                         init_params->socket_id,
1296                         vdev);
1297         if (dev == NULL) {
1298                 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1299                 goto init_error;
1300         }
1301
1302         dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
1303         dev->dev_ops = rte_openssl_pmd_ops;
1304
1305         /* register rx/tx burst functions for data path */
1306         dev->dequeue_burst = openssl_pmd_dequeue_burst;
1307         dev->enqueue_burst = openssl_pmd_enqueue_burst;
1308
1309         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1310                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1311                         RTE_CRYPTODEV_FF_CPU_AESNI |
1312                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1313
1314         /* Set vector instructions mode supported */
1315         internals = dev->data->dev_private;
1316
1317         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1318         internals->max_nb_sessions = init_params->max_nb_sessions;
1319
1320         return 0;
1321
1322 init_error:
1323         OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1324                         init_params->name);
1325
1326         cryptodev_openssl_remove(vdev);
1327         return -EFAULT;
1328 }
1329
1330 /** Initialise OPENSSL crypto device */
1331 static int
1332 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1333 {
1334         struct rte_crypto_vdev_init_params init_params = {
1335                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1336                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1337                 rte_socket_id(),
1338                 {0}
1339         };
1340         const char *name;
1341         const char *input_args;
1342
1343         name = rte_vdev_device_name(vdev);
1344         if (name == NULL)
1345                 return -EINVAL;
1346         input_args = rte_vdev_device_args(vdev);
1347
1348         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1349
1350         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1351                         init_params.socket_id);
1352         if (init_params.name[0] != '\0')
1353                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
1354                         init_params.name);
1355         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
1356                         init_params.max_nb_queue_pairs);
1357         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
1358                         init_params.max_nb_sessions);
1359
1360         return cryptodev_openssl_create(name, vdev, &init_params);
1361 }
1362
1363 /** Uninitialise OPENSSL crypto device */
1364 static int
1365 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1366 {
1367         const char *name;
1368
1369         name = rte_vdev_device_name(vdev);
1370         if (name == NULL)
1371                 return -EINVAL;
1372
1373         RTE_LOG(INFO, PMD,
1374                 "Closing OPENSSL crypto device %s on numa socket %u\n",
1375                 name, rte_socket_id());
1376
1377         return 0;
1378 }
1379
1380 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1381         .probe = cryptodev_openssl_probe,
1382         .remove = cryptodev_openssl_remove
1383 };
1384
1385 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1386         cryptodev_openssl_pmd_drv);
1387 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1388         "max_nb_queue_pairs=<int> "
1389         "max_nb_sessions=<int> "
1390         "socket_id=<int>");