xref: /dpdk/drivers/crypto/openssl/rte_openssl_pmd.c (revision 6cd8b4d8ba105b616f8d6b52a2e1c7c9ed91c3a5)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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_vdev.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
40 
41 #include <openssl/evp.h>
42 
43 #include "rte_openssl_pmd_private.h"
44 
45 static int cryptodev_openssl_remove(const char *name);
46 
47 /*----------------------------------------------------------------------------*/
48 
49 /**
50  * Global static parameter used to create a unique name for each
51  * OPENSSL crypto device.
52  */
53 static unsigned int unique_name_id;
54 
55 static inline int
56 create_unique_device_name(char *name, size_t size)
57 {
58 	int ret;
59 
60 	if (name == NULL)
61 		return -EINVAL;
62 
63 	ret = snprintf(name, size, "%s_%u",
64 			RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD),
65 			unique_name_id++);
66 	if (ret < 0)
67 		return ret;
68 	return 0;
69 }
70 
71 /**
72  * Increment counter by 1
73  * Counter is 64 bit array, big-endian
74  */
75 static void
76 ctr_inc(uint8_t *ctr)
77 {
78 	uint64_t *ctr64 = (uint64_t *)ctr;
79 
80 	*ctr64 = __builtin_bswap64(*ctr64);
81 	(*ctr64)++;
82 	*ctr64 = __builtin_bswap64(*ctr64);
83 }
84 
85 /*
86  *------------------------------------------------------------------------------
87  * Session Prepare
88  *------------------------------------------------------------------------------
89  */
90 
91 /** Get xform chain order */
92 static enum openssl_chain_order
93 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
94 {
95 	enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
96 
97 	if (xform != NULL) {
98 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
99 			if (xform->next == NULL)
100 				res =  OPENSSL_CHAIN_ONLY_AUTH;
101 			else if (xform->next->type ==
102 					RTE_CRYPTO_SYM_XFORM_CIPHER)
103 				res =  OPENSSL_CHAIN_AUTH_CIPHER;
104 		}
105 		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
106 			if (xform->next == NULL)
107 				res =  OPENSSL_CHAIN_ONLY_CIPHER;
108 			else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
109 				res =  OPENSSL_CHAIN_CIPHER_AUTH;
110 		}
111 	}
112 
113 	return res;
114 }
115 
116 /** Get session cipher key from input cipher key */
117 static void
118 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
119 {
120 	memcpy(session_key, input_key, keylen);
121 }
122 
123 /** Get key ede 24 bytes standard from input key */
124 static int
125 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
126 {
127 	int res = 0;
128 
129 	/* Initialize keys - 24 bytes: [key1-key2-key3] */
130 	switch (keylen) {
131 	case 24:
132 		memcpy(key_ede, key, 24);
133 		break;
134 	case 16:
135 		/* K3 = K1 */
136 		memcpy(key_ede, key, 16);
137 		memcpy(key_ede + 16, key, 8);
138 		break;
139 	case 8:
140 		/* K1 = K2 = K3 (DES compatibility) */
141 		memcpy(key_ede, key, 8);
142 		memcpy(key_ede + 8, key, 8);
143 		memcpy(key_ede + 16, key, 8);
144 		break;
145 	default:
146 		OPENSSL_LOG_ERR("Unsupported key size");
147 		res = -EINVAL;
148 	}
149 
150 	return res;
151 }
152 
153 /** Get adequate openssl function for input cipher algorithm */
154 static uint8_t
155 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
156 		const EVP_CIPHER **algo)
157 {
158 	int res = 0;
159 
160 	if (algo != NULL) {
161 		switch (sess_algo) {
162 		case RTE_CRYPTO_CIPHER_3DES_CBC:
163 			switch (keylen) {
164 			case 16:
165 				*algo = EVP_des_ede_cbc();
166 				break;
167 			case 24:
168 				*algo = EVP_des_ede3_cbc();
169 				break;
170 			default:
171 				res = -EINVAL;
172 			}
173 			break;
174 		case RTE_CRYPTO_CIPHER_3DES_CTR:
175 			break;
176 		case RTE_CRYPTO_CIPHER_AES_CBC:
177 			switch (keylen) {
178 			case 16:
179 				*algo = EVP_aes_128_cbc();
180 				break;
181 			case 24:
182 				*algo = EVP_aes_192_cbc();
183 				break;
184 			case 32:
185 				*algo = EVP_aes_256_cbc();
186 				break;
187 			default:
188 				res = -EINVAL;
189 			}
190 			break;
191 		case RTE_CRYPTO_CIPHER_AES_CTR:
192 			switch (keylen) {
193 			case 16:
194 				*algo = EVP_aes_128_ctr();
195 				break;
196 			case 24:
197 				*algo = EVP_aes_192_ctr();
198 				break;
199 			case 32:
200 				*algo = EVP_aes_256_ctr();
201 				break;
202 			default:
203 				res = -EINVAL;
204 			}
205 			break;
206 		case RTE_CRYPTO_CIPHER_AES_GCM:
207 			switch (keylen) {
208 			case 16:
209 				*algo = EVP_aes_128_gcm();
210 				break;
211 			case 24:
212 				*algo = EVP_aes_192_gcm();
213 				break;
214 			case 32:
215 				*algo = EVP_aes_256_gcm();
216 				break;
217 			default:
218 				res = -EINVAL;
219 			}
220 			break;
221 		default:
222 			res = -EINVAL;
223 			break;
224 		}
225 	} else {
226 		res = -EINVAL;
227 	}
228 
229 	return res;
230 }
231 
232 /** Get adequate openssl function for input auth algorithm */
233 static uint8_t
234 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
235 		const EVP_MD **algo)
236 {
237 	int res = 0;
238 
239 	if (algo != NULL) {
240 		switch (sessalgo) {
241 		case RTE_CRYPTO_AUTH_MD5:
242 		case RTE_CRYPTO_AUTH_MD5_HMAC:
243 			*algo = EVP_md5();
244 			break;
245 		case RTE_CRYPTO_AUTH_SHA1:
246 		case RTE_CRYPTO_AUTH_SHA1_HMAC:
247 			*algo = EVP_sha1();
248 			break;
249 		case RTE_CRYPTO_AUTH_SHA224:
250 		case RTE_CRYPTO_AUTH_SHA224_HMAC:
251 			*algo = EVP_sha224();
252 			break;
253 		case RTE_CRYPTO_AUTH_SHA256:
254 		case RTE_CRYPTO_AUTH_SHA256_HMAC:
255 			*algo = EVP_sha256();
256 			break;
257 		case RTE_CRYPTO_AUTH_SHA384:
258 		case RTE_CRYPTO_AUTH_SHA384_HMAC:
259 			*algo = EVP_sha384();
260 			break;
261 		case RTE_CRYPTO_AUTH_SHA512:
262 		case RTE_CRYPTO_AUTH_SHA512_HMAC:
263 			*algo = EVP_sha512();
264 			break;
265 		default:
266 			res = -EINVAL;
267 			break;
268 		}
269 	} else {
270 		res = -EINVAL;
271 	}
272 
273 	return res;
274 }
275 
276 /** Set session cipher parameters */
277 static int
278 openssl_set_session_cipher_parameters(struct openssl_session *sess,
279 		const struct rte_crypto_sym_xform *xform)
280 {
281 	/* Select cipher direction */
282 	sess->cipher.direction = xform->cipher.op;
283 	/* Select cipher key */
284 	sess->cipher.key.length = xform->cipher.key.length;
285 
286 	/* Select cipher algo */
287 	switch (xform->cipher.algo) {
288 	case RTE_CRYPTO_CIPHER_3DES_CBC:
289 	case RTE_CRYPTO_CIPHER_AES_CBC:
290 	case RTE_CRYPTO_CIPHER_AES_CTR:
291 	case RTE_CRYPTO_CIPHER_AES_GCM:
292 		sess->cipher.mode = OPENSSL_CIPHER_LIB;
293 		sess->cipher.algo = xform->cipher.algo;
294 		sess->cipher.ctx = EVP_CIPHER_CTX_new();
295 
296 		if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
297 				&sess->cipher.evp_algo) != 0)
298 			return -EINVAL;
299 
300 		get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
301 			sess->cipher.key.data);
302 
303 		break;
304 
305 	case RTE_CRYPTO_CIPHER_3DES_CTR:
306 		sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
307 		sess->cipher.ctx = EVP_CIPHER_CTX_new();
308 
309 		if (get_cipher_key_ede(xform->cipher.key.data,
310 				sess->cipher.key.length,
311 				sess->cipher.key.data) != 0)
312 			return -EINVAL;
313 		break;
314 
315 	default:
316 		sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
317 		return -EINVAL;
318 	}
319 
320 	return 0;
321 }
322 
323 /* Set session auth parameters */
324 static int
325 openssl_set_session_auth_parameters(struct openssl_session *sess,
326 		const struct rte_crypto_sym_xform *xform)
327 {
328 	/* Select auth generate/verify */
329 	sess->auth.operation = xform->auth.op;
330 	sess->auth.algo = xform->auth.algo;
331 
332 	/* Select auth algo */
333 	switch (xform->auth.algo) {
334 	case RTE_CRYPTO_AUTH_AES_GMAC:
335 	case RTE_CRYPTO_AUTH_AES_GCM:
336 		/* Check additional condition for AES_GMAC/GCM */
337 		if (sess->cipher.algo != RTE_CRYPTO_CIPHER_AES_GCM)
338 			return -EINVAL;
339 		sess->chain_order = OPENSSL_CHAIN_COMBINED;
340 		break;
341 
342 	case RTE_CRYPTO_AUTH_MD5:
343 	case RTE_CRYPTO_AUTH_SHA1:
344 	case RTE_CRYPTO_AUTH_SHA224:
345 	case RTE_CRYPTO_AUTH_SHA256:
346 	case RTE_CRYPTO_AUTH_SHA384:
347 	case RTE_CRYPTO_AUTH_SHA512:
348 		sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
349 		if (get_auth_algo(xform->auth.algo,
350 				&sess->auth.auth.evp_algo) != 0)
351 			return -EINVAL;
352 		sess->auth.auth.ctx = EVP_MD_CTX_create();
353 		break;
354 
355 	case RTE_CRYPTO_AUTH_MD5_HMAC:
356 	case RTE_CRYPTO_AUTH_SHA1_HMAC:
357 	case RTE_CRYPTO_AUTH_SHA224_HMAC:
358 	case RTE_CRYPTO_AUTH_SHA256_HMAC:
359 	case RTE_CRYPTO_AUTH_SHA384_HMAC:
360 	case RTE_CRYPTO_AUTH_SHA512_HMAC:
361 		sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
362 		sess->auth.hmac.ctx = EVP_MD_CTX_create();
363 		if (get_auth_algo(xform->auth.algo,
364 				&sess->auth.hmac.evp_algo) != 0)
365 			return -EINVAL;
366 		sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
367 				xform->auth.key.data, xform->auth.key.length);
368 		break;
369 
370 	default:
371 		return -EINVAL;
372 	}
373 
374 	return 0;
375 }
376 
377 /** Parse crypto xform chain and set private session parameters */
378 int
379 openssl_set_session_parameters(struct openssl_session *sess,
380 		const struct rte_crypto_sym_xform *xform)
381 {
382 	const struct rte_crypto_sym_xform *cipher_xform = NULL;
383 	const struct rte_crypto_sym_xform *auth_xform = NULL;
384 
385 	sess->chain_order = openssl_get_chain_order(xform);
386 	switch (sess->chain_order) {
387 	case OPENSSL_CHAIN_ONLY_CIPHER:
388 		cipher_xform = xform;
389 		break;
390 	case OPENSSL_CHAIN_ONLY_AUTH:
391 		auth_xform = xform;
392 		break;
393 	case OPENSSL_CHAIN_CIPHER_AUTH:
394 		cipher_xform = xform;
395 		auth_xform = xform->next;
396 		break;
397 	case OPENSSL_CHAIN_AUTH_CIPHER:
398 		auth_xform = xform;
399 		cipher_xform = xform->next;
400 		break;
401 	default:
402 		return -EINVAL;
403 	}
404 
405 	/* cipher_xform must be check before auth_xform */
406 	if (cipher_xform) {
407 		if (openssl_set_session_cipher_parameters(
408 				sess, cipher_xform)) {
409 			OPENSSL_LOG_ERR(
410 				"Invalid/unsupported cipher parameters");
411 			return -EINVAL;
412 		}
413 	}
414 
415 	if (auth_xform) {
416 		if (openssl_set_session_auth_parameters(sess, auth_xform)) {
417 			OPENSSL_LOG_ERR(
418 				"Invalid/unsupported auth parameters");
419 			return -EINVAL;
420 		}
421 	}
422 
423 	return 0;
424 }
425 
426 /** Reset private session parameters */
427 void
428 openssl_reset_session(struct openssl_session *sess)
429 {
430 	EVP_CIPHER_CTX_free(sess->cipher.ctx);
431 
432 	switch (sess->auth.mode) {
433 	case OPENSSL_AUTH_AS_AUTH:
434 		EVP_MD_CTX_destroy(sess->auth.auth.ctx);
435 		break;
436 	case OPENSSL_AUTH_AS_HMAC:
437 		EVP_PKEY_free(sess->auth.hmac.pkey);
438 		EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
439 		break;
440 	default:
441 		break;
442 	}
443 }
444 
445 /** Provide session for operation */
446 static struct openssl_session *
447 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
448 {
449 	struct openssl_session *sess = NULL;
450 
451 	if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
452 		/* get existing session */
453 		if (likely(op->sym->session != NULL &&
454 				op->sym->session->dev_type ==
455 				RTE_CRYPTODEV_OPENSSL_PMD))
456 			sess = (struct openssl_session *)
457 				op->sym->session->_private;
458 	} else  {
459 		/* provide internal session */
460 		void *_sess = NULL;
461 
462 		if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
463 			sess = (struct openssl_session *)
464 				((struct rte_cryptodev_sym_session *)_sess)
465 				->_private;
466 
467 			if (unlikely(openssl_set_session_parameters(
468 					sess, op->sym->xform) != 0)) {
469 				rte_mempool_put(qp->sess_mp, _sess);
470 				sess = NULL;
471 			} else
472 				op->sym->session = _sess;
473 		}
474 	}
475 
476 	if (sess == NULL)
477 		op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
478 
479 	return sess;
480 }
481 
482 /*
483  *------------------------------------------------------------------------------
484  * Process Operations
485  *------------------------------------------------------------------------------
486  */
487 
488 /** Process standard openssl cipher encryption */
489 static int
490 process_openssl_cipher_encrypt(uint8_t *src, uint8_t *dst,
491 		uint8_t *iv, uint8_t *key, int srclen,
492 		EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
493 {
494 	int dstlen, totlen;
495 
496 	if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
497 		goto process_cipher_encrypt_err;
498 
499 	EVP_CIPHER_CTX_set_padding(ctx, 0);
500 
501 	if (EVP_EncryptUpdate(ctx, dst, &dstlen, src, srclen) <= 0)
502 		goto process_cipher_encrypt_err;
503 
504 	if (EVP_EncryptFinal_ex(ctx, dst + dstlen, &totlen) <= 0)
505 		goto process_cipher_encrypt_err;
506 
507 	return 0;
508 
509 process_cipher_encrypt_err:
510 	OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
511 	return -EINVAL;
512 }
513 
514 /** Process standard openssl cipher decryption */
515 static int
516 process_openssl_cipher_decrypt(uint8_t *src, uint8_t *dst,
517 		uint8_t *iv, uint8_t *key, int srclen,
518 		EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
519 {
520 	int dstlen, totlen;
521 
522 	if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
523 		goto process_cipher_decrypt_err;
524 
525 	if (EVP_CIPHER_CTX_set_padding(ctx, 0) <= 0)
526 		goto process_cipher_decrypt_err;
527 
528 	if (EVP_DecryptUpdate(ctx, dst, &dstlen, src, srclen) <= 0)
529 		goto process_cipher_decrypt_err;
530 
531 	if (EVP_DecryptFinal_ex(ctx, dst + dstlen, &totlen) <= 0)
532 		goto process_cipher_decrypt_err;
533 
534 	return 0;
535 
536 process_cipher_decrypt_err:
537 	OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
538 	return -EINVAL;
539 }
540 
541 /** Process cipher des 3 ctr encryption, decryption algorithm */
542 static int
543 process_openssl_cipher_des3ctr(uint8_t *src, uint8_t *dst,
544 		uint8_t *iv, uint8_t *key, int srclen, EVP_CIPHER_CTX *ctx)
545 {
546 	uint8_t ebuf[8], ctr[8];
547 	int unused, n;
548 
549 	/* We use 3DES encryption also for decryption.
550 	 * IV is not important for 3DES ecb
551 	 */
552 	if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
553 		goto process_cipher_des3ctr_err;
554 
555 	memcpy(ctr, iv, 8);
556 	n = 0;
557 
558 	while (n < srclen) {
559 		if (n % 8 == 0) {
560 			if (EVP_EncryptUpdate(ctx,
561 					(unsigned char *)&ebuf, &unused,
562 					(const unsigned char *)&ctr, 8) <= 0)
563 				goto process_cipher_des3ctr_err;
564 			ctr_inc(ctr);
565 		}
566 		dst[n] = src[n] ^ ebuf[n % 8];
567 		n++;
568 	}
569 
570 	return 0;
571 
572 process_cipher_des3ctr_err:
573 	OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
574 	return -EINVAL;
575 }
576 
577 /** Process auth/encription aes-gcm algorithm */
578 static int
579 process_openssl_auth_encryption_gcm(uint8_t *src, int srclen,
580 		uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
581 		uint8_t *key, uint8_t *dst,	uint8_t *tag,
582 		EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
583 {
584 	int len = 0, unused = 0;
585 	uint8_t empty[] = {};
586 
587 	if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
588 		goto process_auth_encryption_gcm_err;
589 
590 	if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
591 		goto process_auth_encryption_gcm_err;
592 
593 	if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
594 		goto process_auth_encryption_gcm_err;
595 
596 	if (aadlen > 0) {
597 		if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
598 			goto process_auth_encryption_gcm_err;
599 
600 		/* Workaround open ssl bug in version less then 1.0.1f */
601 		if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
602 			goto process_auth_encryption_gcm_err;
603 	}
604 
605 	if (srclen > 0)
606 		if (EVP_EncryptUpdate(ctx, dst, &len, src, srclen) <= 0)
607 			goto process_auth_encryption_gcm_err;
608 
609 	if (EVP_EncryptFinal_ex(ctx, dst + len, &len) <= 0)
610 		goto process_auth_encryption_gcm_err;
611 
612 	if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
613 		goto process_auth_encryption_gcm_err;
614 
615 	return 0;
616 
617 process_auth_encryption_gcm_err:
618 	OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
619 	return -EINVAL;
620 }
621 
622 static int
623 process_openssl_auth_decryption_gcm(uint8_t *src, int srclen,
624 		uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
625 		uint8_t *key, uint8_t *dst, uint8_t *tag,
626 		EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
627 {
628 	int len = 0, unused = 0;
629 	uint8_t empty[] = {};
630 
631 	if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
632 		goto process_auth_decryption_gcm_err;
633 
634 	if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
635 		goto process_auth_decryption_gcm_err;
636 
637 	if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
638 		goto process_auth_decryption_gcm_err;
639 
640 	if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
641 		goto process_auth_decryption_gcm_err;
642 
643 	if (aadlen > 0) {
644 		if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
645 			goto process_auth_decryption_gcm_err;
646 
647 		/* Workaround open ssl bug in version less then 1.0.1f */
648 		if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
649 			goto process_auth_decryption_gcm_err;
650 	}
651 
652 	if (srclen > 0)
653 		if (EVP_DecryptUpdate(ctx, dst, &len, src, srclen) <= 0)
654 			goto process_auth_decryption_gcm_err;
655 
656 	if (EVP_DecryptFinal_ex(ctx, dst + len, &len) <= 0)
657 		goto process_auth_decryption_gcm_final_err;
658 
659 	return 0;
660 
661 process_auth_decryption_gcm_err:
662 	OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
663 	return -EINVAL;
664 
665 process_auth_decryption_gcm_final_err:
666 	return -EFAULT;
667 }
668 
669 /** Process standard openssl auth algorithms */
670 static int
671 process_openssl_auth(uint8_t *src, uint8_t *dst,
672 		__rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
673 		int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
674 {
675 	size_t dstlen;
676 
677 	if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
678 		goto process_auth_err;
679 
680 	if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
681 		goto process_auth_err;
682 
683 	if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
684 		goto process_auth_err;
685 
686 	return 0;
687 
688 process_auth_err:
689 	OPENSSL_LOG_ERR("Process openssl auth failed");
690 	return -EINVAL;
691 }
692 
693 /** Process standard openssl auth algorithms with hmac */
694 static int
695 process_openssl_auth_hmac(uint8_t *src, uint8_t *dst,
696 		__rte_unused uint8_t *iv, EVP_PKEY *pkey,
697 		int srclen,	EVP_MD_CTX *ctx, const EVP_MD *algo)
698 {
699 	size_t dstlen;
700 
701 	if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
702 		goto process_auth_err;
703 
704 	if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
705 		goto process_auth_err;
706 
707 	if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
708 		goto process_auth_err;
709 
710 	return 0;
711 
712 process_auth_err:
713 	OPENSSL_LOG_ERR("Process openssl auth failed");
714 	return -EINVAL;
715 }
716 
717 /*----------------------------------------------------------------------------*/
718 
719 /** Process auth/cipher combined operation */
720 static void
721 process_openssl_combined_op
722 		(struct rte_crypto_op *op, struct openssl_session *sess,
723 		struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
724 {
725 	/* cipher */
726 	uint8_t *src = NULL, *dst = NULL, *iv, *tag, *aad;
727 	int srclen, ivlen, aadlen, status = -1;
728 
729 	iv = op->sym->cipher.iv.data;
730 	ivlen = op->sym->cipher.iv.length;
731 	aad = op->sym->auth.aad.data;
732 	aadlen = op->sym->auth.aad.length;
733 
734 	tag = op->sym->auth.digest.data;
735 	if (tag == NULL)
736 		tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
737 				op->sym->cipher.data.offset +
738 				op->sym->cipher.data.length);
739 
740 	if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
741 		srclen = 0;
742 	else {
743 		srclen = op->sym->cipher.data.length;
744 		src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
745 				op->sym->cipher.data.offset);
746 		dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
747 				op->sym->cipher.data.offset);
748 	}
749 
750 	if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
751 		status = process_openssl_auth_encryption_gcm(
752 				src, srclen, aad, aadlen, iv, ivlen,
753 				sess->cipher.key.data, dst, tag,
754 				sess->cipher.ctx, sess->cipher.evp_algo);
755 	else
756 		status = process_openssl_auth_decryption_gcm(
757 				src, srclen, aad, aadlen, iv, ivlen,
758 				sess->cipher.key.data, dst, tag,
759 				sess->cipher.ctx, sess->cipher.evp_algo);
760 
761 	if (status != 0) {
762 		if (status == (-EFAULT) &&
763 				sess->auth.operation ==
764 						RTE_CRYPTO_AUTH_OP_VERIFY)
765 			op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
766 		else
767 			op->status = RTE_CRYPTO_OP_STATUS_ERROR;
768 	}
769 }
770 
771 /** Process cipher operation */
772 static void
773 process_openssl_cipher_op
774 		(struct rte_crypto_op *op, struct openssl_session *sess,
775 		struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
776 {
777 	uint8_t *src, *dst, *iv;
778 	int srclen, status;
779 
780 	srclen = op->sym->cipher.data.length;
781 	src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
782 			op->sym->cipher.data.offset);
783 	dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
784 			op->sym->cipher.data.offset);
785 
786 	iv = op->sym->cipher.iv.data;
787 
788 	if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
789 		if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
790 			status = process_openssl_cipher_encrypt(src, dst, iv,
791 					sess->cipher.key.data, srclen,
792 					sess->cipher.ctx,
793 					sess->cipher.evp_algo);
794 		else
795 			status = process_openssl_cipher_decrypt(src, dst, iv,
796 					sess->cipher.key.data, srclen,
797 					sess->cipher.ctx,
798 					sess->cipher.evp_algo);
799 	else
800 		status = process_openssl_cipher_des3ctr(src, dst, iv,
801 				sess->cipher.key.data, srclen,
802 				sess->cipher.ctx);
803 
804 	if (status != 0)
805 		op->status = RTE_CRYPTO_OP_STATUS_ERROR;
806 }
807 
808 /** Process auth operation */
809 static void
810 process_openssl_auth_op
811 		(struct rte_crypto_op *op, struct openssl_session *sess,
812 		struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
813 {
814 	uint8_t *src, *dst;
815 	int srclen, status;
816 
817 	srclen = op->sym->auth.data.length;
818 	src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
819 			op->sym->auth.data.offset);
820 
821 	if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
822 		dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
823 				op->sym->auth.digest.length);
824 	else {
825 		dst = op->sym->auth.digest.data;
826 		if (dst == NULL)
827 			dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
828 					op->sym->auth.data.offset +
829 					op->sym->auth.data.length);
830 	}
831 
832 	switch (sess->auth.mode) {
833 	case OPENSSL_AUTH_AS_AUTH:
834 		status = process_openssl_auth(src, dst,
835 				NULL, NULL,	srclen,
836 				sess->auth.auth.ctx, sess->auth.auth.evp_algo);
837 		break;
838 	case OPENSSL_AUTH_AS_HMAC:
839 		status = process_openssl_auth_hmac(src, dst,
840 				NULL, sess->auth.hmac.pkey, srclen,
841 				sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
842 		break;
843 	default:
844 		status = -1;
845 		break;
846 	}
847 
848 	if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
849 		if (memcmp(dst, op->sym->auth.digest.data,
850 				op->sym->auth.digest.length) != 0) {
851 			op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
852 		}
853 		/* Trim area used for digest from mbuf. */
854 		rte_pktmbuf_trim(mbuf_src,
855 				op->sym->auth.digest.length);
856 	}
857 
858 	if (status != 0)
859 		op->status = RTE_CRYPTO_OP_STATUS_ERROR;
860 }
861 
862 /** Process crypto operation for mbuf */
863 static int
864 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
865 		struct openssl_session *sess)
866 {
867 	struct rte_mbuf *msrc, *mdst;
868 	int retval;
869 
870 	msrc = op->sym->m_src;
871 	mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
872 
873 	op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
874 
875 	switch (sess->chain_order) {
876 	case OPENSSL_CHAIN_ONLY_CIPHER:
877 		process_openssl_cipher_op(op, sess, msrc, mdst);
878 		break;
879 	case OPENSSL_CHAIN_ONLY_AUTH:
880 		process_openssl_auth_op(op, sess, msrc, mdst);
881 		break;
882 	case OPENSSL_CHAIN_CIPHER_AUTH:
883 		process_openssl_cipher_op(op, sess, msrc, mdst);
884 		process_openssl_auth_op(op, sess, mdst, mdst);
885 		break;
886 	case OPENSSL_CHAIN_AUTH_CIPHER:
887 		process_openssl_auth_op(op, sess, msrc, mdst);
888 		process_openssl_cipher_op(op, sess, msrc, mdst);
889 		break;
890 	case OPENSSL_CHAIN_COMBINED:
891 		process_openssl_combined_op(op, sess, msrc, mdst);
892 		break;
893 	default:
894 		op->status = RTE_CRYPTO_OP_STATUS_ERROR;
895 		break;
896 	}
897 
898 	/* Free session if a session-less crypto op */
899 	if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
900 		openssl_reset_session(sess);
901 		memset(sess, 0, sizeof(struct openssl_session));
902 		rte_mempool_put(qp->sess_mp, op->sym->session);
903 		op->sym->session = NULL;
904 	}
905 
906 
907 	if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
908 		op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
909 
910 	if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
911 		retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
912 	else
913 		retval = -1;
914 
915 	return retval;
916 }
917 
918 /*
919  *------------------------------------------------------------------------------
920  * PMD Framework
921  *------------------------------------------------------------------------------
922  */
923 
924 /** Enqueue burst */
925 static uint16_t
926 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
927 		uint16_t nb_ops)
928 {
929 	struct openssl_session *sess;
930 	struct openssl_qp *qp = queue_pair;
931 	int i, retval;
932 
933 	for (i = 0; i < nb_ops; i++) {
934 		sess = get_session(qp, ops[i]);
935 		if (unlikely(sess == NULL))
936 			goto enqueue_err;
937 
938 		retval = process_op(qp, ops[i], sess);
939 		if (unlikely(retval < 0))
940 			goto enqueue_err;
941 	}
942 
943 	qp->stats.enqueued_count += i;
944 	return i;
945 
946 enqueue_err:
947 	qp->stats.enqueue_err_count++;
948 	return i;
949 }
950 
951 /** Dequeue burst */
952 static uint16_t
953 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
954 		uint16_t nb_ops)
955 {
956 	struct openssl_qp *qp = queue_pair;
957 
958 	unsigned int nb_dequeued = 0;
959 
960 	nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
961 			(void **)ops, nb_ops);
962 	qp->stats.dequeued_count += nb_dequeued;
963 
964 	return nb_dequeued;
965 }
966 
967 /** Create OPENSSL crypto device */
968 static int
969 cryptodev_openssl_create(const char *name,
970 		struct rte_crypto_vdev_init_params *init_params)
971 {
972 	struct rte_cryptodev *dev;
973 	char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
974 	struct openssl_private *internals;
975 
976 	/* create a unique device name */
977 	if (create_unique_device_name(crypto_dev_name,
978 			RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
979 		OPENSSL_LOG_ERR("failed to create unique cryptodev name");
980 		return -EINVAL;
981 	}
982 
983 	dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
984 			sizeof(struct openssl_private),
985 			init_params->socket_id);
986 	if (dev == NULL) {
987 		OPENSSL_LOG_ERR("failed to create cryptodev vdev");
988 		goto init_error;
989 	}
990 
991 	dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
992 	dev->dev_ops = rte_openssl_pmd_ops;
993 
994 	/* register rx/tx burst functions for data path */
995 	dev->dequeue_burst = openssl_pmd_dequeue_burst;
996 	dev->enqueue_burst = openssl_pmd_enqueue_burst;
997 
998 	dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
999 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1000 			RTE_CRYPTODEV_FF_CPU_AESNI;
1001 
1002 	/* Set vector instructions mode supported */
1003 	internals = dev->data->dev_private;
1004 
1005 	internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1006 	internals->max_nb_sessions = init_params->max_nb_sessions;
1007 
1008 	return 0;
1009 
1010 init_error:
1011 	OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed", name);
1012 
1013 	cryptodev_openssl_remove(crypto_dev_name);
1014 	return -EFAULT;
1015 }
1016 
1017 /** Initialise OPENSSL crypto device */
1018 static int
1019 cryptodev_openssl_probe(const char *name,
1020 		const char *input_args)
1021 {
1022 	struct rte_crypto_vdev_init_params init_params = {
1023 		RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1024 		RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1025 		rte_socket_id()
1026 	};
1027 
1028 	rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
1029 
1030 	RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1031 			init_params.socket_id);
1032 	RTE_LOG(INFO, PMD, "  Max number of queue pairs = %d\n",
1033 			init_params.max_nb_queue_pairs);
1034 	RTE_LOG(INFO, PMD, "  Max number of sessions = %d\n",
1035 			init_params.max_nb_sessions);
1036 
1037 	return cryptodev_openssl_create(name, &init_params);
1038 }
1039 
1040 /** Uninitialise OPENSSL crypto device */
1041 static int
1042 cryptodev_openssl_remove(const char *name)
1043 {
1044 	if (name == NULL)
1045 		return -EINVAL;
1046 
1047 	RTE_LOG(INFO, PMD,
1048 		"Closing OPENSSL crypto device %s on numa socket %u\n",
1049 		name, rte_socket_id());
1050 
1051 	return 0;
1052 }
1053 
1054 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1055 	.probe = cryptodev_openssl_probe,
1056 	.remove = cryptodev_openssl_remove
1057 };
1058 
1059 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1060 	cryptodev_openssl_pmd_drv);
1061 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1062 	"max_nb_queue_pairs=<int> "
1063 	"max_nb_sessions=<int> "
1064 	"socket_id=<int>");
1065