4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
37 #include <rte_cryptodev_vdev.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
42 #include <openssl/evp.h>
44 #include "rte_openssl_pmd_private.h"
46 #define DES_BLOCK_SIZE 8
48 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
50 /*----------------------------------------------------------------------------*/
53 * Increment counter by 1
54 * Counter is 64 bit array, big-endian
59 uint64_t *ctr64 = (uint64_t *)ctr;
61 *ctr64 = __builtin_bswap64(*ctr64);
63 *ctr64 = __builtin_bswap64(*ctr64);
67 *------------------------------------------------------------------------------
69 *------------------------------------------------------------------------------
72 /** Get xform chain order */
73 static enum openssl_chain_order
74 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
76 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
79 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
80 if (xform->next == NULL)
81 res = OPENSSL_CHAIN_ONLY_AUTH;
82 else if (xform->next->type ==
83 RTE_CRYPTO_SYM_XFORM_CIPHER)
84 res = OPENSSL_CHAIN_AUTH_CIPHER;
86 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
87 if (xform->next == NULL)
88 res = OPENSSL_CHAIN_ONLY_CIPHER;
89 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
90 res = OPENSSL_CHAIN_CIPHER_AUTH;
97 /** Get session cipher key from input cipher key */
99 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key)
101 memcpy(session_key, input_key, keylen);
104 /** Get key ede 24 bytes standard from input key */
106 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede)
110 /* Initialize keys - 24 bytes: [key1-key2-key3] */
113 memcpy(key_ede, key, 24);
117 memcpy(key_ede, key, 16);
118 memcpy(key_ede + 16, key, 8);
121 /* K1 = K2 = K3 (DES compatibility) */
122 memcpy(key_ede, key, 8);
123 memcpy(key_ede + 8, key, 8);
124 memcpy(key_ede + 16, key, 8);
127 OPENSSL_LOG_ERR("Unsupported key size");
134 /** Get adequate openssl function for input cipher algorithm */
136 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
137 const EVP_CIPHER **algo)
143 case RTE_CRYPTO_CIPHER_3DES_CBC:
146 *algo = EVP_des_ede_cbc();
149 *algo = EVP_des_ede3_cbc();
155 case RTE_CRYPTO_CIPHER_3DES_CTR:
157 case RTE_CRYPTO_CIPHER_AES_CBC:
160 *algo = EVP_aes_128_cbc();
163 *algo = EVP_aes_192_cbc();
166 *algo = EVP_aes_256_cbc();
172 case RTE_CRYPTO_CIPHER_AES_CTR:
175 *algo = EVP_aes_128_ctr();
178 *algo = EVP_aes_192_ctr();
181 *algo = EVP_aes_256_ctr();
187 case RTE_CRYPTO_CIPHER_AES_GCM:
190 *algo = EVP_aes_128_gcm();
193 *algo = EVP_aes_192_gcm();
196 *algo = EVP_aes_256_gcm();
213 /** Get adequate openssl function for input auth algorithm */
215 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
222 case RTE_CRYPTO_AUTH_MD5:
223 case RTE_CRYPTO_AUTH_MD5_HMAC:
226 case RTE_CRYPTO_AUTH_SHA1:
227 case RTE_CRYPTO_AUTH_SHA1_HMAC:
230 case RTE_CRYPTO_AUTH_SHA224:
231 case RTE_CRYPTO_AUTH_SHA224_HMAC:
232 *algo = EVP_sha224();
234 case RTE_CRYPTO_AUTH_SHA256:
235 case RTE_CRYPTO_AUTH_SHA256_HMAC:
236 *algo = EVP_sha256();
238 case RTE_CRYPTO_AUTH_SHA384:
239 case RTE_CRYPTO_AUTH_SHA384_HMAC:
240 *algo = EVP_sha384();
242 case RTE_CRYPTO_AUTH_SHA512:
243 case RTE_CRYPTO_AUTH_SHA512_HMAC:
244 *algo = EVP_sha512();
257 /** Set session cipher parameters */
259 openssl_set_session_cipher_parameters(struct openssl_session *sess,
260 const struct rte_crypto_sym_xform *xform)
262 /* Select cipher direction */
263 sess->cipher.direction = xform->cipher.op;
264 /* Select cipher key */
265 sess->cipher.key.length = xform->cipher.key.length;
267 /* Set IV parameters */
268 sess->iv.offset = xform->cipher.iv.offset;
269 sess->iv.length = xform->cipher.iv.length;
271 /* Select cipher algo */
272 switch (xform->cipher.algo) {
273 case RTE_CRYPTO_CIPHER_3DES_CBC:
274 case RTE_CRYPTO_CIPHER_AES_CBC:
275 case RTE_CRYPTO_CIPHER_AES_CTR:
276 case RTE_CRYPTO_CIPHER_AES_GCM:
277 sess->cipher.mode = OPENSSL_CIPHER_LIB;
278 sess->cipher.algo = xform->cipher.algo;
279 sess->cipher.ctx = EVP_CIPHER_CTX_new();
281 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
282 &sess->cipher.evp_algo) != 0)
285 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
286 sess->cipher.key.data);
290 case RTE_CRYPTO_CIPHER_3DES_CTR:
291 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
292 sess->cipher.ctx = EVP_CIPHER_CTX_new();
294 if (get_cipher_key_ede(xform->cipher.key.data,
295 sess->cipher.key.length,
296 sess->cipher.key.data) != 0)
299 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
300 sess->cipher.algo = xform->cipher.algo;
301 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
302 sess->cipher.ctx = EVP_CIPHER_CTX_new();
303 sess->cipher.evp_algo = EVP_des_cbc();
305 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
306 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
307 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
308 NULL, xform->cipher.key.data, 0) != 1)
311 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
312 sess->cipher.key.data);
315 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
322 /* Set session auth parameters */
324 openssl_set_session_auth_parameters(struct openssl_session *sess,
325 const struct rte_crypto_sym_xform *xform)
327 /* Select auth generate/verify */
328 sess->auth.operation = xform->auth.op;
329 sess->auth.algo = xform->auth.algo;
331 /* Select auth algo */
332 switch (xform->auth.algo) {
333 case RTE_CRYPTO_AUTH_AES_GMAC:
334 case RTE_CRYPTO_AUTH_AES_GCM:
335 /* Check additional condition for AES_GMAC/GCM */
336 if (sess->cipher.algo != RTE_CRYPTO_CIPHER_AES_GCM)
338 sess->chain_order = OPENSSL_CHAIN_COMBINED;
341 case RTE_CRYPTO_AUTH_MD5:
342 case RTE_CRYPTO_AUTH_SHA1:
343 case RTE_CRYPTO_AUTH_SHA224:
344 case RTE_CRYPTO_AUTH_SHA256:
345 case RTE_CRYPTO_AUTH_SHA384:
346 case RTE_CRYPTO_AUTH_SHA512:
347 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
348 if (get_auth_algo(xform->auth.algo,
349 &sess->auth.auth.evp_algo) != 0)
351 sess->auth.auth.ctx = EVP_MD_CTX_create();
354 case RTE_CRYPTO_AUTH_MD5_HMAC:
355 case RTE_CRYPTO_AUTH_SHA1_HMAC:
356 case RTE_CRYPTO_AUTH_SHA224_HMAC:
357 case RTE_CRYPTO_AUTH_SHA256_HMAC:
358 case RTE_CRYPTO_AUTH_SHA384_HMAC:
359 case RTE_CRYPTO_AUTH_SHA512_HMAC:
360 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
361 sess->auth.hmac.ctx = EVP_MD_CTX_create();
362 if (get_auth_algo(xform->auth.algo,
363 &sess->auth.hmac.evp_algo) != 0)
365 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
366 xform->auth.key.data, xform->auth.key.length);
373 sess->auth.aad_length = xform->auth.add_auth_data_length;
374 sess->auth.digest_length = xform->auth.digest_length;
379 /** Parse crypto xform chain and set private session parameters */
381 openssl_set_session_parameters(struct openssl_session *sess,
382 const struct rte_crypto_sym_xform *xform)
384 const struct rte_crypto_sym_xform *cipher_xform = NULL;
385 const struct rte_crypto_sym_xform *auth_xform = NULL;
387 sess->chain_order = openssl_get_chain_order(xform);
388 switch (sess->chain_order) {
389 case OPENSSL_CHAIN_ONLY_CIPHER:
390 cipher_xform = xform;
392 case OPENSSL_CHAIN_ONLY_AUTH:
395 case OPENSSL_CHAIN_CIPHER_AUTH:
396 cipher_xform = xform;
397 auth_xform = xform->next;
399 case OPENSSL_CHAIN_AUTH_CIPHER:
401 cipher_xform = xform->next;
407 /* Default IV length = 0 */
410 /* cipher_xform must be check before auth_xform */
412 if (openssl_set_session_cipher_parameters(
413 sess, cipher_xform)) {
415 "Invalid/unsupported cipher parameters");
421 if (openssl_set_session_auth_parameters(sess, auth_xform)) {
423 "Invalid/unsupported auth parameters");
431 /** Reset private session parameters */
433 openssl_reset_session(struct openssl_session *sess)
435 EVP_CIPHER_CTX_free(sess->cipher.ctx);
437 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
438 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
440 switch (sess->auth.mode) {
441 case OPENSSL_AUTH_AS_AUTH:
442 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
444 case OPENSSL_AUTH_AS_HMAC:
445 EVP_PKEY_free(sess->auth.hmac.pkey);
446 EVP_MD_CTX_destroy(sess->auth.hmac.ctx);
453 /** Provide session for operation */
454 static struct openssl_session *
455 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
457 struct openssl_session *sess = NULL;
459 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
460 /* get existing session */
461 if (likely(op->sym->session != NULL &&
462 op->sym->session->dev_type ==
463 RTE_CRYPTODEV_OPENSSL_PMD))
464 sess = (struct openssl_session *)
465 op->sym->session->_private;
467 /* provide internal session */
470 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) {
471 sess = (struct openssl_session *)
472 ((struct rte_cryptodev_sym_session *)_sess)
475 if (unlikely(openssl_set_session_parameters(
476 sess, op->sym->xform) != 0)) {
477 rte_mempool_put(qp->sess_mp, _sess);
480 op->sym->session = _sess;
485 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
491 *------------------------------------------------------------------------------
493 *------------------------------------------------------------------------------
496 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
497 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
504 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
506 offset -= rte_pktmbuf_data_len(m);
511 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
513 l = rte_pktmbuf_data_len(m) - offset;
515 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
521 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
527 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
528 src = rte_pktmbuf_mtod(m, uint8_t *);
529 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
530 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
540 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
541 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx)
548 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
550 offset -= rte_pktmbuf_data_len(m);
555 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
557 l = rte_pktmbuf_data_len(m) - offset;
559 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
565 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
571 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
572 src = rte_pktmbuf_mtod(m, uint8_t *);
573 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
574 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
583 /** Process standard openssl cipher encryption */
585 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
586 int offset, uint8_t *iv, uint8_t *key, int srclen,
587 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
591 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
592 goto process_cipher_encrypt_err;
594 EVP_CIPHER_CTX_set_padding(ctx, 0);
596 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
598 goto process_cipher_encrypt_err;
600 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
601 goto process_cipher_encrypt_err;
605 process_cipher_encrypt_err:
606 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed");
610 /** Process standard openssl cipher encryption */
612 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
613 uint8_t *iv, int srclen,
617 uint8_t encrypted_iv[DES_BLOCK_SIZE];
620 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
621 iv, DES_BLOCK_SIZE) <= 0)
622 goto process_cipher_encrypt_err;
624 for (i = 0; i < srclen; i++)
625 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
629 process_cipher_encrypt_err:
630 OPENSSL_LOG_ERR("Process openssl cipher bpi encrypt failed");
633 /** Process standard openssl cipher decryption */
635 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
636 int offset, uint8_t *iv, uint8_t *key, int srclen,
637 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
641 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0)
642 goto process_cipher_decrypt_err;
644 EVP_CIPHER_CTX_set_padding(ctx, 0);
646 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
648 goto process_cipher_decrypt_err;
650 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
651 goto process_cipher_decrypt_err;
654 process_cipher_decrypt_err:
655 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed");
659 /** Process cipher des 3 ctr encryption, decryption algorithm */
661 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
662 int offset, uint8_t *iv, uint8_t *key, int srclen,
665 uint8_t ebuf[8], ctr[8];
671 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
673 offset -= rte_pktmbuf_data_len(m);
676 goto process_cipher_des3ctr_err;
678 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
679 l = rte_pktmbuf_data_len(m) - offset;
681 /* We use 3DES encryption also for decryption.
682 * IV is not important for 3DES ecb
684 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
685 goto process_cipher_des3ctr_err;
689 for (n = 0; n < srclen; n++) {
691 if (EVP_EncryptUpdate(ctx,
692 (unsigned char *)&ebuf, &unused,
693 (const unsigned char *)&ctr, 8) <= 0)
694 goto process_cipher_des3ctr_err;
697 dst[n] = *(src++) ^ ebuf[n % 8];
703 src = rte_pktmbuf_mtod(m, uint8_t *);
704 l = rte_pktmbuf_data_len(m);
711 process_cipher_des3ctr_err:
712 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed");
716 /** Process auth/encription aes-gcm algorithm */
718 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
719 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
720 uint8_t *key, uint8_t *dst, uint8_t *tag,
721 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo)
723 int len = 0, unused = 0;
724 uint8_t empty[] = {};
726 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
727 goto process_auth_encryption_gcm_err;
729 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
730 goto process_auth_encryption_gcm_err;
732 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
733 goto process_auth_encryption_gcm_err;
736 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
737 goto process_auth_encryption_gcm_err;
740 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
742 goto process_auth_encryption_gcm_err;
744 /* Workaround open ssl bug in version less then 1.0.1f */
745 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
746 goto process_auth_encryption_gcm_err;
748 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
749 goto process_auth_encryption_gcm_err;
751 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
752 goto process_auth_encryption_gcm_err;
756 process_auth_encryption_gcm_err:
757 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed");
762 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
763 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen,
764 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx,
765 const EVP_CIPHER *algo)
767 int len = 0, unused = 0;
768 uint8_t empty[] = {};
770 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0)
771 goto process_auth_decryption_gcm_err;
773 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0)
774 goto process_auth_decryption_gcm_err;
776 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
777 goto process_auth_decryption_gcm_err;
779 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0)
780 goto process_auth_decryption_gcm_err;
783 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
784 goto process_auth_decryption_gcm_err;
787 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
789 goto process_auth_decryption_gcm_err;
791 /* Workaround open ssl bug in version less then 1.0.1f */
792 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
793 goto process_auth_decryption_gcm_err;
795 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
796 goto process_auth_decryption_gcm_final_err;
800 process_auth_decryption_gcm_err:
801 OPENSSL_LOG_ERR("Process openssl auth description gcm failed");
804 process_auth_decryption_gcm_final_err:
808 /** Process standard openssl auth algorithms */
810 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
811 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
812 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
819 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
821 offset -= rte_pktmbuf_data_len(m);
824 goto process_auth_err;
826 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
827 goto process_auth_err;
829 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
831 l = rte_pktmbuf_data_len(m) - offset;
833 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
834 goto process_auth_err;
835 goto process_auth_final;
838 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
839 goto process_auth_err;
843 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
844 src = rte_pktmbuf_mtod(m, uint8_t *);
845 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
846 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
847 goto process_auth_err;
852 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
853 goto process_auth_err;
857 OPENSSL_LOG_ERR("Process openssl auth failed");
861 /** Process standard openssl auth algorithms with hmac */
863 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
864 __rte_unused uint8_t *iv, EVP_PKEY *pkey,
865 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
872 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
874 offset -= rte_pktmbuf_data_len(m);
877 goto process_auth_err;
879 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0)
880 goto process_auth_err;
882 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
884 l = rte_pktmbuf_data_len(m) - offset;
886 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0)
887 goto process_auth_err;
888 goto process_auth_final;
891 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
892 goto process_auth_err;
896 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
897 src = rte_pktmbuf_mtod(m, uint8_t *);
898 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
899 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0)
900 goto process_auth_err;
905 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0)
906 goto process_auth_err;
911 OPENSSL_LOG_ERR("Process openssl auth failed");
915 /*----------------------------------------------------------------------------*/
917 /** Process auth/cipher combined operation */
919 process_openssl_combined_op
920 (struct rte_crypto_op *op, struct openssl_session *sess,
921 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
924 uint8_t *dst = NULL, *iv, *tag, *aad;
925 int srclen, ivlen, aadlen, status = -1;
928 * Segmented destination buffer is not supported for
929 * encryption/decryption
931 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
932 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
936 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
938 ivlen = sess->iv.length;
939 aad = op->sym->auth.aad.data;
940 aadlen = sess->auth.aad_length;
942 tag = op->sym->auth.digest.data;
944 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
945 op->sym->cipher.data.offset +
946 op->sym->cipher.data.length);
948 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC)
951 srclen = op->sym->cipher.data.length;
952 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
953 op->sym->cipher.data.offset);
956 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
957 status = process_openssl_auth_encryption_gcm(
958 mbuf_src, op->sym->cipher.data.offset, srclen,
959 aad, aadlen, iv, ivlen, sess->cipher.key.data,
960 dst, tag, sess->cipher.ctx,
961 sess->cipher.evp_algo);
963 status = process_openssl_auth_decryption_gcm(
964 mbuf_src, op->sym->cipher.data.offset, srclen,
965 aad, aadlen, iv, ivlen, sess->cipher.key.data,
966 dst, tag, sess->cipher.ctx,
967 sess->cipher.evp_algo);
970 if (status == (-EFAULT) &&
971 sess->auth.operation ==
972 RTE_CRYPTO_AUTH_OP_VERIFY)
973 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
975 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
979 /** Process cipher operation */
981 process_openssl_cipher_op
982 (struct rte_crypto_op *op, struct openssl_session *sess,
983 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
989 * Segmented destination buffer is not supported for
990 * encryption/decryption
992 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
993 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
997 srclen = op->sym->cipher.data.length;
998 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
999 op->sym->cipher.data.offset);
1001 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1004 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1005 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1006 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1007 op->sym->cipher.data.offset, iv,
1008 sess->cipher.key.data, srclen,
1010 sess->cipher.evp_algo);
1012 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1013 op->sym->cipher.data.offset, iv,
1014 sess->cipher.key.data, srclen,
1016 sess->cipher.evp_algo);
1018 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1019 op->sym->cipher.data.offset, iv,
1020 sess->cipher.key.data, srclen,
1024 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1027 /** Process cipher operation */
1029 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1030 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1031 struct rte_mbuf *mbuf_dst)
1033 uint8_t *src, *dst, *iv;
1034 uint8_t block_size, last_block_len;
1035 int srclen, status = 0;
1037 srclen = op->sym->cipher.data.length;
1038 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1039 op->sym->cipher.data.offset);
1040 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1041 op->sym->cipher.data.offset);
1043 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1046 block_size = DES_BLOCK_SIZE;
1048 last_block_len = srclen % block_size;
1049 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1050 /* Encrypt only with ECB mode XOR IV */
1051 if (srclen < block_size) {
1052 status = process_openssl_cipher_bpi_encrypt(src, dst,
1054 sess->cipher.bpi_ctx);
1056 srclen -= last_block_len;
1057 /* Encrypt with the block aligned stream with CBC mode */
1058 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1059 op->sym->cipher.data.offset, iv,
1060 sess->cipher.key.data, srclen,
1061 sess->cipher.ctx, sess->cipher.evp_algo);
1062 if (last_block_len) {
1063 /* Point at last block */
1066 * IV is the last encrypted block from
1067 * the previous operation
1069 iv = dst - block_size;
1071 srclen = last_block_len;
1072 /* Encrypt the last frame with ECB mode */
1073 status |= process_openssl_cipher_bpi_encrypt(src,
1075 srclen, sess->cipher.bpi_ctx);
1079 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1080 if (srclen < block_size) {
1081 status = process_openssl_cipher_bpi_encrypt(src, dst,
1084 sess->cipher.bpi_ctx);
1086 if (last_block_len) {
1087 /* Point at last block */
1088 dst += srclen - last_block_len;
1089 src += srclen - last_block_len;
1091 * IV is the last full block
1093 iv = src - block_size;
1095 * Decrypt the last frame with ECB mode
1096 * (encrypt, as it is the same operation)
1098 status = process_openssl_cipher_bpi_encrypt(src,
1100 last_block_len, sess->cipher.bpi_ctx);
1101 /* Prepare parameters for CBC mode op */
1102 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1104 dst += last_block_len - srclen;
1105 srclen -= last_block_len;
1108 /* Decrypt with CBC mode */
1109 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1110 op->sym->cipher.data.offset, iv,
1111 sess->cipher.key.data, srclen,
1113 sess->cipher.evp_algo);
1118 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1121 /** Process auth operation */
1123 process_openssl_auth_op
1124 (struct rte_crypto_op *op, struct openssl_session *sess,
1125 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1130 srclen = op->sym->auth.data.length;
1132 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1133 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src,
1134 sess->auth.digest_length);
1136 dst = op->sym->auth.digest.data;
1138 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1139 op->sym->auth.data.offset +
1140 op->sym->auth.data.length);
1143 switch (sess->auth.mode) {
1144 case OPENSSL_AUTH_AS_AUTH:
1145 status = process_openssl_auth(mbuf_src, dst,
1146 op->sym->auth.data.offset, NULL, NULL, srclen,
1147 sess->auth.auth.ctx, sess->auth.auth.evp_algo);
1149 case OPENSSL_AUTH_AS_HMAC:
1150 status = process_openssl_auth_hmac(mbuf_src, dst,
1151 op->sym->auth.data.offset, NULL,
1152 sess->auth.hmac.pkey, srclen,
1153 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo);
1160 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1161 if (memcmp(dst, op->sym->auth.digest.data,
1162 sess->auth.digest_length) != 0) {
1163 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1165 /* Trim area used for digest from mbuf. */
1166 rte_pktmbuf_trim(mbuf_src, sess->auth.digest_length);
1170 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1173 /** Process crypto operation for mbuf */
1175 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op,
1176 struct openssl_session *sess)
1178 struct rte_mbuf *msrc, *mdst;
1181 msrc = op->sym->m_src;
1182 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
1184 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1186 switch (sess->chain_order) {
1187 case OPENSSL_CHAIN_ONLY_CIPHER:
1188 process_openssl_cipher_op(op, sess, msrc, mdst);
1190 case OPENSSL_CHAIN_ONLY_AUTH:
1191 process_openssl_auth_op(op, sess, msrc, mdst);
1193 case OPENSSL_CHAIN_CIPHER_AUTH:
1194 process_openssl_cipher_op(op, sess, msrc, mdst);
1195 process_openssl_auth_op(op, sess, mdst, mdst);
1197 case OPENSSL_CHAIN_AUTH_CIPHER:
1198 process_openssl_auth_op(op, sess, msrc, mdst);
1199 process_openssl_cipher_op(op, sess, msrc, mdst);
1201 case OPENSSL_CHAIN_COMBINED:
1202 process_openssl_combined_op(op, sess, msrc, mdst);
1204 case OPENSSL_CHAIN_CIPHER_BPI:
1205 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
1208 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1212 /* Free session if a session-less crypto op */
1213 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1214 openssl_reset_session(sess);
1215 memset(sess, 0, sizeof(struct openssl_session));
1216 rte_mempool_put(qp->sess_mp, op->sym->session);
1217 op->sym->session = NULL;
1220 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
1221 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1223 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
1224 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
1232 *------------------------------------------------------------------------------
1234 *------------------------------------------------------------------------------
1237 /** Enqueue burst */
1239 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
1242 struct openssl_session *sess;
1243 struct openssl_qp *qp = queue_pair;
1246 for (i = 0; i < nb_ops; i++) {
1247 sess = get_session(qp, ops[i]);
1248 if (unlikely(sess == NULL))
1251 retval = process_op(qp, ops[i], sess);
1252 if (unlikely(retval < 0))
1256 qp->stats.enqueued_count += i;
1260 qp->stats.enqueue_err_count++;
1264 /** Dequeue burst */
1266 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1269 struct openssl_qp *qp = queue_pair;
1271 unsigned int nb_dequeued = 0;
1273 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
1274 (void **)ops, nb_ops, NULL);
1275 qp->stats.dequeued_count += nb_dequeued;
1280 /** Create OPENSSL crypto device */
1282 cryptodev_openssl_create(const char *name,
1283 struct rte_vdev_device *vdev,
1284 struct rte_crypto_vdev_init_params *init_params)
1286 struct rte_cryptodev *dev;
1287 struct openssl_private *internals;
1289 if (init_params->name[0] == '\0')
1290 snprintf(init_params->name, sizeof(init_params->name),
1293 dev = rte_cryptodev_vdev_pmd_init(init_params->name,
1294 sizeof(struct openssl_private),
1295 init_params->socket_id,
1298 OPENSSL_LOG_ERR("failed to create cryptodev vdev");
1302 dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD;
1303 dev->dev_ops = rte_openssl_pmd_ops;
1305 /* register rx/tx burst functions for data path */
1306 dev->dequeue_burst = openssl_pmd_dequeue_burst;
1307 dev->enqueue_burst = openssl_pmd_enqueue_burst;
1309 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1310 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1311 RTE_CRYPTODEV_FF_CPU_AESNI |
1312 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER;
1314 /* Set vector instructions mode supported */
1315 internals = dev->data->dev_private;
1317 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
1318 internals->max_nb_sessions = init_params->max_nb_sessions;
1323 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed",
1326 cryptodev_openssl_remove(vdev);
1330 /** Initialise OPENSSL crypto device */
1332 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
1334 struct rte_crypto_vdev_init_params init_params = {
1335 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
1336 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
1341 const char *input_args;
1343 name = rte_vdev_device_name(vdev);
1346 input_args = rte_vdev_device_args(vdev);
1348 rte_cryptodev_vdev_parse_init_params(&init_params, input_args);
1350 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
1351 init_params.socket_id);
1352 if (init_params.name[0] != '\0')
1353 RTE_LOG(INFO, PMD, " User defined name = %s\n",
1355 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
1356 init_params.max_nb_queue_pairs);
1357 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
1358 init_params.max_nb_sessions);
1360 return cryptodev_openssl_create(name, vdev, &init_params);
1363 /** Uninitialise OPENSSL crypto device */
1365 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
1369 name = rte_vdev_device_name(vdev);
1374 "Closing OPENSSL crypto device %s on numa socket %u\n",
1375 name, rte_socket_id());
1380 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
1381 .probe = cryptodev_openssl_probe,
1382 .remove = cryptodev_openssl_remove
1385 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
1386 cryptodev_openssl_pmd_drv);
1387 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
1388 "max_nb_queue_pairs=<int> "
1389 "max_nb_sessions=<int> "