Skip to content

Commit 76c2c19

Browse files
committed
[M487/NUC472] Unified code-path for remaining bytes of TRNG_Get
1 parent 4118afa commit 76c2c19

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

targets/TARGET_NUVOTON/TARGET_M480/trng_api.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
static volatile int g_PRNG_done;
2929
volatile int g_AES_done;
3030

31+
/* Implementation that should never be optimized out by the compiler */
32+
static void trng_zeroize( void *v, size_t n ) {
33+
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
34+
}
35+
3136
void CRYPTO_IRQHandler()
3237
{
3338
if (PRNG_GET_INT_FLAG()) {
@@ -78,23 +83,19 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
7883
{
7984
(void)obj;
8085
unsigned char tmpBuff[32];
81-
86+
8287
*output_length = 0;
83-
if (length < 32) {
88+
89+
for (unsigned i = 0; i < (length/32); i++) {
90+
trng_get(output);
91+
*output_length += 32;
92+
output += 32;
93+
}
94+
if( length > *output_length ) {
95+
trng_zeroize(tmpBuff, sizeof(tmpBuff));
8496
trng_get(tmpBuff);
85-
memcpy(output, &tmpBuff, length);
97+
memcpy(output, &tmpBuff, (length - *output_length));
8698
*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-
}
93-
if( length > *output_length ) {
94-
trng_get(tmpBuff);
95-
memcpy(output, &tmpBuff, (length - *output_length));
96-
*output_length = length;
97-
}
9899
}
99100

100101
return 0;

targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
static volatile int g_PRNG_done;
3434
volatile int g_AES_done;
3535

36+
/* Implementation that should never be optimized out by the compiler */
37+
static void trng_zeroize( void *v, size_t n ) {
38+
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
39+
}
40+
3641
void CRYPTO_IRQHandler()
3742
{
3843
if (PRNG_GET_INT_FLAG()) {
@@ -85,21 +90,17 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
8590
unsigned char tmpBuff[32];
8691

8792
*output_length = 0;
88-
if (length < 32) {
93+
94+
for (unsigned i = 0; i < (length/32); i++) {
95+
trng_get(output);
96+
*output_length += 32;
97+
output += 32;
98+
}
99+
if( length > *output_length ) {
100+
trng_zeroize(tmpBuff, sizeof(tmpBuff));
89101
trng_get(tmpBuff);
90-
memcpy(output, &tmpBuff, length);
102+
memcpy(output, &tmpBuff, (length - *output_length));
91103
*output_length = length;
92-
} else {
93-
for (unsigned i = 0; i < (length/32); i++) {
94-
trng_get(output);
95-
*output_length += 32;
96-
output += 32;
97-
}
98-
if( length > *output_length ) {
99-
trng_get(tmpBuff);
100-
memcpy(output, &tmpBuff, (length - *output_length));
101-
*output_length = length;
102-
}
103104
}
104105

105106
return 0;

0 commit comments

Comments
 (0)