Skip to content

Commit 824fff5

Browse files
committed
Add documentation + reformat the code
1 parent 4c50ec2 commit 824fff5

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

hal/targets/hal/TARGET_STM/TARGET_STM32F4/entropy_hardware_poll.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,27 @@
3030
/* RNG handler declaration */
3131
RNG_HandleTypeDef RngHandle;
3232

33-
/*
34-
* Get one byte of entropy from the RNG, assuming it is up and running.
33+
34+
/** rng_get_byte
35+
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
36+
* @param pointer to the hardware generated random byte.
3537
*/
3638
static void rng_get_byte( unsigned char *byte )
3739
{
3840
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
3941
}
4042

41-
/*
42-
* Get len bytes of entropy from the hardware RNG.
43+
44+
/** mbedtls_hardware_poll
45+
* @brief Get len bytes of entropy from the hardware RNG.
46+
* @param data pointer will be NULL
47+
* @param output pointer to the random generated bytes buffer
48+
* @param len input is the requested length of bytes to be generated
49+
* @param olen is the pointer to the length of bytes effectively generated
50+
* @returns 0 if the generation went well. -1 in case of error
4351
*/
44-
int mbedtls_hardware_poll( void *data,
45-
unsigned char *output, size_t len, size_t *olen )
52+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
4653
{
47-
size_t i;
4854
int ret;
4955
((void) data);
5056

@@ -56,22 +62,17 @@ int mbedtls_hardware_poll( void *data,
5662
HAL_RNG_Init(&RngHandle);
5763

5864
/* Get Random byte */
59-
for( i = 0; i < len; i++ ){
65+
for( uint32_t i = 0; i < len; i++ ){
6066
rng_get_byte( output + i );
61-
printf("output %i: %i\n",i,(int)*(output+i));
6267

6368
}
69+
*olen = len;
6470
/* Just be extra sure that we didn't do it wrong */
65-
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 )
66-
{
71+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
6772
ret = -1;
68-
goto cleanup;
73+
} else {
74+
ret = 0;
6975
}
70-
71-
*olen = len;
72-
ret = 0;
73-
74-
cleanup:
7576
/*Disable the RNG peripheral */
7677
HAL_RNG_DeInit(&RngHandle);
7778
/* RNG Peripheral clock disable - assume we're the only users of RNG */

hal/targets/hal/TARGET_STM/TARGET_STM32F7/entropy_hardware_poll.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,27 @@
2525
/* RNG handler declaration */
2626
RNG_HandleTypeDef RngHandle;
2727

28-
/*
29-
* Get one byte of entropy from the RNG, assuming it is up and running.
28+
29+
/** rng_get_byte
30+
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
31+
* @param pointer to the hardware generated random byte.
3032
*/
3133
static void rng_get_byte( unsigned char *byte )
3234
{
3335
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
3436
}
3537

36-
/*
37-
* Get len bytes of entropy from the hardware RNG.
38+
39+
/** mbedtls_hardware_poll
40+
* @brief Get len bytes of entropy from the hardware RNG.
41+
* @param data pointer will be NULL
42+
* @param output pointer to the random generated bytes buffer
43+
* @param len input is the requested length of bytes to be generated
44+
* @param olen is the pointer to the length of bytes effectively generated
45+
* @returns 0 if the generation went well. -1 in case of error
3846
*/
39-
int mbedtls_hardware_poll( void *data,
40-
unsigned char *output, size_t len, size_t *olen )
47+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
4148
{
42-
size_t i;
4349
int ret;
4450
((void) data);
4551

@@ -50,22 +56,18 @@ int mbedtls_hardware_poll( void *data,
5056
RngHandle.Instance = RNG;
5157
HAL_RNG_Init(&RngHandle);
5258

53-
5459
/* Get Random byte */
55-
for( i = 0; i < len; i++ )
60+
for( uint32_t i = 0; i < len; i++ ){
5661
rng_get_byte( output + i );
5762

63+
}
64+
*olen = len;
5865
/* Just be extra sure that we didn't do it wrong */
59-
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 )
60-
{
66+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
6167
ret = -1;
62-
goto cleanup;
68+
} else {
69+
ret = 0;
6370
}
64-
65-
*olen = len;
66-
ret = 0;
67-
68-
cleanup:
6971
/*Disable the RNG peripheral */
7072
HAL_RNG_DeInit(&RngHandle);
7173
/* RNG Peripheral clock disable - assume we're the only users of RNG */

0 commit comments

Comments
 (0)