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