1/* BEGIN_HEADER */ 2#include "mbedtls/camellia.h" 3/* END_HEADER */ 4 5/* BEGIN_DEPENDENCIES 6 * depends_on:MBEDTLS_CAMELLIA_C 7 * END_DEPENDENCIES 8 */ 9 10/* BEGIN_CASE */ 11void camellia_valid_param( ) 12{ 13 TEST_VALID_PARAM( mbedtls_camellia_free( NULL ) ); 14} 15/* END_CASE */ 16 17/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ 18void camellia_invalid_param( ) 19{ 20 mbedtls_camellia_context ctx; 21 unsigned char buf[16] = { 0 }; 22 const size_t valid_keybits = 128; 23 const int invalid_mode = 42; 24 const int valid_mode = MBEDTLS_CAMELLIA_ENCRYPT; 25 size_t off; 26 ((void) off); 27 28 TEST_INVALID_PARAM( mbedtls_camellia_init( NULL ) ); 29 30 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 31 mbedtls_camellia_setkey_enc( NULL, 32 buf, 33 valid_keybits ) ); 34 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 35 mbedtls_camellia_setkey_enc( &ctx, 36 NULL, 37 valid_keybits ) ); 38 39 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 40 mbedtls_camellia_setkey_dec( NULL, 41 buf, 42 valid_keybits ) ); 43 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 44 mbedtls_camellia_setkey_dec( &ctx, 45 NULL, 46 valid_keybits ) ); 47 48 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 49 mbedtls_camellia_crypt_ecb( NULL, 50 valid_mode, 51 buf, buf ) ); 52 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 53 mbedtls_camellia_crypt_ecb( &ctx, 54 invalid_mode, 55 buf, buf ) ); 56 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 57 mbedtls_camellia_crypt_ecb( &ctx, 58 valid_mode, 59 NULL, buf ) ); 60 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 61 mbedtls_camellia_crypt_ecb( &ctx, 62 valid_mode, 63 buf, NULL ) ); 64 65#if defined(MBEDTLS_CIPHER_MODE_CBC) 66 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 67 mbedtls_camellia_crypt_cbc( NULL, 68 valid_mode, 69 sizeof( buf ), 70 buf, buf, buf ) ); 71 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 72 mbedtls_camellia_crypt_cbc( &ctx, 73 invalid_mode, 74 sizeof( buf ), 75 buf, buf, buf ) ); 76 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 77 mbedtls_camellia_crypt_cbc( &ctx, 78 valid_mode, 79 sizeof( buf ), 80 NULL, buf, buf ) ); 81 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 82 mbedtls_camellia_crypt_cbc( &ctx, 83 valid_mode, 84 sizeof( buf ), 85 buf, NULL, buf ) ); 86 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 87 mbedtls_camellia_crypt_cbc( &ctx, 88 valid_mode, 89 sizeof( buf ), 90 buf, buf, NULL ) ); 91#endif /* MBEDTLS_CIPHER_MODE_CBC */ 92 93#if defined(MBEDTLS_CIPHER_MODE_CFB) 94 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 95 mbedtls_camellia_crypt_cfb128( NULL, 96 valid_mode, 97 sizeof( buf ), 98 &off, buf, 99 buf, buf ) ); 100 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 101 mbedtls_camellia_crypt_cfb128( &ctx, 102 invalid_mode, 103 sizeof( buf ), 104 &off, buf, 105 buf, buf ) ); 106 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 107 mbedtls_camellia_crypt_cfb128( &ctx, 108 valid_mode, 109 sizeof( buf ), 110 NULL, buf, 111 buf, buf ) ); 112 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 113 mbedtls_camellia_crypt_cfb128( &ctx, 114 valid_mode, 115 sizeof( buf ), 116 &off, NULL, 117 buf, buf ) ); 118 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 119 mbedtls_camellia_crypt_cfb128( &ctx, 120 valid_mode, 121 sizeof( buf ), 122 &off, buf, 123 NULL, buf ) ); 124 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 125 mbedtls_camellia_crypt_cfb128( &ctx, 126 valid_mode, 127 sizeof( buf ), 128 &off, buf, 129 buf, NULL ) ); 130#endif /* MBEDTLS_CIPHER_MODE_CFB */ 131 132#if defined(MBEDTLS_CIPHER_MODE_CTR) 133 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 134 mbedtls_camellia_crypt_ctr( NULL, 135 sizeof( buf ), 136 &off, 137 buf, buf, 138 buf, buf ) ); 139 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 140 mbedtls_camellia_crypt_ctr( &ctx, 141 sizeof( buf ), 142 NULL, 143 buf, buf, 144 buf, buf ) ); 145 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 146 mbedtls_camellia_crypt_ctr( &ctx, 147 sizeof( buf ), 148 &off, 149 NULL, buf, 150 buf, buf ) ); 151 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 152 mbedtls_camellia_crypt_ctr( &ctx, 153 sizeof( buf ), 154 &off, 155 buf, NULL, 156 buf, buf ) ); 157 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 158 mbedtls_camellia_crypt_ctr( &ctx, 159 sizeof( buf ), 160 &off, 161 buf, buf, 162 NULL, buf ) ); 163 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 164 mbedtls_camellia_crypt_ctr( &ctx, 165 sizeof( buf ), 166 &off, 167 buf, buf, 168 buf, NULL ) ); 169#endif /* MBEDTLS_CIPHER_MODE_CTR */ 170 171exit: 172 return; 173} 174/* END_CASE */ 175 176/* BEGIN_CASE */ 177void camellia_encrypt_ecb( data_t * key_str, data_t * src_str, 178 data_t * dst, int setkey_result ) 179{ 180 unsigned char output[100]; 181 mbedtls_camellia_context ctx; 182 183 memset(output, 0x00, 100); 184 mbedtls_camellia_init( &ctx ); 185 186 187 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result ); 188 if( setkey_result == 0 ) 189 { 190 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x, output ) == 0 ); 191 192 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 ); 193 } 194 195exit: 196 mbedtls_camellia_free( &ctx ); 197} 198/* END_CASE */ 199 200/* BEGIN_CASE */ 201void camellia_decrypt_ecb( data_t * key_str, data_t * src_str, 202 data_t * dst, int setkey_result ) 203{ 204 unsigned char output[100]; 205 mbedtls_camellia_context ctx; 206 207 memset(output, 0x00, 100); 208 mbedtls_camellia_init( &ctx ); 209 210 211 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result ); 212 if( setkey_result == 0 ) 213 { 214 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x, output ) == 0 ); 215 216 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 ); 217 } 218 219exit: 220 mbedtls_camellia_free( &ctx ); 221} 222/* END_CASE */ 223 224/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 225void camellia_encrypt_cbc( data_t * key_str, data_t * iv_str, 226 data_t * src_str, data_t * dst, int cbc_result ) 227{ 228 unsigned char output[100]; 229 mbedtls_camellia_context ctx; 230 231 memset(output, 0x00, 100); 232 mbedtls_camellia_init( &ctx ); 233 234 235 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 236 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->len, iv_str->x, src_str->x, output) == cbc_result ); 237 if( cbc_result == 0 ) 238 { 239 240 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len, 241 dst->len ) == 0 ); 242 } 243 244exit: 245 mbedtls_camellia_free( &ctx ); 246} 247/* END_CASE */ 248 249/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 250void camellia_decrypt_cbc( data_t * key_str, data_t * iv_str, 251 data_t * src_str, data_t * dst, 252 int cbc_result ) 253{ 254 unsigned char output[100]; 255 mbedtls_camellia_context ctx; 256 257 memset(output, 0x00, 100); 258 mbedtls_camellia_init( &ctx ); 259 260 261 mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 ); 262 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); 263 if( cbc_result == 0 ) 264 { 265 266 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len, 267 dst->len ) == 0 ); 268 } 269 270exit: 271 mbedtls_camellia_free( &ctx ); 272} 273/* END_CASE */ 274 275/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 276void camellia_encrypt_cfb128( data_t * key_str, data_t * iv_str, 277 data_t * src_str, data_t * dst ) 278{ 279 unsigned char output[100]; 280 mbedtls_camellia_context ctx; 281 size_t iv_offset = 0; 282 283 memset(output, 0x00, 100); 284 mbedtls_camellia_init( &ctx ); 285 286 287 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 288 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 ); 289 290 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 ); 291 292exit: 293 mbedtls_camellia_free( &ctx ); 294} 295/* END_CASE */ 296 297/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 298void camellia_decrypt_cfb128( data_t * key_str, data_t * iv_str, 299 data_t * src_str, 300 data_t * dst ) 301{ 302 unsigned char output[100]; 303 mbedtls_camellia_context ctx; 304 size_t iv_offset = 0; 305 306 memset(output, 0x00, 100); 307 mbedtls_camellia_init( &ctx ); 308 309 310 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 311 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 ); 312 313 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 ); 314 315exit: 316 mbedtls_camellia_free( &ctx ); 317} 318/* END_CASE */ 319 320/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 321void camellia_selftest( ) 322{ 323 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 ); 324} 325/* END_CASE */ 326