Пример #1
0
static int diffuse(char *src, char *dst, size_t size, const char *hash_name)
{
	int hash_size = crypt_hash_size(hash_name);
	unsigned int digest_size;
	unsigned int i, blocks, padding;

	if (hash_size <= 0)
		return 1;
	digest_size = hash_size;

	blocks = size / digest_size;
	padding = size % digest_size;

	for (i = 0; i < blocks; i++)
		if(hash_buf(src + digest_size * i,
			    dst + digest_size * i,
			    i, (size_t)digest_size, hash_name))
			return 1;

	if(padding)
		if(hash_buf(src + digest_size * i,
			    dst + digest_size * i,
			    i, (size_t)padding, hash_name))
			return 1;

	return 0;
}
bool KeyValClient::insert(KeyVal* kv) {
	int req_size = sizeof(KVRequest) + kv->val.len;
	KVRequest* req = (KVRequest*)mf_malloc(this->mf, req_size);

	req->type = TYPE_INSERT;
	req->kv = *kv;

	uint32_t hash131 = hash_buf(kv->val.bytes, kv->val.len);
#define READBACK_CHECK 1
#if READBACK_CHECK
	ZLC_TERSE(ze, "Insert val len %d hash 0x%x\n",, kv->val.len, hash131);
#endif // READBACK_CHECK

	lite_memcpy(req->kv.val.bytes, kv->val.bytes, kv->val.len);

	bool rc = this->sock->sendto(this->server, req, req_size);
	if (!rc) { return false; }

	ZeroCopyBuf* reply = this->sock->recvfrom(this->server);
	lite_assert(reply!=NULL);
	lite_assert(reply->len()==sizeof(uint32_t));
	lite_assert(((uint32_t*)reply->data())[0] == hash131);
	delete reply;

	mf_free(this->mf, req);
	return true;
}
Пример #3
0
static int diffuse(char *src, char *dst, size_t size, const EVP_MD *hash_id)
{
	unsigned int digest_size = EVP_MD_size(hash_id);
	unsigned int i, blocks, padding;

	blocks = size / digest_size;
	padding = size % digest_size;

	for (i = 0; i < blocks; i++)
		if(hash_buf(src + digest_size * i,
			    dst + digest_size * i,
			    i, digest_size, hash_id))
			return 1;

	if(padding)
		if(hash_buf(src + digest_size * i,
			    dst + digest_size * i,
			    i, padding, hash_id))
			return 1;

	return 0;
}
Value* KeyValClient::lookup(Key* key) {
	KVRequest req;

	req.type = TYPE_LOOKUP;
	req.kv.key = *key;

	bool rc = this->sock->sendto(this->server, &req, sizeof(KVRequest));
	if (!rc) { return NULL; }
	
	ZeroCopyBuf* buf = this->sock->recvfrom(this->server);
	if (buf == NULL) {
		ZLC_TERSE(ze, "WARNING: timeout contacting KeyVal server\n");
		return NULL;
	}

	Value* val = (Value*) buf->data();

#if READBACK_CHECK
{
	uint32_t bytes131hash = hash_buf(val->bytes, val->len);
	ZLC_TERSE(ze, "lookup val len %d hash 0x%x\n",, val->len, bytes131hash);
}
#endif // READBACK_CHECK

	Value* new_val = (Value*) mf_malloc(this->mf, sizeof(Value)+val->len);
	lite_memcpy(new_val, val, sizeof(Value)+val->len);
	delete buf;

#if READBACK_CHECK
{
	uint32_t bytes131hash = hash_buf(new_val->bytes, new_val->len);
	ZLC_TERSE(ze, "lookup new_val len %d hash 0x%x\n",, new_val->len, bytes131hash);
}
#endif // READBACK_CHECK

	return new_val;
}
void KVServer::handle_insert(KVRequest* request)
{
	KeyVal* newKV = NULL;

	// If the key is already present, we need to remove it first
	// (because hash_table doesn't like inserts for existing values)
	newKV = (KeyVal*) hash_table_lookup(&db, &request->kv);
	if (newKV != NULL) {
		hash_table_remove(&db, newKV);
		free(newKV);
	}

	// Now copy the request and insert it into our database
	newKV = KV_clone(&request->kv);
	uint32_t hash131 = hash_buf(newKV->val.bytes, newKV->val.len);
	lite_assert(newKV);
	hash_table_insert(&db, newKV);
	annotate("insert", newKV);

	int rc;
	rc = sendto(sock, &hash131, sizeof(hash131), 0, (sockaddr*)&remote_addr, sizeof(remote_addr));
	if (rc == -1) { fprintf(stderr, "(KeyValueServer) Failed to send insert reply!\n"); }
}
Пример #6
0
/*
 * Spawns the initial bot process and waits for them all to exit.
 */
int
main(int argc, char *argv[])
{
	char *pythonpath = getenv("PYTHONPATH");
	if (pythonpath) {
		char *str = (char*)xzmalloc(strlen(pythonpath) + strlen(":") + strlen("./libs") + 1);
		sprintf(str, "%s:%s", "./libs", pythonpath);
		setenv("PYTHONPATH", str, 1);
		free(str);
	} else {
		setenv("PYTHONPATH", "./libs", 1);
	}
	Py_Initialize();
	init_opencore();
	PyObject *sysPath = PySys_GetObject("path");
	PyObject *libDir = PyString_FromString(".");
	PyList_Append(sysPath, libDir);
	Py_DECREF(libDir);

	if (pthread_key_create(&g_tls_key, NULL) != 0) {
		Log(OP_SMOD, "Error creating thread-specific storage");
		exit(-1);
	}
	pthread_setspecific(g_tls_key, NULL);

	/* make directories */
	mkdir("files", 0700);

	/* setup packet handlers */
	for (int i = 0; i < 256; ++i) {
		g_pkt_core_handlers[i] = null_handler;
		g_pkt_game_handlers[i] = null_handler;
	}

	g_pkt_core_handlers[0x02] = pkt_handle_core_0x02;
	g_pkt_core_handlers[0x03] = pkt_handle_core_0x03;
	g_pkt_core_handlers[0x04] = pkt_handle_core_0x04;
	g_pkt_core_handlers[0x05] = pkt_handle_core_0x05;
	g_pkt_core_handlers[0x06] = pkt_handle_core_0x06;
	g_pkt_core_handlers[0x07] = pkt_handle_core_0x07;
	g_pkt_core_handlers[0x08] = pkt_handle_core_0x08_0x09;
	g_pkt_core_handlers[0x09] = pkt_handle_core_0x08_0x09;
	g_pkt_core_handlers[0x0A] = pkt_handle_core_0x0A;
	g_pkt_core_handlers[0x0E] = pkt_handle_core_0x0E;

	g_pkt_game_handlers[0x02] = pkt_handle_game_0x02;
	g_pkt_game_handlers[0x03] = pkt_handle_game_0x03;
	g_pkt_game_handlers[0x04] = pkt_handle_game_0x04;
	g_pkt_game_handlers[0x07] = pkt_handle_game_0x07;
	g_pkt_game_handlers[0x06] = pkt_handle_game_0x06;
	g_pkt_game_handlers[0x0A] = pkt_handle_game_0x0A;
	g_pkt_game_handlers[0x0D] = pkt_handle_game_0x0D;
	g_pkt_game_handlers[0x0E] = pkt_handle_game_0x0E;
	g_pkt_game_handlers[0x14] = pkt_handle_game_0x14;
	g_pkt_game_handlers[0x19] = pkt_handle_game_0x19;
	g_pkt_game_handlers[0x1C] = pkt_handle_game_0x1C;
	g_pkt_game_handlers[0x1D] = pkt_handle_game_0x1D;
	g_pkt_game_handlers[0x27] = pkt_handle_game_0x27;
	g_pkt_game_handlers[0x28] = pkt_handle_game_0x28;
	g_pkt_game_handlers[0x29] = pkt_handle_game_0x29;
	g_pkt_game_handlers[0x2E] = pkt_handle_game_0x2E;
	g_pkt_game_handlers[0x2F] = pkt_handle_game_0x2F;
	g_pkt_game_handlers[0x31] = pkt_handle_game_0x31;

	struct utsname uts;
	bzero(&uts, sizeof(struct utsname));
	uname(&uts);
	uint64_t hash = hash_buf(&uts, sizeof(struct utsname));
	g_machineid = gen_valid_mid(hash & 0xFFFFFFFF);
	g_permissionid = hash >> 32;

	load_op_file();

	static const char* const masterconfig = "types/master.conf";

	log_init();
	db_init(masterconfig);
	botman_init();

	/* run the master bot */
	char arenaname[32] = { '\0' };
	config_get_string("login.masterarena", arenaname, sizeof(arenaname), "#master", masterconfig);
	LogFmt(OP_MOD, "Starting master into %s", arenaname);
	char *err = StartBot("master", arenaname, NULL);
	if (err) {
		LogFmt(OP_MOD, "Error starting master bot: %s", err);
		return -1;
	}

	/* become the bot management thread and loop */
	botman_mainloop();

	botman_shutdown();
	db_shutdown();
	log_shutdown();

	pthread_key_delete(g_tls_key);

	Py_Finalize();

	return 0;
}
uint32_t MmapBehavior::hash(const void* datum)
{
	MmapBehavior* obj = (MmapBehavior*) datum;
	return hash_buf(obj->filename, strlen(obj->filename));
}
Пример #8
0
int pkcs5_pbkdf2(const char *hash,
			const char *P, size_t Plen,
			const char *S, size_t Slen,
			unsigned int c, unsigned int dkLen,
			char *DK, unsigned int hash_block_size)
{
	struct crypt_hmac *hmac;
	char U[MAX_PRF_BLOCK_LEN];
	char T[MAX_PRF_BLOCK_LEN];
	char P_hash[MAX_PRF_BLOCK_LEN];
	int i, k, rc = -EINVAL;
	unsigned int u, hLen, l, r;
	size_t tmplen = Slen + 4;
	char *tmp;

	tmp = alloca(tmplen);
	if (tmp == NULL)
		return -ENOMEM;

	hLen = crypt_hmac_size(hash);
	if (hLen == 0 || hLen > MAX_PRF_BLOCK_LEN)
		return -EINVAL;

	if (c == 0)
		return -EINVAL;

	if (dkLen == 0)
		return -EINVAL;

	/*
	 *
	 *  Steps:
	 *
	 *     1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
	 *        stop.
	 */

	if (dkLen > 4294967295U)
		return -EINVAL;

	/*
	 *     2. Let l be the number of hLen-octet blocks in the derived key,
	 *        rounding up, and let r be the number of octets in the last
	 *        block:
	 *
	 *                  l = CEIL (dkLen / hLen) ,
	 *                  r = dkLen - (l - 1) * hLen .
	 *
	 *        Here, CEIL (x) is the "ceiling" function, i.e. the smallest
	 *        integer greater than, or equal to, x.
	 */

	l = dkLen / hLen;
	if (dkLen % hLen)
		l++;
	r = dkLen - (l - 1) * hLen;

	/*
	 *     3. For each block of the derived key apply the function F defined
	 *        below to the password P, the salt S, the iteration count c, and
	 *        the block index to compute the block:
	 *
	 *                  T_1 = F (P, S, c, 1) ,
	 *                  T_2 = F (P, S, c, 2) ,
	 *                  ...
	 *                  T_l = F (P, S, c, l) ,
	 *
	 *        where the function F is defined as the exclusive-or sum of the
	 *        first c iterates of the underlying pseudorandom function PRF
	 *        applied to the password P and the concatenation of the salt S
	 *        and the block index i:
	 *
	 *                  F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
	 *
	 *        where
	 *
	 *                  U_1 = PRF (P, S || INT (i)) ,
	 *                  U_2 = PRF (P, U_1) ,
	 *                  ...
	 *                  U_c = PRF (P, U_{c-1}) .
	 *
	 *        Here, INT (i) is a four-octet encoding of the integer i, most
	 *        significant octet first.
	 *
	 *     4. Concatenate the blocks and extract the first dkLen octets to
	 *        produce a derived key DK:
	 *
	 *                  DK = T_1 || T_2 ||  ...  || T_l<0..r-1>
	 *
	 *     5. Output the derived key DK.
	 *
	 *  Note. The construction of the function F follows a "belt-and-
	 *  suspenders" approach. The iterates U_i are computed recursively to
	 *  remove a degree of parallelism from an opponent; they are exclusive-
	 *  ored together to reduce concerns about the recursion degenerating
	 *  into a small set of values.
	 *
	 */

	/* If hash_block_size is provided, hash password in advance. */
	if (hash_block_size > 0 && Plen > hash_block_size) {
		if (hash_buf(P, Plen, P_hash, hLen, hash))
			return -EINVAL;

		if (crypt_hmac_init(&hmac, hash, P_hash, hLen))
			return -EINVAL;
		memset(P_hash, 0, sizeof(P_hash));
	} else {
		if (crypt_hmac_init(&hmac, hash, P, Plen))
			return -EINVAL;
	}

	for (i = 1; (unsigned int) i <= l; i++) {
		memset(T, 0, hLen);

		for (u = 1; u <= c ; u++) {
			if (u == 1) {
				memcpy(tmp, S, Slen);
				tmp[Slen + 0] = (i & 0xff000000) >> 24;
				tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
				tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
				tmp[Slen + 3] = (i & 0x000000ff) >> 0;

				if (crypt_hmac_write(hmac, tmp, tmplen))
					goto out;
			} else {
				if (crypt_hmac_write(hmac, U, hLen))
					goto out;
			}

			if (crypt_hmac_final(hmac, U, hLen))
				goto out;

			for (k = 0; (unsigned int) k < hLen; k++)
				T[k] ^= U[k];
		}
void KVServer::annotate(const char *label, KeyVal* kv)
{
	uint32_t hash131 = hash_buf(kv->val.bytes, kv->val.len);
	fprintf(stderr, "\t%s: key %d len %d hash131 %08x\n",
		label, (int) kv->key, kv->val.len, hash131);
}
uint32_t SockaddrHashable::hash()
{
	return hash_buf(sockaddr, sockaddr_len);
}