cryptodev: remove AAD 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
375         return 0;
376 }
377
378 /** Parse crypto xform chain and set private session parameters */
379 int
380 openssl_set_session_parameters(struct openssl_session *sess,
381                 const struct rte_crypto_sym_xform *xform)
382 {
383         const struct rte_crypto_sym_xform *cipher_xform = NULL;
384         const struct rte_crypto_sym_xform *auth_xform = NULL;
385
386         sess->chain_order = openssl_get_chain_order(xform);
387         switch (sess->chain_order) {
388         case OPENSSL_CHAIN_ONLY_CIPHER:
389                 cipher_xform = xform;
390                 break;
391         case OPENSSL_CHAIN_ONLY_AUTH:
392                 auth_xform = xform;
393                 break;
394         case OPENSSL_CHAIN_CIPHER_AUTH:
395                 cipher_xform = xform;
396                 auth_xform = xform->next;
397                 break;
398         case OPENSSL_CHAIN_AUTH_CIPHER:
399                 auth_xform = xform;
400                 cipher_xform = xform->next;
401                 break;
402         default:
403                 return -EINVAL;
404         }
405
406         /* Default IV length = 0 */
407         sess->iv.length = 0;
408
409         /* cipher_xform must be check before auth_xform */
410         if (cipher_xform) {
411                 if (openssl_set_session_cipher_parameters(
412                                 sess, cipher_xform)) {
413                         OPENSSL_LOG_ERR(
414                                 "Invalid/unsupported cipher parameters");
415                         return -EINVAL;
416                 }
417         }
418
419         if (auth_xform) {
420                 if (openssl_set_session_auth_parameters(sess, auth_xform)) {
421                         OPENSSL_LOG_ERR(
422                                 "Invalid/unsupported auth parameters");
423                         return -EINVAL;
424                 }
425         }
426
427         return 0;
428 }
429
430 /** Reset private session parameters */
431 void
432 openssl_reset_session(struct openssl_session *sess)
433 {
434         EVP_CIPHER_CTX_free(sess->cipher.ctx);
435
436         if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
437                 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
438
439         switch (sess->auth.mode) {
440         case OPENSSL_AUTH_AS_AUTH:
441                 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
442                 break;
443         case OPENSSL_AUTH_AS_HMAC:
444                 EVP_PKEY_free(sess->auth.hmac.pkey);
445                 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
446                 break;
447         default:
448                 break;
449         }
450 }
451
452 /** Provide session for operation */
453 static struct openssl_session *
454 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
455 {
456         struct openssl_session *sess = NULL;
457
458         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
459                 /* get existing session */
460                 if (likely(op->sym->session != NULL &&
461                                 op->sym->session->dev_type ==
462                                 RTE_CRYPTODEV_OPENSSL_PMD))
463                         sess = (struct openssl_session *)
464                                 op->sym->session->_private;
465         } else  {
466                 /* provide internal session */
467                 void *_sess = NULL;
468
469                 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
470                         sess = (struct openssl_session *)
471                                 ((struct rte_cryptodev_sym_session *)_sess)
472                                 ->_private;
473
474                         if (unlikely(openssl_set_session_parameters(
475                                         sess, op->sym->xform) != 0)) {
476                                 rte_mempool_put(qp->sess_mp, _sess);
477                                 sess = NULL;
478                         } else
479                                 op->sym->session = _sess;
480                 }
481         }
482
483         if (sess == NULL)
484                 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
485
486         return sess;
487 }
488
489 /*
490  *------------------------------------------------------------------------------
491  * Process Operations
492  *------------------------------------------------------------------------------
493  */
494 static inline int
495 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
496                 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
497 {
498         struct rte_mbuf *m;
499         int dstlen;
500         int l, n = srclen;
501         uint8_t *src;
502
503         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
504                         m = m->next)
505                 offset -= rte_pktmbuf_data_len(m);
506
507         if (m == 0)
508                 return -1;
509
510         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
511
512         l = rte_pktmbuf_data_len(m) - offset;
513         if (srclen <= l) {
514                 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
515                         return -1;
516                 *dst += l;
517                 return 0;
518         }
519
520         if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
521                 return -1;
522
523         *dst += dstlen;
524         n -= l;
525
526         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
527                 src = rte_pktmbuf_mtod(m, uint8_t *);
528                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
529                 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
530                         return -1;
531                 *dst += dstlen;
532                 n -= l;
533         }
534
535         return 0;
536 }
537
538 static inline int
539 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
540                 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
541 {
542         struct rte_mbuf *m;
543         int dstlen;
544         int l, n = srclen;
545         uint8_t *src;
546
547         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
548                         m = m->next)
549                 offset -= rte_pktmbuf_data_len(m);
550
551         if (m == 0)
552                 return -1;
553
554         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
555
556         l = rte_pktmbuf_data_len(m) - offset;
557         if (srclen <= l) {
558                 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
559                         return -1;
560                 *dst += l;
561                 return 0;
562         }
563
564         if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
565                 return -1;
566
567         *dst += dstlen;
568         n -= l;
569
570         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
571                 src = rte_pktmbuf_mtod(m, uint8_t *);
572                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
573                 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
574                         return -1;
575                 *dst += dstlen;
576                 n -= l;
577         }
578
579         return 0;
580 }
581
582 /** Process standard openssl cipher encryption */
583 static int
584 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
585                 int offset, uint8_t *iv, uint8_t *key, int srclen,
586                 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
587 {
588         int totlen;
589
590         if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
591                 goto process_cipher_encrypt_err;
592
593         EVP_CIPHER_CTX_set_padding(ctx, 0);
594
595         if (process_openssl_encryption_update(mbuf_src, offset, &dst,
596                         srclen, ctx))
597                 goto process_cipher_encrypt_err;
598
599         if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
600                 goto process_cipher_encrypt_err;
601
602         return 0;
603
604 process_cipher_encrypt_err:
605         OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
606         return -EINVAL;
607 }
608
609 /** Process standard openssl cipher encryption */
610 static int
611 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
612                 uint8_t *iv, int srclen,
613                 EVP_CIPHER_CTX *ctx)
614 {
615         uint8_t i;
616         uint8_t encrypted_iv[DES_BLOCK_SIZE];
617         int encrypted_ivlen;
618
619         if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
620                         iv, DES_BLOCK_SIZE) <= 0)
621                 goto process_cipher_encrypt_err;
622
623         for (i = 0; i < srclen; i++)
624                 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
625
626         return 0;
627
628 process_cipher_encrypt_err:
629         OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
630         return -EINVAL;
631 }
632 /** Process standard openssl cipher decryption */
633 static int
634 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
635                 int offset, uint8_t *iv, uint8_t *key, int srclen,
636                 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
637 {
638         int totlen;
639
640         if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
641                 goto process_cipher_decrypt_err;
642
643         EVP_CIPHER_CTX_set_padding(ctx, 0);
644
645         if (process_openssl_decryption_update(mbuf_src, offset, &dst,
646                         srclen, ctx))
647                 goto process_cipher_decrypt_err;
648
649         if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
650                 goto process_cipher_decrypt_err;
651         return 0;
652
653 process_cipher_decrypt_err:
654         OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
655         return -EINVAL;
656 }
657
658 /** Process cipher des 3 ctr encryption, decryption algorithm */
659 static int
660 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
661                 int offset, uint8_t *iv, uint8_t *key, int srclen,
662                 EVP_CIPHER_CTX *ctx)
663 {
664         uint8_t ebuf[8], ctr[8];
665         int unused, n;
666         struct rte_mbuf *m;
667         uint8_t *src;
668         int l;
669
670         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
671                         m = m->next)
672                 offset -= rte_pktmbuf_data_len(m);
673
674         if (m == 0)
675                 goto process_cipher_des3ctr_err;
676
677         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
678         l = rte_pktmbuf_data_len(m) - offset;
679
680         /* We use 3DES encryption also for decryption.
681          * IV is not important for 3DES ecb
682          */
683         if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
684                 goto process_cipher_des3ctr_err;
685
686         memcpy(ctr, iv, 8);
687
688         for (n = 0; n < srclen; n++) {
689                 if (n % 8 == 0) {
690                         if (EVP_EncryptUpdate(ctx,
691                                         (unsigned char *)&ebuf, &unused,
692                                         (const unsigned char *)&ctr, 8) <= 0)
693                                 goto process_cipher_des3ctr_err;
694                         ctr_inc(ctr);
695                 }
696                 dst[n] = *(src++) ^ ebuf[n % 8];
697
698                 l--;
699                 if (!l) {
700                         m = m->next;
701                         if (m) {
702                                 src = rte_pktmbuf_mtod(m, uint8_t *);
703                                 l = rte_pktmbuf_data_len(m);
704                         }
705                 }
706         }
707
708         return 0;
709
710 process_cipher_des3ctr_err:
711         OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
712         return -EINVAL;
713 }
714
715 /** Process auth/encription aes-gcm algorithm */
716 static int
717 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
718                 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
719                 uint8_t *key, uint8_t *dst, uint8_t *tag,
720                 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
721 {
722         int len = 0, unused = 0;
723         uint8_t empty[] = {};
724
725         if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
726                 goto process_auth_encryption_gcm_err;
727
728         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
729                 goto process_auth_encryption_gcm_err;
730
731         if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
732                 goto process_auth_encryption_gcm_err;
733
734         if (aadlen > 0)
735                 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
736                         goto process_auth_encryption_gcm_err;
737
738         if (srclen > 0)
739                 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
740                                 srclen, ctx))
741                         goto process_auth_encryption_gcm_err;
742
743         /* Workaround open ssl bug in version less then 1.0.1f */
744         if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
745                 goto process_auth_encryption_gcm_err;
746
747         if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
748                 goto process_auth_encryption_gcm_err;
749
750         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
751                 goto process_auth_encryption_gcm_err;
752
753         return 0;
754
755 process_auth_encryption_gcm_err:
756         OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
757         return -EINVAL;
758 }
759
760 static int
761 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
762                 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
763                 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
764                 const EVP_CIPHER *algo)
765 {
766         int len = 0, unused = 0;
767         uint8_t empty[] = {};
768
769         if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
770                 goto process_auth_decryption_gcm_err;
771
772         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
773                 goto process_auth_decryption_gcm_err;
774
775         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
776                 goto process_auth_decryption_gcm_err;
777
778         if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
779                 goto process_auth_decryption_gcm_err;
780
781         if (aadlen > 0)
782                 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
783                         goto process_auth_decryption_gcm_err;
784
785         if (srclen > 0)
786                 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
787                                 srclen, ctx))
788                         goto process_auth_decryption_gcm_err;
789
790         /* Workaround open ssl bug in version less then 1.0.1f */
791         if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
792                 goto process_auth_decryption_gcm_err;
793
794         if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
795                 goto process_auth_decryption_gcm_final_err;
796
797         return 0;
798
799 process_auth_decryption_gcm_err:
800         OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
801         return -EINVAL;
802
803 process_auth_decryption_gcm_final_err:
804         return -EFAULT;
805 }
806
807 /** Process standard openssl auth algorithms */
808 static int
809 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
810                 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
811                 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
812 {
813         size_t dstlen;
814         struct rte_mbuf *m;
815         int l, n = srclen;
816         uint8_t *src;
817
818         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
819                         m = m->next)
820                 offset -= rte_pktmbuf_data_len(m);
821
822         if (m == 0)
823                 goto process_auth_err;
824
825         if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
826                 goto process_auth_err;
827
828         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
829
830         l = rte_pktmbuf_data_len(m) - offset;
831         if (srclen <= l) {
832                 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
833                         goto process_auth_err;
834                 goto process_auth_final;
835         }
836
837         if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
838                 goto process_auth_err;
839
840         n -= l;
841
842         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
843                 src = rte_pktmbuf_mtod(m, uint8_t *);
844                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
845                 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
846                         goto process_auth_err;
847                 n -= l;
848         }
849
850 process_auth_final:
851         if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
852                 goto process_auth_err;
853         return 0;
854
855 process_auth_err:
856         OPENSSL_LOG_ERR("Process openssl auth failed");
857         return -EINVAL;
858 }
859
860 /** Process standard openssl auth algorithms with hmac */
861 static int
862 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
863                 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
864                 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
865 {
866         size_t dstlen;
867         struct rte_mbuf *m;
868         int l, n = srclen;
869         uint8_t *src;
870
871         for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
872                         m = m->next)
873                 offset -= rte_pktmbuf_data_len(m);
874
875         if (m == 0)
876                 goto process_auth_err;
877
878         if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
879                 goto process_auth_err;
880
881         src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
882
883         l = rte_pktmbuf_data_len(m) - offset;
884         if (srclen <= l) {
885                 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
886                         goto process_auth_err;
887                 goto process_auth_final;
888         }
889
890         if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
891                 goto process_auth_err;
892
893         n -= l;
894
895         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
896                 src = rte_pktmbuf_mtod(m, uint8_t *);
897                 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
898                 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
899                         goto process_auth_err;
900                 n -= l;
901         }
902
903 process_auth_final:
904         if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
905                 goto process_auth_err;
906
907         return 0;
908
909 process_auth_err:
910         OPENSSL_LOG_ERR("Process openssl auth failed");
911         return -EINVAL;
912 }
913
914 /*----------------------------------------------------------------------------*/
915
916 /** Process auth/cipher combined operation */
917 static void
918 process_openssl_combined_op
919                 (struct rte_crypto_op *op, struct openssl_session *sess,
920                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
921 {
922         /* cipher */
923         uint8_t *dst = NULL, *iv, *tag, *aad;
924         int srclen, ivlen, aadlen, status = -1;
925
926         /*
927          * Segmented destination buffer is not supported for
928          * encryption/decryption
929          */
930         if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
931                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
932                 return;
933         }
934
935         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
936                         sess->iv.offset);
937         ivlen = sess->iv.length;
938         aad = op->sym->auth.aad.data;
939         aadlen = sess->auth.aad_length;
940
941         tag = op->sym->auth.digest.data;
942         if (tag == NULL)
943                 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
944                                 op->sym->cipher.data.offset +
945                                 op->sym->cipher.data.length);
946
947         if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
948                 srclen = 0;
949         else {
950                 srclen = op->sym->cipher.data.length;
951                 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
952                                 op->sym->cipher.data.offset);
953         }
954
955         if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
956                 status = process_openssl_auth_encryption_gcm(
957                                 mbuf_src, op->sym->cipher.data.offset, srclen,
958                                 aad, aadlen, iv, ivlen, sess->cipher.key.data,
959                                 dst, tag, sess->cipher.ctx,
960                                 sess->cipher.evp_algo);
961         else
962                 status = process_openssl_auth_decryption_gcm(
963                                 mbuf_src, op->sym->cipher.data.offset, srclen,
964                                 aad, aadlen, iv, ivlen, sess->cipher.key.data,
965                                 dst, tag, sess->cipher.ctx,
966                                 sess->cipher.evp_algo);
967
968         if (status != 0) {
969                 if (status == (-EFAULT) &&
970                                 sess->auth.operation ==
971                                                 RTE_CRYPTO_AUTH_OP_VERIFY)
972                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
973                 else
974                         op->status = RTE_CRYPTO_OP_STATUS_ERROR;
975         }
976 }
977
978 /** Process cipher operation */
979 static void
980 process_openssl_cipher_op
981                 (struct rte_crypto_op *op, struct openssl_session *sess,
982                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
983 {
984         uint8_t *dst, *iv;
985         int srclen, status;
986
987         /*
988          * Segmented destination buffer is not supported for
989          * encryption/decryption
990          */
991         if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
992                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
993                 return;
994         }
995
996         srclen = op->sym->cipher.data.length;
997         dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
998                         op->sym->cipher.data.offset);
999
1000         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1001                         sess->iv.offset);
1002
1003         if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1004                 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1005                         status = process_openssl_cipher_encrypt(mbuf_src, dst,
1006                                         op->sym->cipher.data.offset, iv,
1007                                         sess->cipher.key.data, srclen,
1008                                         sess->cipher.ctx,
1009                                         sess->cipher.evp_algo);
1010                 else
1011                         status = process_openssl_cipher_decrypt(mbuf_src, dst,
1012                                         op->sym->cipher.data.offset, iv,
1013                                         sess->cipher.key.data, srclen,
1014                                         sess->cipher.ctx,
1015                                         sess->cipher.evp_algo);
1016         else
1017                 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1018                                 op->sym->cipher.data.offset, iv,
1019                                 sess->cipher.key.data, srclen,
1020                                 sess->cipher.ctx);
1021
1022         if (status != 0)
1023                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1024 }
1025
1026 /** Process cipher operation */
1027 static void
1028 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1029                 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1030                 struct rte_mbuf *mbuf_dst)
1031 {
1032         uint8_t *src, *dst, *iv;
1033         uint8_t block_size, last_block_len;
1034         int srclen, status = 0;
1035
1036         srclen = op->sym->cipher.data.length;
1037         src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1038                         op->sym->cipher.data.offset);
1039         dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1040                         op->sym->cipher.data.offset);
1041
1042         iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1043                         sess->iv.offset);
1044
1045         block_size = DES_BLOCK_SIZE;
1046
1047         last_block_len = srclen % block_size;
1048         if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1049                 /* Encrypt only with ECB mode XOR IV */
1050                 if (srclen < block_size) {
1051                         status = process_openssl_cipher_bpi_encrypt(src, dst,
1052                                         iv, srclen,
1053                                         sess->cipher.bpi_ctx);
1054                 } else {
1055                         srclen -= last_block_len;
1056                         /* Encrypt with the block aligned stream with CBC mode */
1057                         status = process_openssl_cipher_encrypt(mbuf_src, dst,
1058                                         op->sym->cipher.data.offset, iv,
1059                                         sess->cipher.key.data, srclen,
1060                                         sess->cipher.ctx, sess->cipher.evp_algo);
1061                         if (last_block_len) {
1062                                 /* Point at last block */
1063                                 dst += srclen;
1064                                 /*
1065                                  * IV is the last encrypted block from
1066                                  * the previous operation
1067                                  */
1068                                 iv = dst - block_size;
1069                                 src += srclen;
1070                                 srclen = last_block_len;
1071                                 /* Encrypt the last frame with ECB mode */
1072                                 status |= process_openssl_cipher_bpi_encrypt(src,
1073                                                 dst, iv,
1074                                                 srclen, sess->cipher.bpi_ctx);
1075                         }
1076                 }
1077         } else {
1078                 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1079                 if (srclen < block_size) {
1080                         status = process_openssl_cipher_bpi_encrypt(src, dst,
1081                                         iv,
1082                                         srclen,
1083                                         sess->cipher.bpi_ctx);
1084                 } else {
1085                         if (last_block_len) {
1086                                 /* Point at last block */
1087                                 dst += srclen - last_block_len;
1088                                 src += srclen - last_block_len;
1089                                 /*
1090                                  * IV is the last full block
1091                                  */
1092                                 iv = src - block_size;
1093                                 /*
1094                                  * Decrypt the last frame with ECB mode
1095                                  * (encrypt, as it is the same operation)
1096                                  */
1097                                 status = process_openssl_cipher_bpi_encrypt(src,
1098                                                 dst, iv,
1099                                                 last_block_len, sess->cipher.bpi_ctx);
1100                                 /* Prepare parameters for CBC mode op */
1101                                 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1102                                                 sess->iv.offset);
1103                                 dst += last_block_len - srclen;
1104                                 srclen -= last_block_len;
1105                         }
1106
1107                         /* Decrypt with CBC mode */
1108                         status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1109                                         op->sym->cipher.data.offset, iv,
1110                                         sess->cipher.key.data, srclen,
1111                                         sess->cipher.ctx,
1112                                         sess->cipher.evp_algo);
1113                 }
1114         }
1115
1116         if (status != 0)
1117                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1118 }
1119
1120 /** Process auth operation */
1121 static void
1122 process_openssl_auth_op
1123                 (struct rte_crypto_op *op, struct openssl_session *sess,
1124                 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1125 {
1126         uint8_t *dst;
1127         int srclen, status;
1128
1129         srclen = op->sym->auth.data.length;
1130
1131         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1132                 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1133                                 op->sym->auth.digest.length);
1134         else {
1135                 dst = op->sym->auth.digest.data;
1136                 if (dst == NULL)
1137                         dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1138                                         op->sym->auth.data.offset +
1139                                         op->sym->auth.data.length);
1140         }
1141
1142         switch (sess->auth.mode) {
1143         case OPENSSL_AUTH_AS_AUTH:
1144                 status = process_openssl_auth(mbuf_src, dst,
1145                                 op->sym->auth.data.offset, NULL, NULL, srclen,
1146                                 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1147                 break;
1148         case OPENSSL_AUTH_AS_HMAC:
1149                 status = process_openssl_auth_hmac(mbuf_src, dst,
1150                                 op->sym->auth.data.offset, NULL,
1151                                 sess->auth.hmac.pkey, srclen,
1152                                 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1153                 break;
1154         default:
1155                 status = -1;
1156                 break;
1157         }
1158
1159         if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1160                 if (memcmp(dst, op->sym->auth.digest.data,
1161                                 op->sym->auth.digest.length) != 0) {
1162                         op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1163                 }
1164                 /* Trim area used for digest from mbuf. */
1165                 rte_pktmbuf_trim(mbuf_src, op->sym->auth.digest.length);
1166         }
1167
1168         if (status != 0)
1169                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1170 }
1171
1172 /** Process crypto operation for mbuf */
1173 static int
1174 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1175                 struct openssl_session *sess)
1176 {
1177         struct rte_mbuf *msrc, *mdst;
1178         int retval;
1179
1180         msrc = op->sym->m_src;
1181         mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1182
1183         op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1184
1185         switch (sess->chain_order) {
1186         case OPENSSL_CHAIN_ONLY_CIPHER:
1187                 process_openssl_cipher_op(op, sess, msrc, mdst);
1188                 break;
1189         case OPENSSL_CHAIN_ONLY_AUTH:
1190                 process_openssl_auth_op(op, sess, msrc, mdst);
1191                 break;
1192         case OPENSSL_CHAIN_CIPHER_AUTH:
1193                 process_openssl_cipher_op(op, sess, msrc, mdst);
1194                 process_openssl_auth_op(op, sess, mdst, mdst);
1195                 break;
1196         case OPENSSL_CHAIN_AUTH_CIPHER:
1197                 process_openssl_auth_op(op, sess, msrc, mdst);
1198                 process_openssl_cipher_op(op, sess, msrc, mdst);
1199                 break;
1200         case OPENSSL_CHAIN_COMBINED:
1201                 process_openssl_combined_op(op, sess, msrc, mdst);
1202                 break;
1203         case OPENSSL_CHAIN_CIPHER_BPI:
1204                 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1205                 break;
1206         default:
1207                 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1208                 break;
1209         }
1210
1211         /* Free session if a session-less crypto op */
1212         if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1213                 openssl_reset_session(sess);
1214                 memset(sess, 0, sizeof(struct openssl_session));
1215                 rte_mempool_put(qp->sess_mp, op->sym->session);
1216                 op->sym->session = NULL;
1217         }
1218
1219         if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1220                 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1221
1222         if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1223                 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1224         else
1225                 retval = -1;
1226
1227         return retval;
1228 }
1229
1230 /*
1231  *------------------------------------------------------------------------------
1232  * PMD Framework
1233  *------------------------------------------------------------------------------
1234  */
1235
1236 /** Enqueue burst */
1237 static uint16_t
1238 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1239                 uint16_t nb_ops)
1240 {
1241         struct openssl_session *sess;
1242         struct openssl_qp *qp = queue_pair;
1243         int i, retval;
1244
1245         for (i = 0; i < nb_ops; i++) {
1246                 sess = get_session(qp, ops[i]);
1247                 if (unlikely(sess == NULL))
1248                         goto enqueue_err;
1249
1250                 retval = process_op(qp, ops[i], sess);
1251                 if (unlikely(retval < 0))
1252                         goto enqueue_err;
1253         }
1254
1255         qp->stats.enqueued_count += i;
1256         return i;
1257
1258 enqueue_err:
1259         qp->stats.enqueue_err_count++;
1260         return i;
1261 }
1262
1263 /** Dequeue burst */
1264 static uint16_t
1265 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1266                 uint16_t nb_ops)
1267 {
1268         struct openssl_qp *qp = queue_pair;
1269
1270         unsigned int nb_dequeued = 0;
1271
1272         nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1273                         (void **)ops, nb_ops, NULL);
1274         qp->stats.dequeued_count += nb_dequeued;
1275
1276         return nb_dequeued;
1277 }
1278
1279 /** Create OPENSSL crypto device */
1280 static int
1281 cryptodev_openssl_create(const char *name,
1282                         struct rte_vdev_device *vdev,
1283                         struct rte_crypto_vdev_init_params *init_params)
1284 {
1285         struct rte_cryptodev *dev;
1286         struct openssl_private *internals;
1287
1288         if (init_params->name[0] == '\0')
1289                 snprintf(init_params->name, sizeof(init_params->name),
1290                                 "%s", name);
1291
1292         dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1293                         sizeof(struct openssl_private),
1294                         init_params->socket_id,
1295                         vdev);
1296         if (dev == NULL) {
1297                 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1298                 goto init_error;
1299         }
1300
1301         dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
1302         dev->dev_ops = rte_openssl_pmd_ops;
1303
1304         /* register rx/tx burst functions for data path */
1305         dev->dequeue_burst = openssl_pmd_dequeue_burst;
1306         dev->enqueue_burst = openssl_pmd_enqueue_burst;
1307
1308         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1309                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1310                         RTE_CRYPTODEV_FF_CPU_AESNI |
1311                         RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1312
1313         /* Set vector instructions mode supported */
1314         internals = dev->data->dev_private;
1315
1316         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1317         internals->max_nb_sessions = init_params->max_nb_sessions;
1318
1319         return 0;
1320
1321 init_error:
1322         OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1323                         init_params->name);
1324
1325         cryptodev_openssl_remove(vdev);
1326         return -EFAULT;
1327 }
1328
1329 /** Initialise OPENSSL crypto device */
1330 static int
1331 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1332 {
1333         struct rte_crypto_vdev_init_params init_params = {
1334                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1335                 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1336                 rte_socket_id(),
1337                 {0}
1338         };
1339         const char *name;
1340         const char *input_args;
1341
1342         name = rte_vdev_device_name(vdev);
1343         if (name == NULL)
1344                 return -EINVAL;
1345         input_args = rte_vdev_device_args(vdev);
1346
1347         rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1348
1349         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1350                         init_params.socket_id);
1351         if (init_params.name[0] != '\0')
1352                 RTE_LOG(INFO, PMD, "  User defined name = %s\n",
1353                         init_params.name);
1354         RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
1355                         init_params.max_nb_queue_pairs);
1356         RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
1357                         init_params.max_nb_sessions);
1358
1359         return cryptodev_openssl_create(name, vdev, &init_params);
1360 }
1361
1362 /** Uninitialise OPENSSL crypto device */
1363 static int
1364 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1365 {
1366         const char *name;
1367
1368         name = rte_vdev_device_name(vdev);
1369         if (name == NULL)
1370                 return -EINVAL;
1371
1372         RTE_LOG(INFO, PMD,
1373                 "Closing OPENSSL crypto device %s on numa socket %u\n",
1374                 name, rte_socket_id());
1375
1376         return 0;
1377 }
1378
1379 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1380         .probe = cryptodev_openssl_probe,
1381         .remove = cryptodev_openssl_remove
1382 };
1383
1384 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1385         cryptodev_openssl_pmd_drv);
1386 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1387         "max_nb_queue_pairs=<int> "
1388         "max_nb_sessions=<int> "
1389         "socket_id=<int>");