1 /* Test for aes ctr encrypting the plain text which is not multiples of 16. */
2
3 #include <stdio.h>
4 #include "nx_crypto_aes.h"
5 #include "nx_crypto_ccm.h"
6 #include "nx_crypto_ctr.h"
7 #include "nx_crypto_cbc.h"
8 #include "tls_test_utility.h"
9 #ifndef NX_CRYPTO_STANDALONE_ENABLE
10 #include "nx_secure_tls.h"
11 #endif
12 #include "nx_crypto_xcbc_mac.h"
13 #include "nx_crypto_method_self_test.h"
14
15 #define MAXIMUM_KEY_BITS 256
16
17 #include "nx_secure_aes_additional_test_data.c"
18
19 /* Define software AES method. */
20 static NX_CRYPTO_METHOD test_crypto_method_aes =
21 {
22 0, /* AES crypto algorithm filled at runtime */
23 0, /* Key size in bits */
24 NX_CRYPTO_AES_IV_LEN_IN_BITS, /* IV size in bits */
25 0, /* ICV size in bits, not used. */
26 NX_CRYPTO_AES_BLOCK_SIZE_IN_BITS >> 3, /* Block size in bytes. */
27 sizeof(NX_AES), /* Metadata size in bytes */
28 _nx_crypto_method_aes_init, /* AES initialization routine. */
29 NX_CRYPTO_NULL, /* AES cleanup routine, not used. */
30 _nx_crypto_method_aes_operation /* AES operation */
31
32 };
33
34 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_256;
35
36 /* AES context. */
37 static NX_AES aes_ctx;
38
39 /* Input to hold plain plus nonce. */
40 static ULONG key[(MAXIMUM_KEY_BITS >> 5) + 1];
41
42 /* IV. */
43 static ULONG iv[4];
44
45 /* Output. */
46 static ULONG encrypt_output[MAXIMUM_KEY_BITS >> 5];
47 static ULONG decrypt_output[MAXIMUM_KEY_BITS >> 5];
48
49 #ifndef NX_CRYPTO_STANDALONE_ENABLE
50 static TX_THREAD thread_0;
51 #endif
52
53 static VOID thread_0_entry(ULONG thread_input);
54
test_crypto_function(VOID * a,UCHAR * b,UCHAR * c,UINT d)55 static UINT test_crypto_function(VOID *a, UCHAR *b, UCHAR *c, UINT d)
56 {
57 return 0;
58 }
59
test_key_set_function(VOID * a,UCHAR * b,UINT c)60 static UINT test_key_set_function(VOID *a, UCHAR *b, UINT c)
61 {
62 return 0;
63 }
64
test_nx_crypto_init_failed(NX_CRYPTO_METHOD * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,VOID ** handler,VOID * crypto_metadata,ULONG crypto_metadata_size)65 static UINT test_nx_crypto_init_failed(NX_CRYPTO_METHOD *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, VOID **handler, VOID *crypto_metadata, ULONG crypto_metadata_size)
66 {
67 return 233;
68 }
69
70 static UINT count = 0;
test_nx_crypto_init_failed_second(NX_CRYPTO_METHOD * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,VOID ** handler,VOID * crypto_metadata,ULONG crypto_metadata_size)71 static UINT test_nx_crypto_init_failed_second(NX_CRYPTO_METHOD *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, VOID **handler, VOID *crypto_metadata, ULONG crypto_metadata_size)
72 {
73 count++;
74 if (count == 2)
75 return 233;
76 return 0;
77 }
78
test_nx_crypto_init_succeed(NX_CRYPTO_METHOD * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,VOID ** handler,VOID * crypto_metadata,ULONG crypto_metadata_size)79 static UINT test_nx_crypto_init_succeed(NX_CRYPTO_METHOD *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, VOID **handler, VOID *crypto_metadata, ULONG crypto_metadata_size)
80 {
81 return 0;
82 }
83
test_nx_crypto_operation_failed(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))84 static UINT test_nx_crypto_operation_failed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
85 {
86 return 233;
87 }
88
test_nx_crypto_operation_succeed(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))89 static UINT test_nx_crypto_operation_succeed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
90 {
91 return 0;
92 }
93
test_nx_crypto_cleanup_failed(VOID * crypto_metadata)94 static UINT test_nx_crypto_cleanup_failed(VOID *crypto_metadata)
95 {
96 return 233;
97 }
98
test_nx_crypto_operation_NX_CRYPTO_DECRYPT_failed(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))99 static UINT test_nx_crypto_operation_NX_CRYPTO_DECRYPT_failed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
100 {
101 if (op == NX_CRYPTO_DECRYPT)
102 {
103 return 233;
104 }
105
106 return _nx_crypto_method_aes_operation(op, handler, method, key, key_size_in_bits, input, input_length_in_byte, iv_ptr, output, output_length_in_byte, crypto_metadata, crypto_metadata_size, packet_ptr, nx_crypto_hw_process_callback);
107 }
108
test_nx_crypto_operation_NX_CRYPTO_DECRYPT_error(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))109 static UINT test_nx_crypto_operation_NX_CRYPTO_DECRYPT_error(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
110 {
111 if (op == NX_CRYPTO_DECRYPT)
112 {
113 return 0;
114 }
115
116 return _nx_crypto_method_aes_operation(op, handler, method, key, key_size_in_bits, input, input_length_in_byte, iv_ptr, output, output_length_in_byte, crypto_metadata, crypto_metadata_size, packet_ptr, nx_crypto_hw_process_callback);
117 }
118
119 #ifdef CTEST
120 void test_application_define(void *first_unused_memory);
test_application_define(void * first_unused_memory)121 void test_application_define(void *first_unused_memory)
122 #else
123 void nx_secure_aes_additional_test_application_define(void *first_unused_memory)
124 #endif
125 {
126 #ifndef NX_CRYPTO_STANDALONE_ENABLE
127 tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
128 first_unused_memory, 4096,
129 16, 16, 4, TX_AUTO_START);
130 #else
131 thread_0_entry(0);
132 #endif
133 }
134
thread_0_entry(ULONG thread_input)135 static VOID thread_0_entry(ULONG thread_input)
136 {
137 UINT i, status, backup;
138 UCHAR test_plain[17] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
139 UCHAR metadata[2048], input[1024], output[1024];
140 NX_CRYPTO_METHOD test_method;
141
142 /* Print out test information banner. */
143 printf("NetX Secure Test: AES ADDITIONAL TEST................................");
144
145 for (i = 0; i < sizeof(aes_data) / sizeof(AES_DATA); i++)
146 {
147
148 /* Encryption. */
149 memset(encrypt_output, 0xFF, sizeof(encrypt_output));
150 memset(decrypt_output, 0xFF, sizeof(decrypt_output));
151
152 /* Conjunction of key and nonce for CTR mode. */
153 /* It does not affect CBC mode. */
154 memcpy(key, aes_data[i].key, aes_data[i].key_len);
155 memcpy(key + (aes_data[i].key_len >> 2), aes_data[i].iv, 4);
156
157 if (aes_data[i].algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC)
158 {
159 memcpy(iv, aes_data[i].iv, sizeof(iv));
160 }
161 else
162 {
163 memcpy(iv, aes_data[i].iv + 4, 8);
164 }
165
166 /* Set crypto algorithm. */
167 test_crypto_method_aes.nx_crypto_algorithm = aes_data[i].algorithm;
168
169 test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes,
170 (UCHAR *)key,
171 (aes_data[i].key_len << 3),
172 NX_CRYPTO_NULL,
173 &aes_ctx,
174 sizeof(aes_ctx));
175
176 status = test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT,
177 NX_CRYPTO_NULL,
178 &test_crypto_method_aes,
179 (UCHAR *)key,
180 (aes_data[i].key_len << 3),
181 aes_data[i].plain,
182 aes_data[i].plain_len,
183 (UCHAR *)iv,
184 (UCHAR *)encrypt_output,
185 sizeof(encrypt_output),
186 &aes_ctx,
187 sizeof(aes_ctx),
188 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
189 if ( ( aes_data[i].plain_len % test_crypto_method_aes.nx_crypto_block_size_in_bytes) && ( aes_data[i].algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC))
190 {
191 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
192 }
193
194 /* Decryption. */
195 status = test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_DECRYPT,
196 NX_CRYPTO_NULL,
197 &test_crypto_method_aes,
198 (UCHAR *)key,
199 (aes_data[i].key_len << 3),
200 (UCHAR *)encrypt_output,
201 aes_data[i].plain_len,
202 (UCHAR *)iv,
203 (UCHAR *)decrypt_output,
204 sizeof(decrypt_output),
205 &aes_ctx,
206 sizeof(aes_ctx),
207 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
208 if ( ( aes_data[i].plain_len % test_crypto_method_aes.nx_crypto_block_size_in_bytes) && ( aes_data[i].algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC))
209 {
210 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
211 }
212 else
213 {
214 EXPECT_EQ( 0, memcmp( aes_data[i].plain, decrypt_output, aes_data[i].plain_len));
215 }
216
217 }
218
219 /* Specify an illegal algorithm type. */
220 i = 0;
221 test_crypto_method_aes.nx_crypto_algorithm = 0xFFFF;
222 status = test_crypto_method_aes.nx_crypto_operation(NX_CRYPTO_ENCRYPT,
223 NX_CRYPTO_NULL,
224 &test_crypto_method_aes,
225 (UCHAR *)key,
226 (aes_data[i].key_len << 3),
227 aes_data[i].plain,
228 aes_data[i].plain_len,
229 (UCHAR *)iv,
230 (UCHAR *)encrypt_output,
231 sizeof(encrypt_output),
232 &aes_ctx,
233 sizeof(aes_ctx),
234 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
235 EXPECT_EQ( status, NX_CRYPTO_INVALID_ALGORITHM);
236
237 /* Specify an illegal operation type in cbc mode. */
238 test_crypto_method_aes.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CBC;
239 status = test_crypto_method_aes.nx_crypto_operation(0xFFFF,
240 NX_CRYPTO_NULL,
241 &test_crypto_method_aes,
242 (UCHAR *)key,
243 (aes_data[i].key_len << 3),
244 aes_data[i].plain,
245 test_crypto_method_aes.nx_crypto_block_size_in_bytes,
246 (UCHAR *)iv,
247 (UCHAR *)encrypt_output,
248 sizeof(encrypt_output),
249 &aes_ctx,
250 sizeof(aes_ctx),
251 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
252 EXPECT_EQ( status, NX_CRYPTO_INVALID_ALGORITHM);
253
254 /* Invalid block_size. */
255 status = _nx_crypto_cbc_encrypt_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 17);
256 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
257 status = _nx_crypto_cbc_encrypt(NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, 17);
258 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
259
260 iv[0] = 0;
261
262 /* Invalid block_size. */
263 status = _nx_crypto_ccm_encrypt_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, 0, (UCHAR *)iv, 0, 1);
264 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
265 status = _nx_crypto_ccm_encrypt_update(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, 1);
266 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
267 status = _nx_crypto_ccm_encrypt_calculate(NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 1);
268 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
269 status = _nx_crypto_ccm_decrypt_calculate(NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 1);
270 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
271
272 /* Invalid iv_len. */
273 status = _nx_crypto_ctr_encrypt_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 7, NX_CRYPTO_NULL, 4);
274 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
275 status = _nx_crypto_ctr_encrypt_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 8, NX_CRYPTO_NULL, 3);
276 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
277
278 /* Invalid block_size. */
279 status = _nx_crypto_ctr_encrypt(NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, 1);
280 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
281
282 /* Unrecognized operations. */
283 test_crypto_method_aes.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CCM_8;
284 status = test_crypto_method_aes.nx_crypto_operation(0xFFFF,
285 NX_CRYPTO_NULL,
286 &test_crypto_method_aes,
287 (UCHAR *)key,
288 (aes_data[i].key_len << 3),
289 aes_data[i].plain,
290 test_crypto_method_aes.nx_crypto_block_size_in_bytes,
291 (UCHAR *)iv,
292 (UCHAR *)encrypt_output,
293 sizeof(encrypt_output),
294 &aes_ctx,
295 sizeof(aes_ctx),
296 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
297 EXPECT_EQ( status, NX_CRYPTO_INVALID_ALGORITHM);
298
299 #if 0
300 /* Authenication failed. */
301 status = _nx_crypto_ccm_authentication_check(metadata,
302 (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt,
303 NX_CRYPTO_NULL,
304 input, 16,
305 input, output, 16,
306 (UCHAR *)iv, 16, NX_CRYPTO_CCM_BLOCK_SIZE);
307 EXPECT_EQ( status, NX_CRYPTO_AUTHENTICATION_FAILED);
308
309 /* 0 byte additional data. */
310 status = _nx_crypto_ccm_authentication_check(metadata,
311 (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt,
312 NX_CRYPTO_NULL,
313 input, 0,
314 input, output, 16,
315 (UCHAR *)iv, 16, NX_CRYPTO_CCM_BLOCK_SIZE);
316 EXPECT_EQ( status, NX_CRYPTO_AUTHENTICATION_FAILED);
317
318 /* 0 byte plaintext to be encrypted. */
319 status = _nx_crypto_ccm_encrypt(metadata,
320 (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt,
321 NX_CRYPTO_NULL,
322 input, 0,
323 input, output, 0,
324 (UCHAR *)iv, 0, NX_CRYPTO_CCM_BLOCK_SIZE
325 );
326 EXPECT_EQ( status, NX_CRYPTO_SUCCESS);
327
328 /* the length of plaintext is not multiples of block size. */
329 status = _nx_crypto_ccm_encrypt(metadata,
330 (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt,
331 NX_CRYPTO_NULL,
332 input, 0,
333 input, output, 27,
334 (UCHAR *)iv, 0, NX_CRYPTO_CCM_BLOCK_SIZE
335 );
336 EXPECT_EQ( status, NX_CRYPTO_SUCCESS);
337 #endif
338
339 /* NULL crypto method pointer. */
340 status = test_crypto_method_aes.nx_crypto_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 128, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
341 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
342
343 /* NULL key pointer. */
344 status = test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes, NX_CRYPTO_NULL, 128, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
345 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
346
347 /* NULL metadata pointer. */
348 status = test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes, (UCHAR *)key, 128, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
349 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
350
351 /* Metadata is not 4-byte aligned. */
352 status = test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes, (UCHAR *)key, 128, NX_CRYPTO_NULL, (VOID *)0x03, 0);
353 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
354
355 /* Invalid metadata size. */
356 status = test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, (VOID *)0x04, 0);
357 EXPECT_EQ( status, NX_CRYPTO_PTR_ERROR);
358
359 /* Invalid key size. */
360 status = test_crypto_method_aes.nx_crypto_init(&test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, (VOID *)0x04, 6400);
361 EXPECT_EQ( status, NX_CRYPTO_UNSUPPORTED_KEY_SIZE);
362
363 /* Just cover it. */
364 status = _nx_crypto_method_aes_cleanup(NX_CRYPTO_NULL);
365 EXPECT_EQ(status, NX_CRYPTO_SUCCESS);
366
367 /* NULL method pointer. */
368 status = _nx_crypto_method_aes_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
369 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
370
371 /* NULL metadata pointer. */
372 status = _nx_crypto_method_aes_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
373 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
374
375 /* Metadata is not 4-byte aligned. */
376 status = _nx_crypto_method_aes_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x03, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
377 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
378
379 /* Invalid metadata size. */
380 status = _nx_crypto_method_aes_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x04, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
381 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
382
383 /* NULL method pointer. */
384 status = _nx_crypto_method_aes_cbc_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
385 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
386
387 /* NULL metadata pointer. */
388 status = _nx_crypto_method_aes_cbc_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
389 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
390
391 /* Metadata is not 4-byte aligned. */
392 status = _nx_crypto_method_aes_cbc_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x03, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
393 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
394
395 /* Invalid metadata size. */
396 status = _nx_crypto_method_aes_cbc_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x04, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
397 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
398
399 /* NULL method pointer. */
400 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
401 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
402
403 /* NULL metadata pointer. */
404 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
405 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
406
407 /* Metadata is not 4-byte aligned. */
408 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x03, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
409 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
410
411 /* Invalid metadata size. */
412 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x04, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
413 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
414
415 /* NULL method pointer. */
416 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
417 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
418
419 /* NULL key pointer. */
420 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
421 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
422
423 /* NULL metadata pointer. */
424 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
425 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
426
427 /* Metadata is not 4-byte aligned. */
428 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x03, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
429 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
430
431 /* Invalid metadata size. */
432 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, (VOID *)0x04, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
433 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
434
435 /* Invalid algorithm. */
436 test_crypto_method_aes.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CCM_8 - 1;
437 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, &aes_ctx, sizeof(NX_CRYPTO_AES), NX_CRYPTO_NULL, NX_CRYPTO_NULL);
438 EXPECT_EQ(status, NX_CRYPTO_INVALID_ALGORITHM);
439
440 test_crypto_method_aes.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CCM + 1;
441 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, &test_crypto_method_aes, (UCHAR *)key, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, &aes_ctx, sizeof(NX_CRYPTO_AES), NX_CRYPTO_NULL, NX_CRYPTO_NULL);
442 EXPECT_EQ(status, NX_CRYPTO_INVALID_ALGORITHM);
443
444 /* NULL method pointer. */
445 status = _nx_crypto_method_aes_xcbc_operation(0, NX_CRYPTO_NULL,
446 NX_CRYPTO_NULL, /* method */
447 NX_CRYPTO_NULL, 0, /* key */
448 NX_CRYPTO_NULL, 0, /* input */
449 NX_CRYPTO_NULL,
450 NX_CRYPTO_NULL, 0, /* output */
451 NX_CRYPTO_NULL, 0, /* metadata */
452 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
453 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
454
455 /* NULL key pointer. */
456 status = _nx_crypto_method_aes_xcbc_operation(0, NX_CRYPTO_NULL,
457 &test_crypto_method_aes, /* method */
458 NX_CRYPTO_NULL, 0, /* key */
459 NX_CRYPTO_NULL, 0, /* input */
460 NX_CRYPTO_NULL, /* iv */
461 NX_CRYPTO_NULL, 0, /* output */
462 NX_CRYPTO_NULL, 0, /* metadata */
463 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
464 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
465
466 /* NULL metadata pointer. */
467 status = _nx_crypto_method_aes_xcbc_operation(0, NX_CRYPTO_NULL,
468 &test_crypto_method_aes,
469 (UCHAR *)key, 0,
470 NX_CRYPTO_NULL, 0,
471 NX_CRYPTO_NULL,
472 NX_CRYPTO_NULL, 0,
473 NX_CRYPTO_NULL, 0, /* metadata */
474 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
475 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
476
477 /* Metadata is not 4-byte aligned. */
478 status = _nx_crypto_method_aes_xcbc_operation(0, NX_CRYPTO_NULL,
479 &test_crypto_method_aes,
480 (UCHAR *)key, 0,
481 NX_CRYPTO_NULL, 0,
482 NX_CRYPTO_NULL,
483 NX_CRYPTO_NULL, 0,
484 (VOID *)0x03, 0, /* metadata */
485 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
486 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
487
488 /* Invalid metadata size. */
489 status = _nx_crypto_method_aes_xcbc_operation(0, NX_CRYPTO_NULL,
490 &test_crypto_method_aes,
491 (UCHAR *)key, 0,
492 NX_CRYPTO_NULL, 0,
493 NX_CRYPTO_NULL,
494 NX_CRYPTO_NULL, 0,
495 (VOID *)0x04, 0,
496 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
497 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
498
499 /* Invalid metadata size. */
500 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL,
501 &test_crypto_method_aes,
502 (UCHAR *)key, 0,
503 NX_CRYPTO_NULL, 0,
504 NX_CRYPTO_NULL,
505 NX_CRYPTO_NULL, 0,
506 (VOID *)0x04, 0,
507 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
508 EXPECT_EQ(status, NX_CRYPTO_PTR_ERROR);
509
510 /* For NX_CRYPTO_STATE_CHECK. */
511 #ifdef NX_CRYPTO_SELF_TEST
512 backup = _nx_crypto_library_state;
513 _nx_crypto_library_state = 0;
514
515 status = _nx_crypto_method_aes_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
516 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
517
518 status = _nx_crypto_method_aes_cleanup(NX_CRYPTO_NULL);
519 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
520
521 status = _nx_crypto_method_aes_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
522 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
523
524 status = _nx_crypto_method_aes_cbc_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
525 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
526
527 status = _nx_crypto_method_aes_ccm_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
528 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
529
530 status = _nx_crypto_method_aes_ctr_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
531 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
532
533 status = _nx_crypto_method_aes_xcbc_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
534 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
535
536 _nx_crypto_library_state = backup;
537 #endif /* NX_CRYPTO_SELF_TEST */
538
539 /* Invoke xcbc-mac operation. */
540 test_crypto_method_aes.nx_crypto_algorithm = NX_CRYPTO_AUTHENTICATION_AES_XCBC_MAC_96;
541 status = _nx_crypto_method_aes_operation(0, NX_CRYPTO_NULL,
542 &test_crypto_method_aes,
543 key_cbc_128_1, sizeof(key_cbc_128_1), /* key */
544 test_plain, sizeof(test_plain), /* input */
545 iv_cbc_128_0,
546 output, sizeof(output),
547 metadata, sizeof(metadata),
548 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
549 EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
550
551 /* Invalid block size. */
552 status = _nx_crypto_xcbc_mac(NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, 0);
553 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
554
555 /* input_length_in_byte == block_size. */
556 status = _nx_crypto_xcbc_mac(metadata, test_crypto_function, test_key_set_function, NX_CRYPTO_NULL, 0, input, output, NX_CRYPTO_XCBC_MAC_BLOCK_SIZE, NX_CRYPTO_NULL, 0, NX_CRYPTO_XCBC_MAC_BLOCK_SIZE);
557 EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
558
559 #ifdef NX_CRYPTO_SELF_TEST
560 /* Tests for _nx_crypto_method_self_test_aes. */
561
562 /* NULL method pointer. */
563 status = _nx_crypto_method_self_test_aes(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
564 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
565
566 /* nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC nx_crypto_key_size_in_bits != 256 */
567 test_method.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CBC;
568 test_method.nx_crypto_key_size_in_bits = 0;
569 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
570 EXPECT_EQ(1, status);
571
572 /* nx_crypto_init failed. */
573 test_method.nx_crypto_init = test_nx_crypto_init_failed;
574 test_method.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CTR;
575 test_method.nx_crypto_key_size_in_bits = 256;
576 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
577 EXPECT_EQ(233, status);
578
579 /* nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CTR nx_crypto_key_size_in_bits != 256 */
580 test_method.nx_crypto_init = test_nx_crypto_init_failed;
581 test_method.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CTR;
582 test_method.nx_crypto_key_size_in_bits = 0;
583 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
584 EXPECT_EQ(1, status);
585
586 /* Invalid algorithm id. */
587 test_method.nx_crypto_algorithm = 0;
588 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
589 EXPECT_EQ(1, status);
590
591 /* nx_crypto_operation is NULL */
592 test_method.nx_crypto_algorithm = NX_CRYPTO_ENCRYPTION_AES_CTR;
593 test_method.nx_crypto_key_size_in_bits = 256;
594 test_method.nx_crypto_init = test_nx_crypto_init_succeed;
595 test_method.nx_crypto_operation = NX_CRYPTO_NULL;
596 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
597 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
598
599 /* nx_crypto_init and nx_crypto_operation are both NULL. */
600 test_method.nx_crypto_init = NX_CRYPTO_NULL;
601 test_method.nx_crypto_operation = NX_CRYPTO_NULL;
602 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
603 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
604
605 /* nx_crypto_operation failed. */
606 test_method.nx_crypto_operation = test_nx_crypto_operation_failed;
607 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
608 EXPECT_EQ(233, status);
609
610 /* NX_CRYPTO_MEMCMP failed. */
611 test_method.nx_crypto_operation = test_nx_crypto_operation_succeed;
612 status = _nx_crypto_method_self_test_aes(&test_method, NX_CRYPTO_NULL, 0);
613 EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);
614
615 /* nx_crypto_cleanup failed. */
616 test_method = crypto_method_aes_cbc_256;
617 test_method.nx_crypto_cleanup = test_nx_crypto_cleanup_failed;
618 status = _nx_crypto_method_self_test_aes(&test_method, &aes_ctx, sizeof(aes_ctx));
619 test_method.nx_crypto_cleanup = NX_CRYPTO_NULL;
620 EXPECT_EQ(233, status);
621
622 /* nx_crypto_init failed at the second times. */
623 test_method.nx_crypto_init = test_nx_crypto_init_failed_second;
624 status = _nx_crypto_method_self_test_aes(&test_method, &aes_ctx, sizeof(aes_ctx));
625 EXPECT_EQ(233, status);
626
627 /* nx_crypto_operation NX_CRYPTO_DECRYPT failed. */
628 test_method.nx_crypto_init = NX_CRYPTO_NULL;
629 test_method.nx_crypto_operation = test_nx_crypto_operation_NX_CRYPTO_DECRYPT_failed;
630 status = _nx_crypto_method_self_test_aes(&test_method, &aes_ctx, sizeof(aes_ctx));
631 EXPECT_EQ(233, status);
632
633 /* NX_CRYPTO_MEMCMP(output, plain_decrypt, xx) != 0. */
634 test_method.nx_crypto_operation = test_nx_crypto_operation_NX_CRYPTO_DECRYPT_error;
635 status = _nx_crypto_method_self_test_aes(&test_method, &aes_ctx, sizeof(aes_ctx));
636 EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);
637
638 /* nx_crypto_cleanup is NULL. */
639 test_method = crypto_method_aes_cbc_256;
640 test_method.nx_crypto_cleanup = NX_CRYPTO_NULL;
641 status = _nx_crypto_method_self_test_aes(&test_method, &aes_ctx, sizeof(aes_ctx));
642 EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
643
644 #endif /* NX_CRYPTO_SELF_TEST */
645
646 printf("SUCCESS!\n");
647 test_control_return(0);
648 }
649