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_3DES_KEY_LEN_IN_BITS
30 #define NX_CRYPTO_3DES_KEY_LEN_IN_BITS 192
31 #endif
32
33
34 #ifdef NX_CRYPTO_SELF_TEST
35
36 /* 8f4f7aab25043720f4fbae01aedf071c68a283689b08ad20 */
37 static UCHAR key_1[] = {
38 0x8f, 0x4f, 0x7a, 0xab, 0x25, 0x04, 0x37, 0x20, 0xf4, 0xfb, 0xae, 0x01, 0xae, 0xdf, 0x07, 0x1c,
39 0x68, 0xa2, 0x83, 0x68, 0x9b, 0x08, 0xad, 0x20,
40 };
41
42 /* 17fdf67a5290baff */
43 static UCHAR iv_1[] = {
44 0x17, 0xfd, 0xf6, 0x7a, 0x52, 0x90, 0xba, 0xff,
45 };
46
47 /* 67c3aaf34a1dce2ee6a65b4f6c9c272384d343cae3fd2d1520284733c388888da07772ee63ba44e76b067072dcc24fd5ef0f14c98d06ffdc1d40d3149a9c89d5e83c460468d18b6d */
48 static UCHAR plain_1[] = {
49 0x67, 0xc3, 0xaa, 0xf3, 0x4a, 0x1d, 0xce, 0x2e, 0xe6, 0xa6, 0x5b, 0x4f, 0x6c, 0x9c, 0x27, 0x23,
50 0x84, 0xd3, 0x43, 0xca, 0xe3, 0xfd, 0x2d, 0x15, 0x20, 0x28, 0x47, 0x33, 0xc3, 0x88, 0x88, 0x8d,
51 0xa0, 0x77, 0x72, 0xee, 0x63, 0xba, 0x44, 0xe7, 0x6b, 0x06, 0x70, 0x72, 0xdc, 0xc2, 0x4f, 0xd5,
52 0xef, 0x0f, 0x14, 0xc9, 0x8d, 0x06, 0xff, 0xdc, 0x1d, 0x40, 0xd3, 0x14, 0x9a, 0x9c, 0x89, 0xd5,
53 0xe8, 0x3c, 0x46, 0x04, 0x68, 0xd1, 0x8b, 0x6d,
54 };
55
56 /* b2a6679b02081e22aaa950f81a414fa3e7023ca6b1ba0e8e599ecfa80797392a70081c68b73aec962384d70835a80f8739d6d5f1aba404f6d16eab6f6115ccedc4da93a27ef36bff */
57 static UCHAR secret_1[] = {
58 0xb2, 0xa6, 0x67, 0x9b, 0x02, 0x08, 0x1e, 0x22, 0xaa, 0xa9, 0x50, 0xf8, 0x1a, 0x41, 0x4f, 0xa3,
59 0xe7, 0x02, 0x3c, 0xa6, 0xb1, 0xba, 0x0e, 0x8e, 0x59, 0x9e, 0xcf, 0xa8, 0x07, 0x97, 0x39, 0x2a,
60 0x70, 0x08, 0x1c, 0x68, 0xb7, 0x3a, 0xec, 0x96, 0x23, 0x84, 0xd7, 0x08, 0x35, 0xa8, 0x0f, 0x87,
61 0x39, 0xd6, 0xd5, 0xf1, 0xab, 0xa4, 0x04, 0xf6, 0xd1, 0x6e, 0xab, 0x6f, 0x61, 0x15, 0xcc, 0xed,
62 0xc4, 0xda, 0x93, 0xa2, 0x7e, 0xf3, 0x6b, 0xff,
63 };
64
65 /* 7acb4a6d4f37897029e5fefb8cfd2c4c499b4ca7f8d9df49 */
66 static UCHAR key_2[] = {
67 0x7a, 0xcb, 0x4a, 0x6d, 0x4f, 0x37, 0x89, 0x70, 0x29, 0xe5, 0xfe, 0xfb, 0x8c, 0xfd, 0x2c, 0x4c,
68 0x49, 0x9b, 0x4c, 0xa7, 0xf8, 0xd9, 0xdf, 0x49,
69 };
70
71 /* c925db87872e384e */
72 static UCHAR iv_2[] = {
73 0xc9, 0x25, 0xdb, 0x87, 0x87, 0x2e, 0x38, 0x4e,
74 };
75 /* c482190e780ebe87136b60b489493fecacc84d62aae9d641d74266fbadf7e8dd11e50be8d820eadc8aa0eb956637e79aa6c68cf404b1003c7c90c7f85428e728 */
76 static UCHAR plain_2[] = {
77 0xc4, 0x82, 0x19, 0x0e, 0x78, 0x0e, 0xbe, 0x87, 0x13, 0x6b, 0x60, 0xb4, 0x89, 0x49, 0x3f, 0xec,
78 0xac, 0xc8, 0x4d, 0x62, 0xaa, 0xe9, 0xd6, 0x41, 0xd7, 0x42, 0x66, 0xfb, 0xad, 0xf7, 0xe8, 0xdd,
79 0x11, 0xe5, 0x0b, 0xe8, 0xd8, 0x20, 0xea, 0xdc, 0x8a, 0xa0, 0xeb, 0x95, 0x66, 0x37, 0xe7, 0x9a,
80 0xa6, 0xc6, 0x8c, 0xf4, 0x04, 0xb1, 0x00, 0x3c, 0x7c, 0x90, 0xc7, 0xf8, 0x54, 0x28, 0xe7, 0x28,
81 };
82
83 /* 56304aa594944ede1dbd40f2e64d2da3057a52dc617c08094cf07a8a07a90eb4eba3d3c3790640ffd3b124213487e7c0031cf521251ae7d13f4e797aa4fbae81 */
84 static UCHAR secret_2[] = {
85 0x56, 0x30, 0x4a, 0xa5, 0x94, 0x94, 0x4e, 0xde, 0x1d, 0xbd, 0x40, 0xf2, 0xe6, 0x4d, 0x2d, 0xa3,
86 0x05, 0x7a, 0x52, 0xdc, 0x61, 0x7c, 0x08, 0x09, 0x4c, 0xf0, 0x7a, 0x8a, 0x07, 0xa9, 0x0e, 0xb4,
87 0xeb, 0xa3, 0xd3, 0xc3, 0x79, 0x06, 0x40, 0xff, 0xd3, 0xb1, 0x24, 0x21, 0x34, 0x87, 0xe7, 0xc0,
88 0x03, 0x1c, 0xf5, 0x21, 0x25, 0x1a, 0xe7, 0xd1, 0x3f, 0x4e, 0x79, 0x7a, 0xa4, 0xfb, 0xae, 0x81,
89 };
90
91 static UCHAR output[72];
92
93 /**************************************************************************/
94 /* */
95 /* FUNCTION RELEASE */
96 /* */
97 /* nx_crypto_method_self_test_3des PORTABLE C */
98 /* 6.1.7 */
99 /* AUTHOR */
100 /* */
101 /* Timothy Stapko, Microsoft Corporation */
102 /* */
103 /* DESCRIPTION */
104 /* */
105 /* This function performs the Known Answer Test for 3DES crypto method.*/
106 /* */
107 /* INPUT */
108 /* */
109 /* method_ptr Pointer to the crypto method */
110 /* to be tested. */
111 /* */
112 /* OUTPUT */
113 /* */
114 /* status Completion status */
115 /* */
116 /* CALLS */
117 /* */
118 /* None */
119 /* */
120 /* CALLED BY */
121 /* */
122 /* Application Code */
123 /* */
124 /* RELEASE HISTORY */
125 /* */
126 /* DATE NAME DESCRIPTION */
127 /* */
128 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
129 /* 09-30-2020 Timothy Stapko Modified comment(s), */
130 /* resulting in version 6.1 */
131 /* 06-02-2021 Bhupendra Naphade Modified comment(s), */
132 /* renamed FIPS symbol to */
133 /* self-test, */
134 /* resulting in version 6.1.7 */
135 /* */
136 /**************************************************************************/
_nx_crypto_method_self_test_3des(NX_CRYPTO_METHOD * crypto_method_3des,VOID * metadata,UINT metadata_size)137 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_3des(NX_CRYPTO_METHOD *crypto_method_3des,
138 VOID *metadata, UINT metadata_size)
139 {
140 UINT status;
141 VOID *handler = NX_CRYPTO_NULL;
142 UINT input_output_length;
143
144
145 /* Validate the crypto method */
146 if(crypto_method_3des == NX_CRYPTO_NULL)
147 return(NX_CRYPTO_PTR_ERROR);
148
149 /* Encryption. */
150 if (crypto_method_3des -> nx_crypto_init)
151 {
152 status = crypto_method_3des -> nx_crypto_init(crypto_method_3des,
153 key_1,
154 NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
155 &handler,
156 metadata,
157 metadata_size);
158
159 if (status != NX_CRYPTO_SUCCESS)
160 {
161 return(status);
162 }
163 }
164
165 if (crypto_method_3des -> nx_crypto_operation == NX_CRYPTO_NULL)
166 {
167 return(NX_CRYPTO_PTR_ERROR);
168 }
169
170 input_output_length = sizeof(plain_1);
171
172 status = crypto_method_3des -> nx_crypto_operation(NX_CRYPTO_ENCRYPT,
173 handler,
174 crypto_method_3des,
175 key_1,
176 NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
177 plain_1,
178 input_output_length,
179 iv_1,
180 (UCHAR *)output,
181 input_output_length,
182 metadata,
183 metadata_size,
184 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
185
186 if (status != NX_CRYPTO_SUCCESS)
187 {
188 return(status);
189 }
190
191 if (NX_CRYPTO_MEMCMP(output, secret_1, input_output_length) != 0)
192 {
193 return(NX_CRYPTO_NOT_SUCCESSFUL);
194 }
195
196 if (crypto_method_3des -> nx_crypto_cleanup)
197 {
198 status = crypto_method_3des -> nx_crypto_cleanup(metadata);
199
200 if (status != NX_CRYPTO_SUCCESS)
201 {
202 return(status);
203 }
204 }
205
206 /* Decryption. */
207 if (crypto_method_3des -> nx_crypto_init)
208 {
209 status = crypto_method_3des -> nx_crypto_init(crypto_method_3des,
210 key_2,
211 NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
212 &handler,
213 metadata,
214 metadata_size);
215
216 if (status != NX_CRYPTO_SUCCESS)
217 {
218 return(status);
219 }
220 }
221
222 input_output_length = sizeof(secret_2);
223
224 status = crypto_method_3des -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
225 handler,
226 crypto_method_3des,
227 key_2,
228 NX_CRYPTO_3DES_KEY_LEN_IN_BITS,
229 secret_2,
230 input_output_length,
231 iv_2,
232 output,
233 input_output_length,
234 metadata,
235 metadata_size,
236 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
237
238 if (status != NX_CRYPTO_SUCCESS)
239 {
240 return(status);
241 }
242
243 if (NX_CRYPTO_MEMCMP(output, plain_2, input_output_length) != 0)
244 {
245 return(NX_CRYPTO_NOT_SUCCESSFUL);
246 }
247
248 if (crypto_method_3des -> nx_crypto_cleanup)
249 {
250 status = crypto_method_3des -> nx_crypto_cleanup(metadata);
251 }
252
253 return(status);
254 }
255 #endif
256