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 */
20void 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 */
27void 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
202exit:
203    return;
204
205}
206/* END_CASE */
207
208/* BEGIN_CASE */
209void aria_encrypt_ecb( data_t *key_str, data_t *src_str,
210                       data_t *expected_output, int setkey_result )
211{
212    unsigned char output[ARIA_MAX_DATASIZE];
213    mbedtls_aria_context ctx;
214    size_t i;
215
216    memset( output, 0x00, sizeof( output ) );
217    mbedtls_aria_init( &ctx );
218
219    TEST_ASSERT( mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 )
220                 == setkey_result );
221    if( setkey_result == 0 )
222    {
223        for( i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE )
224        {
225            TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i,
226                                                 output + i ) == 0 );
227        }
228
229        ASSERT_COMPARE( output, expected_output->len,
230                        expected_output->x, expected_output->len );
231    }
232
233exit:
234    mbedtls_aria_free( &ctx );
235}
236/* END_CASE */
237
238/* BEGIN_CASE */
239void aria_decrypt_ecb( data_t *key_str, data_t *src_str,
240                       data_t *expected_output, int setkey_result )
241{
242    unsigned char output[ARIA_MAX_DATASIZE];
243    mbedtls_aria_context ctx;
244    size_t i;
245
246    memset( output, 0x00, sizeof( output ) );
247    mbedtls_aria_init( &ctx );
248
249    TEST_ASSERT( mbedtls_aria_setkey_dec( &ctx, key_str->x, key_str->len * 8 )
250                 == setkey_result );
251    if( setkey_result == 0 )
252    {
253        for( i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE )
254        {
255            TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i,
256                                                 output + i ) == 0 );
257        }
258
259        ASSERT_COMPARE( output, expected_output->len,
260                        expected_output->x, expected_output->len );
261    }
262
263exit:
264    mbedtls_aria_free( &ctx );
265}
266/* END_CASE */
267
268/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
269void aria_encrypt_cbc( data_t *key_str, data_t *iv_str,
270                       data_t *src_str, data_t *expected_output,
271                       int cbc_result )
272{
273    unsigned char output[ARIA_MAX_DATASIZE];
274    mbedtls_aria_context ctx;
275
276    memset( output, 0x00, sizeof( output ) );
277    mbedtls_aria_init( &ctx );
278
279    mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
280    TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_ENCRYPT,
281                                         src_str->len, iv_str->x, src_str->x,
282                                         output ) == cbc_result );
283    if( cbc_result == 0 )
284    {
285        ASSERT_COMPARE( output, expected_output->len,
286                        expected_output->x, expected_output->len );
287    }
288
289exit:
290    mbedtls_aria_free( &ctx );
291}
292/* END_CASE */
293
294/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
295void aria_decrypt_cbc( data_t *key_str, data_t *iv_str,
296                       data_t *src_str, data_t *expected_output,
297                       int cbc_result )
298{
299    unsigned char output[ARIA_MAX_DATASIZE];
300    mbedtls_aria_context ctx;
301
302    memset( output, 0x00, sizeof( output ) );
303    mbedtls_aria_init( &ctx );
304
305    mbedtls_aria_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
306    TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_DECRYPT,
307                                         src_str->len, iv_str->x, src_str->x,
308                                         output ) == cbc_result );
309    if( cbc_result == 0 )
310    {
311        ASSERT_COMPARE( output, expected_output->len,
312                        expected_output->x, expected_output->len );
313    }
314
315exit:
316    mbedtls_aria_free( &ctx );
317}
318/* END_CASE */
319
320/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
321void aria_encrypt_cfb128( data_t *key_str, data_t *iv_str,
322                          data_t *src_str, data_t *expected_output,
323                          int result )
324{
325    unsigned char output[ARIA_MAX_DATASIZE];
326    mbedtls_aria_context ctx;
327    size_t iv_offset = 0;
328
329    memset( output, 0x00, sizeof( output ) );
330    mbedtls_aria_init( &ctx );
331
332    mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
333    TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_ENCRYPT,
334                                            src_str->len, &iv_offset,
335                                            iv_str->x, src_str->x, output )
336                 == result );
337
338    ASSERT_COMPARE( output, expected_output->len,
339                    expected_output->x, expected_output->len );
340
341exit:
342    mbedtls_aria_free( &ctx );
343}
344/* END_CASE */
345
346/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
347void aria_decrypt_cfb128( data_t *key_str, data_t *iv_str,
348                          data_t *src_str, data_t *expected_output,
349                          int result  )
350{
351    unsigned char output[ARIA_MAX_DATASIZE];
352    mbedtls_aria_context ctx;
353    size_t iv_offset = 0;
354
355    memset( output, 0x00, sizeof( output ) );
356    mbedtls_aria_init( &ctx );
357
358    mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
359    TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_DECRYPT,
360                                            src_str->len, &iv_offset,
361                                            iv_str->x, src_str->x, output )
362                 == result );
363
364    ASSERT_COMPARE( output, expected_output->len,
365                    expected_output->x, expected_output->len );
366
367exit:
368    mbedtls_aria_free( &ctx );
369}
370/* END_CASE */
371
372/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
373void aria_encrypt_ctr( data_t *key_str, data_t *iv_str,
374                       data_t *src_str, data_t *expected_output,
375                       int result )
376{
377    unsigned char output[ARIA_MAX_DATASIZE];
378    unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
379    mbedtls_aria_context ctx;
380    size_t iv_offset = 0;
381
382    memset( output, 0x00, sizeof( output ) );
383    mbedtls_aria_init( &ctx );
384
385    mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
386    TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset,
387                                         iv_str->x, blk, src_str->x, output )
388                 == result );
389
390    ASSERT_COMPARE( output, expected_output->len,
391                    expected_output->x, expected_output->len );
392
393exit:
394    mbedtls_aria_free( &ctx );
395}
396/* END_CASE */
397
398/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
399void aria_decrypt_ctr( data_t *key_str, data_t *iv_str,
400                       data_t *src_str, data_t *expected_output,
401                       int result )
402{
403    unsigned char output[ARIA_MAX_DATASIZE];
404    unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
405    mbedtls_aria_context ctx;
406    size_t iv_offset = 0;
407
408    memset( output, 0x00, sizeof( output ) );
409    mbedtls_aria_init( &ctx );
410
411    mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
412    TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset,
413                                         iv_str->x, blk, src_str->x, output )
414                 == result );
415
416    ASSERT_COMPARE( output, expected_output->len,
417                    expected_output->x, expected_output->len );
418
419exit:
420    mbedtls_aria_free( &ctx );
421}
422/* END_CASE */
423
424/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
425void aria_selftest()
426{
427    TEST_ASSERT( mbedtls_aria_self_test( 1 ) == 0 );
428}
429/* END_CASE */
430