diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c b/hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c deleted file mode 100644 index c8fd7dbe5f..0000000000 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L0/trng_api.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Hardware entropy collector for the STM32L0 family - * - * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#if defined(DEVICE_TRNG) - -#if defined (TARGET_STM32L052xx) || defined (TARGET_STM32L053xx) || defined (TARGET_STM32L062xx) || defined (TARGET_STM32L063xx) || \ - defined (TARGET_STM32L072xx) || defined (TARGET_STM32L073xx) || defined (TARGET_STM32L082xx) || defined (TARGET_STM32L083xx) - -#include -#include "cmsis.h" -#include "trng_api.h" - -/** trng_get_byte - * @brief Get one byte of entropy from the RNG, assuming it is up and running. - * @param obj TRNG obj - * @param pointer to the hardware generated random byte. - */ -static void trng_get_byte(trng_t *obj, unsigned char *byte ) -{ - *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); -} - -void trng_init(trng_t *obj) -{ - /* RNG Peripheral clock enable */ - __HAL_RCC_RNG_CLK_ENABLE(); - - /* Initialize RNG instance */ - obj->handle.Instance = RNG; - HAL_RNG_Init(&obj->handle); - -} - -void trng_free(trng_t *obj) -{ - /*Disable the RNG peripheral */ - HAL_RNG_DeInit(&obj->handle); - /* RNG Peripheral clock disable - assume we're the only users of RNG */ - __HAL_RCC_RNG_CLK_DISABLE(); -} - -int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) -{ - int ret; - - /* Get Random byte */ - for( uint32_t i = 0; i < length; i++ ){ - trng_get_byte(obj, output + i ); - } - - *output_length = length; - /* 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; - } - - return( ret ); -} -#endif /* STM32L073RZ */ - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/trng_api.c b/targets/TARGET_STM/TARGET_STM32F4/trng_api.c deleted file mode 100644 index 54ac041e53..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F4/trng_api.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Hardware entropy collector for the STM32F4 family - * - * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#if defined(DEVICE_TRNG) - -#if defined(TARGET_STM32F405xx) || defined(TARGET_STM32F415xx) || defined(TARGET_STM32F407xx) || defined(TARGET_STM32F417xx) ||\ - defined(TARGET_STM32F427xx) || defined(TARGET_STM32F437xx) || defined(TARGET_STM32F429xx) || defined(TARGET_STM32F439xx) ||\ - defined(TARGET_STM32F410Tx) || defined(TARGET_STM32F410Cx) || defined(TARGET_STM32F410Rx) || defined(TARGET_STM32F469xx) ||\ - defined(TARGET_STM32F479xx) -#include -#include "cmsis.h" -#include "trng_api.h" - -/** trng_get_byte - * @brief Get one byte of entropy from the RNG, assuming it is up and running. - * @param obj TRNG obj - * @param pointer to the hardware generated random byte. - */ -static void trng_get_byte(trng_t *obj, unsigned char *byte ) -{ - *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); -} - -void trng_init(trng_t *obj) -{ - /* RNG Peripheral clock enable */ - __HAL_RCC_RNG_CLK_ENABLE(); - - /* Initialize RNG instance */ - obj->handle.Instance = RNG; - HAL_RNG_Init(&obj->handle); - -} - -void trng_free(trng_t *obj) -{ - /*Disable the RNG peripheral */ - HAL_RNG_DeInit(&obj->handle); - /* RNG Peripheral clock disable - assume we're the only users of RNG */ - __HAL_RCC_RNG_CLK_DISABLE(); -} - -int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) -{ - int ret; - - /* Get Random byte */ - for( uint32_t i = 0; i < length; i++ ){ - trng_get_byte(obj, output + i ); - } - - *output_length = length; - /* 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; - } - - return( ret ); -} -#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\ - STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx */ - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/trng_api.c b/targets/TARGET_STM/TARGET_STM32F7/trng_api.c deleted file mode 100644 index f435798f36..0000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/trng_api.c +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Hardware entropy collector for the STM32F7 family - * - * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#if defined(DEVICE_TRNG) - -#include -#include "cmsis.h" -#include "trng_api.h" - -/** trng_get_byte - * @brief Get one byte of entropy from the RNG, assuming it is up and running. - * @param obj TRNG obj - * @param pointer to the hardware generated random byte. - */ -static void trng_get_byte(trng_t *obj, unsigned char *byte ) -{ - *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); -} - -void trng_init(trng_t *obj) -{ - /* RNG Peripheral clock enable */ - __HAL_RCC_RNG_CLK_ENABLE(); - - /* Initialize RNG instance */ - obj->handle.Instance = RNG; - HAL_RNG_Init(&obj->handle); - -} - -void trng_free(trng_t *obj) -{ - /*Disable the RNG peripheral */ - HAL_RNG_DeInit(&obj->handle); - /* RNG Peripheral clock disable - assume we're the only users of RNG */ - __HAL_RCC_RNG_CLK_DISABLE(); -} - -int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) -{ - int ret; - - /* Get Random byte */ - for( uint32_t i = 0; i < length; i++ ){ - trng_get_byte(obj, output + i ); - } - - *output_length = length; - /* 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; - } - - return( ret ); -} - -#endif diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c b/targets/TARGET_STM/trng_api.c similarity index 91% rename from hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c rename to targets/TARGET_STM/trng_api.c index 64483b8f12..f10bf69e06 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/trng_api.c +++ b/targets/TARGET_STM/trng_api.c @@ -1,5 +1,5 @@ /* - * Hardware entropy collector for the STM32L4 family + * Hardware entropy collector for the STM32 families * * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved * SPDX-License-Identifier: Apache-2.0 @@ -36,12 +36,14 @@ static void trng_get_byte(trng_t *obj, unsigned char *byte ) void trng_init(trng_t *obj) { +#if defined(TARGET_STM32L4) RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; /*Select PLLQ output as RNG clock source */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG; PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); +#endif /* RNG Peripheral clock enable */ __HAL_RCC_RNG_CLK_ENABLE(); @@ -50,6 +52,9 @@ void trng_init(trng_t *obj) obj->handle.Instance = RNG; HAL_RNG_Init(&obj->handle); + /* first random number generated after setting the RNGEN bit should not be used */ + HAL_RNG_GetRandomNumber(&obj->handle); + } void trng_free(trng_t *obj) @@ -80,5 +85,4 @@ int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_l return( ret ); } - #endif