Skip to content

Commit 5e351c2

Browse files
committed
NUMAKER_PFM_NUC472: Add RGN HAL API implementation
1 parent 72fac61 commit 5e351c2

File tree

3 files changed

+41
-53
lines changed

3 files changed

+41
-53
lines changed

hal/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@
20882088
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
20892089
"inherits": ["Target"],
20902090
"progen": {"target": "numaker-pfm-nuc472"},
2091-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"],
2091+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "RNG"],
20922092
"features": ["IPV4"],
20932093
"release_versions": ["2", "5"]
20942094
},

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ struct sleep_s {
124124
int powerdown;
125125
};
126126

127+
struct rng_s {
128+
129+
};
130+
127131
#ifdef __cplusplus
128132
}
129133
#endif

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/entropy_hardware_poll.c renamed to hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/rng_api.c

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "cmsis.h"
2525
#include "NUC472_442.h"
2626
#include "us_ticker_api.h"
27+
#include "rng_api.h"
2728

2829
/*
2930
* Get Random number generator.
@@ -43,73 +44,56 @@ static void rng_get(unsigned char *pConversionData)
4344
uint32_t *p32ConversionData;
4445

4546
p32ConversionData = (uint32_t *)pConversionData;
46-
47-
/* Unlock protected registers */
48-
SYS_UnlockReg();
49-
/* Enable IP clock */
50-
CLK_EnableModuleClock(CRPT_MODULE);
51-
52-
/* Lock protected registers */
53-
SYS_LockReg();
54-
55-
NVIC_EnableIRQ(CRPT_IRQn);
56-
PRNG_ENABLE_INT();
57-
47+
5848
// PRNG_Open(PRNG_KEY_SIZE_64, 0, 0);
5949
PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read());
6050
PRNG_Start();
6151
while (!g_PRNG_done);
6252

63-
6453
PRNG_Read(p32ConversionData);
6554

6655
// printf(" 0x%08x 0x%08x 0x%08x 0x%08x\n\r", *p32ConversionData, *(p32ConversionData+1), *(p32ConversionData+2), *(p32ConversionData+3));
6756
// printf(" 0x%08x 0x%08x 0x%08x 0x%08x\n\r", *(p32ConversionData+4), *(p32ConversionData+5), *(p32ConversionData+6), *(p32ConversionData+7));
57+
}
6858

59+
void rng_init(rng_t *obj)
60+
{
61+
/* Unlock protected registers */
62+
SYS_UnlockReg();
63+
/* Enable IP clock */
64+
CLK_EnableModuleClock(CRPT_MODULE);
65+
66+
/* Lock protected registers */
67+
SYS_LockReg();
68+
69+
NVIC_EnableIRQ(CRPT_IRQn);
70+
PRNG_ENABLE_INT();
71+
}
72+
73+
void rng_free(rng_t *obj)
74+
{
6975
PRNG_DISABLE_INT();
7076
NVIC_DisableIRQ(CRPT_IRQn);
71-
// CLK_DisableModuleClock(CRPT_MODULE);
72-
77+
//CLK_DisableModuleClock(CRPT_MODULE);
7378
}
7479

75-
76-
/*
77-
* Get len bytes of entropy from the hardware RNG.
78-
*/
79-
80-
int mbedtls_hardware_poll( void *data,
81-
unsigned char *output, size_t len, size_t *olen )
80+
int rng_get_numbers(rng_t *obj, uint8_t *output, size_t length, size_t *output_length)
8281
{
83-
#if 0
84-
unsigned long timer = us_ticker_read();
85-
((void) data);
86-
*olen = 0;
87-
88-
if( len < sizeof(unsigned long) )
89-
return( 0 );
90-
91-
memcpy( output, &timer, sizeof(unsigned long) );
92-
*olen = sizeof(unsigned long);
93-
#else
94-
*olen = 0;
95-
if( len < 32 )
96-
{
97-
unsigned char tmpBuff[32];
98-
rng_get(tmpBuff);
99-
memcpy( output, &tmpBuff, len );
100-
*olen = len;
101-
return( 0 );
102-
}
103-
for( int i = 0; i < (len/32) ; i++)
104-
{
105-
rng_get(output);
106-
*olen += 32;
107-
// printf("Output result of len[%d][%d]: 0x%08x 0x%08x\n\r", len, *olen, *((int32_t *)output), *(((int32_t *)output)+1));
108-
output += 32;
109-
}
110-
#endif
111-
112-
return( 0 );
82+
*output_length = 0;
83+
if (length < 32) {
84+
unsigned char tmpBuff[32];
85+
rng_get(tmpBuff);
86+
memcpy( output, &tmpBuff, length );
87+
*output_length = length;
88+
} else {
89+
for ( int i = 0; i < (length/32); i++) {
90+
rng_get(output);
91+
*output_length += 32;
92+
output += 32;
93+
}
94+
}
95+
96+
return 0;
11397
}
11498

11599

0 commit comments

Comments
 (0)