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/hmac.h>
43 #include <openssl/evp.h>
45 #include "rte_openssl_pmd_private.h"
47 #define DES_BLOCK_SIZE 8
49 static uint8_t cryptodev_driver_id;
51 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
52 static HMAC_CTX *HMAC_CTX_new(void)
54 HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
61 static void HMAC_CTX_free(HMAC_CTX *ctx)
64 HMAC_CTX_cleanup(ctx);
70 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
72 /*----------------------------------------------------------------------------*/
75 * Increment counter by 1
76 * Counter is 64 bit array, big-endian
81 uint64_t *ctr64 = (uint64_t *)ctr;
83 *ctr64 = __builtin_bswap64(*ctr64);
85 *ctr64 = __builtin_bswap64(*ctr64);
89 *------------------------------------------------------------------------------
91 *------------------------------------------------------------------------------
94 /** Get xform chain order */
95 static enum openssl_chain_order
96 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
98 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
101 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
102 if (xform->next == NULL)
103 res = OPENSSL_CHAIN_ONLY_AUTH;
104 else if (xform->next->type ==
105 RTE_CRYPTO_SYM_XFORM_CIPHER)
106 res = OPENSSL_CHAIN_AUTH_CIPHER;
108 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
109 if (xform->next == NULL)
110 res = OPENSSL_CHAIN_ONLY_CIPHER;
111 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
112 res = OPENSSL_CHAIN_CIPHER_AUTH;
114 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
115 res = OPENSSL_CHAIN_COMBINED;
121 /** Get session cipher key from input cipher key */
123 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
125 memcpy(session_key, input_key, keylen);
128 /** Get key ede 24 bytes standard from input key */
130 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
134 /* Initialize keys - 24 bytes: [key1-key2-key3] */
137 memcpy(key_ede, key, 24);
141 memcpy(key_ede, key, 16);
142 memcpy(key_ede + 16, key, 8);
145 /* K1 = K2 = K3 (DES compatibility) */
146 memcpy(key_ede, key, 8);
147 memcpy(key_ede + 8, key, 8);
148 memcpy(key_ede + 16, key, 8);
151 OPENSSL_LOG_ERR("Unsupported key size");
158 /** Get adequate openssl function for input cipher algorithm */
160 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
161 const EVP_CIPHER **algo)
167 case RTE_CRYPTO_CIPHER_3DES_CBC:
170 *algo = EVP_des_ede_cbc();
173 *algo = EVP_des_ede3_cbc();
179 case RTE_CRYPTO_CIPHER_3DES_CTR:
181 case RTE_CRYPTO_CIPHER_AES_CBC:
184 *algo = EVP_aes_128_cbc();
187 *algo = EVP_aes_192_cbc();
190 *algo = EVP_aes_256_cbc();
196 case RTE_CRYPTO_CIPHER_AES_CTR:
199 *algo = EVP_aes_128_ctr();
202 *algo = EVP_aes_192_ctr();
205 *algo = EVP_aes_256_ctr();
222 /** Get adequate openssl function for input auth algorithm */
224 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
231 case RTE_CRYPTO_AUTH_MD5:
232 case RTE_CRYPTO_AUTH_MD5_HMAC:
235 case RTE_CRYPTO_AUTH_SHA1:
236 case RTE_CRYPTO_AUTH_SHA1_HMAC:
239 case RTE_CRYPTO_AUTH_SHA224:
240 case RTE_CRYPTO_AUTH_SHA224_HMAC:
241 *algo = EVP_sha224();
243 case RTE_CRYPTO_AUTH_SHA256:
244 case RTE_CRYPTO_AUTH_SHA256_HMAC:
245 *algo = EVP_sha256();
247 case RTE_CRYPTO_AUTH_SHA384:
248 case RTE_CRYPTO_AUTH_SHA384_HMAC:
249 *algo = EVP_sha384();
251 case RTE_CRYPTO_AUTH_SHA512:
252 case RTE_CRYPTO_AUTH_SHA512_HMAC:
253 *algo = EVP_sha512();
266 /** Get adequate openssl function for input cipher algorithm */
268 get_aead_algo(enum rte_crypto_aead_algorithm sess_algo, size_t keylen,
269 const EVP_CIPHER **algo)
275 case RTE_CRYPTO_AEAD_AES_GCM:
278 *algo = EVP_aes_128_gcm();
281 *algo = EVP_aes_192_gcm();
284 *algo = EVP_aes_256_gcm();
301 /** Set session cipher parameters */
303 openssl_set_session_cipher_parameters(struct openssl_session *sess,
304 const struct rte_crypto_sym_xform *xform)
306 /* Select cipher direction */
307 sess->cipher.direction = xform->cipher.op;
308 /* Select cipher key */
309 sess->cipher.key.length = xform->cipher.key.length;
311 /* Set IV parameters */
312 sess->iv.offset = xform->cipher.iv.offset;
313 sess->iv.length = xform->cipher.iv.length;
315 /* Select cipher algo */
316 switch (xform->cipher.algo) {
317 case RTE_CRYPTO_CIPHER_3DES_CBC:
318 case RTE_CRYPTO_CIPHER_AES_CBC:
319 case RTE_CRYPTO_CIPHER_AES_CTR:
320 sess->cipher.mode = OPENSSL_CIPHER_LIB;
321 sess->cipher.algo = xform->cipher.algo;
322 sess->cipher.ctx = EVP_CIPHER_CTX_new();
324 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
325 &sess->cipher.evp_algo) != 0)
328 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
329 sess->cipher.key.data);
330 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
331 if (EVP_EncryptInit_ex(sess->cipher.ctx,
332 sess->cipher.evp_algo,
333 NULL, xform->cipher.key.data,
337 } else if (sess->cipher.direction ==
338 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
339 if (EVP_DecryptInit_ex(sess->cipher.ctx,
340 sess->cipher.evp_algo,
341 NULL, xform->cipher.key.data,
349 case RTE_CRYPTO_CIPHER_3DES_CTR:
350 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
351 sess->cipher.ctx = EVP_CIPHER_CTX_new();
353 if (get_cipher_key_ede(xform->cipher.key.data,
354 sess->cipher.key.length,
355 sess->cipher.key.data) != 0)
359 case RTE_CRYPTO_CIPHER_DES_CBC:
360 sess->cipher.algo = xform->cipher.algo;
361 sess->cipher.ctx = EVP_CIPHER_CTX_new();
362 sess->cipher.evp_algo = EVP_des_cbc();
364 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
365 sess->cipher.key.data);
366 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
367 if (EVP_EncryptInit_ex(sess->cipher.ctx,
368 sess->cipher.evp_algo,
369 NULL, xform->cipher.key.data,
373 } else if (sess->cipher.direction ==
374 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
375 if (EVP_DecryptInit_ex(sess->cipher.ctx,
376 sess->cipher.evp_algo,
377 NULL, xform->cipher.key.data,
385 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
386 sess->cipher.algo = xform->cipher.algo;
387 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
388 sess->cipher.ctx = EVP_CIPHER_CTX_new();
389 sess->cipher.evp_algo = EVP_des_cbc();
391 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
392 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
393 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
394 NULL, xform->cipher.key.data, 0) != 1)
397 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
398 sess->cipher.key.data);
399 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
400 if (EVP_EncryptInit_ex(sess->cipher.ctx,
401 sess->cipher.evp_algo,
402 NULL, xform->cipher.key.data,
406 } else if (sess->cipher.direction ==
407 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
408 if (EVP_DecryptInit_ex(sess->cipher.ctx,
409 sess->cipher.evp_algo,
410 NULL, xform->cipher.key.data,
418 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
425 /* Set session auth parameters */
427 openssl_set_session_auth_parameters(struct openssl_session *sess,
428 const struct rte_crypto_sym_xform *xform)
430 /* Select auth generate/verify */
431 sess->auth.operation = xform->auth.op;
432 sess->auth.algo = xform->auth.algo;
434 /* Select auth algo */
435 switch (xform->auth.algo) {
436 case RTE_CRYPTO_AUTH_AES_GMAC:
437 sess->chain_order = OPENSSL_CHAIN_COMBINED;
439 /* Set IV parameters */
440 sess->iv.offset = xform->auth.iv.offset;
441 sess->iv.length = xform->auth.iv.length;
444 * OpenSSL requires GMAC to be a GCM operation
445 * with no cipher data length
447 sess->cipher.mode = OPENSSL_CIPHER_LIB;
448 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE)
449 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
451 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_DECRYPT;
453 sess->cipher.key.length = xform->auth.key.length;
454 sess->cipher.ctx = EVP_CIPHER_CTX_new();
456 if (get_aead_algo(RTE_CRYPTO_AEAD_AES_GCM,
457 sess->cipher.key.length,
458 &sess->cipher.evp_algo) != 0)
461 get_cipher_key(xform->auth.key.data, xform->auth.key.length,
462 sess->cipher.key.data);
466 case RTE_CRYPTO_AUTH_MD5:
467 case RTE_CRYPTO_AUTH_SHA1:
468 case RTE_CRYPTO_AUTH_SHA224:
469 case RTE_CRYPTO_AUTH_SHA256:
470 case RTE_CRYPTO_AUTH_SHA384:
471 case RTE_CRYPTO_AUTH_SHA512:
472 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
473 if (get_auth_algo(xform->auth.algo,
474 &sess->auth.auth.evp_algo) != 0)
476 sess->auth.auth.ctx = EVP_MD_CTX_create();
479 case RTE_CRYPTO_AUTH_MD5_HMAC:
480 case RTE_CRYPTO_AUTH_SHA1_HMAC:
481 case RTE_CRYPTO_AUTH_SHA224_HMAC:
482 case RTE_CRYPTO_AUTH_SHA256_HMAC:
483 case RTE_CRYPTO_AUTH_SHA384_HMAC:
484 case RTE_CRYPTO_AUTH_SHA512_HMAC:
485 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
486 sess->auth.hmac.ctx = HMAC_CTX_new();
487 if (get_auth_algo(xform->auth.algo,
488 &sess->auth.hmac.evp_algo) != 0)
491 if (HMAC_Init_ex(sess->auth.hmac.ctx,
492 xform->auth.key.data,
493 xform->auth.key.length,
494 sess->auth.hmac.evp_algo, NULL) != 1)
502 sess->auth.digest_length = xform->auth.digest_length;
507 /* Set session AEAD parameters */
509 openssl_set_session_aead_parameters(struct openssl_session *sess,
510 const struct rte_crypto_sym_xform *xform)
512 /* Select cipher direction */
513 sess->cipher.direction = xform->cipher.op;
514 /* Select cipher key */
515 sess->cipher.key.length = xform->aead.key.length;
517 /* Set IV parameters */
518 sess->iv.offset = xform->aead.iv.offset;
519 sess->iv.length = xform->aead.iv.length;
521 /* Select auth generate/verify */
522 sess->auth.operation = xform->auth.op;
523 sess->auth.algo = xform->auth.algo;
525 /* Select auth algo */
526 switch (xform->aead.algo) {
527 case RTE_CRYPTO_AEAD_AES_GCM:
528 sess->cipher.mode = OPENSSL_CIPHER_LIB;
529 sess->aead_algo = xform->aead.algo;
530 sess->cipher.ctx = EVP_CIPHER_CTX_new();
532 if (get_aead_algo(sess->aead_algo, sess->cipher.key.length,
533 &sess->cipher.evp_algo) != 0)
536 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
537 sess->cipher.key.data);
539 sess->chain_order = OPENSSL_CHAIN_COMBINED;
545 sess->auth.aad_length = xform->aead.aad_length;
546 sess->auth.digest_length = xform->aead.digest_length;
551 /** Parse crypto xform chain and set private session parameters */
553 openssl_set_session_parameters(struct openssl_session *sess,
554 const struct rte_crypto_sym_xform *xform)
556 const struct rte_crypto_sym_xform *cipher_xform = NULL;
557 const struct rte_crypto_sym_xform *auth_xform = NULL;
558 const struct rte_crypto_sym_xform *aead_xform = NULL;
561 sess->chain_order = openssl_get_chain_order(xform);
562 switch (sess->chain_order) {
563 case OPENSSL_CHAIN_ONLY_CIPHER:
564 cipher_xform = xform;
566 case OPENSSL_CHAIN_ONLY_AUTH:
569 case OPENSSL_CHAIN_CIPHER_AUTH:
570 cipher_xform = xform;
571 auth_xform = xform->next;
573 case OPENSSL_CHAIN_AUTH_CIPHER:
575 cipher_xform = xform->next;
577 case OPENSSL_CHAIN_COMBINED:
584 /* Default IV length = 0 */
587 /* cipher_xform must be check before auth_xform */
589 ret = openssl_set_session_cipher_parameters(
593 "Invalid/unsupported cipher parameters");
599 ret = openssl_set_session_auth_parameters(sess, auth_xform);
602 "Invalid/unsupported auth parameters");
608 ret = openssl_set_session_aead_parameters(sess, aead_xform);
611 "Invalid/unsupported AEAD parameters");
619 /** Reset private session parameters */
621 openssl_reset_session(struct openssl_session *sess)
623 EVP_CIPHER_CTX_free(sess->cipher.ctx);
625 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
626 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
628 switch (sess->auth.mode) {
629 case OPENSSL_AUTH_AS_AUTH:
630 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
632 case OPENSSL_AUTH_AS_HMAC:
633 EVP_PKEY_free(sess->auth.hmac.pkey);
634 HMAC_CTX_free(sess->auth.hmac.ctx);
641 /** Provide session for operation */
642 static struct openssl_session *
643 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
645 struct openssl_session *sess = NULL;
647 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
648 /* get existing session */
649 if (likely(op->sym->session != NULL))
650 sess = (struct openssl_session *)
651 get_session_private_data(
653 cryptodev_driver_id);
655 /* provide internal session */
657 void *_sess_private_data = NULL;
659 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
662 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
665 sess = (struct openssl_session *)_sess_private_data;
667 if (unlikely(openssl_set_session_parameters(sess,
668 op->sym->xform) != 0)) {
669 rte_mempool_put(qp->sess_mp, _sess);
670 rte_mempool_put(qp->sess_mp, _sess_private_data);
673 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
674 set_session_private_data(op->sym->session, cryptodev_driver_id,
679 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
685 *------------------------------------------------------------------------------
687 *------------------------------------------------------------------------------
690 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
691 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
698 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
700 offset -= rte_pktmbuf_data_len(m);
705 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
707 l = rte_pktmbuf_data_len(m) - offset;
709 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
715 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
721 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
722 src = rte_pktmbuf_mtod(m, uint8_t *);
723 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
724 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
734 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
735 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
742 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
744 offset -= rte_pktmbuf_data_len(m);
749 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
751 l = rte_pktmbuf_data_len(m) - offset;
753 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
759 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
765 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
766 src = rte_pktmbuf_mtod(m, uint8_t *);
767 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
768 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
777 /** Process standard openssl cipher encryption */
779 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
780 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx)
784 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
785 goto process_cipher_encrypt_err;
787 EVP_CIPHER_CTX_set_padding(ctx, 0);
789 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
791 goto process_cipher_encrypt_err;
793 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
794 goto process_cipher_encrypt_err;
798 process_cipher_encrypt_err:
799 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
803 /** Process standard openssl cipher encryption */
805 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
806 uint8_t *iv, int srclen,
810 uint8_t encrypted_iv[DES_BLOCK_SIZE];
813 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
814 iv, DES_BLOCK_SIZE) <= 0)
815 goto process_cipher_encrypt_err;
817 for (i = 0; i < srclen; i++)
818 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
822 process_cipher_encrypt_err:
823 OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
826 /** Process standard openssl cipher decryption */
828 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
829 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx)
833 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
834 goto process_cipher_decrypt_err;
836 EVP_CIPHER_CTX_set_padding(ctx, 0);
838 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
840 goto process_cipher_decrypt_err;
842 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
843 goto process_cipher_decrypt_err;
846 process_cipher_decrypt_err:
847 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
851 /** Process cipher des 3 ctr encryption, decryption algorithm */
853 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
854 int offset, uint8_t *iv, uint8_t *key, int srclen,
857 uint8_t ebuf[8], ctr[8];
863 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
865 offset -= rte_pktmbuf_data_len(m);
868 goto process_cipher_des3ctr_err;
870 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
871 l = rte_pktmbuf_data_len(m) - offset;
873 /* We use 3DES encryption also for decryption.
874 * IV is not important for 3DES ecb
876 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
877 goto process_cipher_des3ctr_err;
881 for (n = 0; n < srclen; n++) {
883 if (EVP_EncryptUpdate(ctx,
884 (unsigned char *)&ebuf, &unused,
885 (const unsigned char *)&ctr, 8) <= 0)
886 goto process_cipher_des3ctr_err;
889 dst[n] = *(src++) ^ ebuf[n % 8];
895 src = rte_pktmbuf_mtod(m, uint8_t *);
896 l = rte_pktmbuf_data_len(m);
903 process_cipher_des3ctr_err:
904 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
908 /** Process auth/encription aes-gcm algorithm */
910 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
911 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
912 uint8_t *key, uint8_t *dst, uint8_t *tag,
913 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
915 int len = 0, unused = 0;
916 uint8_t empty[] = {};
918 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
919 goto process_auth_encryption_gcm_err;
921 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
922 goto process_auth_encryption_gcm_err;
924 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
925 goto process_auth_encryption_gcm_err;
928 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
929 goto process_auth_encryption_gcm_err;
932 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
934 goto process_auth_encryption_gcm_err;
936 /* Workaround open ssl bug in version less then 1.0.1f */
937 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
938 goto process_auth_encryption_gcm_err;
940 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
941 goto process_auth_encryption_gcm_err;
943 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
944 goto process_auth_encryption_gcm_err;
948 process_auth_encryption_gcm_err:
949 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
954 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
955 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
956 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
957 const EVP_CIPHER *algo)
959 int len = 0, unused = 0;
960 uint8_t empty[] = {};
962 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
963 goto process_auth_decryption_gcm_err;
965 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
966 goto process_auth_decryption_gcm_err;
968 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
969 goto process_auth_decryption_gcm_err;
971 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
972 goto process_auth_decryption_gcm_err;
975 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
976 goto process_auth_decryption_gcm_err;
979 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
981 goto process_auth_decryption_gcm_err;
983 /* Workaround open ssl bug in version less then 1.0.1f */
984 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
985 goto process_auth_decryption_gcm_err;
987 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
988 goto process_auth_decryption_gcm_final_err;
992 process_auth_decryption_gcm_err:
993 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
996 process_auth_decryption_gcm_final_err:
1000 /** Process standard openssl auth algorithms */
1002 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1003 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
1004 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
1011 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1013 offset -= rte_pktmbuf_data_len(m);
1016 goto process_auth_err;
1018 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
1019 goto process_auth_err;
1021 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1023 l = rte_pktmbuf_data_len(m) - offset;
1025 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
1026 goto process_auth_err;
1027 goto process_auth_final;
1030 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1031 goto process_auth_err;
1035 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1036 src = rte_pktmbuf_mtod(m, uint8_t *);
1037 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1038 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1039 goto process_auth_err;
1044 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
1045 goto process_auth_err;
1049 OPENSSL_LOG_ERR("Process openssl auth failed");
1053 /** Process standard openssl auth algorithms with hmac */
1055 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1056 int srclen, HMAC_CTX *ctx)
1058 unsigned int dstlen;
1063 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1065 offset -= rte_pktmbuf_data_len(m);
1068 goto process_auth_err;
1070 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1072 l = rte_pktmbuf_data_len(m) - offset;
1074 if (HMAC_Update(ctx, (unsigned char *)src, srclen) != 1)
1075 goto process_auth_err;
1076 goto process_auth_final;
1079 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1080 goto process_auth_err;
1084 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1085 src = rte_pktmbuf_mtod(m, uint8_t *);
1086 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1087 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1088 goto process_auth_err;
1093 if (HMAC_Final(ctx, dst, &dstlen) != 1)
1094 goto process_auth_err;
1096 if (unlikely(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) != 1))
1097 goto process_auth_err;
1102 OPENSSL_LOG_ERR("Process openssl auth failed");
1106 /*----------------------------------------------------------------------------*/
1108 /** Process auth/cipher combined operation */
1110 process_openssl_combined_op
1111 (struct rte_crypto_op *op, struct openssl_session *sess,
1112 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1115 uint8_t *dst = NULL, *iv, *tag, *aad;
1116 int srclen, ivlen, aadlen, status = -1;
1120 * Segmented destination buffer is not supported for
1121 * encryption/decryption
1123 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1124 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1128 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1130 ivlen = sess->iv.length;
1131 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1133 offset = op->sym->auth.data.offset;
1134 aadlen = op->sym->auth.data.length;
1135 aad = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1136 op->sym->auth.data.offset);
1137 tag = op->sym->auth.digest.data;
1139 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1142 srclen = op->sym->aead.data.length;
1143 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1144 op->sym->aead.data.offset);
1145 offset = op->sym->aead.data.offset;
1146 aad = op->sym->aead.aad.data;
1147 aadlen = sess->auth.aad_length;
1148 tag = op->sym->aead.digest.data;
1150 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1154 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1155 status = process_openssl_auth_encryption_gcm(
1156 mbuf_src, offset, srclen,
1157 aad, aadlen, iv, ivlen, sess->cipher.key.data,
1158 dst, tag, sess->cipher.ctx,
1159 sess->cipher.evp_algo);
1161 status = process_openssl_auth_decryption_gcm(
1162 mbuf_src, offset, srclen,
1163 aad, aadlen, iv, ivlen, sess->cipher.key.data,
1164 dst, tag, sess->cipher.ctx,
1165 sess->cipher.evp_algo);
1168 if (status == (-EFAULT) &&
1169 sess->auth.operation ==
1170 RTE_CRYPTO_AUTH_OP_VERIFY)
1171 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1173 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1177 /** Process cipher operation */
1179 process_openssl_cipher_op
1180 (struct rte_crypto_op *op, struct openssl_session *sess,
1181 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1187 * Segmented destination buffer is not supported for
1188 * encryption/decryption
1190 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1191 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1195 srclen = op->sym->cipher.data.length;
1196 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1197 op->sym->cipher.data.offset);
1199 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1202 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1203 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1204 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1205 op->sym->cipher.data.offset, iv,
1206 srclen, sess->cipher.ctx);
1208 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1209 op->sym->cipher.data.offset, iv,
1210 srclen, sess->cipher.ctx);
1212 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1213 op->sym->cipher.data.offset, iv,
1214 sess->cipher.key.data, srclen,
1218 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1221 /** Process cipher operation */
1223 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1224 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1225 struct rte_mbuf *mbuf_dst)
1227 uint8_t *src, *dst, *iv;
1228 uint8_t block_size, last_block_len;
1229 int srclen, status = 0;
1231 srclen = op->sym->cipher.data.length;
1232 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1233 op->sym->cipher.data.offset);
1234 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1235 op->sym->cipher.data.offset);
1237 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1240 block_size = DES_BLOCK_SIZE;
1242 last_block_len = srclen % block_size;
1243 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1244 /* Encrypt only with ECB mode XOR IV */
1245 if (srclen < block_size) {
1246 status = process_openssl_cipher_bpi_encrypt(src, dst,
1248 sess->cipher.bpi_ctx);
1250 srclen -= last_block_len;
1251 /* Encrypt with the block aligned stream with CBC mode */
1252 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1253 op->sym->cipher.data.offset, iv,
1254 srclen, sess->cipher.ctx);
1255 if (last_block_len) {
1256 /* Point at last block */
1259 * IV is the last encrypted block from
1260 * the previous operation
1262 iv = dst - block_size;
1264 srclen = last_block_len;
1265 /* Encrypt the last frame with ECB mode */
1266 status |= process_openssl_cipher_bpi_encrypt(src,
1268 srclen, sess->cipher.bpi_ctx);
1272 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1273 if (srclen < block_size) {
1274 status = process_openssl_cipher_bpi_encrypt(src, dst,
1277 sess->cipher.bpi_ctx);
1279 if (last_block_len) {
1280 /* Point at last block */
1281 dst += srclen - last_block_len;
1282 src += srclen - last_block_len;
1284 * IV is the last full block
1286 iv = src - block_size;
1288 * Decrypt the last frame with ECB mode
1289 * (encrypt, as it is the same operation)
1291 status = process_openssl_cipher_bpi_encrypt(src,
1293 last_block_len, sess->cipher.bpi_ctx);
1294 /* Prepare parameters for CBC mode op */
1295 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1297 dst += last_block_len - srclen;
1298 srclen -= last_block_len;
1301 /* Decrypt with CBC mode */
1302 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1303 op->sym->cipher.data.offset, iv,
1304 srclen, sess->cipher.ctx);
1309 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1312 /** Process auth operation */
1314 process_openssl_auth_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1315 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1316 struct rte_mbuf *mbuf_dst)
1321 srclen = op->sym->auth.data.length;
1323 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1324 dst = qp->temp_digest;
1326 dst = op->sym->auth.digest.data;
1328 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1329 op->sym->auth.data.offset +
1330 op->sym->auth.data.length);
1333 switch (sess->auth.mode) {
1334 case OPENSSL_AUTH_AS_AUTH:
1335 status = process_openssl_auth(mbuf_src, dst,
1336 op->sym->auth.data.offset, NULL, NULL, srclen,
1337 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1339 case OPENSSL_AUTH_AS_HMAC:
1340 status = process_openssl_auth_hmac(mbuf_src, dst,
1341 op->sym->auth.data.offset, srclen,
1342 sess->auth.hmac.ctx);
1349 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1350 if (memcmp(dst, op->sym->auth.digest.data,
1351 sess->auth.digest_length) != 0) {
1352 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1357 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1360 /** Process crypto operation for mbuf */
1362 process_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1363 struct openssl_session *sess)
1365 struct rte_mbuf *msrc, *mdst;
1368 msrc = op->sym->m_src;
1369 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1371 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1373 switch (sess->chain_order) {
1374 case OPENSSL_CHAIN_ONLY_CIPHER:
1375 process_openssl_cipher_op(op, sess, msrc, mdst);
1377 case OPENSSL_CHAIN_ONLY_AUTH:
1378 process_openssl_auth_op(qp, op, sess, msrc, mdst);
1380 case OPENSSL_CHAIN_CIPHER_AUTH:
1381 process_openssl_cipher_op(op, sess, msrc, mdst);
1382 process_openssl_auth_op(qp, op, sess, mdst, mdst);
1384 case OPENSSL_CHAIN_AUTH_CIPHER:
1385 process_openssl_auth_op(qp, op, sess, msrc, mdst);
1386 process_openssl_cipher_op(op, sess, msrc, mdst);
1388 case OPENSSL_CHAIN_COMBINED:
1389 process_openssl_combined_op(op, sess, msrc, mdst);
1391 case OPENSSL_CHAIN_CIPHER_BPI:
1392 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1395 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1399 /* Free session if a session-less crypto op */
1400 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1401 openssl_reset_session(sess);
1402 memset(sess, 0, sizeof(struct openssl_session));
1403 memset(op->sym->session, 0,
1404 rte_cryptodev_get_header_session_size());
1405 rte_mempool_put(qp->sess_mp, sess);
1406 rte_mempool_put(qp->sess_mp, op->sym->session);
1407 op->sym->session = NULL;
1410 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1411 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1413 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1414 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1422 *------------------------------------------------------------------------------
1424 *------------------------------------------------------------------------------
1427 /** Enqueue burst */
1429 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1432 struct openssl_session *sess;
1433 struct openssl_qp *qp = queue_pair;
1436 for (i = 0; i < nb_ops; i++) {
1437 sess = get_session(qp, ops[i]);
1438 if (unlikely(sess == NULL))
1441 retval = process_op(qp, ops[i], sess);
1442 if (unlikely(retval < 0))
1446 qp->stats.enqueued_count += i;
1450 qp->stats.enqueue_err_count++;
1454 /** Dequeue burst */
1456 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1459 struct openssl_qp *qp = queue_pair;
1461 unsigned int nb_dequeued = 0;
1463 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1464 (void **)ops, nb_ops, NULL);
1465 qp->stats.dequeued_count += nb_dequeued;
1470 /** Create OPENSSL crypto device */
1472 cryptodev_openssl_create(const char *name,
1473 struct rte_vdev_device *vdev,
1474 struct rte_crypto_vdev_init_params *init_params)
1476 struct rte_cryptodev *dev;
1477 struct openssl_private *internals;
1479 if (init_params->name[0] == '\0')
1480 snprintf(init_params->name, sizeof(init_params->name),
1483 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1484 sizeof(struct openssl_private),
1485 init_params->socket_id,
1488 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1492 dev->driver_id = cryptodev_driver_id;
1493 dev->dev_ops = rte_openssl_pmd_ops;
1495 /* register rx/tx burst functions for data path */
1496 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1497 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1499 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1500 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1501 RTE_CRYPTODEV_FF_CPU_AESNI |
1502 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1504 /* Set vector instructions mode supported */
1505 internals = dev->data->dev_private;
1507 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1508 internals->max_nb_sessions = init_params->max_nb_sessions;
1513 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1516 cryptodev_openssl_remove(vdev);
1520 /** Initialise OPENSSL crypto device */
1522 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1524 struct rte_crypto_vdev_init_params init_params = {
1525 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1526 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1531 const char *input_args;
1533 name = rte_vdev_device_name(vdev);
1536 input_args = rte_vdev_device_args(vdev);
1538 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1540 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1541 init_params.socket_id);
1542 if (init_params.name[0] != '\0')
1543 RTE_LOG(INFO, PMD, " User defined name = %s\n",
1545 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1546 init_params.max_nb_queue_pairs);
1547 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1548 init_params.max_nb_sessions);
1550 return cryptodev_openssl_create(name, vdev, &init_params);
1553 /** Uninitialise OPENSSL crypto device */
1555 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1559 name = rte_vdev_device_name(vdev);
1564 "Closing OPENSSL crypto device %s on numa socket %u\n",
1565 name, rte_socket_id());
1570 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1571 .probe = cryptodev_openssl_probe,
1572 .remove = cryptodev_openssl_remove
1575 static struct cryptodev_driver openssl_crypto_drv;
1577 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1578 cryptodev_openssl_pmd_drv);
1579 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1580 "max_nb_queue_pairs=<int> "
1581 "max_nb_sessions=<int> "
1583 RTE_PMD_REGISTER_CRYPTO_DRIVER(openssl_crypto_drv, cryptodev_openssl_pmd_drv,
1584 cryptodev_driver_id);