forked
targets/TARGET_STM/trng_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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" |
<> | 149:156823d33999 | 26 | |
<> | 149:156823d33999 | 27 | /** trng_get_byte |
<> | 149:156823d33999 | 28 | * @brief Get one byte of entropy from the RNG, assuming it is up and running. |
<> | 149:156823d33999 | 29 | * @param obj TRNG obj |
<> | 149:156823d33999 | 30 | * @param pointer to the hardware generated random byte. |
<> | 149:156823d33999 | 31 | */ |
<> | 149:156823d33999 | 32 | static void trng_get_byte(trng_t *obj, unsigned char *byte ) |
<> | 149:156823d33999 | 33 | { |
<> | 149:156823d33999 | 34 | *byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle); |
<> | 149:156823d33999 | 35 | } |
<> | 149:156823d33999 | 36 | |
<> | 149:156823d33999 | 37 | void trng_init(trng_t *obj) |
<> | 149:156823d33999 | 38 | { |
<> | 149:156823d33999 | 39 | #if defined(TARGET_STM32L4) |
<> | 149:156823d33999 | 40 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
<> | 149:156823d33999 | 41 | |
<> | 149:156823d33999 | 42 | /*Select PLLQ output as RNG clock source */ |
<> | 149:156823d33999 | 43 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG; |
<> | 149:156823d33999 | 44 | PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL; |
<> | 149:156823d33999 | 45 | HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); |
<> | 149:156823d33999 | 46 | #endif |
<> | 149:156823d33999 | 47 | |
<> | 149:156823d33999 | 48 | /* RNG Peripheral clock enable */ |
<> | 149:156823d33999 | 49 | __HAL_RCC_RNG_CLK_ENABLE(); |
<> | 149:156823d33999 | 50 | |
<> | 149:156823d33999 | 51 | /* Initialize RNG instance */ |
<> | 149:156823d33999 | 52 | obj->handle.Instance = RNG; |
<> | 149:156823d33999 | 53 | HAL_RNG_Init(&obj->handle); |
<> | 149:156823d33999 | 54 | |
<> | 149:156823d33999 | 55 | /* first random number generated after setting the RNGEN bit should not be used */ |
<> | 149:156823d33999 | 56 | HAL_RNG_GetRandomNumber(&obj->handle); |
<> | 149:156823d33999 | 57 | |
<> | 149:156823d33999 | 58 | } |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | void trng_free(trng_t *obj) |
<> | 149:156823d33999 | 61 | { |
<> | 149:156823d33999 | 62 | /*Disable the RNG peripheral */ |
<> | 149:156823d33999 | 63 | HAL_RNG_DeInit(&obj->handle); |
<> | 149:156823d33999 | 64 | /* RNG Peripheral clock disable - assume we're the only users of RNG */ |
<> | 149:156823d33999 | 65 | __HAL_RCC_RNG_CLK_DISABLE(); |
<> | 149:156823d33999 | 66 | } |
<> | 149:156823d33999 | 67 | |
<> | 149:156823d33999 | 68 | int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
<> | 149:156823d33999 | 69 | { |
<> | 149:156823d33999 | 70 | int ret; |
<> | 149:156823d33999 | 71 | |
<> | 149:156823d33999 | 72 | /* Get Random byte */ |
<> | 149:156823d33999 | 73 | for( uint32_t i = 0; i < length; i++ ){ |
<> | 149:156823d33999 | 74 | trng_get_byte(obj, output + i ); |
<> | 149:156823d33999 | 75 | } |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | *output_length = length; |
<> | 149:156823d33999 | 78 | /* Just be extra sure that we didn't do it wrong */ |
<> | 149:156823d33999 | 79 | if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) { |
<> | 149:156823d33999 | 80 | ret = -1; |
<> | 149:156823d33999 | 81 | } else { |
<> | 149:156823d33999 | 82 | ret = 0; |
<> | 149:156823d33999 | 83 | } |
<> | 149:156823d33999 | 84 | |
<> | 149:156823d33999 | 85 | return( ret ); |
<> | 149:156823d33999 | 86 | } |
<> | 149:156823d33999 | 87 | |
<> | 149:156823d33999 | 88 | #endif |