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 Crypto Component                                                 */
17 /**                                                                       */
18 /**   Crypto Self Test                                                    */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_CRYPTO_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 #include "nx_crypto_method_self_test.h"
28 
29 #ifdef NX_CRYPTO_SELF_TEST
30 static UCHAR metadata[10240];
31 
32 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_128;
33 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_192;
34 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_256;
35 extern NX_CRYPTO_METHOD crypto_method_3des;
36 extern NX_CRYPTO_METHOD crypto_method_des;
37 extern NX_CRYPTO_METHOD crypto_method_rsa;
38 extern NX_CRYPTO_METHOD crypto_method_md5;
39 extern NX_CRYPTO_METHOD crypto_method_sha1;
40 extern NX_CRYPTO_METHOD crypto_method_sha224;
41 extern NX_CRYPTO_METHOD crypto_method_sha256;
42 extern NX_CRYPTO_METHOD crypto_method_sha384;
43 extern NX_CRYPTO_METHOD crypto_method_sha512;
44 extern NX_CRYPTO_METHOD crypto_method_sha512_224;
45 extern NX_CRYPTO_METHOD crypto_method_sha512_256;
46 extern NX_CRYPTO_METHOD crypto_method_hmac_md5;
47 extern NX_CRYPTO_METHOD crypto_method_hmac_sha1;
48 extern NX_CRYPTO_METHOD crypto_method_hmac_sha224;
49 extern NX_CRYPTO_METHOD crypto_method_hmac_sha256;
50 extern NX_CRYPTO_METHOD crypto_method_hmac_sha384;
51 extern NX_CRYPTO_METHOD crypto_method_hmac_sha512;
52 extern NX_CRYPTO_METHOD crypto_method_hmac_sha512_224;
53 extern NX_CRYPTO_METHOD crypto_method_hmac_sha512_256;
54 extern NX_CRYPTO_METHOD crypto_method_tls_prf_1;
55 extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha256;
56 extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha384;
57 extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha512;
58 extern NX_CRYPTO_METHOD crypto_method_drbg;
59 extern NX_CRYPTO_METHOD crypto_method_ecdsa;
60 extern NX_CRYPTO_METHOD crypto_method_pkcs1;
61 extern NX_CRYPTO_METHOD crypto_method_ecdh;
62 extern NX_CRYPTO_METHOD crypto_method_ecdhe;
63 
64 const CHAR nx_crypto_hash_key[] = "EL_CRYPTO_VERSION_5.12   _FOR_FIPS";
65 const UINT nx_crypto_hash_key_size = sizeof(nx_crypto_hash_key) << 3;
66 
67 #define NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)                                     \
68     if(status)                                                                      \
69     {                                                                               \
70         _nx_crypto_library_state |= NX_CRYPTO_LIBRARY_STATE_POST_FAILED;            \
71     }
72 
73 
74 /**************************************************************************/
75 /*                                                                        */
76 /*  FUNCTION                                               RELEASE        */
77 /*                                                                        */
78 /*    nx_crypto_method_self_test                          PORTABLE C      */
79 /*                                                           6.1.7        */
80 /*  AUTHOR                                                                */
81 /*                                                                        */
82 /*    Timothy Stapko, Microsoft Corporation                               */
83 /*                                                                        */
84 /*  DESCRIPTION                                                           */
85 /*                                                                        */
86 /*    This function performs the Known Answer Test for crypto method.     */
87 /*                                                                        */
88 /*  INPUT                                                                 */
89 /*                                                                        */
90 /*    method_ptr                            Pointer to the crypto method  */
91 /*                                            to be tested.               */
92 /*                                                                        */
93 /*  OUTPUT                                                                */
94 /*                                                                        */
95 /*    status                                Completion status             */
96 /*                                                                        */
97 /*  CALLS                                                                 */
98 /*                                                                        */
99 /*    None                                                                */
100 /*                                                                        */
101 /*  CALLED BY                                                             */
102 /*                                                                        */
103 /*    Application Code                                                    */
104 /*                                                                        */
105 /*  RELEASE HISTORY                                                       */
106 /*                                                                        */
107 /*    DATE              NAME                      DESCRIPTION             */
108 /*                                                                        */
109 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
110 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
111 /*                                            resulting in version 6.1    */
112 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
113 /*                                            renamed FIPS symbol to      */
114 /*                                            self-test,                  */
115 /*                                            resulting in version 6.1.7  */
116 /*                                                                        */
117 /**************************************************************************/
118 
_nx_crypto_method_self_test(INT arg)119 NX_CRYPTO_KEEP INT _nx_crypto_method_self_test(INT arg)
120 {
121 UINT metadata_size = sizeof(metadata);
122 UINT status;
123 
124     /* Set the crypto state to POST_IN_PROGRESS */
125     /* Also clear the UNINITIALIZED flag */
126     _nx_crypto_library_state = _nx_crypto_library_state & (~NX_CRYPTO_LIBRARY_STATE_UNINITIALIZED);
127     _nx_crypto_library_state = _nx_crypto_library_state | NX_CRYPTO_LIBRARY_STATE_POST_IN_PROGRESS;
128 
129     /* Initialize hardware random number generator.  */
130     NX_CRYPTO_HARDWARE_RAND_INITIALIZE
131 
132     NX_CRYPTO_INTEGRITY_TEST
133 
134     status = _nx_crypto_method_self_test_des(&crypto_method_des, metadata, metadata_size);
135     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
136 
137     status = _nx_crypto_method_self_test_aes(&crypto_method_aes_cbc_256, metadata, metadata_size);
138     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
139 
140     status = _nx_crypto_method_self_test_3des(&crypto_method_3des, metadata, metadata_size);
141     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
142 
143     status = _nx_crypto_method_self_test_rsa(&crypto_method_rsa, metadata, metadata_size);
144     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
145 
146     status = _nx_crypto_method_self_test_md5(&crypto_method_md5, metadata, metadata_size);
147     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
148 
149     status = _nx_crypto_method_self_test_sha(&crypto_method_sha1, metadata, metadata_size);
150     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
151 
152     status = _nx_crypto_method_self_test_sha(&crypto_method_sha224, metadata, metadata_size);
153     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
154 
155     status = _nx_crypto_method_self_test_sha(&crypto_method_sha256, metadata, metadata_size);
156     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
157 
158     status = _nx_crypto_method_self_test_sha(&crypto_method_sha384, metadata, metadata_size);
159     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
160 
161     status = _nx_crypto_method_self_test_sha(&crypto_method_sha512, metadata, metadata_size);
162     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
163 
164     status = _nx_crypto_method_self_test_sha(&crypto_method_sha512_224, metadata, metadata_size);
165     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
166 
167     status = _nx_crypto_method_self_test_sha(&crypto_method_sha512_256, metadata, metadata_size);
168     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
169 
170     status = _nx_crypto_method_self_test_hmac_md5(&crypto_method_hmac_md5, metadata, metadata_size);
171     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
172 
173     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha1, metadata, metadata_size);
174     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
175 
176     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha224, metadata, metadata_size);
177     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
178 
179     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha256, metadata, metadata_size);
180     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
181 
182     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha384, metadata, metadata_size);
183     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
184 
185     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha512, metadata, metadata_size);
186     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
187 
188     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha512_224, metadata, metadata_size);
189     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
190 
191     status = _nx_crypto_method_self_test_hmac_sha(&crypto_method_hmac_sha512_256, metadata, metadata_size);
192     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
193 
194     status = _nx_crypto_method_self_test_prf(&crypto_method_tls_prf_1, metadata, metadata_size);
195     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
196 
197     status = _nx_crypto_method_self_test_prf(&crypto_method_tls_prf_sha256, metadata, metadata_size);
198     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
199 
200     status = _nx_crypto_method_self_test_prf(&crypto_method_tls_prf_sha384, metadata, metadata_size);
201     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
202 
203     status = _nx_crypto_method_self_test_prf(&crypto_method_tls_prf_sha512, metadata, metadata_size);
204     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
205 
206     status = _nx_crypto_method_self_test_drbg(&crypto_method_drbg, metadata, metadata_size);
207     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
208 
209     status = _nx_crypto_method_self_test_ecdsa(&crypto_method_ecdsa, metadata, metadata_size);
210     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
211 
212     status = _nx_crypto_method_self_test_pkcs1(&crypto_method_pkcs1, metadata, metadata_size);
213     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
214 
215     status = _nx_crypto_method_self_test_ecdh(&crypto_method_ecdh, metadata, metadata_size);
216     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
217 
218     status = _nx_crypto_method_self_test_ecdh(&crypto_method_ecdhe, metadata, metadata_size);
219     NX_CRYPTO_FUNCTIONAL_TEST_CHECK(status)
220 
221     /* Clear the POST-inprogress flag */
222     _nx_crypto_library_state = _nx_crypto_library_state & (~NX_CRYPTO_LIBRARY_STATE_POST_IN_PROGRESS);
223 
224     /* Set the library state to "operational" if POST is successful. */
225     if((_nx_crypto_library_state & NX_CRYPTO_LIBRARY_STATE_POST_FAILED) == 0)
226        _nx_crypto_library_state = NX_CRYPTO_LIBRARY_STATE_OPERATIONAL;
227 
228     /* All done. Return. */
229     return(arg);
230 }
231 #endif
232