1/* BEGIN_HEADER */
2#include "mbedtls/aria.h"
3
4/* Maximum 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 depends_on:NOT_DEFINED */
20void aria_invalid_param()
21{
22    mbedtls_aria_context ctx;
23    unsigned char input[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
24    unsigned char output[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
25    unsigned char iv[MBEDTLS_ARIA_BLOCKSIZE] = { 0 };
26    size_t iv_off = 0;
27
28    ((void) iv_off);
29    ((void) iv);
30    ((void) ctx);
31    ((void) input);
32    ((void) output);
33
34#if defined(MBEDTLS_CIPHER_MODE_CBC)
35    TEST_EQUAL(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
36               mbedtls_aria_crypt_cbc(&ctx,
37                                      42 /* invalid mode */,
38                                      sizeof(input),
39                                      iv,
40                                      input,
41                                      output));
42#endif /* MBEDTLS_CIPHER_MODE_CBC */
43
44#if defined(MBEDTLS_CIPHER_MODE_CFB)
45    TEST_EQUAL(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA,
46               mbedtls_aria_crypt_cfb128(&ctx,
47                                         42,            /* invalid mode */
48                                         sizeof(input),
49                                         &iv_off,
50                                         iv,
51                                         input,
52                                         output));
53#endif /* MBEDTLS_CIPHER_MODE_CFB */
54
55exit:
56    return;
57
58}
59/* END_CASE */
60
61/* BEGIN_CASE */
62void aria_encrypt_ecb(data_t *key_str, data_t *src_str,
63                      data_t *expected_output, int setkey_result)
64{
65    unsigned char output[ARIA_MAX_DATASIZE];
66    mbedtls_aria_context ctx;
67    size_t i;
68
69    memset(output, 0x00, sizeof(output));
70    mbedtls_aria_init(&ctx);
71
72    TEST_ASSERT(mbedtls_aria_setkey_enc(&ctx, key_str->x, key_str->len * 8)
73                == setkey_result);
74    if (setkey_result == 0) {
75        for (i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE) {
76            TEST_ASSERT(mbedtls_aria_crypt_ecb(&ctx, src_str->x + i,
77                                               output + i) == 0);
78        }
79
80        TEST_MEMORY_COMPARE(output, expected_output->len,
81                            expected_output->x, expected_output->len);
82    }
83
84exit:
85    mbedtls_aria_free(&ctx);
86}
87/* END_CASE */
88
89/* BEGIN_CASE */
90void aria_decrypt_ecb(data_t *key_str, data_t *src_str,
91                      data_t *expected_output, int setkey_result)
92{
93    unsigned char output[ARIA_MAX_DATASIZE];
94    mbedtls_aria_context ctx;
95    size_t i;
96
97    memset(output, 0x00, sizeof(output));
98    mbedtls_aria_init(&ctx);
99
100    TEST_ASSERT(mbedtls_aria_setkey_dec(&ctx, key_str->x, key_str->len * 8)
101                == setkey_result);
102    if (setkey_result == 0) {
103        for (i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE) {
104            TEST_ASSERT(mbedtls_aria_crypt_ecb(&ctx, src_str->x + i,
105                                               output + i) == 0);
106        }
107
108        TEST_MEMORY_COMPARE(output, expected_output->len,
109                            expected_output->x, expected_output->len);
110    }
111
112exit:
113    mbedtls_aria_free(&ctx);
114}
115/* END_CASE */
116
117/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
118void aria_encrypt_cbc(data_t *key_str, data_t *iv_str,
119                      data_t *src_str, data_t *expected_output,
120                      int cbc_result)
121{
122    unsigned char output[ARIA_MAX_DATASIZE];
123    mbedtls_aria_context ctx;
124
125    memset(output, 0x00, sizeof(output));
126    mbedtls_aria_init(&ctx);
127
128    mbedtls_aria_setkey_enc(&ctx, key_str->x, key_str->len * 8);
129    TEST_ASSERT(mbedtls_aria_crypt_cbc(&ctx, MBEDTLS_ARIA_ENCRYPT,
130                                       src_str->len, iv_str->x, src_str->x,
131                                       output) == cbc_result);
132    if (cbc_result == 0) {
133        TEST_MEMORY_COMPARE(output, expected_output->len,
134                            expected_output->x, expected_output->len);
135    }
136
137exit:
138    mbedtls_aria_free(&ctx);
139}
140/* END_CASE */
141
142/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
143void aria_decrypt_cbc(data_t *key_str, data_t *iv_str,
144                      data_t *src_str, data_t *expected_output,
145                      int cbc_result)
146{
147    unsigned char output[ARIA_MAX_DATASIZE];
148    mbedtls_aria_context ctx;
149
150    memset(output, 0x00, sizeof(output));
151    mbedtls_aria_init(&ctx);
152
153    mbedtls_aria_setkey_dec(&ctx, key_str->x, key_str->len * 8);
154    TEST_ASSERT(mbedtls_aria_crypt_cbc(&ctx, MBEDTLS_ARIA_DECRYPT,
155                                       src_str->len, iv_str->x, src_str->x,
156                                       output) == cbc_result);
157    if (cbc_result == 0) {
158        TEST_MEMORY_COMPARE(output, expected_output->len,
159                            expected_output->x, expected_output->len);
160    }
161
162exit:
163    mbedtls_aria_free(&ctx);
164}
165/* END_CASE */
166
167/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
168void aria_encrypt_cfb128(data_t *key_str, data_t *iv_str,
169                         data_t *src_str, data_t *expected_output,
170                         int result)
171{
172    unsigned char output[ARIA_MAX_DATASIZE];
173    mbedtls_aria_context ctx;
174    size_t iv_offset = 0;
175
176    memset(output, 0x00, sizeof(output));
177    mbedtls_aria_init(&ctx);
178
179    mbedtls_aria_setkey_enc(&ctx, key_str->x, key_str->len * 8);
180    TEST_ASSERT(mbedtls_aria_crypt_cfb128(&ctx, MBEDTLS_ARIA_ENCRYPT,
181                                          src_str->len, &iv_offset,
182                                          iv_str->x, src_str->x, output)
183                == result);
184
185    TEST_MEMORY_COMPARE(output, expected_output->len,
186                        expected_output->x, expected_output->len);
187
188exit:
189    mbedtls_aria_free(&ctx);
190}
191/* END_CASE */
192
193/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
194void aria_decrypt_cfb128(data_t *key_str, data_t *iv_str,
195                         data_t *src_str, data_t *expected_output,
196                         int result)
197{
198    unsigned char output[ARIA_MAX_DATASIZE];
199    mbedtls_aria_context ctx;
200    size_t iv_offset = 0;
201
202    memset(output, 0x00, sizeof(output));
203    mbedtls_aria_init(&ctx);
204
205    mbedtls_aria_setkey_enc(&ctx, key_str->x, key_str->len * 8);
206    TEST_ASSERT(mbedtls_aria_crypt_cfb128(&ctx, MBEDTLS_ARIA_DECRYPT,
207                                          src_str->len, &iv_offset,
208                                          iv_str->x, src_str->x, output)
209                == result);
210
211    TEST_MEMORY_COMPARE(output, expected_output->len,
212                        expected_output->x, expected_output->len);
213
214exit:
215    mbedtls_aria_free(&ctx);
216}
217/* END_CASE */
218
219/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
220void aria_encrypt_ctr(data_t *key_str, data_t *iv_str,
221                      data_t *src_str, data_t *expected_output,
222                      int result)
223{
224    unsigned char output[ARIA_MAX_DATASIZE];
225    unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
226    mbedtls_aria_context ctx;
227    size_t iv_offset = 0;
228
229    memset(output, 0x00, sizeof(output));
230    mbedtls_aria_init(&ctx);
231
232    mbedtls_aria_setkey_enc(&ctx, key_str->x, key_str->len * 8);
233    TEST_ASSERT(mbedtls_aria_crypt_ctr(&ctx, src_str->len, &iv_offset,
234                                       iv_str->x, blk, src_str->x, output)
235                == result);
236
237    TEST_MEMORY_COMPARE(output, expected_output->len,
238                        expected_output->x, expected_output->len);
239
240exit:
241    mbedtls_aria_free(&ctx);
242}
243/* END_CASE */
244
245/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */
246void aria_decrypt_ctr(data_t *key_str, data_t *iv_str,
247                      data_t *src_str, data_t *expected_output,
248                      int result)
249{
250    unsigned char output[ARIA_MAX_DATASIZE];
251    unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE];
252    mbedtls_aria_context ctx;
253    size_t iv_offset = 0;
254
255    memset(output, 0x00, sizeof(output));
256    mbedtls_aria_init(&ctx);
257
258    mbedtls_aria_setkey_enc(&ctx, key_str->x, key_str->len * 8);
259    TEST_ASSERT(mbedtls_aria_crypt_ctr(&ctx, src_str->len, &iv_offset,
260                                       iv_str->x, blk, src_str->x, output)
261                == result);
262
263    TEST_MEMORY_COMPARE(output, expected_output->len,
264                        expected_output->x, expected_output->len);
265
266exit:
267    mbedtls_aria_free(&ctx);
268}
269/* END_CASE */
270
271/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
272void aria_selftest()
273{
274    TEST_ASSERT(mbedtls_aria_self_test(1) == 0);
275}
276/* END_CASE */
277