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_psec_osal.h"
37 #include "em_core.h"
38 
39 #if defined(SLI_PSEC_THREADING)
40 static sli_psec_osal_lock_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 
85 // Initialize the RADIOAES lock (mutex) for mutual exclusive access
sli_protocol_crypto_init(void)86 sl_status_t sli_protocol_crypto_init(void)
87 {
88   sl_status_t sl_status = SL_STATUS_OK;
89 
90 #if defined(SLI_PSEC_THREADING)
91   // Check flag first before going into a critical section, to avoid going into
92   // a critical section on every single acquire() call. Since the _initialized
93   // flag only transitions false -> true, we can in 99% of the calls avoid the
94   // critical section.
95   if (!radioaes_lock_initialized) {
96     int32_t kernel_lock_state = 0;
97     osKernelState_t kernel_state = sli_psec_osal_kernel_get_state();
98     if (kernel_state != osKernelInactive && kernel_state != osKernelReady) {
99       kernel_lock_state = sli_psec_osal_kernel_lock();
100       if (kernel_lock_state < 0) {
101         return SL_STATUS_SUSPENDED;
102       }
103     }
104 
105     // Check the flag again after entering the critical section. Now that we're
106     // in the critical section, we can be sure that we are the only ones looking
107     // at the flag and no-one is interrupting us during its manipulation.
108     if (!radioaes_lock_initialized) {
109       sl_status = sli_psec_osal_init_lock(&radioaes_lock);
110       if (sl_status == SL_STATUS_OK) {
111         radioaes_lock_initialized = true;
112       }
113     }
114 
115     if (kernel_state != osKernelInactive && kernel_state != osKernelReady) {
116       if (sli_psec_osal_kernel_restore_lock(kernel_lock_state) < 0) {
117         return SL_STATUS_INVALID_STATE;
118       }
119     }
120   }
121 #endif
122   return sl_status;
123 }
124 
sli_radioaes_acquire(void)125 sl_status_t sli_radioaes_acquire(void)
126 {
127 #if defined(_CMU_CLKEN0_MASK)
128   CMU->CLKEN0 |= CMU_CLKEN0_RADIOAES;
129 #endif
130   CMU->RADIOCLKCTRL |= CMU_RADIOCLKCTRL_EN;
131   if ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0U) {
132     // IRQ: need to store & restore RADIOAES registers
133     while (RADIOAES->STATUS & (AES_STATUS_FETCHERBSY | AES_STATUS_PUSHERBSY | AES_STATUS_SOFTRSTBSY)) {
134       // Wait for completion of the previous operation, since the RADIOAES
135       // peripheral does not support preemption of an operation in progress.
136     }
137     #if defined(SLI_RADIOAES_REQUIRES_MASKING)
138     // The mask should have been initialized from non-ISR context by calling
139     // sl_mbedtls_init, before using the radioaes.
140     EFM_ASSERT(sli_radioaes_mask != 0);
141     #endif
142     return SL_STATUS_ISR;
143   } else {
144 #if defined(SLI_PSEC_THREADING)
145     sl_status_t ret = SL_STATUS_OK;
146     if (!radioaes_lock_initialized) {
147       ret = sli_protocol_crypto_init();
148     }
149     if (ret == SL_STATUS_OK) {
150       ret = sli_psec_osal_take_lock(&radioaes_lock);
151       #if defined(SLI_RADIOAES_REQUIRES_MASKING)
152       if (ret == SL_STATUS_OK) {
153         sli_radioaes_update_mask();
154       }
155       #endif
156     }
157     return ret;
158 #else
159     // Non-IRQ, no RTOS: busywait
160     while (RADIOAES->STATUS & (AES_STATUS_FETCHERBSY | AES_STATUS_PUSHERBSY | AES_STATUS_SOFTRSTBSY)) {
161       // Wait for completion
162     }
163     #if defined(SLI_RADIOAES_REQUIRES_MASKING)
164     sli_radioaes_update_mask();
165     #endif
166     return SL_STATUS_OK;
167 #endif
168   }
169 }
170 
sli_radioaes_release(void)171 sl_status_t sli_radioaes_release(void)
172 {
173   // IRQ: nothing to do
174   if ((SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0U) {
175     return SL_STATUS_OK;
176   }
177 #if defined(SLI_PSEC_THREADING)
178   // Non-IRQ, RTOS available: free lock
179   return sli_psec_osal_give_lock(&radioaes_lock);
180 #else
181   // Non-IRQ, no RTOS: nothing to do.
182   return SL_STATUS_OK;
183 #endif
184 }
185 
sli_radioaes_save_state(sli_radioaes_state_t * ctx)186 sl_status_t sli_radioaes_save_state(sli_radioaes_state_t *ctx)
187 {
188   CORE_DECLARE_IRQ_STATE;
189   CORE_ENTER_CRITICAL();
190   ctx->FETCHADDR = RADIOAES->FETCHADDR;
191   ctx->PUSHADDR = RADIOAES->PUSHADDR;
192 
193   CORE_EXIT_CRITICAL();
194   return SL_STATUS_OK;
195 }
196 
sli_radioaes_restore_state(sli_radioaes_state_t * ctx)197 sl_status_t sli_radioaes_restore_state(sli_radioaes_state_t *ctx)
198 {
199   CORE_DECLARE_IRQ_STATE;
200   CORE_ENTER_CRITICAL();
201   RADIOAES->FETCHADDR = ctx->FETCHADDR;
202   RADIOAES->PUSHADDR = ctx->PUSHADDR;
203 
204   CORE_EXIT_CRITICAL();
205   return SL_STATUS_OK;
206 }
207 
208 /// @endcond
209 #endif //defined(RADIOAES_PRESENT)
210