Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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