4 * Copyright(c) 2016-2017 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>
37 #include <rte_cryptodev_vdev.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include <openssl/evp.h>
44 #include "rte_openssl_pmd_private.h"
46 #define DES_BLOCK_SIZE 8
48 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
50 /*----------------------------------------------------------------------------*/
53 * Increment counter by 1
54 * Counter is 64 bit array, big-endian
59 uint64_t *ctr64 = (uint64_t *)ctr;
61 *ctr64 = __builtin_bswap64(*ctr64);
63 *ctr64 = __builtin_bswap64(*ctr64);
67 *------------------------------------------------------------------------------
69 *------------------------------------------------------------------------------
72 /** Get xform chain order */
73 static enum openssl_chain_order
74 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
76 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
79 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
80 if (xform->next == NULL)
81 res = OPENSSL_CHAIN_ONLY_AUTH;
82 else if (xform->next->type ==
83 RTE_CRYPTO_SYM_XFORM_CIPHER)
84 res = OPENSSL_CHAIN_AUTH_CIPHER;
86 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
87 if (xform->next == NULL)
88 res = OPENSSL_CHAIN_ONLY_CIPHER;
89 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
90 res = OPENSSL_CHAIN_CIPHER_AUTH;
97 /** Get session cipher key from input cipher key */
99 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
101 memcpy(session_key, input_key, keylen);
104 /** Get key ede 24 bytes standard from input key */
106 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
110 /* Initialize keys - 24 bytes: [key1-key2-key3] */
113 memcpy(key_ede, key, 24);
117 memcpy(key_ede, key, 16);
118 memcpy(key_ede + 16, key, 8);
121 /* K1 = K2 = K3 (DES compatibility) */
122 memcpy(key_ede, key, 8);
123 memcpy(key_ede + 8, key, 8);
124 memcpy(key_ede + 16, key, 8);
127 OPENSSL_LOG_ERR("Unsupported key size");
134 /** Get adequate openssl function for input cipher algorithm */
136 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
137 const EVP_CIPHER **algo)
143 case RTE_CRYPTO_CIPHER_3DES_CBC:
146 *algo = EVP_des_ede_cbc();
149 *algo = EVP_des_ede3_cbc();
155 case RTE_CRYPTO_CIPHER_3DES_CTR:
157 case RTE_CRYPTO_CIPHER_AES_CBC:
160 *algo = EVP_aes_128_cbc();
163 *algo = EVP_aes_192_cbc();
166 *algo = EVP_aes_256_cbc();
172 case RTE_CRYPTO_CIPHER_AES_CTR:
175 *algo = EVP_aes_128_ctr();
178 *algo = EVP_aes_192_ctr();
181 *algo = EVP_aes_256_ctr();
187 case RTE_CRYPTO_CIPHER_AES_GCM:
190 *algo = EVP_aes_128_gcm();
193 *algo = EVP_aes_192_gcm();
196 *algo = EVP_aes_256_gcm();
213 /** Get adequate openssl function for input auth algorithm */
215 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
222 case RTE_CRYPTO_AUTH_MD5:
223 case RTE_CRYPTO_AUTH_MD5_HMAC:
226 case RTE_CRYPTO_AUTH_SHA1:
227 case RTE_CRYPTO_AUTH_SHA1_HMAC:
230 case RTE_CRYPTO_AUTH_SHA224:
231 case RTE_CRYPTO_AUTH_SHA224_HMAC:
232 *algo = EVP_sha224();
234 case RTE_CRYPTO_AUTH_SHA256:
235 case RTE_CRYPTO_AUTH_SHA256_HMAC:
236 *algo = EVP_sha256();
238 case RTE_CRYPTO_AUTH_SHA384:
239 case RTE_CRYPTO_AUTH_SHA384_HMAC:
240 *algo = EVP_sha384();
242 case RTE_CRYPTO_AUTH_SHA512:
243 case RTE_CRYPTO_AUTH_SHA512_HMAC:
244 *algo = EVP_sha512();
257 /** Set session cipher parameters */
259 openssl_set_session_cipher_parameters(struct openssl_session *sess,
260 const struct rte_crypto_sym_xform *xform)
262 /* Select cipher direction */
263 sess->cipher.direction = xform->cipher.op;
264 /* Select cipher key */
265 sess->cipher.key.length = xform->cipher.key.length;
267 /* Select cipher algo */
268 switch (xform->cipher.algo) {
269 case RTE_CRYPTO_CIPHER_3DES_CBC:
270 case RTE_CRYPTO_CIPHER_AES_CBC:
271 case RTE_CRYPTO_CIPHER_AES_CTR:
272 case RTE_CRYPTO_CIPHER_AES_GCM:
273 sess->cipher.mode = OPENSSL_CIPHER_LIB;
274 sess->cipher.algo = xform->cipher.algo;
275 sess->cipher.ctx = EVP_CIPHER_CTX_new();
277 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
278 &sess->cipher.evp_algo) != 0)
281 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
282 sess->cipher.key.data);
286 case RTE_CRYPTO_CIPHER_3DES_CTR:
287 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
288 sess->cipher.ctx = EVP_CIPHER_CTX_new();
290 if (get_cipher_key_ede(xform->cipher.key.data,
291 sess->cipher.key.length,
292 sess->cipher.key.data) != 0)
295 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
296 sess->cipher.algo = xform->cipher.algo;
297 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
298 sess->cipher.ctx = EVP_CIPHER_CTX_new();
299 sess->cipher.evp_algo = EVP_des_cbc();
301 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
302 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
303 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
304 NULL, xform->cipher.key.data, 0) != 1)
307 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
308 sess->cipher.key.data);
311 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
318 /* Set session auth parameters */
320 openssl_set_session_auth_parameters(struct openssl_session *sess,
321 const struct rte_crypto_sym_xform *xform)
323 /* Select auth generate/verify */
324 sess->auth.operation = xform->auth.op;
325 sess->auth.algo = xform->auth.algo;
327 /* Select auth algo */
328 switch (xform->auth.algo) {
329 case RTE_CRYPTO_AUTH_AES_GMAC:
330 case RTE_CRYPTO_AUTH_AES_GCM:
331 /* Check additional condition for AES_GMAC/GCM */
332 if (sess->cipher.algo != RTE_CRYPTO_CIPHER_AES_GCM)
334 sess->chain_order = OPENSSL_CHAIN_COMBINED;
337 case RTE_CRYPTO_AUTH_MD5:
338 case RTE_CRYPTO_AUTH_SHA1:
339 case RTE_CRYPTO_AUTH_SHA224:
340 case RTE_CRYPTO_AUTH_SHA256:
341 case RTE_CRYPTO_AUTH_SHA384:
342 case RTE_CRYPTO_AUTH_SHA512:
343 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
344 if (get_auth_algo(xform->auth.algo,
345 &sess->auth.auth.evp_algo) != 0)
347 sess->auth.auth.ctx = EVP_MD_CTX_create();
350 case RTE_CRYPTO_AUTH_MD5_HMAC:
351 case RTE_CRYPTO_AUTH_SHA1_HMAC:
352 case RTE_CRYPTO_AUTH_SHA224_HMAC:
353 case RTE_CRYPTO_AUTH_SHA256_HMAC:
354 case RTE_CRYPTO_AUTH_SHA384_HMAC:
355 case RTE_CRYPTO_AUTH_SHA512_HMAC:
356 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
357 sess->auth.hmac.ctx = EVP_MD_CTX_create();
358 if (get_auth_algo(xform->auth.algo,
359 &sess->auth.hmac.evp_algo) != 0)
361 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
362 xform->auth.key.data, xform->auth.key.length);
372 /** Parse crypto xform chain and set private session parameters */
374 openssl_set_session_parameters(struct openssl_session *sess,
375 const struct rte_crypto_sym_xform *xform)
377 const struct rte_crypto_sym_xform *cipher_xform = NULL;
378 const struct rte_crypto_sym_xform *auth_xform = NULL;
380 sess->chain_order = openssl_get_chain_order(xform);
381 switch (sess->chain_order) {
382 case OPENSSL_CHAIN_ONLY_CIPHER:
383 cipher_xform = xform;
385 case OPENSSL_CHAIN_ONLY_AUTH:
388 case OPENSSL_CHAIN_CIPHER_AUTH:
389 cipher_xform = xform;
390 auth_xform = xform->next;
392 case OPENSSL_CHAIN_AUTH_CIPHER:
394 cipher_xform = xform->next;
400 /* cipher_xform must be check before auth_xform */
402 if (openssl_set_session_cipher_parameters(
403 sess, cipher_xform)) {
405 "Invalid/unsupported cipher parameters");
411 if (openssl_set_session_auth_parameters(sess, auth_xform)) {
413 "Invalid/unsupported auth parameters");
421 /** Reset private session parameters */
423 openssl_reset_session(struct openssl_session *sess)
425 EVP_CIPHER_CTX_free(sess->cipher.ctx);
427 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
428 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
430 switch (sess->auth.mode) {
431 case OPENSSL_AUTH_AS_AUTH:
432 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
434 case OPENSSL_AUTH_AS_HMAC:
435 EVP_PKEY_free(sess->auth.hmac.pkey);
436 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
443 /** Provide session for operation */
444 static struct openssl_session *
445 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
447 struct openssl_session *sess = NULL;
449 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
450 /* get existing session */
451 if (likely(op->sym->session != NULL &&
452 op->sym->session->dev_type ==
453 RTE_CRYPTODEV_OPENSSL_PMD))
454 sess = (struct openssl_session *)
455 op->sym->session->_private;
457 /* provide internal session */
460 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
461 sess = (struct openssl_session *)
462 ((struct rte_cryptodev_sym_session *)_sess)
465 if (unlikely(openssl_set_session_parameters(
466 sess, op->sym->xform) != 0)) {
467 rte_mempool_put(qp->sess_mp, _sess);
470 op->sym->session = _sess;
475 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
481 *------------------------------------------------------------------------------
483 *------------------------------------------------------------------------------
486 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
487 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
494 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
496 offset -= rte_pktmbuf_data_len(m);
501 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
503 l = rte_pktmbuf_data_len(m) - offset;
505 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
511 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
517 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
518 src = rte_pktmbuf_mtod(m, uint8_t *);
519 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
520 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
530 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
531 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
538 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
540 offset -= rte_pktmbuf_data_len(m);
545 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
547 l = rte_pktmbuf_data_len(m) - offset;
549 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
555 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
561 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
562 src = rte_pktmbuf_mtod(m, uint8_t *);
563 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
564 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
573 /** Process standard openssl cipher encryption */
575 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
576 int offset, uint8_t *iv, uint8_t *key, int srclen,
577 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
581 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
582 goto process_cipher_encrypt_err;
584 EVP_CIPHER_CTX_set_padding(ctx, 0);
586 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
588 goto process_cipher_encrypt_err;
590 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
591 goto process_cipher_encrypt_err;
595 process_cipher_encrypt_err:
596 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
600 /** Process standard openssl cipher encryption */
602 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
603 uint8_t *iv, int srclen,
607 uint8_t encrypted_iv[DES_BLOCK_SIZE];
610 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
611 iv, DES_BLOCK_SIZE) <= 0)
612 goto process_cipher_encrypt_err;
614 for (i = 0; i < srclen; i++)
615 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
619 process_cipher_encrypt_err:
620 OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
623 /** Process standard openssl cipher decryption */
625 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
626 int offset, uint8_t *iv, uint8_t *key, int srclen,
627 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
631 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
632 goto process_cipher_decrypt_err;
634 EVP_CIPHER_CTX_set_padding(ctx, 0);
636 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
638 goto process_cipher_decrypt_err;
640 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
641 goto process_cipher_decrypt_err;
644 process_cipher_decrypt_err:
645 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
649 /** Process cipher des 3 ctr encryption, decryption algorithm */
651 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
652 int offset, uint8_t *iv, uint8_t *key, int srclen,
655 uint8_t ebuf[8], ctr[8];
661 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
663 offset -= rte_pktmbuf_data_len(m);
666 goto process_cipher_des3ctr_err;
668 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
669 l = rte_pktmbuf_data_len(m) - offset;
671 /* We use 3DES encryption also for decryption.
672 * IV is not important for 3DES ecb
674 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
675 goto process_cipher_des3ctr_err;
679 for (n = 0; n < srclen; n++) {
681 if (EVP_EncryptUpdate(ctx,
682 (unsigned char *)&ebuf, &unused,
683 (const unsigned char *)&ctr, 8) <= 0)
684 goto process_cipher_des3ctr_err;
687 dst[n] = *(src++) ^ ebuf[n % 8];
693 src = rte_pktmbuf_mtod(m, uint8_t *);
694 l = rte_pktmbuf_data_len(m);
701 process_cipher_des3ctr_err:
702 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
706 /** Process auth/encription aes-gcm algorithm */
708 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
709 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
710 uint8_t *key, uint8_t *dst, uint8_t *tag,
711 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
713 int len = 0, unused = 0;
714 uint8_t empty[] = {};
716 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
717 goto process_auth_encryption_gcm_err;
719 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
720 goto process_auth_encryption_gcm_err;
722 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
723 goto process_auth_encryption_gcm_err;
726 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
727 goto process_auth_encryption_gcm_err;
730 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
732 goto process_auth_encryption_gcm_err;
734 /* Workaround open ssl bug in version less then 1.0.1f */
735 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
736 goto process_auth_encryption_gcm_err;
738 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
739 goto process_auth_encryption_gcm_err;
741 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
742 goto process_auth_encryption_gcm_err;
746 process_auth_encryption_gcm_err:
747 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
752 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
753 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
754 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
755 const EVP_CIPHER *algo)
757 int len = 0, unused = 0;
758 uint8_t empty[] = {};
760 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
761 goto process_auth_decryption_gcm_err;
763 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
764 goto process_auth_decryption_gcm_err;
766 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
767 goto process_auth_decryption_gcm_err;
769 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
770 goto process_auth_decryption_gcm_err;
773 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
774 goto process_auth_decryption_gcm_err;
777 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
779 goto process_auth_decryption_gcm_err;
781 /* Workaround open ssl bug in version less then 1.0.1f */
782 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
783 goto process_auth_decryption_gcm_err;
785 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
786 goto process_auth_decryption_gcm_final_err;
790 process_auth_decryption_gcm_err:
791 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
794 process_auth_decryption_gcm_final_err:
798 /** Process standard openssl auth algorithms */
800 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
801 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
802 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
809 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
811 offset -= rte_pktmbuf_data_len(m);
814 goto process_auth_err;
816 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
817 goto process_auth_err;
819 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
821 l = rte_pktmbuf_data_len(m) - offset;
823 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
824 goto process_auth_err;
825 goto process_auth_final;
828 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
829 goto process_auth_err;
833 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
834 src = rte_pktmbuf_mtod(m, uint8_t *);
835 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
836 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
837 goto process_auth_err;
842 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
843 goto process_auth_err;
847 OPENSSL_LOG_ERR("Process openssl auth failed");
851 /** Process standard openssl auth algorithms with hmac */
853 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
854 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
855 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
862 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
864 offset -= rte_pktmbuf_data_len(m);
867 goto process_auth_err;
869 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
870 goto process_auth_err;
872 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
874 l = rte_pktmbuf_data_len(m) - offset;
876 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
877 goto process_auth_err;
878 goto process_auth_final;
881 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
882 goto process_auth_err;
886 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
887 src = rte_pktmbuf_mtod(m, uint8_t *);
888 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
889 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
890 goto process_auth_err;
895 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
896 goto process_auth_err;
901 OPENSSL_LOG_ERR("Process openssl auth failed");
905 /*----------------------------------------------------------------------------*/
907 /** Process auth/cipher combined operation */
909 process_openssl_combined_op
910 (struct rte_crypto_op *op, struct openssl_session *sess,
911 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
914 uint8_t *dst = NULL, *iv, *tag, *aad;
915 int srclen, ivlen, aadlen, status = -1;
918 * Segmented destination buffer is not supported for
919 * encryption/decryption
921 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
922 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
926 iv = op->sym->cipher.iv.data;
927 ivlen = op->sym->cipher.iv.length;
928 aad = op->sym->auth.aad.data;
929 aadlen = op->sym->auth.aad.length;
931 tag = op->sym->auth.digest.data;
933 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
934 op->sym->cipher.data.offset +
935 op->sym->cipher.data.length);
937 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
940 srclen = op->sym->cipher.data.length;
941 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
942 op->sym->cipher.data.offset);
945 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
946 status = process_openssl_auth_encryption_gcm(
947 mbuf_src, op->sym->cipher.data.offset, srclen,
948 aad, aadlen, iv, ivlen, sess->cipher.key.data,
949 dst, tag, sess->cipher.ctx,
950 sess->cipher.evp_algo);
952 status = process_openssl_auth_decryption_gcm(
953 mbuf_src, op->sym->cipher.data.offset, srclen,
954 aad, aadlen, iv, ivlen, sess->cipher.key.data,
955 dst, tag, sess->cipher.ctx,
956 sess->cipher.evp_algo);
959 if (status == (-EFAULT) &&
960 sess->auth.operation ==
961 RTE_CRYPTO_AUTH_OP_VERIFY)
962 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
964 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
968 /** Process cipher operation */
970 process_openssl_cipher_op
971 (struct rte_crypto_op *op, struct openssl_session *sess,
972 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
978 * Segmented destination buffer is not supported for
979 * encryption/decryption
981 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
982 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
986 srclen = op->sym->cipher.data.length;
987 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
988 op->sym->cipher.data.offset);
990 iv = op->sym->cipher.iv.data;
992 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
993 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
994 status = process_openssl_cipher_encrypt(mbuf_src, dst,
995 op->sym->cipher.data.offset, iv,
996 sess->cipher.key.data, srclen,
998 sess->cipher.evp_algo);
1000 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1001 op->sym->cipher.data.offset, iv,
1002 sess->cipher.key.data, srclen,
1004 sess->cipher.evp_algo);
1006 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1007 op->sym->cipher.data.offset, iv,
1008 sess->cipher.key.data, srclen,
1012 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1015 /** Process cipher operation */
1017 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1018 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1019 struct rte_mbuf *mbuf_dst)
1021 uint8_t *src, *dst, *iv;
1022 uint8_t block_size, last_block_len;
1023 int srclen, status = 0;
1025 srclen = op->sym->cipher.data.length;
1026 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1027 op->sym->cipher.data.offset);
1028 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1029 op->sym->cipher.data.offset);
1031 iv = op->sym->cipher.iv.data;
1033 block_size = DES_BLOCK_SIZE;
1035 last_block_len = srclen % block_size;
1036 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1037 /* Encrypt only with ECB mode XOR IV */
1038 if (srclen < block_size) {
1039 status = process_openssl_cipher_bpi_encrypt(src, dst,
1041 sess->cipher.bpi_ctx);
1043 srclen -= last_block_len;
1044 /* Encrypt with the block aligned stream with CBC mode */
1045 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1046 op->sym->cipher.data.offset, iv,
1047 sess->cipher.key.data, srclen,
1048 sess->cipher.ctx, sess->cipher.evp_algo);
1049 if (last_block_len) {
1050 /* Point at last block */
1053 * IV is the last encrypted block from
1054 * the previous operation
1056 iv = dst - block_size;
1058 srclen = last_block_len;
1059 /* Encrypt the last frame with ECB mode */
1060 status |= process_openssl_cipher_bpi_encrypt(src,
1062 srclen, sess->cipher.bpi_ctx);
1066 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1067 if (srclen < block_size) {
1068 status = process_openssl_cipher_bpi_encrypt(src, dst,
1071 sess->cipher.bpi_ctx);
1073 if (last_block_len) {
1074 /* Point at last block */
1075 dst += srclen - last_block_len;
1076 src += srclen - last_block_len;
1078 * IV is the last full block
1080 iv = src - block_size;
1082 * Decrypt the last frame with ECB mode
1083 * (encrypt, as it is the same operation)
1085 status = process_openssl_cipher_bpi_encrypt(src,
1087 last_block_len, sess->cipher.bpi_ctx);
1088 /* Prepare parameters for CBC mode op */
1089 iv = op->sym->cipher.iv.data;
1090 dst += last_block_len - srclen;
1091 srclen -= last_block_len;
1094 /* Decrypt with CBC mode */
1095 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1096 op->sym->cipher.data.offset, iv,
1097 sess->cipher.key.data, srclen,
1099 sess->cipher.evp_algo);
1104 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1107 /** Process auth operation */
1109 process_openssl_auth_op
1110 (struct rte_crypto_op *op, struct openssl_session *sess,
1111 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1116 srclen = op->sym->auth.data.length;
1118 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1119 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1120 op->sym->auth.digest.length);
1122 dst = op->sym->auth.digest.data;
1124 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1125 op->sym->auth.data.offset +
1126 op->sym->auth.data.length);
1129 switch (sess->auth.mode) {
1130 case OPENSSL_AUTH_AS_AUTH:
1131 status = process_openssl_auth(mbuf_src, dst,
1132 op->sym->auth.data.offset, NULL, NULL, srclen,
1133 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1135 case OPENSSL_AUTH_AS_HMAC:
1136 status = process_openssl_auth_hmac(mbuf_src, dst,
1137 op->sym->auth.data.offset, NULL,
1138 sess->auth.hmac.pkey, srclen,
1139 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1146 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1147 if (memcmp(dst, op->sym->auth.digest.data,
1148 op->sym->auth.digest.length) != 0) {
1149 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1151 /* Trim area used for digest from mbuf. */
1152 rte_pktmbuf_trim(mbuf_src, op->sym->auth.digest.length);
1156 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1159 /** Process crypto operation for mbuf */
1161 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1162 struct openssl_session *sess)
1164 struct rte_mbuf *msrc, *mdst;
1167 msrc = op->sym->m_src;
1168 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1170 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1172 switch (sess->chain_order) {
1173 case OPENSSL_CHAIN_ONLY_CIPHER:
1174 process_openssl_cipher_op(op, sess, msrc, mdst);
1176 case OPENSSL_CHAIN_ONLY_AUTH:
1177 process_openssl_auth_op(op, sess, msrc, mdst);
1179 case OPENSSL_CHAIN_CIPHER_AUTH:
1180 process_openssl_cipher_op(op, sess, msrc, mdst);
1181 process_openssl_auth_op(op, sess, mdst, mdst);
1183 case OPENSSL_CHAIN_AUTH_CIPHER:
1184 process_openssl_auth_op(op, sess, msrc, mdst);
1185 process_openssl_cipher_op(op, sess, msrc, mdst);
1187 case OPENSSL_CHAIN_COMBINED:
1188 process_openssl_combined_op(op, sess, msrc, mdst);
1190 case OPENSSL_CHAIN_CIPHER_BPI:
1191 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1194 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1198 /* Free session if a session-less crypto op */
1199 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1200 openssl_reset_session(sess);
1201 memset(sess, 0, sizeof(struct openssl_session));
1202 rte_mempool_put(qp->sess_mp, op->sym->session);
1203 op->sym->session = NULL;
1206 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1207 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1209 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1210 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1218 *------------------------------------------------------------------------------
1220 *------------------------------------------------------------------------------
1223 /** Enqueue burst */
1225 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1228 struct openssl_session *sess;
1229 struct openssl_qp *qp = queue_pair;
1232 for (i = 0; i < nb_ops; i++) {
1233 sess = get_session(qp, ops[i]);
1234 if (unlikely(sess == NULL))
1237 retval = process_op(qp, ops[i], sess);
1238 if (unlikely(retval < 0))
1242 qp->stats.enqueued_count += i;
1246 qp->stats.enqueue_err_count++;
1250 /** Dequeue burst */
1252 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1255 struct openssl_qp *qp = queue_pair;
1257 unsigned int nb_dequeued = 0;
1259 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1260 (void **)ops, nb_ops, NULL);
1261 qp->stats.dequeued_count += nb_dequeued;
1266 /** Create OPENSSL crypto device */
1268 cryptodev_openssl_create(const char *name,
1269 struct rte_vdev_device *vdev,
1270 struct rte_crypto_vdev_init_params *init_params)
1272 struct rte_cryptodev *dev;
1273 struct openssl_private *internals;
1275 if (init_params->name[0] == '\0')
1276 snprintf(init_params->name, sizeof(init_params->name),
1279 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1280 sizeof(struct openssl_private),
1281 init_params->socket_id,
1284 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1288 dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
1289 dev->dev_ops = rte_openssl_pmd_ops;
1291 /* register rx/tx burst functions for data path */
1292 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1293 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1295 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1296 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1297 RTE_CRYPTODEV_FF_CPU_AESNI |
1298 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1300 /* Set vector instructions mode supported */
1301 internals = dev->data->dev_private;
1303 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1304 internals->max_nb_sessions = init_params->max_nb_sessions;
1309 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1312 cryptodev_openssl_remove(vdev);
1316 /** Initialise OPENSSL crypto device */
1318 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1320 struct rte_crypto_vdev_init_params init_params = {
1321 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1322 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1327 const char *input_args;
1329 name = rte_vdev_device_name(vdev);
1332 input_args = rte_vdev_device_args(vdev);
1334 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1336 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1337 init_params.socket_id);
1338 if (init_params.name[0] != '\0')
1339 RTE_LOG(INFO, PMD, " User defined name = %s\n",
1341 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1342 init_params.max_nb_queue_pairs);
1343 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1344 init_params.max_nb_sessions);
1346 return cryptodev_openssl_create(name, vdev, &init_params);
1349 /** Uninitialise OPENSSL crypto device */
1351 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1355 name = rte_vdev_device_name(vdev);
1360 "Closing OPENSSL crypto device %s on numa socket %u\n",
1361 name, rte_socket_id());
1366 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1367 .probe = cryptodev_openssl_probe,
1368 .remove = cryptodev_openssl_remove
1371 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1372 cryptodev_openssl_pmd_drv);
1373 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1374 "max_nb_queue_pairs=<int> "
1375 "max_nb_sessions=<int> "