test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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