// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movl $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
callq 0x676c
testl %eax, %eax
je 0x4f43
movl %eax, %r14d
callq 0x2090
leaq 0x8122(%rip), %rsi      # 0xd060
jmp 0x51d7
leaq 0x10(%rsp), %rsi
movl $0x10, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
jne 0x5181
movq %rsp, %rsi
movl $0x10, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
jne 0x5197
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %r12
movl $0x10, %esi
movq %r12, %rdx
callq 0x69d5
movq %rsp, %r13
leaq 0x50(%rsp), %r14
movq %r13, %rdi
movl $0x10, %esi
movq %r14, %rdx
callq 0x69d5
movdqa (%r14), %xmm0
pxor (%r12), %xmm0
leaq 0x40(%rsp), %r14
movdqa %xmm0, (%r14)
leaq 0x30(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movl $0x10, %edx
movl $0x10000, %ecx          # imm = 0x10000
movq %r12, %r8
callq 0x4e44
leaq 0x810e(%rip), %rdi      # 0xd0f1
leaq 0x10(%rsp), %rsi
callq 0x520b
leaq 0x810b(%rip), %rdi      # 0xd0ff
movq %r13, %rsi
callq 0x520b
leaq 0x810c(%rip), %rdi      # 0xd10f
movq %r14, %rsi
callq 0x520b
leaq 0x8102(%rip), %rdi      # 0xd114
movq %r12, %rsi
callq 0x520b
movl $0xf, %eax
incb (%rsp,%rax)
sete %cl
testq %rax, %rax
je 0x5031
decq %rax
testb %cl, %cl
jne 0x501f
leaq 0x80e0(%rip), %rdi      # 0xd118
movq %rsp, %r14
movq %r14, %rsi
callq 0x520b
leaq 0x90(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movl $0x80, %edx
callq 0x4a2e
leaq 0x20(%rsp), %r12
movl $0x10, %edx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x4a84
leaq 0x80a6(%rip), %rdi      # 0xd122
movq %r12, %rsi
callq 0x520b
movl $0x10, %edx
movl %ebp, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
jne 0x51ad
leaq 0x20(%rsp), %rsi
movl $0x10, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
jne 0x51c3
leaq 0x80d6(%rip), %rdi      # 0xd199
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x520b
leaq 0x90(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movl $0x80, %edx
callq 0x4a59
movq %rsp, %rcx
movl $0x10, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x4ad1
movl $0xf, %eax
incb 0x10(%rsp,%rax)
sete %cl
testq %rax, %rax
je 0x5115
decq %rax
testb %cl, %cl
jne 0x5102
leaq 0x808b(%rip), %rdi      # 0xd1a7
movq %rsp, %r14
movq %r14, %rsi
callq 0x520b
leaq 0x8087(%rip), %rdi      # 0xd1b5
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x520b
movdqa (%r14), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
xorl %ebp, %ebp
cmpl $0xffff, %eax           # imm = 0xFFFF
setne %bpl
je 0x51fd
callq 0x2090
leaq 0x8057(%rip), %rsi      # 0xd1bd
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x1, %edi
movl %eax, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x52e4
jmp 0x5200
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7eff(%rip), %rsi      # 0xd094
jmp 0x51d7
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7f16(%rip), %rsi      # 0xd0c1
jmp 0x51d7
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7f71(%rip), %rsi      # 0xd132
jmp 0x51d7
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7f8d(%rip), %rsi      # 0xd164
movl $0x1, %edi
movl %eax, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x52e4
movl %r14d, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movl %eax, (%rbx)
jmp 0x51e8