// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
leaq -0x30(%rbp), %rdi
callq 0x6eb0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x6b6e
callq 0x2090
movl %eax, %edx
movl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movl $0x1, %edi
leaq 0x94fe(%rip), %rsi      # 0x10060
movb $0x0, %al
callq 0x7070
jmp 0x6e9d
movl -0x4(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
je 0x6bb6
callq 0x2050
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
callq 0x2090
movl %eax, %edx
movl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movl $0x1, %edi
leaq 0x94ea(%rip), %rsi      # 0x10094
movb $0x0, %al
callq 0x7070
jmp 0x6e9d
movl -0x4(%rbp), %edi
leaq -0x60(%rbp), %rsi
movl $0x10, %edx
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
je 0x6bfe
callq 0x2050
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
callq 0x2090
movl %eax, %edx
movl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movl $0x1, %edi
leaq 0x94cf(%rip), %rsi      # 0x100c1
movb $0x0, %al
callq 0x7070
jmp 0x6e9d
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movl $0x10, %esi
callq 0x9550
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movl $0x10, %esi
callq 0x9550
movq $0x0, -0x2b0(%rbp)
cmpq $0x10, -0x2b0(%rbp)
jae 0x6c75
movq -0x2b0(%rbp), %rax
movzbl -0x50(%rbp,%rax), %eax
movq -0x2b0(%rbp), %rcx
movzbl -0x80(%rbp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movq -0x2b0(%rbp), %rax
movb %cl, -0x90(%rbp,%rax)
movq -0x2b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x2b0(%rbp)
jmp 0x6c2d
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0xa0(%rbp), %r8
movl $0x10, %edx
movl $0x10000, %ecx          # imm = 0x10000
callq 0x69f0
leaq -0x30(%rbp), %rsi
leaq 0x9450(%rip), %rdi      # 0x100f1
callq 0x6ed0
leaq -0x60(%rbp), %rsi
leaq 0x944e(%rip), %rdi      # 0x100ff
callq 0x6ed0
leaq -0x90(%rbp), %rsi
leaq 0x944b(%rip), %rdi      # 0x1010f
callq 0x6ed0
leaq -0xa0(%rbp), %rsi
leaq 0x943d(%rip), %rdi      # 0x10114
callq 0x6ed0
leaq -0x60(%rbp), %rdi
callq 0x7010
leaq -0x60(%rbp), %rsi
leaq 0x9428(%rip), %rdi      # 0x10118
callq 0x6ed0
leaq -0xa0(%rbp), %rsi
leaq -0x2a8(%rbp), %rdi
movl $0x80, %edx
callq 0x63a0
leaq -0x60(%rbp), %rsi
leaq -0xb0(%rbp), %rcx
leaq -0x2a8(%rbp), %rdi
movl $0x10, %edx
callq 0x6440
leaq -0xb0(%rbp), %rsi
leaq 0x93eb(%rip), %rdi      # 0x10122
callq 0x6ed0
movl -0x4(%rbp), %edi
leaq -0xb0(%rbp), %rsi
movl $0x10, %edx
xorl %ecx, %ecx
callq 0x20d0
cmpq $0x10, %rax
je 0x6d87
callq 0x2050
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
callq 0x2090
movl %eax, %edx
movl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movl $0x1, %edi
leaq 0x93b7(%rip), %rsi      # 0x10132
movb $0x0, %al
callq 0x7070
jmp 0x6e9d
movl -0x4(%rbp), %edi
leaq -0xb0(%rbp), %rsi
movl $0x10, %edx
xorl %ecx, %ecx
callq 0x2040
cmpq $0x10, %rax
je 0x6dd2
callq 0x2050
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
callq 0x2090
movl %eax, %edx
movl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movl $0x1, %edi
leaq 0x939e(%rip), %rsi      # 0x10164
movb $0x0, %al
callq 0x7070
jmp 0x6e9d
movl $0x0, -0x1c(%rbp)
leaq -0xb0(%rbp), %rsi
leaq 0x93b2(%rip), %rdi      # 0x10199
callq 0x6ed0
leaq -0xa0(%rbp), %rsi
leaq -0x2a8(%rbp), %rdi
movl $0x80, %edx
callq 0x63f0
leaq -0xb0(%rbp), %rsi
leaq -0x60(%rbp), %rcx
leaq -0x2a8(%rbp), %rdi
movl $0x10, %edx
callq 0x64c0
leaq -0x30(%rbp), %rdi
callq 0x7010
leaq -0x60(%rbp), %rsi
leaq 0x9373(%rip), %rdi      # 0x101a7
callq 0x6ed0
leaq -0x30(%rbp), %rsi
leaq 0x9371(%rip), %rdi      # 0x101b5
callq 0x6ed0
leaq -0x60(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x2130
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x6e8b
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
callq 0x2090
movl %eax, %edx
movl -0x1c(%rbp), %ecx
addl $0x0, %ecx
movl $0x1, %edi
leaq 0x9339(%rip), %rsi      # 0x101bd
movb $0x0, %al
callq 0x7070
cmpl $0x0, -0x1c(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x1c(%rbp), %eax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
nopl (%rax)