inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /*
NYX 0:85b3fd62ea1a 2 * Hardware entropy collector for the STM32 families
NYX 0:85b3fd62ea1a 3 *
NYX 0:85b3fd62ea1a 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
NYX 0:85b3fd62ea1a 5 * SPDX-License-Identifier: Apache-2.0
NYX 0:85b3fd62ea1a 6 *
NYX 0:85b3fd62ea1a 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
NYX 0:85b3fd62ea1a 8 * not use this file except in compliance with the License.
NYX 0:85b3fd62ea1a 9 * You may obtain a copy of the License at
NYX 0:85b3fd62ea1a 10 *
NYX 0:85b3fd62ea1a 11 * http://www.apache.org/licenses/LICENSE-2.0
NYX 0:85b3fd62ea1a 12 *
NYX 0:85b3fd62ea1a 13 * Unless required by applicable law or agreed to in writing, software
NYX 0:85b3fd62ea1a 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
NYX 0:85b3fd62ea1a 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NYX 0:85b3fd62ea1a 16 * See the License for the specific language governing permissions and
NYX 0:85b3fd62ea1a 17 * limitations under the License.
NYX 0:85b3fd62ea1a 18 *
NYX 0:85b3fd62ea1a 19 */
NYX 0:85b3fd62ea1a 20
NYX 0:85b3fd62ea1a 21 #if defined(DEVICE_TRNG)
NYX 0:85b3fd62ea1a 22
NYX 0:85b3fd62ea1a 23 #include <stdlib.h>
NYX 0:85b3fd62ea1a 24 #include "cmsis.h"
NYX 0:85b3fd62ea1a 25 #include "trng_api.h"
NYX 0:85b3fd62ea1a 26
NYX 0:85b3fd62ea1a 27 /** trng_get_byte
NYX 0:85b3fd62ea1a 28 * @brief Get one byte of entropy from the RNG, assuming it is up and running.
NYX 0:85b3fd62ea1a 29 * @param obj TRNG obj
NYX 0:85b3fd62ea1a 30 * @param pointer to the hardware generated random byte.
NYX 0:85b3fd62ea1a 31 */
NYX 0:85b3fd62ea1a 32 static void trng_get_byte(trng_t *obj, unsigned char *byte )
NYX 0:85b3fd62ea1a 33 {
NYX 0:85b3fd62ea1a 34 *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle);
NYX 0:85b3fd62ea1a 35 }
NYX 0:85b3fd62ea1a 36
NYX 0:85b3fd62ea1a 37 void trng_init(trng_t *obj)
NYX 0:85b3fd62ea1a 38 {
NYX 0:85b3fd62ea1a 39 #if defined(TARGET_STM32L4)
NYX 0:85b3fd62ea1a 40 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
NYX 0:85b3fd62ea1a 41
NYX 0:85b3fd62ea1a 42 /*Select PLLQ output as RNG clock source */
NYX 0:85b3fd62ea1a 43 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG;
NYX 0:85b3fd62ea1a 44 PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL;
NYX 0:85b3fd62ea1a 45 HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
NYX 0:85b3fd62ea1a 46 #endif
NYX 0:85b3fd62ea1a 47
NYX 0:85b3fd62ea1a 48 /* RNG Peripheral clock enable */
NYX 0:85b3fd62ea1a 49 __HAL_RCC_RNG_CLK_ENABLE();
NYX 0:85b3fd62ea1a 50
NYX 0:85b3fd62ea1a 51 /* Initialize RNG instance */
NYX 0:85b3fd62ea1a 52 obj->handle.Instance = RNG;
NYX 0:85b3fd62ea1a 53 HAL_RNG_Init(&obj->handle);
NYX 0:85b3fd62ea1a 54
NYX 0:85b3fd62ea1a 55 /* first random number generated after setting the RNGEN bit should not be used */
NYX 0:85b3fd62ea1a 56 HAL_RNG_GetRandomNumber(&obj->handle);
NYX 0:85b3fd62ea1a 57
NYX 0:85b3fd62ea1a 58 }
NYX 0:85b3fd62ea1a 59
NYX 0:85b3fd62ea1a 60 void trng_free(trng_t *obj)
NYX 0:85b3fd62ea1a 61 {
NYX 0:85b3fd62ea1a 62 /*Disable the RNG peripheral */
NYX 0:85b3fd62ea1a 63 HAL_RNG_DeInit(&obj->handle);
NYX 0:85b3fd62ea1a 64 /* RNG Peripheral clock disable - assume we're the only users of RNG */
NYX 0:85b3fd62ea1a 65 __HAL_RCC_RNG_CLK_DISABLE();
NYX 0:85b3fd62ea1a 66 }
NYX 0:85b3fd62ea1a 67
NYX 0:85b3fd62ea1a 68 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
NYX 0:85b3fd62ea1a 69 {
NYX 0:85b3fd62ea1a 70 int ret;
NYX 0:85b3fd62ea1a 71
NYX 0:85b3fd62ea1a 72 /* Get Random byte */
NYX 0:85b3fd62ea1a 73 for( uint32_t i = 0; i < length; i++ ){
NYX 0:85b3fd62ea1a 74 trng_get_byte(obj, output + i );
NYX 0:85b3fd62ea1a 75 }
NYX 0:85b3fd62ea1a 76
NYX 0:85b3fd62ea1a 77 *output_length = length;
NYX 0:85b3fd62ea1a 78 /* Just be extra sure that we didn't do it wrong */
NYX 0:85b3fd62ea1a 79 if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) {
NYX 0:85b3fd62ea1a 80 ret = -1;
NYX 0:85b3fd62ea1a 81 } else {
NYX 0:85b3fd62ea1a 82 ret = 0;
NYX 0:85b3fd62ea1a 83 }
NYX 0:85b3fd62ea1a 84
NYX 0:85b3fd62ea1a 85 return( ret );
NYX 0:85b3fd62ea1a 86 }
NYX 0:85b3fd62ea1a 87
NYX 0:85b3fd62ea1a 88 #endif