// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x290, %rsp            # imm = 0x290
movq %rdx, %rbx
movq %rsi, %r15
movl %edi, %ebp
movl $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
callq 0x6850
testl %eax, %eax
je 0x4f6d
movl %eax, %r14d
callq 0x2090
leaq 0x80f8(%rip), %rsi      # 0xd060
jmp 0x51ff
leaq 0x10(%rsp), %rsi
movl $0x10, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
jne 0x51a9
movq %rsp, %rsi
movl $0x10, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
jne 0x51bf
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rdx
movl $0x10, %esi
callq 0x6aa4
movq %rsp, %rdi
leaq 0x50(%rsp), %rdx
movl $0x10, %esi
callq 0x6aa4
xorl %eax, %eax
movb 0x50(%rsp,%rax), %cl
xorb 0x70(%rsp,%rax), %cl
movb %cl, 0x40(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x4fcd
leaq 0x40(%rsp), %r12
leaq 0x30(%rsp), %r14
movq %r15, %rdi
movq %r12, %rsi
movl $0x10, %edx
movl $0x10000, %ecx          # imm = 0x10000
movq %r14, %r8
callq 0x4e70
leaq 0x80e6(%rip), %rdi      # 0xd0f1
leaq 0x10(%rsp), %rsi
callq 0x5231
leaq 0x80e3(%rip), %rdi      # 0xd0ff
movq %rsp, %rsi
callq 0x5231
leaq 0x80e4(%rip), %rdi      # 0xd10f
movq %r12, %rsi
callq 0x5231
leaq 0x80da(%rip), %rdi      # 0xd114
movq %r14, %rsi
callq 0x5231
movl $0xf, %eax
incb (%rsp,%rax)
sete %cl
testq %rax, %rax
je 0x5059
decq %rax
testb %cl, %cl
jne 0x5047
leaq 0x80b8(%rip), %rdi      # 0xd118
movq %rsp, %r14
movq %r14, %rsi
callq 0x5231
leaq 0x98(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movl $0x80, %edx
callq 0x4a35
leaq 0x20(%rsp), %r12
movl $0x10, %edx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x4a8b
leaq 0x807e(%rip), %rdi      # 0xd122
movq %r12, %rsi
callq 0x5231
movl $0x10, %edx
movl %ebp, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
jne 0x51d5
leaq 0x20(%rsp), %rsi
movl $0x10, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
jne 0x51eb
leaq 0x80ae(%rip), %rdi      # 0xd199
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x5231
leaq 0x98(%rsp), %r15
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
movl $0x80, %edx
callq 0x4a60
movq %rsp, %rcx
movl $0x10, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x4ad8
movl $0xf, %eax
incb 0x10(%rsp,%rax)
sete %cl
testq %rax, %rax
je 0x513d
decq %rax
testb %cl, %cl
jne 0x512a
leaq 0x8063(%rip), %rdi      # 0xd1a7
movq %rsp, %r14
movq %r14, %rsi
callq 0x5231
leaq 0x805f(%rip), %rdi      # 0xd1b5
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x5231
movdqa (%r14), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
xorl %ebp, %ebp
cmpl $0xffff, %eax           # imm = 0xFFFF
setne %bpl
je 0x5223
callq 0x2090
leaq 0x802f(%rip), %rsi      # 0xd1bd
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x1, %edi
movl %eax, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x530c
jmp 0x5226
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7ed7(%rip), %rsi      # 0xd094
jmp 0x51ff
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7eee(%rip), %rsi      # 0xd0c1
jmp 0x51ff
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7f49(%rip), %rsi      # 0xd132
jmp 0x51ff
callq 0x2050
movl (%rax), %r14d
callq 0x2090
leaq 0x7f65(%rip), %rsi      # 0xd164
movl $0x1, %edi
movl %eax, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x530c
movl %r14d, %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
xorl %eax, %eax
testl %ebp, %ebp
sete %al
movl %eax, (%rbx)
jmp 0x5210