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