test_aes_sha_parallel.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <esp_system.h>
  9. #include "mbedtls/aes.h"
  10. #include "mbedtls/sha256.h"
  11. #include "unity.h"
  12. #include "sdkconfig.h"
  13. #include "esp_heap_caps.h"
  14. #include "test_utils.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/semphr.h"
  18. static SemaphoreHandle_t done_sem;
  19. static const unsigned char *one_hundred_bs = (unsigned char *)
  20. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
  21. static const uint8_t sha256_thousand_bs[32] = {
  22. 0xf6, 0xf1, 0x18, 0xe1, 0x20, 0xe5, 0x2b, 0xe0, 0xbd, 0x0c, 0xfd, 0xf2, 0x79, 0x4c, 0xd1, 0x2c, 0x07, 0x68, 0x6c, 0xc8, 0x71, 0x23, 0x5a, 0xc2, 0xf1, 0x14, 0x59, 0x37, 0x8e, 0x6d, 0x23, 0x5b
  23. };
  24. static void tskRunSHA256Test(void *pvParameters)
  25. {
  26. mbedtls_sha256_context sha256_ctx;
  27. unsigned char sha256[32];
  28. for (int i = 0; i < 1000; i++) {
  29. mbedtls_sha256_init(&sha256_ctx);
  30. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts(&sha256_ctx, false));
  31. for (int j = 0; j < 10; j++) {
  32. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update(&sha256_ctx, (unsigned char *)one_hundred_bs, 100));
  33. }
  34. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish(&sha256_ctx, sha256));
  35. mbedtls_sha256_free(&sha256_ctx);
  36. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_thousand_bs, sha256, 32, "SHA256 calculation");
  37. }
  38. xSemaphoreGive(done_sem);
  39. vTaskDelete(NULL);
  40. }
  41. static void tskRunAES256Test(void *pvParameters)
  42. {
  43. static const uint8_t iv[] = {
  44. 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09,
  45. 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
  46. };
  47. static const uint8_t key_256[] = {
  48. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  49. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  50. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  51. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  52. };
  53. for (int i = 0; i <1000; i++)
  54. {
  55. const unsigned SZ = 1600;
  56. mbedtls_aes_context ctx;
  57. uint8_t nonce[16];
  58. const uint8_t expected_cipher_end[] = {
  59. 0x3e, 0x68, 0x8a, 0x02, 0xe6, 0xf2, 0x6a, 0x9e,
  60. 0x9b, 0xb2, 0xc0, 0xc4, 0x63, 0x63, 0xd9, 0x25,
  61. 0x51, 0xdc, 0xc2, 0x71, 0x96, 0xb3, 0xe5, 0xcd,
  62. 0xbd, 0x0e, 0xf2, 0xef, 0xa9, 0xab, 0xab, 0x2d,
  63. };
  64. memcpy(nonce, iv, 16);
  65. // allocate internal memory
  66. uint8_t *chipertext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  67. uint8_t *plaintext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  68. uint8_t *decryptedtext = heap_caps_malloc(SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  69. TEST_ASSERT_NOT_NULL(chipertext);
  70. TEST_ASSERT_NOT_NULL(plaintext);
  71. TEST_ASSERT_NOT_NULL(decryptedtext);
  72. mbedtls_aes_init(&ctx);
  73. mbedtls_aes_setkey_enc(&ctx, key_256, 256);
  74. memset(plaintext, 0x3A, SZ);
  75. memset(decryptedtext, 0x0, SZ);
  76. // Encrypt
  77. mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, SZ, nonce, plaintext, chipertext);
  78. TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_cipher_end, chipertext + SZ - 32, 32);
  79. // Decrypt
  80. memcpy(nonce, iv, 16);
  81. mbedtls_aes_setkey_dec(&ctx, key_256, 256);
  82. mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, SZ, nonce, chipertext, decryptedtext);
  83. TEST_ASSERT_EQUAL_HEX8_ARRAY(plaintext, decryptedtext, SZ);
  84. mbedtls_aes_free(&ctx);
  85. free(plaintext);
  86. free(chipertext);
  87. free(decryptedtext);
  88. }
  89. xSemaphoreGive(done_sem);
  90. vTaskDelete(NULL);
  91. }
  92. #include "esp_crypto_shared_gdma.h"
  93. #define TASK_STACK_SIZE (20*1024)
  94. TEST_CASE("mbedtls AES/SHA multithreading", "[mbedtls]")
  95. {
  96. done_sem = xSemaphoreCreateCounting(2, 0);
  97. xTaskCreate(tskRunSHA256Test, "SHA256Task", TASK_STACK_SIZE, NULL, 3, NULL);
  98. xTaskCreate(tskRunAES256Test, "AES256Task", TASK_STACK_SIZE, NULL, 3, NULL);
  99. for (int i = 0; i < 2; i++) {
  100. if (!xSemaphoreTake(done_sem, 10000 / portTICK_PERIOD_MS)) {
  101. TEST_FAIL_MESSAGE("done_sem not released by test task");
  102. }
  103. }
  104. vSemaphoreDelete(done_sem);
  105. }