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