-
Notifications
You must be signed in to change notification settings - Fork 3k
STM32: TRNG: remove call to deprecated HAL_RNG_GetRandomNumber #5179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
6deaa96
to
73d9da1
Compare
LGTM |
73d9da1
to
b0854a3
Compare
@@ -63,23 +66,31 @@ void trng_free(trng_t *obj) | |||
HAL_RNG_DeInit(&obj->handle); | |||
/* RNG Peripheral clock disable - assume we're the only users of RNG */ | |||
__HAL_RCC_RNG_CLK_DISABLE(); | |||
|
|||
users = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 36 - uses atomic access, not here when you are setting it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the free case, I don't have to test the value, I just have to set it to unused, so I preferred to keep it simple: as there is only 1 user, once freed, it will always be 0.
An alternative is to throw an error in case of unbalanced, (like in sleep manager):
core_util_critical_section_enter();
if (users == 0) {
core_util_critical_section_exit();
error("Unbalanced number of trng free (< 0)");
}
core_util_atomic_decr_u16(&users, 1);
core_util_critical_section_exit();
but that seemed overkill to me ...
I can do the change anyway if people think it's of added value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, can be as it is.
Why init can be called only once - one owner limitation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes - one HW only and only one client in MBED in features/mbedtls/platform/src/mbed_trng.c, so I'll stick to it for now.
ARM Internal Ref: IOTSSL-1790 |
bump for review |
Hello @0xc0170 Kind regards |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left one comment about a corner case where a buffer overflow could happen
targets/TARGET_STM/trng_api.c
Outdated
ret = -1; | ||
} else { | ||
for (uint8_t i =0; i < 4; i++) { | ||
*output++ = random[i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about corner cases where output length will not be a multiple of 4? In this case you will overflow the buffer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I'd covered it, but clearly missed here - will fix - thx Ron !
b0854a3
to
91c6b42
Compare
@RonEld thanks for review - I pushed an update to the code which should address the issue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @LMESTM for the changes.
Looks good to me
thanks again @RonEld
|
@0xc0170 Could you please wait for my input? I'll have a look at it quickly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good in general, I just have a minor request and a question.
for( uint32_t i = 0; i < length; i++ ){ | ||
trng_get_byte(obj, output + i ); | ||
while ((*output_length < length) && (ret ==0)) { | ||
if ( HAL_RNG_GenerateRandomNumber(&obj->handle, (uint32_t *)random ) != HAL_OK) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we call HAL_RNG_GenerateRandomNumber()
without calling trng_init()
first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HAL would return an error in such case.
/* Just be extra sure that we didn't do it wrong */ | ||
if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { | ||
ret = -1; | ||
} else { | ||
ret = 0; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that I am being paranoid, but could we please wipe the random[]
buffer before returning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure - will do so and post an update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I made an update and now random is erased immediately after use.
Build : SUCCESSBuild number : 119 Triggering tests/test mbed-os |
Test : SUCCESSBuild number : 50 |
91c6b42
to
8251ff4
Compare
Build : SUCCESSBuild number : 157 Triggering tests/test mbed-os |
targets/TARGET_STM/trng_api.c
Outdated
} | ||
|
||
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) | ||
{ | ||
int ret; | ||
int ret = 0; | ||
uint8_t random[4]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this way most of the compilers would optimise the out the instructions zeroising the array, declaring random[]
to be volatile might be necessary to prevent it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yet another good point - thanks !
done
HAL_RNG_GetRandomNumber is a deprecated API and replaced here with a call to HAL_RNG_GenerateRandomNumber. Doing so, we also rework the driver to use the 4 bytes returned by a call to HAL_RNG_GenerateRandomNumber instead of 1 byte out of 4. HAL_RNG_GenerateRandomNumber was not returning any error code, so now we can also check the return code.
There is only 1 RNG HW IP and we do not support more than one driver user at a time, so let's ensure this is the case and raise an error if needed.
8251ff4
to
849749f
Compare
/morph build |
/morph build |
Build : SUCCESSBuild number : 289 Triggering tests/morph test |
Build : SUCCESSBuild number : 292 Triggering tests/morph test |
Test : SUCCESSBuild number : 131 |
Description
HAL_RNG_GetRandomNumber is a deprecated API and replaced here with a call to HAL_RNG_GenerateRandomNumber. Doing so, we also rework the driver to use the 4 bytes returned by a call to HAL_RNG_GenerateRandomNumber instead of 1 byte out of 4.
HAL_RNG_GenerateRandomNumber was not returning any error code, so now we can also check the return code.
A second commit ensures that there is a single user at a time.
Status
READY
Tests