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 #ifndef NX_CRYPTO_DES_KEY_LEN_IN_BITS
30 #define NX_CRYPTO_DES_KEY_LEN_IN_BITS 64
31 #endif
32 
33 
34 #ifdef NX_CRYPTO_SELF_TEST
35 
36 #define INPUT_OUTPUT_LENGTH 64
37 
38 /* 7a52e4d342aa0725 */
39 static UCHAR key_1[] = {
40 0x7a, 0x52, 0xe4, 0xd3, 0x42, 0xaa, 0x07, 0x25,
41 };
42 
43 /* 5a7e7c34266cf730 */
44 static UCHAR iv_1[] = {
45 0x5a, 0x7e, 0x7c, 0x34, 0x26, 0x6c, 0xf7, 0x30,
46 };
47 
48 /* 7e771c6ee4b26db89050e982ba7e9803c8da34606434dd85d2910e538076d00168c9885ba2be03181f65f1e04e83d6ba6880467550bcf099be26dc9d9c0af15a */
49 static UCHAR plain_1[] = {
50 0x7e, 0x77, 0x1c, 0x6e, 0xe4, 0xb2, 0x6d, 0xb8, 0x90, 0x50, 0xe9, 0x82, 0xba, 0x7e, 0x98, 0x03,
51 0xc8, 0xda, 0x34, 0x60, 0x64, 0x34, 0xdd, 0x85, 0xd2, 0x91, 0x0e, 0x53, 0x80, 0x76, 0xd0, 0x01,
52 0x68, 0xc9, 0x88, 0x5b, 0xa2, 0xbe, 0x03, 0x18, 0x1f, 0x65, 0xf1, 0xe0, 0x4e, 0x83, 0xd6, 0xba,
53 0x68, 0x80, 0x46, 0x75, 0x50, 0xbc, 0xf0, 0x99, 0xbe, 0x26, 0xdc, 0x9d, 0x9c, 0x0a, 0xf1, 0x5a,
54 };
55 
56 /* 13570ef15c1e8a71ae9de5552d9c7f7ca12eb830d85e6eb6d0402f627e23b967ba6e15cd97e9c9b64b16fe910903d9cb3757b3078a90c2ba4645fd638fa0725c */
57 static UCHAR secret_1[] = {
58 0x13, 0x57, 0x0e, 0xf1, 0x5c, 0x1e, 0x8a, 0x71, 0xae, 0x9d, 0xe5, 0x55, 0x2d, 0x9c, 0x7f, 0x7c,
59 0xa1, 0x2e, 0xb8, 0x30, 0xd8, 0x5e, 0x6e, 0xb6, 0xd0, 0x40, 0x2f, 0x62, 0x7e, 0x23, 0xb9, 0x67,
60 0xba, 0x6e, 0x15, 0xcd, 0x97, 0xe9, 0xc9, 0xb6, 0x4b, 0x16, 0xfe, 0x91, 0x09, 0x03, 0xd9, 0xcb,
61 0x37, 0x57, 0xb3, 0x07, 0x8a, 0x90, 0xc2, 0xba, 0x46, 0x45, 0xfd, 0x63, 0x8f, 0xa0, 0x72, 0x5c,
62 };
63 
64 static UCHAR output[INPUT_OUTPUT_LENGTH];
65 
66 /**************************************************************************/
67 /*                                                                        */
68 /*  FUNCTION                                               RELEASE        */
69 /*                                                                        */
70 /*    nx_crypto_method_self_test_des                      PORTABLE C      */
71 /*                                                           6.1.7        */
72 /*  AUTHOR                                                                */
73 /*                                                                        */
74 /*    Timothy Stapko, Microsoft Corporation                               */
75 /*                                                                        */
76 /*  DESCRIPTION                                                           */
77 /*                                                                        */
78 /*    This function performs the Known Answer Test for DES crypto method. */
79 /*                                                                        */
80 /*  INPUT                                                                 */
81 /*                                                                        */
82 /*    method_ptr                            Pointer to the crypto method  */
83 /*                                            to be tested.               */
84 /*                                                                        */
85 /*  OUTPUT                                                                */
86 /*                                                                        */
87 /*    status                                Completion status             */
88 /*                                                                        */
89 /*  CALLS                                                                 */
90 /*                                                                        */
91 /*    None                                                                */
92 /*                                                                        */
93 /*  CALLED BY                                                             */
94 /*                                                                        */
95 /*    Application Code                                                    */
96 /*                                                                        */
97 /*  RELEASE HISTORY                                                       */
98 /*                                                                        */
99 /*    DATE              NAME                      DESCRIPTION             */
100 /*                                                                        */
101 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
102 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
103 /*                                            resulting in version 6.1    */
104 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
105 /*                                            renamed FIPS symbol to      */
106 /*                                            self-test,                  */
107 /*                                            resulting in version 6.1.7  */
108 /*                                                                        */
109 /**************************************************************************/
_nx_crypto_method_self_test_des(NX_CRYPTO_METHOD * crypto_method_des,VOID * metadata,UINT metadata_size)110 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_des(NX_CRYPTO_METHOD *crypto_method_des,
111                                                     VOID *metadata, UINT metadata_size)
112 {
113 UINT status;
114 VOID *handler = NX_CRYPTO_NULL;
115 
116 
117     /* Validate the crypto method */
118     if(crypto_method_des == NX_CRYPTO_NULL)
119         return(NX_CRYPTO_PTR_ERROR);
120 
121     if (crypto_method_des -> nx_crypto_init)
122     {
123         status = crypto_method_des -> nx_crypto_init(crypto_method_des,
124                                                      key_1,
125                                                      NX_CRYPTO_DES_KEY_LEN_IN_BITS,
126                                                      &handler,
127                                                      metadata,
128                                                      metadata_size);
129 
130         if (status != NX_CRYPTO_SUCCESS)
131         {
132             return(status);
133         }
134     }
135 
136     if (crypto_method_des -> nx_crypto_operation == NX_CRYPTO_NULL)
137     {
138         return(NX_CRYPTO_PTR_ERROR);
139     }
140 
141     /* Encryption. */
142     status = crypto_method_des -> nx_crypto_operation(NX_CRYPTO_ENCRYPT,
143                                                       handler,
144                                                       crypto_method_des,
145                                                       key_1,
146                                                       NX_CRYPTO_DES_KEY_LEN_IN_BITS,
147                                                       plain_1,
148                                                       INPUT_OUTPUT_LENGTH,
149                                                       iv_1,
150                                                       (UCHAR *)output,
151                                                       INPUT_OUTPUT_LENGTH,
152                                                       metadata,
153                                                       metadata_size,
154                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
155 
156     if (status != NX_CRYPTO_SUCCESS)
157     {
158         return(status);
159     }
160 
161     if (NX_CRYPTO_MEMCMP(output, secret_1, INPUT_OUTPUT_LENGTH) != 0)
162     {
163         return(NX_CRYPTO_NOT_SUCCESSFUL);
164     }
165 
166     /* Decryption. */
167     status = crypto_method_des -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
168                                                       handler,
169                                                       crypto_method_des,
170                                                       key_1,
171                                                       NX_CRYPTO_DES_KEY_LEN_IN_BITS,
172                                                       secret_1,
173                                                       INPUT_OUTPUT_LENGTH,
174                                                       iv_1,
175                                                       output,
176                                                       INPUT_OUTPUT_LENGTH,
177                                                       metadata,
178                                                       metadata_size,
179                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
180 
181     if (status != NX_CRYPTO_SUCCESS)
182     {
183         return(status);
184     }
185 
186     if (NX_CRYPTO_MEMCMP(output, plain_1, INPUT_OUTPUT_LENGTH) != 0)
187     {
188         return(NX_CRYPTO_NOT_SUCCESSFUL);
189     }
190 
191     if (crypto_method_des -> nx_crypto_cleanup)
192     {
193         status = crypto_method_des -> nx_crypto_cleanup(metadata);
194     }
195 
196     return(status);
197 }
198 #endif
199