00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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