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_encrypt_ecb( char *hex_key_string, char *hex_src_string, 12 char *hex_dst_string, int setkey_result ) 13{ 14 unsigned char key_str[100]; 15 unsigned char src_str[100]; 16 unsigned char dst_str[100]; 17 unsigned char output[100]; 18 mbedtls_camellia_context ctx; 19 int key_len; 20 21 memset(key_str, 0x00, 100); 22 memset(src_str, 0x00, 100); 23 memset(dst_str, 0x00, 100); 24 memset(output, 0x00, 100); 25 mbedtls_camellia_init( &ctx ); 26 27 key_len = unhexify( key_str, hex_key_string ); 28 unhexify( src_str, hex_src_string ); 29 30 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result ); 31 if( setkey_result == 0 ) 32 { 33 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str, output ) == 0 ); 34 hexify( dst_str, output, 16 ); 35 36 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); 37 } 38 39exit: 40 mbedtls_camellia_free( &ctx ); 41} 42/* END_CASE */ 43 44/* BEGIN_CASE */ 45void camellia_decrypt_ecb( char *hex_key_string, char *hex_src_string, 46 char *hex_dst_string, int setkey_result ) 47{ 48 unsigned char key_str[100]; 49 unsigned char src_str[100]; 50 unsigned char dst_str[100]; 51 unsigned char output[100]; 52 mbedtls_camellia_context ctx; 53 int key_len; 54 55 memset(key_str, 0x00, 100); 56 memset(src_str, 0x00, 100); 57 memset(dst_str, 0x00, 100); 58 memset(output, 0x00, 100); 59 mbedtls_camellia_init( &ctx ); 60 61 key_len = unhexify( key_str, hex_key_string ); 62 unhexify( src_str, hex_src_string ); 63 64 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result ); 65 if( setkey_result == 0 ) 66 { 67 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str, output ) == 0 ); 68 hexify( dst_str, output, 16 ); 69 70 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); 71 } 72 73exit: 74 mbedtls_camellia_free( &ctx ); 75} 76/* END_CASE */ 77 78/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 79void camellia_encrypt_cbc( char *hex_key_string, char *hex_iv_string, 80 char *hex_src_string, char *hex_dst_string, 81 int cbc_result ) 82{ 83 unsigned char key_str[100]; 84 unsigned char iv_str[100]; 85 unsigned char src_str[100]; 86 unsigned char dst_str[100]; 87 unsigned char output[100]; 88 mbedtls_camellia_context ctx; 89 int key_len, data_len; 90 91 memset(key_str, 0x00, 100); 92 memset(iv_str, 0x00, 100); 93 memset(src_str, 0x00, 100); 94 memset(dst_str, 0x00, 100); 95 memset(output, 0x00, 100); 96 mbedtls_camellia_init( &ctx ); 97 98 key_len = unhexify( key_str, hex_key_string ); 99 unhexify( iv_str, hex_iv_string ); 100 data_len = unhexify( src_str, hex_src_string ); 101 102 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ); 103 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, data_len, iv_str, src_str, output) == cbc_result ); 104 if( cbc_result == 0 ) 105 { 106 hexify( dst_str, output, data_len ); 107 108 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); 109 } 110 111exit: 112 mbedtls_camellia_free( &ctx ); 113} 114/* END_CASE */ 115 116/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 117void camellia_decrypt_cbc( char *hex_key_string, char *hex_iv_string, 118 char *hex_src_string, char *hex_dst_string, 119 int cbc_result ) 120{ 121 unsigned char key_str[100]; 122 unsigned char iv_str[100]; 123 unsigned char src_str[100]; 124 unsigned char dst_str[100]; 125 unsigned char output[100]; 126 mbedtls_camellia_context ctx; 127 int key_len, data_len; 128 129 memset(key_str, 0x00, 100); 130 memset(iv_str, 0x00, 100); 131 memset(src_str, 0x00, 100); 132 memset(dst_str, 0x00, 100); 133 memset(output, 0x00, 100); 134 mbedtls_camellia_init( &ctx ); 135 136 key_len = unhexify( key_str, hex_key_string ); 137 unhexify( iv_str, hex_iv_string ); 138 data_len = unhexify( src_str, hex_src_string ); 139 140 mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 ); 141 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result ); 142 if( cbc_result == 0 ) 143 { 144 hexify( dst_str, output, data_len ); 145 146 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); 147 } 148 149exit: 150 mbedtls_camellia_free( &ctx ); 151} 152/* END_CASE */ 153 154/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 155void camellia_encrypt_cfb128( char *hex_key_string, char *hex_iv_string, 156 char *hex_src_string, char *hex_dst_string ) 157{ 158 unsigned char key_str[100]; 159 unsigned char iv_str[100]; 160 unsigned char src_str[100]; 161 unsigned char dst_str[100]; 162 unsigned char output[100]; 163 mbedtls_camellia_context ctx; 164 size_t iv_offset = 0; 165 int key_len; 166 167 memset(key_str, 0x00, 100); 168 memset(iv_str, 0x00, 100); 169 memset(src_str, 0x00, 100); 170 memset(dst_str, 0x00, 100); 171 memset(output, 0x00, 100); 172 mbedtls_camellia_init( &ctx ); 173 174 key_len = unhexify( key_str, hex_key_string ); 175 unhexify( iv_str, hex_iv_string ); 176 unhexify( src_str, hex_src_string ); 177 178 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ); 179 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); 180 hexify( dst_str, output, 16 ); 181 182 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); 183 184exit: 185 mbedtls_camellia_free( &ctx ); 186} 187/* END_CASE */ 188 189/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 190void camellia_decrypt_cfb128( char *hex_key_string, char *hex_iv_string, 191 char *hex_src_string, char *hex_dst_string ) 192{ 193 unsigned char key_str[100]; 194 unsigned char iv_str[100]; 195 unsigned char src_str[100]; 196 unsigned char dst_str[100]; 197 unsigned char output[100]; 198 mbedtls_camellia_context ctx; 199 size_t iv_offset = 0; 200 int key_len; 201 202 memset(key_str, 0x00, 100); 203 memset(iv_str, 0x00, 100); 204 memset(src_str, 0x00, 100); 205 memset(dst_str, 0x00, 100); 206 memset(output, 0x00, 100); 207 mbedtls_camellia_init( &ctx ); 208 209 key_len = unhexify( key_str, hex_key_string ); 210 unhexify( iv_str, hex_iv_string ); 211 unhexify( src_str, hex_src_string ); 212 213 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ); 214 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); 215 hexify( dst_str, output, 16 ); 216 217 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 ); 218 219exit: 220 mbedtls_camellia_free( &ctx ); 221} 222/* END_CASE */ 223 224/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 225void camellia_selftest() 226{ 227 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 ); 228} 229/* END_CASE */ 230