1 /***************************************************************************//**
2 * @file
3 * @brief Threadsafe utilities for RADIOAES peripheral.
4 *******************************************************************************
5 * # License
6 * <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
7 *******************************************************************************
8 *
9 * SPDX-License-Identifier: Zlib
10 *
11 * The licensor of this software is Silicon Laboratories Inc.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty. In no event will the authors be held liable for any damages
15 * arising from the use of this software.
16 *
17 * Permission is granted to anyone to use this software for any purpose,
18 * including commercial applications, and to alter it and redistribute it
19 * freely, subject to the following restrictions:
20 *
21 * 1. The origin of this software must not be misrepresented; you must not
22 * claim that you wrote the original software. If you use this software
23 * in a product, an acknowledgment in the product documentation would be
24 * appreciated but is not required.
25 * 2. Altered source versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software.
27 * 3. This notice may not be removed or altered from any source distribution.
28 *
29 ******************************************************************************/
30 #include "em_device.h"
31
32 #if defined(RADIOAES_PRESENT)
33 /// @cond DO_NOT_INCLUDE_WITH_DOXYGEN
34
35 #include "sli_radioaes_management.h"
36 #include "sli_se_manager_osal.h"
37 #include "em_core.h"
38
39 #if defined(SL_SE_MANAGER_THREADING)
40 static se_manager_osal_mutex_t radioaes_lock = { 0 };
41 static volatile bool radioaes_lock_initialized = false;
42 #endif
43
44 #if defined(SLI_RADIOAES_REQUIRES_MASKING)
45
46 #if defined(SL_COMPONENT_CATALOG_PRESENT)
47 #include "sl_component_catalog.h"
48 #endif
49
50 #if defined(SL_CATALOG_PSA_CRYPTO_PRESENT)
51 // If the PSA Crypto core is present, use its randomness abstraction to get
52 // the initial mask seed.
53 #include "psa/crypto.h"
54 #else
55 // If the PSA Crypto core is not present, we need to target the TRNG driver
56 // directly to get the initial mask seed. We'll always have an external randomness
57 // provider function on devices containing a RADIOAES instance.
58 #include "sli_psa_driver_common.h"
59 #endif
60 #include "sl_assert.h"
61
62 uint32_t sli_radioaes_mask = 0;
63
sli_radioaes_update_mask(void)64 static void sli_radioaes_update_mask(void)
65 {
66 if (sli_radioaes_mask == 0) {
67 // Mask has not been initialized yet, get a random value to start
68 #if defined(SL_CATALOG_PSA_CRYPTO_PRESENT)
69 psa_status_t status = psa_generate_random((uint8_t*)&sli_radioaes_mask, sizeof(sli_radioaes_mask));
70 EFM_ASSERT(status == PSA_SUCCESS);
71 #else
72 size_t out_len = 0;
73 psa_status_t status = mbedtls_psa_external_get_random(NULL, (uint8_t*)&sli_radioaes_mask, sizeof(sli_radioaes_mask), &out_len);
74 EFM_ASSERT(status == PSA_SUCCESS);
75 EFM_ASSERT(out_len == sizeof(sli_radioaes_mask));
76 #endif
77 }
78
79 // Use a different mask for each new operation
80 // The masking logic requires the upper mask bit to be set
81 sli_radioaes_mask = (sli_radioaes_mask + 1) | (1UL << 31);
82 }
83 #endif // SLI_RADIOAES_REQUIRES_MASKING
84
sli_radioaes_acquire(void)85 sl_status_t sli_radioaes_acquire(void)
86 {
87 #if defined(_CMU_CLKEN0_MASK)
88 CMU->CLKEN0 |= CMU_CLKEN0_RADIOAES;
89 #endif
90 CMU->RADIOCLKCTRL |= CMU_RADIOCLKCTRL_EN;
91 if ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0U) {
92 // IRQ: need to store & restore RADIOAES registers
93 while (RADIOAES->STATUS & (AES_STATUS_FETCHERBSY | AES_STATUS_PUSHERBSY | AES_STATUS_SOFTRSTBSY)) {
94 // Wait for completion of the previous operation, since the RADIOAES
95 // peripheral does not support preemption of an operation in progress.
96 }
97 #if defined(SLI_RADIOAES_REQUIRES_MASKING)
98 // The mask should have been initialized from non-ISR context by calling
99 // sl_mbedtls_init, before using the radioaes.
100 EFM_ASSERT(sli_radioaes_mask != 0);
101 #endif
102 return SL_STATUS_ISR;
103 } else {
104 #if defined(SL_SE_MANAGER_THREADING)
105 sl_status_t ret = SL_STATUS_OK;
106
107 // Non-IRQ, RTOS available: take mutex
108 // Initialize mutex if that hasn't happened yet
109
110 // Check flag first before going into a critical section, to avoid going into
111 // a critical section on every single acquire() call. Since the _initialized
112 // flag only transitions false -> true, we can in 99% of the calls avoid the
113 // critical section.
114 if (!radioaes_lock_initialized) {
115 int32_t kernel_lock_state = 0;
116 osKernelState_t kernel_state = se_manager_osal_kernel_get_state();
117 if (kernel_state != osKernelInactive && kernel_state != osKernelReady) {
118 kernel_lock_state = se_manager_osal_kernel_lock();
119 if (kernel_lock_state < 0) {
120 return SL_STATUS_SUSPENDED;
121 }
122 }
123
124 // Check the flag again after entering the critical section. Now that we're
125 // in the critical section, we can be sure that we are the only ones looking
126 // at the flag and no-one is interrupting us during its manipulation.
127 if (!radioaes_lock_initialized) {
128 ret = se_manager_osal_init_mutex(&radioaes_lock);
129 if (ret == SL_STATUS_OK) {
130 radioaes_lock_initialized = true;
131 }
132 }
133
134 if (kernel_state != osKernelInactive && kernel_state != osKernelReady) {
135 if (se_manager_osal_kernel_restore_lock(kernel_lock_state) < 0) {
136 return SL_STATUS_INVALID_STATE;
137 }
138 }
139 }
140
141 if (ret == SL_STATUS_OK) {
142 ret = se_manager_osal_take_mutex(&radioaes_lock);
143 }
144
145 #if defined(SLI_RADIOAES_REQUIRES_MASKING)
146 if (ret == SL_STATUS_OK) {
147 sli_radioaes_update_mask();
148 }
149 #endif
150
151 return ret;
152 #else
153 // Non-IRQ, no RTOS: busywait
154 while (RADIOAES->STATUS & (AES_STATUS_FETCHERBSY | AES_STATUS_PUSHERBSY | AES_STATUS_SOFTRSTBSY)) {
155 // Wait for completion
156 }
157 #if defined(SLI_RADIOAES_REQUIRES_MASKING)
158 sli_radioaes_update_mask();
159 #endif
160 return SL_STATUS_OK;
161 #endif
162 }
163 }
164
sli_radioaes_release(void)165 sl_status_t sli_radioaes_release(void)
166 {
167 // IRQ: nothing to do
168 if ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0U) {
169 return SL_STATUS_OK;
170 }
171 #if defined(SL_SE_MANAGER_THREADING)
172 // Non-IRQ, RTOS available: free mutex
173 return se_manager_osal_give_mutex(&radioaes_lock);
174 #else
175 // Non-IRQ, no RTOS: nothing to do.
176 return SL_STATUS_OK;
177 #endif
178 }
179
sli_radioaes_save_state(sli_radioaes_state_t * ctx)180 sl_status_t sli_radioaes_save_state(sli_radioaes_state_t *ctx)
181 {
182 CORE_DECLARE_IRQ_STATE;
183 CORE_ENTER_CRITICAL();
184 ctx->FETCHADDR = RADIOAES->FETCHADDR;
185 ctx->PUSHADDR = RADIOAES->PUSHADDR;
186
187 CORE_EXIT_CRITICAL();
188 return SL_STATUS_OK;
189 }
190
sli_radioaes_restore_state(sli_radioaes_state_t * ctx)191 sl_status_t sli_radioaes_restore_state(sli_radioaes_state_t *ctx)
192 {
193 CORE_DECLARE_IRQ_STATE;
194 CORE_ENTER_CRITICAL();
195 RADIOAES->FETCHADDR = ctx->FETCHADDR;
196 RADIOAES->PUSHADDR = ctx->PUSHADDR;
197
198 CORE_EXIT_CRITICAL();
199 return SL_STATUS_OK;
200 }
201
202 /// @endcond
203 #endif //defined(RADIOAES_PRESENT)
204