Exemple #1
0
void gtkhash_hmac_finish(struct hash_func_s *func)
{
	g_assert(func);
	g_assert(func->hmac_supported);
	g_assert(func->block_size > 0);
	g_assert(func->hmac_data);

	gtkhash_hash_lib_update(func->hmac_data, func->digest->bin, func->digest_size);
	gtkhash_hash_lib_finish(func->hmac_data);

	uint8_t *digest = g_memdup(func->hmac_data->digest->bin, func->hmac_data->digest_size);
	gtkhash_hash_func_set_digest(func, digest, func->hmac_data->digest_size);

	gtkhash_hmac_deinit(func);
}
Exemple #2
0
void gtkhash_hash_string(struct hash_func_s *funcs, const char *str,
	const enum digest_format_e format)
{
	g_assert(str);
	g_assert(DIGEST_FORMAT_IS_VALID(format));

	for (int i = 0; i < HASH_FUNCS_N; i++) {
		if (!funcs[i].enabled)
			continue;
		gtkhash_hash_lib_start(&funcs[i]);
		// Assuming this won't take too long
		gtkhash_hash_lib_update(&funcs[i], (const uint8_t *)str, strlen(str));
		gtkhash_hash_lib_finish(&funcs[i]);

		const char *digest = gtkhash_hash_func_get_digest(&funcs[i], format);
		gtkhash_hash_string_finish_cb(funcs[i].id, digest);
	}
}
Exemple #3
0
void gtkhash_hmac_start(struct hash_func_s *func, const uint8_t *key,
	const size_t key_size)
{
	g_assert(func);
	g_assert(func->hmac_supported);
	g_assert(func->block_size > 0);
	g_assert(key);

	func->hmac_data = g_new(struct hash_func_s, 1);

	uint8_t padded_key[func->block_size];
	memset(padded_key, 0, func->block_size);

	if (key_size > func->block_size) {
		gtkhash_hash_func_init(func->hmac_data, func->id);
		func->hmac_data->enabled = true;

		gtkhash_hash_lib_start(func->hmac_data, NULL, 0);
		gtkhash_hash_lib_update(func->hmac_data, key, key_size);
		gtkhash_hash_lib_finish(func->hmac_data);

		size_t newsize = func->hmac_data->digest_size;
		g_assert((newsize > 0) && (newsize <= func->block_size));
		memcpy(padded_key, func->hmac_data->digest->bin, newsize);

		gtkhash_hash_func_deinit(func->hmac_data);
	} else
		memcpy(padded_key, key, key_size);

	uint8_t pad[func->block_size];

	for (int i = 0; i < func->block_size; i++)
		pad[i] = 0x36 ^ padded_key[i];
	gtkhash_hash_lib_update(func, pad, func->block_size);

	gtkhash_hash_func_init(func->hmac_data, func->id);
	func->hmac_data->enabled = true;
	gtkhash_hash_lib_start(func->hmac_data, NULL, 0);

	for (int i = 0; i < func->block_size; i++)
		pad[i] = 0x5c ^ padded_key[i];
	gtkhash_hash_lib_update(func->hmac_data, pad, func->block_size);
}
Exemple #4
0
static void gtkhash_hash_file_finish(struct hash_file_s *data)
{
	if (G_UNLIKELY(g_cancellable_is_cancelled(data->cancellable))) {
		for (int i = 0; i < HASH_FUNCS_N; i++)
			if (data->funcs[i].enabled)
				gtkhash_hash_lib_stop(&data->funcs[i]);
	} else {
		for (int i = 0; i < HASH_FUNCS_N; i++)
			if (data->funcs[i].enabled)
				gtkhash_hash_lib_finish(&data->funcs[i]);
	}

	g_object_unref(data->file);
	g_free(data->buffer);
	g_timer_destroy(data->timer);
	g_thread_pool_free(data->thread_pool, true, false);

	gtkhash_hash_file_set_state(data, HASH_FILE_STATE_CALLBACK);
}