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