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