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 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 { 76 for( i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE ) 77 { 78 TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i, 79 output + i ) == 0 ); 80 } 81 82 ASSERT_COMPARE( output, expected_output->len, 83 expected_output->x, expected_output->len ); 84 } 85 86exit: 87 mbedtls_aria_free( &ctx ); 88} 89/* END_CASE */ 90 91/* BEGIN_CASE */ 92void aria_decrypt_ecb( data_t *key_str, data_t *src_str, 93 data_t *expected_output, int setkey_result ) 94{ 95 unsigned char output[ARIA_MAX_DATASIZE]; 96 mbedtls_aria_context ctx; 97 size_t i; 98 99 memset( output, 0x00, sizeof( output ) ); 100 mbedtls_aria_init( &ctx ); 101 102 TEST_ASSERT( mbedtls_aria_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) 103 == setkey_result ); 104 if( setkey_result == 0 ) 105 { 106 for( i = 0; i < src_str->len; i += MBEDTLS_ARIA_BLOCKSIZE ) 107 { 108 TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, src_str->x + i, 109 output + i ) == 0 ); 110 } 111 112 ASSERT_COMPARE( output, expected_output->len, 113 expected_output->x, expected_output->len ); 114 } 115 116exit: 117 mbedtls_aria_free( &ctx ); 118} 119/* END_CASE */ 120 121/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 122void aria_encrypt_cbc( data_t *key_str, data_t *iv_str, 123 data_t *src_str, data_t *expected_output, 124 int cbc_result ) 125{ 126 unsigned char output[ARIA_MAX_DATASIZE]; 127 mbedtls_aria_context ctx; 128 129 memset( output, 0x00, sizeof( output ) ); 130 mbedtls_aria_init( &ctx ); 131 132 mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 133 TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_ENCRYPT, 134 src_str->len, iv_str->x, src_str->x, 135 output ) == cbc_result ); 136 if( cbc_result == 0 ) 137 { 138 ASSERT_COMPARE( output, expected_output->len, 139 expected_output->x, expected_output->len ); 140 } 141 142exit: 143 mbedtls_aria_free( &ctx ); 144} 145/* END_CASE */ 146 147/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 148void aria_decrypt_cbc( data_t *key_str, data_t *iv_str, 149 data_t *src_str, data_t *expected_output, 150 int cbc_result ) 151{ 152 unsigned char output[ARIA_MAX_DATASIZE]; 153 mbedtls_aria_context ctx; 154 155 memset( output, 0x00, sizeof( output ) ); 156 mbedtls_aria_init( &ctx ); 157 158 mbedtls_aria_setkey_dec( &ctx, key_str->x, key_str->len * 8 ); 159 TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_DECRYPT, 160 src_str->len, iv_str->x, src_str->x, 161 output ) == cbc_result ); 162 if( cbc_result == 0 ) 163 { 164 ASSERT_COMPARE( output, expected_output->len, 165 expected_output->x, expected_output->len ); 166 } 167 168exit: 169 mbedtls_aria_free( &ctx ); 170} 171/* END_CASE */ 172 173/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 174void aria_encrypt_cfb128( data_t *key_str, data_t *iv_str, 175 data_t *src_str, data_t *expected_output, 176 int result ) 177{ 178 unsigned char output[ARIA_MAX_DATASIZE]; 179 mbedtls_aria_context ctx; 180 size_t iv_offset = 0; 181 182 memset( output, 0x00, sizeof( output ) ); 183 mbedtls_aria_init( &ctx ); 184 185 mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 186 TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_ENCRYPT, 187 src_str->len, &iv_offset, 188 iv_str->x, src_str->x, output ) 189 == result ); 190 191 ASSERT_COMPARE( output, expected_output->len, 192 expected_output->x, expected_output->len ); 193 194exit: 195 mbedtls_aria_free( &ctx ); 196} 197/* END_CASE */ 198 199/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 200void aria_decrypt_cfb128( data_t *key_str, data_t *iv_str, 201 data_t *src_str, data_t *expected_output, 202 int result ) 203{ 204 unsigned char output[ARIA_MAX_DATASIZE]; 205 mbedtls_aria_context ctx; 206 size_t iv_offset = 0; 207 208 memset( output, 0x00, sizeof( output ) ); 209 mbedtls_aria_init( &ctx ); 210 211 mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 212 TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_DECRYPT, 213 src_str->len, &iv_offset, 214 iv_str->x, src_str->x, output ) 215 == result ); 216 217 ASSERT_COMPARE( output, expected_output->len, 218 expected_output->x, expected_output->len ); 219 220exit: 221 mbedtls_aria_free( &ctx ); 222} 223/* END_CASE */ 224 225/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ 226void aria_encrypt_ctr( data_t *key_str, data_t *iv_str, 227 data_t *src_str, data_t *expected_output, 228 int result ) 229{ 230 unsigned char output[ARIA_MAX_DATASIZE]; 231 unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE]; 232 mbedtls_aria_context ctx; 233 size_t iv_offset = 0; 234 235 memset( output, 0x00, sizeof( output ) ); 236 mbedtls_aria_init( &ctx ); 237 238 mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 239 TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset, 240 iv_str->x, blk, src_str->x, output ) 241 == result ); 242 243 ASSERT_COMPARE( output, expected_output->len, 244 expected_output->x, expected_output->len ); 245 246exit: 247 mbedtls_aria_free( &ctx ); 248} 249/* END_CASE */ 250 251/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ 252void aria_decrypt_ctr( data_t *key_str, data_t *iv_str, 253 data_t *src_str, data_t *expected_output, 254 int result ) 255{ 256 unsigned char output[ARIA_MAX_DATASIZE]; 257 unsigned char blk[MBEDTLS_ARIA_BLOCKSIZE]; 258 mbedtls_aria_context ctx; 259 size_t iv_offset = 0; 260 261 memset( output, 0x00, sizeof( output ) ); 262 mbedtls_aria_init( &ctx ); 263 264 mbedtls_aria_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 265 TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, src_str->len, &iv_offset, 266 iv_str->x, blk, src_str->x, output ) 267 == result ); 268 269 ASSERT_COMPARE( output, expected_output->len, 270 expected_output->x, expected_output->len ); 271 272exit: 273 mbedtls_aria_free( &ctx ); 274} 275/* END_CASE */ 276 277/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 278void aria_selftest() 279{ 280 TEST_ASSERT( mbedtls_aria_self_test( 1 ) == 0 ); 281} 282/* END_CASE */ 283