|
25 | 25 |
|
26 | 26 | #if defined(WOLFHSM_CFG_BENCH_ENABLE) |
27 | 27 |
|
| 28 | +#if defined(WOLFHSM_CFG_DMA) && defined(WOLFHSM_CFG_TEST_POSIX) |
| 29 | +#include "port/posix/posix_transport_shm.h" |
| 30 | +#endif /* WOLFHSM_CFG_DMA && WOLFHSM_CFG_TEST_POSIX */ |
| 31 | + |
28 | 32 | #if !defined(NO_AES) |
29 | 33 |
|
30 | 34 | /* 128-bit key */ |
@@ -440,6 +444,164 @@ int wh_Bench_Mod_Aes256CBCDecrypt(whClientContext* client, |
440 | 444 | #endif /* HAVE_AES_CBC */ |
441 | 445 |
|
442 | 446 | #if defined(HAVE_AESGCM) |
| 447 | +#ifdef WOLFHSM_CFG_DMA |
| 448 | +static int _benchAesGcmDma(whClientContext* client, whBenchOpContext* ctx, |
| 449 | + int id, const uint8_t* key, size_t keyLen, |
| 450 | + int encrypt) |
| 451 | +{ |
| 452 | + int ret = 0; |
| 453 | + int needEvict = 0; |
| 454 | + whKeyId keyId = WH_KEYID_ERASED; |
| 455 | + Aes aes[1]; |
| 456 | + char keyLabel[] = "key label"; |
| 457 | + byte iv[WC_AES_BLOCK_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, |
| 458 | + 8, 9, 10, 11, 12, 13, 14, 15}; |
| 459 | + byte authData[WC_AES_BLOCK_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, |
| 460 | + 8, 9, 10, 11, 12, 13, 14, 15}; |
| 461 | + byte authTag[WC_AES_BLOCK_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, |
| 462 | + 8, 9, 10, 11, 12, 13, 14, 15}; |
| 463 | + const size_t inLen = WOLFHSM_CFG_BENCH_DMA_BUFFER_SIZE / 2; |
| 464 | + int i; |
| 465 | + const uint8_t* in = NULL; |
| 466 | + uint8_t* out = NULL; |
| 467 | + |
| 468 | +#if defined(WOLFHSM_CFG_TEST_POSIX) |
| 469 | + /* Allocate buffers using XMALLOC with heap hints for DMA */ |
| 470 | + if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) { |
| 471 | + void* heap = |
| 472 | + posixTransportShm_GetDmaHeap(client->comm->transport_context); |
| 473 | + in = XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER); |
| 474 | + if (in == NULL) { |
| 475 | + WH_BENCH_PRINTF("Failed to allocate memory for DMA input\n"); |
| 476 | + return WH_ERROR_NOSPACE; |
| 477 | + } |
| 478 | + |
| 479 | + out = XMALLOC(inLen, heap, DYNAMIC_TYPE_TMP_BUFFER); |
| 480 | + if (out == NULL) { |
| 481 | + WH_BENCH_PRINTF("Failed to allocate memory for DMA output\n"); |
| 482 | + XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER); |
| 483 | + return WH_ERROR_NOSPACE; |
| 484 | + } |
| 485 | + } |
| 486 | + else |
| 487 | +#endif /* WOLFHSM_CFG_TEST_POSIX */ |
| 488 | + { |
| 489 | + in = WH_BENCH_DMA_BUFFER; |
| 490 | + out = (uint8_t*)in + inLen; |
| 491 | + } |
| 492 | + |
| 493 | +#if defined(WOLFHSM_CFG_BENCH_INIT_DATA_BUFFERS) |
| 494 | + /* Initialize the buffers with something non-zero */ |
| 495 | + memset((uint8_t*)in, 0xAA, inLen); |
| 496 | + memset(out, 0xAA, inLen); |
| 497 | +#endif |
| 498 | + |
| 499 | + /* initialize the aes struct */ |
| 500 | + ret = wc_AesInit(aes, NULL, WH_DEV_ID_DMA); |
| 501 | + if (ret != 0) { |
| 502 | + WH_BENCH_PRINTF("Failed to wc_AesInit %d\n", ret); |
| 503 | + return ret; |
| 504 | + } |
| 505 | + |
| 506 | + /* cache the key on the HSM */ |
| 507 | + ret = wh_Client_KeyCache(client, 0, (uint8_t*)keyLabel, sizeof(keyLabel), |
| 508 | + (uint8_t*)key, keyLen, &keyId); |
| 509 | + if (ret != 0) { |
| 510 | + WH_BENCH_PRINTF("Failed to wh_Client_KeyCache %d\n", ret); |
| 511 | + goto exit; |
| 512 | + } |
| 513 | + |
| 514 | + needEvict = 1; |
| 515 | + |
| 516 | + /* set the keyId on the struct */ |
| 517 | + ret = wh_Client_AesSetKeyId(aes, keyId); |
| 518 | + if (ret != 0) { |
| 519 | + WH_BENCH_PRINTF("Failed to wh_Client_SetKeyIdAes %d\n", ret); |
| 520 | + goto exit; |
| 521 | + } |
| 522 | + |
| 523 | + /* set the iv */ |
| 524 | + ret = wc_AesSetIV(aes, iv); |
| 525 | + if (ret != 0) { |
| 526 | + WH_BENCH_PRINTF("Failed to wc_AesSetIV %d\n", ret); |
| 527 | + goto exit; |
| 528 | + } |
| 529 | + |
| 530 | + ret = wh_Bench_SetDataSize(ctx, id, inLen); |
| 531 | + if (ret != 0) { |
| 532 | + WH_BENCH_PRINTF("Failed to wh_Bench_SetDataSize %d\n", ret); |
| 533 | + goto exit; |
| 534 | + } |
| 535 | + |
| 536 | + for (i = 0; i < WOLFHSM_CFG_BENCH_CRYPT_ITERS; i++) { |
| 537 | + int benchStartRet; |
| 538 | + int benchStopRet; |
| 539 | + |
| 540 | + if (encrypt == ENCRYPT) { |
| 541 | + benchStartRet = wh_Bench_StartOp(ctx, id); |
| 542 | + |
| 543 | + ret = wh_Client_AesGcmDma(client, aes, ENCRYPT, in, inLen, iv, |
| 544 | + sizeof(iv), authData, sizeof(authData), |
| 545 | + NULL, authTag, sizeof(authTag), out); |
| 546 | + |
| 547 | + benchStopRet = wh_Bench_StopOp(ctx, id); |
| 548 | + } |
| 549 | + else { |
| 550 | + benchStartRet = wh_Bench_StartOp(ctx, id); |
| 551 | + |
| 552 | + ret = wh_Client_AesGcmDma(client, aes, DECRYPT, in, inLen, iv, |
| 553 | + sizeof(iv), authData, sizeof(authData), |
| 554 | + authTag, NULL, sizeof(authTag), out); |
| 555 | + |
| 556 | + benchStopRet = wh_Bench_StopOp(ctx, id); |
| 557 | + |
| 558 | + /* Squash auth error since we are using dummy data */ |
| 559 | + if (ret == AES_GCM_AUTH_E) { |
| 560 | + ret = 0; |
| 561 | + } |
| 562 | + } |
| 563 | + |
| 564 | + if (benchStartRet != 0) { |
| 565 | + WH_BENCH_PRINTF("Failed to wh_Bench_StartOp %d\n", benchStartRet); |
| 566 | + ret = benchStartRet; |
| 567 | + goto exit; |
| 568 | + } |
| 569 | + if (ret != 0) { |
| 570 | + WH_BENCH_PRINTF("Failed to wh_Client_AesGcmDma %d\n", ret); |
| 571 | + goto exit; |
| 572 | + } |
| 573 | + if (benchStopRet != 0) { |
| 574 | + WH_BENCH_PRINTF("Failed to wh_Bench_StopOp %d\n", benchStopRet); |
| 575 | + ret = benchStopRet; |
| 576 | + goto exit; |
| 577 | + } |
| 578 | + } |
| 579 | + |
| 580 | +exit: |
| 581 | + wc_AesFree(aes); |
| 582 | + |
| 583 | + if (needEvict) { |
| 584 | + int evictRet = wh_Client_KeyEvict(client, keyId); |
| 585 | + if (evictRet != 0) { |
| 586 | + WH_BENCH_PRINTF("Failed to evict key from cache: %d\n", evictRet); |
| 587 | + ret = evictRet; |
| 588 | + } |
| 589 | + } |
| 590 | + |
| 591 | +#if defined(WOLFHSM_CFG_TEST_POSIX) |
| 592 | + if (ctx->transportType == WH_BENCH_TRANSPORT_POSIX_DMA) { |
| 593 | + /* if static memory was used with DMA then use XFREE */ |
| 594 | + void* heap = |
| 595 | + posixTransportShm_GetDmaHeap(client->comm->transport_context); |
| 596 | + XFREE((uint8_t*)in, heap, DYNAMIC_TYPE_TMP_BUFFER); |
| 597 | + XFREE(out, heap, DYNAMIC_TYPE_TMP_BUFFER); |
| 598 | + } |
| 599 | +#endif /* WOLFHSM_CFG_TEST_POSIX */ |
| 600 | + |
| 601 | + return ret; |
| 602 | +} |
| 603 | +#endif /* WOLFHSM_CFG_DMA */ |
| 604 | + |
443 | 605 | static int _benchAesGcm(whClientContext* client, whBenchOpContext* ctx, int id, |
444 | 606 | const uint8_t* key, size_t keyLen, int encrypt) |
445 | 607 | { |
@@ -598,6 +760,74 @@ int wh_Bench_Mod_Aes256GCMDecrypt(whClientContext* client, |
598 | 760 | return _benchAesGcm(client, ctx, id, (uint8_t*)key256, sizeof(key256), |
599 | 761 | DECRYPT); |
600 | 762 | } |
| 763 | + |
| 764 | +int wh_Bench_Mod_Aes128GCMEncryptDma(whClientContext* client, |
| 765 | + whBenchOpContext* ctx, int id, |
| 766 | + void* params) |
| 767 | +{ |
| 768 | +#if defined(WOLFHSM_CFG_DMA) |
| 769 | + (void)params; |
| 770 | + return _benchAesGcmDma(client, ctx, id, (uint8_t*)key128, sizeof(key128), |
| 771 | + ENCRYPT); |
| 772 | +#else |
| 773 | + (void)client; |
| 774 | + (void)ctx; |
| 775 | + (void)id; |
| 776 | + (void)params; |
| 777 | + return WH_ERROR_NOTIMPL; |
| 778 | +#endif |
| 779 | +} |
| 780 | + |
| 781 | +int wh_Bench_Mod_Aes128GCMDecryptDma(whClientContext* client, |
| 782 | + whBenchOpContext* ctx, int id, |
| 783 | + void* params) |
| 784 | +{ |
| 785 | +#if defined(WOLFHSM_CFG_DMA) |
| 786 | + (void)params; |
| 787 | + return _benchAesGcmDma(client, ctx, id, (uint8_t*)key128, sizeof(key128), |
| 788 | + DECRYPT); |
| 789 | +#else |
| 790 | + (void)client; |
| 791 | + (void)ctx; |
| 792 | + (void)id; |
| 793 | + (void)params; |
| 794 | + return WH_ERROR_NOTIMPL; |
| 795 | +#endif |
| 796 | +} |
| 797 | + |
| 798 | +int wh_Bench_Mod_Aes256GCMEncryptDma(whClientContext* client, |
| 799 | + whBenchOpContext* ctx, int id, |
| 800 | + void* params) |
| 801 | +{ |
| 802 | +#if defined(WOLFHSM_CFG_DMA) |
| 803 | + (void)params; |
| 804 | + return _benchAesGcmDma(client, ctx, id, (uint8_t*)key256, sizeof(key256), |
| 805 | + ENCRYPT); |
| 806 | +#else |
| 807 | + (void)client; |
| 808 | + (void)ctx; |
| 809 | + (void)id; |
| 810 | + (void)params; |
| 811 | + return WH_ERROR_NOTIMPL; |
| 812 | +#endif |
| 813 | +} |
| 814 | + |
| 815 | +int wh_Bench_Mod_Aes256GCMDecryptDma(whClientContext* client, |
| 816 | + whBenchOpContext* ctx, int id, |
| 817 | + void* params) |
| 818 | +{ |
| 819 | +#if defined(WOLFHSM_CFG_DMA) |
| 820 | + (void)params; |
| 821 | + return _benchAesGcmDma(client, ctx, id, (uint8_t*)key256, sizeof(key256), |
| 822 | + DECRYPT); |
| 823 | +#else |
| 824 | + (void)client; |
| 825 | + (void)ctx; |
| 826 | + (void)id; |
| 827 | + (void)params; |
| 828 | + return WH_ERROR_NOTIMPL; |
| 829 | +#endif |
| 830 | +} |
601 | 831 | #endif /* HAVE_AESGCM */ |
602 | 832 |
|
603 | 833 | #endif /* !defined(NO_AES) */ |
|
0 commit comments