1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2021 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_rng.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.rng"
14 #endif
15 
16 /*******************************************************************************
17  * Definitions
18  *******************************************************************************/
19 
20 /*******************************************************************************
21  * Prototypes
22  *******************************************************************************/
23 
24 /*******************************************************************************
25  * Code
26  ******************************************************************************/
27 
RNG_GetRandomData(void * data,size_t dataSize)28 status_t RNG_GetRandomData(void *data, size_t dataSize)
29 {
30     status_t result = kStatus_Fail;
31     uint32_t random32;
32     uint32_t randomSize;
33     uint8_t *pRandom;
34     uint8_t *pData = (uint8_t *)data;
35     uint32_t i, j;
36 
37     /* Check input parameters.*/
38     if (!(data != NULL) && (dataSize != 0U))
39     {
40         result = kStatus_InvalidArgument;
41     }
42     else
43     {
44         do
45         {
46             /* Read Entropy.*/
47             random32 = RNG_GetRandomDataWord();
48             pRandom  = (uint8_t *)&random32;
49 
50             if (dataSize < sizeof(random32))
51             {
52                 randomSize = dataSize;
53             }
54             else
55             {
56                 randomSize = sizeof(random32);
57             }
58 
59             for (i = 0; i < randomSize; i++)
60             {
61                 *pData++ = *pRandom++;
62             }
63 
64             dataSize -= randomSize;
65 
66             /* Discard next 32 random numbers for better entropy */
67             for (j = 0; j < 32U; j++)
68             {
69                 (void)RNG_GetRandomDataWord();
70             }
71 
72         } while (dataSize > 0U);
73 
74         result = kStatus_Success;
75     }
76 
77     return result;
78 }
79