mbed
Fork of mbed-dev by
targets/TARGET_STM/trng_api.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 178:d650f5d4c87a
mbed-dev library. Release version 159.
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 | |
<> | 149:156823d33999 | 21 | #if defined(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 | 178:d650f5d4c87a | 26 | #include "mbed_error.h" |
AnnaBridge | 178:d650f5d4c87a | 27 | #include "mbed_critical.h" |
<> | 149:156823d33999 | 28 | |
AnnaBridge | 178:d650f5d4c87a | 29 | static uint8_t users = 0; |
<> | 149:156823d33999 | 30 | |
<> | 149:156823d33999 | 31 | void trng_init(trng_t *obj) |
<> | 149:156823d33999 | 32 | { |
AnnaBridge | 178:d650f5d4c87a | 33 | uint32_t dummy; |
AnnaBridge | 178:d650f5d4c87a | 34 | |
AnnaBridge | 178:d650f5d4c87a | 35 | /* We're only supporting a single user of RNG */ |
AnnaBridge | 178:d650f5d4c87a | 36 | if (core_util_atomic_incr_u8(&users, 1) > 1 ) { |
AnnaBridge | 178:d650f5d4c87a | 37 | error("Only 1 RNG instance supported\r\n"); |
AnnaBridge | 178:d650f5d4c87a | 38 | } |
AnnaBridge | 178:d650f5d4c87a | 39 | |
<> | 149:156823d33999 | 40 | #if defined(TARGET_STM32L4) |
<> | 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; |
<> | 149:156823d33999 | 45 | PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL; |
<> | 149:156823d33999 | 46 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
<> | 149:156823d33999 | 47 | #endif |
<> | 149:156823d33999 | 48 | |
<> | 149:156823d33999 | 49 | /* RNG Peripheral clock enable */ |
<> | 149:156823d33999 | 50 | __HAL_RCC_RNG_CLK_ENABLE(); |
<> | 149:156823d33999 | 51 | |
<> | 149:156823d33999 | 52 | /* Initialize RNG instance */ |
<> | 149:156823d33999 | 53 | obj->handle.Instance = RNG; |
AnnaBridge | 178:d650f5d4c87a | 54 | obj->handle.State = HAL_RNG_STATE_RESET; |
AnnaBridge | 178:d650f5d4c87a | 55 | obj->handle.Lock = HAL_UNLOCKED; |
AnnaBridge | 178:d650f5d4c87a | 56 | |
<> | 149:156823d33999 | 57 | HAL_RNG_Init(&obj->handle); |
<> | 149:156823d33999 | 58 | |
<> | 149:156823d33999 | 59 | /* first random number generated after setting the RNGEN bit should not be used */ |
AnnaBridge | 178:d650f5d4c87a | 60 | HAL_RNG_GenerateRandomNumber(&obj->handle, &dummy); |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | |
<> | 149:156823d33999 | 63 | void trng_free(trng_t *obj) |
<> | 149:156823d33999 | 64 | { |
<> | 149:156823d33999 | 65 | /*Disable the RNG peripheral */ |
<> | 149:156823d33999 | 66 | HAL_RNG_DeInit(&obj->handle); |
<> | 149:156823d33999 | 67 | /* RNG Peripheral clock disable - assume we're the only users of RNG */ |
<> | 149:156823d33999 | 68 | __HAL_RCC_RNG_CLK_DISABLE(); |
AnnaBridge | 178:d650f5d4c87a | 69 | |
AnnaBridge | 178:d650f5d4c87a | 70 | users = 0; |
<> | 149:156823d33999 | 71 | } |
<> | 149:156823d33999 | 72 | |
<> | 149:156823d33999 | 73 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
<> | 149:156823d33999 | 74 | { |
AnnaBridge | 178:d650f5d4c87a | 75 | int ret = 0; |
AnnaBridge | 178:d650f5d4c87a | 76 | volatile uint8_t random[4]; |
AnnaBridge | 178:d650f5d4c87a | 77 | *output_length = 0; |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | /* Get Random byte */ |
AnnaBridge | 178:d650f5d4c87a | 80 | while ((*output_length < length) && (ret ==0)) { |
AnnaBridge | 178:d650f5d4c87a | 81 | if ( HAL_RNG_GenerateRandomNumber(&obj->handle, (uint32_t *)random ) != HAL_OK) { |
AnnaBridge | 178:d650f5d4c87a | 82 | ret = -1; |
AnnaBridge | 178:d650f5d4c87a | 83 | } else { |
AnnaBridge | 178:d650f5d4c87a | 84 | for (uint8_t i =0; (i < 4) && (*output_length < length) ; i++) { |
AnnaBridge | 178:d650f5d4c87a | 85 | *output++ = random[i]; |
AnnaBridge | 178:d650f5d4c87a | 86 | *output_length += 1; |
AnnaBridge | 178:d650f5d4c87a | 87 | random[i] = 0; |
AnnaBridge | 178:d650f5d4c87a | 88 | } |
AnnaBridge | 178:d650f5d4c87a | 89 | } |
<> | 149:156823d33999 | 90 | } |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | /* Just be extra sure that we didn't do it wrong */ |
<> | 149:156823d33999 | 93 | if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { |
<> | 149:156823d33999 | 94 | ret = -1; |
<> | 149:156823d33999 | 95 | } |
<> | 149:156823d33999 | 96 | |
<> | 149:156823d33999 | 97 | return( ret ); |
<> | 149:156823d33999 | 98 | } |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | #endif |