[M487/NUC472] Refine for correctness control

pull/5454/head
cyliangtw 2017-11-10 16:22:35 +08:00
parent e252b10148
commit 2ee058be53
2 changed files with 12 additions and 14 deletions

View File

@ -83,21 +83,20 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
{
(void)obj;
unsigned char tmpBuff[32];
*output_length = 0;
size_t cur_length = 0;
for (unsigned i = 0; i < (length/32); i++) {
trng_get(output);
*output_length += 32;
cur_length += 32;
output += 32;
}
if( length > *output_length ) {
if( length > cur_length ) {
trng_get(tmpBuff);
memcpy(output, &tmpBuff, (length - *output_length));
*output_length = length;
memcpy(output, &tmpBuff, (length - cur_length));
cur_length = length;
trng_zeroize(tmpBuff, sizeof(tmpBuff));
}
*output_length = cur_length;
return 0;
}

View File

@ -88,21 +88,20 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l
{
(void)obj;
unsigned char tmpBuff[32];
*output_length = 0;
size_t cur_length = 0;
for (unsigned i = 0; i < (length/32); i++) {
trng_get(output);
*output_length += 32;
cur_length += 32;
output += 32;
}
if( length > *output_length ) {
if( length > cur_length ) {
trng_get(tmpBuff);
memcpy(output, &tmpBuff, (length - *output_length));
*output_length = length;
memcpy(output, &tmpBuff, (length - cur_length));
cur_length = length;
trng_zeroize(tmpBuff, sizeof(tmpBuff));
}
*output_length = cur_length;
return 0;
}