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