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