24
24
#include < limits.h>
25
25
#include " nu_modutil.h"
26
26
#include " nu_bitutil.h"
27
+ #include " nu_timer.h"
27
28
#include " crypto-misc.h"
28
29
#include " platform/SingletonPtr.h"
29
30
#include " platform/PlatformMutex.h"
@@ -72,7 +73,7 @@ static volatile uint16_t crypto_ecc_done;
72
73
static volatile uint16_t crypto_rsa_done;
73
74
74
75
static void crypto_submodule_prestart (volatile uint16_t *submodule_done);
75
- static bool crypto_submodule_wait (volatile uint16_t *submodule_done);
76
+ static bool crypto_submodule_wait (volatile uint16_t *submodule_done, int32_t timeout_us );
76
77
77
78
/* As crypto init counter changes from 0 to 1:
78
79
*
@@ -206,7 +207,12 @@ void crypto_prng_prestart(void)
206
207
207
208
bool crypto_prng_wait (void )
208
209
{
209
- return crypto_submodule_wait (&crypto_prng_done);
210
+ return crypto_prng_wait2 (-1 );
211
+ }
212
+
213
+ bool crypto_prng_wait2 (int32_t timeout_us)
214
+ {
215
+ return crypto_submodule_wait (&crypto_prng_done, timeout_us);
210
216
}
211
217
212
218
void crypto_aes_prestart (void )
@@ -216,7 +222,12 @@ void crypto_aes_prestart(void)
216
222
217
223
bool crypto_aes_wait (void )
218
224
{
219
- return crypto_submodule_wait (&crypto_aes_done);
225
+ return crypto_aes_wait2 (-1 );
226
+ }
227
+
228
+ bool crypto_aes_wait2 (int32_t timeout_us)
229
+ {
230
+ return crypto_submodule_wait (&crypto_aes_done, timeout_us);
220
231
}
221
232
222
233
void crypto_sha_prestart (void )
@@ -226,7 +237,12 @@ void crypto_sha_prestart(void)
226
237
227
238
bool crypto_sha_wait (void )
228
239
{
229
- return crypto_submodule_wait (&crypto_sha_done);
240
+ return crypto_sha_wait2 (-1 );
241
+ }
242
+
243
+ bool crypto_sha_wait2 (int32_t timeout_us)
244
+ {
245
+ return crypto_submodule_wait (&crypto_sha_done, timeout_us);
230
246
}
231
247
232
248
void crypto_ecc_prestart (void )
@@ -236,7 +252,12 @@ void crypto_ecc_prestart(void)
236
252
237
253
bool crypto_ecc_wait (void )
238
254
{
239
- return crypto_submodule_wait (&crypto_ecc_done);
255
+ return crypto_ecc_wait2 (-1 );
256
+ }
257
+
258
+ bool crypto_ecc_wait2 (int32_t timeout_us)
259
+ {
260
+ return crypto_submodule_wait (&crypto_ecc_done, timeout_us);
240
261
}
241
262
242
263
void crypto_rsa_prestart (void )
@@ -246,7 +267,12 @@ void crypto_rsa_prestart(void)
246
267
247
268
bool crypto_rsa_wait (void )
248
269
{
249
- return crypto_submodule_wait (&crypto_rsa_done);
270
+ return crypto_rsa_wait2 (-1 );
271
+ }
272
+
273
+ bool crypto_rsa_wait2 (int32_t timeout_us)
274
+ {
275
+ return crypto_submodule_wait (&crypto_rsa_done, timeout_us);
250
276
}
251
277
252
278
bool crypto_dma_buff_compat (const void *buff, size_t buff_size, size_t size_aligned_to)
@@ -304,9 +330,21 @@ static void crypto_submodule_prestart(volatile uint16_t *submodule_done)
304
330
__DSB ();
305
331
}
306
332
307
- static bool crypto_submodule_wait (volatile uint16_t *submodule_done)
333
+ static bool crypto_submodule_wait (volatile uint16_t *submodule_done, int32_t timeout_us )
308
334
{
309
- while (! *submodule_done);
335
+ /* Translate indefinite to extremely large */
336
+ if (timeout_us < 0 ) {
337
+ timeout_us = 0x7FFFFFFF ;
338
+ }
339
+
340
+ struct nu_countdown_ctx_s ctx;
341
+ nu_countdown_init (&ctx, timeout_us);
342
+ while (! *submodule_done) {
343
+ if (nu_countdown_expired (&ctx)) {
344
+ break ;
345
+ }
346
+ }
347
+ nu_countdown_free (&ctx);
310
348
311
349
/* Ensure while loop above and subsequent code are not reordered */
312
350
__DSB ();
0 commit comments