test_suite_camellia.function (5953B)
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 */ 11 void camellia_invalid_param() 12 { 13 mbedtls_camellia_context ctx; 14 unsigned char buf[16] = { 0 }; 15 const int invalid_mode = 42; 16 size_t off; 17 ((void) off); 18 19 TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 20 mbedtls_camellia_crypt_ecb(&ctx, 21 invalid_mode, 22 buf, buf)); 23 24 #if defined(MBEDTLS_CIPHER_MODE_CBC) 25 TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 26 mbedtls_camellia_crypt_cbc(&ctx, 27 invalid_mode, 28 sizeof(buf), 29 buf, buf, buf)); 30 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 31 32 #if defined(MBEDTLS_CIPHER_MODE_CFB) 33 TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA, 34 mbedtls_camellia_crypt_cfb128(&ctx, 35 invalid_mode, 36 sizeof(buf), 37 &off, buf, 38 buf, buf)); 39 #endif /* MBEDTLS_CIPHER_MODE_CFB */ 40 41 exit: 42 return; 43 } 44 /* END_CASE */ 45 46 /* BEGIN_CASE */ 47 void camellia_encrypt_ecb(data_t *key_str, data_t *src_str, 48 data_t *dst, int setkey_result) 49 { 50 unsigned char output[100]; 51 mbedtls_camellia_context ctx; 52 53 memset(output, 0x00, 100); 54 mbedtls_camellia_init(&ctx); 55 56 57 TEST_ASSERT(mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8) == setkey_result); 58 if (setkey_result == 0) { 59 TEST_ASSERT(mbedtls_camellia_crypt_ecb(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x, 60 output) == 0); 61 62 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); 63 } 64 65 exit: 66 mbedtls_camellia_free(&ctx); 67 } 68 /* END_CASE */ 69 70 /* BEGIN_CASE depends_on:!MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */ 71 void camellia_decrypt_ecb(data_t *key_str, data_t *src_str, 72 data_t *dst, int setkey_result) 73 { 74 unsigned char output[100]; 75 mbedtls_camellia_context ctx; 76 77 memset(output, 0x00, 100); 78 mbedtls_camellia_init(&ctx); 79 80 81 TEST_ASSERT(mbedtls_camellia_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result); 82 if (setkey_result == 0) { 83 TEST_ASSERT(mbedtls_camellia_crypt_ecb(&ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x, 84 output) == 0); 85 86 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); 87 } 88 89 exit: 90 mbedtls_camellia_free(&ctx); 91 } 92 /* END_CASE */ 93 94 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 95 void camellia_encrypt_cbc(data_t *key_str, data_t *iv_str, 96 data_t *src_str, data_t *dst, int cbc_result) 97 { 98 unsigned char output[100]; 99 mbedtls_camellia_context ctx; 100 101 memset(output, 0x00, 100); 102 mbedtls_camellia_init(&ctx); 103 104 105 mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8); 106 TEST_ASSERT(mbedtls_camellia_crypt_cbc(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->len, iv_str->x, 107 src_str->x, output) == cbc_result); 108 if (cbc_result == 0) { 109 110 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len, 111 dst->len) == 0); 112 } 113 114 exit: 115 mbedtls_camellia_free(&ctx); 116 } 117 /* END_CASE */ 118 119 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ 120 void camellia_decrypt_cbc(data_t *key_str, data_t *iv_str, 121 data_t *src_str, data_t *dst, 122 int cbc_result) 123 { 124 unsigned char output[100]; 125 mbedtls_camellia_context ctx; 126 127 memset(output, 0x00, 100); 128 mbedtls_camellia_init(&ctx); 129 130 131 mbedtls_camellia_setkey_dec(&ctx, key_str->x, key_str->len * 8); 132 TEST_ASSERT(mbedtls_camellia_crypt_cbc(&ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->len, iv_str->x, 133 src_str->x, output) == cbc_result); 134 if (cbc_result == 0) { 135 136 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len, 137 dst->len) == 0); 138 } 139 140 exit: 141 mbedtls_camellia_free(&ctx); 142 } 143 /* END_CASE */ 144 145 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 146 void camellia_encrypt_cfb128(data_t *key_str, data_t *iv_str, 147 data_t *src_str, data_t *dst) 148 { 149 unsigned char output[100]; 150 mbedtls_camellia_context ctx; 151 size_t iv_offset = 0; 152 153 memset(output, 0x00, 100); 154 mbedtls_camellia_init(&ctx); 155 156 157 mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8); 158 TEST_ASSERT(mbedtls_camellia_crypt_cfb128(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, 159 iv_str->x, src_str->x, output) == 0); 160 161 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); 162 163 exit: 164 mbedtls_camellia_free(&ctx); 165 } 166 /* END_CASE */ 167 168 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 169 void camellia_decrypt_cfb128(data_t *key_str, data_t *iv_str, 170 data_t *src_str, 171 data_t *dst) 172 { 173 unsigned char output[100]; 174 mbedtls_camellia_context ctx; 175 size_t iv_offset = 0; 176 177 memset(output, 0x00, 100); 178 mbedtls_camellia_init(&ctx); 179 180 181 mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8); 182 TEST_ASSERT(mbedtls_camellia_crypt_cfb128(&ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, 183 iv_str->x, src_str->x, output) == 0); 184 185 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0); 186 187 exit: 188 mbedtls_camellia_free(&ctx); 189 } 190 /* END_CASE */ 191 192 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 193 void camellia_selftest() 194 { 195 TEST_ASSERT(mbedtls_camellia_self_test(1) == 0); 196 } 197 /* END_CASE */