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