2023-09-04 01:35 AM
Hi,
I am using MBEDTLS/LWIP/FREERTOS on STMCube Ide v1.10.0
I noticed by calling manually `mbedtls_hardware_poll` that the returned values are only random by group of 4 chars:
When asking for 32 random bytes, the buffer returned is the following:
res=[62 62 62 62 78 78 78 78 49 49 49 49 D9 D9 D9 D9 80 80 80 80 E8 E8 E8 E8 FB FB FB FB 0E 0E 0E 0E ]
Here is the generated code:
int mbedtls_hardware_poll( void *Data, unsigned char *Output, size_t Len, size_t *oLen )
{
uint32_t index;
uint32_t randomValue;
for (index = 0; index < Len/4; index++)
{
if (HAL_RNG_GenerateRandomNumber(&hrng, &randomValue) == HAL_OK)
{
*oLen += 4;
memset(&(Output[index * 4]), (int)randomValue, 4);
}
else
{
Error_Handler();
}
}
return 0;
}
But the memset will write every 32-bit word with only 8 random bits (See `memset` page : the `c` parameter is int but the filled value uses it as a char.
Replacing the `memset` line by the following line seems to fix the issue:
memcpy(&(Output[index * 4]), &randomValue, 4);
And now the returned buffer is:
res=[50 4E C4 1E DD E5 E0 BC 76 74 3D 93 A6 FD A6 D8 F4 15 8C D4 86 86 45 C6 F9 D6 AB 12 81 37 06 5E ]
Note that I have also checked that the generated code under Ide v1.13.0 is the same