4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
41 #include <openssl/evp.h>
43 #include "rte_openssl_pmd_private.h"
45 static int cryptodev_openssl_remove(const char *name);
47 /*----------------------------------------------------------------------------*/
50 * Global static parameter used to create a unique name for each
51 * OPENSSL crypto device.
53 static unsigned int unique_name_id;
56 create_unique_device_name(char *name, size_t size)
63 ret = snprintf(name, size, "%s_%u",
64 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD),
72 * Increment counter by 1
73 * Counter is 64 bit array, big-endian
78 uint64_t *ctr64 = (uint64_t *)ctr;
80 *ctr64 = __builtin_bswap64(*ctr64);
82 *ctr64 = __builtin_bswap64(*ctr64);
86 *------------------------------------------------------------------------------
88 *------------------------------------------------------------------------------
91 /** Get xform chain order */
92 static enum openssl_chain_order
93 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
95 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
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;
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;
116 /** Get session cipher key from input cipher key */
118 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
120 memcpy(session_key, input_key, keylen);
123 /** Get key ede 24 bytes standard from input key */
125 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
129 /* Initialize keys - 24 bytes: [key1-key2-key3] */
132 memcpy(key_ede, key, 24);
136 memcpy(key_ede, key, 16);
137 memcpy(key_ede + 16, key, 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);
146 OPENSSL_LOG_ERR("Unsupported key size");
153 /** Get adequate openssl function for input cipher algorithm */
155 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
156 const EVP_CIPHER **algo)
162 case RTE_CRYPTO_CIPHER_3DES_CBC:
165 *algo = EVP_des_ede_cbc();
168 *algo = EVP_des_ede3_cbc();
174 case RTE_CRYPTO_CIPHER_3DES_CTR:
176 case RTE_CRYPTO_CIPHER_AES_CBC:
179 *algo = EVP_aes_128_cbc();
182 *algo = EVP_aes_192_cbc();
185 *algo = EVP_aes_256_cbc();
191 case RTE_CRYPTO_CIPHER_AES_CTR:
194 *algo = EVP_aes_128_ctr();
197 *algo = EVP_aes_192_ctr();
200 *algo = EVP_aes_256_ctr();
206 case RTE_CRYPTO_CIPHER_AES_GCM:
209 *algo = EVP_aes_128_gcm();
212 *algo = EVP_aes_192_gcm();
215 *algo = EVP_aes_256_gcm();
232 /** Get adequate openssl function for input auth algorithm */
234 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
241 case RTE_CRYPTO_AUTH_MD5:
242 case RTE_CRYPTO_AUTH_MD5_HMAC:
245 case RTE_CRYPTO_AUTH_SHA1:
246 case RTE_CRYPTO_AUTH_SHA1_HMAC:
249 case RTE_CRYPTO_AUTH_SHA224:
250 case RTE_CRYPTO_AUTH_SHA224_HMAC:
251 *algo = EVP_sha224();
253 case RTE_CRYPTO_AUTH_SHA256:
254 case RTE_CRYPTO_AUTH_SHA256_HMAC:
255 *algo = EVP_sha256();
257 case RTE_CRYPTO_AUTH_SHA384:
258 case RTE_CRYPTO_AUTH_SHA384_HMAC:
259 *algo = EVP_sha384();
261 case RTE_CRYPTO_AUTH_SHA512:
262 case RTE_CRYPTO_AUTH_SHA512_HMAC:
263 *algo = EVP_sha512();
276 /** Set session cipher parameters */
278 openssl_set_session_cipher_parameters(struct openssl_session *sess,
279 const struct rte_crypto_sym_xform *xform)
281 /* Select cipher direction */
282 sess->cipher.direction = xform->cipher.op;
283 /* Select cipher key */
284 sess->cipher.key.length = xform->cipher.key.length;
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();
296 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
297 &sess->cipher.evp_algo) != 0)
300 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
301 sess->cipher.key.data);
305 case RTE_CRYPTO_CIPHER_3DES_CTR:
306 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
307 sess->cipher.ctx = EVP_CIPHER_CTX_new();
309 if (get_cipher_key_ede(xform->cipher.key.data,
310 sess->cipher.key.length,
311 sess->cipher.key.data) != 0)
316 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
323 /* Set session auth parameters */
325 openssl_set_session_auth_parameters(struct openssl_session *sess,
326 const struct rte_crypto_sym_xform *xform)
328 /* Select auth generate/verify */
329 sess->auth.operation = xform->auth.op;
330 sess->auth.algo = xform->auth.algo;
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)
339 sess->chain_order = OPENSSL_CHAIN_COMBINED;
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)
352 sess->auth.auth.ctx = EVP_MD_CTX_create();
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)
366 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
367 xform->auth.key.data, xform->auth.key.length);
377 /** Parse crypto xform chain and set private session parameters */
379 openssl_set_session_parameters(struct openssl_session *sess,
380 const struct rte_crypto_sym_xform *xform)
382 const struct rte_crypto_sym_xform *cipher_xform = NULL;
383 const struct rte_crypto_sym_xform *auth_xform = NULL;
385 sess->chain_order = openssl_get_chain_order(xform);
386 switch (sess->chain_order) {
387 case OPENSSL_CHAIN_ONLY_CIPHER:
388 cipher_xform = xform;
390 case OPENSSL_CHAIN_ONLY_AUTH:
393 case OPENSSL_CHAIN_CIPHER_AUTH:
394 cipher_xform = xform;
395 auth_xform = xform->next;
397 case OPENSSL_CHAIN_AUTH_CIPHER:
399 cipher_xform = xform->next;
405 /* cipher_xform must be check before auth_xform */
407 if (openssl_set_session_cipher_parameters(
408 sess, cipher_xform)) {
410 "Invalid/unsupported cipher parameters");
416 if (openssl_set_session_auth_parameters(sess, auth_xform)) {
418 "Invalid/unsupported auth parameters");
426 /** Reset private session parameters */
428 openssl_reset_session(struct openssl_session *sess)
430 EVP_CIPHER_CTX_free(sess->cipher.ctx);
432 switch (sess->auth.mode) {
433 case OPENSSL_AUTH_AS_AUTH:
434 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
436 case OPENSSL_AUTH_AS_HMAC:
437 EVP_PKEY_free(sess->auth.hmac.pkey);
438 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
445 /** Provide session for operation */
446 static struct openssl_session *
447 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
449 struct openssl_session *sess = NULL;
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;
459 /* provide internal session */
462 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
463 sess = (struct openssl_session *)
464 ((struct rte_cryptodev_sym_session *)_sess)
467 if (unlikely(openssl_set_session_parameters(
468 sess, op->sym->xform) != 0)) {
469 rte_mempool_put(qp->sess_mp, _sess);
472 op->sym->session = _sess;
477 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
483 *------------------------------------------------------------------------------
485 *------------------------------------------------------------------------------
488 /** Process standard openssl cipher encryption */
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)
496 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
497 goto process_cipher_encrypt_err;
499 EVP_CIPHER_CTX_set_padding(ctx, 0);
501 if (EVP_EncryptUpdate(ctx, dst, &dstlen, src, srclen) <= 0)
502 goto process_cipher_encrypt_err;
504 if (EVP_EncryptFinal_ex(ctx, dst + dstlen, &totlen) <= 0)
505 goto process_cipher_encrypt_err;
509 process_cipher_encrypt_err:
510 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
514 /** Process standard openssl cipher decryption */
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)
522 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
523 goto process_cipher_decrypt_err;
525 EVP_CIPHER_CTX_set_padding(ctx, 0);
527 if (EVP_DecryptUpdate(ctx, dst, &dstlen, src, srclen) <= 0)
528 goto process_cipher_decrypt_err;
530 if (EVP_DecryptFinal_ex(ctx, dst + dstlen, &totlen) <= 0)
531 goto process_cipher_decrypt_err;
535 process_cipher_decrypt_err:
536 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
540 /** Process cipher des 3 ctr encryption, decryption algorithm */
542 process_openssl_cipher_des3ctr(uint8_t *src, uint8_t *dst,
543 uint8_t *iv, uint8_t *key, int srclen, EVP_CIPHER_CTX *ctx)
545 uint8_t ebuf[8], ctr[8];
548 /* We use 3DES encryption also for decryption.
549 * IV is not important for 3DES ecb
551 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
552 goto process_cipher_des3ctr_err;
559 if (EVP_EncryptUpdate(ctx,
560 (unsigned char *)&ebuf, &unused,
561 (const unsigned char *)&ctr, 8) <= 0)
562 goto process_cipher_des3ctr_err;
565 dst[n] = src[n] ^ ebuf[n % 8];
571 process_cipher_des3ctr_err:
572 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
576 /** Process auth/encription aes-gcm algorithm */
578 process_openssl_auth_encryption_gcm(uint8_t *src, int srclen,
579 uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
580 uint8_t *key, uint8_t *dst, uint8_t *tag,
581 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
583 int len = 0, unused = 0;
584 uint8_t empty[] = {};
586 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
587 goto process_auth_encryption_gcm_err;
589 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
590 goto process_auth_encryption_gcm_err;
592 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
593 goto process_auth_encryption_gcm_err;
596 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
597 goto process_auth_encryption_gcm_err;
599 /* Workaround open ssl bug in version less then 1.0.1f */
600 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
601 goto process_auth_encryption_gcm_err;
605 if (EVP_EncryptUpdate(ctx, dst, &len, src, srclen) <= 0)
606 goto process_auth_encryption_gcm_err;
608 if (EVP_EncryptFinal_ex(ctx, dst + len, &len) <= 0)
609 goto process_auth_encryption_gcm_err;
611 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
612 goto process_auth_encryption_gcm_err;
616 process_auth_encryption_gcm_err:
617 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
622 process_openssl_auth_decryption_gcm(uint8_t *src, int srclen,
623 uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
624 uint8_t *key, uint8_t *dst, uint8_t *tag,
625 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
627 int len = 0, unused = 0;
628 uint8_t empty[] = {};
630 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
631 goto process_auth_decryption_gcm_err;
633 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
634 goto process_auth_decryption_gcm_err;
636 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
637 goto process_auth_decryption_gcm_err;
639 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
640 goto process_auth_decryption_gcm_err;
643 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
644 goto process_auth_decryption_gcm_err;
646 /* Workaround open ssl bug in version less then 1.0.1f */
647 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
648 goto process_auth_decryption_gcm_err;
652 if (EVP_DecryptUpdate(ctx, dst, &len, src, srclen) <= 0)
653 goto process_auth_decryption_gcm_err;
655 if (EVP_DecryptFinal_ex(ctx, dst + len, &len) <= 0)
656 goto process_auth_decryption_gcm_final_err;
660 process_auth_decryption_gcm_err:
661 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
664 process_auth_decryption_gcm_final_err:
668 /** Process standard openssl auth algorithms */
670 process_openssl_auth(uint8_t *src, uint8_t *dst,
671 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
672 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
676 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
677 goto process_auth_err;
679 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
680 goto process_auth_err;
682 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
683 goto process_auth_err;
688 OPENSSL_LOG_ERR("Process openssl auth failed");
692 /** Process standard openssl auth algorithms with hmac */
694 process_openssl_auth_hmac(uint8_t *src, uint8_t *dst,
695 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
696 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
700 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
701 goto process_auth_err;
703 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
704 goto process_auth_err;
706 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
707 goto process_auth_err;
712 OPENSSL_LOG_ERR("Process openssl auth failed");
716 /*----------------------------------------------------------------------------*/
718 /** Process auth/cipher combined operation */
720 process_openssl_combined_op
721 (struct rte_crypto_op *op, struct openssl_session *sess,
722 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
725 uint8_t *src = NULL, *dst = NULL, *iv, *tag, *aad;
726 int srclen, ivlen, aadlen, status = -1;
728 iv = op->sym->cipher.iv.data;
729 ivlen = op->sym->cipher.iv.length;
730 aad = op->sym->auth.aad.data;
731 aadlen = op->sym->auth.aad.length;
733 tag = op->sym->auth.digest.data;
735 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
736 op->sym->cipher.data.offset +
737 op->sym->cipher.data.length);
739 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
742 srclen = op->sym->cipher.data.length;
743 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
744 op->sym->cipher.data.offset);
745 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
746 op->sym->cipher.data.offset);
749 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
750 status = process_openssl_auth_encryption_gcm(
751 src, srclen, aad, aadlen, iv, ivlen,
752 sess->cipher.key.data, dst, tag,
753 sess->cipher.ctx, sess->cipher.evp_algo);
755 status = process_openssl_auth_decryption_gcm(
756 src, srclen, aad, aadlen, iv, ivlen,
757 sess->cipher.key.data, dst, tag,
758 sess->cipher.ctx, sess->cipher.evp_algo);
761 if (status == (-EFAULT) &&
762 sess->auth.operation ==
763 RTE_CRYPTO_AUTH_OP_VERIFY)
764 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
766 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
770 /** Process cipher operation */
772 process_openssl_cipher_op
773 (struct rte_crypto_op *op, struct openssl_session *sess,
774 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
776 uint8_t *src, *dst, *iv;
779 srclen = op->sym->cipher.data.length;
780 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
781 op->sym->cipher.data.offset);
782 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
783 op->sym->cipher.data.offset);
785 iv = op->sym->cipher.iv.data;
787 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
788 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
789 status = process_openssl_cipher_encrypt(src, dst, iv,
790 sess->cipher.key.data, srclen,
792 sess->cipher.evp_algo);
794 status = process_openssl_cipher_decrypt(src, dst, iv,
795 sess->cipher.key.data, srclen,
797 sess->cipher.evp_algo);
799 status = process_openssl_cipher_des3ctr(src, dst, iv,
800 sess->cipher.key.data, srclen,
804 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
807 /** Process auth operation */
809 process_openssl_auth_op
810 (struct rte_crypto_op *op, struct openssl_session *sess,
811 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
816 srclen = op->sym->auth.data.length;
817 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
818 op->sym->auth.data.offset);
820 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
821 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
822 op->sym->auth.digest.length);
824 dst = op->sym->auth.digest.data;
826 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
827 op->sym->auth.data.offset +
828 op->sym->auth.data.length);
831 switch (sess->auth.mode) {
832 case OPENSSL_AUTH_AS_AUTH:
833 status = process_openssl_auth(src, dst,
835 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
837 case OPENSSL_AUTH_AS_HMAC:
838 status = process_openssl_auth_hmac(src, dst,
839 NULL, sess->auth.hmac.pkey, srclen,
840 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
847 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
848 if (memcmp(dst, op->sym->auth.digest.data,
849 op->sym->auth.digest.length) != 0) {
850 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
852 /* Trim area used for digest from mbuf. */
853 rte_pktmbuf_trim(mbuf_src,
854 op->sym->auth.digest.length);
858 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
861 /** Process crypto operation for mbuf */
863 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
864 struct openssl_session *sess)
866 struct rte_mbuf *msrc, *mdst;
869 msrc = op->sym->m_src;
870 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
872 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
874 switch (sess->chain_order) {
875 case OPENSSL_CHAIN_ONLY_CIPHER:
876 process_openssl_cipher_op(op, sess, msrc, mdst);
878 case OPENSSL_CHAIN_ONLY_AUTH:
879 process_openssl_auth_op(op, sess, msrc, mdst);
881 case OPENSSL_CHAIN_CIPHER_AUTH:
882 process_openssl_cipher_op(op, sess, msrc, mdst);
883 process_openssl_auth_op(op, sess, mdst, mdst);
885 case OPENSSL_CHAIN_AUTH_CIPHER:
886 process_openssl_auth_op(op, sess, msrc, mdst);
887 process_openssl_cipher_op(op, sess, msrc, mdst);
889 case OPENSSL_CHAIN_COMBINED:
890 process_openssl_combined_op(op, sess, msrc, mdst);
893 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
897 /* Free session if a session-less crypto op */
898 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
899 openssl_reset_session(sess);
900 memset(sess, 0, sizeof(struct openssl_session));
901 rte_mempool_put(qp->sess_mp, op->sym->session);
902 op->sym->session = NULL;
906 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
907 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
909 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
910 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
918 *------------------------------------------------------------------------------
920 *------------------------------------------------------------------------------
925 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
928 struct openssl_session *sess;
929 struct openssl_qp *qp = queue_pair;
932 for (i = 0; i < nb_ops; i++) {
933 sess = get_session(qp, ops[i]);
934 if (unlikely(sess == NULL))
937 retval = process_op(qp, ops[i], sess);
938 if (unlikely(retval < 0))
942 qp->stats.enqueued_count += i;
946 qp->stats.enqueue_err_count++;
952 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
955 struct openssl_qp *qp = queue_pair;
957 unsigned int nb_dequeued = 0;
959 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
960 (void **)ops, nb_ops);
961 qp->stats.dequeued_count += nb_dequeued;
966 /** Create OPENSSL crypto device */
968 cryptodev_openssl_create(const char *name,
969 struct rte_crypto_vdev_init_params *init_params)
971 struct rte_cryptodev *dev;
972 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
973 struct openssl_private *internals;
975 /* create a unique device name */
976 if (create_unique_device_name(crypto_dev_name,
977 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
978 OPENSSL_LOG_ERR("failed to create unique cryptodev name");
982 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
983 sizeof(struct openssl_private),
984 init_params->socket_id);
986 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
990 dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
991 dev->dev_ops = rte_openssl_pmd_ops;
993 /* register rx/tx burst functions for data path */
994 dev->dequeue_burst = openssl_pmd_dequeue_burst;
995 dev->enqueue_burst = openssl_pmd_enqueue_burst;
997 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
998 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
999 RTE_CRYPTODEV_FF_CPU_AESNI;
1001 /* Set vector instructions mode supported */
1002 internals = dev->data->dev_private;
1004 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1005 internals->max_nb_sessions = init_params->max_nb_sessions;
1010 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed", name);
1012 cryptodev_openssl_remove(crypto_dev_name);
1016 /** Initialise OPENSSL crypto device */
1018 cryptodev_openssl_probe(const char *name,
1019 const char *input_args)
1021 struct rte_crypto_vdev_init_params init_params = {
1022 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1023 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1027 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
1029 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1030 init_params.socket_id);
1031 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1032 init_params.max_nb_queue_pairs);
1033 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1034 init_params.max_nb_sessions);
1036 return cryptodev_openssl_create(name, &init_params);
1039 /** Uninitialise OPENSSL crypto device */
1041 cryptodev_openssl_remove(const char *name)
1047 "Closing OPENSSL crypto device %s on numa socket %u\n",
1048 name, rte_socket_id());
1053 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1054 .probe = cryptodev_openssl_probe,
1055 .remove = cryptodev_openssl_remove
1058 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1059 cryptodev_openssl_pmd_drv);
1060 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1061 "max_nb_queue_pairs=<int> "
1062 "max_nb_sessions=<int> "