TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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