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