Markku-Juhani O. Saarinen | 8df81e0 | 2017-12-01 14:26:40 +0000 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/aria.h" |
| 3 | /* END_HEADER */ |
| 4 | |
| 5 | /* BEGIN_DEPENDENCIES |
| 6 | * depends_on:MBEDTLS_ARIA_C |
| 7 | * END_DEPENDENCIES |
| 8 | */ |
| 9 | |
| 10 | /* BEGIN_CASE */ |
| 11 | void aria_encrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 12 | char *hex_dst_string, int setkey_result ) |
| 13 | { |
| 14 | unsigned char key_str[1000]; |
| 15 | unsigned char src_str[1000]; |
| 16 | unsigned char dst_str[1000]; |
| 17 | unsigned char output[1000]; |
| 18 | mbedtls_aria_context ctx; |
| 19 | int key_len, data_len, i; |
| 20 | |
| 21 | memset( key_str, 0x00, 1000 ); |
| 22 | memset( src_str, 0x00, 1000 ); |
| 23 | memset( dst_str, 0x00, 1000 ); |
| 24 | memset( output, 0x00, 1000 ); |
| 25 | mbedtls_aria_init( &ctx ); |
| 26 | |
| 27 | key_len = unhexify( key_str, hex_key_string ); |
| 28 | data_len = unhexify( src_str, hex_src_string ); |
| 29 | |
| 30 | TEST_ASSERT( mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result ); |
| 31 | if( setkey_result == 0 ) |
| 32 | { |
| 33 | for( i = 0; i < data_len; i += 16 ) |
| 34 | { |
| 35 | TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, MBEDTLS_ARIA_ENCRYPT, src_str + i, output + i ) == 0 ); |
| 36 | } |
| 37 | hexify( dst_str, output, data_len ); |
| 38 | |
| 39 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 40 | } |
| 41 | |
| 42 | exit: |
| 43 | mbedtls_aria_free( &ctx ); |
| 44 | } |
| 45 | /* END_CASE */ |
| 46 | |
| 47 | /* BEGIN_CASE */ |
| 48 | void aria_decrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 49 | char *hex_dst_string, int setkey_result ) |
| 50 | { |
| 51 | unsigned char key_str[1000]; |
| 52 | unsigned char src_str[1000]; |
| 53 | unsigned char dst_str[1000]; |
| 54 | unsigned char output[1000]; |
| 55 | mbedtls_aria_context ctx; |
| 56 | int key_len, data_len, i; |
| 57 | |
| 58 | memset( key_str, 0x00, 1000 ); |
| 59 | memset( src_str, 0x00, 1000 ); |
| 60 | memset( dst_str, 0x00, 1000 ); |
| 61 | memset( output, 0x00, 1000 ); |
| 62 | mbedtls_aria_init( &ctx ); |
| 63 | |
| 64 | key_len = unhexify( key_str, hex_key_string ); |
| 65 | data_len = unhexify( src_str, hex_src_string ); |
| 66 | |
| 67 | TEST_ASSERT( mbedtls_aria_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result ); |
| 68 | if( setkey_result == 0 ) |
| 69 | { |
| 70 | for( i = 0; i < data_len; i += 16 ) |
| 71 | { |
| 72 | TEST_ASSERT( mbedtls_aria_crypt_ecb( &ctx, MBEDTLS_ARIA_DECRYPT, |
| 73 | src_str + i, output + i ) == 0 ); |
| 74 | } |
| 75 | hexify( dst_str, output, data_len ); |
| 76 | |
| 77 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 78 | } |
| 79 | |
| 80 | exit: |
| 81 | mbedtls_aria_free( &ctx ); |
| 82 | } |
| 83 | /* END_CASE */ |
| 84 | |
| 85 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
| 86 | void aria_encrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 87 | char *hex_src_string, char *hex_dst_string, |
| 88 | int cbc_result ) |
| 89 | { |
| 90 | unsigned char key_str[1000]; |
| 91 | unsigned char iv_str[1000]; |
| 92 | unsigned char src_str[1000]; |
| 93 | unsigned char dst_str[1000]; |
| 94 | unsigned char output[1000]; |
| 95 | mbedtls_aria_context ctx; |
| 96 | int key_len, data_len; |
| 97 | |
| 98 | memset( key_str, 0x00, 1000 ); |
| 99 | memset( iv_str, 0x00, 1000 ); |
| 100 | memset( src_str, 0x00, 1000 ); |
| 101 | memset( dst_str, 0x00, 1000 ); |
| 102 | memset( output, 0x00, 1000 ); |
| 103 | mbedtls_aria_init( &ctx ); |
| 104 | |
| 105 | key_len = unhexify( key_str, hex_key_string ); |
| 106 | unhexify( iv_str, hex_iv_string ); |
| 107 | data_len = unhexify( src_str, hex_src_string ); |
| 108 | |
| 109 | mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 110 | TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_ENCRYPT, |
| 111 | data_len, iv_str, src_str, output) == cbc_result ); |
| 112 | if( cbc_result == 0 ) |
| 113 | { |
| 114 | hexify( dst_str, output, data_len ); |
| 115 | |
| 116 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 117 | } |
| 118 | |
| 119 | exit: |
| 120 | mbedtls_aria_free( &ctx ); |
| 121 | } |
| 122 | /* END_CASE */ |
| 123 | |
| 124 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
| 125 | void aria_decrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 126 | char *hex_src_string, char *hex_dst_string, |
| 127 | int cbc_result ) |
| 128 | { |
| 129 | unsigned char key_str[1000]; |
| 130 | unsigned char iv_str[1000]; |
| 131 | unsigned char src_str[1000]; |
| 132 | unsigned char dst_str[1000]; |
| 133 | unsigned char output[1000]; |
| 134 | mbedtls_aria_context ctx; |
| 135 | int key_len, data_len; |
| 136 | |
| 137 | memset( key_str, 0x00, 1000 ); |
| 138 | memset( iv_str, 0x00, 1000 ); |
| 139 | memset( src_str, 0x00, 1000 ); |
| 140 | memset( dst_str, 0x00, 1000 ); |
| 141 | memset( output, 0x00, 1000 ); |
| 142 | mbedtls_aria_init( &ctx ); |
| 143 | |
| 144 | key_len = unhexify( key_str, hex_key_string ); |
| 145 | unhexify( iv_str, hex_iv_string ); |
| 146 | data_len = unhexify( src_str, hex_src_string ); |
| 147 | |
| 148 | mbedtls_aria_setkey_dec( &ctx, key_str, key_len * 8 ); |
| 149 | TEST_ASSERT( mbedtls_aria_crypt_cbc( &ctx, MBEDTLS_ARIA_DECRYPT, |
| 150 | data_len, iv_str, src_str, output ) == cbc_result ); |
| 151 | if( cbc_result == 0 ) |
| 152 | { |
| 153 | hexify( dst_str, output, data_len ); |
| 154 | |
| 155 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 156 | } |
| 157 | |
| 158 | exit: |
| 159 | mbedtls_aria_free( &ctx ); |
| 160 | } |
| 161 | /* END_CASE */ |
| 162 | |
| 163 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
| 164 | void aria_encrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 165 | char *hex_src_string, char *hex_dst_string, |
| 166 | int result ) |
| 167 | { |
| 168 | unsigned char key_str[1000]; |
| 169 | unsigned char iv_str[1000]; |
| 170 | unsigned char src_str[1000]; |
| 171 | unsigned char dst_str[1000]; |
| 172 | unsigned char output[1000]; |
| 173 | mbedtls_aria_context ctx; |
| 174 | size_t iv_offset = 0; |
| 175 | int key_len, data_len; |
| 176 | |
| 177 | memset( key_str, 0x00, 1000 ); |
| 178 | memset( iv_str, 0x00, 1000 ); |
| 179 | memset( src_str, 0x00, 1000 ); |
| 180 | memset( dst_str, 0x00, 1000 ); |
| 181 | memset( output, 0x00, 1000 ); |
| 182 | mbedtls_aria_init( &ctx ); |
| 183 | |
| 184 | key_len = unhexify( key_str, hex_key_string ); |
| 185 | unhexify( iv_str, hex_iv_string ); |
| 186 | data_len = unhexify( src_str, hex_src_string ); |
| 187 | |
| 188 | mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 189 | TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_ENCRYPT, |
| 190 | data_len, &iv_offset, iv_str, src_str, output ) == result ); |
| 191 | hexify( dst_str, output, data_len ); |
| 192 | |
| 193 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 194 | |
| 195 | exit: |
| 196 | mbedtls_aria_free( &ctx ); |
| 197 | } |
| 198 | /* END_CASE */ |
| 199 | |
| 200 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
| 201 | void aria_decrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 202 | char *hex_src_string, char *hex_dst_string, |
| 203 | int result ) |
| 204 | { |
| 205 | unsigned char key_str[1000]; |
| 206 | unsigned char iv_str[1000]; |
| 207 | unsigned char src_str[1000]; |
| 208 | unsigned char dst_str[1000]; |
| 209 | unsigned char output[1000]; |
| 210 | mbedtls_aria_context ctx; |
| 211 | size_t iv_offset = 0; |
| 212 | int key_len, data_len; |
| 213 | |
| 214 | memset( key_str, 0x00, 1000 ); |
| 215 | memset( iv_str, 0x00, 1000 ); |
| 216 | memset( src_str, 0x00, 1000 ); |
| 217 | memset( dst_str, 0x00, 1000 ); |
| 218 | memset( output, 0x00, 1000 ); |
| 219 | mbedtls_aria_init( &ctx ); |
| 220 | |
| 221 | key_len = unhexify( key_str, hex_key_string ); |
| 222 | unhexify( iv_str, hex_iv_string ); |
| 223 | data_len = unhexify( src_str, hex_src_string ); |
| 224 | |
| 225 | mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 226 | TEST_ASSERT( mbedtls_aria_crypt_cfb128( &ctx, MBEDTLS_ARIA_DECRYPT, |
| 227 | data_len, &iv_offset, iv_str, src_str, output ) == result ); |
| 228 | hexify( dst_str, output, data_len ); |
| 229 | |
| 230 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 231 | |
| 232 | exit: |
| 233 | mbedtls_aria_free( &ctx ); |
| 234 | } |
| 235 | /* END_CASE */ |
| 236 | |
| 237 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ |
| 238 | void aria_encrypt_ctr( char *hex_key_string, char *hex_iv_string, |
| 239 | char *hex_src_string, char *hex_dst_string, |
| 240 | int result ) |
| 241 | { |
| 242 | unsigned char key_str[1000]; |
| 243 | unsigned char iv_str[1000]; |
| 244 | unsigned char src_str[1000]; |
| 245 | unsigned char dst_str[1000]; |
| 246 | unsigned char output[1000]; |
| 247 | unsigned char blk[16]; |
| 248 | mbedtls_aria_context ctx; |
| 249 | size_t iv_offset = 0; |
| 250 | int key_len, data_len; |
| 251 | |
| 252 | memset( key_str, 0x00, 1000 ); |
| 253 | memset( iv_str, 0x00, 1000 ); |
| 254 | memset( src_str, 0x00, 1000 ); |
| 255 | memset( dst_str, 0x00, 1000 ); |
| 256 | memset( output, 0x00, 1000 ); |
| 257 | mbedtls_aria_init( &ctx ); |
| 258 | |
| 259 | key_len = unhexify( key_str, hex_key_string ); |
| 260 | unhexify( iv_str, hex_iv_string ); |
| 261 | data_len = unhexify( src_str, hex_src_string ); |
| 262 | |
| 263 | mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 264 | TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, data_len, |
| 265 | &iv_offset, iv_str, blk, src_str, output ) == result ); |
| 266 | hexify( dst_str, output, data_len ); |
| 267 | |
| 268 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 269 | |
| 270 | exit: |
| 271 | mbedtls_aria_free( &ctx ); |
| 272 | } |
| 273 | /* END_CASE */ |
| 274 | |
| 275 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CTR */ |
| 276 | void aria_decrypt_ctr( char *hex_key_string, char *hex_iv_string, |
| 277 | char *hex_src_string, char *hex_dst_string, |
| 278 | int result ) |
| 279 | { |
| 280 | unsigned char key_str[1000]; |
| 281 | unsigned char iv_str[1000]; |
| 282 | unsigned char src_str[1000]; |
| 283 | unsigned char dst_str[1000]; |
| 284 | unsigned char output[1000]; |
| 285 | unsigned char blk[16]; |
| 286 | mbedtls_aria_context ctx; |
| 287 | size_t iv_offset = 0; |
| 288 | int key_len, data_len; |
| 289 | |
| 290 | memset( key_str, 0x00, 1000 ); |
| 291 | memset( iv_str, 0x00, 1000 ); |
| 292 | memset( src_str, 0x00, 1000 ); |
| 293 | memset( dst_str, 0x00, 1000 ); |
| 294 | memset( output, 0x00, 1000 ); |
| 295 | mbedtls_aria_init( &ctx ); |
| 296 | |
| 297 | key_len = unhexify( key_str, hex_key_string ); |
| 298 | unhexify( iv_str, hex_iv_string ); |
| 299 | data_len = unhexify( src_str, hex_src_string ); |
| 300 | |
| 301 | mbedtls_aria_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 302 | TEST_ASSERT( mbedtls_aria_crypt_ctr( &ctx, data_len, |
| 303 | &iv_offset, iv_str, blk, src_str, output ) == result ); |
| 304 | hexify( dst_str, output, data_len ); |
| 305 | |
| 306 | TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 307 | |
| 308 | exit: |
| 309 | mbedtls_aria_free( &ctx ); |
| 310 | } |
| 311 | /* END_CASE */ |
| 312 | |
| 313 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
| 314 | void aria_selftest() |
| 315 | { |
| 316 | TEST_ASSERT( mbedtls_aria_self_test( 1 ) == 0 ); |
| 317 | } |
| 318 | /* END_CASE */ |