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 #include "nx_crypto_pkcs1_v1.5.h"
29
30 #ifdef NX_CRYPTO_SELF_TEST
31
32 static UCHAR pub_e_2048[] = {0x01, 0x00, 0x01};
33
34 static UCHAR pri_e_2048[] = {
35 0x13, 0xFF, 0x74, 0x29, 0xF8, 0xE8, 0x51, 0xF1, 0x07, 0x9C, 0xCF, 0xCE, 0x3B, 0x3C, 0xD8, 0x60,
36 0x6A, 0xBA, 0x86, 0x07, 0xAD, 0x85, 0xCB, 0xB3, 0x05, 0x75, 0x01, 0xEB, 0xD5, 0x88, 0x11, 0xF3,
37 0xC0, 0x48, 0x23, 0x17, 0x1F, 0x19, 0x2C, 0x04, 0x8E, 0x1E, 0x88, 0x3A, 0xF8, 0xCF, 0x95, 0x88,
38 0x10, 0x15, 0x1D, 0x38, 0x74, 0xAE, 0xDC, 0x8E, 0xC4, 0xF8, 0x8D, 0x20, 0x65, 0xC5, 0x81, 0x56,
39 0x9F, 0x1E, 0x20, 0x08, 0x52, 0xDD, 0x40, 0xB6, 0xDF, 0xD1, 0x65, 0x26, 0x59, 0x08, 0x5A, 0x9D,
40 0xD1, 0xD3, 0xB8, 0x69, 0xEA, 0x36, 0x17, 0xD9, 0x04, 0xD2, 0x09, 0xDE, 0x15, 0x6A, 0x60, 0xBA,
41 0x59, 0x29, 0xD0, 0x2F, 0x16, 0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x6B, 0xE6, 0xE1, 0xDA, 0xA4, 0xDC, 0x60, 0x7E, 0x94, 0xAF, 0x38, 0x42
81 };
82
83 static UCHAR s[] = {
84 0x5c, 0x49, 0x25, 0xc1, 0x17, 0x72, 0x00, 0x48, 0x09, 0x15, 0x62, 0x38, 0xab, 0xbf, 0x42, 0x82,
85 0x30, 0x4c, 0x42, 0xa0, 0x1f, 0x01, 0xa2, 0xc0, 0xc1, 0x67, 0xe0, 0xd2, 0x3c, 0x81, 0x86, 0x59,
86 0x5e, 0xca, 0xa6, 0x46, 0x27, 0x0c, 0x89, 0x8e, 0x2a, 0xa8, 0x43, 0x39, 0x61, 0x12, 0xf9, 0xb4,
87 0x35, 0x71, 0xe0, 0xd4, 0x28, 0x22, 0xbc, 0x03, 0xe5, 0x24, 0x64, 0x08, 0xc3, 0x87, 0x14, 0x92,
88 0xb6, 0xc7, 0x1f, 0xfa, 0x5b, 0x33, 0xee, 0xa1, 0x4f, 0x87, 0x86, 0xe4, 0xc2, 0x49, 0x05, 0x53,
89 0x37, 0xc8, 0x4e, 0x9a, 0xdc, 0xd2, 0x1e, 0x5e, 0x47, 0x2a, 0xb7, 0xdb, 0x12, 0xa4, 0x2b, 0x9a,
90 0x67, 0x72, 0xc6, 0x3e, 0x92, 0x62, 0x7f, 0x52, 0xe4, 0x65, 0xb2, 0x3e, 0x7e, 0xab, 0xd8, 0xb0,
91 0x7a, 0x9f, 0x96, 0x92, 0xd5, 0x8b, 0xd1, 0x49, 0x31, 0xb5, 0xa7, 0xca, 0xa4, 0x63, 0x62, 0xcd,
92 0x0b, 0xf9, 0xa3, 0x9c, 0xb3, 0xaf, 0x44, 0x61, 0xd3, 0x2a, 0x1e, 0xca, 0x9e, 0xab, 0xc9, 0x0f,
93 0x66, 0x25, 0x2a, 0x9c, 0x38, 0x48, 0x3f, 0xfc, 0x5f, 0x6e, 0x70, 0xb2, 0x75, 0x5f, 0xc3, 0xac,
94 0xa8, 0xe9, 0xa5, 0xab, 0xeb, 0x1d, 0x61, 0x4b, 0x42, 0x35, 0x86, 0x40, 0x35, 0x35, 0x7c, 0x77,
95 0x11, 0x94, 0x33, 0x60, 0xcb, 0xe4, 0xcb, 0x49, 0x9a, 0xd7, 0x6e, 0x3e, 0xc1, 0x79, 0x93, 0x61,
96 0x1d, 0x81, 0xe9, 0x5b, 0x9f, 0x91, 0x74, 0x2a, 0x20, 0x74, 0x64, 0xb1, 0xd0, 0x25, 0xd3, 0x6b,
97 0xd5, 0xeb, 0x35, 0x89, 0x51, 0x07, 0xe9, 0x77, 0x2e, 0xd1, 0x45, 0xc3, 0xcf, 0xc9, 0x81, 0xd9,
98 0xd3, 0xf9, 0xa3, 0xb5, 0x7c, 0x9a, 0x9f, 0x43, 0xc4, 0xde, 0xeb, 0x08, 0x2a, 0x83, 0x64, 0x32,
99 0x25, 0xf3, 0x63, 0xea, 0xc8, 0xe6, 0xe0, 0x00, 0x8c, 0x1b, 0x64, 0xfa, 0x4d, 0xb6, 0xd2, 0x0d
100 };
101
102 static UCHAR output[512];
103
104 extern NX_CRYPTO_METHOD crypto_method_rsa;
105 extern NX_CRYPTO_METHOD crypto_method_sha256;
106
107 /**************************************************************************/
108 /* */
109 /* FUNCTION RELEASE */
110 /* */
111 /* nx_crypto_method_self_test_pkcs1 PORTABLE C */
112 /* 6.1.7 */
113 /* AUTHOR */
114 /* */
115 /* Timothy Stapko, Microsoft Corporation */
116 /* */
117 /* DESCRIPTION */
118 /* */
119 /* This function performs the Known Answer Test for PKCS1#v1.5 crypto */
120 /* method. */
121 /* */
122 /* INPUT */
123 /* */
124 /* method_ptr Pointer to the crypto method */
125 /* to be tested. */
126 /* */
127 /* OUTPUT */
128 /* */
129 /* status Completion status */
130 /* */
131 /* CALLS */
132 /* */
133 /* None */
134 /* */
135 /* CALLED BY */
136 /* */
137 /* Application Code */
138 /* */
139 /* RELEASE HISTORY */
140 /* */
141 /* DATE NAME DESCRIPTION */
142 /* */
143 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
144 /* 09-30-2020 Timothy Stapko Modified comment(s), */
145 /* resulting in version 6.1 */
146 /* 06-02-2021 Bhupendra Naphade Modified comment(s), */
147 /* renamed FIPS symbol to */
148 /* self-test, */
149 /* resulting in version 6.1.7 */
150 /* */
151 /**************************************************************************/
_nx_crypto_method_self_test_pkcs1(NX_CRYPTO_METHOD * crypto_method_pkcs1,VOID * metadata,UINT metadata_size)152 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_pkcs1(NX_CRYPTO_METHOD *crypto_method_pkcs1,
153 VOID *metadata, UINT metadata_size)
154 {
155 NX_CRYPTO_PKCS1_OPTIONS pkcs1_options;
156 UCHAR *modulus;
157 UINT modulus_len;
158 UCHAR *pub_e;
159 UINT pub_e_len;
160 UCHAR *pri_e;
161 UINT pri_e_len;
162 UCHAR *plain_text;
163 UINT input_length;
164 UCHAR *cipher_text;
165 UINT status;
166
167
168 /* Validate the crypto method */
169 if(crypto_method_pkcs1 == NX_CRYPTO_NULL)
170 return(NX_CRYPTO_PTR_ERROR);
171
172 /* Set the test data. */
173 pkcs1_options.public_cipher_method = &crypto_method_rsa;
174 pkcs1_options.public_cipher_metadata = (UCHAR *)metadata + sizeof(NX_CRYPTO_PKCS1);
175 pkcs1_options.public_cipher_metadata_size = crypto_method_rsa.nx_crypto_metadata_area_size;
176 pkcs1_options.hash_method = &crypto_method_sha256;
177 pkcs1_options.hash_metadata = (UCHAR *)pkcs1_options.public_cipher_metadata + sizeof(NX_CRYPTO_RSA);
178 pkcs1_options.hash_metadata_size = crypto_method_sha256.nx_crypto_metadata_area_size;
179
180 modulus = m_2048;
181 modulus_len = sizeof(m_2048);
182 pub_e = pub_e_2048;
183 pub_e_len = sizeof(pub_e_2048);
184 pri_e = pri_e_2048;
185 pri_e_len = sizeof(pri_e_2048);
186 plain_text = msg;
187 input_length = sizeof(msg);
188 cipher_text = s;
189
190 /* Clear the output buffer. */
191 NX_CRYPTO_MEMSET(output, 0, sizeof(output));
192
193 /* Call the crypto initialization function. */
194 if (crypto_method_pkcs1 -> nx_crypto_init)
195 {
196 status = crypto_method_pkcs1 -> nx_crypto_init(crypto_method_pkcs1,
197 NX_CRYPTO_NULL,
198 0,
199 NX_CRYPTO_NULL,
200 metadata,
201 metadata_size);
202
203 if (status != NX_CRYPTO_SUCCESS)
204 {
205 return(status);
206 }
207 }
208
209 if (crypto_method_pkcs1 -> nx_crypto_operation == NX_CRYPTO_NULL)
210 {
211 return(NX_CRYPTO_PTR_ERROR);
212 }
213
214 /* Call the crypto operation function. */
215 status = crypto_method_pkcs1 -> nx_crypto_operation(NX_CRYPTO_SET_ADDITIONAL_DATA,
216 NX_CRYPTO_NULL,
217 crypto_method_pkcs1,
218 modulus,
219 modulus_len << 3,
220 (UCHAR *)&pkcs1_options,
221 sizeof(pkcs1_options),
222 NX_CRYPTO_NULL,
223 NX_CRYPTO_NULL,
224 0,
225 metadata,
226 metadata_size,
227 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
228 if (status != NX_CRYPTO_SUCCESS)
229 {
230 return(status);
231 }
232
233 status = crypto_method_pkcs1 -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
234 NX_CRYPTO_NULL,
235 crypto_method_pkcs1,
236 pri_e,
237 pri_e_len << 3,
238 plain_text,
239 input_length,
240 NX_CRYPTO_NULL,
241 output,
242 modulus_len,
243 metadata,
244 metadata_size,
245 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
246 if (status != NX_CRYPTO_SUCCESS)
247 {
248 return(status);
249 }
250
251 /* Validate the output. */
252 if(NX_CRYPTO_MEMCMP(output, cipher_text, modulus_len) != 0)
253 {
254 return(NX_CRYPTO_NOT_SUCCESSFUL);
255 }
256
257 status = crypto_method_pkcs1 -> nx_crypto_operation(NX_CRYPTO_VERIFY,
258 NX_CRYPTO_NULL,
259 crypto_method_pkcs1,
260 pub_e,
261 pub_e_len << 3,
262 plain_text,
263 input_length,
264 NX_CRYPTO_NULL,
265 cipher_text,
266 modulus_len,
267 metadata,
268 metadata_size,
269 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
270
271 /* Check the status. */
272 if(status != NX_CRYPTO_SUCCESS)
273 {
274 return(status);
275 }
276
277 if (crypto_method_pkcs1 -> nx_crypto_cleanup)
278 {
279 status = crypto_method_pkcs1 -> nx_crypto_cleanup(metadata);
280 }
281
282 return(status);
283 }
284 #endif
285