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)
358 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
359 sess->cipher.algo = xform->cipher.algo;
360 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
361 sess->cipher.ctx = EVP_CIPHER_CTX_new();
362 sess->cipher.evp_algo = EVP_des_cbc();
364 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
365 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
366 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
367 NULL, xform->cipher.key.data, 0) != 1)
370 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
371 sess->cipher.key.data);
372 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
373 if (EVP_EncryptInit_ex(sess->cipher.ctx,
374 sess->cipher.evp_algo,
375 NULL, xform->cipher.key.data,
379 } else if (sess->cipher.direction ==
380 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
381 if (EVP_DecryptInit_ex(sess->cipher.ctx,
382 sess->cipher.evp_algo,
383 NULL, xform->cipher.key.data,
391 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
398 /* Set session auth parameters */
400 openssl_set_session_auth_parameters(struct openssl_session *sess,
401 const struct rte_crypto_sym_xform *xform)
403 /* Select auth generate/verify */
404 sess->auth.operation = xform->auth.op;
405 sess->auth.algo = xform->auth.algo;
407 /* Select auth algo */
408 switch (xform->auth.algo) {
409 case RTE_CRYPTO_AUTH_AES_GMAC:
410 sess->chain_order = OPENSSL_CHAIN_COMBINED;
412 /* Set IV parameters */
413 sess->iv.offset = xform->auth.iv.offset;
414 sess->iv.length = xform->auth.iv.length;
417 * OpenSSL requires GMAC to be a GCM operation
418 * with no cipher data length
420 sess->cipher.mode = OPENSSL_CIPHER_LIB;
421 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE)
422 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
424 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_DECRYPT;
426 sess->cipher.key.length = xform->auth.key.length;
427 sess->cipher.ctx = EVP_CIPHER_CTX_new();
429 if (get_aead_algo(RTE_CRYPTO_AEAD_AES_GCM,
430 sess->cipher.key.length,
431 &sess->cipher.evp_algo) != 0)
434 get_cipher_key(xform->auth.key.data, xform->auth.key.length,
435 sess->cipher.key.data);
439 case RTE_CRYPTO_AUTH_MD5:
440 case RTE_CRYPTO_AUTH_SHA1:
441 case RTE_CRYPTO_AUTH_SHA224:
442 case RTE_CRYPTO_AUTH_SHA256:
443 case RTE_CRYPTO_AUTH_SHA384:
444 case RTE_CRYPTO_AUTH_SHA512:
445 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
446 if (get_auth_algo(xform->auth.algo,
447 &sess->auth.auth.evp_algo) != 0)
449 sess->auth.auth.ctx = EVP_MD_CTX_create();
452 case RTE_CRYPTO_AUTH_MD5_HMAC:
453 case RTE_CRYPTO_AUTH_SHA1_HMAC:
454 case RTE_CRYPTO_AUTH_SHA224_HMAC:
455 case RTE_CRYPTO_AUTH_SHA256_HMAC:
456 case RTE_CRYPTO_AUTH_SHA384_HMAC:
457 case RTE_CRYPTO_AUTH_SHA512_HMAC:
458 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
459 sess->auth.hmac.ctx = HMAC_CTX_new();
460 if (get_auth_algo(xform->auth.algo,
461 &sess->auth.hmac.evp_algo) != 0)
464 if (HMAC_Init_ex(sess->auth.hmac.ctx,
465 xform->auth.key.data,
466 xform->auth.key.length,
467 sess->auth.hmac.evp_algo, NULL) != 1)
475 sess->auth.digest_length = xform->auth.digest_length;
480 /* Set session AEAD parameters */
482 openssl_set_session_aead_parameters(struct openssl_session *sess,
483 const struct rte_crypto_sym_xform *xform)
485 /* Select cipher direction */
486 sess->cipher.direction = xform->cipher.op;
487 /* Select cipher key */
488 sess->cipher.key.length = xform->aead.key.length;
490 /* Set IV parameters */
491 sess->iv.offset = xform->aead.iv.offset;
492 sess->iv.length = xform->aead.iv.length;
494 /* Select auth generate/verify */
495 sess->auth.operation = xform->auth.op;
496 sess->auth.algo = xform->auth.algo;
498 /* Select auth algo */
499 switch (xform->aead.algo) {
500 case RTE_CRYPTO_AEAD_AES_GCM:
501 sess->cipher.mode = OPENSSL_CIPHER_LIB;
502 sess->aead_algo = xform->aead.algo;
503 sess->cipher.ctx = EVP_CIPHER_CTX_new();
505 if (get_aead_algo(sess->aead_algo, sess->cipher.key.length,
506 &sess->cipher.evp_algo) != 0)
509 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
510 sess->cipher.key.data);
512 sess->chain_order = OPENSSL_CHAIN_COMBINED;
518 sess->auth.aad_length = xform->aead.aad_length;
519 sess->auth.digest_length = xform->aead.digest_length;
524 /** Parse crypto xform chain and set private session parameters */
526 openssl_set_session_parameters(struct openssl_session *sess,
527 const struct rte_crypto_sym_xform *xform)
529 const struct rte_crypto_sym_xform *cipher_xform = NULL;
530 const struct rte_crypto_sym_xform *auth_xform = NULL;
531 const struct rte_crypto_sym_xform *aead_xform = NULL;
534 sess->chain_order = openssl_get_chain_order(xform);
535 switch (sess->chain_order) {
536 case OPENSSL_CHAIN_ONLY_CIPHER:
537 cipher_xform = xform;
539 case OPENSSL_CHAIN_ONLY_AUTH:
542 case OPENSSL_CHAIN_CIPHER_AUTH:
543 cipher_xform = xform;
544 auth_xform = xform->next;
546 case OPENSSL_CHAIN_AUTH_CIPHER:
548 cipher_xform = xform->next;
550 case OPENSSL_CHAIN_COMBINED:
557 /* Default IV length = 0 */
560 /* cipher_xform must be check before auth_xform */
562 ret = openssl_set_session_cipher_parameters(
566 "Invalid/unsupported cipher parameters");
572 ret = openssl_set_session_auth_parameters(sess, auth_xform);
575 "Invalid/unsupported auth parameters");
581 ret = openssl_set_session_aead_parameters(sess, aead_xform);
584 "Invalid/unsupported AEAD parameters");
592 /** Reset private session parameters */
594 openssl_reset_session(struct openssl_session *sess)
596 EVP_CIPHER_CTX_free(sess->cipher.ctx);
598 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
599 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
601 switch (sess->auth.mode) {
602 case OPENSSL_AUTH_AS_AUTH:
603 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
605 case OPENSSL_AUTH_AS_HMAC:
606 EVP_PKEY_free(sess->auth.hmac.pkey);
607 HMAC_CTX_free(sess->auth.hmac.ctx);
614 /** Provide session for operation */
615 static struct openssl_session *
616 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
618 struct openssl_session *sess = NULL;
620 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
621 /* get existing session */
622 if (likely(op->sym->session != NULL))
623 sess = (struct openssl_session *)
624 get_session_private_data(
626 cryptodev_driver_id);
628 /* provide internal session */
630 void *_sess_private_data = NULL;
632 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
635 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
638 sess = (struct openssl_session *)_sess_private_data;
640 if (unlikely(openssl_set_session_parameters(sess,
641 op->sym->xform) != 0)) {
642 rte_mempool_put(qp->sess_mp, _sess);
643 rte_mempool_put(qp->sess_mp, _sess_private_data);
646 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
647 set_session_private_data(op->sym->session, cryptodev_driver_id,
652 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
658 *------------------------------------------------------------------------------
660 *------------------------------------------------------------------------------
663 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
664 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
671 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
673 offset -= rte_pktmbuf_data_len(m);
678 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
680 l = rte_pktmbuf_data_len(m) - offset;
682 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
688 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
694 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
695 src = rte_pktmbuf_mtod(m, uint8_t *);
696 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
697 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
707 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
708 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
715 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
717 offset -= rte_pktmbuf_data_len(m);
722 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
724 l = rte_pktmbuf_data_len(m) - offset;
726 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
732 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
738 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
739 src = rte_pktmbuf_mtod(m, uint8_t *);
740 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
741 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
750 /** Process standard openssl cipher encryption */
752 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
753 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx)
757 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
758 goto process_cipher_encrypt_err;
760 EVP_CIPHER_CTX_set_padding(ctx, 0);
762 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
764 goto process_cipher_encrypt_err;
766 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
767 goto process_cipher_encrypt_err;
771 process_cipher_encrypt_err:
772 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
776 /** Process standard openssl cipher encryption */
778 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
779 uint8_t *iv, int srclen,
783 uint8_t encrypted_iv[DES_BLOCK_SIZE];
786 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
787 iv, DES_BLOCK_SIZE) <= 0)
788 goto process_cipher_encrypt_err;
790 for (i = 0; i < srclen; i++)
791 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
795 process_cipher_encrypt_err:
796 OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
799 /** Process standard openssl cipher decryption */
801 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
802 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx)
806 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
807 goto process_cipher_decrypt_err;
809 EVP_CIPHER_CTX_set_padding(ctx, 0);
811 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
813 goto process_cipher_decrypt_err;
815 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
816 goto process_cipher_decrypt_err;
819 process_cipher_decrypt_err:
820 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
824 /** Process cipher des 3 ctr encryption, decryption algorithm */
826 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
827 int offset, uint8_t *iv, uint8_t *key, int srclen,
830 uint8_t ebuf[8], ctr[8];
836 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
838 offset -= rte_pktmbuf_data_len(m);
841 goto process_cipher_des3ctr_err;
843 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
844 l = rte_pktmbuf_data_len(m) - offset;
846 /* We use 3DES encryption also for decryption.
847 * IV is not important for 3DES ecb
849 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
850 goto process_cipher_des3ctr_err;
854 for (n = 0; n < srclen; n++) {
856 if (EVP_EncryptUpdate(ctx,
857 (unsigned char *)&ebuf, &unused,
858 (const unsigned char *)&ctr, 8) <= 0)
859 goto process_cipher_des3ctr_err;
862 dst[n] = *(src++) ^ ebuf[n % 8];
868 src = rte_pktmbuf_mtod(m, uint8_t *);
869 l = rte_pktmbuf_data_len(m);
876 process_cipher_des3ctr_err:
877 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
881 /** Process auth/encription aes-gcm algorithm */
883 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
884 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
885 uint8_t *key, uint8_t *dst, uint8_t *tag,
886 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
888 int len = 0, unused = 0;
889 uint8_t empty[] = {};
891 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
892 goto process_auth_encryption_gcm_err;
894 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
895 goto process_auth_encryption_gcm_err;
897 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
898 goto process_auth_encryption_gcm_err;
901 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
902 goto process_auth_encryption_gcm_err;
905 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
907 goto process_auth_encryption_gcm_err;
909 /* Workaround open ssl bug in version less then 1.0.1f */
910 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
911 goto process_auth_encryption_gcm_err;
913 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
914 goto process_auth_encryption_gcm_err;
916 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
917 goto process_auth_encryption_gcm_err;
921 process_auth_encryption_gcm_err:
922 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
927 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
928 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
929 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
930 const EVP_CIPHER *algo)
932 int len = 0, unused = 0;
933 uint8_t empty[] = {};
935 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
936 goto process_auth_decryption_gcm_err;
938 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
939 goto process_auth_decryption_gcm_err;
941 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
942 goto process_auth_decryption_gcm_err;
944 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
945 goto process_auth_decryption_gcm_err;
948 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
949 goto process_auth_decryption_gcm_err;
952 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
954 goto process_auth_decryption_gcm_err;
956 /* Workaround open ssl bug in version less then 1.0.1f */
957 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
958 goto process_auth_decryption_gcm_err;
960 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
961 goto process_auth_decryption_gcm_final_err;
965 process_auth_decryption_gcm_err:
966 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
969 process_auth_decryption_gcm_final_err:
973 /** Process standard openssl auth algorithms */
975 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
976 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
977 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
984 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
986 offset -= rte_pktmbuf_data_len(m);
989 goto process_auth_err;
991 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
992 goto process_auth_err;
994 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
996 l = rte_pktmbuf_data_len(m) - offset;
998 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
999 goto process_auth_err;
1000 goto process_auth_final;
1003 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1004 goto process_auth_err;
1008 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1009 src = rte_pktmbuf_mtod(m, uint8_t *);
1010 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1011 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1012 goto process_auth_err;
1017 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
1018 goto process_auth_err;
1022 OPENSSL_LOG_ERR("Process openssl auth failed");
1026 /** Process standard openssl auth algorithms with hmac */
1028 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1029 int srclen, HMAC_CTX *ctx)
1031 unsigned int dstlen;
1036 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1038 offset -= rte_pktmbuf_data_len(m);
1041 goto process_auth_err;
1043 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1045 l = rte_pktmbuf_data_len(m) - offset;
1047 if (HMAC_Update(ctx, (unsigned char *)src, srclen) != 1)
1048 goto process_auth_err;
1049 goto process_auth_final;
1052 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1053 goto process_auth_err;
1057 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1058 src = rte_pktmbuf_mtod(m, uint8_t *);
1059 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1060 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1061 goto process_auth_err;
1066 if (HMAC_Final(ctx, dst, &dstlen) != 1)
1067 goto process_auth_err;
1069 if (unlikely(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) != 1))
1070 goto process_auth_err;
1075 OPENSSL_LOG_ERR("Process openssl auth failed");
1079 /*----------------------------------------------------------------------------*/
1081 /** Process auth/cipher combined operation */
1083 process_openssl_combined_op
1084 (struct rte_crypto_op *op, struct openssl_session *sess,
1085 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1088 uint8_t *dst = NULL, *iv, *tag, *aad;
1089 int srclen, ivlen, aadlen, status = -1;
1093 * Segmented destination buffer is not supported for
1094 * encryption/decryption
1096 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1097 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1101 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1103 ivlen = sess->iv.length;
1104 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1106 offset = op->sym->auth.data.offset;
1107 aadlen = op->sym->auth.data.length;
1108 aad = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1109 op->sym->auth.data.offset);
1110 tag = op->sym->auth.digest.data;
1112 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1115 srclen = op->sym->aead.data.length;
1116 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1117 op->sym->aead.data.offset);
1118 offset = op->sym->aead.data.offset;
1119 aad = op->sym->aead.aad.data;
1120 aadlen = sess->auth.aad_length;
1121 tag = op->sym->aead.digest.data;
1123 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1127 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1128 status = process_openssl_auth_encryption_gcm(
1129 mbuf_src, offset, srclen,
1130 aad, aadlen, iv, ivlen, sess->cipher.key.data,
1131 dst, tag, sess->cipher.ctx,
1132 sess->cipher.evp_algo);
1134 status = process_openssl_auth_decryption_gcm(
1135 mbuf_src, offset, srclen,
1136 aad, aadlen, iv, ivlen, sess->cipher.key.data,
1137 dst, tag, sess->cipher.ctx,
1138 sess->cipher.evp_algo);
1141 if (status == (-EFAULT) &&
1142 sess->auth.operation ==
1143 RTE_CRYPTO_AUTH_OP_VERIFY)
1144 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1146 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1150 /** Process cipher operation */
1152 process_openssl_cipher_op
1153 (struct rte_crypto_op *op, struct openssl_session *sess,
1154 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1160 * Segmented destination buffer is not supported for
1161 * encryption/decryption
1163 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1164 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1168 srclen = op->sym->cipher.data.length;
1169 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1170 op->sym->cipher.data.offset);
1172 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1175 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1176 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1177 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1178 op->sym->cipher.data.offset, iv,
1179 srclen, sess->cipher.ctx);
1181 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1182 op->sym->cipher.data.offset, iv,
1183 srclen, sess->cipher.ctx);
1185 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1186 op->sym->cipher.data.offset, iv,
1187 sess->cipher.key.data, srclen,
1191 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1194 /** Process cipher operation */
1196 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1197 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1198 struct rte_mbuf *mbuf_dst)
1200 uint8_t *src, *dst, *iv;
1201 uint8_t block_size, last_block_len;
1202 int srclen, status = 0;
1204 srclen = op->sym->cipher.data.length;
1205 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1206 op->sym->cipher.data.offset);
1207 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1208 op->sym->cipher.data.offset);
1210 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1213 block_size = DES_BLOCK_SIZE;
1215 last_block_len = srclen % block_size;
1216 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1217 /* Encrypt only with ECB mode XOR IV */
1218 if (srclen < block_size) {
1219 status = process_openssl_cipher_bpi_encrypt(src, dst,
1221 sess->cipher.bpi_ctx);
1223 srclen -= last_block_len;
1224 /* Encrypt with the block aligned stream with CBC mode */
1225 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1226 op->sym->cipher.data.offset, iv,
1227 srclen, sess->cipher.ctx);
1228 if (last_block_len) {
1229 /* Point at last block */
1232 * IV is the last encrypted block from
1233 * the previous operation
1235 iv = dst - block_size;
1237 srclen = last_block_len;
1238 /* Encrypt the last frame with ECB mode */
1239 status |= process_openssl_cipher_bpi_encrypt(src,
1241 srclen, sess->cipher.bpi_ctx);
1245 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1246 if (srclen < block_size) {
1247 status = process_openssl_cipher_bpi_encrypt(src, dst,
1250 sess->cipher.bpi_ctx);
1252 if (last_block_len) {
1253 /* Point at last block */
1254 dst += srclen - last_block_len;
1255 src += srclen - last_block_len;
1257 * IV is the last full block
1259 iv = src - block_size;
1261 * Decrypt the last frame with ECB mode
1262 * (encrypt, as it is the same operation)
1264 status = process_openssl_cipher_bpi_encrypt(src,
1266 last_block_len, sess->cipher.bpi_ctx);
1267 /* Prepare parameters for CBC mode op */
1268 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1270 dst += last_block_len - srclen;
1271 srclen -= last_block_len;
1274 /* Decrypt with CBC mode */
1275 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1276 op->sym->cipher.data.offset, iv,
1277 srclen, sess->cipher.ctx);
1282 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1285 /** Process auth operation */
1287 process_openssl_auth_op
1288 (struct rte_crypto_op *op, struct openssl_session *sess,
1289 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1294 srclen = op->sym->auth.data.length;
1296 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1297 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1298 sess->auth.digest_length);
1300 dst = op->sym->auth.digest.data;
1302 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1303 op->sym->auth.data.offset +
1304 op->sym->auth.data.length);
1307 switch (sess->auth.mode) {
1308 case OPENSSL_AUTH_AS_AUTH:
1309 status = process_openssl_auth(mbuf_src, dst,
1310 op->sym->auth.data.offset, NULL, NULL, srclen,
1311 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1313 case OPENSSL_AUTH_AS_HMAC:
1314 status = process_openssl_auth_hmac(mbuf_src, dst,
1315 op->sym->auth.data.offset, srclen,
1316 sess->auth.hmac.ctx);
1323 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1324 if (memcmp(dst, op->sym->auth.digest.data,
1325 sess->auth.digest_length) != 0) {
1326 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1328 /* Trim area used for digest from mbuf. */
1329 rte_pktmbuf_trim(mbuf_src, sess->auth.digest_length);
1333 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1336 /** Process crypto operation for mbuf */
1338 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1339 struct openssl_session *sess)
1341 struct rte_mbuf *msrc, *mdst;
1344 msrc = op->sym->m_src;
1345 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1347 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1349 switch (sess->chain_order) {
1350 case OPENSSL_CHAIN_ONLY_CIPHER:
1351 process_openssl_cipher_op(op, sess, msrc, mdst);
1353 case OPENSSL_CHAIN_ONLY_AUTH:
1354 process_openssl_auth_op(op, sess, msrc, mdst);
1356 case OPENSSL_CHAIN_CIPHER_AUTH:
1357 process_openssl_cipher_op(op, sess, msrc, mdst);
1358 process_openssl_auth_op(op, sess, mdst, mdst);
1360 case OPENSSL_CHAIN_AUTH_CIPHER:
1361 process_openssl_auth_op(op, sess, msrc, mdst);
1362 process_openssl_cipher_op(op, sess, msrc, mdst);
1364 case OPENSSL_CHAIN_COMBINED:
1365 process_openssl_combined_op(op, sess, msrc, mdst);
1367 case OPENSSL_CHAIN_CIPHER_BPI:
1368 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1371 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1375 /* Free session if a session-less crypto op */
1376 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1377 openssl_reset_session(sess);
1378 memset(sess, 0, sizeof(struct openssl_session));
1379 memset(op->sym->session, 0,
1380 rte_cryptodev_get_header_session_size());
1381 rte_mempool_put(qp->sess_mp, sess);
1382 rte_mempool_put(qp->sess_mp, op->sym->session);
1383 op->sym->session = NULL;
1386 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1387 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1389 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1390 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1398 *------------------------------------------------------------------------------
1400 *------------------------------------------------------------------------------
1403 /** Enqueue burst */
1405 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1408 struct openssl_session *sess;
1409 struct openssl_qp *qp = queue_pair;
1412 for (i = 0; i < nb_ops; i++) {
1413 sess = get_session(qp, ops[i]);
1414 if (unlikely(sess == NULL))
1417 retval = process_op(qp, ops[i], sess);
1418 if (unlikely(retval < 0))
1422 qp->stats.enqueued_count += i;
1426 qp->stats.enqueue_err_count++;
1430 /** Dequeue burst */
1432 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1435 struct openssl_qp *qp = queue_pair;
1437 unsigned int nb_dequeued = 0;
1439 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1440 (void **)ops, nb_ops, NULL);
1441 qp->stats.dequeued_count += nb_dequeued;
1446 /** Create OPENSSL crypto device */
1448 cryptodev_openssl_create(const char *name,
1449 struct rte_vdev_device *vdev,
1450 struct rte_crypto_vdev_init_params *init_params)
1452 struct rte_cryptodev *dev;
1453 struct openssl_private *internals;
1455 if (init_params->name[0] == '\0')
1456 snprintf(init_params->name, sizeof(init_params->name),
1459 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1460 sizeof(struct openssl_private),
1461 init_params->socket_id,
1464 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1468 dev->driver_id = cryptodev_driver_id;
1469 dev->dev_ops = rte_openssl_pmd_ops;
1471 /* register rx/tx burst functions for data path */
1472 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1473 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1475 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1476 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1477 RTE_CRYPTODEV_FF_CPU_AESNI |
1478 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1480 /* Set vector instructions mode supported */
1481 internals = dev->data->dev_private;
1483 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1484 internals->max_nb_sessions = init_params->max_nb_sessions;
1489 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1492 cryptodev_openssl_remove(vdev);
1496 /** Initialise OPENSSL crypto device */
1498 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1500 struct rte_crypto_vdev_init_params init_params = {
1501 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1502 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1507 const char *input_args;
1509 name = rte_vdev_device_name(vdev);
1512 input_args = rte_vdev_device_args(vdev);
1514 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1516 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1517 init_params.socket_id);
1518 if (init_params.name[0] != '\0')
1519 RTE_LOG(INFO, PMD, " User defined name = %s\n",
1521 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1522 init_params.max_nb_queue_pairs);
1523 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1524 init_params.max_nb_sessions);
1526 return cryptodev_openssl_create(name, vdev, &init_params);
1529 /** Uninitialise OPENSSL crypto device */
1531 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1535 name = rte_vdev_device_name(vdev);
1540 "Closing OPENSSL crypto device %s on numa socket %u\n",
1541 name, rte_socket_id());
1546 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1547 .probe = cryptodev_openssl_probe,
1548 .remove = cryptodev_openssl_remove
1551 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1552 cryptodev_openssl_pmd_drv);
1553 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1554 "max_nb_queue_pairs=<int> "
1555 "max_nb_sessions=<int> "
1557 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_openssl_pmd_drv, cryptodev_driver_id);