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 * Increment counter by 1
51 * Counter is 64 bit array, big-endian
56 uint64_t *ctr64 = (uint64_t *)ctr;
58 *ctr64 = __builtin_bswap64(*ctr64);
60 *ctr64 = __builtin_bswap64(*ctr64);
64 *------------------------------------------------------------------------------
66 *------------------------------------------------------------------------------
69 /** Get xform chain order */
70 static enum openssl_chain_order
71 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
73 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
76 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
77 if (xform->next == NULL)
78 res = OPENSSL_CHAIN_ONLY_AUTH;
79 else if (xform->next->type ==
80 RTE_CRYPTO_SYM_XFORM_CIPHER)
81 res = OPENSSL_CHAIN_AUTH_CIPHER;
83 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
84 if (xform->next == NULL)
85 res = OPENSSL_CHAIN_ONLY_CIPHER;
86 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
87 res = OPENSSL_CHAIN_CIPHER_AUTH;
94 /** Get session cipher key from input cipher key */
96 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
98 memcpy(session_key, input_key, keylen);
101 /** Get key ede 24 bytes standard from input key */
103 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
107 /* Initialize keys - 24 bytes: [key1-key2-key3] */
110 memcpy(key_ede, key, 24);
114 memcpy(key_ede, key, 16);
115 memcpy(key_ede + 16, key, 8);
118 /* K1 = K2 = K3 (DES compatibility) */
119 memcpy(key_ede, key, 8);
120 memcpy(key_ede + 8, key, 8);
121 memcpy(key_ede + 16, key, 8);
124 OPENSSL_LOG_ERR("Unsupported key size");
131 /** Get adequate openssl function for input cipher algorithm */
133 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
134 const EVP_CIPHER **algo)
140 case RTE_CRYPTO_CIPHER_3DES_CBC:
143 *algo = EVP_des_ede_cbc();
146 *algo = EVP_des_ede3_cbc();
152 case RTE_CRYPTO_CIPHER_3DES_CTR:
154 case RTE_CRYPTO_CIPHER_AES_CBC:
157 *algo = EVP_aes_128_cbc();
160 *algo = EVP_aes_192_cbc();
163 *algo = EVP_aes_256_cbc();
169 case RTE_CRYPTO_CIPHER_AES_CTR:
172 *algo = EVP_aes_128_ctr();
175 *algo = EVP_aes_192_ctr();
178 *algo = EVP_aes_256_ctr();
184 case RTE_CRYPTO_CIPHER_AES_GCM:
187 *algo = EVP_aes_128_gcm();
190 *algo = EVP_aes_192_gcm();
193 *algo = EVP_aes_256_gcm();
210 /** Get adequate openssl function for input auth algorithm */
212 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
219 case RTE_CRYPTO_AUTH_MD5:
220 case RTE_CRYPTO_AUTH_MD5_HMAC:
223 case RTE_CRYPTO_AUTH_SHA1:
224 case RTE_CRYPTO_AUTH_SHA1_HMAC:
227 case RTE_CRYPTO_AUTH_SHA224:
228 case RTE_CRYPTO_AUTH_SHA224_HMAC:
229 *algo = EVP_sha224();
231 case RTE_CRYPTO_AUTH_SHA256:
232 case RTE_CRYPTO_AUTH_SHA256_HMAC:
233 *algo = EVP_sha256();
235 case RTE_CRYPTO_AUTH_SHA384:
236 case RTE_CRYPTO_AUTH_SHA384_HMAC:
237 *algo = EVP_sha384();
239 case RTE_CRYPTO_AUTH_SHA512:
240 case RTE_CRYPTO_AUTH_SHA512_HMAC:
241 *algo = EVP_sha512();
254 /** Set session cipher parameters */
256 openssl_set_session_cipher_parameters(struct openssl_session *sess,
257 const struct rte_crypto_sym_xform *xform)
259 /* Select cipher direction */
260 sess->cipher.direction = xform->cipher.op;
261 /* Select cipher key */
262 sess->cipher.key.length = xform->cipher.key.length;
264 /* Select cipher algo */
265 switch (xform->cipher.algo) {
266 case RTE_CRYPTO_CIPHER_3DES_CBC:
267 case RTE_CRYPTO_CIPHER_AES_CBC:
268 case RTE_CRYPTO_CIPHER_AES_CTR:
269 case RTE_CRYPTO_CIPHER_AES_GCM:
270 sess->cipher.mode = OPENSSL_CIPHER_LIB;
271 sess->cipher.algo = xform->cipher.algo;
272 sess->cipher.ctx = EVP_CIPHER_CTX_new();
274 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
275 &sess->cipher.evp_algo) != 0)
278 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
279 sess->cipher.key.data);
283 case RTE_CRYPTO_CIPHER_3DES_CTR:
284 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
285 sess->cipher.ctx = EVP_CIPHER_CTX_new();
287 if (get_cipher_key_ede(xform->cipher.key.data,
288 sess->cipher.key.length,
289 sess->cipher.key.data) != 0)
294 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
301 /* Set session auth parameters */
303 openssl_set_session_auth_parameters(struct openssl_session *sess,
304 const struct rte_crypto_sym_xform *xform)
306 /* Select auth generate/verify */
307 sess->auth.operation = xform->auth.op;
308 sess->auth.algo = xform->auth.algo;
310 /* Select auth algo */
311 switch (xform->auth.algo) {
312 case RTE_CRYPTO_AUTH_AES_GMAC:
313 case RTE_CRYPTO_AUTH_AES_GCM:
314 /* Check additional condition for AES_GMAC/GCM */
315 if (sess->cipher.algo != RTE_CRYPTO_CIPHER_AES_GCM)
317 sess->chain_order = OPENSSL_CHAIN_COMBINED;
320 case RTE_CRYPTO_AUTH_MD5:
321 case RTE_CRYPTO_AUTH_SHA1:
322 case RTE_CRYPTO_AUTH_SHA224:
323 case RTE_CRYPTO_AUTH_SHA256:
324 case RTE_CRYPTO_AUTH_SHA384:
325 case RTE_CRYPTO_AUTH_SHA512:
326 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
327 if (get_auth_algo(xform->auth.algo,
328 &sess->auth.auth.evp_algo) != 0)
330 sess->auth.auth.ctx = EVP_MD_CTX_create();
333 case RTE_CRYPTO_AUTH_MD5_HMAC:
334 case RTE_CRYPTO_AUTH_SHA1_HMAC:
335 case RTE_CRYPTO_AUTH_SHA224_HMAC:
336 case RTE_CRYPTO_AUTH_SHA256_HMAC:
337 case RTE_CRYPTO_AUTH_SHA384_HMAC:
338 case RTE_CRYPTO_AUTH_SHA512_HMAC:
339 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
340 sess->auth.hmac.ctx = EVP_MD_CTX_create();
341 if (get_auth_algo(xform->auth.algo,
342 &sess->auth.hmac.evp_algo) != 0)
344 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
345 xform->auth.key.data, xform->auth.key.length);
355 /** Parse crypto xform chain and set private session parameters */
357 openssl_set_session_parameters(struct openssl_session *sess,
358 const struct rte_crypto_sym_xform *xform)
360 const struct rte_crypto_sym_xform *cipher_xform = NULL;
361 const struct rte_crypto_sym_xform *auth_xform = NULL;
363 sess->chain_order = openssl_get_chain_order(xform);
364 switch (sess->chain_order) {
365 case OPENSSL_CHAIN_ONLY_CIPHER:
366 cipher_xform = xform;
368 case OPENSSL_CHAIN_ONLY_AUTH:
371 case OPENSSL_CHAIN_CIPHER_AUTH:
372 cipher_xform = xform;
373 auth_xform = xform->next;
375 case OPENSSL_CHAIN_AUTH_CIPHER:
377 cipher_xform = xform->next;
383 /* cipher_xform must be check before auth_xform */
385 if (openssl_set_session_cipher_parameters(
386 sess, cipher_xform)) {
388 "Invalid/unsupported cipher parameters");
394 if (openssl_set_session_auth_parameters(sess, auth_xform)) {
396 "Invalid/unsupported auth parameters");
404 /** Reset private session parameters */
406 openssl_reset_session(struct openssl_session *sess)
408 EVP_CIPHER_CTX_free(sess->cipher.ctx);
410 switch (sess->auth.mode) {
411 case OPENSSL_AUTH_AS_AUTH:
412 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
414 case OPENSSL_AUTH_AS_HMAC:
415 EVP_PKEY_free(sess->auth.hmac.pkey);
416 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
423 /** Provide session for operation */
424 static struct openssl_session *
425 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
427 struct openssl_session *sess = NULL;
429 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
430 /* get existing session */
431 if (likely(op->sym->session != NULL &&
432 op->sym->session->dev_type ==
433 RTE_CRYPTODEV_OPENSSL_PMD))
434 sess = (struct openssl_session *)
435 op->sym->session->_private;
437 /* provide internal session */
440 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
441 sess = (struct openssl_session *)
442 ((struct rte_cryptodev_sym_session *)_sess)
445 if (unlikely(openssl_set_session_parameters(
446 sess, op->sym->xform) != 0)) {
447 rte_mempool_put(qp->sess_mp, _sess);
450 op->sym->session = _sess;
455 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
461 *------------------------------------------------------------------------------
463 *------------------------------------------------------------------------------
466 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
467 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
474 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
476 offset -= rte_pktmbuf_data_len(m);
481 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
483 l = rte_pktmbuf_data_len(m) - offset;
485 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
491 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
497 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
498 src = rte_pktmbuf_mtod(m, uint8_t *);
499 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
500 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
510 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
511 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
518 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
520 offset -= rte_pktmbuf_data_len(m);
525 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
527 l = rte_pktmbuf_data_len(m) - offset;
529 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
535 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
541 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
542 src = rte_pktmbuf_mtod(m, uint8_t *);
543 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
544 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
553 /** Process standard openssl cipher encryption */
555 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
556 int offset, uint8_t *iv, uint8_t *key, int srclen,
557 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
561 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
562 goto process_cipher_encrypt_err;
564 EVP_CIPHER_CTX_set_padding(ctx, 0);
566 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
568 goto process_cipher_encrypt_err;
570 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
571 goto process_cipher_encrypt_err;
575 process_cipher_encrypt_err:
576 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
580 /** Process standard openssl cipher decryption */
582 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
583 int offset, uint8_t *iv, uint8_t *key, int srclen,
584 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
588 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
589 goto process_cipher_decrypt_err;
591 EVP_CIPHER_CTX_set_padding(ctx, 0);
593 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
595 goto process_cipher_decrypt_err;
597 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
598 goto process_cipher_decrypt_err;
601 process_cipher_decrypt_err:
602 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
606 /** Process cipher des 3 ctr encryption, decryption algorithm */
608 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
609 int offset, uint8_t *iv, uint8_t *key, int srclen,
612 uint8_t ebuf[8], ctr[8];
618 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
620 offset -= rte_pktmbuf_data_len(m);
623 goto process_cipher_des3ctr_err;
625 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
626 l = rte_pktmbuf_data_len(m) - offset;
628 /* We use 3DES encryption also for decryption.
629 * IV is not important for 3DES ecb
631 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
632 goto process_cipher_des3ctr_err;
636 for (n = 0; n < srclen; n++) {
638 if (EVP_EncryptUpdate(ctx,
639 (unsigned char *)&ebuf, &unused,
640 (const unsigned char *)&ctr, 8) <= 0)
641 goto process_cipher_des3ctr_err;
644 dst[n] = *(src++) ^ ebuf[n % 8];
650 src = rte_pktmbuf_mtod(m, uint8_t *);
651 l = rte_pktmbuf_data_len(m);
658 process_cipher_des3ctr_err:
659 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
663 /** Process auth/encription aes-gcm algorithm */
665 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
666 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
667 uint8_t *key, uint8_t *dst, uint8_t *tag,
668 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
670 int len = 0, unused = 0;
671 uint8_t empty[] = {};
673 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
674 goto process_auth_encryption_gcm_err;
676 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
677 goto process_auth_encryption_gcm_err;
679 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
680 goto process_auth_encryption_gcm_err;
683 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
684 goto process_auth_encryption_gcm_err;
687 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
689 goto process_auth_encryption_gcm_err;
691 /* Workaround open ssl bug in version less then 1.0.1f */
692 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
693 goto process_auth_encryption_gcm_err;
695 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
696 goto process_auth_encryption_gcm_err;
698 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
699 goto process_auth_encryption_gcm_err;
703 process_auth_encryption_gcm_err:
704 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
709 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
710 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
711 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
712 const EVP_CIPHER *algo)
714 int len = 0, unused = 0;
715 uint8_t empty[] = {};
717 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
718 goto process_auth_decryption_gcm_err;
720 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
721 goto process_auth_decryption_gcm_err;
723 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
724 goto process_auth_decryption_gcm_err;
726 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
727 goto process_auth_decryption_gcm_err;
730 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
731 goto process_auth_decryption_gcm_err;
734 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
736 goto process_auth_decryption_gcm_err;
738 /* Workaround open ssl bug in version less then 1.0.1f */
739 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
740 goto process_auth_decryption_gcm_err;
742 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
743 goto process_auth_decryption_gcm_final_err;
747 process_auth_decryption_gcm_err:
748 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
751 process_auth_decryption_gcm_final_err:
755 /** Process standard openssl auth algorithms */
757 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
758 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
759 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
766 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
768 offset -= rte_pktmbuf_data_len(m);
771 goto process_auth_err;
773 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
774 goto process_auth_err;
776 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
778 l = rte_pktmbuf_data_len(m) - offset;
780 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
781 goto process_auth_err;
782 goto process_auth_final;
785 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
786 goto process_auth_err;
790 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
791 src = rte_pktmbuf_mtod(m, uint8_t *);
792 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
793 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
794 goto process_auth_err;
799 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
800 goto process_auth_err;
804 OPENSSL_LOG_ERR("Process openssl auth failed");
808 /** Process standard openssl auth algorithms with hmac */
810 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
811 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
812 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
819 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
821 offset -= rte_pktmbuf_data_len(m);
824 goto process_auth_err;
826 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
827 goto process_auth_err;
829 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
831 l = rte_pktmbuf_data_len(m) - offset;
833 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
834 goto process_auth_err;
835 goto process_auth_final;
838 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
839 goto process_auth_err;
843 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
844 src = rte_pktmbuf_mtod(m, uint8_t *);
845 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
846 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
847 goto process_auth_err;
852 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
853 goto process_auth_err;
858 OPENSSL_LOG_ERR("Process openssl auth failed");
862 /*----------------------------------------------------------------------------*/
864 /** Process auth/cipher combined operation */
866 process_openssl_combined_op
867 (struct rte_crypto_op *op, struct openssl_session *sess,
868 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
871 uint8_t *dst = NULL, *iv, *tag, *aad;
872 int srclen, ivlen, aadlen, status = -1;
875 * Segmented destination buffer is not supported for
876 * encryption/decryption
878 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
879 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
883 iv = op->sym->cipher.iv.data;
884 ivlen = op->sym->cipher.iv.length;
885 aad = op->sym->auth.aad.data;
886 aadlen = op->sym->auth.aad.length;
888 tag = op->sym->auth.digest.data;
890 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
891 op->sym->cipher.data.offset +
892 op->sym->cipher.data.length);
894 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
897 srclen = op->sym->cipher.data.length;
898 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
899 op->sym->cipher.data.offset);
902 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
903 status = process_openssl_auth_encryption_gcm(
904 mbuf_src, op->sym->cipher.data.offset, srclen,
905 aad, aadlen, iv, ivlen, sess->cipher.key.data,
906 dst, tag, sess->cipher.ctx,
907 sess->cipher.evp_algo);
909 status = process_openssl_auth_decryption_gcm(
910 mbuf_src, op->sym->cipher.data.offset, srclen,
911 aad, aadlen, iv, ivlen, sess->cipher.key.data,
912 dst, tag, sess->cipher.ctx,
913 sess->cipher.evp_algo);
916 if (status == (-EFAULT) &&
917 sess->auth.operation ==
918 RTE_CRYPTO_AUTH_OP_VERIFY)
919 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
921 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
925 /** Process cipher operation */
927 process_openssl_cipher_op
928 (struct rte_crypto_op *op, struct openssl_session *sess,
929 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
935 * Segmented destination buffer is not supported for
936 * encryption/decryption
938 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
939 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
943 srclen = op->sym->cipher.data.length;
944 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
945 op->sym->cipher.data.offset);
947 iv = op->sym->cipher.iv.data;
949 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
950 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
951 status = process_openssl_cipher_encrypt(mbuf_src, dst,
952 op->sym->cipher.data.offset, iv,
953 sess->cipher.key.data, srclen,
955 sess->cipher.evp_algo);
957 status = process_openssl_cipher_decrypt(mbuf_src, dst,
958 op->sym->cipher.data.offset, iv,
959 sess->cipher.key.data, srclen,
961 sess->cipher.evp_algo);
963 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
964 op->sym->cipher.data.offset, iv,
965 sess->cipher.key.data, srclen,
969 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
972 /** Process auth operation */
974 process_openssl_auth_op
975 (struct rte_crypto_op *op, struct openssl_session *sess,
976 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
981 srclen = op->sym->auth.data.length;
983 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
984 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
985 op->sym->auth.digest.length);
987 dst = op->sym->auth.digest.data;
989 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
990 op->sym->auth.data.offset +
991 op->sym->auth.data.length);
994 switch (sess->auth.mode) {
995 case OPENSSL_AUTH_AS_AUTH:
996 status = process_openssl_auth(mbuf_src, dst,
997 op->sym->auth.data.offset, NULL, NULL, srclen,
998 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1000 case OPENSSL_AUTH_AS_HMAC:
1001 status = process_openssl_auth_hmac(mbuf_src, dst,
1002 op->sym->auth.data.offset, NULL,
1003 sess->auth.hmac.pkey, srclen,
1004 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1011 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1012 if (memcmp(dst, op->sym->auth.digest.data,
1013 op->sym->auth.digest.length) != 0) {
1014 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1016 /* Trim area used for digest from mbuf. */
1017 rte_pktmbuf_trim(mbuf_src, op->sym->auth.digest.length);
1021 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1024 /** Process crypto operation for mbuf */
1026 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1027 struct openssl_session *sess)
1029 struct rte_mbuf *msrc, *mdst;
1032 msrc = op->sym->m_src;
1033 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1035 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1037 switch (sess->chain_order) {
1038 case OPENSSL_CHAIN_ONLY_CIPHER:
1039 process_openssl_cipher_op(op, sess, msrc, mdst);
1041 case OPENSSL_CHAIN_ONLY_AUTH:
1042 process_openssl_auth_op(op, sess, msrc, mdst);
1044 case OPENSSL_CHAIN_CIPHER_AUTH:
1045 process_openssl_cipher_op(op, sess, msrc, mdst);
1046 process_openssl_auth_op(op, sess, mdst, mdst);
1048 case OPENSSL_CHAIN_AUTH_CIPHER:
1049 process_openssl_auth_op(op, sess, msrc, mdst);
1050 process_openssl_cipher_op(op, sess, msrc, mdst);
1052 case OPENSSL_CHAIN_COMBINED:
1053 process_openssl_combined_op(op, sess, msrc, mdst);
1056 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1060 /* Free session if a session-less crypto op */
1061 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
1062 openssl_reset_session(sess);
1063 memset(sess, 0, sizeof(struct openssl_session));
1064 rte_mempool_put(qp->sess_mp, op->sym->session);
1065 op->sym->session = NULL;
1068 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1069 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1071 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1072 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1080 *------------------------------------------------------------------------------
1082 *------------------------------------------------------------------------------
1085 /** Enqueue burst */
1087 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1090 struct openssl_session *sess;
1091 struct openssl_qp *qp = queue_pair;
1094 for (i = 0; i < nb_ops; i++) {
1095 sess = get_session(qp, ops[i]);
1096 if (unlikely(sess == NULL))
1099 retval = process_op(qp, ops[i], sess);
1100 if (unlikely(retval < 0))
1104 qp->stats.enqueued_count += i;
1108 qp->stats.enqueue_err_count++;
1112 /** Dequeue burst */
1114 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1117 struct openssl_qp *qp = queue_pair;
1119 unsigned int nb_dequeued = 0;
1121 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1122 (void **)ops, nb_ops);
1123 qp->stats.dequeued_count += nb_dequeued;
1128 /** Create OPENSSL crypto device */
1130 cryptodev_openssl_create(struct rte_crypto_vdev_init_params *init_params)
1132 struct rte_cryptodev *dev;
1133 struct openssl_private *internals;
1135 if (init_params->name[0] == '\0') {
1136 int ret = rte_cryptodev_pmd_create_dev_name(
1138 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD));
1141 OPENSSL_LOG_ERR("failed to create unique name");
1146 dev = rte_cryptodev_pmd_virtual_dev_init(init_params->name,
1147 sizeof(struct openssl_private),
1148 init_params->socket_id);
1150 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1154 dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
1155 dev->dev_ops = rte_openssl_pmd_ops;
1157 /* register rx/tx burst functions for data path */
1158 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1159 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1161 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1162 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1163 RTE_CRYPTODEV_FF_CPU_AESNI |
1164 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1166 /* Set vector instructions mode supported */
1167 internals = dev->data->dev_private;
1169 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1170 internals->max_nb_sessions = init_params->max_nb_sessions;
1175 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1178 cryptodev_openssl_remove(init_params->name);
1182 /** Initialise OPENSSL crypto device */
1184 cryptodev_openssl_probe(const char *name,
1185 const char *input_args)
1187 struct rte_crypto_vdev_init_params init_params = {
1188 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1189 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1194 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
1196 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1197 init_params.socket_id);
1198 if (init_params.name[0] != '\0')
1199 RTE_LOG(INFO, PMD, " User defined name = %s\n",
1201 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1202 init_params.max_nb_queue_pairs);
1203 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1204 init_params.max_nb_sessions);
1206 return cryptodev_openssl_create(&init_params);
1209 /** Uninitialise OPENSSL crypto device */
1211 cryptodev_openssl_remove(const char *name)
1217 "Closing OPENSSL crypto device %s on numa socket %u\n",
1218 name, rte_socket_id());
1223 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1224 .probe = cryptodev_openssl_probe,
1225 .remove = cryptodev_openssl_remove
1228 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1229 cryptodev_openssl_pmd_drv);
1230 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1231 "max_nb_queue_pairs=<int> "
1232 "max_nb_sessions=<int> "