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