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_ecdsa.h"
29
30 #ifdef NX_CRYPTO_SELF_TEST
31
32 extern NX_CRYPTO_METHOD crypto_method_ec_secp256;
33 extern NX_CRYPTO_METHOD crypto_method_sha256;
34
35 static UCHAR msg_p256_sha256[] = {
36 0xdb, 0x34, 0x4a, 0x78, 0x9b, 0x77, 0xde, 0x2e, 0x52, 0x75, 0xa9, 0xaf, 0x0e, 0xe6, 0x47, 0xff,
37 0x87, 0x86, 0x6f, 0x3b, 0x66, 0xdd, 0x89, 0x1e, 0x55, 0x80, 0x18, 0xed, 0xec, 0x70, 0x3b, 0xde,
38 0x9f, 0x7b, 0x4a, 0xc6, 0xc2, 0xc3, 0xe9, 0xdc, 0x14, 0x8e, 0x8b, 0xb8, 0x97, 0x4a, 0x21, 0x1f,
39 0x76, 0x8e, 0x43, 0xa9, 0x29, 0x86, 0xc1, 0x6f, 0x8f, 0xc3, 0x5a, 0xe9, 0xca, 0x9c, 0x14, 0x61,
40 0xb6, 0x8d, 0xab, 0x06, 0x1e, 0x97, 0x93, 0x55, 0xf8, 0x82, 0x8e, 0xeb, 0xfc, 0xd5, 0xa3, 0x62,
41 0xf7, 0x10, 0x87, 0xb4, 0x30, 0xd3, 0x5e, 0xd4, 0x70, 0xf8, 0xaa, 0x5c, 0x12, 0xbd, 0xde, 0xef,
42 0x83, 0xb9, 0xc3, 0x24, 0x48, 0x7c, 0x3b, 0x6d, 0x07, 0x51, 0xd9, 0xca, 0x7e, 0x9f, 0xb8, 0x1e,
43 0xda, 0x17, 0xea, 0xa6, 0xf9, 0x39, 0xeb, 0x42, 0x5f, 0xf3, 0x48, 0x96, 0xb6, 0xad, 0xa9, 0x75,
44 };
45
46 /* Output. */
47 static UCHAR output[400];
48 static UCHAR scratch_buffer[4000];
49
50 /**************************************************************************/
51 /* */
52 /* FUNCTION RELEASE */
53 /* */
54 /* nx_crypto_method_self_test_ecdsa PORTABLE C */
55 /* 6.1.7 */
56 /* AUTHOR */
57 /* */
58 /* Timothy Stapko, Microsoft Corporation */
59 /* */
60 /* DESCRIPTION */
61 /* */
62 /* This function performs the Known Answer Test for ECDSA crypto */
63 /* method. */
64 /* */
65 /* INPUT */
66 /* */
67 /* method_ptr Pointer to the crypto method */
68 /* to be tested. */
69 /* */
70 /* OUTPUT */
71 /* */
72 /* status Completion status */
73 /* */
74 /* CALLS */
75 /* */
76 /* None */
77 /* */
78 /* CALLED BY */
79 /* */
80 /* Application Code */
81 /* */
82 /* RELEASE HISTORY */
83 /* */
84 /* DATE NAME DESCRIPTION */
85 /* */
86 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
87 /* 09-30-2020 Timothy Stapko Modified comment(s), */
88 /* resulting in version 6.1 */
89 /* 06-02-2021 Bhupendra Naphade Modified comment(s), */
90 /* renamed FIPS symbol to */
91 /* self-test, */
92 /* resulting in version 6.1.7 */
93 /* */
94 /**************************************************************************/
_nx_crypto_method_self_test_ecdsa(NX_CRYPTO_METHOD * crypto_method_ecdsa,VOID * metadata,UINT metadata_size)95 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_ecdsa(NX_CRYPTO_METHOD *crypto_method_ecdsa,
96 VOID *metadata, UINT metadata_size)
97 {
98 UINT status;
99 VOID *handler = NX_CRYPTO_NULL;
100 NX_CRYPTO_HUGE_NUMBER private_key;
101 NX_CRYPTO_EC_POINT public_key;
102 UCHAR *privkey;
103 UCHAR *pubkey;
104 UINT pubkey_length;
105 NX_CRYPTO_METHOD *curve_method;
106 NX_CRYPTO_METHOD *hash_method;
107 UINT buffer_size;
108 UINT curve_size;
109 HN_UBASE *scratch;
110 NX_CRYPTO_EC *curve = NX_CRYPTO_NULL;
111 UCHAR *msg;
112 UINT msg_length;
113 ULONG sig_length;
114 NX_CRYPTO_EXTENDED_OUTPUT extended_output;
115
116
117 /* Validate the crypto method */
118 if(crypto_method_ecdsa == NX_CRYPTO_NULL)
119 return(NX_CRYPTO_PTR_ERROR);
120
121 /* Set the test data. */
122 curve_method = &crypto_method_ec_secp256;
123 hash_method = &crypto_method_sha256;
124 msg = msg_p256_sha256;
125 msg_length = sizeof(msg_p256_sha256);
126
127 /* Clear the output buffer. */
128 NX_CRYPTO_MEMSET(output, 0, sizeof(output));
129
130 /* Call the crypto initialization function. */
131 if (crypto_method_ecdsa -> nx_crypto_init)
132 {
133 status = crypto_method_ecdsa -> nx_crypto_init(crypto_method_ecdsa,
134 NX_CRYPTO_NULL,
135 0,
136 &handler,
137 metadata,
138 metadata_size);
139
140 if(status != NX_CRYPTO_SUCCESS)
141 {
142 return(status);
143 }
144 }
145
146 if (crypto_method_ecdsa -> nx_crypto_operation == NX_CRYPTO_NULL)
147 {
148 return(NX_CRYPTO_PTR_ERROR);
149 }
150
151 /* Set hash method. */
152 status = crypto_method_ecdsa -> nx_crypto_operation(NX_CRYPTO_HASH_METHOD_SET,
153 handler,
154 crypto_method_ecdsa,
155 NX_CRYPTO_NULL,
156 0,
157 (UCHAR *)hash_method,
158 sizeof(NX_CRYPTO_METHOD *),
159 NX_CRYPTO_NULL,
160 NX_CRYPTO_NULL,
161 0,
162 metadata,
163 metadata_size,
164 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
165
166 /* Check the status. */
167 if(status != NX_CRYPTO_SUCCESS)
168 {
169 return(status);
170 }
171
172 /* Set EC curve. */
173 status = crypto_method_ecdsa -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_SET,
174 handler,
175 crypto_method_ecdsa,
176 NX_CRYPTO_NULL,
177 0,
178 (UCHAR *)curve_method,
179 sizeof(NX_CRYPTO_METHOD *),
180 NX_CRYPTO_NULL,
181 NX_CRYPTO_NULL,
182 0,
183 metadata,
184 metadata_size,
185 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
186
187 /* Check the status. */
188 if(status != NX_CRYPTO_SUCCESS)
189 {
190 return(status);
191 }
192
193 /* Get EC curve. */
194 status = curve_method -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_GET,
195 NX_CRYPTO_NULL,
196 curve_method,
197 NX_CRYPTO_NULL,
198 0,
199 NX_CRYPTO_NULL,
200 0,
201 NX_CRYPTO_NULL,
202 (UCHAR *)&curve,
203 0,
204 NX_CRYPTO_NULL,
205 0,
206 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
207
208 /* Check the status. */
209 if(status != NX_CRYPTO_SUCCESS)
210 {
211 return(status);
212 }
213
214 buffer_size = curve -> nx_crypto_ec_n.nx_crypto_huge_buffer_size;
215 curve_size = curve -> nx_crypto_ec_bits >> 3;
216 if (curve -> nx_crypto_ec_bits & 7)
217 {
218 curve_size++;
219 }
220
221 scratch = (HN_UBASE*)(&scratch_buffer[3 * buffer_size + 4]);
222 privkey = scratch_buffer;
223 pubkey = &scratch_buffer[buffer_size];
224 NX_CRYPTO_EC_POINT_INITIALIZE(&public_key, NX_CRYPTO_EC_POINT_AFFINE, scratch, buffer_size);
225 NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&private_key, scratch, buffer_size + 8);
226
227 /* Generate the key pair. */
228 status = _nx_crypto_ec_key_pair_generation_extra(curve, &curve -> nx_crypto_ec_g, &private_key,
229 &public_key, scratch);
230
231 if (status)
232 {
233 return(status);
234 }
235
236
237 status = _nx_crypto_huge_number_extract_fixed_size(&private_key, privkey, buffer_size);
238 if (status)
239 {
240 return(status);
241 }
242
243 pubkey_length = 0;
244 _nx_crypto_ec_point_extract_uncompressed(curve, &public_key, pubkey, 4 + 2 * buffer_size, &pubkey_length);
245
246
247 extended_output.nx_crypto_extended_output_data = output;
248 extended_output.nx_crypto_extended_output_length_in_byte = sizeof(output);
249 /* Sign the hash data using ECDSA. */
250 status = crypto_method_ecdsa -> nx_crypto_operation(NX_CRYPTO_SIGNATURE_GENERATE,
251 handler,
252 crypto_method_ecdsa,
253 privkey,
254 buffer_size << 3,
255 msg,
256 msg_length,
257 NX_CRYPTO_NULL,
258 (UCHAR *)&extended_output,
259 sizeof(extended_output),
260 metadata,
261 metadata_size,
262 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
263
264 /* Check the status. */
265 if(status != NX_CRYPTO_SUCCESS)
266 {
267 return(status);
268 }
269 sig_length = extended_output.nx_crypto_extended_output_actual_size;
270
271 /* Verify the signature. */
272 status = crypto_method_ecdsa -> nx_crypto_operation(NX_CRYPTO_SIGNATURE_VERIFY,
273 handler,
274 crypto_method_ecdsa,
275 pubkey,
276 pubkey_length << 3,
277 msg,
278 msg_length,
279 NX_CRYPTO_NULL,
280 output,
281 sig_length,
282 metadata,
283 metadata_size,
284 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
285
286 /* Check the status. */
287 if(status != NX_CRYPTO_SUCCESS)
288 {
289 return(status);
290 }
291
292 if (crypto_method_ecdsa -> nx_crypto_cleanup)
293 {
294 status = crypto_method_ecdsa -> nx_crypto_cleanup(metadata);
295 }
296
297 return(status);
298 }
299 #endif
300