void test_aes_encrypt(void) { MegaAesKey* k = mega_aes_key_new_from_binary(KEY_BINARY); g_assert(mega_aes_key_is_loaded(k)); guchar plain[64] = {0}, cipher[64] = {0}, plain_dec[64] = {0}; mega_aes_key_encrypt_raw(k, plain, cipher, 64); mega_aes_key_decrypt_raw(k, cipher, plain_dec, 64); g_assert(memcmp(plain, plain_dec, 64) == 0); g_assert(memcmp(plain, cipher, 64) != 0); // VALID use of UBase64 funcs gchar* cipher_ubase64 = mega_aes_key_encrypt(k, plain, 16); g_assert_cmpstr(cipher_ubase64, ==, "muj9ArNAKIoOe7_w8LpU1g"); GBytes* plain_bytes = mega_aes_key_decrypt(k, cipher_ubase64); g_assert_cmpuint(g_bytes_get_size(plain_bytes), ==, 16); g_assert(memcmp(g_bytes_get_data(plain_bytes, NULL), plain, 16) == 0); // INVALID use of UBase64 funcs g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*(len % 16) == 0*failed*"); gchar* non_multiple_of_16 = mega_aes_key_encrypt(k, plain, 18); g_test_assert_expected_messages(); g_assert(non_multiple_of_16 == NULL); g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*len > 0*failed*"); gchar* zero_length = mega_aes_key_encrypt(k, plain, 0); g_test_assert_expected_messages(); g_assert(zero_length == NULL); GBytes* invalid_key_ubase64 = mega_aes_key_decrypt(k, KEY_INVALID); g_assert(invalid_key_ubase64 == NULL); GBytes* null_key_ubase64 = mega_aes_key_decrypt(k, KEY_NULL); g_assert(null_key_ubase64 == NULL); }
static gboolean handle_auth(const gchar* handle, const gchar* b64_ha, MegaAesKey* master_key) { gsize ha_len = 0; gboolean status; g_return_val_if_fail(handle != NULL, FALSE); g_return_val_if_fail(b64_ha != NULL, FALSE); g_return_val_if_fail(master_key != NULL, FALSE); GBytes* ha = mega_aes_key_decrypt(master_key, b64_ha); if (!ha || g_bytes_get_size(ha) != 16) { g_bytes_unref(ha); return FALSE; } const gchar* ha_data = g_bytes_get_data(ha, NULL); status = !memcmp(ha_data, handle, 8) && !memcmp(ha_data + 8, handle, 8); g_bytes_unref(ha); return status; }