1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 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 15 * distribution. 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. 19 * 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. 31 */ 32 33 #include <string.h> 34 35 #include <rte_common.h> 36 #include <rte_malloc.h> 37 #include <rte_cryptodev_pmd.h> 38 39 #include "rte_openssl_pmd_private.h" 40 41 42 static const struct rte_cryptodev_capabilities openssl_pmd_capabilities[] = { 43 { /* MD5 HMAC */ 44 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 45 {.sym = { 46 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 47 {.auth = { 48 .algo = RTE_CRYPTO_AUTH_MD5_HMAC, 49 .block_size = 64, 50 .key_size = { 51 .min = 1, 52 .max = 64, 53 .increment = 1 54 }, 55 .digest_size = { 56 .min = 16, 57 .max = 16, 58 .increment = 0 59 }, 60 .iv_size = { 0 } 61 }, } 62 }, } 63 }, 64 { /* MD5 */ 65 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 66 {.sym = { 67 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 68 {.auth = { 69 .algo = RTE_CRYPTO_AUTH_MD5, 70 .block_size = 64, 71 .key_size = { 72 .min = 0, 73 .max = 0, 74 .increment = 0 75 }, 76 .digest_size = { 77 .min = 16, 78 .max = 16, 79 .increment = 0 80 }, 81 .iv_size = { 0 } 82 }, } 83 }, } 84 }, 85 { /* SHA1 HMAC */ 86 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 87 {.sym = { 88 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 89 {.auth = { 90 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC, 91 .block_size = 64, 92 .key_size = { 93 .min = 1, 94 .max = 64, 95 .increment = 1 96 }, 97 .digest_size = { 98 .min = 20, 99 .max = 20, 100 .increment = 0 101 }, 102 .iv_size = { 0 } 103 }, } 104 }, } 105 }, 106 { /* SHA1 */ 107 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 108 {.sym = { 109 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 110 {.auth = { 111 .algo = RTE_CRYPTO_AUTH_SHA1, 112 .block_size = 64, 113 .key_size = { 114 .min = 0, 115 .max = 0, 116 .increment = 0 117 }, 118 .digest_size = { 119 .min = 20, 120 .max = 20, 121 .increment = 0 122 }, 123 .iv_size = { 0 } 124 }, } 125 }, } 126 }, 127 { /* SHA224 HMAC */ 128 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 129 {.sym = { 130 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 131 {.auth = { 132 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC, 133 .block_size = 64, 134 .key_size = { 135 .min = 1, 136 .max = 64, 137 .increment = 1 138 }, 139 .digest_size = { 140 .min = 28, 141 .max = 28, 142 .increment = 0 143 }, 144 .iv_size = { 0 } 145 }, } 146 }, } 147 }, 148 { /* SHA224 */ 149 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 150 {.sym = { 151 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 152 {.auth = { 153 .algo = RTE_CRYPTO_AUTH_SHA224, 154 .block_size = 64, 155 .key_size = { 156 .min = 0, 157 .max = 0, 158 .increment = 0 159 }, 160 .digest_size = { 161 .min = 28, 162 .max = 28, 163 .increment = 0 164 }, 165 .iv_size = { 0 } 166 }, } 167 }, } 168 }, 169 { /* SHA256 HMAC */ 170 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 171 {.sym = { 172 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 173 {.auth = { 174 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC, 175 .block_size = 64, 176 .key_size = { 177 .min = 1, 178 .max = 64, 179 .increment = 1 180 }, 181 .digest_size = { 182 .min = 32, 183 .max = 32, 184 .increment = 0 185 }, 186 .iv_size = { 0 } 187 }, } 188 }, } 189 }, 190 { /* SHA256 */ 191 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 192 {.sym = { 193 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 194 {.auth = { 195 .algo = RTE_CRYPTO_AUTH_SHA256, 196 .block_size = 64, 197 .key_size = { 198 .min = 0, 199 .max = 0, 200 .increment = 0 201 }, 202 .digest_size = { 203 .min = 32, 204 .max = 32, 205 .increment = 0 206 }, 207 .iv_size = { 0 } 208 }, } 209 }, } 210 }, 211 { /* SHA384 HMAC */ 212 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 213 {.sym = { 214 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 215 {.auth = { 216 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC, 217 .block_size = 128, 218 .key_size = { 219 .min = 1, 220 .max = 128, 221 .increment = 1 222 }, 223 .digest_size = { 224 .min = 48, 225 .max = 48, 226 .increment = 0 227 }, 228 .iv_size = { 0 } 229 }, } 230 }, } 231 }, 232 { /* SHA384 */ 233 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 234 {.sym = { 235 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 236 {.auth = { 237 .algo = RTE_CRYPTO_AUTH_SHA384, 238 .block_size = 128, 239 .key_size = { 240 .min = 0, 241 .max = 0, 242 .increment = 0 243 }, 244 .digest_size = { 245 .min = 48, 246 .max = 48, 247 .increment = 0 248 }, 249 .iv_size = { 0 } 250 }, } 251 }, } 252 }, 253 { /* SHA512 HMAC */ 254 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 255 {.sym = { 256 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 257 {.auth = { 258 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC, 259 .block_size = 128, 260 .key_size = { 261 .min = 1, 262 .max = 128, 263 .increment = 1 264 }, 265 .digest_size = { 266 .min = 64, 267 .max = 64, 268 .increment = 0 269 }, 270 .iv_size = { 0 } 271 }, } 272 }, } 273 }, 274 { /* SHA512 */ 275 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 276 {.sym = { 277 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 278 {.auth = { 279 .algo = RTE_CRYPTO_AUTH_SHA512, 280 .block_size = 128, 281 .key_size = { 282 .min = 0, 283 .max = 0, 284 .increment = 0 285 }, 286 .digest_size = { 287 .min = 64, 288 .max = 64, 289 .increment = 0 290 }, 291 .iv_size = { 0 } 292 }, } 293 }, } 294 }, 295 { /* AES CBC */ 296 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 297 {.sym = { 298 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 299 {.cipher = { 300 .algo = RTE_CRYPTO_CIPHER_AES_CBC, 301 .block_size = 16, 302 .key_size = { 303 .min = 16, 304 .max = 32, 305 .increment = 8 306 }, 307 .iv_size = { 308 .min = 16, 309 .max = 16, 310 .increment = 0 311 } 312 }, } 313 }, } 314 }, 315 { /* AES CTR */ 316 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 317 {.sym = { 318 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 319 {.cipher = { 320 .algo = RTE_CRYPTO_CIPHER_AES_CTR, 321 .block_size = 16, 322 .key_size = { 323 .min = 16, 324 .max = 32, 325 .increment = 8 326 }, 327 .iv_size = { 328 .min = 16, 329 .max = 16, 330 .increment = 0 331 } 332 }, } 333 }, } 334 }, 335 { /* AES GCM */ 336 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 337 {.sym = { 338 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD, 339 {.aead = { 340 .algo = RTE_CRYPTO_AEAD_AES_GCM, 341 .block_size = 16, 342 .key_size = { 343 .min = 16, 344 .max = 32, 345 .increment = 8 346 }, 347 .digest_size = { 348 .min = 16, 349 .max = 16, 350 .increment = 0 351 }, 352 .aad_size = { 353 .min = 0, 354 .max = 65535, 355 .increment = 1 356 }, 357 .iv_size = { 358 .min = 12, 359 .max = 16, 360 .increment = 4 361 }, 362 }, } 363 }, } 364 }, 365 { /* AES GMAC (AUTH) */ 366 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 367 {.sym = { 368 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 369 {.auth = { 370 .algo = RTE_CRYPTO_AUTH_AES_GMAC, 371 .block_size = 16, 372 .key_size = { 373 .min = 16, 374 .max = 32, 375 .increment = 8 376 }, 377 .digest_size = { 378 .min = 16, 379 .max = 16, 380 .increment = 0 381 }, 382 .iv_size = { 383 .min = 12, 384 .max = 16, 385 .increment = 4 386 } 387 }, } 388 }, } 389 }, 390 { /* 3DES CBC */ 391 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 392 {.sym = { 393 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 394 {.cipher = { 395 .algo = RTE_CRYPTO_CIPHER_3DES_CBC, 396 .block_size = 8, 397 .key_size = { 398 .min = 16, 399 .max = 24, 400 .increment = 8 401 }, 402 .iv_size = { 403 .min = 8, 404 .max = 8, 405 .increment = 0 406 } 407 }, } 408 }, } 409 }, 410 { /* 3DES CTR */ 411 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 412 {.sym = { 413 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 414 {.cipher = { 415 .algo = RTE_CRYPTO_CIPHER_3DES_CTR, 416 .block_size = 8, 417 .key_size = { 418 .min = 16, 419 .max = 24, 420 .increment = 8 421 }, 422 .iv_size = { 423 .min = 8, 424 .max = 8, 425 .increment = 0 426 } 427 }, } 428 }, } 429 }, 430 { /* DES DOCSIS BPI */ 431 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 432 {.sym = { 433 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 434 {.cipher = { 435 .algo = RTE_CRYPTO_CIPHER_DES_DOCSISBPI, 436 .block_size = 8, 437 .key_size = { 438 .min = 8, 439 .max = 8, 440 .increment = 0 441 }, 442 .iv_size = { 443 .min = 8, 444 .max = 8, 445 .increment = 0 446 } 447 }, } 448 }, } 449 }, 450 451 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST() 452 }; 453 454 455 /** Configure device */ 456 static int 457 openssl_pmd_config(__rte_unused struct rte_cryptodev *dev, 458 __rte_unused struct rte_cryptodev_config *config) 459 { 460 return 0; 461 } 462 463 /** Start device */ 464 static int 465 openssl_pmd_start(__rte_unused struct rte_cryptodev *dev) 466 { 467 return 0; 468 } 469 470 /** Stop device */ 471 static void 472 openssl_pmd_stop(__rte_unused struct rte_cryptodev *dev) 473 { 474 } 475 476 /** Close device */ 477 static int 478 openssl_pmd_close(__rte_unused struct rte_cryptodev *dev) 479 { 480 return 0; 481 } 482 483 484 /** Get device statistics */ 485 static void 486 openssl_pmd_stats_get(struct rte_cryptodev *dev, 487 struct rte_cryptodev_stats *stats) 488 { 489 int qp_id; 490 491 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) { 492 struct openssl_qp *qp = dev->data->queue_pairs[qp_id]; 493 494 stats->enqueued_count += qp->stats.enqueued_count; 495 stats->dequeued_count += qp->stats.dequeued_count; 496 497 stats->enqueue_err_count += qp->stats.enqueue_err_count; 498 stats->dequeue_err_count += qp->stats.dequeue_err_count; 499 } 500 } 501 502 /** Reset device statistics */ 503 static void 504 openssl_pmd_stats_reset(struct rte_cryptodev *dev) 505 { 506 int qp_id; 507 508 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) { 509 struct openssl_qp *qp = dev->data->queue_pairs[qp_id]; 510 511 memset(&qp->stats, 0, sizeof(qp->stats)); 512 } 513 } 514 515 516 /** Get device info */ 517 static void 518 openssl_pmd_info_get(struct rte_cryptodev *dev, 519 struct rte_cryptodev_info *dev_info) 520 { 521 struct openssl_private *internals = dev->data->dev_private; 522 523 if (dev_info != NULL) { 524 dev_info->driver_id = dev->driver_id; 525 dev_info->feature_flags = dev->feature_flags; 526 dev_info->capabilities = openssl_pmd_capabilities; 527 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs; 528 dev_info->sym.max_nb_sessions = internals->max_nb_sessions; 529 } 530 } 531 532 /** Release queue pair */ 533 static int 534 openssl_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id) 535 { 536 if (dev->data->queue_pairs[qp_id] != NULL) { 537 rte_free(dev->data->queue_pairs[qp_id]); 538 dev->data->queue_pairs[qp_id] = NULL; 539 } 540 return 0; 541 } 542 543 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */ 544 static int 545 openssl_pmd_qp_set_unique_name(struct rte_cryptodev *dev, 546 struct openssl_qp *qp) 547 { 548 unsigned int n = snprintf(qp->name, sizeof(qp->name), 549 "openssl_pmd_%u_qp_%u", 550 dev->data->dev_id, qp->id); 551 552 if (n > sizeof(qp->name)) 553 return -1; 554 555 return 0; 556 } 557 558 559 /** Create a ring to place processed operations on */ 560 static struct rte_ring * 561 openssl_pmd_qp_create_processed_ops_ring(struct openssl_qp *qp, 562 unsigned int ring_size, int socket_id) 563 { 564 struct rte_ring *r; 565 566 r = rte_ring_lookup(qp->name); 567 if (r) { 568 if (rte_ring_get_size(r) >= ring_size) { 569 OPENSSL_LOG_INFO( 570 "Reusing existing ring %s for processed ops", 571 qp->name); 572 return r; 573 } 574 575 OPENSSL_LOG_ERR( 576 "Unable to reuse existing ring %s for processed ops", 577 qp->name); 578 return NULL; 579 } 580 581 return rte_ring_create(qp->name, ring_size, socket_id, 582 RING_F_SP_ENQ | RING_F_SC_DEQ); 583 } 584 585 586 /** Setup a queue pair */ 587 static int 588 openssl_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id, 589 const struct rte_cryptodev_qp_conf *qp_conf, 590 int socket_id, struct rte_mempool *session_pool) 591 { 592 struct openssl_qp *qp = NULL; 593 594 /* Free memory prior to re-allocation if needed. */ 595 if (dev->data->queue_pairs[qp_id] != NULL) 596 openssl_pmd_qp_release(dev, qp_id); 597 598 /* Allocate the queue pair data structure. */ 599 qp = rte_zmalloc_socket("OPENSSL PMD Queue Pair", sizeof(*qp), 600 RTE_CACHE_LINE_SIZE, socket_id); 601 if (qp == NULL) 602 return -ENOMEM; 603 604 qp->id = qp_id; 605 dev->data->queue_pairs[qp_id] = qp; 606 607 if (openssl_pmd_qp_set_unique_name(dev, qp)) 608 goto qp_setup_cleanup; 609 610 qp->processed_ops = openssl_pmd_qp_create_processed_ops_ring(qp, 611 qp_conf->nb_descriptors, socket_id); 612 if (qp->processed_ops == NULL) 613 goto qp_setup_cleanup; 614 615 qp->sess_mp = session_pool; 616 617 memset(&qp->stats, 0, sizeof(qp->stats)); 618 619 return 0; 620 621 qp_setup_cleanup: 622 if (qp) 623 rte_free(qp); 624 625 return -1; 626 } 627 628 /** Start queue pair */ 629 static int 630 openssl_pmd_qp_start(__rte_unused struct rte_cryptodev *dev, 631 __rte_unused uint16_t queue_pair_id) 632 { 633 return -ENOTSUP; 634 } 635 636 /** Stop queue pair */ 637 static int 638 openssl_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev, 639 __rte_unused uint16_t queue_pair_id) 640 { 641 return -ENOTSUP; 642 } 643 644 /** Return the number of allocated queue pairs */ 645 static uint32_t 646 openssl_pmd_qp_count(struct rte_cryptodev *dev) 647 { 648 return dev->data->nb_queue_pairs; 649 } 650 651 /** Returns the size of the session structure */ 652 static unsigned 653 openssl_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused) 654 { 655 return sizeof(struct openssl_session); 656 } 657 658 /** Configure the session from a crypto xform chain */ 659 static int 660 openssl_pmd_session_configure(struct rte_cryptodev *dev __rte_unused, 661 struct rte_crypto_sym_xform *xform, 662 struct rte_cryptodev_sym_session *sess, 663 struct rte_mempool *mempool) 664 { 665 void *sess_private_data; 666 int ret; 667 668 if (unlikely(sess == NULL)) { 669 OPENSSL_LOG_ERR("invalid session struct"); 670 return -EINVAL; 671 } 672 673 if (rte_mempool_get(mempool, &sess_private_data)) { 674 CDEV_LOG_ERR( 675 "Couldn't get object from session mempool"); 676 return -ENOMEM; 677 } 678 679 ret = openssl_set_session_parameters(sess_private_data, xform); 680 if (ret != 0) { 681 OPENSSL_LOG_ERR("failed configure session parameters"); 682 683 /* Return session to mempool */ 684 rte_mempool_put(mempool, sess_private_data); 685 return ret; 686 } 687 688 set_session_private_data(sess, dev->driver_id, 689 sess_private_data); 690 691 return 0; 692 } 693 694 695 /** Clear the memory of session so it doesn't leave key material behind */ 696 static void 697 openssl_pmd_session_clear(struct rte_cryptodev *dev, 698 struct rte_cryptodev_sym_session *sess) 699 { 700 uint8_t index = dev->driver_id; 701 void *sess_priv = get_session_private_data(sess, index); 702 703 /* Zero out the whole structure */ 704 if (sess_priv) { 705 openssl_reset_session(sess_priv); 706 memset(sess_priv, 0, sizeof(struct openssl_session)); 707 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv); 708 set_session_private_data(sess, index, NULL); 709 rte_mempool_put(sess_mp, sess_priv); 710 } 711 } 712 713 struct rte_cryptodev_ops openssl_pmd_ops = { 714 .dev_configure = openssl_pmd_config, 715 .dev_start = openssl_pmd_start, 716 .dev_stop = openssl_pmd_stop, 717 .dev_close = openssl_pmd_close, 718 719 .stats_get = openssl_pmd_stats_get, 720 .stats_reset = openssl_pmd_stats_reset, 721 722 .dev_infos_get = openssl_pmd_info_get, 723 724 .queue_pair_setup = openssl_pmd_qp_setup, 725 .queue_pair_release = openssl_pmd_qp_release, 726 .queue_pair_start = openssl_pmd_qp_start, 727 .queue_pair_stop = openssl_pmd_qp_stop, 728 .queue_pair_count = openssl_pmd_qp_count, 729 730 .session_get_size = openssl_pmd_session_get_size, 731 .session_configure = openssl_pmd_session_configure, 732 .session_clear = openssl_pmd_session_clear 733 }; 734 735 struct rte_cryptodev_ops *rte_openssl_pmd_ops = &openssl_pmd_ops; 736