1/* BEGIN_HEADER */ 2#include "mbedtls/aes.h" 3/* END_HEADER */ 4 5/* BEGIN_DEPENDENCIES 6 * depends_on:MBEDTLS_AES_C 7 * END_DEPENDENCIES 8 */ 9 10/* BEGIN_CASE */ 11void aes_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_aes_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_aes_init( &ctx ); 26 27 key_len = unhexify( key_str, hex_key_string ); 28 unhexify( src_str, hex_src_string ); 29 30 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result ); 31 if( setkey_result == 0 ) 32 { 33 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 ); 34 hexify( dst_str, output, 16 ); 35 36 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 37 } 38 39exit: 40 mbedtls_aes_free( &ctx ); 41} 42/* END_CASE */ 43 44/* BEGIN_CASE */ 45void aes_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_aes_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_aes_init( &ctx ); 60 61 key_len = unhexify( key_str, hex_key_string ); 62 unhexify( src_str, hex_src_string ); 63 64 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result ); 65 if( setkey_result == 0 ) 66 { 67 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 ); 68 hexify( dst_str, output, 16 ); 69 70 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 71 } 72 73exit: 74 mbedtls_aes_free( &ctx ); 75} 76/* END_CASE */ 77 78/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 79void aes_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_aes_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_aes_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_aes_setkey_enc( &ctx, key_str, key_len * 8 ); 103 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_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( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 109 } 110 111exit: 112 mbedtls_aes_free( &ctx ); 113} 114/* END_CASE */ 115 116/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 117void aes_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_aes_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_aes_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_aes_setkey_dec( &ctx, key_str, key_len * 8 ); 141 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_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( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 147 } 148 149exit: 150 mbedtls_aes_free( &ctx ); 151} 152/* END_CASE */ 153 154/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 155void aes_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_aes_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_aes_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_aes_setkey_enc( &ctx, key_str, key_len * 8 ); 179 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); 180 hexify( dst_str, output, 16 ); 181 182 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 183 184exit: 185 mbedtls_aes_free( &ctx ); 186} 187/* END_CASE */ 188 189/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 190void aes_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_aes_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_aes_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_aes_setkey_enc( &ctx, key_str, key_len * 8 ); 214 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); 215 hexify( dst_str, output, 16 ); 216 217 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 218 219exit: 220 mbedtls_aes_free( &ctx ); 221} 222/* END_CASE */ 223 224/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 225void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string, 226 char *hex_src_string, char *hex_dst_string ) 227{ 228 unsigned char key_str[100]; 229 unsigned char iv_str[100]; 230 unsigned char src_str[100]; 231 unsigned char dst_str[100]; 232 unsigned char output[100]; 233 mbedtls_aes_context ctx; 234 int key_len, src_len; 235 236 memset(key_str, 0x00, 100); 237 memset(iv_str, 0x00, 100); 238 memset(src_str, 0x00, 100); 239 memset(dst_str, 0x00, 100); 240 memset(output, 0x00, 100); 241 mbedtls_aes_init( &ctx ); 242 243 key_len = unhexify( key_str, hex_key_string ); 244 unhexify( iv_str, hex_iv_string ); 245 src_len = unhexify( src_str, hex_src_string ); 246 247 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); 248 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_len, iv_str, src_str, output ) == 0 ); 249 hexify( dst_str, output, src_len ); 250 251 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 252 253exit: 254 mbedtls_aes_free( &ctx ); 255} 256/* END_CASE */ 257 258/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 259void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string, 260 char *hex_src_string, char *hex_dst_string ) 261{ 262 unsigned char key_str[100]; 263 unsigned char iv_str[100]; 264 unsigned char src_str[100]; 265 unsigned char dst_str[100]; 266 unsigned char output[100]; 267 mbedtls_aes_context ctx; 268 int key_len, src_len; 269 270 memset(key_str, 0x00, 100); 271 memset(iv_str, 0x00, 100); 272 memset(src_str, 0x00, 100); 273 memset(dst_str, 0x00, 100); 274 memset(output, 0x00, 100); 275 mbedtls_aes_init( &ctx ); 276 277 key_len = unhexify( key_str, hex_key_string ); 278 unhexify( iv_str, hex_iv_string ); 279 src_len = unhexify( src_str, hex_src_string ); 280 281 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); 282 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_len, iv_str, src_str, output ) == 0 ); 283 hexify( dst_str, output, src_len ); 284 285 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); 286 287exit: 288 mbedtls_aes_free( &ctx ); 289} 290/* END_CASE */ 291 292/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 293void aes_selftest() 294{ 295 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); 296} 297/* END_CASE */ 298