// File: /workspace/github_c_makefile_star2_O2/MartinDrab[P]netpipe/auth.c
// Function: AuthSocket
int AuthSocket(SOCKET Socket, const char *Password, int *Success)
{
	int ret = 0;
	unsigned char ourChallenge[16];
	unsigned char ourChallengeHash[32];
	unsigned char otherChallenge[16];
	unsigned char otherChallengeHash[32];
	unsigned char salt[16];
	unsigned char key[16];
	unsigned char tmp[16];
	AES_Ctx aesCtx;

	*Success = 0;
	ret = _generateSalt(ourChallenge);
	if (ret != 0) {
		LogError("[AUTH]: Unable to generate our challenge: %i", ret);;
		goto Exit;
	}

	if (send(Socket, ourChallenge, sizeof(ourChallenge), 0) != sizeof(ourChallenge)) {
		ret = errno;
		LogError("[AUTH]: Failed to sned challenge: %i\n", ret);;
		goto Exit;
	}

	if (recv(Socket, otherChallenge, sizeof(otherChallenge), 0) != sizeof(otherChallenge)) {
		ret = errno;
		LogError("[AUTH]: Failed to receive challenge: %i\n", ret);;
		goto Exit;
	}

	sha256(ourChallenge, sizeof(ourChallenge), ourChallengeHash);
	sha256(otherChallenge, sizeof(otherChallenge), otherChallengeHash);
	for (size_t i = 0; i < sizeof(salt) / sizeof(salt[0]); ++i)
		salt[i] = ourChallengeHash[i] ^ otherChallengeHash[i];

	AuthKeyGen(Password, salt, sizeof(salt), 65536, key);
	_print16("our challenge", ourChallenge);
	_print16("other challenge", otherChallenge);
	_print16("salt", salt);
	_print16("key", key);

	_increment16(otherChallenge);
	_print16("other inc", otherChallenge);
	AES_SetupEncrypt(&aesCtx, key, sizeof(key) * 8);
	AES_Encrypt(&aesCtx, otherChallenge, sizeof(otherChallenge), tmp);
	_print16("other encrypted", tmp);
	if (send(Socket, tmp, sizeof(tmp), 0) != sizeof(tmp)) {
		ret = errno;
		LogError("[AUTH]: Failed to send encrypted info: %i\n", ret);
		goto Exit;
	}

	if (recv(Socket, tmp, sizeof(tmp), 0) != sizeof(tmp)) {
		ret = errno;
		LogError("[AUTH]: Failed to receive encrypted info: %u\n", ret);
		goto Exit;
	}
	
	ret = 0;
	_print16("our encrypted", tmp);
	AES_SetupDecrypt(&aesCtx, key, sizeof(key) * 8);
	AES_Decrypt(&aesCtx, tmp, sizeof(tmp), otherChallenge);
	_increment16(ourChallenge);
	_print16("our decrypted", otherChallenge);
	_print16("our inc", ourChallenge);
	ret = memcmp(otherChallenge, ourChallenge, sizeof(otherChallenge));
	if (ret != 0) {
		ret = -1;
		LogError("[AUTH]: Authentication failed: %i\n", ret);;
	}

	*Success = (ret == 0);
Exit:
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdx, %rbx
movq %rsi, %r15
movl %edi, %r14d
andl $0x0, (%rdx)
movq %rsp, %rdi
pushq $0x10
popq %rsi
callq 0x6524
testl %eax, %eax
je 0x4cde
movl %eax, %ebp
callq 0x2090
leaq 0x8387(%rip), %rsi      # 0xd060
jmp 0x4f44
movq %rsp, %rsi
pushq $0x10
popq %rdx
movl %r14d, %edi
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
jne 0x4d48
leaq 0x10(%rsp), %rsi
pushq $0x10
popq %rdx
movl %r14d, %edi
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
jne 0x4d60
movq %rsp, %rdi
pushq $0x10
popq %rbp
leaq 0x70(%rsp), %rdx
movl %ebp, %esi
callq 0x6771
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movl %ebp, %esi
callq 0x6771
xorl %eax, %eax
cmpq $0x10, %rax
je 0x4d78
movb 0x50(%rsp,%rax), %cl
xorb 0x70(%rsp,%rax), %cl
movb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x4d31
callq 0x2050
movl (%rax), %ebp
callq 0x2090
leaq 0x8339(%rip), %rsi      # 0xd094
jmp 0x4f44
callq 0x2050
movl (%rax), %ebp
callq 0x2090
leaq 0x834e(%rip), %rsi      # 0xd0c1
jmp 0x4f44
leaq 0x40(%rsp), %r13
pushq $0x10
popq %rdx
leaq 0x30(%rsp), %r12
movq %r15, %rdi
movq %r13, %rsi
movl $0x10000, %ecx          # imm = 0x10000
movq %r12, %r8
callq 0x4bf0
leaq 0x8352(%rip), %rdi      # 0xd0f1
movq %rsp, %rsi
callq 0x4f66
leaq 0x8351(%rip), %rdi      # 0xd0ff
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x4f66
leaq 0x834d(%rip), %rdi      # 0xd10f
movq %r13, %rsi
callq 0x4f66
leaq 0x8343(%rip), %rdi      # 0xd114
movq %r12, %rsi
callq 0x4f66
movq %r15, %rdi
callq 0x503d
leaq 0x8330(%rip), %rdi      # 0xd118
movq %r15, %rsi
callq 0x4f66
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movl $0x80, %edx
callq 0x47fa
pushq $0x10
popq %r12
leaq 0x20(%rsp), %rbp
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x484a
leaq 0x82f9(%rip), %rdi      # 0xd122
movq %rbp, %rsi
callq 0x4f66
movl %r14d, %edi
movq %rbp, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
jne 0x4f1c
leaq 0x20(%rsp), %rsi
pushq $0x10
popq %rdx
xorl %ebp, %ebp
movl %r14d, %edi
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
jne 0x4f31
leaq 0x8329(%rip), %rdi      # 0xd199
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x4f66
leaq 0x90(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movl $0x80, %edx
callq 0x4822
pushq $0x10
popq %rdx
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x4894
movq %rsp, %r14
movq %r14, %rdi
callq 0x503d
leaq 0x82e8(%rip), %rdi      # 0xd1a7
movq %r12, %rsi
callq 0x4f66
leaq 0x82e7(%rip), %rdi      # 0xd1b5
movq %r14, %rsi
callq 0x4f66
movdqa (%r12), %xmm0
pcmpeqb (%r14), %xmm0
pmovmskb %xmm0, %eax
xorl %r14d, %r14d
cmpl $0xffff, %eax           # imm = 0xFFFF
setne %r14b
je 0x4f10
callq 0x2090
leaq 0x82be(%rip), %rsi      # 0xd1bd
pushq $0x1
popq %rdi
pushq $-0x1
popq %rbp
movl %eax, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x5054
xorl %eax, %eax
testl %r14d, %r14d
sete %al
movl %eax, (%rbx)
jmp 0x4f52
callq 0x2050
movl (%rax), %ebp
callq 0x2090
leaq 0x8203(%rip), %rsi      # 0xd132
jmp 0x4f44
callq 0x2050
movl (%rax), %ebp
callq 0x2090
leaq 0x8220(%rip), %rsi      # 0xd164
pushq $0x1
popq %rdi
movl %eax, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x5054
movl %ebp, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq