|
| 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 | + |
0 commit comments