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 Secure Component                                                 */
17 /**                                                                       */
18 /**    Transport Layer Security (TLS)                                     */
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 /* f149e41d848f59276cfddd743bafa9a90e1ee4a263a118142b33e3702176ef0a59f8237a1cb51b42f3ded6b202d9af0997898fdd03cf60bda951c514547a0850cec25444ae2f24cb711bfbafcc3956c941d3de69f155e3f8b10f06db5f37359b772ddd43e1035a0a0d3db33242d5843033833b0dd43b870c6bf60e8deab55f317cc3273f5e3ba747f0cb65050cb7228796210d9254873643008d45f29cfd6c5b060c9a */
32 static UCHAR plain_1[] = {
33 0xf1, 0x49, 0xe4, 0x1d, 0x84, 0x8f, 0x59, 0x27, 0x6c, 0xfd, 0xdd, 0x74, 0x3b, 0xaf, 0xa9, 0xa9,
34 0x0e, 0x1e, 0xe4, 0xa2, 0x63, 0xa1, 0x18, 0x14, 0x2b, 0x33, 0xe3, 0x70, 0x21, 0x76, 0xef, 0x0a,
35 0x59, 0xf8, 0x23, 0x7a, 0x1c, 0xb5, 0x1b, 0x42, 0xf3, 0xde, 0xd6, 0xb2, 0x02, 0xd9, 0xaf, 0x09,
36 0x97, 0x89, 0x8f, 0xdd, 0x03, 0xcf, 0x60, 0xbd, 0xa9, 0x51, 0xc5, 0x14, 0x54, 0x7a, 0x08, 0x50,
37 0xce, 0xc2, 0x54, 0x44, 0xae, 0x2f, 0x24, 0xcb, 0x71, 0x1b, 0xfb, 0xaf, 0xcc, 0x39, 0x56, 0xc9,
38 0x41, 0xd3, 0xde, 0x69, 0xf1, 0x55, 0xe3, 0xf8, 0xb1, 0x0f, 0x06, 0xdb, 0x5f, 0x37, 0x35, 0x9b,
39 0x77, 0x2d, 0xdd, 0x43, 0xe1, 0x03, 0x5a, 0x0a, 0x0d, 0x3d, 0xb3, 0x32, 0x42, 0xd5, 0x84, 0x30,
40 0x33, 0x83, 0x3b, 0x0d, 0xd4, 0x3b, 0x87, 0x0c, 0x6b, 0xf6, 0x0e, 0x8d, 0xea, 0xb5, 0x5f, 0x31,
41 0x7c, 0xc3, 0x27, 0x3f, 0x5e, 0x3b, 0xa7, 0x47, 0xf0, 0xcb, 0x65, 0x05, 0x0c, 0xb7, 0x22, 0x87,
42 0x96, 0x21, 0x0d, 0x92, 0x54, 0x87, 0x36, 0x43, 0x00, 0x8d, 0x45, 0xf2, 0x9c, 0xfd, 0x6c, 0x5b,
43 0x06, 0x0c, 0x9a,
44 };
45 
46 /* c108aa74e7a4dfd52806e67e72d76f54 */
47 static UCHAR secret_1[] = {
48 0xc1, 0x08, 0xaa, 0x74, 0xe7, 0xa4, 0xdf, 0xd5, 0x28, 0x06, 0xe6, 0x7e, 0x72, 0xd7, 0x6f, 0x54,
49 };
50 
51 /* Output. */
52 static ULONG output[4];
53 
54 /**************************************************************************/
55 /*                                                                        */
56 /*  FUNCTION                                               RELEASE        */
57 /*                                                                        */
58 /*    nx_crypto_method_self_test_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 MD5 crypto method. */
67 /*                                                                        */
68 /*  INPUT                                                                 */
69 /*                                                                        */
70 /*    method_ptr                            Pointer to the crypto method  */
71 /*                                            to be tested.               */
72 /*                                                                        */
73 /*  OUTPUT                                                                */
74 /*                                                                        */
75 /*    status                                Completion status             */
76 /*                                                                        */
77 /*  CALLS                                                                 */
78 /*                                                                        */
79 /*    None                                                                */
80 /*                                                                        */
81 /*  CALLED BY                                                             */
82 /*                                                                        */
83 /*    Application Code                                                    */
84 /*                                                                        */
85 /*  RELEASE HISTORY                                                       */
86 /*                                                                        */
87 /*    DATE              NAME                      DESCRIPTION             */
88 /*                                                                        */
89 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
90 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
91 /*                                            resulting in version 6.1    */
92 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
93 /*                                            renamed FIPS symbol to      */
94 /*                                            self-test,                  */
95 /*                                            resulting in version 6.1.7  */
96 /*                                                                        */
97 /**************************************************************************/
_nx_crypto_method_self_test_md5(NX_CRYPTO_METHOD * crypto_method_md5,VOID * metadata,UINT metadata_size)98 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_md5(NX_CRYPTO_METHOD *crypto_method_md5,
99                                                     VOID *metadata, UINT metadata_size)
100 {
101 UINT    status;
102 VOID   *handler = NX_CRYPTO_NULL;
103 
104 
105     /* Validate the crypto method */
106     if(crypto_method_md5 == NX_CRYPTO_NULL)
107         return(NX_CRYPTO_PTR_ERROR);
108 
109     if (crypto_method_md5 -> nx_crypto_init)
110     {
111         status = crypto_method_md5 -> nx_crypto_init(crypto_method_md5,
112                                                      NX_CRYPTO_NULL,
113                                                      0,
114                                                      &handler,
115                                                      metadata,
116                                                      metadata_size);
117 
118         if (status != NX_CRYPTO_SUCCESS)
119         {
120             return(status);
121         }
122     }
123 
124     if (crypto_method_md5 -> nx_crypto_operation == NX_CRYPTO_NULL)
125     {
126         return(NX_CRYPTO_PTR_ERROR);
127     }
128 
129     /* Clear the output buffer.  */
130     NX_CRYPTO_MEMSET(output, 0, sizeof(output));
131 
132     /* Call the crypto operation function.  */
133     status = crypto_method_md5 -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
134                                                       handler,
135                                                       crypto_method_md5,
136                                                       NX_CRYPTO_NULL,
137                                                       0,
138                                                       plain_1,
139                                                       sizeof(plain_1),
140                                                       NX_CRYPTO_NULL,
141                                                       (UCHAR *)output,
142                                                       sizeof(secret_1),
143                                                       metadata,
144                                                       metadata_size,
145                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
146 
147     /* Check the status.  */
148     if(status != NX_CRYPTO_SUCCESS)
149     {
150         return(status);
151     }
152 
153     /* Validate the output.  */
154     if(NX_CRYPTO_MEMCMP(output, secret_1, sizeof(secret_1)) != 0)
155     {
156         return(NX_CRYPTO_NOT_SUCCESSFUL);
157     }
158 
159     if (crypto_method_md5 -> nx_crypto_cleanup)
160     {
161         status = crypto_method_md5 -> nx_crypto_cleanup(metadata);
162     }
163 
164     return(status);
165 }
166 #endif
167