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