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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/**************************************************************************//**
* @file rng.c
* @version V3.01
* @brief Show how to get true random number.
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
/** @addtogroup Standard_Driver Standard Driver
@{
*/
/** @addtogroup RNG_Driver RNG Driver
@{
*/
/** @addtogroup RNG_EXPORTED_FUNCTIONS RNG Exported Functions
@{
*/
typedef enum _RNG_KEY_SIZE
{
KEY_128 = 0,
KEY_192 = 2,
KEY_224 = 3,
KEY_233 = 4,
KEY_255 = 5,
KEY_256 = 6,
KEY_283 = 7,
KEY_384 = 8,
KEY_409 = 9,
KEY_512 = 10,
KEY_521 = 11,
KEY_571 = 12
} eRNG_SZ;
/**
* @brief Basic Configuration of TRNG and PRNG
*
* @details The function is used to set the basic configuration for TRNG and PRNG.
*/
static void RNG_BasicConfig()
{
int32_t i;
int32_t timeout = 0x1000000;
/* Enable TRNG & PRNG */
CLK->AHBCLK0 |= CLK_AHBCLK0_CRPTCKEN_Msk;
CLK->APBCLK1 |= CLK_APBCLK1_TRNGCKEN_Msk;
/* Use LIRC as TRNG engine clock */
CLK->PWRCTL |= CLK_PWRCTL_LIRCEN_Msk;
while ((CLK->STATUS & CLK_STATUS_LIRCSTB_Msk) == 0)
{
if (i++ > timeout) break; /* Wait LIRC time-out */
}
CLK->CLKSEL2 = (CLK->CLKSEL2 & (~CLK_CLKSEL2_TRNGSEL_Msk)) | CLK_CLKSEL2_TRNGSEL_LIRC;
}
/**
* @brief Open random number generator
*
* @return 0 Successful
* -1 Failed
*
* @details The function is used to disable rng interrupt.
*/
int32_t RNG_Open()
{
int32_t i;
int32_t timeout = 0x1000000;
RNG_BasicConfig();
/* TRNG Activate */
TRNG->ACT |= TRNG_ACT_ACT_Msk;
/* Waiting for ready */
i = 0;
while ((TRNG->CTL & TRNG_CTL_READY_Msk) == 0)
{
if (i++ > timeout)
{
/* TRNG ready timeout */
return -1;
}
}
/* CLKPSC is default to 0. The performance maybe low but suitable for any cases */
TRNG->CTL = 0;
/* Waiting for PRNG busy */
i = 0;
while ((CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk) == CRPT_PRNG_CTL_BUSY_Msk)
{
if (i++ > timeout)
{
/* PRNG busy timeout */
return -1;
}
}
/* Reload seed from TRNG only at first time */
CRPT->PRNG_CTL = (PRNG_KEY_SIZE_256 << CRPT_PRNG_CTL_KEYSZ_Pos) | CRPT_PRNG_CTL_START_Msk | CRPT_PRNG_CTL_SEEDRLD_Msk | PRNG_CTL_SEEDSRC_TRNG;
i = 0;
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)
{
if (i++ > timeout)
{
/* busy timeout */
return -1;
}
}
return 0;
}
/**
* @brief Get random words
*
* @param[in] pu32Buf Buffer pointer to store the random number
*
* @param[in] nWords Buffer size in word count. nWords must <= 8
*
* @return Word count of random number in buffer
*
* @details The function is used to generate random numbers
*/
int32_t RNG_Random(uint32_t *pu32Buf, int32_t nWords)
{
int32_t i;
int32_t timeout = 0x10000;
/* Waiting for Busy */
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)
{
if (timeout-- < 0)
return 0;
}
if (nWords > 8)
nWords = 8;
/* Trig to generate seed 256 bits random number */
CRPT->PRNG_CTL = (6 << CRPT_PRNG_CTL_KEYSZ_Pos) | CRPT_PRNG_CTL_START_Msk;
timeout = 0x10000;
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)
{
if (timeout-- < 0)
return 0;
}
for (i = 0; i < nWords; i++)
{
pu32Buf[i] = CRPT->PRNG_KEY[i];
}
return nWords;
}
/**
* @brief Initial function for ECDSA key generator for Key Store
*
* @param[in] u32KeySize It could be PRNG_KEY_SIZE_128 ~ PRNG_KEY_SIZE_571
*
* @param[in] au32ECC_N The N value of specified ECC curve.
*
* @return -1 Failed
* Others The key number in KS SRAM
*
* @details The function is initial funciton of RNG_ECDSA function.
* This funciton should be called before calling RNG_ECDSA().
*/
int32_t RNG_ECDSA_Init(uint32_t u32KeySize, uint32_t au32ECC_N[18])
{
int32_t i;
/* Initial TRNG and PRNG for random number */
if (RNG_Open())
return -1;
/* It is necessary to set ECC_N for ECDSA */
for (i = 0; i < 18; i++)
CRPT->ECC_N[i] = au32ECC_N[i];
CRPT->PRNG_KSCTL = (KS_OWNER_ECC << CRPT_PRNG_KSCTL_OWNER_Pos) |
CRPT_PRNG_KSCTL_ECDSA_Msk |
(CRPT_PRNG_KSCTL_WDST_Msk) |
(KS_SRAM << CRPT_PRNG_KSCTL_WSDST_Pos);
return 0;
}
/**
* @brief To generate a key to KS SRAM for ECDSA.
*
* @return -1 Failed
* Others The key number in KS SRAM
*
* @details The function is used to generate a key to KS SRAM for ECDSA.
* This key is necessary for ECDSA+Key Store function of ECC.
*/
int32_t RNG_ECDSA(uint32_t u32KeySize)
{
int32_t timeout;
int32_t i;
/* Reload seed only at first time */
CRPT->PRNG_CTL = (u32KeySize << CRPT_PRNG_CTL_KEYSZ_Pos) | CRPT_PRNG_CTL_START_Msk | PRNG_CTL_SEEDSRC_TRNG;
timeout = 0x10000;
i = 0;
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)
{
if (i++ > timeout)
{
return -1;
}
}
if (CRPT->PRNG_KSSTS & CRPT_PRNG_KSSTS_KCTLERR_Msk)
{
return -1;
}
return (CRPT->PRNG_KSSTS & CRPT_PRNG_KSCTL_NUM_Msk);
}
/**
* @brief Initial funciton for RNG_ECDH.
*
* @param[in] u32KeySize It could be PRNG_KEY_SIZE_128 ~ PRNG_KEY_SIZE_571
*
* @param[in] au32ECC_N The N value of specified ECC curve.
*
* @return -1 Failed
* Others The key number in KS SRAM
*
* @details The function is initial function of RNG_ECDH.
*
*/
int32_t RNG_ECDH_Init(uint32_t u32KeySize, uint32_t au32ECC_N[18])
{
int32_t i;
/* Initial Random Number Generator */
if (RNG_Open())
return -1;
/* It is necessary to set ECC_N for ECDSA */
for (i = 0; i < 18; i++)
CRPT->ECC_N[i] = au32ECC_N[i];
CRPT->PRNG_KSCTL = (KS_OWNER_ECC << CRPT_PRNG_KSCTL_OWNER_Pos) |
(CRPT_PRNG_KSCTL_ECDH_Msk) |
(CRPT_PRNG_KSCTL_WDST_Msk) |
(KS_SRAM << CRPT_PRNG_KSCTL_WSDST_Pos);
return 0;
}
/**
* @brief To generate a key to KS SRAM for ECDH.
*
* @return -1 Failed
* Others The key number in KS SRAM
*
* @details The function is used to generate a key to KS SRAM for ECDH.
* This key is necessary for ECDH+Key Store function of ECC.
*/
int32_t RNG_ECDH(uint32_t u32KeySize)
{
int32_t timeout;
int32_t i;
/* Reload seed only at first time */
CRPT->PRNG_CTL = (u32KeySize << CRPT_PRNG_CTL_KEYSZ_Pos) | CRPT_PRNG_CTL_START_Msk | PRNG_CTL_SEEDSRC_TRNG;
timeout = 0x10000;
i = 0;
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk)
{
if (i++ > timeout)
return -1;
}
if (CRPT->PRNG_KSSTS & CRPT_PRNG_KSSTS_KCTLERR_Msk)
return -1;
return (CRPT->PRNG_KSSTS & CRPT_PRNG_KSCTL_NUM_Msk);
}
/**
* @brief To generate entropy from hardware entropy source (TRNG)
*
* @return -1 Failed
* Others The bytes in pu8Out buffer
*
* @details The function is used to generate entropy from TRNG.
*/
int32_t RNG_EntropyPoll(uint8_t *pu8Out, int32_t i32Len)
{
int32_t timeout;
int32_t i;
if ((TRNG->CTL & TRNG_CTL_READY_Msk) == 0)
{
/* TRNG is not in active */
printf("trng is not active\n");
return -1;
}
/* Trigger entropy generate */
TRNG->CTL |= TRNG_CTL_TRNGEN_Msk;
for (i = 0; i < i32Len; i++)
{
timeout = SystemCoreClock;
while ((TRNG->CTL & TRNG_CTL_DVIF_Msk) == 0)
{
if (timeout-- <= 0)
{
/* Timeout error */
printf("timeout\n");
return -1;
}
}
/* Get one byte entroy */
*pu8Out++ = TRNG->DATA;
}
return i32Len;
}
/**@}*/ /* end of group RNG_EXPORTED_FUNCTIONS */
/**@}*/ /* end of group RNG_Driver */
/**@}*/ /* end of group Standard_Driver */
|