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