Rahul Dahiya / Mbed OS STM32F7 Ethernet
Committer:
rahul_dahiya
Date:
Wed Jan 15 15:57:15 2020 +0530
Revision:
0:fb8047b156bb
STM32F7 LWIP

Who changed what in which revision?

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