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 uint8_t cryptodev_driver_id;
50 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
52 /*----------------------------------------------------------------------------*/
55 * Increment counter by 1
56 * Counter is 64 bit array, big-endian
61 uint64_t *ctr64 = (uint64_t *)ctr;
63 *ctr64 = __builtin_bswap64(*ctr64);
65 *ctr64 = __builtin_bswap64(*ctr64);
69 *------------------------------------------------------------------------------
71 *------------------------------------------------------------------------------
74 /** Get xform chain order */
75 static enum openssl_chain_order
76 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
78 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
81 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
82 if (xform->next == NULL)
83 res = OPENSSL_CHAIN_ONLY_AUTH;
84 else if (xform->next->type ==
85 RTE_CRYPTO_SYM_XFORM_CIPHER)
86 res = OPENSSL_CHAIN_AUTH_CIPHER;
88 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
89 if (xform->next == NULL)
90 res = OPENSSL_CHAIN_ONLY_CIPHER;
91 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
92 res = OPENSSL_CHAIN_CIPHER_AUTH;
94 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
95 res = OPENSSL_CHAIN_COMBINED;
101 /** Get session cipher key from input cipher key */
103 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
105 memcpy(session_key, input_key, keylen);
108 /** Get key ede 24 bytes standard from input key */
110 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
114 /* Initialize keys - 24 bytes: [key1-key2-key3] */
117 memcpy(key_ede, key, 24);
121 memcpy(key_ede, key, 16);
122 memcpy(key_ede + 16, key, 8);
125 /* K1 = K2 = K3 (DES compatibility) */
126 memcpy(key_ede, key, 8);
127 memcpy(key_ede + 8, key, 8);
128 memcpy(key_ede + 16, key, 8);
131 OPENSSL_LOG_ERR("Unsupported key size");
138 /** Get adequate openssl function for input cipher algorithm */
140 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
141 const EVP_CIPHER **algo)
147 case RTE_CRYPTO_CIPHER_3DES_CBC:
150 *algo = EVP_des_ede_cbc();
153 *algo = EVP_des_ede3_cbc();
159 case RTE_CRYPTO_CIPHER_3DES_CTR:
161 case RTE_CRYPTO_CIPHER_AES_CBC:
164 *algo = EVP_aes_128_cbc();
167 *algo = EVP_aes_192_cbc();
170 *algo = EVP_aes_256_cbc();
176 case RTE_CRYPTO_CIPHER_AES_CTR:
179 *algo = EVP_aes_128_ctr();
182 *algo = EVP_aes_192_ctr();
185 *algo = EVP_aes_256_ctr();
202 /** Get adequate openssl function for input auth algorithm */
204 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
211 case RTE_CRYPTO_AUTH_MD5:
212 case RTE_CRYPTO_AUTH_MD5_HMAC:
215 case RTE_CRYPTO_AUTH_SHA1:
216 case RTE_CRYPTO_AUTH_SHA1_HMAC:
219 case RTE_CRYPTO_AUTH_SHA224:
220 case RTE_CRYPTO_AUTH_SHA224_HMAC:
221 *algo = EVP_sha224();
223 case RTE_CRYPTO_AUTH_SHA256:
224 case RTE_CRYPTO_AUTH_SHA256_HMAC:
225 *algo = EVP_sha256();
227 case RTE_CRYPTO_AUTH_SHA384:
228 case RTE_CRYPTO_AUTH_SHA384_HMAC:
229 *algo = EVP_sha384();
231 case RTE_CRYPTO_AUTH_SHA512:
232 case RTE_CRYPTO_AUTH_SHA512_HMAC:
233 *algo = EVP_sha512();
246 /** Get adequate openssl function for input cipher algorithm */
248 get_aead_algo(enum rte_crypto_aead_algorithm sess_algo, size_t keylen,
249 const EVP_CIPHER **algo)
255 case RTE_CRYPTO_AEAD_AES_GCM:
258 *algo = EVP_aes_128_gcm();
261 *algo = EVP_aes_192_gcm();
264 *algo = EVP_aes_256_gcm();
281 /** Set session cipher parameters */
283 openssl_set_session_cipher_parameters(struct openssl_session *sess,
284 const struct rte_crypto_sym_xform *xform)
286 /* Select cipher direction */
287 sess->cipher.direction = xform->cipher.op;
288 /* Select cipher key */
289 sess->cipher.key.length = xform->cipher.key.length;
291 /* Set IV parameters */
292 sess->iv.offset = xform->cipher.iv.offset;
293 sess->iv.length = xform->cipher.iv.length;
295 /* Select cipher algo */
296 switch (xform->cipher.algo) {
297 case RTE_CRYPTO_CIPHER_3DES_CBC:
298 case RTE_CRYPTO_CIPHER_AES_CBC:
299 case RTE_CRYPTO_CIPHER_AES_CTR:
300 sess->cipher.mode = OPENSSL_CIPHER_LIB;
301 sess->cipher.algo = xform->cipher.algo;
302 sess->cipher.ctx = EVP_CIPHER_CTX_new();
304 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
305 &sess->cipher.evp_algo) != 0)
308 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
309 sess->cipher.key.data);
313 case RTE_CRYPTO_CIPHER_3DES_CTR:
314 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
315 sess->cipher.ctx = EVP_CIPHER_CTX_new();
317 if (get_cipher_key_ede(xform->cipher.key.data,
318 sess->cipher.key.length,
319 sess->cipher.key.data) != 0)
322 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
323 sess->cipher.algo = xform->cipher.algo;
324 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
325 sess->cipher.ctx = EVP_CIPHER_CTX_new();
326 sess->cipher.evp_algo = EVP_des_cbc();
328 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
329 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
330 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
331 NULL, xform->cipher.key.data, 0) != 1)
334 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
335 sess->cipher.key.data);
338 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
345 /* Set session auth parameters */
347 openssl_set_session_auth_parameters(struct openssl_session *sess,
348 const struct rte_crypto_sym_xform *xform)
350 /* Select auth generate/verify */
351 sess->auth.operation = xform->auth.op;
352 sess->auth.algo = xform->auth.algo;
354 /* Select auth algo */
355 switch (xform->auth.algo) {
356 case RTE_CRYPTO_AUTH_AES_GMAC:
357 sess->chain_order = OPENSSL_CHAIN_COMBINED;
359 /* Set IV parameters */
360 sess->iv.offset = xform->auth.iv.offset;
361 sess->iv.length = xform->auth.iv.length;
364 * OpenSSL requires GMAC to be a GCM operation
365 * with no cipher data length
367 sess->cipher.mode = OPENSSL_CIPHER_LIB;
368 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE)
369 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
371 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_DECRYPT;
373 sess->cipher.key.length = xform->auth.key.length;
374 sess->cipher.ctx = EVP_CIPHER_CTX_new();
376 if (get_aead_algo(RTE_CRYPTO_AEAD_AES_GCM,
377 sess->cipher.key.length,
378 &sess->cipher.evp_algo) != 0)
381 get_cipher_key(xform->auth.key.data, xform->auth.key.length,
382 sess->cipher.key.data);
386 case RTE_CRYPTO_AUTH_MD5:
387 case RTE_CRYPTO_AUTH_SHA1:
388 case RTE_CRYPTO_AUTH_SHA224:
389 case RTE_CRYPTO_AUTH_SHA256:
390 case RTE_CRYPTO_AUTH_SHA384:
391 case RTE_CRYPTO_AUTH_SHA512:
392 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
393 if (get_auth_algo(xform->auth.algo,
394 &sess->auth.auth.evp_algo) != 0)
396 sess->auth.auth.ctx = EVP_MD_CTX_create();
399 case RTE_CRYPTO_AUTH_MD5_HMAC:
400 case RTE_CRYPTO_AUTH_SHA1_HMAC:
401 case RTE_CRYPTO_AUTH_SHA224_HMAC:
402 case RTE_CRYPTO_AUTH_SHA256_HMAC:
403 case RTE_CRYPTO_AUTH_SHA384_HMAC:
404 case RTE_CRYPTO_AUTH_SHA512_HMAC:
405 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
406 sess->auth.hmac.ctx = EVP_MD_CTX_create();
407 if (get_auth_algo(xform->auth.algo,
408 &sess->auth.hmac.evp_algo) != 0)
410 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
411 xform->auth.key.data, xform->auth.key.length);
418 sess->auth.digest_length = xform->auth.digest_length;
423 /* Set session AEAD parameters */
425 openssl_set_session_aead_parameters(struct openssl_session *sess,
426 const struct rte_crypto_sym_xform *xform)
428 /* Select cipher direction */
429 sess->cipher.direction = xform->cipher.op;
430 /* Select cipher key */
431 sess->cipher.key.length = xform->aead.key.length;
433 /* Set IV parameters */
434 sess->iv.offset = xform->aead.iv.offset;
435 sess->iv.length = xform->aead.iv.length;
437 /* Select auth generate/verify */
438 sess->auth.operation = xform->auth.op;
439 sess->auth.algo = xform->auth.algo;
441 /* Select auth algo */
442 switch (xform->aead.algo) {
443 case RTE_CRYPTO_AEAD_AES_GCM:
444 sess->cipher.mode = OPENSSL_CIPHER_LIB;
445 sess->aead_algo = xform->aead.algo;
446 sess->cipher.ctx = EVP_CIPHER_CTX_new();
448 if (get_aead_algo(sess->aead_algo, sess->cipher.key.length,
449 &sess->cipher.evp_algo) != 0)
452 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
453 sess->cipher.key.data);
455 sess->chain_order = OPENSSL_CHAIN_COMBINED;
461 sess->auth.aad_length = xform->aead.aad_length;
462 sess->auth.digest_length = xform->aead.digest_length;
467 /** Parse crypto xform chain and set private session parameters */
469 openssl_set_session_parameters(struct openssl_session *sess,
470 const struct rte_crypto_sym_xform *xform)
472 const struct rte_crypto_sym_xform *cipher_xform = NULL;
473 const struct rte_crypto_sym_xform *auth_xform = NULL;
474 const struct rte_crypto_sym_xform *aead_xform = NULL;
477 sess->chain_order = openssl_get_chain_order(xform);
478 switch (sess->chain_order) {
479 case OPENSSL_CHAIN_ONLY_CIPHER:
480 cipher_xform = xform;
482 case OPENSSL_CHAIN_ONLY_AUTH:
485 case OPENSSL_CHAIN_CIPHER_AUTH:
486 cipher_xform = xform;
487 auth_xform = xform->next;
489 case OPENSSL_CHAIN_AUTH_CIPHER:
491 cipher_xform = xform->next;
493 case OPENSSL_CHAIN_COMBINED:
500 /* Default IV length = 0 */
503 /* cipher_xform must be check before auth_xform */
505 ret = openssl_set_session_cipher_parameters(
509 "Invalid/unsupported cipher parameters");
515 ret = openssl_set_session_auth_parameters(sess, auth_xform);
518 "Invalid/unsupported auth parameters");
524 ret = openssl_set_session_aead_parameters(sess, aead_xform);
527 "Invalid/unsupported AEAD parameters");
535 /** Reset private session parameters */
537 openssl_reset_session(struct openssl_session *sess)
539 EVP_CIPHER_CTX_free(sess->cipher.ctx);
541 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
542 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
544 switch (sess->auth.mode) {
545 case OPENSSL_AUTH_AS_AUTH:
546 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
548 case OPENSSL_AUTH_AS_HMAC:
549 EVP_PKEY_free(sess->auth.hmac.pkey);
550 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
557 /** Provide session for operation */
558 static struct openssl_session *
559 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
561 struct openssl_session *sess = NULL;
563 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
564 /* get existing session */
565 if (likely(op->sym->session != NULL))
566 sess = (struct openssl_session *)
567 get_session_private_data(
569 cryptodev_driver_id);
571 /* provide internal session */
573 void *_sess_private_data = NULL;
575 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
578 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
581 sess = (struct openssl_session *)_sess_private_data;
583 if (unlikely(openssl_set_session_parameters(sess,
584 op->sym->xform) != 0)) {
585 rte_mempool_put(qp->sess_mp, _sess);
586 rte_mempool_put(qp->sess_mp, _sess_private_data);
589 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
590 set_session_private_data(op->sym->session, cryptodev_driver_id,
595 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
601 *------------------------------------------------------------------------------
603 *------------------------------------------------------------------------------
606 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
607 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
614 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
616 offset -= rte_pktmbuf_data_len(m);
621 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
623 l = rte_pktmbuf_data_len(m) - offset;
625 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
631 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
637 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
638 src = rte_pktmbuf_mtod(m, uint8_t *);
639 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
640 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
650 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
651 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
658 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
660 offset -= rte_pktmbuf_data_len(m);
665 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
667 l = rte_pktmbuf_data_len(m) - offset;
669 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
675 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
681 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
682 src = rte_pktmbuf_mtod(m, uint8_t *);
683 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
684 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
693 /** Process standard openssl cipher encryption */
695 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
696 int offset, uint8_t *iv, uint8_t *key, int srclen,
697 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
701 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
702 goto process_cipher_encrypt_err;
704 EVP_CIPHER_CTX_set_padding(ctx, 0);
706 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
708 goto process_cipher_encrypt_err;
710 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
711 goto process_cipher_encrypt_err;
715 process_cipher_encrypt_err:
716 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
720 /** Process standard openssl cipher encryption */
722 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
723 uint8_t *iv, int srclen,
727 uint8_t encrypted_iv[DES_BLOCK_SIZE];
730 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
731 iv, DES_BLOCK_SIZE) <= 0)
732 goto process_cipher_encrypt_err;
734 for (i = 0; i < srclen; i++)
735 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
739 process_cipher_encrypt_err:
740 OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
743 /** Process standard openssl cipher decryption */
745 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
746 int offset, uint8_t *iv, uint8_t *key, int srclen,
747 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
751 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
752 goto process_cipher_decrypt_err;
754 EVP_CIPHER_CTX_set_padding(ctx, 0);
756 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
758 goto process_cipher_decrypt_err;
760 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
761 goto process_cipher_decrypt_err;
764 process_cipher_decrypt_err:
765 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
769 /** Process cipher des 3 ctr encryption, decryption algorithm */
771 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
772 int offset, uint8_t *iv, uint8_t *key, int srclen,
775 uint8_t ebuf[8], ctr[8];
781 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
783 offset -= rte_pktmbuf_data_len(m);
786 goto process_cipher_des3ctr_err;
788 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
789 l = rte_pktmbuf_data_len(m) - offset;
791 /* We use 3DES encryption also for decryption.
792 * IV is not important for 3DES ecb
794 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
795 goto process_cipher_des3ctr_err;
799 for (n = 0; n < srclen; n++) {
801 if (EVP_EncryptUpdate(ctx,
802 (unsigned char *)&ebuf, &unused,
803 (const unsigned char *)&ctr, 8) <= 0)
804 goto process_cipher_des3ctr_err;
807 dst[n] = *(src++) ^ ebuf[n % 8];
813 src = rte_pktmbuf_mtod(m, uint8_t *);
814 l = rte_pktmbuf_data_len(m);
821 process_cipher_des3ctr_err:
822 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
826 /** Process auth/encription aes-gcm algorithm */
828 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
829 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
830 uint8_t *key, uint8_t *dst, uint8_t *tag,
831 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
833 int len = 0, unused = 0;
834 uint8_t empty[] = {};
836 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
837 goto process_auth_encryption_gcm_err;
839 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
840 goto process_auth_encryption_gcm_err;
842 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
843 goto process_auth_encryption_gcm_err;
846 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
847 goto process_auth_encryption_gcm_err;
850 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
852 goto process_auth_encryption_gcm_err;
854 /* Workaround open ssl bug in version less then 1.0.1f */
855 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
856 goto process_auth_encryption_gcm_err;
858 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
859 goto process_auth_encryption_gcm_err;
861 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
862 goto process_auth_encryption_gcm_err;
866 process_auth_encryption_gcm_err:
867 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
872 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
873 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
874 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
875 const EVP_CIPHER *algo)
877 int len = 0, unused = 0;
878 uint8_t empty[] = {};
880 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
881 goto process_auth_decryption_gcm_err;
883 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
884 goto process_auth_decryption_gcm_err;
886 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
887 goto process_auth_decryption_gcm_err;
889 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
890 goto process_auth_decryption_gcm_err;
893 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
894 goto process_auth_decryption_gcm_err;
897 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
899 goto process_auth_decryption_gcm_err;
901 /* Workaround open ssl bug in version less then 1.0.1f */
902 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
903 goto process_auth_decryption_gcm_err;
905 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
906 goto process_auth_decryption_gcm_final_err;
910 process_auth_decryption_gcm_err:
911 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
914 process_auth_decryption_gcm_final_err:
918 /** Process standard openssl auth algorithms */
920 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
921 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
922 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
929 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
931 offset -= rte_pktmbuf_data_len(m);
934 goto process_auth_err;
936 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
937 goto process_auth_err;
939 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
941 l = rte_pktmbuf_data_len(m) - offset;
943 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
944 goto process_auth_err;
945 goto process_auth_final;
948 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
949 goto process_auth_err;
953 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
954 src = rte_pktmbuf_mtod(m, uint8_t *);
955 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
956 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
957 goto process_auth_err;
962 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
963 goto process_auth_err;
967 OPENSSL_LOG_ERR("Process openssl auth failed");
971 /** Process standard openssl auth algorithms with hmac */
973 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
974 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
975 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
982 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
984 offset -= rte_pktmbuf_data_len(m);
987 goto process_auth_err;
989 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
990 goto process_auth_err;
992 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
994 l = rte_pktmbuf_data_len(m) - offset;
996 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
997 goto process_auth_err;
998 goto process_auth_final;
1001 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
1002 goto process_auth_err;
1006 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1007 src = rte_pktmbuf_mtod(m, uint8_t *);
1008 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1009 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
1010 goto process_auth_err;
1015 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
1016 goto process_auth_err;
1021 OPENSSL_LOG_ERR("Process openssl auth failed");
1025 /*----------------------------------------------------------------------------*/
1027 /** Process auth/cipher combined operation */
1029 process_openssl_combined_op
1030 (struct rte_crypto_op *op, struct openssl_session *sess,
1031 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1034 uint8_t *dst = NULL, *iv, *tag, *aad;
1035 int srclen, ivlen, aadlen, status = -1;
1039 * Segmented destination buffer is not supported for
1040 * encryption/decryption
1042 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1043 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1047 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1049 ivlen = sess->iv.length;
1050 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1052 offset = op->sym->auth.data.offset;
1053 aadlen = op->sym->auth.data.length;
1054 aad = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1055 op->sym->auth.data.offset);
1056 tag = op->sym->auth.digest.data;
1058 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1061 srclen = op->sym->aead.data.length;
1062 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1063 op->sym->aead.data.offset);
1064 offset = op->sym->aead.data.offset;
1065 aad = op->sym->aead.aad.data;
1066 aadlen = sess->auth.aad_length;
1067 tag = op->sym->aead.digest.data;
1069 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1073 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1074 status = process_openssl_auth_encryption_gcm(
1075 mbuf_src, offset, srclen,
1076 aad, aadlen, iv, ivlen, sess->cipher.key.data,
1077 dst, tag, sess->cipher.ctx,
1078 sess->cipher.evp_algo);
1080 status = process_openssl_auth_decryption_gcm(
1081 mbuf_src, offset, srclen,
1082 aad, aadlen, iv, ivlen, sess->cipher.key.data,
1083 dst, tag, sess->cipher.ctx,
1084 sess->cipher.evp_algo);
1087 if (status == (-EFAULT) &&
1088 sess->auth.operation ==
1089 RTE_CRYPTO_AUTH_OP_VERIFY)
1090 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1092 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1096 /** Process cipher operation */
1098 process_openssl_cipher_op
1099 (struct rte_crypto_op *op, struct openssl_session *sess,
1100 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1106 * Segmented destination buffer is not supported for
1107 * encryption/decryption
1109 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1110 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1114 srclen = op->sym->cipher.data.length;
1115 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1116 op->sym->cipher.data.offset);
1118 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1121 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1122 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1123 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1124 op->sym->cipher.data.offset, iv,
1125 sess->cipher.key.data, srclen,
1127 sess->cipher.evp_algo);
1129 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1130 op->sym->cipher.data.offset, iv,
1131 sess->cipher.key.data, srclen,
1133 sess->cipher.evp_algo);
1135 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1136 op->sym->cipher.data.offset, iv,
1137 sess->cipher.key.data, srclen,
1141 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1144 /** Process cipher operation */
1146 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1147 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1148 struct rte_mbuf *mbuf_dst)
1150 uint8_t *src, *dst, *iv;
1151 uint8_t block_size, last_block_len;
1152 int srclen, status = 0;
1154 srclen = op->sym->cipher.data.length;
1155 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1156 op->sym->cipher.data.offset);
1157 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1158 op->sym->cipher.data.offset);
1160 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1163 block_size = DES_BLOCK_SIZE;
1165 last_block_len = srclen % block_size;
1166 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1167 /* Encrypt only with ECB mode XOR IV */
1168 if (srclen < block_size) {
1169 status = process_openssl_cipher_bpi_encrypt(src, dst,
1171 sess->cipher.bpi_ctx);
1173 srclen -= last_block_len;
1174 /* Encrypt with the block aligned stream with CBC mode */
1175 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1176 op->sym->cipher.data.offset, iv,
1177 sess->cipher.key.data, srclen,
1178 sess->cipher.ctx, sess->cipher.evp_algo);
1179 if (last_block_len) {
1180 /* Point at last block */
1183 * IV is the last encrypted block from
1184 * the previous operation
1186 iv = dst - block_size;
1188 srclen = last_block_len;
1189 /* Encrypt the last frame with ECB mode */
1190 status |= process_openssl_cipher_bpi_encrypt(src,
1192 srclen, sess->cipher.bpi_ctx);
1196 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1197 if (srclen < block_size) {
1198 status = process_openssl_cipher_bpi_encrypt(src, dst,
1201 sess->cipher.bpi_ctx);
1203 if (last_block_len) {
1204 /* Point at last block */
1205 dst += srclen - last_block_len;
1206 src += srclen - last_block_len;
1208 * IV is the last full block
1210 iv = src - block_size;
1212 * Decrypt the last frame with ECB mode
1213 * (encrypt, as it is the same operation)
1215 status = process_openssl_cipher_bpi_encrypt(src,
1217 last_block_len, sess->cipher.bpi_ctx);
1218 /* Prepare parameters for CBC mode op */
1219 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1221 dst += last_block_len - srclen;
1222 srclen -= last_block_len;
1225 /* Decrypt with CBC mode */
1226 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1227 op->sym->cipher.data.offset, iv,
1228 sess->cipher.key.data, srclen,
1230 sess->cipher.evp_algo);
1235 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1238 /** Process auth operation */
1240 process_openssl_auth_op
1241 (struct rte_crypto_op *op, struct openssl_session *sess,
1242 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1247 srclen = op->sym->auth.data.length;
1249 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1250 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1251 sess->auth.digest_length);
1253 dst = op->sym->auth.digest.data;
1255 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1256 op->sym->auth.data.offset +
1257 op->sym->auth.data.length);
1260 switch (sess->auth.mode) {
1261 case OPENSSL_AUTH_AS_AUTH:
1262 status = process_openssl_auth(mbuf_src, dst,
1263 op->sym->auth.data.offset, NULL, NULL, srclen,
1264 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1266 case OPENSSL_AUTH_AS_HMAC:
1267 status = process_openssl_auth_hmac(mbuf_src, dst,
1268 op->sym->auth.data.offset, NULL,
1269 sess->auth.hmac.pkey, srclen,
1270 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1277 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1278 if (memcmp(dst, op->sym->auth.digest.data,
1279 sess->auth.digest_length) != 0) {
1280 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1282 /* Trim area used for digest from mbuf. */
1283 rte_pktmbuf_trim(mbuf_src, sess->auth.digest_length);
1287 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1290 /** Process crypto operation for mbuf */
1292 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1293 struct openssl_session *sess)
1295 struct rte_mbuf *msrc, *mdst;
1298 msrc = op->sym->m_src;
1299 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1301 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1303 switch (sess->chain_order) {
1304 case OPENSSL_CHAIN_ONLY_CIPHER:
1305 process_openssl_cipher_op(op, sess, msrc, mdst);
1307 case OPENSSL_CHAIN_ONLY_AUTH:
1308 process_openssl_auth_op(op, sess, msrc, mdst);
1310 case OPENSSL_CHAIN_CIPHER_AUTH:
1311 process_openssl_cipher_op(op, sess, msrc, mdst);
1312 process_openssl_auth_op(op, sess, mdst, mdst);
1314 case OPENSSL_CHAIN_AUTH_CIPHER:
1315 process_openssl_auth_op(op, sess, msrc, mdst);
1316 process_openssl_cipher_op(op, sess, msrc, mdst);
1318 case OPENSSL_CHAIN_COMBINED:
1319 process_openssl_combined_op(op, sess, msrc, mdst);
1321 case OPENSSL_CHAIN_CIPHER_BPI:
1322 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1325 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1329 /* Free session if a session-less crypto op */
1330 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1331 openssl_reset_session(sess);
1332 memset(sess, 0, sizeof(struct openssl_session));
1333 memset(op->sym->session, 0,
1334 rte_cryptodev_get_header_session_size());
1335 rte_mempool_put(qp->sess_mp, sess);
1336 rte_mempool_put(qp->sess_mp, op->sym->session);
1337 op->sym->session = NULL;
1340 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1341 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1343 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1344 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1352 *------------------------------------------------------------------------------
1354 *------------------------------------------------------------------------------
1357 /** Enqueue burst */
1359 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1362 struct openssl_session *sess;
1363 struct openssl_qp *qp = queue_pair;
1366 for (i = 0; i < nb_ops; i++) {
1367 sess = get_session(qp, ops[i]);
1368 if (unlikely(sess == NULL))
1371 retval = process_op(qp, ops[i], sess);
1372 if (unlikely(retval < 0))
1376 qp->stats.enqueued_count += i;
1380 qp->stats.enqueue_err_count++;
1384 /** Dequeue burst */
1386 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1389 struct openssl_qp *qp = queue_pair;
1391 unsigned int nb_dequeued = 0;
1393 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1394 (void **)ops, nb_ops, NULL);
1395 qp->stats.dequeued_count += nb_dequeued;
1400 /** Create OPENSSL crypto device */
1402 cryptodev_openssl_create(const char *name,
1403 struct rte_vdev_device *vdev,
1404 struct rte_crypto_vdev_init_params *init_params)
1406 struct rte_cryptodev *dev;
1407 struct openssl_private *internals;
1409 if (init_params->name[0] == '\0')
1410 snprintf(init_params->name, sizeof(init_params->name),
1413 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1414 sizeof(struct openssl_private),
1415 init_params->socket_id,
1418 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1422 dev->driver_id = cryptodev_driver_id;
1423 dev->dev_ops = rte_openssl_pmd_ops;
1425 /* register rx/tx burst functions for data path */
1426 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1427 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1429 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1430 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1431 RTE_CRYPTODEV_FF_CPU_AESNI |
1432 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1434 /* Set vector instructions mode supported */
1435 internals = dev->data->dev_private;
1437 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1438 internals->max_nb_sessions = init_params->max_nb_sessions;
1443 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1446 cryptodev_openssl_remove(vdev);
1450 /** Initialise OPENSSL crypto device */
1452 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1454 struct rte_crypto_vdev_init_params init_params = {
1455 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1456 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1461 const char *input_args;
1463 name = rte_vdev_device_name(vdev);
1466 input_args = rte_vdev_device_args(vdev);
1468 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1470 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1471 init_params.socket_id);
1472 if (init_params.name[0] != '\0')
1473 RTE_LOG(INFO, PMD, " User defined name = %s\n",
1475 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1476 init_params.max_nb_queue_pairs);
1477 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1478 init_params.max_nb_sessions);
1480 return cryptodev_openssl_create(name, vdev, &init_params);
1483 /** Uninitialise OPENSSL crypto device */
1485 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1489 name = rte_vdev_device_name(vdev);
1494 "Closing OPENSSL crypto device %s on numa socket %u\n",
1495 name, rte_socket_id());
1500 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1501 .probe = cryptodev_openssl_probe,
1502 .remove = cryptodev_openssl_remove
1505 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1506 cryptodev_openssl_pmd_drv);
1507 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1508 "max_nb_queue_pairs=<int> "
1509 "max_nb_sessions=<int> "
1511 RTE_PMD_REGISTER_CRYPTO_DRIVER(cryptodev_openssl_pmd_drv, cryptodev_driver_id);