1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2016 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 <rte_common.h> 34 #include <rte_hexdump.h> 35 #include <rte_cryptodev.h> 36 #include <rte_cryptodev_pmd.h> 37 #include <rte_vdev.h> 38 #include <rte_malloc.h> 39 #include <rte_cpuflags.h> 40 41 #include <openssl/evp.h> 42 43 #include "rte_openssl_pmd_private.h" 44 45 static int cryptodev_openssl_remove(const char *name); 46 47 /*----------------------------------------------------------------------------*/ 48 49 /** 50 * Global static parameter used to create a unique name for each 51 * OPENSSL crypto device. 52 */ 53 static unsigned int unique_name_id; 54 55 static inline int 56 create_unique_device_name(char *name, size_t size) 57 { 58 int ret; 59 60 if (name == NULL) 61 return -EINVAL; 62 63 ret = snprintf(name, size, "%s_%u", 64 RTE_STR(CRYPTODEV_NAME_OPENSSL_PMD), 65 unique_name_id++); 66 if (ret < 0) 67 return ret; 68 return 0; 69 } 70 71 /** 72 * Increment counter by 1 73 * Counter is 64 bit array, big-endian 74 */ 75 static void 76 ctr_inc(uint8_t *ctr) 77 { 78 uint64_t *ctr64 = (uint64_t *)ctr; 79 80 *ctr64 = __builtin_bswap64(*ctr64); 81 (*ctr64)++; 82 *ctr64 = __builtin_bswap64(*ctr64); 83 } 84 85 /* 86 *------------------------------------------------------------------------------ 87 * Session Prepare 88 *------------------------------------------------------------------------------ 89 */ 90 91 /** Get xform chain order */ 92 static enum openssl_chain_order 93 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform) 94 { 95 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED; 96 97 if (xform != NULL) { 98 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) { 99 if (xform->next == NULL) 100 res = OPENSSL_CHAIN_ONLY_AUTH; 101 else if (xform->next->type == 102 RTE_CRYPTO_SYM_XFORM_CIPHER) 103 res = OPENSSL_CHAIN_AUTH_CIPHER; 104 } 105 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) { 106 if (xform->next == NULL) 107 res = OPENSSL_CHAIN_ONLY_CIPHER; 108 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) 109 res = OPENSSL_CHAIN_CIPHER_AUTH; 110 } 111 } 112 113 return res; 114 } 115 116 /** Get session cipher key from input cipher key */ 117 static void 118 get_cipher_key(uint8_t *input_key, int keylen, uint8_t *session_key) 119 { 120 memcpy(session_key, input_key, keylen); 121 } 122 123 /** Get key ede 24 bytes standard from input key */ 124 static int 125 get_cipher_key_ede(uint8_t *key, int keylen, uint8_t *key_ede) 126 { 127 int res = 0; 128 129 /* Initialize keys - 24 bytes: [key1-key2-key3] */ 130 switch (keylen) { 131 case 24: 132 memcpy(key_ede, key, 24); 133 break; 134 case 16: 135 /* K3 = K1 */ 136 memcpy(key_ede, key, 16); 137 memcpy(key_ede + 16, key, 8); 138 break; 139 case 8: 140 /* K1 = K2 = K3 (DES compatibility) */ 141 memcpy(key_ede, key, 8); 142 memcpy(key_ede + 8, key, 8); 143 memcpy(key_ede + 16, key, 8); 144 break; 145 default: 146 OPENSSL_LOG_ERR("Unsupported key size"); 147 res = -EINVAL; 148 } 149 150 return res; 151 } 152 153 /** Get adequate openssl function for input cipher algorithm */ 154 static uint8_t 155 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen, 156 const EVP_CIPHER **algo) 157 { 158 int res = 0; 159 160 if (algo != NULL) { 161 switch (sess_algo) { 162 case RTE_CRYPTO_CIPHER_3DES_CBC: 163 switch (keylen) { 164 case 16: 165 *algo = EVP_des_ede_cbc(); 166 break; 167 case 24: 168 *algo = EVP_des_ede3_cbc(); 169 break; 170 default: 171 res = -EINVAL; 172 } 173 break; 174 case RTE_CRYPTO_CIPHER_3DES_CTR: 175 break; 176 case RTE_CRYPTO_CIPHER_AES_CBC: 177 switch (keylen) { 178 case 16: 179 *algo = EVP_aes_128_cbc(); 180 break; 181 case 24: 182 *algo = EVP_aes_192_cbc(); 183 break; 184 case 32: 185 *algo = EVP_aes_256_cbc(); 186 break; 187 default: 188 res = -EINVAL; 189 } 190 break; 191 case RTE_CRYPTO_CIPHER_AES_CTR: 192 switch (keylen) { 193 case 16: 194 *algo = EVP_aes_128_ctr(); 195 break; 196 case 24: 197 *algo = EVP_aes_192_ctr(); 198 break; 199 case 32: 200 *algo = EVP_aes_256_ctr(); 201 break; 202 default: 203 res = -EINVAL; 204 } 205 break; 206 case RTE_CRYPTO_CIPHER_AES_GCM: 207 switch (keylen) { 208 case 16: 209 *algo = EVP_aes_128_gcm(); 210 break; 211 case 24: 212 *algo = EVP_aes_192_gcm(); 213 break; 214 case 32: 215 *algo = EVP_aes_256_gcm(); 216 break; 217 default: 218 res = -EINVAL; 219 } 220 break; 221 default: 222 res = -EINVAL; 223 break; 224 } 225 } else { 226 res = -EINVAL; 227 } 228 229 return res; 230 } 231 232 /** Get adequate openssl function for input auth algorithm */ 233 static uint8_t 234 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo, 235 const EVP_MD **algo) 236 { 237 int res = 0; 238 239 if (algo != NULL) { 240 switch (sessalgo) { 241 case RTE_CRYPTO_AUTH_MD5: 242 case RTE_CRYPTO_AUTH_MD5_HMAC: 243 *algo = EVP_md5(); 244 break; 245 case RTE_CRYPTO_AUTH_SHA1: 246 case RTE_CRYPTO_AUTH_SHA1_HMAC: 247 *algo = EVP_sha1(); 248 break; 249 case RTE_CRYPTO_AUTH_SHA224: 250 case RTE_CRYPTO_AUTH_SHA224_HMAC: 251 *algo = EVP_sha224(); 252 break; 253 case RTE_CRYPTO_AUTH_SHA256: 254 case RTE_CRYPTO_AUTH_SHA256_HMAC: 255 *algo = EVP_sha256(); 256 break; 257 case RTE_CRYPTO_AUTH_SHA384: 258 case RTE_CRYPTO_AUTH_SHA384_HMAC: 259 *algo = EVP_sha384(); 260 break; 261 case RTE_CRYPTO_AUTH_SHA512: 262 case RTE_CRYPTO_AUTH_SHA512_HMAC: 263 *algo = EVP_sha512(); 264 break; 265 default: 266 res = -EINVAL; 267 break; 268 } 269 } else { 270 res = -EINVAL; 271 } 272 273 return res; 274 } 275 276 /** Set session cipher parameters */ 277 static int 278 openssl_set_session_cipher_parameters(struct openssl_session *sess, 279 const struct rte_crypto_sym_xform *xform) 280 { 281 /* Select cipher direction */ 282 sess->cipher.direction = xform->cipher.op; 283 /* Select cipher key */ 284 sess->cipher.key.length = xform->cipher.key.length; 285 286 /* Select cipher algo */ 287 switch (xform->cipher.algo) { 288 case RTE_CRYPTO_CIPHER_3DES_CBC: 289 case RTE_CRYPTO_CIPHER_AES_CBC: 290 case RTE_CRYPTO_CIPHER_AES_CTR: 291 case RTE_CRYPTO_CIPHER_AES_GCM: 292 sess->cipher.mode = OPENSSL_CIPHER_LIB; 293 sess->cipher.algo = xform->cipher.algo; 294 sess->cipher.ctx = EVP_CIPHER_CTX_new(); 295 296 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length, 297 &sess->cipher.evp_algo) != 0) 298 return -EINVAL; 299 300 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length, 301 sess->cipher.key.data); 302 303 break; 304 305 case RTE_CRYPTO_CIPHER_3DES_CTR: 306 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR; 307 sess->cipher.ctx = EVP_CIPHER_CTX_new(); 308 309 if (get_cipher_key_ede(xform->cipher.key.data, 310 sess->cipher.key.length, 311 sess->cipher.key.data) != 0) 312 return -EINVAL; 313 break; 314 315 default: 316 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL; 317 return -EINVAL; 318 } 319 320 return 0; 321 } 322 323 /* Set session auth parameters */ 324 static int 325 openssl_set_session_auth_parameters(struct openssl_session *sess, 326 const struct rte_crypto_sym_xform *xform) 327 { 328 /* Select auth generate/verify */ 329 sess->auth.operation = xform->auth.op; 330 sess->auth.algo = xform->auth.algo; 331 332 /* Select auth algo */ 333 switch (xform->auth.algo) { 334 case RTE_CRYPTO_AUTH_AES_GMAC: 335 case RTE_CRYPTO_AUTH_AES_GCM: 336 /* Check additional condition for AES_GMAC/GCM */ 337 if (sess->cipher.algo != RTE_CRYPTO_CIPHER_AES_GCM) 338 return -EINVAL; 339 sess->chain_order = OPENSSL_CHAIN_COMBINED; 340 break; 341 342 case RTE_CRYPTO_AUTH_MD5: 343 case RTE_CRYPTO_AUTH_SHA1: 344 case RTE_CRYPTO_AUTH_SHA224: 345 case RTE_CRYPTO_AUTH_SHA256: 346 case RTE_CRYPTO_AUTH_SHA384: 347 case RTE_CRYPTO_AUTH_SHA512: 348 sess->auth.mode = OPENSSL_AUTH_AS_AUTH; 349 if (get_auth_algo(xform->auth.algo, 350 &sess->auth.auth.evp_algo) != 0) 351 return -EINVAL; 352 sess->auth.auth.ctx = EVP_MD_CTX_create(); 353 break; 354 355 case RTE_CRYPTO_AUTH_MD5_HMAC: 356 case RTE_CRYPTO_AUTH_SHA1_HMAC: 357 case RTE_CRYPTO_AUTH_SHA224_HMAC: 358 case RTE_CRYPTO_AUTH_SHA256_HMAC: 359 case RTE_CRYPTO_AUTH_SHA384_HMAC: 360 case RTE_CRYPTO_AUTH_SHA512_HMAC: 361 sess->auth.mode = OPENSSL_AUTH_AS_HMAC; 362 sess->auth.hmac.ctx = EVP_MD_CTX_create(); 363 if (get_auth_algo(xform->auth.algo, 364 &sess->auth.hmac.evp_algo) != 0) 365 return -EINVAL; 366 sess->auth.hmac.pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, 367 xform->auth.key.data, xform->auth.key.length); 368 break; 369 370 default: 371 return -EINVAL; 372 } 373 374 return 0; 375 } 376 377 /** Parse crypto xform chain and set private session parameters */ 378 int 379 openssl_set_session_parameters(struct openssl_session *sess, 380 const struct rte_crypto_sym_xform *xform) 381 { 382 const struct rte_crypto_sym_xform *cipher_xform = NULL; 383 const struct rte_crypto_sym_xform *auth_xform = NULL; 384 385 sess->chain_order = openssl_get_chain_order(xform); 386 switch (sess->chain_order) { 387 case OPENSSL_CHAIN_ONLY_CIPHER: 388 cipher_xform = xform; 389 break; 390 case OPENSSL_CHAIN_ONLY_AUTH: 391 auth_xform = xform; 392 break; 393 case OPENSSL_CHAIN_CIPHER_AUTH: 394 cipher_xform = xform; 395 auth_xform = xform->next; 396 break; 397 case OPENSSL_CHAIN_AUTH_CIPHER: 398 auth_xform = xform; 399 cipher_xform = xform->next; 400 break; 401 default: 402 return -EINVAL; 403 } 404 405 /* cipher_xform must be check before auth_xform */ 406 if (cipher_xform) { 407 if (openssl_set_session_cipher_parameters( 408 sess, cipher_xform)) { 409 OPENSSL_LOG_ERR( 410 "Invalid/unsupported cipher parameters"); 411 return -EINVAL; 412 } 413 } 414 415 if (auth_xform) { 416 if (openssl_set_session_auth_parameters(sess, auth_xform)) { 417 OPENSSL_LOG_ERR( 418 "Invalid/unsupported auth parameters"); 419 return -EINVAL; 420 } 421 } 422 423 return 0; 424 } 425 426 /** Reset private session parameters */ 427 void 428 openssl_reset_session(struct openssl_session *sess) 429 { 430 EVP_CIPHER_CTX_free(sess->cipher.ctx); 431 432 switch (sess->auth.mode) { 433 case OPENSSL_AUTH_AS_AUTH: 434 EVP_MD_CTX_destroy(sess->auth.auth.ctx); 435 break; 436 case OPENSSL_AUTH_AS_HMAC: 437 EVP_PKEY_free(sess->auth.hmac.pkey); 438 EVP_MD_CTX_destroy(sess->auth.hmac.ctx); 439 break; 440 default: 441 break; 442 } 443 } 444 445 /** Provide session for operation */ 446 static struct openssl_session * 447 get_session(struct openssl_qp *qp, struct rte_crypto_op *op) 448 { 449 struct openssl_session *sess = NULL; 450 451 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) { 452 /* get existing session */ 453 if (likely(op->sym->session != NULL && 454 op->sym->session->dev_type == 455 RTE_CRYPTODEV_OPENSSL_PMD)) 456 sess = (struct openssl_session *) 457 op->sym->session->_private; 458 } else { 459 /* provide internal session */ 460 void *_sess = NULL; 461 462 if (!rte_mempool_get(qp->sess_mp, (void **)&_sess)) { 463 sess = (struct openssl_session *) 464 ((struct rte_cryptodev_sym_session *)_sess) 465 ->_private; 466 467 if (unlikely(openssl_set_session_parameters( 468 sess, op->sym->xform) != 0)) { 469 rte_mempool_put(qp->sess_mp, _sess); 470 sess = NULL; 471 } else 472 op->sym->session = _sess; 473 } 474 } 475 476 if (sess == NULL) 477 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION; 478 479 return sess; 480 } 481 482 /* 483 *------------------------------------------------------------------------------ 484 * Process Operations 485 *------------------------------------------------------------------------------ 486 */ 487 static inline int 488 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset, 489 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx) 490 { 491 struct rte_mbuf *m; 492 int dstlen; 493 int l, n = srclen; 494 uint8_t *src; 495 496 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m); 497 m = m->next) 498 offset -= rte_pktmbuf_data_len(m); 499 500 if (m == 0) 501 return -1; 502 503 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset); 504 505 l = rte_pktmbuf_data_len(m) - offset; 506 if (srclen <= l) { 507 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0) 508 return -1; 509 *dst += l; 510 return 0; 511 } 512 513 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0) 514 return -1; 515 516 *dst += dstlen; 517 n -= l; 518 519 for (m = m->next; (m != NULL) && (n > 0); m = m->next) { 520 src = rte_pktmbuf_mtod(m, uint8_t *); 521 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n; 522 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0) 523 return -1; 524 *dst += dstlen; 525 n -= l; 526 } 527 528 return 0; 529 } 530 531 static inline int 532 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset, 533 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx) 534 { 535 struct rte_mbuf *m; 536 int dstlen; 537 int l, n = srclen; 538 uint8_t *src; 539 540 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m); 541 m = m->next) 542 offset -= rte_pktmbuf_data_len(m); 543 544 if (m == 0) 545 return -1; 546 547 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset); 548 549 l = rte_pktmbuf_data_len(m) - offset; 550 if (srclen <= l) { 551 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0) 552 return -1; 553 *dst += l; 554 return 0; 555 } 556 557 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0) 558 return -1; 559 560 *dst += dstlen; 561 n -= l; 562 563 for (m = m->next; (m != NULL) && (n > 0); m = m->next) { 564 src = rte_pktmbuf_mtod(m, uint8_t *); 565 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n; 566 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0) 567 return -1; 568 *dst += dstlen; 569 n -= l; 570 } 571 572 return 0; 573 } 574 575 /** Process standard openssl cipher encryption */ 576 static int 577 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst, 578 int offset, uint8_t *iv, uint8_t *key, int srclen, 579 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo) 580 { 581 int totlen; 582 583 if (EVP_EncryptInit_ex(ctx, algo, NULL, key, iv) <= 0) 584 goto process_cipher_encrypt_err; 585 586 EVP_CIPHER_CTX_set_padding(ctx, 0); 587 588 if (process_openssl_encryption_update(mbuf_src, offset, &dst, 589 srclen, ctx)) 590 goto process_cipher_encrypt_err; 591 592 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0) 593 goto process_cipher_encrypt_err; 594 595 return 0; 596 597 process_cipher_encrypt_err: 598 OPENSSL_LOG_ERR("Process openssl cipher encrypt failed"); 599 return -EINVAL; 600 } 601 602 /** Process standard openssl cipher decryption */ 603 static int 604 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst, 605 int offset, uint8_t *iv, uint8_t *key, int srclen, 606 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo) 607 { 608 int totlen; 609 610 if (EVP_DecryptInit_ex(ctx, algo, NULL, key, iv) <= 0) 611 goto process_cipher_decrypt_err; 612 613 EVP_CIPHER_CTX_set_padding(ctx, 0); 614 615 if (process_openssl_decryption_update(mbuf_src, offset, &dst, 616 srclen, ctx)) 617 goto process_cipher_decrypt_err; 618 619 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0) 620 goto process_cipher_decrypt_err; 621 return 0; 622 623 process_cipher_decrypt_err: 624 OPENSSL_LOG_ERR("Process openssl cipher decrypt failed"); 625 return -EINVAL; 626 } 627 628 /** Process cipher des 3 ctr encryption, decryption algorithm */ 629 static int 630 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst, 631 int offset, uint8_t *iv, uint8_t *key, int srclen, 632 EVP_CIPHER_CTX *ctx) 633 { 634 uint8_t ebuf[8], ctr[8]; 635 int unused, n; 636 struct rte_mbuf *m; 637 uint8_t *src; 638 int l; 639 640 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m); 641 m = m->next) 642 offset -= rte_pktmbuf_data_len(m); 643 644 if (m == 0) 645 goto process_cipher_des3ctr_err; 646 647 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset); 648 l = rte_pktmbuf_data_len(m) - offset; 649 650 /* We use 3DES encryption also for decryption. 651 * IV is not important for 3DES ecb 652 */ 653 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0) 654 goto process_cipher_des3ctr_err; 655 656 memcpy(ctr, iv, 8); 657 658 for (n = 0; n < srclen; n++) { 659 if (n % 8 == 0) { 660 if (EVP_EncryptUpdate(ctx, 661 (unsigned char *)&ebuf, &unused, 662 (const unsigned char *)&ctr, 8) <= 0) 663 goto process_cipher_des3ctr_err; 664 ctr_inc(ctr); 665 } 666 dst[n] = *(src++) ^ ebuf[n % 8]; 667 668 l--; 669 if (!l) { 670 m = m->next; 671 if (m) { 672 src = rte_pktmbuf_mtod(m, uint8_t *); 673 l = rte_pktmbuf_data_len(m); 674 } 675 } 676 } 677 678 return 0; 679 680 process_cipher_des3ctr_err: 681 OPENSSL_LOG_ERR("Process openssl cipher des 3 ede ctr failed"); 682 return -EINVAL; 683 } 684 685 /** Process auth/encription aes-gcm algorithm */ 686 static int 687 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset, 688 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen, 689 uint8_t *key, uint8_t *dst, uint8_t *tag, 690 EVP_CIPHER_CTX *ctx, const EVP_CIPHER *algo) 691 { 692 int len = 0, unused = 0; 693 uint8_t empty[] = {}; 694 695 if (EVP_EncryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0) 696 goto process_auth_encryption_gcm_err; 697 698 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0) 699 goto process_auth_encryption_gcm_err; 700 701 if (EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv) <= 0) 702 goto process_auth_encryption_gcm_err; 703 704 if (aadlen > 0) 705 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0) 706 goto process_auth_encryption_gcm_err; 707 708 if (srclen > 0) 709 if (process_openssl_encryption_update(mbuf_src, offset, &dst, 710 srclen, ctx)) 711 goto process_auth_encryption_gcm_err; 712 713 /* Workaround open ssl bug in version less then 1.0.1f */ 714 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0) 715 goto process_auth_encryption_gcm_err; 716 717 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0) 718 goto process_auth_encryption_gcm_err; 719 720 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0) 721 goto process_auth_encryption_gcm_err; 722 723 return 0; 724 725 process_auth_encryption_gcm_err: 726 OPENSSL_LOG_ERR("Process openssl auth encryption gcm failed"); 727 return -EINVAL; 728 } 729 730 static int 731 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset, 732 int srclen, uint8_t *aad, int aadlen, uint8_t *iv, int ivlen, 733 uint8_t *key, uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx, 734 const EVP_CIPHER *algo) 735 { 736 int len = 0, unused = 0; 737 uint8_t empty[] = {}; 738 739 if (EVP_DecryptInit_ex(ctx, algo, NULL, NULL, NULL) <= 0) 740 goto process_auth_decryption_gcm_err; 741 742 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, NULL) <= 0) 743 goto process_auth_decryption_gcm_err; 744 745 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0) 746 goto process_auth_decryption_gcm_err; 747 748 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) <= 0) 749 goto process_auth_decryption_gcm_err; 750 751 if (aadlen > 0) 752 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0) 753 goto process_auth_decryption_gcm_err; 754 755 if (srclen > 0) 756 if (process_openssl_decryption_update(mbuf_src, offset, &dst, 757 srclen, ctx)) 758 goto process_auth_decryption_gcm_err; 759 760 /* Workaround open ssl bug in version less then 1.0.1f */ 761 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0) 762 goto process_auth_decryption_gcm_err; 763 764 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0) 765 goto process_auth_decryption_gcm_final_err; 766 767 return 0; 768 769 process_auth_decryption_gcm_err: 770 OPENSSL_LOG_ERR("Process openssl auth description gcm failed"); 771 return -EINVAL; 772 773 process_auth_decryption_gcm_final_err: 774 return -EFAULT; 775 } 776 777 /** Process standard openssl auth algorithms */ 778 static int 779 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset, 780 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey, 781 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo) 782 { 783 size_t dstlen; 784 struct rte_mbuf *m; 785 int l, n = srclen; 786 uint8_t *src; 787 788 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m); 789 m = m->next) 790 offset -= rte_pktmbuf_data_len(m); 791 792 if (m == 0) 793 goto process_auth_err; 794 795 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0) 796 goto process_auth_err; 797 798 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset); 799 800 l = rte_pktmbuf_data_len(m) - offset; 801 if (srclen <= l) { 802 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0) 803 goto process_auth_err; 804 goto process_auth_final; 805 } 806 807 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0) 808 goto process_auth_err; 809 810 n -= l; 811 812 for (m = m->next; (m != NULL) && (n > 0); m = m->next) { 813 src = rte_pktmbuf_mtod(m, uint8_t *); 814 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n; 815 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0) 816 goto process_auth_err; 817 n -= l; 818 } 819 820 process_auth_final: 821 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0) 822 goto process_auth_err; 823 return 0; 824 825 process_auth_err: 826 OPENSSL_LOG_ERR("Process openssl auth failed"); 827 return -EINVAL; 828 } 829 830 /** Process standard openssl auth algorithms with hmac */ 831 static int 832 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset, 833 __rte_unused uint8_t *iv, EVP_PKEY *pkey, 834 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo) 835 { 836 size_t dstlen; 837 struct rte_mbuf *m; 838 int l, n = srclen; 839 uint8_t *src; 840 841 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m); 842 m = m->next) 843 offset -= rte_pktmbuf_data_len(m); 844 845 if (m == 0) 846 goto process_auth_err; 847 848 if (EVP_DigestSignInit(ctx, NULL, algo, NULL, pkey) <= 0) 849 goto process_auth_err; 850 851 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset); 852 853 l = rte_pktmbuf_data_len(m) - offset; 854 if (srclen <= l) { 855 if (EVP_DigestSignUpdate(ctx, (char *)src, srclen) <= 0) 856 goto process_auth_err; 857 goto process_auth_final; 858 } 859 860 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0) 861 goto process_auth_err; 862 863 n -= l; 864 865 for (m = m->next; (m != NULL) && (n > 0); m = m->next) { 866 src = rte_pktmbuf_mtod(m, uint8_t *); 867 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n; 868 if (EVP_DigestSignUpdate(ctx, (char *)src, l) <= 0) 869 goto process_auth_err; 870 n -= l; 871 } 872 873 process_auth_final: 874 if (EVP_DigestSignFinal(ctx, dst, &dstlen) <= 0) 875 goto process_auth_err; 876 877 return 0; 878 879 process_auth_err: 880 OPENSSL_LOG_ERR("Process openssl auth failed"); 881 return -EINVAL; 882 } 883 884 /*----------------------------------------------------------------------------*/ 885 886 /** Process auth/cipher combined operation */ 887 static void 888 process_openssl_combined_op 889 (struct rte_crypto_op *op, struct openssl_session *sess, 890 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst) 891 { 892 /* cipher */ 893 uint8_t *dst = NULL, *iv, *tag, *aad; 894 int srclen, ivlen, aadlen, status = -1; 895 896 /* 897 * Segmented destination buffer is not supported for 898 * encryption/decryption 899 */ 900 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) { 901 op->status = RTE_CRYPTO_OP_STATUS_ERROR; 902 return; 903 } 904 905 iv = op->sym->cipher.iv.data; 906 ivlen = op->sym->cipher.iv.length; 907 aad = op->sym->auth.aad.data; 908 aadlen = op->sym->auth.aad.length; 909 910 tag = op->sym->auth.digest.data; 911 if (tag == NULL) 912 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *, 913 op->sym->cipher.data.offset + 914 op->sym->cipher.data.length); 915 916 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) 917 srclen = 0; 918 else { 919 srclen = op->sym->cipher.data.length; 920 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *, 921 op->sym->cipher.data.offset); 922 } 923 924 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) 925 status = process_openssl_auth_encryption_gcm( 926 mbuf_src, op->sym->cipher.data.offset, srclen, 927 aad, aadlen, iv, ivlen, sess->cipher.key.data, 928 dst, tag, sess->cipher.ctx, 929 sess->cipher.evp_algo); 930 else 931 status = process_openssl_auth_decryption_gcm( 932 mbuf_src, op->sym->cipher.data.offset, srclen, 933 aad, aadlen, iv, ivlen, sess->cipher.key.data, 934 dst, tag, sess->cipher.ctx, 935 sess->cipher.evp_algo); 936 937 if (status != 0) { 938 if (status == (-EFAULT) && 939 sess->auth.operation == 940 RTE_CRYPTO_AUTH_OP_VERIFY) 941 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED; 942 else 943 op->status = RTE_CRYPTO_OP_STATUS_ERROR; 944 } 945 } 946 947 /** Process cipher operation */ 948 static void 949 process_openssl_cipher_op 950 (struct rte_crypto_op *op, struct openssl_session *sess, 951 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst) 952 { 953 uint8_t *dst, *iv; 954 int srclen, status; 955 956 /* 957 * Segmented destination buffer is not supported for 958 * encryption/decryption 959 */ 960 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) { 961 op->status = RTE_CRYPTO_OP_STATUS_ERROR; 962 return; 963 } 964 965 srclen = op->sym->cipher.data.length; 966 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *, 967 op->sym->cipher.data.offset); 968 969 iv = op->sym->cipher.iv.data; 970 971 if (sess->cipher.mode == OPENSSL_CIPHER_LIB) 972 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) 973 status = process_openssl_cipher_encrypt(mbuf_src, dst, 974 op->sym->cipher.data.offset, iv, 975 sess->cipher.key.data, srclen, 976 sess->cipher.ctx, 977 sess->cipher.evp_algo); 978 else 979 status = process_openssl_cipher_decrypt(mbuf_src, dst, 980 op->sym->cipher.data.offset, iv, 981 sess->cipher.key.data, srclen, 982 sess->cipher.ctx, 983 sess->cipher.evp_algo); 984 else 985 status = process_openssl_cipher_des3ctr(mbuf_src, dst, 986 op->sym->cipher.data.offset, iv, 987 sess->cipher.key.data, srclen, 988 sess->cipher.ctx); 989 990 if (status != 0) 991 op->status = RTE_CRYPTO_OP_STATUS_ERROR; 992 } 993 994 /** Process auth operation */ 995 static void 996 process_openssl_auth_op 997 (struct rte_crypto_op *op, struct openssl_session *sess, 998 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst) 999 { 1000 uint8_t *dst; 1001 int srclen, status; 1002 1003 srclen = op->sym->auth.data.length; 1004 1005 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) 1006 dst = (uint8_t *)rte_pktmbuf_append(mbuf_src, 1007 op->sym->auth.digest.length); 1008 else { 1009 dst = op->sym->auth.digest.data; 1010 if (dst == NULL) 1011 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *, 1012 op->sym->auth.data.offset + 1013 op->sym->auth.data.length); 1014 } 1015 1016 switch (sess->auth.mode) { 1017 case OPENSSL_AUTH_AS_AUTH: 1018 status = process_openssl_auth(mbuf_src, dst, 1019 op->sym->auth.data.offset, NULL, NULL, srclen, 1020 sess->auth.auth.ctx, sess->auth.auth.evp_algo); 1021 break; 1022 case OPENSSL_AUTH_AS_HMAC: 1023 status = process_openssl_auth_hmac(mbuf_src, dst, 1024 op->sym->auth.data.offset, NULL, 1025 sess->auth.hmac.pkey, srclen, 1026 sess->auth.hmac.ctx, sess->auth.hmac.evp_algo); 1027 break; 1028 default: 1029 status = -1; 1030 break; 1031 } 1032 1033 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) { 1034 if (memcmp(dst, op->sym->auth.digest.data, 1035 op->sym->auth.digest.length) != 0) { 1036 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED; 1037 } 1038 /* Trim area used for digest from mbuf. */ 1039 rte_pktmbuf_trim(mbuf_src, op->sym->auth.digest.length); 1040 } 1041 1042 if (status != 0) 1043 op->status = RTE_CRYPTO_OP_STATUS_ERROR; 1044 } 1045 1046 /** Process crypto operation for mbuf */ 1047 static int 1048 process_op(const struct openssl_qp *qp, struct rte_crypto_op *op, 1049 struct openssl_session *sess) 1050 { 1051 struct rte_mbuf *msrc, *mdst; 1052 int retval; 1053 1054 msrc = op->sym->m_src; 1055 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src; 1056 1057 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 1058 1059 switch (sess->chain_order) { 1060 case OPENSSL_CHAIN_ONLY_CIPHER: 1061 process_openssl_cipher_op(op, sess, msrc, mdst); 1062 break; 1063 case OPENSSL_CHAIN_ONLY_AUTH: 1064 process_openssl_auth_op(op, sess, msrc, mdst); 1065 break; 1066 case OPENSSL_CHAIN_CIPHER_AUTH: 1067 process_openssl_cipher_op(op, sess, msrc, mdst); 1068 process_openssl_auth_op(op, sess, mdst, mdst); 1069 break; 1070 case OPENSSL_CHAIN_AUTH_CIPHER: 1071 process_openssl_auth_op(op, sess, msrc, mdst); 1072 process_openssl_cipher_op(op, sess, msrc, mdst); 1073 break; 1074 case OPENSSL_CHAIN_COMBINED: 1075 process_openssl_combined_op(op, sess, msrc, mdst); 1076 break; 1077 default: 1078 op->status = RTE_CRYPTO_OP_STATUS_ERROR; 1079 break; 1080 } 1081 1082 /* Free session if a session-less crypto op */ 1083 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) { 1084 openssl_reset_session(sess); 1085 memset(sess, 0, sizeof(struct openssl_session)); 1086 rte_mempool_put(qp->sess_mp, op->sym->session); 1087 op->sym->session = NULL; 1088 } 1089 1090 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED) 1091 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS; 1092 1093 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) 1094 retval = rte_ring_enqueue(qp->processed_ops, (void *)op); 1095 else 1096 retval = -1; 1097 1098 return retval; 1099 } 1100 1101 /* 1102 *------------------------------------------------------------------------------ 1103 * PMD Framework 1104 *------------------------------------------------------------------------------ 1105 */ 1106 1107 /** Enqueue burst */ 1108 static uint16_t 1109 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops, 1110 uint16_t nb_ops) 1111 { 1112 struct openssl_session *sess; 1113 struct openssl_qp *qp = queue_pair; 1114 int i, retval; 1115 1116 for (i = 0; i < nb_ops; i++) { 1117 sess = get_session(qp, ops[i]); 1118 if (unlikely(sess == NULL)) 1119 goto enqueue_err; 1120 1121 retval = process_op(qp, ops[i], sess); 1122 if (unlikely(retval < 0)) 1123 goto enqueue_err; 1124 } 1125 1126 qp->stats.enqueued_count += i; 1127 return i; 1128 1129 enqueue_err: 1130 qp->stats.enqueue_err_count++; 1131 return i; 1132 } 1133 1134 /** Dequeue burst */ 1135 static uint16_t 1136 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops, 1137 uint16_t nb_ops) 1138 { 1139 struct openssl_qp *qp = queue_pair; 1140 1141 unsigned int nb_dequeued = 0; 1142 1143 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops, 1144 (void **)ops, nb_ops); 1145 qp->stats.dequeued_count += nb_dequeued; 1146 1147 return nb_dequeued; 1148 } 1149 1150 /** Create OPENSSL crypto device */ 1151 static int 1152 cryptodev_openssl_create(const char *name, 1153 struct rte_crypto_vdev_init_params *init_params) 1154 { 1155 struct rte_cryptodev *dev; 1156 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN]; 1157 struct openssl_private *internals; 1158 1159 /* create a unique device name */ 1160 if (create_unique_device_name(crypto_dev_name, 1161 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) { 1162 OPENSSL_LOG_ERR("failed to create unique cryptodev name"); 1163 return -EINVAL; 1164 } 1165 1166 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name, 1167 sizeof(struct openssl_private), 1168 init_params->socket_id); 1169 if (dev == NULL) { 1170 OPENSSL_LOG_ERR("failed to create cryptodev vdev"); 1171 goto init_error; 1172 } 1173 1174 dev->dev_type = RTE_CRYPTODEV_OPENSSL_PMD; 1175 dev->dev_ops = rte_openssl_pmd_ops; 1176 1177 /* register rx/tx burst functions for data path */ 1178 dev->dequeue_burst = openssl_pmd_dequeue_burst; 1179 dev->enqueue_burst = openssl_pmd_enqueue_burst; 1180 1181 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | 1182 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING | 1183 RTE_CRYPTODEV_FF_CPU_AESNI | 1184 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER; 1185 1186 /* Set vector instructions mode supported */ 1187 internals = dev->data->dev_private; 1188 1189 internals->max_nb_qpairs = init_params->max_nb_queue_pairs; 1190 internals->max_nb_sessions = init_params->max_nb_sessions; 1191 1192 return 0; 1193 1194 init_error: 1195 OPENSSL_LOG_ERR("driver %s: cryptodev_openssl_create failed", name); 1196 1197 cryptodev_openssl_remove(crypto_dev_name); 1198 return -EFAULT; 1199 } 1200 1201 /** Initialise OPENSSL crypto device */ 1202 static int 1203 cryptodev_openssl_probe(const char *name, 1204 const char *input_args) 1205 { 1206 struct rte_crypto_vdev_init_params init_params = { 1207 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS, 1208 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS, 1209 rte_socket_id() 1210 }; 1211 1212 rte_cryptodev_parse_vdev_init_params(&init_params, input_args); 1213 1214 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name, 1215 init_params.socket_id); 1216 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n", 1217 init_params.max_nb_queue_pairs); 1218 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n", 1219 init_params.max_nb_sessions); 1220 1221 return cryptodev_openssl_create(name, &init_params); 1222 } 1223 1224 /** Uninitialise OPENSSL crypto device */ 1225 static int 1226 cryptodev_openssl_remove(const char *name) 1227 { 1228 if (name == NULL) 1229 return -EINVAL; 1230 1231 RTE_LOG(INFO, PMD, 1232 "Closing OPENSSL crypto device %s on numa socket %u\n", 1233 name, rte_socket_id()); 1234 1235 return 0; 1236 } 1237 1238 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = { 1239 .probe = cryptodev_openssl_probe, 1240 .remove = cryptodev_openssl_remove 1241 }; 1242 1243 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD, 1244 cryptodev_openssl_pmd_drv); 1245 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD, 1246 "max_nb_queue_pairs=<int> " 1247 "max_nb_sessions=<int> " 1248 "socket_id=<int>"); 1249