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