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 
30 #ifdef NX_CRYPTO_SELF_TEST
31 
32 /* BF8D54470B5F365F14F84C0E046DE4053DE7A05DB847CD2C9844632082F588602EA3885DEF9CA21B8B46AA7CB9588938 */
33 static UCHAR key_1[] = {
34 0xBF, 0x8D, 0x54, 0x47, 0x0B, 0x5F, 0x36, 0x5F, 0x14, 0xF8, 0x4C, 0x0E, 0x04, 0x6D, 0xE4, 0x05,
35 0x3D, 0xE7, 0xA0, 0x5D, 0xB8, 0x47, 0xCD, 0x2C, 0x98, 0x44, 0x63, 0x20, 0x82, 0xF5, 0x88, 0x60,
36 0x2E, 0xA3, 0x88, 0x5D, 0xEF, 0x9C, 0xA2, 0x1B, 0x8B, 0x46, 0xAA, 0x7C, 0xB9, 0x58, 0x89, 0x38,
37 };
38 
39 /* B9CE4947AFA6EB6FB36FC514B7C6A33704F47007E9846663B4564C455202D568FCD43044C156DC0154C5772F99D0093194864A5CEDD5640F202C3874222AB061AF */
40 static UCHAR plain_1[] = {
41 0xB9, 0xCE, 0x49, 0x47, 0xAF, 0xA6, 0xEB, 0x6F, 0xB3, 0x6F, 0xC5, 0x14, 0xB7, 0xC6, 0xA3, 0x37,
42 0x04, 0xF4, 0x70, 0x07, 0xE9, 0x84, 0x66, 0x63, 0xB4, 0x56, 0x4C, 0x45, 0x52, 0x02, 0xD5, 0x68,
43 0xFC, 0xD4, 0x30, 0x44, 0xC1, 0x56, 0xDC, 0x01, 0x54, 0xC5, 0x77, 0x2F, 0x99, 0xD0, 0x09, 0x31,
44 0x94, 0x86, 0x4A, 0x5C, 0xED, 0xD5, 0x64, 0x0F, 0x20, 0x2C, 0x38, 0x74, 0x22, 0x2A, 0xB0, 0x61,
45 0xAF, };
46 
47 /* 806EB34DD557E8C076832A27D2E5FC51 */
48 static UCHAR secret_1[] = {
49 0x80, 0x6E, 0xB3, 0x4D, 0xD5, 0x57, 0xE8, 0xC0, 0x76, 0x83, 0x2A, 0x27, 0xD2, 0xE5, 0xFC, 0x51,
50 };
51 
52 /* Output. */
53 static ULONG output[4];
54 
55 /**************************************************************************/
56 /*                                                                        */
57 /*  FUNCTION                                               RELEASE        */
58 /*                                                                        */
59 /*    nx_crypto_method_self_test_hmac_md5                 PORTABLE C      */
60 /*                                                           6.1.7        */
61 /*  AUTHOR                                                                */
62 /*                                                                        */
63 /*    Timothy Stapko, Microsoft Corporation                               */
64 /*                                                                        */
65 /*  DESCRIPTION                                                           */
66 /*                                                                        */
67 /*    This function performs the Known Answer Test for HMAC MD5 crypto    */
68 /*    method.                                                             */
69 /*                                                                        */
70 /*  INPUT                                                                 */
71 /*                                                                        */
72 /*    method_ptr                            Pointer to the crypto method  */
73 /*                                            to be tested.               */
74 /*                                                                        */
75 /*  OUTPUT                                                                */
76 /*                                                                        */
77 /*    status                                Completion status             */
78 /*                                                                        */
79 /*  CALLS                                                                 */
80 /*                                                                        */
81 /*    None                                                                */
82 /*                                                                        */
83 /*  CALLED BY                                                             */
84 /*                                                                        */
85 /*    Application Code                                                    */
86 /*                                                                        */
87 /*  RELEASE HISTORY                                                       */
88 /*                                                                        */
89 /*    DATE              NAME                      DESCRIPTION             */
90 /*                                                                        */
91 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
92 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
93 /*                                            resulting in version 6.1    */
94 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
95 /*                                            renamed FIPS symbol to      */
96 /*                                            self-test,                  */
97 /*                                            resulting in version 6.1.7  */
98 /*                                                                        */
99 /**************************************************************************/
_nx_crypto_method_self_test_hmac_md5(NX_CRYPTO_METHOD * crypto_method_hmac_md5,VOID * metadata,UINT metadata_size)100 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_hmac_md5(NX_CRYPTO_METHOD *crypto_method_hmac_md5,
101                                                          VOID *metadata, UINT metadata_size)
102 {
103 UINT    status;
104 UINT    output_length;
105 VOID   *handler = NX_CRYPTO_NULL;
106 
107 
108     /* Validate the crypto method */
109     if(crypto_method_hmac_md5 == NX_CRYPTO_NULL)
110         return(NX_CRYPTO_PTR_ERROR);
111 
112     if (crypto_method_hmac_md5 -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_MD5_96)
113     {
114         output_length = 12;
115     }
116     else if (crypto_method_hmac_md5 -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_MD5_128)
117     {
118         output_length = 16;
119     }
120     else
121     {
122         return(1);
123     }
124 
125     if (crypto_method_hmac_md5 -> nx_crypto_init)
126     {
127         status = crypto_method_hmac_md5 -> nx_crypto_init(crypto_method_hmac_md5,
128                                                           key_1,
129                                                           (sizeof(key_1) << 3),
130                                                           &handler,
131                                                           metadata,
132                                                           metadata_size);
133 
134         if (status != NX_CRYPTO_SUCCESS)
135         {
136             return(status);
137         }
138     }
139 
140     if (crypto_method_hmac_md5 -> nx_crypto_operation == NX_CRYPTO_NULL)
141     {
142         return(NX_CRYPTO_PTR_ERROR);
143     }
144 
145     /* Reset the output buffer.  */
146     NX_CRYPTO_MEMSET(output, 0xFF, sizeof(output));
147 
148     /* Test HMAC MD5 with Authenticate operation.  */
149     status = crypto_method_hmac_md5 -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
150                                                            handler,
151                                                            crypto_method_hmac_md5,
152                                                            key_1,
153                                                            (sizeof(key_1) << 3),
154                                                            plain_1,
155                                                            sizeof(plain_1),
156                                                            NX_CRYPTO_NULL,
157                                                            (UCHAR *)output,
158                                                            output_length,
159                                                            metadata,
160                                                            metadata_size,
161                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
162 
163     /* Check the status.  */
164     if(status != NX_CRYPTO_SUCCESS)
165     {
166         return(status);
167     }
168 
169     /* Validate the output.  */
170     if(NX_CRYPTO_MEMCMP(output, secret_1, output_length) != 0)
171     {
172         return(NX_CRYPTO_NOT_SUCCESSFUL);
173     }
174 
175     /* Reset the output buffer.  */
176     NX_CRYPTO_MEMSET(output, 0xFF, sizeof(output));
177 
178     /* Test HMAC MD5 with Initialize, Update and Calculate operation.  */
179     status = crypto_method_hmac_md5 -> nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE,
180                                                            handler,
181                                                            crypto_method_hmac_md5,
182                                                            key_1,
183                                                            (sizeof(key_1) << 3),
184                                                            NX_CRYPTO_NULL,
185                                                            0,
186                                                            NX_CRYPTO_NULL,
187                                                            NX_CRYPTO_NULL,
188                                                            0,
189                                                            metadata,
190                                                            metadata_size,
191                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
192 
193     /* Check the status.  */
194     if(status != NX_CRYPTO_SUCCESS)
195     {
196         return(status);
197     }
198 
199     status = crypto_method_hmac_md5 -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
200                                                            handler,
201                                                            crypto_method_hmac_md5,
202                                                            NX_CRYPTO_NULL,
203                                                            0,
204                                                            plain_1,
205                                                            sizeof(plain_1),
206                                                            NX_CRYPTO_NULL,
207                                                            NX_CRYPTO_NULL,
208                                                            0,
209                                                            metadata,
210                                                            metadata_size,
211                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
212 
213     /* Check the status.  */
214     if(status != NX_CRYPTO_SUCCESS)
215     {
216         return(status);
217     }
218 
219     status = crypto_method_hmac_md5 -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
220                                                            handler,
221                                                            crypto_method_hmac_md5,
222                                                            NX_CRYPTO_NULL,
223                                                            0,
224                                                            NX_CRYPTO_NULL,
225                                                            0,
226                                                            NX_CRYPTO_NULL,
227                                                            (UCHAR *)output,
228                                                            output_length,
229                                                            metadata,
230                                                            metadata_size,
231                                                            NX_CRYPTO_NULL, NX_CRYPTO_NULL);
232 
233     /* Check the status.  */
234     if(status != NX_CRYPTO_SUCCESS)
235     {
236         return(status);
237     }
238 
239     /* Validate the output.  */
240     if(NX_CRYPTO_MEMCMP(output, secret_1, output_length) != 0)
241     {
242         return(NX_CRYPTO_NOT_SUCCESSFUL);
243     }
244 
245     if (crypto_method_hmac_md5 -> nx_crypto_cleanup)
246     {
247         status = crypto_method_hmac_md5 -> nx_crypto_cleanup(metadata);
248     }
249 
250     return(status);
251 }
252 #endif
253