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