1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Secure Component                                                 */
17 /**                                                                       */
18 /**    Transport Layer Security (TLS)                                     */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_secure_tls.h"
29 
30 #ifndef NX_SECURE_RNG_CHECK_COUNT
31 #define NX_SECURE_RNG_CHECK_COUNT   3
32 #endif /* NX_SECURE_RNG_CHECK_COUNT */
33 
34 UINT _nx_secure_crypto_rng_self_test(VOID);
35 
36 /**************************************************************************/
37 /*                                                                        */
38 /*  FUNCTION                                               RELEASE        */
39 /*                                                                        */
40 /*    _nx_secure_crypto_rng_self_test                     PORTABLE C      */
41 /*                                                           6.1.5        */
42 /*  AUTHOR                                                                */
43 /*                                                                        */
44 /*    Timothy Stapko, Microsoft Corporation                               */
45 /*                                                                        */
46 /*  DESCRIPTION                                                           */
47 /*                                                                        */
48 /*    This function runs the simple random number generator test to make  */
49 /*    sure the numbers generated are unique.                              */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    status                                Completion status             */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    None                                                                */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
72 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  03-02-2021     Yuxin Zhou               Modified comment(s), and      */
75 /*                                            fixed compiler warnings,    */
76 /*                                            resulting in version 6.1.5  */
77 /*                                                                        */
78 /**************************************************************************/
_nx_secure_crypto_rng_self_test(VOID)79 UINT _nx_secure_crypto_rng_self_test(VOID)
80 {
81 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
82 UINT results[NX_SECURE_RNG_CHECK_COUNT];
83 UINT i, j;
84 
85     for (i = 0; i < NX_SECURE_RNG_CHECK_COUNT; i++)
86     {
87         results[i] = (UINT)NX_RAND();
88 
89         /* Make sure the random number is unique. */
90         for (j = 0; j < i; j++)
91         {
92             if (results[i] == results[j])
93             {
94 
95                 /* Found duplicated random number. */
96                 return(1);
97             }
98         }
99     }
100 
101     /* All random numbers generated are unique. */
102     return(0);
103 #else
104     return(0);
105 #endif
106 }
107