1 /*
2  * Copyright (c) 2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "cmsis.h"
9 #include "tfm_fih_rng.h"
10 
tfm_fih_random_init(void)11 fih_int tfm_fih_random_init(void)
12 {
13     SysTick->LOAD  = 0x000000FE;                  /* Set reload register */
14     SysTick->VAL   = 0UL;                         /* Load the SysTick Counter Value */
15     SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
16                      SysTick_CTRL_ENABLE_Msk;     /* Enable SysTick Timer */
17 
18     return FIH_SUCCESS;
19 }
20 
tfm_fih_random_generate(uint8_t * rand)21 void tfm_fih_random_generate(uint8_t *rand)
22 {
23     /* Repeat reading SysTick value to mitigate instruction skip */
24     *rand = (uint8_t)SysTick->VAL;
25     *rand = (uint8_t)SysTick->VAL;
26     *rand = (uint8_t)SysTick->VAL;
27     *rand = (uint8_t)SysTick->VAL;
28     *rand = (uint8_t)SysTick->VAL;
29 }
30