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_DES_KEY_LEN_IN_BITS
30 #define NX_CRYPTO_DES_KEY_LEN_IN_BITS 64
31 #endif
32
33
34 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
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_secure_crypto_method_self_test_des PORTABLE C */
71 /* 6.1 */
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 /* */
105 /**************************************************************************/
_nx_secure_crypto_method_self_test_des(NX_CRYPTO_METHOD * crypto_method_des,VOID * metadata,UINT metadata_size)106 UINT _nx_secure_crypto_method_self_test_des(NX_CRYPTO_METHOD *crypto_method_des,
107 VOID *metadata, UINT metadata_size)
108 {
109 UINT status;
110 VOID *handler = NX_NULL;
111
112
113 /* Validate the crypto method */
114 if(crypto_method_des == NX_NULL)
115 return(NX_PTR_ERROR);
116
117 if (crypto_method_des -> nx_crypto_init)
118 {
119 status = crypto_method_des -> nx_crypto_init(crypto_method_des,
120 key_1,
121 NX_CRYPTO_DES_KEY_LEN_IN_BITS,
122 &handler,
123 metadata,
124 metadata_size);
125
126 if (status != NX_CRYPTO_SUCCESS)
127 {
128 return(status);
129 }
130 }
131
132 if (crypto_method_des -> nx_crypto_operation == NX_NULL)
133 {
134 return(NX_PTR_ERROR);
135 }
136
137 /* Encryption. */
138 status = crypto_method_des -> nx_crypto_operation(NX_CRYPTO_ENCRYPT,
139 handler,
140 crypto_method_des,
141 key_1,
142 NX_CRYPTO_DES_KEY_LEN_IN_BITS,
143 plain_1,
144 INPUT_OUTPUT_LENGTH,
145 iv_1,
146 (UCHAR *)output,
147 INPUT_OUTPUT_LENGTH,
148 metadata,
149 metadata_size,
150 NX_NULL, NX_NULL);
151
152 if (status != NX_CRYPTO_SUCCESS)
153 {
154 return(status);
155 }
156
157 if (NX_SECURE_MEMCMP(output, secret_1, INPUT_OUTPUT_LENGTH) != 0)
158 {
159 return(NX_NOT_SUCCESSFUL);
160 }
161
162 /* Decryption. */
163 status = crypto_method_des -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
164 handler,
165 crypto_method_des,
166 key_1,
167 NX_CRYPTO_DES_KEY_LEN_IN_BITS,
168 secret_1,
169 INPUT_OUTPUT_LENGTH,
170 iv_1,
171 output,
172 INPUT_OUTPUT_LENGTH,
173 metadata,
174 metadata_size,
175 NX_NULL, NX_NULL);
176
177 if (status != NX_CRYPTO_SUCCESS)
178 {
179 return(status);
180 }
181
182 if (NX_SECURE_MEMCMP(output, plain_1, INPUT_OUTPUT_LENGTH) != 0)
183 {
184 return(NX_NOT_SUCCESSFUL);
185 }
186
187 if (crypto_method_des -> nx_crypto_cleanup)
188 {
189 status = crypto_method_des -> nx_crypto_cleanup(metadata);
190 }
191
192 return(status);
193 }
194 #endif
195