blob: 56318b9447fe9baade7f92f100e2a0ae67906fd0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/**************************************************************************//**
* @file trng.c
* @version V3.00
* @brief M460 series TRNG driver source file
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
/** @addtogroup Standard_Driver Standard Driver
@{
*/
/** @addtogroup TRNG_Driver TRNG Driver
@{
*/
/** @addtogroup TRNG_EXPORTED_FUNCTIONS TRNG Exported Functions
@{
*/
/**
* @brief Initialize TRNG hardware.
* @return TRNG hardware enable success or failed.
* @retval 0 Success
* @retval -1 Time-out. TRNG hardware may not be enabled.
*/
int32_t TRNG_Open(void)
{
uint32_t u32TimeOutCount = SystemCoreClock; /* 1 second time-out */
SYS->IPRST1 |= SYS_IPRST1_TRNGRST_Msk;
SYS->IPRST1 ^= SYS_IPRST1_TRNGRST_Msk;
TRNG->CTL |= TRNG_CTL_TRNGEN_Msk;
TRNG->ACT |= TRNG_ACT_ACT_Msk;
/* Waiting for ready */
while ((TRNG->CTL & TRNG_CTL_READY_Msk) == 0)
{
if (--u32TimeOutCount == 0) return -1; /* Time-out error */
}
return 0;
}
/**
* @brief Generate a 32-bits random number word.
* @param[out] u32RndNum The output 32-bits word random number.
*
* @return Success or time-out.
* @retval 0 Success
* @retval -1 Time-out. TRNG hardware may not be enabled.
*/
int32_t TRNG_GenWord(uint32_t *u32RndNum)
{
uint32_t i, u32Reg, timeout;
*u32RndNum = 0;
u32Reg = TRNG->CTL;
for (i = 0; i < 4; i++)
{
TRNG->CTL = TRNG_CTL_TRNGEN_Msk | u32Reg;
/* TRNG should generate one byte per 125*8 us */
for (timeout = (CLK_GetHCLKFreq() / 100); timeout > 0; timeout--)
{
if (TRNG->CTL & TRNG_CTL_DVIF_Msk)
break;
}
if (timeout == 0)
return -1;
*u32RndNum |= ((TRNG->DATA & 0xff) << i * 8);
}
return 0;
}
/**
* @brief Generate a big number in binary format.
* @param[out] u8BigNum The output big number.
* @param[in] i32Len Request bit length of the output big number. It must be multiple of 8.
*
* @return Success or time-out.
* @retval 0 Success
* @retval -1 Time-out. TRNG hardware may not be enabled.
*/
int32_t TRNG_GenBignum(uint8_t u8BigNum[], int32_t i32Len)
{
uint32_t i, u32Reg, timeout;
u32Reg = TRNG->CTL;
for (i = 0; i < i32Len / 8; i++)
{
TRNG->CTL = TRNG_CTL_TRNGEN_Msk | u32Reg;
/* TRNG should generate one byte per 125*8 us */
for (timeout = (CLK_GetHCLKFreq() / 100); timeout > 0; timeout--)
{
if (TRNG->CTL & TRNG_CTL_DVIF_Msk)
break;
}
if (timeout == 0)
return -1;
u8BigNum[i] = (TRNG->DATA & 0xff);
}
return 0;
}
/**
* @brief Generate a big number in hex format.
* @param[out] cBigNumHex The output hex format big number.
* @param[in] i32Len Request bit length of the output big number. It must be multiple of 8.
*
* @return Success or time-out.
* @retval 0 Success
* @retval -1 Time-out. TRNG hardware may not be enabled.
*/
int32_t TRNG_GenBignumHex(char cBigNumHex[], int32_t i32Len)
{
uint32_t i, idx, u32Reg, timeout;
uint32_t data;
u32Reg = TRNG->CTL;
idx = 0;
for (i = 0; i < i32Len / 8; i++)
{
TRNG->CTL = TRNG_CTL_TRNGEN_Msk | u32Reg;
/* TRNG should generate one byte per 125*8 us */
for (timeout = (CLK_GetHCLKFreq() / 100); timeout > 0; timeout--)
{
if (TRNG->CTL & TRNG_CTL_DVIF_Msk)
break;
}
if (timeout == 0)
return -1;
data = (TRNG->DATA & 0xff);
if (data >= 0xA0)
cBigNumHex[idx++] = ((data >> 4) & 0xf) - 10 + 'A';
else
cBigNumHex[idx++] = ((data >> 4) & 0xf) + '0';
data &= 0xf;
if (data >= 0xA)
cBigNumHex[idx++] = data - 10 + 'A';
else
cBigNumHex[idx++] = data + '0';
}
cBigNumHex[idx] = 0;
return 0;
}
/*@}*/ /* end of group TRNG_EXPORTED_FUNCTIONS */
/*@}*/ /* end of group TRNG_Driver */
/*@}*/ /* end of group Standard_Driver */
|