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_3DES_KEY_LEN_IN_BITS
29 #define NX_CRYPTO_3DES_KEY_LEN_IN_BITS    192
30 #endif
31 
32 
33 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
34 
35 #define INPUT_OUTPUT_LENGTH 72
36 
37 /* 8f4f7aab25043720f4fbae01aedf071c68a283689b08ad20 */
38 static UCHAR key_1[] = {
39 0x8f, 0x4f, 0x7a, 0xab, 0x25, 0x04, 0x37, 0x20, 0xf4, 0xfb, 0xae, 0x01, 0xae, 0xdf, 0x07, 0x1c,
40 0x68, 0xa2, 0x83, 0x68, 0x9b, 0x08, 0xad, 0x20,
41 };
42 
43 /* 17fdf67a5290baff */
44 static UCHAR iv_1[] = {
45 0x17, 0xfd, 0xf6, 0x7a, 0x52, 0x90, 0xba, 0xff,
46 };
47 
48 /* 67c3aaf34a1dce2ee6a65b4f6c9c272384d343cae3fd2d1520284733c388888da07772ee63ba44e76b067072dcc24fd5ef0f14c98d06ffdc1d40d3149a9c89d5e83c460468d18b6d */
49 static UCHAR plain_1[] = {
50 0x67, 0xc3, 0xaa, 0xf3, 0x4a, 0x1d, 0xce, 0x2e, 0xe6, 0xa6, 0x5b, 0x4f, 0x6c, 0x9c, 0x27, 0x23,
51 0x84, 0xd3, 0x43, 0xca, 0xe3, 0xfd, 0x2d, 0x15, 0x20, 0x28, 0x47, 0x33, 0xc3, 0x88, 0x88, 0x8d,
52 0xa0, 0x77, 0x72, 0xee, 0x63, 0xba, 0x44, 0xe7, 0x6b, 0x06, 0x70, 0x72, 0xdc, 0xc2, 0x4f, 0xd5,
53 0xef, 0x0f, 0x14, 0xc9, 0x8d, 0x06, 0xff, 0xdc, 0x1d, 0x40, 0xd3, 0x14, 0x9a, 0x9c, 0x89, 0xd5,
54 0xe8, 0x3c, 0x46, 0x04, 0x68, 0xd1, 0x8b, 0x6d,
55 };
56 
57 /* b2a6679b02081e22aaa950f81a414fa3e7023ca6b1ba0e8e599ecfa80797392a70081c68b73aec962384d70835a80f8739d6d5f1aba404f6d16eab6f6115ccedc4da93a27ef36bff */
58 static UCHAR secret_1[] = {
59 0xb2, 0xa6, 0x67, 0x9b, 0x02, 0x08, 0x1e, 0x22, 0xaa, 0xa9, 0x50, 0xf8, 0x1a, 0x41, 0x4f, 0xa3,
60 0xe7, 0x02, 0x3c, 0xa6, 0xb1, 0xba, 0x0e, 0x8e, 0x59, 0x9e, 0xcf, 0xa8, 0x07, 0x97, 0x39, 0x2a,
61 0x70, 0x08, 0x1c, 0x68, 0xb7, 0x3a, 0xec, 0x96, 0x23, 0x84, 0xd7, 0x08, 0x35, 0xa8, 0x0f, 0x87,
62 0x39, 0xd6, 0xd5, 0xf1, 0xab, 0xa4, 0x04, 0xf6, 0xd1, 0x6e, 0xab, 0x6f, 0x61, 0x15, 0xcc, 0xed,
63 0xc4, 0xda, 0x93, 0xa2, 0x7e, 0xf3, 0x6b, 0xff,
64 };
65 
66 static UCHAR output[INPUT_OUTPUT_LENGTH];
67 
68 /**************************************************************************/
69 /*                                                                        */
70 /*  FUNCTION                                               RELEASE        */
71 /*                                                                        */
72 /*    nx_secure_crypto_method_self_test_3des              PORTABLE C      */
73 /*                                                           6.1          */
74 /*  AUTHOR                                                                */
75 /*                                                                        */
76 /*    Timothy Stapko, Microsoft Corporation                               */
77 /*                                                                        */
78 /*  DESCRIPTION                                                           */
79 /*                                                                        */
80 /*    This function performs the Known Answer Test for 3DES crypto method.*/
81 /*                                                                        */
82 /*  INPUT                                                                 */
83 /*                                                                        */
84 /*    method_ptr                            Pointer to the crypto method  */
85 /*                                            to be tested.               */
86 /*                                                                        */
87 /*  OUTPUT                                                                */
88 /*                                                                        */
89 /*    status                                Completion status             */
90 /*                                                                        */
91 /*  CALLS                                                                 */
92 /*                                                                        */
93 /*    None                                                                */
94 /*                                                                        */
95 /*  CALLED BY                                                             */
96 /*                                                                        */
97 /*    Application Code                                                    */
98 /*                                                                        */
99 /*  RELEASE HISTORY                                                       */
100 /*                                                                        */
101 /*    DATE              NAME                      DESCRIPTION             */
102 /*                                                                        */
103 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
104 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
105 /*                                            resulting in version 6.1    */
106 /*                                                                        */
107 /**************************************************************************/
_nx_secure_crypto_method_self_test_3des(NX_CRYPTO_METHOD * crypto_method_3des,VOID * metadata,UINT metadata_size)108 UINT _nx_secure_crypto_method_self_test_3des(NX_CRYPTO_METHOD *crypto_method_3des,
109                                              VOID *metadata, UINT metadata_size)
110 {
111 UINT status;
112 VOID *handler = NX_NULL;
113 
114 
115     /* Validate the crypto method */
116     if(crypto_method_3des == NX_NULL)
117         return(NX_PTR_ERROR);
118 
119     if (crypto_method_3des -> nx_crypto_init)
120     {
121         status = crypto_method_3des -> nx_crypto_init(crypto_method_3des,
122                                                       key_1,
123                                                       NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
124                                                       &handler,
125                                                       metadata,
126                                                       metadata_size);
127 
128         if (status != NX_CRYPTO_SUCCESS)
129         {
130             return(status);
131         }
132     }
133 
134     if (crypto_method_3des -> nx_crypto_operation == NX_NULL)
135     {
136         return(NX_PTR_ERROR);
137     }
138 
139     /* Encryption. */
140     status = crypto_method_3des -> nx_crypto_operation(NX_CRYPTO_ENCRYPT,
141                                                        handler,
142                                                        crypto_method_3des,
143                                                        key_1,
144                                                        NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
145                                                        plain_1,
146                                                        INPUT_OUTPUT_LENGTH,
147                                                        iv_1,
148                                                        (UCHAR *)output,
149                                                        INPUT_OUTPUT_LENGTH,
150                                                        metadata,
151                                                        metadata_size,
152                                                        NX_NULL, NX_NULL);
153 
154     if (status != NX_CRYPTO_SUCCESS)
155     {
156         return(status);
157     }
158 
159     if (NX_SECURE_MEMCMP(output, secret_1, INPUT_OUTPUT_LENGTH) != 0)
160     {
161         return(NX_NOT_SUCCESSFUL);
162     }
163 
164     /* Decryption. */
165     status = crypto_method_3des -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
166                                                        handler,
167                                                        crypto_method_3des,
168                                                        key_1,
169                                                        NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
170                                                        secret_1,
171                                                        INPUT_OUTPUT_LENGTH,
172                                                        iv_1,
173                                                        output,
174                                                        INPUT_OUTPUT_LENGTH,
175                                                        metadata,
176                                                        metadata_size,
177                                                        NX_NULL, NX_NULL);
178 
179     if (status != NX_CRYPTO_SUCCESS)
180     {
181         return(status);
182     }
183 
184     if (NX_SECURE_MEMCMP(output, plain_1, INPUT_OUTPUT_LENGTH) != 0)
185     {
186         return(NX_NOT_SUCCESSFUL);
187     }
188 
189     if (crypto_method_3des -> nx_crypto_cleanup)
190     {
191         status = crypto_method_3des -> nx_crypto_cleanup(metadata);
192     }
193 
194     return(status);
195 }
196 #endif
197