Skip to content
File

Blob: firmware/platform/tests/crypto_profile/profile_test.c

c122 lines
1#include <assert.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include "radio_bridge.h"
7#include "freertos/task.h"
8 
9/* Synthetic native operations exercise wrappers without keys, SDK or hardware. */
10unsigned test_lock_depth;
11static TaskHandle_t current_task = (void *)(uintptr_t)1;
12static bool in_isr;
13static bool expect_active;
14int64_t test_now_us;
15static char lines[7][256];
16static size_t line_count;
17 
18int xPortInIsrContext(void)
19{
20 return in_isr;
21}
22 
23TaskHandle_t xTaskGetCurrentTaskHandle(void)
24{
25 return current_task;
26}
27 
28int64_t esp_timer_get_time(void)
29{
30 assert(test_lock_depth == 0);
31 return test_now_us;
32}
33 
34void radio_log(const char *message)
35{
36 assert(test_lock_depth == 0);
37 assert(line_count < sizeof(lines) / sizeof(lines[0]));
38 assert(strlen(message) < sizeof(lines[0]));
39 strcpy(lines[line_count++], message);
40}
41 
42void *esp_mbedtls_mem_calloc(size_t count, size_t size);
43 
44void test_allocate(size_t count, size_t size)
45{
46 free(esp_mbedtls_mem_calloc(count, size));
47}
48 
49void test_crypto_allocations(int32_t bits)
50{
51 assert(test_lock_depth == 0);
52 if (expect_active)
53 assert(radio_crypto_profile_finish() == -1);
54 free(esp_mbedtls_mem_calloc(2, 16));
55 free(esp_mbedtls_mem_calloc(2, 64));
56 if (bits == 384)
57 assert(esp_mbedtls_mem_calloc(99, 1) == NULL);
58 /* Concurrent allocations from another task must not inherit this tag. */
59 TaskHandle_t saved = current_task;
60 current_task = (void *)(uintptr_t)2;
61 free(esp_mbedtls_mem_calloc(3, 128));
62 current_task = saved;
63}
64 
65int main(void)
66{
67 const uint8_t key[16] = {0};
68 const uint8_t iv[16] = {0};
69 const uint8_t data[4] = {0};
70 uint8_t output[64] = {0};
71 const radio_crypto_part part = {data, sizeof(data)};
72 
73 /* Disabled wrappers retain their native return values and stay silent. */
74 assert(radio_crypto_hmac(160, key, sizeof(key), &part, 1, output, sizeof(output)) == 0);
75 assert(radio_crypto_profile_finish() == -1);
76 assert(line_count == 0);
77 
78 assert(radio_crypto_profile_start() == 0);
79 assert(radio_crypto_profile_start() == -1);
80 current_task = (void *)(uintptr_t)2;
81 assert(radio_crypto_profile_finish() == -1);
82 assert(radio_crypto_hmac(160, key, sizeof(key), &part, 1, output, sizeof(output)) == 0);
83 current_task = (void *)(uintptr_t)1;
84 in_isr = true;
85 assert(radio_crypto_profile_start() == -1);
86 assert(radio_crypto_profile_finish() == -1);
87 assert(radio_crypto_hmac(160, key, sizeof(key), &part, 1, output, sizeof(output)) == 0);
88 in_isr = false;
89 expect_active = true;
90 assert(radio_crypto_hmac(160, key, sizeof(key), &part, 1, output, sizeof(output)) == 0);
91 assert(radio_crypto_hmac(256, key, sizeof(key), &part, 1, output, sizeof(output)) == 0);
92 assert(radio_crypto_hmac(384, key, sizeof(key), &part, 1, output, sizeof(output)) == -7);
93 expect_active = false;
94 assert(radio_crypto_aes_ctr(key, sizeof(key), iv, data, sizeof(data), output, sizeof(output)) ==
95 0);
96 assert(radio_crypto_aes_gcm(0, key, sizeof(key), iv, data, sizeof(data), data, sizeof(data),
97 output, sizeof(output)) == 0);
98 assert(radio_crypto_aes_gcm(1, key, sizeof(key), iv, data, sizeof(data), data, sizeof(data),
99 output, sizeof(output)) == -9);
100 /* Untagged mbedTLS work is deliberately outside the native crypto totals. */
101 free(esp_mbedtls_mem_calloc(1, 999));
102 assert(radio_crypto_profile_finish() == 0);
103 assert(strcmp(lines[0], "Crypto profile: elapsed_us=130") == 0);
104 assert(strcmp(lines[1], "Crypto profile: op=hmac-sha1 calls=1 failures=0 us=10 calloc=2 "
105 "calloc_failures=0 requested_bytes=160") == 0);
106 assert(strcmp(lines[2], "Crypto profile: op=hmac-sha256 calls=1 failures=0 us=10 calloc=2 "
107 "calloc_failures=0 requested_bytes=160") == 0);
108 assert(strcmp(lines[3], "Crypto profile: op=hmac-sha384 calls=1 failures=1 us=10 calloc=3 "
109 "calloc_failures=1 requested_bytes=259") == 0);
110 assert(strcmp(lines[4], "Crypto profile: op=aes-ctr calls=1 failures=0 us=20 calloc=1 "
111 "calloc_failures=0 requested_bytes=16") == 0);
112 assert(strcmp(lines[5], "Crypto profile: op=gcm-encrypt calls=1 failures=0 us=30 calloc=1 "
113 "calloc_failures=0 requested_bytes=20") == 0);
114 assert(strcmp(lines[6], "Crypto profile: op=gcm-decrypt calls=1 failures=1 us=30 calloc=1 "
115 "calloc_failures=0 requested_bytes=20") == 0);
116 assert(radio_crypto_profile_finish() == -1);
117 assert(radio_crypto_hmac(160, key, sizeof(key), &part, 1, output, sizeof(output)) == 0);
118 assert(line_count == 7);
119 puts("Native crypto profile ownership, attribution and delegation passed");
120 return 0;
121}