Skip to content

Commit ab1b3ae

Browse files
Merge pull request #5454 from OpenNuvoton/trng_get_unalignment
Nuvoton: TRNG_Get support 32 bytes unalignment
2 parents 1ea4e4c + d8a9e35 commit ab1b3ae

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

targets/TARGET_NUVOTON/TARGET_M480/trng_api.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@
2525
/*
2626
* Get Random number generator.
2727
*/
28+
29+
#define PRNG_KEY_SIZE (0x20UL)
30+
2831
static volatile int g_PRNG_done;
2932
volatile int g_AES_done;
3033

34+
/* Implementation that should never be optimized out by the compiler */
35+
static void trng_zeroize( void *v, size_t n ) {
36+
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
37+
}
38+
3139
void CRYPTO_IRQHandler()
3240
{
3341
if (PRNG_GET_INT_FLAG()) {
@@ -77,21 +85,22 @@ void trng_free(trng_t *obj)
7785
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
7886
{
7987
(void)obj;
80-
81-
*output_length = 0;
82-
if (length < 32) {
83-
unsigned char tmpBuff[32];
88+
unsigned char tmpBuff[PRNG_KEY_SIZE];
89+
size_t cur_length = 0;
90+
91+
while (length >= sizeof(tmpBuff)) {
92+
trng_get(output);
93+
output += sizeof(tmpBuff);
94+
cur_length += sizeof(tmpBuff);
95+
length -= sizeof(tmpBuff);
96+
}
97+
if (length > 0) {
8498
trng_get(tmpBuff);
85-
memcpy(output, &tmpBuff, length);
86-
*output_length = length;
87-
} else {
88-
for (unsigned i = 0; i < (length/32); i++) {
89-
trng_get(output);
90-
*output_length += 32;
91-
output += 32;
92-
}
99+
memcpy(output, tmpBuff, length);
100+
cur_length += length;
101+
trng_zeroize(tmpBuff, sizeof(tmpBuff));
93102
}
94-
103+
*output_length = cur_length;
95104
return 0;
96105
}
97106

targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@
3030
/*
3131
* Get Random number generator.
3232
*/
33+
34+
#define PRNG_KEY_SIZE (0x20UL)
35+
3336
static volatile int g_PRNG_done;
3437
volatile int g_AES_done;
3538

39+
/* Implementation that should never be optimized out by the compiler */
40+
static void trng_zeroize( void *v, size_t n ) {
41+
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
42+
}
43+
3644
void CRYPTO_IRQHandler()
3745
{
3846
if (PRNG_GET_INT_FLAG()) {
@@ -82,21 +90,22 @@ void trng_free(trng_t *obj)
8290
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
8391
{
8492
(void)obj;
85-
86-
*output_length = 0;
87-
if (length < 32) {
88-
unsigned char tmpBuff[32];
93+
unsigned char tmpBuff[PRNG_KEY_SIZE];
94+
size_t cur_length = 0;
95+
96+
while (length >= sizeof(tmpBuff)) {
97+
trng_get(output);
98+
output += sizeof(tmpBuff);
99+
cur_length += sizeof(tmpBuff);
100+
length -= sizeof(tmpBuff);
101+
}
102+
if (length > 0) {
89103
trng_get(tmpBuff);
90-
memcpy(output, &tmpBuff, length);
91-
*output_length = length;
92-
} else {
93-
for (int i = 0; i < (length/32); i++) {
94-
trng_get(output);
95-
*output_length += 32;
96-
output += 32;
97-
}
104+
memcpy(output, tmpBuff, length);
105+
cur_length += length;
106+
trng_zeroize(tmpBuff, sizeof(tmpBuff));
98107
}
99-
108+
*output_length = cur_length;
100109
return 0;
101110
}
102111

0 commit comments

Comments
 (0)