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 #define INPUT_OUTPUT_LENGTH (16)
32 /* 02CF1E1D9796AD3395D8126757B6FC7C705F082FB85270427760AC173759CE07 */
33 static const UCHAR key_cbc_256_encrypt[] = {
34 0x02, 0xCF, 0x1E, 0x1D, 0x97, 0x96, 0xAD, 0x33, 0x95, 0xD8, 0x12, 0x67, 0x57, 0xB6, 0xFC, 0x7C,
35 0x70, 0x5F, 0x08, 0x2F, 0xB8, 0x52, 0x70, 0x42, 0x77, 0x60, 0xAC, 0x17, 0x37, 0x59, 0xCE, 0x07,
36 };
37 
38 /* BB35190002242F28FC79C01B89022604 */
39 static const UCHAR iv_cbc_256_encrypt[] = {
40 0xBB, 0x35, 0x19, 0x00, 0x02, 0x24, 0x2F, 0x28, 0xFC, 0x79, 0xC0, 0x1B, 0x89, 0x02, 0x26, 0x04,
41 };
42 
43 /* 05D4EE7191E69A0AC7CBC1625258085D */
44 static const UCHAR plain_cbc_256_encrypt[] = {
45 0x05, 0xD4, 0xEE, 0x71, 0x91, 0xE6, 0x9A, 0x0A, 0xC7, 0xCB, 0xC1, 0x62, 0x52, 0x58, 0x08, 0x5D,
46 };
47 
48 /* F65F0278E9F5D525F66DC5AFAD99F205 */
49 static const UCHAR secret_cbc_256_encrypt[] = {
50 0xF6, 0x5F, 0x02, 0x78, 0xE9, 0xF5, 0xD5, 0x25, 0xF6, 0x6D, 0xC5, 0xAF, 0xAD, 0x99, 0xF2, 0x05,
51 };
52 
53 /* 6CB931425732CB1F813B2F3B648A13326942421EA6BAB74E6F774764AE85541D */
54 static UCHAR key_cbc_256_decrypt[] = {
55 0x6C, 0xB9, 0x31, 0x42, 0x57, 0x32, 0xCB, 0x1F, 0x81, 0x3B, 0x2F, 0x3B, 0x64, 0x8A, 0x13, 0x32,
56 0x69, 0x42, 0x42, 0x1E, 0xA6, 0xBA, 0xB7, 0x4E, 0x6F, 0x77, 0x47, 0x64, 0xAE, 0x85, 0x54, 0x1D,
57 };
58 
59 /* CDA29622B5C38C785784EC74E6F8BA31 */
60 static UCHAR iv_cbc_256_decrypt[] = {
61 0xCD, 0xA2, 0x96, 0x22, 0xB5, 0xC3, 0x8C, 0x78, 0x57, 0x84, 0xEC, 0x74, 0xE6, 0xF8, 0xBA, 0x31,
62 };
63 
64 /* F19B9D43CE897F2577D10C60C82E1E17 */
65 static UCHAR plain_cbc_256_decrypt[] = {
66 0xF1, 0x9B, 0x9D, 0x43, 0xCE, 0x89, 0x7F, 0x25, 0x77, 0xD1, 0x0C, 0x60, 0xC8, 0x2E, 0x1E, 0x17,
67 };
68 
69 /* 3CBB285FB0244F551DEEF7C0DDBE5BFE */
70 static UCHAR secret_cbc_256_decrypt[] = {
71 0x3C, 0xBB, 0x28, 0x5F, 0xB0, 0x24, 0x4F, 0x55, 0x1D, 0xEE, 0xF7, 0xC0, 0xDD, 0xBE, 0x5B, 0xFE,
72 };
73 
74 /*Note: For CTR, the key_ctr is the conjunction of key and nonce.  */
75 /* D0E78C4D0B30D33F5BF4A132B2F94A4A38963511A3904B117E35A37B5AAC8A193BF0D158 */
76 static const UCHAR key_ctr_256_encrypt[] = {
77 0xD0, 0xE7, 0x8C, 0x4D, 0x0B, 0x30, 0xD3, 0x3F, 0x5B, 0xF4, 0xA1, 0x32, 0xB2, 0xF9, 0x4A, 0x4A,
78 0x38, 0x96, 0x35, 0x11, 0xA3, 0x90, 0x4B, 0x11, 0x7E, 0x35, 0xA3, 0x7B, 0x5A, 0xAC, 0x8A, 0x19,
79 0x3B, 0xF0, 0xD1, 0x58,
80 };
81 
82 /* A1A31704C8B7E16C */
83 static const UCHAR iv_ctr_256_encrypt[] = {
84 0xA1, 0xA3, 0x17, 0x04, 0xC8, 0xB7, 0xE1, 0x6C,
85 };
86 
87 /* 981FA33222C5451017530155A4BF7F29 */
88 static const UCHAR plain_ctr_256_encrypt[] = {
89 0x98, 0x1F, 0xA3, 0x32, 0x22, 0xC5, 0x45, 0x10, 0x17, 0x53, 0x01, 0x55, 0xA4, 0xBF, 0x7F, 0x29,
90 };
91 
92 /* 643B91B4E541B20AAAEAB77F2D328566 */
93 static const UCHAR secret_ctr_256_encrypt[] = {
94 0x64, 0x3B, 0x91, 0xB4, 0xE5, 0x41, 0xB2, 0x0A, 0xAA, 0xEA, 0xB7, 0x7F, 0x2D, 0x32, 0x85, 0x66,
95 };
96 
97 /* D1C30E262F0D457A4B1F9B0B5753084793C5A627776D41430B152E6D195CA276 */
98 static UCHAR key_ctr_256_decrypt[] = {
99 0xD1, 0xC3, 0x0E, 0x26, 0x2F, 0x0D, 0x45, 0x7A, 0x4B, 0x1F, 0x9B, 0x0B, 0x57, 0x53, 0x08, 0x47,
100 0x93, 0xC5, 0xA6, 0x27, 0x77, 0x6D, 0x41, 0x43, 0x0B, 0x15, 0x2E, 0x6D, 0x19, 0x5C, 0xA2, 0x76,
101 };
102 
103 /* 65F42E5B2D9EEC75DEF7017500000001 */
104 static UCHAR iv_ctr_256_decrypt[] = {
105 0x65, 0xF4, 0x2E, 0x5B, 0x2D, 0x9E, 0xEC, 0x75, 0xDE, 0xF7, 0x01, 0x75, 0x00, 0x00, 0x00, 0x01,
106 };
107 
108 /* F0D0D405C53C6B67200B485B6219BB22 */
109 static UCHAR plain_ctr_256_decrypt[] = {
110 0xF0, 0xD0, 0xD4, 0x05, 0xC5, 0x3C, 0x6B, 0x67, 0x20, 0x0B, 0x48, 0x5B, 0x62, 0x19, 0xBB, 0x22,
111 };
112 
113 /* 8EF64BF7EAF4390BB42D3B3B5D3611D3 */
114 static UCHAR secret_ctr_256_decrypt[] = {
115 0x8E, 0xF6, 0x4B, 0xF7, 0xEA, 0xF4, 0x39, 0x0B, 0xB4, 0x2D, 0x3B, 0x3B, 0x5D, 0x36, 0x11, 0xD3,
116 };
117 
118 static UCHAR output[INPUT_OUTPUT_LENGTH];
119 
120 /**************************************************************************/
121 /*                                                                        */
122 /*  FUNCTION                                               RELEASE        */
123 /*                                                                        */
124 /*    nx_crypto_method_self_test_aes                      PORTABLE C      */
125 /*                                                           6.1.7        */
126 /*  AUTHOR                                                                */
127 /*                                                                        */
128 /*    Timothy Stapko, Microsoft Corporation                               */
129 /*                                                                        */
130 /*  DESCRIPTION                                                           */
131 /*                                                                        */
132 /*    This function performs the Known Answer Test for AES crypto method. */
133 /*                                                                        */
134 /*  INPUT                                                                 */
135 /*                                                                        */
136 /*    method_ptr                            Pointer to the crypto method  */
137 /*                                            to be tested.               */
138 /*                                                                        */
139 /*  OUTPUT                                                                */
140 /*                                                                        */
141 /*    status                                Completion status             */
142 /*                                                                        */
143 /*  CALLS                                                                 */
144 /*                                                                        */
145 /*    None                                                                */
146 /*                                                                        */
147 /*  CALLED BY                                                             */
148 /*                                                                        */
149 /*    Application Code                                                    */
150 /*                                                                        */
151 /*  RELEASE HISTORY                                                       */
152 /*                                                                        */
153 /*    DATE              NAME                      DESCRIPTION             */
154 /*                                                                        */
155 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
156 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
157 /*                                            resulting in version 6.1    */
158 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
159 /*                                            renamed FIPS symbol to      */
160 /*                                            self-test,                  */
161 /*                                            resulting in version 6.1.7  */
162 /*                                                                        */
163 /**************************************************************************/
_nx_crypto_method_self_test_aes(NX_CRYPTO_METHOD * crypto_method_aes,VOID * metadata,UINT metadata_size)164 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_aes(NX_CRYPTO_METHOD *crypto_method_aes,
165                                                     VOID *metadata, UINT metadata_size)
166 {
167 UCHAR *key_encrypt, *key_decrypt;
168 UCHAR *iv_encrypt, *iv_decrypt;
169 UCHAR *plain_encrypt, *plain_decrypt;
170 UCHAR *secret_encrypt, *secret_decrypt;
171 UINT key_size;
172 UINT status;
173 VOID *handler = NX_CRYPTO_NULL;
174 
175 
176     /* Validate the crypto method */
177     if(crypto_method_aes == NX_CRYPTO_NULL)
178         return(NX_CRYPTO_PTR_ERROR);
179 
180     key_size = crypto_method_aes -> nx_crypto_key_size_in_bits;
181 
182     switch (crypto_method_aes -> nx_crypto_algorithm)
183     {
184     case NX_CRYPTO_ENCRYPTION_AES_CBC:
185         switch (key_size)
186         {
187         case 256:
188             key_encrypt = (UCHAR *)key_cbc_256_encrypt;
189             iv_encrypt = (UCHAR *)iv_cbc_256_encrypt;
190             plain_encrypt = (UCHAR *)plain_cbc_256_encrypt;
191             secret_encrypt = (UCHAR *)secret_cbc_256_encrypt;
192             key_decrypt = (UCHAR *)key_cbc_256_decrypt;
193             iv_decrypt = (UCHAR *)iv_cbc_256_decrypt;
194             plain_decrypt = (UCHAR *)plain_cbc_256_decrypt;
195             secret_decrypt = (UCHAR *)secret_cbc_256_decrypt;
196             break;
197         default:
198             return(1);
199         }
200         break;
201 
202     case NX_CRYPTO_ENCRYPTION_AES_CTR:
203         switch (key_size)
204         {
205         case 256:
206             key_encrypt = (UCHAR *)key_ctr_256_encrypt;
207             iv_encrypt = (UCHAR *)iv_ctr_256_encrypt;
208             plain_encrypt = (UCHAR *)plain_ctr_256_encrypt;
209             secret_encrypt = (UCHAR *)secret_ctr_256_encrypt;
210             key_decrypt = (UCHAR *)key_ctr_256_decrypt;
211             iv_decrypt = (UCHAR *)iv_ctr_256_decrypt;
212             plain_decrypt = (UCHAR *)plain_ctr_256_decrypt;
213             secret_decrypt = (UCHAR *)secret_ctr_256_decrypt;
214             break;
215         default:
216             return(1);
217         }
218         break;
219 
220     default:
221         return(1);
222     }
223 
224     if (crypto_method_aes -> nx_crypto_init)
225     {
226         status = crypto_method_aes -> nx_crypto_init(crypto_method_aes,
227                                                      key_encrypt,
228                                                      key_size,
229                                                      &handler,
230                                                      metadata,
231                                                      metadata_size);
232         if (status != NX_CRYPTO_SUCCESS)
233         {
234             return(status);
235         }
236     }
237 
238     if (crypto_method_aes -> nx_crypto_operation == NX_CRYPTO_NULL)
239     {
240         return(NX_CRYPTO_PTR_ERROR);
241     }
242 
243     /* Encryption. */
244     status = crypto_method_aes -> nx_crypto_operation(NX_CRYPTO_ENCRYPT,
245                                                       handler,
246                                                       crypto_method_aes,
247                                                       key_encrypt,
248                                                       key_size,
249                                                       plain_encrypt,
250                                                       INPUT_OUTPUT_LENGTH,
251                                                       iv_encrypt,
252                                                       output,
253                                                       INPUT_OUTPUT_LENGTH,
254                                                       metadata,
255                                                       metadata_size,
256                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
257     if (status != NX_CRYPTO_SUCCESS)
258     {
259         return(status);
260     }
261 
262     if (NX_CRYPTO_MEMCMP(output, secret_encrypt, INPUT_OUTPUT_LENGTH) != 0)
263     {
264         return(NX_CRYPTO_NOT_SUCCESSFUL);
265     }
266 
267     if (crypto_method_aes -> nx_crypto_cleanup)
268     {
269         status = crypto_method_aes -> nx_crypto_cleanup(metadata);
270 
271         if (status != NX_CRYPTO_SUCCESS)
272         {
273             return(status);
274         }
275     }
276 
277     /* Decryption. */
278     if (crypto_method_aes -> nx_crypto_init)
279     {
280         status = crypto_method_aes -> nx_crypto_init(crypto_method_aes,
281                                                      key_decrypt,
282                                                      key_size,
283                                                      &handler,
284                                                      metadata,
285                                                      metadata_size);
286         if (status != NX_CRYPTO_SUCCESS)
287         {
288             return(status);
289         }
290     }
291 
292     status = crypto_method_aes -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
293                                                       handler,
294                                                       crypto_method_aes,
295                                                       key_decrypt,
296                                                       key_size,
297                                                       secret_decrypt,
298                                                       INPUT_OUTPUT_LENGTH,
299                                                       iv_decrypt,
300                                                       output,
301                                                       INPUT_OUTPUT_LENGTH,
302                                                       metadata,
303                                                       metadata_size,
304                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
305     if (status != NX_CRYPTO_SUCCESS)
306     {
307         return(status);
308     }
309 
310     if (NX_CRYPTO_MEMCMP(output, plain_decrypt, INPUT_OUTPUT_LENGTH) != 0)
311     {
312         return(NX_CRYPTO_NOT_SUCCESSFUL);
313     }
314 
315     if (crypto_method_aes -> nx_crypto_cleanup)
316     {
317         status = crypto_method_aes -> nx_crypto_cleanup(metadata);
318     }
319 
320     return(status);
321 }
322 #endif
323 
324 
325