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