Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / suites / test_suite_aria.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/aria.h"
3
4 /* Maxium size of data used by test vectors
5  * WARNING: to be adapted if and when adding larger test cases */
6 #define ARIA_MAX_DATASIZE  160
7
8 /* Maximum sizes of hexified things */
9 #define ARIA_MAX_KEY_STR    ( 2 * MBEDTLS_ARIA_MAX_KEYSIZE + 1 )
10 #define ARIA_BLOCK_STR      ( 2 * MBEDTLS_ARIA_BLOCKSIZE + 1 )
11 #define ARIA_MAX_DATA_STR   ( 2 * ARIA_MAX_DATASIZE + 1 )
12 /* END_HEADER */
13
14 /* BEGIN_DEPENDENCIES
15  * depends_on:MBEDTLS_ARIA_C
16  * END_DEPENDENCIES
17  */
18
19 /* BEGIN_CASE */
20 void aria_valid_param( )
21 {
22     TEST_VALID_PARAM( mbedtls_aria_free( NULL ) );
23 }
24 /* END_CASE */
25
26 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
27 void aria_invalid_param( )
28 {
29     mbedtls_aria_context ctx;
30     unsigned char key[128 / 8] = { 0 };
31     unsigned char input[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
32     unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
33     unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
34     size_t iv_off = 0;
35
36     ((void) iv_off);
37     ((void) iv);
38
39     TEST_INVALID_PARAM( mbedtls_aria_init( NULL ) );
40
41     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
42                             mbedtls_aria_setkey_enc( NULL, key,
43                                                      sizeof( key ) ) );
44     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
45                             mbedtls_aria_setkey_enc( &ctx, NULL,
46                                                      sizeof( key ) ) );
47
48     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
49                             mbedtls_aria_setkey_dec( NULL, key,
50                                                      sizeof( key ) ) );
51     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
52                             mbedtls_aria_setkey_dec( &ctx, NULL,
53                                                      sizeof( key ) ) );
54
55     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
56                             mbedtls_aria_crypt_ecb( NULL, input, output ) );
57     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
58                             mbedtls_aria_crypt_ecb( &ctx, NULL, output ) );
59     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
60                             mbedtls_aria_crypt_ecb( &ctx, input, NULL ) );
61
62 #if defined(MBEDTLS_CIPHER_MODE_CBC)
63     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
64                             mbedtls_aria_crypt_cbc( NULL,
65                                                     MBEDTLS_ARIA_ENCRYPT,
66                                                     sizeof( input ),
67                                                     iv,
68                                                     input,
69                                                     output ) );
70     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
71                             mbedtls_aria_crypt_cbc( &ctx,
72                                                     42 /* invalid mode */,
73                                                     sizeof( input ),
74                                                     iv,
75                                                     input,
76                                                     output ) );
77     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
78                             mbedtls_aria_crypt_cbc( &ctx,
79                                                     MBEDTLS_ARIA_ENCRYPT,
80                                                     sizeof( input ),
81                                                     NULL,
82                                                     input,
83                                                     output ) );
84     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
85                             mbedtls_aria_crypt_cbc( &ctx,
86                                                     MBEDTLS_ARIA_ENCRYPT,
87                                                     sizeof( input ),
88                                                     iv,
89                                                     NULL,
90                                                     output ) );
91     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
92                             mbedtls_aria_crypt_cbc( &ctx,
93                                                     MBEDTLS_ARIA_ENCRYPT,
94                                                     sizeof( input ),
95                                                     iv,
96                                                     input,
97                                                     NULL ) );
98 #endif /* MBEDTLS_CIPHER_MODE_CBC */
99
100 #if defined(MBEDTLS_CIPHER_MODE_CFB)
101     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
102                             mbedtls_aria_crypt_cfb128( NULL,
103                                                     MBEDTLS_ARIA_ENCRYPT,
104                                                     sizeof( input ),
105                                                     &iv_off,
106                                                     iv,
107                                                     input,
108                                                     output ) );
109     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
110                             mbedtls_aria_crypt_cfb128( &ctx,
111                                                     42, /* invalid mode */
112                                                     sizeof( input ),
113                                                     &iv_off,
114                                                     iv,
115                                                     input,
116                                                     output ) );
117     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
118                             mbedtls_aria_crypt_cfb128( &ctx,
119                                                     MBEDTLS_ARIA_ENCRYPT,
120                                                     sizeof( input ),
121                                                     NULL,
122                                                     iv,
123                                                     input,
124                                                     output ) );
125     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
126                             mbedtls_aria_crypt_cfb128( &ctx,
127                                                     MBEDTLS_ARIA_ENCRYPT,
128                                                     sizeof( input ),
129                                                     &iv_off,
130                                                     NULL,
131                                                     input,
132                                                     output ) );
133     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
134                             mbedtls_aria_crypt_cfb128( &ctx,
135                                                     MBEDTLS_ARIA_ENCRYPT,
136                                                     sizeof( input ),
137                                                     &iv_off,
138                                                     iv,
139                                                     NULL,
140                                                     output ) );
141     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
142                             mbedtls_aria_crypt_cfb128( &ctx,
143                                                     MBEDTLS_ARIA_ENCRYPT,
144                                                     sizeof( input ),
145                                                     &iv_off,
146                                                     iv,
147                                                     input,
148                                                     NULL ) );
149 #endif /* MBEDTLS_CIPHER_MODE_CFB */
150
151 #if defined(MBEDTLS_CIPHER_MODE_CTR)
152     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
153                             mbedtls_aria_crypt_ctr( NULL,
154                                                     sizeof( input ),
155                                                     &iv_off,
156                                                     iv,
157                                                     iv,
158                                                     input,
159                                                     output ) );
160     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
161                             mbedtls_aria_crypt_ctr( &ctx,
162                                                     sizeof( input ),
163                                                     NULL,
164                                                     iv,
165                                                     iv,
166                                                     input,
167                                                     output ) );
168     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
169                             mbedtls_aria_crypt_ctr( &ctx,
170                                                     sizeof( input ),
171                                                     &iv_off,
172                                                     NULL,
173                                                     iv,
174                                                     input,
175                                                     output ) );
176     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
177                             mbedtls_aria_crypt_ctr( &ctx,
178                                                     sizeof( input ),
179                                                     &iv_off,
180                                                     iv,
181                                                     NULL,
182                                                     input,
183                                                     output ) );
184     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
185                             mbedtls_aria_crypt_ctr( &ctx,
186                                                     sizeof( input ),
187                                                     &iv_off,
188                                                     iv,
189                                                     iv,
190                                                     NULL,
191                                                     output ) );
192     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
193                             mbedtls_aria_crypt_ctr( &ctx,
194                                                     sizeof( input ),
195                                                     &iv_off,
196                                                     iv,
197                                                     iv,
198                                                     input,
199                                                     NULL ) );
200 #endif /* MBEDTLS_CIPHER_MODE_CTR */
201
202 exit:
203     return;
204
205 }
206 /* END_CASE */
207
208 /* BEGIN_CASE */
209 void aria_encrypt_ecb( char *hex_key_string, char *hex_src_string,
210                        char *hex_dst_string, int setkey_result )
211 {
212     unsigned char key_str[ARIA_MAX_KEY_STR];
213     unsigned char src_str[ARIA_MAX_DATA_STR];
214     unsigned char dst_str[ARIA_MAX_DATA_STR];
215     unsigned char output[ARIA_MAX_DATASIZE];
216     mbedtls_aria_context ctx;
217     int key_len, data_len, i;
218
219     memset( key_str, 0x00, sizeof( key_str ) );
220     memset( src_str, 0x00, sizeof( src_str ) );
221     memset( dst_str, 0x00, sizeof( dst_str ) );
222     memset( output, 0x00, sizeof( output ) );
223     mbedtls_aria_init( &ctx );
224
225     key_len = unhexify( key_str, hex_key_string );
226     data_len = unhexify( src_str, hex_src_string );
227
228     TEST_ASSERT( mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 )
229                  == setkey_result );
230     if( setkey_result == 0 )
231     {
232         for( i = 0; i < data_len; i += MBEDTLS_ARIA_BLOCKSIZE )
233         {
234             TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str + i, output + i )
235                                                  == 0 );
236         }
237         hexify( dst_str, output, data_len );
238
239         TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
240     }
241
242 exit:
243     mbedtls_aria_free( &ctx );
244 }
245 /* END_CASE */
246
247 /* BEGIN_CASE */
248 void aria_decrypt_ecb( char *hex_key_string, char *hex_src_string,
249                        char *hex_dst_string, int setkey_result )
250 {
251     unsigned char key_str[ARIA_MAX_KEY_STR];
252     unsigned char src_str[ARIA_MAX_DATA_STR];
253     unsigned char dst_str[ARIA_MAX_DATA_STR];
254     unsigned char output[ARIA_MAX_DATASIZE];
255     mbedtls_aria_context ctx;
256     int key_len, data_len, i;
257
258     memset( key_str, 0x00, sizeof( key_str ) );
259     memset( src_str, 0x00, sizeof( src_str ) );
260     memset( dst_str, 0x00, sizeof( dst_str ) );
261     memset( output, 0x00, sizeof( output ) );
262     mbedtls_aria_init( &ctx );
263
264     key_len = unhexify( key_str, hex_key_string );
265     data_len = unhexify( src_str, hex_src_string );
266
267     TEST_ASSERT( mbedtls_aria_setkey_dec( &ctx, key_str, key_len * 8 )
268                  == setkey_result );
269     if( setkey_result == 0 )
270     {
271         for( i = 0; i < data_len; i += MBEDTLS_ARIA_BLOCKSIZE )
272         {
273             TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str + i, output + i )
274                          == 0 );
275         }
276         hexify( dst_str, output, data_len );
277
278         TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
279     }
280
281 exit:
282     mbedtls_aria_free( &ctx );
283 }
284 /* END_CASE */
285
286 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
287 void aria_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
288                        char *hex_src_string, char *hex_dst_string,
289                        int cbc_result )
290 {
291     unsigned char key_str[ARIA_MAX_KEY_STR];
292     unsigned char iv_str[ARIA_BLOCK_STR];
293     unsigned char src_str[ARIA_MAX_DATA_STR];
294     unsigned char dst_str[ARIA_MAX_DATA_STR];
295     unsigned char output[ARIA_MAX_DATASIZE];
296     mbedtls_aria_context ctx;
297     int key_len, data_len;
298
299     memset( key_str, 0x00, sizeof( key_str ) );
300     memset( iv_str, 0x00, sizeof( iv_str ) );
301     memset( src_str, 0x00, sizeof( src_str ) );
302     memset( dst_str, 0x00, sizeof( dst_str ) );
303     memset( output, 0x00, sizeof( output ) );
304     mbedtls_aria_init( &ctx );
305
306     key_len = unhexify( key_str, hex_key_string );
307     unhexify( iv_str, hex_iv_string );
308     data_len = unhexify( src_str, hex_src_string );
309
310     mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 );
311     TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_ENCRYPT, data_len,
312                                          iv_str, src_str, output )
313                  == cbc_result );
314     if( cbc_result == 0 )
315     {
316         hexify( dst_str, output, data_len );
317
318         TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
319     }
320
321 exit:
322     mbedtls_aria_free( &ctx );
323 }
324 /* END_CASE */
325
326 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
327 void aria_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
328                        char *hex_src_string, char *hex_dst_string,
329                        int cbc_result )
330 {
331     unsigned char key_str[ARIA_MAX_KEY_STR];
332     unsigned char iv_str[ARIA_BLOCK_STR];
333     unsigned char src_str[ARIA_MAX_DATA_STR];
334     unsigned char dst_str[ARIA_MAX_DATA_STR];
335     unsigned char output[ARIA_MAX_DATASIZE];
336     mbedtls_aria_context ctx;
337     int key_len, data_len;
338
339     memset( key_str, 0x00, sizeof( key_str ) );
340     memset( iv_str, 0x00, sizeof( iv_str ) );
341     memset( src_str, 0x00, sizeof( src_str ) );
342     memset( dst_str, 0x00, sizeof( dst_str ) );
343     memset( output, 0x00, sizeof( output ) );
344     mbedtls_aria_init( &ctx );
345
346     key_len = unhexify( key_str, hex_key_string );
347     unhexify( iv_str, hex_iv_string );
348     data_len = unhexify( src_str, hex_src_string );
349
350     mbedtls_aria_setkey_dec( &ctx, key_str, key_len * 8 );
351     TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_DECRYPT, data_len,
352                                          iv_str, src_str, output )
353                  == cbc_result );
354     if( cbc_result == 0 )
355     {
356         hexify( dst_str, output, data_len );
357
358         TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
359     }
360
361 exit:
362     mbedtls_aria_free( &ctx );
363 }
364 /* END_CASE */
365
366 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
367 void aria_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
368                           char *hex_src_string, char *hex_dst_string,
369                           int result )
370 {
371     unsigned char key_str[ARIA_MAX_KEY_STR];
372     unsigned char iv_str[ARIA_BLOCK_STR];
373     unsigned char src_str[ARIA_MAX_DATA_STR];
374     unsigned char dst_str[ARIA_MAX_DATA_STR];
375     unsigned char output[ARIA_MAX_DATASIZE];
376     mbedtls_aria_context ctx;
377     size_t iv_offset = 0;
378     int key_len, data_len;
379
380     memset( key_str, 0x00, sizeof( key_str ) );
381     memset( iv_str, 0x00, sizeof( iv_str ) );
382     memset( src_str, 0x00, sizeof( src_str ) );
383     memset( dst_str, 0x00, sizeof( dst_str ) );
384     memset( output, 0x00, sizeof( output ) );
385     mbedtls_aria_init( &ctx );
386
387     key_len = unhexify( key_str, hex_key_string );
388     unhexify( iv_str, hex_iv_string );
389     data_len = unhexify( src_str, hex_src_string );
390
391     mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 );
392     TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_ENCRYPT,
393                                             data_len, &iv_offset, iv_str,
394                                             src_str, output )
395                  == result );
396     hexify( dst_str, output, data_len );
397
398     TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
399
400 exit:
401     mbedtls_aria_free( &ctx );
402 }
403 /* END_CASE */
404
405 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
406 void aria_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
407                           char *hex_src_string, char *hex_dst_string,
408                           int result  )
409 {
410     unsigned char key_str[ARIA_MAX_KEY_STR];
411     unsigned char iv_str[ARIA_BLOCK_STR];
412     unsigned char src_str[ARIA_MAX_DATA_STR];
413     unsigned char dst_str[ARIA_MAX_DATA_STR];
414     unsigned char output[ARIA_MAX_DATASIZE];
415     mbedtls_aria_context ctx;
416     size_t iv_offset = 0;
417     int key_len, data_len;
418
419     memset( key_str, 0x00, sizeof( key_str ) );
420     memset( iv_str, 0x00, sizeof( iv_str ) );
421     memset( src_str, 0x00, sizeof( src_str ) );
422     memset( dst_str, 0x00, sizeof( dst_str ) );
423     memset( output, 0x00, sizeof( output ) );
424     mbedtls_aria_init( &ctx );
425
426     key_len = unhexify( key_str, hex_key_string );
427     unhexify( iv_str, hex_iv_string );
428     data_len = unhexify( src_str, hex_src_string );
429
430     mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 );
431     TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_DECRYPT,
432                                             data_len, &iv_offset, iv_str,
433                                             src_str, output )
434                  == result );
435     hexify( dst_str, output, data_len );
436
437     TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
438
439 exit:
440     mbedtls_aria_free( &ctx );
441 }
442 /* END_CASE */
443
444 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
445 void aria_encrypt_ctr( char *hex_key_string, char *hex_iv_string,
446                        char *hex_src_string, char *hex_dst_string,
447                        int result )
448 {
449     unsigned char key_str[ARIA_MAX_KEY_STR];
450     unsigned char iv_str[ARIA_BLOCK_STR];
451     unsigned char src_str[ARIA_MAX_DATA_STR];
452     unsigned char dst_str[ARIA_MAX_DATA_STR];
453     unsigned char output[ARIA_MAX_DATASIZE];
454     unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
455     mbedtls_aria_context ctx;
456     size_t iv_offset = 0;
457     int key_len, data_len;
458
459     memset( key_str, 0x00, sizeof( key_str ) );
460     memset( iv_str, 0x00, sizeof( iv_str ) );
461     memset( src_str, 0x00, sizeof( src_str ) );
462     memset( dst_str, 0x00, sizeof( dst_str ) );
463     memset( output, 0x00, sizeof( output ) );
464     mbedtls_aria_init( &ctx );
465
466     key_len = unhexify( key_str, hex_key_string );
467     unhexify( iv_str, hex_iv_string );
468     data_len = unhexify( src_str, hex_src_string );
469
470     mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 );
471     TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, data_len, &iv_offset, iv_str,
472                                          blk, src_str, output )
473                  == result );
474     hexify( dst_str, output, data_len );
475
476     TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
477
478 exit:
479     mbedtls_aria_free( &ctx );
480 }
481 /* END_CASE */
482
483 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
484 void aria_decrypt_ctr( char *hex_key_string, char *hex_iv_string,
485                        char *hex_src_string, char *hex_dst_string,
486                        int result )
487 {
488     unsigned char key_str[ARIA_MAX_KEY_STR];
489     unsigned char iv_str[ARIA_BLOCK_STR];
490     unsigned char src_str[ARIA_MAX_DATA_STR];
491     unsigned char dst_str[ARIA_MAX_DATA_STR];
492     unsigned char output[ARIA_MAX_DATASIZE];
493     unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
494     mbedtls_aria_context ctx;
495     size_t iv_offset = 0;
496     int key_len, data_len;
497
498     memset( key_str, 0x00, sizeof( key_str ) );
499     memset( iv_str, 0x00, sizeof( iv_str ) );
500     memset( src_str, 0x00, sizeof( src_str ) );
501     memset( dst_str, 0x00, sizeof( dst_str ) );
502     memset( output, 0x00, sizeof( output ) );
503     mbedtls_aria_init( &ctx );
504
505     key_len = unhexify( key_str, hex_key_string );
506     unhexify( iv_str, hex_iv_string );
507     data_len = unhexify( src_str, hex_src_string );
508
509     mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 );
510     TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, data_len, &iv_offset, iv_str,
511                                          blk, src_str, output )
512                  == result );
513     hexify( dst_str, output, data_len );
514
515     TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
516
517 exit:
518     mbedtls_aria_free( &ctx );
519 }
520 /* END_CASE */
521
522 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
523 void aria_selftest()
524 {
525     TEST_ASSERT( mbedtls_aria_self_test( 1 ) == 0 );
526 }
527 /* END_CASE */