Skip to content

Commit 24832ca

Browse files
committed
Add random generator for stm32L0 + stm32L4
1 parent 4f1f272 commit 24832ca

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Hardware entropy collector for the STM32L0 family
3+
*
4+
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
22+
#if defined (TARGET_STM32L052xx) || defined (TARGET_STM32L053xx) || defined (TARGET_STM32L062xx) || defined (TARGET_STM32L063xx) || \
23+
defined (TARGET_STM32L072xx) || defined (TARGET_STM32L073xx) || defined (TARGET_STM32L082xx) || defined (TARGET_STM32L083xx)
24+
25+
#include <stdlib.h>
26+
#include "cmsis.h"
27+
28+
/* RNG handler declaration */
29+
RNG_HandleTypeDef RngHandle;
30+
31+
32+
/** rng_get_byte
33+
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
34+
* @param pointer to the hardware generated random byte.
35+
*/
36+
static void rng_get_byte( unsigned char *byte )
37+
{
38+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
39+
}
40+
41+
42+
/** mbedtls_hardware_poll
43+
* @brief Get len bytes of entropy from the hardware RNG.
44+
* @param data pointer will be NULL
45+
* @param output pointer to the random generated bytes buffer
46+
* @param len input is the requested length of bytes to be generated
47+
* @param olen is the pointer to the length of bytes effectively generated
48+
* @returns 0 if the generation went well. -1 in case of error
49+
*/
50+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
51+
{
52+
int ret;
53+
((void) data);
54+
55+
/* RNG Peripheral clock enable */
56+
__HAL_RCC_RNG_CLK_ENABLE();
57+
58+
/* Initialize RNG instance */
59+
RngHandle.Instance = RNG;
60+
HAL_RNG_Init(&RngHandle);
61+
62+
/* Get Random byte */
63+
for( uint32_t i = 0; i < len; i++ ){
64+
rng_get_byte( output + i );
65+
66+
}
67+
*olen = len;
68+
/* Just be extra sure that we didn't do it wrong */
69+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
70+
ret = -1;
71+
} else {
72+
ret = 0;
73+
}
74+
/*Disable the RNG peripheral */
75+
HAL_RNG_DeInit(&RngHandle);
76+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
77+
__HAL_RCC_RNG_CLK_DISABLE();
78+
79+
80+
return( ret );
81+
}
82+
#endif /* STM32L073RZ */
83+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Hardware entropy collector for the STM32L4 family
3+
*
4+
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
22+
#include <stdlib.h>
23+
#include "cmsis.h"
24+
25+
/* RNG handler declaration */
26+
RNG_HandleTypeDef RngHandle;
27+
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.
32+
*/
33+
static void rng_get_byte( unsigned char *byte )
34+
{
35+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
36+
}
37+
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
46+
*/
47+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
48+
{
49+
int ret;
50+
((void) data);
51+
52+
/* RNG Peripheral clock enable */
53+
__HAL_RCC_RNG_CLK_ENABLE();
54+
55+
/* Initialize RNG instance */
56+
RngHandle.Instance = RNG;
57+
HAL_RNG_Init(&RngHandle);
58+
59+
/* Get Random byte */
60+
for( uint32_t i = 0; i < len; i++ ){
61+
rng_get_byte( output + i );
62+
63+
}
64+
*olen = len;
65+
/* Just be extra sure that we didn't do it wrong */
66+
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
67+
ret = -1;
68+
} else {
69+
ret = 0;
70+
}
71+
/*Disable the RNG peripheral */
72+
HAL_RNG_DeInit(&RngHandle);
73+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
74+
__HAL_RCC_RNG_CLK_DISABLE();
75+
76+
77+
return( ret );
78+
}
79+
80+

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@
915915
"supported_form_factors": ["ARDUINO", "MORPHO"],
916916
"core": "Cortex-M0+",
917917
"default_toolchain": "ARM",
918-
"extra_labels": ["STM", "STM32L0", "STM32L073RZ"],
918+
"extra_labels": ["STM", "STM32L0", "STM32L073RZ", "STM32L073xx"],
919919
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
920920
"inherits": ["Target"],
921921
"detect_code": ["0760"],

0 commit comments

Comments
 (0)