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
31 static UCHAR secret_sha1[] = { 0x86, 0xec, 0x88, 0xbb };
32 static UCHAR label_sha1[] = { 0xc8, 0x37, 0xaf, 0x7d };
33 static UCHAR seed_sha1[] = { 0x36, 0x54, 0xf1, 0x6f };
34 static UCHAR result_sha1[] = {
35 0xab, 0xe3, 0x77, 0xa6, 0x58, 0x4c, 0x97, 0x03, 0x98, 0xe9, 0xe4, 0x62, 0xe6, 0x44, 0xe4, 0x2d,
36 0x21, 0x16, 0xdb, 0x4f, 0x0e, 0x70, 0xc9, 0x83, 0xe5, 0x31, 0x61, 0x95, 0x17, 0xcd, 0xc2, 0xd0,
37 0x7e, 0x9a, 0xdf, 0xf6, 0xe2, 0x44, 0x01, 0x05, 0xa9, 0xb0, 0x7a, 0xbe, 0xc3, 0x9a, 0x47, 0x9b,
38 0xd7, 0xd9, 0x2c, 0xba, 0xb7, 0x8e, 0x90, 0x1d, 0x4f, 0x21, 0xae, 0x4e, 0x0f, 0x60, 0xcf, 0x3b,
39 0xdf, 0xe5, 0x77, 0x79, 0xff, 0x23, 0x23, 0x2d, 0x62, 0x48, 0xc6, 0x72, 0xb3, 0xf9, 0xce, 0x4f,
40 0x46, 0x66, 0x2f, 0xc5, 0x0e, 0xbc, 0x2a, 0x34, 0xd0, 0xc5, 0x37, 0xa2, 0x2f, 0x69, 0x43, 0x74,
41 0x6d, 0x11, 0x3c, 0x1c, 0x75, 0xa5, 0x12, 0x61, 0x1a, 0xc7, 0x8f, 0x41, 0xab, 0xa8, 0x45, 0xd5,
42 0xf3, 0xb3, 0xb4, 0xbd, 0xe3, 0x7c, 0x8b, 0xbb, 0x0f, 0x0d, 0xcb, 0x57, 0xc8, 0x6c, 0x13, 0x32,
43 0x8b, 0xe1, 0xd0, 0x2e, 0x2c, 0x2d, 0xb2, 0xd5, 0x67, 0x1c, 0xb0, 0x61, 0x3d, 0x77, 0x96, 0x68,
44 0x1b, 0x47, 0xdf, 0x3a, 0x50, 0x62, 0x31, 0x4b, 0x30, 0x09, 0xe9, 0x3b, 0xdd, 0xfb, 0x34, 0x1d,
45 };
46
47 static UCHAR secret_sha256[] = {
48 0x6e, 0x97, 0x83, 0xfe, 0x56, 0x7e, 0xda, 0x66, 0xfc, 0x0e, 0xf5, 0x17, 0xd9, 0x52, 0xc4, 0x7c,
49 0x2f, 0x17, 0x06, 0x3c, 0xc5, 0x44, 0xd2, 0x6f, 0x98, 0x49, 0x2c, 0x50, 0x21, 0xe1, 0x27, 0x74,
50 0xa5, 0x98, 0x60, 0x87, 0x3e, 0x7a, 0x31, 0xaa, 0x2d, 0xc6, 0x90, 0x9e, 0x93, 0xb1, 0xb5, 0x72
51 };
52 static UCHAR label_sha256[] = {
53 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74
54 };
55 static UCHAR seed_sha256[] = {
56 0x66, 0xc1, 0x7c, 0xf0, 0x53, 0x40, 0x84, 0x42, 0x7c, 0xaa, 0x2b, 0xf7, 0xbd, 0x58, 0x53, 0x9d,
57 0xb9, 0x3a, 0xc0, 0xeb, 0xcc, 0x05, 0xd7, 0x3c, 0x46, 0x43, 0x63, 0x6b, 0x09, 0x3a, 0x70, 0xe8,
58 0xa4, 0xe5, 0x80, 0x45, 0x5b, 0x08, 0xda, 0x0f, 0x33, 0x0f, 0x98, 0x82, 0xaf, 0xcd, 0x7f, 0xe7,
59 0x9c, 0x64, 0x9c, 0x35, 0x86, 0x24, 0x43, 0xb9, 0xb7, 0x99, 0x1b, 0xa0, 0xd1, 0xb1, 0x28, 0x9c
60 };
61 static UCHAR result_sha256[] = {
62 0x8f, 0x80, 0x2d, 0x64, 0x6f, 0x18, 0x04, 0xdd, 0xda, 0x9d, 0xc9, 0x57, 0x79, 0x85, 0x4f, 0x86,
63 0xc2, 0x9e, 0x2b, 0x9c, 0x17, 0xc3, 0x91, 0x68, 0xac, 0xea, 0x05, 0xc5, 0x3b, 0x4a, 0x67, 0xd0,
64 0x4d, 0x78, 0x49, 0x87, 0xdf, 0x04, 0xd2, 0x5c, 0xd3, 0xe8, 0xb4, 0x6d, 0x11, 0x70, 0x3a, 0xe5,
65 };
66
67 static UCHAR secret_sha384[] = {
68 0x06, 0x40, 0xec, 0xd9, 0x8b, 0xee, 0xba, 0x99, 0x79, 0xae, 0x75, 0x1d, 0xfa, 0x51, 0x0f, 0xc2,
69 0xc7, 0x4a, 0x83, 0x54, 0xec, 0x9a, 0x12, 0x1e, 0x99, 0xe1, 0xde, 0xcb, 0xcb, 0xe2, 0x33, 0x7c,
70 0x35, 0xb6, 0xc8, 0xd5, 0x85, 0x9a, 0xd2, 0x57, 0xbd, 0x9d, 0x04, 0x59, 0x40, 0x44, 0x1f, 0x10
71 };
72 static UCHAR label_sha384[] = {
73 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74
74 };
75 static UCHAR seed_sha384[] = {
76 0xdd, 0xfe, 0x04, 0x5c, 0x59, 0xd7, 0x87, 0xf8, 0x5d, 0xaf, 0x1c, 0x49, 0x48, 0xd9, 0xa8, 0x16,
77 0x62, 0x07, 0x16, 0x7b, 0x18, 0x20, 0xa2, 0x24, 0x0b, 0x5b, 0x52, 0x8b, 0x51, 0xfb, 0x3c, 0xf6,
78 0x70, 0xd2, 0xfe, 0xd4, 0x8a, 0xc7, 0xe5, 0x37, 0xac, 0xbb, 0xf6, 0x5b, 0x8b, 0x4d, 0xd6, 0x99,
79 0x4a, 0xb7, 0x9c, 0x1f, 0x26, 0x8e, 0x2f, 0xfb, 0xf2, 0x0e, 0x76, 0x4a, 0x2b, 0xcf, 0x09, 0xa1
80 };
81 static UCHAR result_sha384[] = {
82 0xd2, 0xfc, 0x23, 0x9e, 0x32, 0x0c, 0xbe, 0x2b, 0x4d, 0xa4, 0xc3, 0x79, 0x76, 0xff, 0xd5, 0x56,
83 0x56, 0x45, 0xc9, 0xff, 0x7c, 0x23, 0x9c, 0xfe, 0xa1, 0x0f, 0x6a, 0xf4, 0x47, 0xbe, 0xfe, 0x67,
84 0xa2, 0xc5, 0x45, 0x78, 0x2b, 0x6d, 0x1b, 0xcb, 0xda, 0xc7, 0x24, 0x8c, 0xe6, 0xb6, 0x25, 0xda
85 };
86
87 static UCHAR secret_sha512[] = {
88 0xcb, 0x1b, 0xc6, 0x9f, 0xea, 0xb9, 0x64, 0xb7, 0x66, 0x29, 0x7c, 0x7e, 0xb8, 0xf2, 0x9c, 0x89,
89 0x2a, 0xb3, 0x80, 0x27, 0x08, 0x98, 0x7d, 0xf5, 0xb6, 0x1a, 0x80, 0x66, 0x6b, 0x9e, 0x12, 0xb2,
90 0x32, 0xcb, 0x5e, 0x0e, 0x40, 0xf0, 0x5f, 0xb3, 0x92, 0x35, 0xb4, 0x88, 0x94, 0x5a, 0x70, 0x61
91 };
92 static UCHAR label_sha512[] = {
93 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74
94 };
95 static UCHAR seed_sha512[] = {
96 0x13, 0x20, 0xa8, 0xa1, 0x17, 0xbb, 0x6c, 0xa0, 0xf8, 0x8e, 0x19, 0x23, 0x64, 0x11, 0x4b, 0x7f,
97 0x73, 0x21, 0x6b, 0x7a, 0xcc, 0x4f, 0xb9, 0x1b, 0xef, 0x49, 0xfa, 0x9b, 0xb1, 0x4d, 0x32, 0xc2,
98 0xac, 0xd7, 0x02, 0xca, 0xf7, 0xd3, 0x76, 0xf3, 0x5e, 0x65, 0x66, 0xb8, 0x5d, 0xdb, 0x3d, 0xa9,
99 0x3a, 0xe4, 0xeb, 0x63, 0x6d, 0x2a, 0x7a, 0x7a, 0xf7, 0x2f, 0xf3, 0x2a, 0x43, 0xe1, 0x64, 0x95
100 };
101 static UCHAR result_sha512[] = {
102 0x91, 0xf2, 0xc6, 0x51, 0x77, 0xe4, 0x8f, 0xde, 0x4d, 0xd1, 0x27, 0xc3, 0xf5, 0xb5, 0x9c, 0x2a,
103 0x7d, 0x38, 0xa4, 0x78, 0x29, 0x86, 0x19, 0x7c, 0x11, 0x0c, 0xca, 0x2f, 0x0a, 0x18, 0x56, 0x8f,
104 0x31, 0x7a, 0x74, 0xa6, 0xea, 0xd3, 0x3c, 0x0f, 0x84, 0xa5, 0xd4, 0x1f, 0x8d, 0x36, 0x8f, 0x3a
105 };
106
107 /* Output. */
108 static UCHAR output[256];
109
110 /**************************************************************************/
111 /* */
112 /* FUNCTION RELEASE */
113 /* */
114 /* nx_crypto_method_self_test_prf PORTABLE C */
115 /* 6.1.7 */
116 /* AUTHOR */
117 /* */
118 /* Timothy Stapko, Microsoft Corporation */
119 /* */
120 /* DESCRIPTION */
121 /* */
122 /* This function performs the Known Answer Test for PRF crypto method. */
123 /* */
124 /* INPUT */
125 /* */
126 /* method_ptr Pointer to the crypto method */
127 /* to be tested. */
128 /* */
129 /* OUTPUT */
130 /* */
131 /* status Completion status */
132 /* */
133 /* CALLS */
134 /* */
135 /* None */
136 /* */
137 /* CALLED BY */
138 /* */
139 /* Application Code */
140 /* */
141 /* RELEASE HISTORY */
142 /* */
143 /* DATE NAME DESCRIPTION */
144 /* */
145 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
146 /* 09-30-2020 Timothy Stapko Modified comment(s), */
147 /* resulting in version 6.1 */
148 /* 06-02-2021 Bhupendra Naphade Modified comment(s), */
149 /* renamed FIPS symbol to */
150 /* self-test, */
151 /* resulting in version 6.1.7 */
152 /* */
153 /**************************************************************************/
_nx_crypto_method_self_test_prf(NX_CRYPTO_METHOD * crypto_method_prf,VOID * metadata,UINT metadata_size)154 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_prf(NX_CRYPTO_METHOD *crypto_method_prf,
155 VOID *metadata, UINT metadata_size)
156 {
157 UCHAR *secret;
158 UCHAR *label;
159 UCHAR *seed;
160 UCHAR *result;
161 UINT secret_length;
162 UINT label_length;
163 UINT seed_length;
164 UINT result_length;
165 UINT status;
166 VOID *handler = NX_CRYPTO_NULL;
167
168
169 /* Validate the crypto method */
170 if(crypto_method_prf == NX_CRYPTO_NULL)
171 return(NX_CRYPTO_PTR_ERROR);
172
173 /* Set the test data. */
174 switch (crypto_method_prf -> nx_crypto_algorithm)
175 {
176 case NX_CRYPTO_PRF_HMAC_SHA1:
177 secret = secret_sha1;
178 secret_length = sizeof(secret_sha1);
179 label = label_sha1;
180 label_length = sizeof(label_sha1);
181 seed = seed_sha1;
182 seed_length = sizeof(seed_sha1);
183 result = result_sha1;
184 result_length = sizeof(result_sha1);
185 break;
186 case NX_CRYPTO_PRF_HMAC_SHA2_256:
187 secret = secret_sha256;
188 secret_length = sizeof(secret_sha256);
189 label = label_sha256;
190 label_length = sizeof(label_sha256);
191 seed = seed_sha256;
192 seed_length = sizeof(seed_sha256);
193 result = result_sha256;
194 result_length = sizeof(result_sha256);
195 break;
196 case NX_CRYPTO_PRF_HMAC_SHA2_384:
197 secret = secret_sha384;
198 secret_length = sizeof(secret_sha384);
199 label = label_sha384;
200 label_length = sizeof(label_sha384);
201 seed = seed_sha384;
202 seed_length = sizeof(seed_sha384);
203 result = result_sha384;
204 result_length = sizeof(result_sha384);
205 break;
206 case NX_CRYPTO_PRF_HMAC_SHA2_512:
207 secret = secret_sha512;
208 secret_length = sizeof(secret_sha512);
209 label = label_sha512;
210 label_length = sizeof(label_sha512);
211 seed = seed_sha512;
212 seed_length = sizeof(seed_sha512);
213 result = result_sha512;
214 result_length = sizeof(result_sha512);
215 break;
216 default:
217 return(1);
218 }
219
220 /* Clear the output buffer. */
221 NX_CRYPTO_MEMSET(output, 0, sizeof(output));
222
223 /* Call the crypto initialization function. */
224 if (crypto_method_prf -> nx_crypto_init)
225 {
226 status = crypto_method_prf -> nx_crypto_init(crypto_method_prf,
227 secret,
228 secret_length,
229 &handler,
230 metadata,
231 metadata_size);
232
233 if (status != NX_CRYPTO_SUCCESS)
234 {
235 return(status);
236 }
237 }
238
239 if (crypto_method_prf -> nx_crypto_operation == NX_CRYPTO_NULL)
240 {
241 return(NX_CRYPTO_PTR_ERROR);
242 }
243
244 /* Call the crypto operation function. */
245 status = crypto_method_prf -> nx_crypto_operation(NX_CRYPTO_PRF,
246 handler,
247 crypto_method_prf,
248 label,
249 label_length,
250 seed,
251 seed_length,
252 NX_CRYPTO_NULL,
253 (UCHAR *)output,
254 result_length,
255 metadata,
256 metadata_size,
257 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
258
259 /* Check the status. */
260 if(status != NX_CRYPTO_SUCCESS)
261 {
262 return(status);
263 }
264
265 /* Validate the output. */
266 if(NX_CRYPTO_MEMCMP(output, result, result_length) != 0)
267 {
268 return(NX_CRYPTO_NOT_SUCCESSFUL);
269 }
270
271 if (crypto_method_prf -> nx_crypto_cleanup)
272 {
273 status = crypto_method_prf -> nx_crypto_cleanup(metadata);
274 }
275
276 return(status);
277 }
278 #endif
279