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_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
489 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
496 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
498 offset -= rte_pktmbuf_data_len(m);
503 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
505 l = rte_pktmbuf_data_len(m) - offset;
507 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
513 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
519 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
520 src = rte_pktmbuf_mtod(m, uint8_t *);
521 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
522 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
532 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
533 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
540 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
542 offset -= rte_pktmbuf_data_len(m);
547 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
549 l = rte_pktmbuf_data_len(m) - offset;
551 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
557 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
563 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
564 src = rte_pktmbuf_mtod(m, uint8_t *);
565 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
566 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
575 /** Process standard openssl cipher encryption */
577 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
578 int offset, uint8_t *iv, uint8_t *key, int srclen,
579 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
583 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
584 goto process_cipher_encrypt_err;
586 EVP_CIPHER_CTX_set_padding(ctx, 0);
588 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
590 goto process_cipher_encrypt_err;
592 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
593 goto process_cipher_encrypt_err;
597 process_cipher_encrypt_err:
598 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
602 /** Process standard openssl cipher decryption */
604 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
605 int offset, uint8_t *iv, uint8_t *key, int srclen,
606 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
610 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
611 goto process_cipher_decrypt_err;
613 EVP_CIPHER_CTX_set_padding(ctx, 0);
615 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
617 goto process_cipher_decrypt_err;
619 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
620 goto process_cipher_decrypt_err;
623 process_cipher_decrypt_err:
624 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
628 /** Process cipher des 3 ctr encryption, decryption algorithm */
630 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
631 int offset, uint8_t *iv, uint8_t *key, int srclen,
634 uint8_t ebuf[8], ctr[8];
640 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
642 offset -= rte_pktmbuf_data_len(m);
645 goto process_cipher_des3ctr_err;
647 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
648 l = rte_pktmbuf_data_len(m) - offset;
650 /* We use 3DES encryption also for decryption.
651 * IV is not important for 3DES ecb
653 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
654 goto process_cipher_des3ctr_err;
658 for (n = 0; n < srclen; n++) {
660 if (EVP_EncryptUpdate(ctx,
661 (unsigned char *)&ebuf, &unused,
662 (const unsigned char *)&ctr, 8) <= 0)
663 goto process_cipher_des3ctr_err;
666 dst[n] = *(src++) ^ ebuf[n % 8];
672 src = rte_pktmbuf_mtod(m, uint8_t *);
673 l = rte_pktmbuf_data_len(m);
680 process_cipher_des3ctr_err:
681 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
685 /** Process auth/encription aes-gcm algorithm */
687 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
688 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
689 uint8_t *key, uint8_t *dst, uint8_t *tag,
690 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
692 int len = 0, unused = 0;
693 uint8_t empty[] = {};
695 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
696 goto process_auth_encryption_gcm_err;
698 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
699 goto process_auth_encryption_gcm_err;
701 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
702 goto process_auth_encryption_gcm_err;
705 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
706 goto process_auth_encryption_gcm_err;
709 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
711 goto process_auth_encryption_gcm_err;
713 /* Workaround open ssl bug in version less then 1.0.1f */
714 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
715 goto process_auth_encryption_gcm_err;
717 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
718 goto process_auth_encryption_gcm_err;
720 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
721 goto process_auth_encryption_gcm_err;
725 process_auth_encryption_gcm_err:
726 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
731 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
732 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
733 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
734 const EVP_CIPHER *algo)
736 int len = 0, unused = 0;
737 uint8_t empty[] = {};
739 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
740 goto process_auth_decryption_gcm_err;
742 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
743 goto process_auth_decryption_gcm_err;
745 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
746 goto process_auth_decryption_gcm_err;
748 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
749 goto process_auth_decryption_gcm_err;
752 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
753 goto process_auth_decryption_gcm_err;
756 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
758 goto process_auth_decryption_gcm_err;
760 /* Workaround open ssl bug in version less then 1.0.1f */
761 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
762 goto process_auth_decryption_gcm_err;
764 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
765 goto process_auth_decryption_gcm_final_err;
769 process_auth_decryption_gcm_err:
770 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
773 process_auth_decryption_gcm_final_err:
777 /** Process standard openssl auth algorithms */
779 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
780 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
781 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
788 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
790 offset -= rte_pktmbuf_data_len(m);
793 goto process_auth_err;
795 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
796 goto process_auth_err;
798 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
800 l = rte_pktmbuf_data_len(m) - offset;
802 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
803 goto process_auth_err;
804 goto process_auth_final;
807 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
808 goto process_auth_err;
812 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
813 src = rte_pktmbuf_mtod(m, uint8_t *);
814 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
815 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
816 goto process_auth_err;
821 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
822 goto process_auth_err;
826 OPENSSL_LOG_ERR("Process openssl auth failed");
830 /** Process standard openssl auth algorithms with hmac */
832 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
833 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
834 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
841 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
843 offset -= rte_pktmbuf_data_len(m);
846 goto process_auth_err;
848 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
849 goto process_auth_err;
851 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
853 l = rte_pktmbuf_data_len(m) - offset;
855 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
856 goto process_auth_err;
857 goto process_auth_final;
860 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
861 goto process_auth_err;
865 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
866 src = rte_pktmbuf_mtod(m, uint8_t *);
867 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
868 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
869 goto process_auth_err;
874 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
875 goto process_auth_err;
880 OPENSSL_LOG_ERR("Process openssl auth failed");
884 /*----------------------------------------------------------------------------*/
886 /** Process auth/cipher combined operation */
888 process_openssl_combined_op
889 (struct rte_crypto_op *op, struct openssl_session *sess,
890 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
893 uint8_t *dst = NULL, *iv, *tag, *aad;
894 int srclen, ivlen, aadlen, status = -1;
897 * Segmented destination buffer is not supported for
898 * encryption/decryption
900 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
901 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
905 iv = op->sym->cipher.iv.data;
906 ivlen = op->sym->cipher.iv.length;
907 aad = op->sym->auth.aad.data;
908 aadlen = op->sym->auth.aad.length;
910 tag = op->sym->auth.digest.data;
912 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
913 op->sym->cipher.data.offset +
914 op->sym->cipher.data.length);
916 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
919 srclen = op->sym->cipher.data.length;
920 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
921 op->sym->cipher.data.offset);
924 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
925 status = process_openssl_auth_encryption_gcm(
926 mbuf_src, op->sym->cipher.data.offset, srclen,
927 aad, aadlen, iv, ivlen, sess->cipher.key.data,
928 dst, tag, sess->cipher.ctx,
929 sess->cipher.evp_algo);
931 status = process_openssl_auth_decryption_gcm(
932 mbuf_src, op->sym->cipher.data.offset, srclen,
933 aad, aadlen, iv, ivlen, sess->cipher.key.data,
934 dst, tag, sess->cipher.ctx,
935 sess->cipher.evp_algo);
938 if (status == (-EFAULT) &&
939 sess->auth.operation ==
940 RTE_CRYPTO_AUTH_OP_VERIFY)
941 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
943 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
947 /** Process cipher operation */
949 process_openssl_cipher_op
950 (struct rte_crypto_op *op, struct openssl_session *sess,
951 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
957 * Segmented destination buffer is not supported for
958 * encryption/decryption
960 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
961 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
965 srclen = op->sym->cipher.data.length;
966 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
967 op->sym->cipher.data.offset);
969 iv = op->sym->cipher.iv.data;
971 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
972 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
973 status = process_openssl_cipher_encrypt(mbuf_src, dst,
974 op->sym->cipher.data.offset, iv,
975 sess->cipher.key.data, srclen,
977 sess->cipher.evp_algo);
979 status = process_openssl_cipher_decrypt(mbuf_src, dst,
980 op->sym->cipher.data.offset, iv,
981 sess->cipher.key.data, srclen,
983 sess->cipher.evp_algo);
985 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
986 op->sym->cipher.data.offset, iv,
987 sess->cipher.key.data, srclen,
991 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
994 /** Process auth operation */
996 process_openssl_auth_op
997 (struct rte_crypto_op *op, struct openssl_session *sess,
998 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1003 srclen = op->sym->auth.data.length;
1005 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1006 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1007 op->sym->auth.digest.length);
1009 dst = op->sym->auth.digest.data;
1011 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1012 op->sym->auth.data.offset +
1013 op->sym->auth.data.length);
1016 switch (sess->auth.mode) {
1017 case OPENSSL_AUTH_AS_AUTH:
1018 status = process_openssl_auth(mbuf_src, dst,
1019 op->sym->auth.data.offset, NULL, NULL, srclen,
1020 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1022 case OPENSSL_AUTH_AS_HMAC:
1023 status = process_openssl_auth_hmac(mbuf_src, dst,
1024 op->sym->auth.data.offset, NULL,
1025 sess->auth.hmac.pkey, srclen,
1026 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1033 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1034 if (memcmp(dst, op->sym->auth.digest.data,
1035 op->sym->auth.digest.length) != 0) {
1036 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1038 /* Trim area used for digest from mbuf. */
1039 rte_pktmbuf_trim(mbuf_src, op->sym->auth.digest.length);
1043 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1046 /** Process crypto operation for mbuf */
1048 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1049 struct openssl_session *sess)
1051 struct rte_mbuf *msrc, *mdst;
1054 msrc = op->sym->m_src;
1055 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1057 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1059 switch (sess->chain_order) {
1060 case OPENSSL_CHAIN_ONLY_CIPHER:
1061 process_openssl_cipher_op(op, sess, msrc, mdst);
1063 case OPENSSL_CHAIN_ONLY_AUTH:
1064 process_openssl_auth_op(op, sess, msrc, mdst);
1066 case OPENSSL_CHAIN_CIPHER_AUTH:
1067 process_openssl_cipher_op(op, sess, msrc, mdst);
1068 process_openssl_auth_op(op, sess, mdst, mdst);
1070 case OPENSSL_CHAIN_AUTH_CIPHER:
1071 process_openssl_auth_op(op, sess, msrc, mdst);
1072 process_openssl_cipher_op(op, sess, msrc, mdst);
1074 case OPENSSL_CHAIN_COMBINED:
1075 process_openssl_combined_op(op, sess, msrc, mdst);
1078 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1082 /* Free session if a session-less crypto op */
1083 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
1084 openssl_reset_session(sess);
1085 memset(sess, 0, sizeof(struct openssl_session));
1086 rte_mempool_put(qp->sess_mp, op->sym->session);
1087 op->sym->session = NULL;
1090 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1091 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1093 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1094 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1102 *------------------------------------------------------------------------------
1104 *------------------------------------------------------------------------------
1107 /** Enqueue burst */
1109 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1112 struct openssl_session *sess;
1113 struct openssl_qp *qp = queue_pair;
1116 for (i = 0; i < nb_ops; i++) {
1117 sess = get_session(qp, ops[i]);
1118 if (unlikely(sess == NULL))
1121 retval = process_op(qp, ops[i], sess);
1122 if (unlikely(retval < 0))
1126 qp->stats.enqueued_count += i;
1130 qp->stats.enqueue_err_count++;
1134 /** Dequeue burst */
1136 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1139 struct openssl_qp *qp = queue_pair;
1141 unsigned int nb_dequeued = 0;
1143 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1144 (void **)ops, nb_ops);
1145 qp->stats.dequeued_count += nb_dequeued;
1150 /** Create OPENSSL crypto device */
1152 cryptodev_openssl_create(const char *name,
1153 struct rte_crypto_vdev_init_params *init_params)
1155 struct rte_cryptodev *dev;
1156 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1157 struct openssl_private *internals;
1159 /* create a unique device name */
1160 if (create_unique_device_name(crypto_dev_name,
1161 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
1162 OPENSSL_LOG_ERR("failed to create unique cryptodev name");
1166 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
1167 sizeof(struct openssl_private),
1168 init_params->socket_id);
1170 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1174 dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
1175 dev->dev_ops = rte_openssl_pmd_ops;
1177 /* register rx/tx burst functions for data path */
1178 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1179 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1181 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1182 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1183 RTE_CRYPTODEV_FF_CPU_AESNI |
1184 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1186 /* Set vector instructions mode supported */
1187 internals = dev->data->dev_private;
1189 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1190 internals->max_nb_sessions = init_params->max_nb_sessions;
1195 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed", name);
1197 cryptodev_openssl_remove(crypto_dev_name);
1201 /** Initialise OPENSSL crypto device */
1203 cryptodev_openssl_probe(const char *name,
1204 const char *input_args)
1206 struct rte_crypto_vdev_init_params init_params = {
1207 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1208 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1212 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
1214 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1215 init_params.socket_id);
1216 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1217 init_params.max_nb_queue_pairs);
1218 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1219 init_params.max_nb_sessions);
1221 return cryptodev_openssl_create(name, &init_params);
1224 /** Uninitialise OPENSSL crypto device */
1226 cryptodev_openssl_remove(const char *name)
1232 "Closing OPENSSL crypto device %s on numa socket %u\n",
1233 name, rte_socket_id());
1238 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1239 .probe = cryptodev_openssl_probe,
1240 .remove = cryptodev_openssl_remove
1243 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1244 cryptodev_openssl_pmd_drv);
1245 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1246 "max_nb_queue_pairs=<int> "
1247 "max_nb_sessions=<int> "