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