1 /* 2 * Copyright 2018 NXP 3 * All rights reserved. 4 * 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 9 #include "fsl_common.h" 10 #include "fsl_adapter_rng.h" 11 #include "fsl_trng.h" 12 HAL_RngInit(void)13hal_rng_status_t HAL_RngInit(void) 14 { 15 trng_config_t config; 16 hal_rng_status_t status = kStatus_HAL_RngSuccess; 17 TRNG_Type *trngArr[] = TRNG_BASE_PTRS; 18 uint32_t randomSeed = 0; 19 20 (void)TRNG_GetDefaultConfig(&config); 21 config.sampleMode = kTRNG_SampleModeVonNeumann; 22 /* Init TRNG */ 23 if ((int32_t)kStatus_InvalidArgument == TRNG_Init(trngArr[0], (void *)&config)) 24 { 25 return kStatus_HAL_RngInvalidArgumen; 26 } 27 28 status = HAL_RngHwGetData(&randomSeed, sizeof(randomSeed)); 29 if (kStatus_HAL_RngSuccess != status) 30 { 31 return status; 32 } 33 srand(randomSeed); 34 return kStatus_HAL_RngSuccess; 35 } 36 HAL_RngDeinit(void)37void HAL_RngDeinit(void) 38 { 39 TRNG_Type *trngArr[] = TRNG_BASE_PTRS; 40 TRNG_Deinit(trngArr[0]); 41 return; 42 } 43 HAL_RngHwGetData(void * pRandomNo,uint32_t dataSize)44hal_rng_status_t HAL_RngHwGetData(void *pRandomNo, uint32_t dataSize) 45 { 46 TRNG_Type *trngArr[] = TRNG_BASE_PTRS; 47 hal_rng_status_t status = kStatus_HAL_RngSuccess; 48 49 if (NULL == pRandomNo) 50 { 51 return kStatus_HAL_RngNullPointer; 52 } 53 if ((int32_t)kStatus_Success != TRNG_GetRandomData(trngArr[0], pRandomNo, dataSize)) 54 { 55 status = kStatus_HAL_RngInternalError; 56 } 57 return status; 58 } HAL_RngGetData(void * pRandomNo,uint32_t dataSize)59hal_rng_status_t HAL_RngGetData(void *pRandomNo, uint32_t dataSize) 60 { 61 if (NULL == pRandomNo) 62 { 63 return kStatus_HAL_RngNullPointer; 64 } 65 66 for (uint32_t i = 0; i < dataSize; i++) 67 { 68 ((uint8_t *)pRandomNo)[i] = (uint8_t)rand() & 0xFFU; 69 } 70 return kStatus_HAL_RngSuccess; 71 } 72 HAL_RngSetSeed(uint32_t seed)73hal_rng_status_t HAL_RngSetSeed(uint32_t seed) 74 { 75 return KStatus_HAL_RngNotSupport; 76 } 77