1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2016-2017 Intel Corporation. 3 4Cryptography Device Library 5=========================== 6 7The cryptodev library provides a Crypto device framework for management and 8provisioning of hardware and software Crypto poll mode drivers, defining generic 9APIs which support a number of different Crypto operations. The framework 10currently only supports cipher, authentication, chained cipher/authentication 11and AEAD symmetric and asymmetric Crypto operations. 12 13 14Design Principles 15----------------- 16 17The cryptodev library follows the same basic principles as those used in DPDK's 18Ethernet Device framework. The Crypto framework provides a generic Crypto device 19framework which supports both physical (hardware) and virtual (software) Crypto 20devices as well as a generic Crypto API which allows Crypto devices to be 21managed and configured and supports Crypto operations to be provisioned on 22Crypto poll mode driver. 23 24 25Device Management 26----------------- 27 28Device Creation 29~~~~~~~~~~~~~~~ 30 31Physical Crypto devices are discovered during the PCI probe/enumeration of the 32EAL function which is executed at DPDK initialization, based on 33their PCI device identifier, each unique PCI BDF (bus/bridge, device, 34function). Specific physical Crypto devices, like other physical devices in DPDK 35can be white-listed or black-listed using the EAL command line options. 36 37Virtual devices can be created by two mechanisms, either using the EAL command 38line options or from within the application using an EAL API directly. 39 40From the command line using the --vdev EAL option 41 42.. code-block:: console 43 44 --vdev 'crypto_aesni_mb0,max_nb_queue_pairs=2,socket_id=0' 45 46.. Note:: 47 48 * If DPDK application requires multiple software crypto PMD devices then required 49 number of ``--vdev`` with appropriate libraries are to be added. 50 51 * An Application with crypto PMD instances sharing the same library requires unique ID. 52 53 Example: ``--vdev 'crypto_aesni_mb0' --vdev 'crypto_aesni_mb1'`` 54 55Or using the rte_vdev_init API within the application code. 56 57.. code-block:: c 58 59 rte_vdev_init("crypto_aesni_mb", 60 "max_nb_queue_pairs=2,socket_id=0") 61 62All virtual Crypto devices support the following initialization parameters: 63 64* ``max_nb_queue_pairs`` - maximum number of queue pairs supported by the device. 65* ``socket_id`` - socket on which to allocate the device resources on. 66 67 68Device Identification 69~~~~~~~~~~~~~~~~~~~~~ 70 71Each device, whether virtual or physical is uniquely designated by two 72identifiers: 73 74- A unique device index used to designate the Crypto device in all functions 75 exported by the cryptodev API. 76 77- A device name used to designate the Crypto device in console messages, for 78 administration or debugging purposes. For ease of use, the port name includes 79 the port index. 80 81 82Device Configuration 83~~~~~~~~~~~~~~~~~~~~ 84 85The configuration of each Crypto device includes the following operations: 86 87- Allocation of resources, including hardware resources if a physical device. 88- Resetting the device into a well-known default state. 89- Initialization of statistics counters. 90 91The rte_cryptodev_configure API is used to configure a Crypto device. 92 93.. code-block:: c 94 95 int rte_cryptodev_configure(uint8_t dev_id, 96 struct rte_cryptodev_config *config) 97 98The ``rte_cryptodev_config`` structure is used to pass the configuration 99parameters for socket selection and number of queue pairs. 100 101.. code-block:: c 102 103 struct rte_cryptodev_config { 104 int socket_id; 105 /**< Socket to allocate resources on */ 106 uint16_t nb_queue_pairs; 107 /**< Number of queue pairs to configure on device */ 108 }; 109 110 111Configuration of Queue Pairs 112~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 113 114Each Crypto devices queue pair is individually configured through the 115``rte_cryptodev_queue_pair_setup`` API. 116Each queue pairs resources may be allocated on a specified socket. 117 118.. code-block:: c 119 120 int rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id, 121 const struct rte_cryptodev_qp_conf *qp_conf, 122 int socket_id) 123 124 struct rte_cryptodev_qp_conf { 125 uint32_t nb_descriptors; /**< Number of descriptors per queue pair */ 126 struct rte_mempool *mp_session; 127 /**< The mempool for creating session in sessionless mode */ 128 struct rte_mempool *mp_session_private; 129 /**< The mempool for creating sess private data in sessionless mode */ 130 }; 131 132 133The fields ``mp_session`` and ``mp_session_private`` are used for creating 134temporary session to process the crypto operations in the session-less mode. 135They can be the same other different mempools. Please note not all Cryptodev 136PMDs supports session-less mode. 137 138 139Logical Cores, Memory and Queues Pair Relationships 140~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 141 142The Crypto device Library as the Poll Mode Driver library support NUMA for when 143a processor’s logical cores and interfaces utilize its local memory. Therefore 144Crypto operations, and in the case of symmetric Crypto operations, the session 145and the mbuf being operated on, should be allocated from memory pools created 146in the local memory. The buffers should, if possible, remain on the local 147processor to obtain the best performance results and buffer descriptors should 148be populated with mbufs allocated from a mempool allocated from local memory. 149 150The run-to-completion model also performs better, especially in the case of 151virtual Crypto devices, if the Crypto operation and session and data buffer is 152in local memory instead of a remote processor's memory. This is also true for 153the pipe-line model provided all logical cores used are located on the same 154processor. 155 156Multiple logical cores should never share the same queue pair for enqueuing 157operations or dequeuing operations on the same Crypto device since this would 158require global locks and hinder performance. It is however possible to use a 159different logical core to dequeue an operation on a queue pair from the logical 160core which it was enqueued on. This means that a crypto burst enqueue/dequeue 161APIs are a logical place to transition from one logical core to another in a 162packet processing pipeline. 163 164 165Device Features and Capabilities 166--------------------------------- 167 168Crypto devices define their functionality through two mechanisms, global device 169features and algorithm capabilities. Global devices features identify device 170wide level features which are applicable to the whole device such as 171the device having hardware acceleration or supporting symmetric and/or asymmetric 172Crypto operations. 173 174The capabilities mechanism defines the individual algorithms/functions which 175the device supports, such as a specific symmetric Crypto cipher, 176authentication operation or Authenticated Encryption with Associated Data 177(AEAD) operation. 178 179 180Device Features 181~~~~~~~~~~~~~~~ 182 183Currently the following Crypto device features are defined: 184 185* Symmetric Crypto operations 186* Asymmetric Crypto operations 187* Chaining of symmetric Crypto operations 188* SSE accelerated SIMD vector operations 189* AVX accelerated SIMD vector operations 190* AVX2 accelerated SIMD vector operations 191* AESNI accelerated instructions 192* Hardware off-load processing 193 194 195Device Operation Capabilities 196~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 197 198Crypto capabilities which identify particular algorithm which the Crypto PMD 199supports are defined by the operation type, the operation transform, the 200transform identifier and then the particulars of the transform. For the full 201scope of the Crypto capability see the definition of the structure in the 202*DPDK API Reference*. 203 204.. code-block:: c 205 206 struct rte_cryptodev_capabilities; 207 208Each Crypto poll mode driver defines its own private array of capabilities 209for the operations it supports. Below is an example of the capabilities for a 210PMD which supports the authentication algorithm SHA1_HMAC and the cipher 211algorithm AES_CBC. 212 213.. code-block:: c 214 215 static const struct rte_cryptodev_capabilities pmd_capabilities[] = { 216 { /* SHA1 HMAC */ 217 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 218 .sym = { 219 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 220 .auth = { 221 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC, 222 .block_size = 64, 223 .key_size = { 224 .min = 64, 225 .max = 64, 226 .increment = 0 227 }, 228 .digest_size = { 229 .min = 12, 230 .max = 12, 231 .increment = 0 232 }, 233 .aad_size = { 0 }, 234 .iv_size = { 0 } 235 } 236 } 237 }, 238 { /* AES CBC */ 239 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 240 .sym = { 241 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 242 .cipher = { 243 .algo = RTE_CRYPTO_CIPHER_AES_CBC, 244 .block_size = 16, 245 .key_size = { 246 .min = 16, 247 .max = 32, 248 .increment = 8 249 }, 250 .iv_size = { 251 .min = 16, 252 .max = 16, 253 .increment = 0 254 } 255 } 256 } 257 } 258 } 259 260 261Capabilities Discovery 262~~~~~~~~~~~~~~~~~~~~~~ 263 264Discovering the features and capabilities of a Crypto device poll mode driver 265is achieved through the ``rte_cryptodev_info_get`` function. 266 267.. code-block:: c 268 269 void rte_cryptodev_info_get(uint8_t dev_id, 270 struct rte_cryptodev_info *dev_info); 271 272This allows the user to query a specific Crypto PMD and get all the device 273features and capabilities. The ``rte_cryptodev_info`` structure contains all the 274relevant information for the device. 275 276.. code-block:: c 277 278 struct rte_cryptodev_info { 279 const char *driver_name; 280 uint8_t driver_id; 281 struct rte_device *device; 282 283 uint64_t feature_flags; 284 285 const struct rte_cryptodev_capabilities *capabilities; 286 287 unsigned max_nb_queue_pairs; 288 289 struct { 290 unsigned max_nb_sessions; 291 } sym; 292 }; 293 294 295Operation Processing 296-------------------- 297 298Scheduling of Crypto operations on DPDK's application data path is 299performed using a burst oriented asynchronous API set. A queue pair on a Crypto 300device accepts a burst of Crypto operations using enqueue burst API. On physical 301Crypto devices the enqueue burst API will place the operations to be processed 302on the devices hardware input queue, for virtual devices the processing of the 303Crypto operations is usually completed during the enqueue call to the Crypto 304device. The dequeue burst API will retrieve any processed operations available 305from the queue pair on the Crypto device, from physical devices this is usually 306directly from the devices processed queue, and for virtual device's from a 307``rte_ring`` where processed operations are placed after being processed on the 308enqueue call. 309 310 311Private data 312~~~~~~~~~~~~ 313For session-based operations, the set and get API provides a mechanism for an 314application to store and retrieve the private user data information stored along 315with the crypto session. 316 317For example, suppose an application is submitting a crypto operation with a session 318associated and wants to indicate private user data information which is required to be 319used after completion of the crypto operation. In this case, the application can use 320the set API to set the user data and retrieve it using get API. 321 322.. code-block:: c 323 324 int rte_cryptodev_sym_session_set_user_data( 325 struct rte_cryptodev_sym_session *sess, void *data, uint16_t size); 326 327 void * rte_cryptodev_sym_session_get_user_data( 328 struct rte_cryptodev_sym_session *sess); 329 330Please note the ``size`` passed to set API cannot be bigger than the predefined 331``user_data_sz`` when creating the session header mempool, otherwise the 332function will return error. Also when ``user_data_sz`` was defined as ``0`` when 333creating the session header mempool, the get API will always return ``NULL``. 334 335For session-less mode, the private user data information can be placed along with the 336``struct rte_crypto_op``. The ``rte_crypto_op::private_data_offset`` indicates the 337start of private data information. The offset is counted from the start of the 338rte_crypto_op including other crypto information such as the IVs (since there can 339be an IV also for authentication). 340 341 342Enqueue / Dequeue Burst APIs 343~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 344 345The burst enqueue API uses a Crypto device identifier and a queue pair 346identifier to specify the Crypto device queue pair to schedule the processing on. 347The ``nb_ops`` parameter is the number of operations to process which are 348supplied in the ``ops`` array of ``rte_crypto_op`` structures. 349The enqueue function returns the number of operations it actually enqueued for 350processing, a return value equal to ``nb_ops`` means that all packets have been 351enqueued. 352 353.. code-block:: c 354 355 uint16_t rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id, 356 struct rte_crypto_op **ops, uint16_t nb_ops) 357 358The dequeue API uses the same format as the enqueue API of processed but 359the ``nb_ops`` and ``ops`` parameters are now used to specify the max processed 360operations the user wishes to retrieve and the location in which to store them. 361The API call returns the actual number of processed operations returned, this 362can never be larger than ``nb_ops``. 363 364.. code-block:: c 365 366 uint16_t rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id, 367 struct rte_crypto_op **ops, uint16_t nb_ops) 368 369 370Operation Representation 371~~~~~~~~~~~~~~~~~~~~~~~~ 372 373An Crypto operation is represented by an rte_crypto_op structure, which is a 374generic metadata container for all necessary information required for the 375Crypto operation to be processed on a particular Crypto device poll mode driver. 376 377.. figure:: img/crypto_op.* 378 379The operation structure includes the operation type, the operation status 380and the session type (session-based/less), a reference to the operation 381specific data, which can vary in size and content depending on the operation 382being provisioned. It also contains the source mempool for the operation, 383if it allocated from a mempool. 384 385If Crypto operations are allocated from a Crypto operation mempool, see next 386section, there is also the ability to allocate private memory with the 387operation for applications purposes. 388 389Application software is responsible for specifying all the operation specific 390fields in the ``rte_crypto_op`` structure which are then used by the Crypto PMD 391to process the requested operation. 392 393 394Operation Management and Allocation 395~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 396 397The cryptodev library provides an API set for managing Crypto operations which 398utilize the Mempool Library to allocate operation buffers. Therefore, it ensures 399that the crypto operation is interleaved optimally across the channels and 400ranks for optimal processing. 401A ``rte_crypto_op`` contains a field indicating the pool that it originated from. 402When calling ``rte_crypto_op_free(op)``, the operation returns to its original pool. 403 404.. code-block:: c 405 406 extern struct rte_mempool * 407 rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type, 408 unsigned nb_elts, unsigned cache_size, uint16_t priv_size, 409 int socket_id); 410 411During pool creation ``rte_crypto_op_init()`` is called as a constructor to 412initialize each Crypto operation which subsequently calls 413``__rte_crypto_op_reset()`` to configure any operation type specific fields based 414on the type parameter. 415 416 417``rte_crypto_op_alloc()`` and ``rte_crypto_op_bulk_alloc()`` are used to allocate 418Crypto operations of a specific type from a given Crypto operation mempool. 419``__rte_crypto_op_reset()`` is called on each operation before being returned to 420allocate to a user so the operation is always in a good known state before use 421by the application. 422 423.. code-block:: c 424 425 struct rte_crypto_op *rte_crypto_op_alloc(struct rte_mempool *mempool, 426 enum rte_crypto_op_type type) 427 428 unsigned rte_crypto_op_bulk_alloc(struct rte_mempool *mempool, 429 enum rte_crypto_op_type type, 430 struct rte_crypto_op **ops, uint16_t nb_ops) 431 432``rte_crypto_op_free()`` is called by the application to return an operation to 433its allocating pool. 434 435.. code-block:: c 436 437 void rte_crypto_op_free(struct rte_crypto_op *op) 438 439 440Symmetric Cryptography Support 441------------------------------ 442 443The cryptodev library currently provides support for the following symmetric 444Crypto operations; cipher, authentication, including chaining of these 445operations, as well as also supporting AEAD operations. 446 447 448Session and Session Management 449~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 450 451Sessions are used in symmetric cryptographic processing to store the immutable 452data defined in a cryptographic transform which is used in the operation 453processing of a packet flow. Sessions are used to manage information such as 454expand cipher keys and HMAC IPADs and OPADs, which need to be calculated for a 455particular Crypto operation, but are immutable on a packet to packet basis for 456a flow. Crypto sessions cache this immutable data in a optimal way for the 457underlying PMD and this allows further acceleration of the offload of 458Crypto workloads. 459 460.. figure:: img/cryptodev_sym_sess.* 461 462The Crypto device framework provides APIs to create session mempool and allocate 463and initialize sessions for crypto devices, where sessions are mempool objects. 464The application has to use ``rte_cryptodev_sym_session_pool_create()`` to 465create the session header mempool that creates a mempool with proper element 466size automatically and stores necessary information for safely accessing the 467session in the mempool's private data field. 468 469To create a mempool for storing session private data, the application has two 470options. The first is to create another mempool with elt size equal to or 471bigger than the maximum session private data size of all crypto devices that 472will share the same session header. The creation of the mempool shall use the 473traditional ``rte_mempool_create()`` with the correct ``elt_size``. The other 474option is to change the ``elt_size`` parameter in 475``rte_cryptodev_sym_session_pool_create()`` to the correct value. The first 476option is more complex to implement but may result in better memory usage as 477a session header normally takes smaller memory footprint as the session private 478data. 479 480Once the session mempools have been created, ``rte_cryptodev_sym_session_create()`` 481is used to allocate an uninitialized session from the given mempool. 482The session then must be initialized using ``rte_cryptodev_sym_session_init()`` 483for each of the required crypto devices. A symmetric transform chain 484is used to specify the operation and its parameters. See the section below for 485details on transforms. 486 487When a session is no longer used, user must call ``rte_cryptodev_sym_session_clear()`` 488for each of the crypto devices that are using the session, to free all driver 489private session data. Once this is done, session should be freed using 490``rte_cryptodev_sym_session_free`` which returns them to their mempool. 491 492 493Transforms and Transform Chaining 494~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 495 496Symmetric Crypto transforms (``rte_crypto_sym_xform``) are the mechanism used 497to specify the details of the Crypto operation. For chaining of symmetric 498operations such as cipher encrypt and authentication generate, the next pointer 499allows transform to be chained together. Crypto devices which support chaining 500must publish the chaining of symmetric Crypto operations feature flag. Allocation of the 501xform structure is in the the application domain. To allow future API extensions in a 502backwardly compatible manner, e.g. addition of a new parameter, the application should 503zero the full xform struct before populating it. 504 505Currently there are three transforms types cipher, authentication and AEAD. 506Also it is important to note that the order in which the 507transforms are passed indicates the order of the chaining. 508 509.. code-block:: c 510 511 struct rte_crypto_sym_xform { 512 struct rte_crypto_sym_xform *next; 513 /**< next xform in chain */ 514 enum rte_crypto_sym_xform_type type; 515 /**< xform type */ 516 union { 517 struct rte_crypto_auth_xform auth; 518 /**< Authentication / hash xform */ 519 struct rte_crypto_cipher_xform cipher; 520 /**< Cipher xform */ 521 struct rte_crypto_aead_xform aead; 522 /**< AEAD xform */ 523 }; 524 }; 525 526The API does not place a limit on the number of transforms that can be chained 527together but this will be limited by the underlying Crypto device poll mode 528driver which is processing the operation. 529 530.. figure:: img/crypto_xform_chain.* 531 532 533Symmetric Operations 534~~~~~~~~~~~~~~~~~~~~ 535 536The symmetric Crypto operation structure contains all the mutable data relating 537to performing symmetric cryptographic processing on a referenced mbuf data 538buffer. It is used for either cipher, authentication, AEAD and chained 539operations. 540 541As a minimum the symmetric operation must have a source data buffer (``m_src``), 542a valid session (or transform chain if in session-less mode) and the minimum 543authentication/ cipher/ AEAD parameters required depending on the type of operation 544specified in the session or the transform 545chain. 546 547.. code-block:: c 548 549 struct rte_crypto_sym_op { 550 struct rte_mbuf *m_src; 551 struct rte_mbuf *m_dst; 552 553 union { 554 struct rte_cryptodev_sym_session *session; 555 /**< Handle for the initialised session context */ 556 struct rte_crypto_sym_xform *xform; 557 /**< Session-less API Crypto operation parameters */ 558 }; 559 560 union { 561 struct { 562 struct { 563 uint32_t offset; 564 uint32_t length; 565 } data; /**< Data offsets and length for AEAD */ 566 567 struct { 568 uint8_t *data; 569 rte_iova_t phys_addr; 570 } digest; /**< Digest parameters */ 571 572 struct { 573 uint8_t *data; 574 rte_iova_t phys_addr; 575 } aad; 576 /**< Additional authentication parameters */ 577 } aead; 578 579 struct { 580 struct { 581 struct { 582 uint32_t offset; 583 uint32_t length; 584 } data; /**< Data offsets and length for ciphering */ 585 } cipher; 586 587 struct { 588 struct { 589 uint32_t offset; 590 uint32_t length; 591 } data; 592 /**< Data offsets and length for authentication */ 593 594 struct { 595 uint8_t *data; 596 rte_iova_t phys_addr; 597 } digest; /**< Digest parameters */ 598 } auth; 599 }; 600 }; 601 }; 602 603Sample code 604----------- 605 606There are various sample applications that show how to use the cryptodev library, 607such as the L2fwd with Crypto sample application (L2fwd-crypto) and 608the IPsec Security Gateway application (ipsec-secgw). 609 610While these applications demonstrate how an application can be created to perform 611generic crypto operation, the required complexity hides the basic steps of 612how to use the cryptodev APIs. 613 614The following sample code shows the basic steps to encrypt several buffers 615with AES-CBC (although performing other crypto operations is similar), 616using one of the crypto PMDs available in DPDK. 617 618.. code-block:: c 619 620 /* 621 * Simple example to encrypt several buffers with AES-CBC using 622 * the Cryptodev APIs. 623 */ 624 625 #define MAX_SESSIONS 1024 626 #define NUM_MBUFS 1024 627 #define POOL_CACHE_SIZE 128 628 #define BURST_SIZE 32 629 #define BUFFER_SIZE 1024 630 #define AES_CBC_IV_LENGTH 16 631 #define AES_CBC_KEY_LENGTH 16 632 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \ 633 sizeof(struct rte_crypto_sym_op)) 634 635 struct rte_mempool *mbuf_pool, *crypto_op_pool; 636 struct rte_mempool *session_pool, *session_priv_pool; 637 unsigned int session_size; 638 int ret; 639 640 /* Initialize EAL. */ 641 ret = rte_eal_init(argc, argv); 642 if (ret < 0) 643 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); 644 645 uint8_t socket_id = rte_socket_id(); 646 647 /* Create the mbuf pool. */ 648 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 649 NUM_MBUFS, 650 POOL_CACHE_SIZE, 651 0, 652 RTE_MBUF_DEFAULT_BUF_SIZE, 653 socket_id); 654 if (mbuf_pool == NULL) 655 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); 656 657 /* 658 * The IV is always placed after the crypto operation, 659 * so some private data is required to be reserved. 660 */ 661 unsigned int crypto_op_private_data = AES_CBC_IV_LENGTH; 662 663 /* Create crypto operation pool. */ 664 crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool", 665 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 666 NUM_MBUFS, 667 POOL_CACHE_SIZE, 668 crypto_op_private_data, 669 socket_id); 670 if (crypto_op_pool == NULL) 671 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n"); 672 673 /* Create the virtual crypto device. */ 674 char args[128]; 675 const char *crypto_name = "crypto_aesni_mb0"; 676 snprintf(args, sizeof(args), "socket_id=%d", socket_id); 677 ret = rte_vdev_init(crypto_name, args); 678 if (ret != 0) 679 rte_exit(EXIT_FAILURE, "Cannot create virtual device"); 680 681 uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name); 682 683 /* Get private session data size. */ 684 session_size = rte_cryptodev_sym_get_private_session_size(cdev_id); 685 686 #ifdef USE_TWO_MEMPOOLS 687 /* Create session mempool for the session header. */ 688 session_pool = rte_cryptodev_sym_session_pool_create("session_pool", 689 MAX_SESSIONS, 690 0, 691 POOL_CACHE_SIZE, 692 0, 693 socket_id); 694 695 /* 696 * Create session private data mempool for the 697 * private session data for the crypto device. 698 */ 699 session_priv_pool = rte_mempool_create("session_pool", 700 MAX_SESSIONS, 701 session_size, 702 POOL_CACHE_SIZE, 703 0, NULL, NULL, NULL, 704 NULL, socket_id, 705 0); 706 707 #else 708 /* Use of the same mempool for session header and private data */ 709 session_pool = rte_cryptodev_sym_session_pool_create("session_pool", 710 MAX_SESSIONS * 2, 711 session_size, 712 POOL_CACHE_SIZE, 713 0, 714 socket_id); 715 716 session_priv_pool = session_pool; 717 718 #endif 719 720 /* Configure the crypto device. */ 721 struct rte_cryptodev_config conf = { 722 .nb_queue_pairs = 1, 723 .socket_id = socket_id 724 }; 725 726 struct rte_cryptodev_qp_conf qp_conf = { 727 .nb_descriptors = 2048, 728 .mp_session = session_pool, 729 .mp_session_private = session_priv_pool 730 }; 731 732 if (rte_cryptodev_configure(cdev_id, &conf) < 0) 733 rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id); 734 735 if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, socket_id) < 0) 736 rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n"); 737 738 if (rte_cryptodev_start(cdev_id) < 0) 739 rte_exit(EXIT_FAILURE, "Failed to start device\n"); 740 741 /* Create the crypto transform. */ 742 uint8_t cipher_key[16] = {0}; 743 struct rte_crypto_sym_xform cipher_xform = { 744 .next = NULL, 745 .type = RTE_CRYPTO_SYM_XFORM_CIPHER, 746 .cipher = { 747 .op = RTE_CRYPTO_CIPHER_OP_ENCRYPT, 748 .algo = RTE_CRYPTO_CIPHER_AES_CBC, 749 .key = { 750 .data = cipher_key, 751 .length = AES_CBC_KEY_LENGTH 752 }, 753 .iv = { 754 .offset = IV_OFFSET, 755 .length = AES_CBC_IV_LENGTH 756 } 757 } 758 }; 759 760 /* Create crypto session and initialize it for the crypto device. */ 761 struct rte_cryptodev_sym_session *session; 762 session = rte_cryptodev_sym_session_create(session_pool); 763 if (session == NULL) 764 rte_exit(EXIT_FAILURE, "Session could not be created\n"); 765 766 if (rte_cryptodev_sym_session_init(cdev_id, session, 767 &cipher_xform, session_priv_pool) < 0) 768 rte_exit(EXIT_FAILURE, "Session could not be initialized " 769 "for the crypto device\n"); 770 771 /* Get a burst of crypto operations. */ 772 struct rte_crypto_op *crypto_ops[BURST_SIZE]; 773 if (rte_crypto_op_bulk_alloc(crypto_op_pool, 774 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 775 crypto_ops, BURST_SIZE) == 0) 776 rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n"); 777 778 /* Get a burst of mbufs. */ 779 struct rte_mbuf *mbufs[BURST_SIZE]; 780 if (rte_pktmbuf_alloc_bulk(mbuf_pool, mbufs, BURST_SIZE) < 0) 781 rte_exit(EXIT_FAILURE, "Not enough mbufs available"); 782 783 /* Initialize the mbufs and append them to the crypto operations. */ 784 unsigned int i; 785 for (i = 0; i < BURST_SIZE; i++) { 786 if (rte_pktmbuf_append(mbufs[i], BUFFER_SIZE) == NULL) 787 rte_exit(EXIT_FAILURE, "Not enough room in the mbuf\n"); 788 crypto_ops[i]->sym->m_src = mbufs[i]; 789 } 790 791 /* Set up the crypto operations. */ 792 for (i = 0; i < BURST_SIZE; i++) { 793 struct rte_crypto_op *op = crypto_ops[i]; 794 /* Modify bytes of the IV at the end of the crypto operation */ 795 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *, 796 IV_OFFSET); 797 798 generate_random_bytes(iv_ptr, AES_CBC_IV_LENGTH); 799 800 op->sym->cipher.data.offset = 0; 801 op->sym->cipher.data.length = BUFFER_SIZE; 802 803 /* Attach the crypto session to the operation */ 804 rte_crypto_op_attach_sym_session(op, session); 805 } 806 807 /* Enqueue the crypto operations in the crypto device. */ 808 uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0, 809 crypto_ops, BURST_SIZE); 810 811 /* 812 * Dequeue the crypto operations until all the operations 813 * are processed in the crypto device. 814 */ 815 uint16_t num_dequeued_ops, total_num_dequeued_ops = 0; 816 do { 817 struct rte_crypto_op *dequeued_ops[BURST_SIZE]; 818 num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0, 819 dequeued_ops, BURST_SIZE); 820 total_num_dequeued_ops += num_dequeued_ops; 821 822 /* Check if operation was processed successfully */ 823 for (i = 0; i < num_dequeued_ops; i++) { 824 if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS) 825 rte_exit(EXIT_FAILURE, 826 "Some operations were not processed correctly"); 827 } 828 829 rte_mempool_put_bulk(crypto_op_pool, (void **)dequeued_ops, 830 num_dequeued_ops); 831 } while (total_num_dequeued_ops < num_enqueued_ops); 832 833Asymmetric Cryptography 834----------------------- 835 836The cryptodev library currently provides support for the following asymmetric 837Crypto operations; RSA, Modular exponentiation and inversion, Diffie-Hellman 838public and/or private key generation and shared secret compute, DSA Signature 839generation and verification. 840 841Session and Session Management 842~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 843 844Sessions are used in asymmetric cryptographic processing to store the immutable 845data defined in asymmetric cryptographic transform which is further used in the 846operation processing. Sessions typically stores information, such as, public 847and private key information or domain params or prime modulus data i.e. immutable 848across data sets. Crypto sessions cache this immutable data in a optimal way for the 849underlying PMD and this allows further acceleration of the offload of Crypto workloads. 850 851Like symmetric, the Crypto device framework provides APIs to allocate and initialize 852asymmetric sessions for crypto devices, where sessions are mempool objects. 853It is the application's responsibility to create and manage the session mempools. 854Application using both symmetric and asymmetric sessions should allocate and maintain 855different sessions pools for each type. 856 857An application can use ``rte_cryptodev_get_asym_session_private_size()`` to 858get the private size of asymmetric session on a given crypto device. This 859function would allow an application to calculate the max device asymmetric 860session size of all crypto devices to create a single session mempool. 861If instead an application creates multiple asymmetric session mempools, 862the Crypto device framework also provides ``rte_cryptodev_asym_get_header_session_size()`` to get 863the size of an uninitialized session. 864 865Once the session mempools have been created, ``rte_cryptodev_asym_session_create()`` 866is used to allocate an uninitialized asymmetric session from the given mempool. 867The session then must be initialized using ``rte_cryptodev_asym_session_init()`` 868for each of the required crypto devices. An asymmetric transform chain 869is used to specify the operation and its parameters. See the section below for 870details on transforms. 871 872When a session is no longer used, user must call ``rte_cryptodev_asym_session_clear()`` 873for each of the crypto devices that are using the session, to free all driver 874private asymmetric session data. Once this is done, session should be freed using 875``rte_cryptodev_asym_session_free()`` which returns them to their mempool. 876 877Asymmetric Sessionless Support 878~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 879Currently asymmetric crypto framework does not support sessionless. 880 881Transforms and Transform Chaining 882~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 883 884Asymmetric Crypto transforms (``rte_crypto_asym_xform``) are the mechanism used 885to specify the details of the asymmetric Crypto operation. Next pointer within 886xform allows transform to be chained together. Also it is important to note that 887the order in which the transforms are passed indicates the order of the chaining. Allocation 888of the xform structure is in the the application domain. To allow future API extensions in a 889backwardly compatible manner, e.g. addition of a new parameter, the application should 890zero the full xform struct before populating it. 891 892Not all asymmetric crypto xforms are supported for chaining. Currently supported 893asymmetric crypto chaining is Diffie-Hellman private key generation followed by 894public generation. Also, currently API does not support chaining of symmetric and 895asymmetric crypto xforms. 896 897Each xform defines specific asymmetric crypto algo. Currently supported are: 898* RSA 899* Modular operations (Exponentiation and Inverse) 900* Diffie-Hellman 901* DSA 902* None - special case where PMD may support a passthrough mode. More for diagnostic purpose 903 904See *DPDK API Reference* for details on each rte_crypto_xxx_xform struct 905 906Asymmetric Operations 907~~~~~~~~~~~~~~~~~~~~~ 908 909The asymmetric Crypto operation structure contains all the mutable data relating 910to asymmetric cryptographic processing on an input data buffer. It uses either 911RSA, Modular, Diffie-Hellman or DSA operations depending upon session it is attached 912to. 913 914Every operation must carry a valid session handle which further carries information 915on xform or xform-chain to be performed on op. Every xform type defines its own set 916of operational params in their respective rte_crypto_xxx_op_param struct. Depending 917on xform information within session, PMD picks up and process respective op_param 918struct. 919Unlike symmetric, asymmetric operations do not use mbufs for input/output. 920They operate on data buffer of type ``rte_crypto_param``. 921 922See *DPDK API Reference* for details on each rte_crypto_xxx_op_param struct 923 924Asymmetric crypto Sample code 925----------------------------- 926 927There's a unit test application test_cryptodev_asym.c inside unit test framework that 928show how to setup and process asymmetric operations using cryptodev library. 929 930The following sample code shows the basic steps to compute modular exponentiation 931using 1024-bit modulus length using openssl PMD available in DPDK (performing other 932crypto operations is similar except change to respective op and xform setup). 933 934.. code-block:: c 935 936 /* 937 * Simple example to compute modular exponentiation with 1024-bit key 938 * 939 */ 940 #define MAX_ASYM_SESSIONS 10 941 #define NUM_ASYM_BUFS 10 942 943 struct rte_mempool *crypto_op_pool, *asym_session_pool; 944 unsigned int asym_session_size; 945 int ret; 946 947 /* Initialize EAL. */ 948 ret = rte_eal_init(argc, argv); 949 if (ret < 0) 950 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); 951 952 uint8_t socket_id = rte_socket_id(); 953 954 /* Create crypto operation pool. */ 955 crypto_op_pool = rte_crypto_op_pool_create( 956 "crypto_op_pool", 957 RTE_CRYPTO_OP_TYPE_ASYMMETRIC, 958 NUM_ASYM_BUFS, 0, 0, 959 socket_id); 960 if (crypto_op_pool == NULL) 961 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n"); 962 963 /* Create the virtual crypto device. */ 964 char args[128]; 965 const char *crypto_name = "crypto_openssl"; 966 snprintf(args, sizeof(args), "socket_id=%d", socket_id); 967 ret = rte_vdev_init(crypto_name, args); 968 if (ret != 0) 969 rte_exit(EXIT_FAILURE, "Cannot create virtual device"); 970 971 uint8_t cdev_id = rte_cryptodev_get_dev_id(crypto_name); 972 973 /* Get private asym session data size. */ 974 asym_session_size = rte_cryptodev_get_asym_private_session_size(cdev_id); 975 976 /* 977 * Create session mempool, with two objects per session, 978 * one for the session header and another one for the 979 * private asym session data for the crypto device. 980 */ 981 asym_session_pool = rte_mempool_create("asym_session_pool", 982 MAX_ASYM_SESSIONS * 2, 983 asym_session_size, 984 0, 985 0, NULL, NULL, NULL, 986 NULL, socket_id, 987 0); 988 989 /* Configure the crypto device. */ 990 struct rte_cryptodev_config conf = { 991 .nb_queue_pairs = 1, 992 .socket_id = socket_id 993 }; 994 struct rte_cryptodev_qp_conf qp_conf = { 995 .nb_descriptors = 2048 996 }; 997 998 if (rte_cryptodev_configure(cdev_id, &conf) < 0) 999 rte_exit(EXIT_FAILURE, "Failed to configure cryptodev %u", cdev_id); 1000 1001 if (rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf, 1002 socket_id, asym_session_pool) < 0) 1003 rte_exit(EXIT_FAILURE, "Failed to setup queue pair\n"); 1004 1005 if (rte_cryptodev_start(cdev_id) < 0) 1006 rte_exit(EXIT_FAILURE, "Failed to start device\n"); 1007 1008 /* Setup crypto xform to do modular exponentiation with 1024 bit 1009 * length modulus 1010 */ 1011 struct rte_crypto_asym_xform modex_xform = { 1012 .next = NULL, 1013 .xform_type = RTE_CRYPTO_ASYM_XFORM_MODEX, 1014 .modex = { 1015 .modulus = { 1016 .data = 1017 (uint8_t *) 1018 ("\xb3\xa1\xaf\xb7\x13\x08\x00\x0a\x35\xdc\x2b\x20\x8d" 1019 "\xa1\xb5\xce\x47\x8a\xc3\x80\xf4\x7d\x4a\xa2\x62\xfd\x61\x7f" 1020 "\xb5\xa8\xde\x0a\x17\x97\xa0\xbf\xdf\x56\x5a\x3d\x51\x56\x4f" 1021 "\x70\x70\x3f\x63\x6a\x44\x5b\xad\x84\x0d\x3f\x27\x6e\x3b\x34" 1022 "\x91\x60\x14\xb9\xaa\x72\xfd\xa3\x64\xd2\x03\xa7\x53\x87\x9e" 1023 "\x88\x0b\xc1\x14\x93\x1a\x62\xff\xb1\x5d\x74\xcd\x59\x63\x18" 1024 "\x11\x3d\x4f\xba\x75\xd4\x33\x4e\x23\x6b\x7b\x57\x44\xe1\xd3" 1025 "\x03\x13\xa6\xf0\x8b\x60\xb0\x9e\xee\x75\x08\x9d\x71\x63\x13" 1026 "\xcb\xa6\x81\x92\x14\x03\x22\x2d\xde\x55"), 1027 .length = 128 1028 }, 1029 .exponent = { 1030 .data = (uint8_t *)("\x01\x00\x01"), 1031 .length = 3 1032 } 1033 } 1034 }; 1035 /* Create asym crypto session and initialize it for the crypto device. */ 1036 struct rte_cryptodev_asym_session *asym_session; 1037 asym_session = rte_cryptodev_asym_session_create(asym_session_pool); 1038 if (asym_session == NULL) 1039 rte_exit(EXIT_FAILURE, "Session could not be created\n"); 1040 1041 if (rte_cryptodev_asym_session_init(cdev_id, asym_session, 1042 &modex_xform, asym_session_pool) < 0) 1043 rte_exit(EXIT_FAILURE, "Session could not be initialized " 1044 "for the crypto device\n"); 1045 1046 /* Get a burst of crypto operations. */ 1047 struct rte_crypto_op *crypto_ops[1]; 1048 if (rte_crypto_op_bulk_alloc(crypto_op_pool, 1049 RTE_CRYPTO_OP_TYPE_ASYMMETRIC, 1050 crypto_ops, 1) == 0) 1051 rte_exit(EXIT_FAILURE, "Not enough crypto operations available\n"); 1052 1053 /* Set up the crypto operations. */ 1054 struct rte_crypto_asym_op *asym_op = crypto_ops[0]->asym; 1055 1056 /* calculate mod exp of value 0xf8 */ 1057 static unsigned char base[] = {0xF8}; 1058 asym_op->modex.base.data = base; 1059 asym_op->modex.base.length = sizeof(base); 1060 asym_op->modex.base.iova = base; 1061 1062 /* Attach the asym crypto session to the operation */ 1063 rte_crypto_op_attach_asym_session(op, asym_session); 1064 1065 /* Enqueue the crypto operations in the crypto device. */ 1066 uint16_t num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, 0, 1067 crypto_ops, 1); 1068 1069 /* 1070 * Dequeue the crypto operations until all the operations 1071 * are processed in the crypto device. 1072 */ 1073 uint16_t num_dequeued_ops, total_num_dequeued_ops = 0; 1074 do { 1075 struct rte_crypto_op *dequeued_ops[1]; 1076 num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, 0, 1077 dequeued_ops, 1); 1078 total_num_dequeued_ops += num_dequeued_ops; 1079 1080 /* Check if operation was processed successfully */ 1081 if (dequeued_ops[0]->status != RTE_CRYPTO_OP_STATUS_SUCCESS) 1082 rte_exit(EXIT_FAILURE, 1083 "Some operations were not processed correctly"); 1084 1085 } while (total_num_dequeued_ops < num_enqueued_ops); 1086 1087 1088Asymmetric Crypto Device API 1089~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1090 1091The cryptodev Library API is described in the 1092`DPDK API Reference <http://doc.dpdk.org/api/>`_ 1093