Skip to content

Commit 2ee058b

Browse files
committed
[M487/NUC472] Refine for correctness control
1 parent e252b10 commit 2ee058b

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

targets/TARGET_NUVOTON/TARGET_M480/trng_api.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,20 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
8383
{
8484
(void)obj;
8585
unsigned char tmpBuff[32];
86-
87-
*output_length = 0;
86+
size_t cur_length = 0;
8887

8988
for (unsigned i = 0; i < (length/32); i++) {
9089
trng_get(output);
91-
*output_length += 32;
90+
cur_length += 32;
9291
output += 32;
9392
}
94-
if( length > *output_length ) {
93+
if( length > cur_length ) {
9594
trng_get(tmpBuff);
96-
memcpy(output, &tmpBuff, (length - *output_length));
97-
*output_length = length;
95+
memcpy(output, &tmpBuff, (length - cur_length));
96+
cur_length = length;
9897
trng_zeroize(tmpBuff, sizeof(tmpBuff));
9998
}
100-
99+
*output_length = cur_length;
101100
return 0;
102101
}
103102

targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,20 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
8888
{
8989
(void)obj;
9090
unsigned char tmpBuff[32];
91-
92-
*output_length = 0;
91+
size_t cur_length = 0;
9392

9493
for (unsigned i = 0; i < (length/32); i++) {
9594
trng_get(output);
96-
*output_length += 32;
95+
cur_length += 32;
9796
output += 32;
9897
}
99-
if( length > *output_length ) {
98+
if( length > cur_length ) {
10099
trng_get(tmpBuff);
101-
memcpy(output, &tmpBuff, (length - *output_length));
102-
*output_length = length;
100+
memcpy(output, &tmpBuff, (length - cur_length));
101+
cur_length = length;
103102
trng_zeroize(tmpBuff, sizeof(tmpBuff));
104103
}
105-
104+
*output_length = cur_length;
106105
return 0;
107106
}
108107

0 commit comments

Comments
 (0)