// File: /workspace/github_cpp_makefile_star2_O3/akw0088[P]openssl/aes_upload_reverse.cpp
// Function: aes_file_upload_reverse(char*, char*, unsigned short, unsigned char*, unsigned char*, unsigned char*)
int aes_file_upload_reverse(char *file, char *ip_str, unsigned short port, unsigned char *key, unsigned char *iv, unsigned char *public_key_filename)
{
	struct sockaddr_in	servaddr;
	SOCKET sock;
	int ret;


	unsigned int file_size = 0;
	unsigned char *data = (unsigned char *)get_file(file, &file_size);
	if (data == NULL)
	{
		printf("Error: could not open file %s\r\n", file);
		return -1;
	}
	printf("Opened file %s size %d bytes\r\n", file, file_size);

	char file_name[128] = { 0 };

	unsigned char *ciphertext = NULL;
	unsigned int ciphertext_len = 0;

	printf("Performing AES encryptions\r\n");
	aes_encrypt(key, iv, data, file_size, ciphertext, ciphertext_len);

	char rsa_plaintext[1024] = {0};

	sprintf(rsa_plaintext, "%s %s", iv, key);

	int rsa_plaintext_len = strlen(rsa_plaintext);

	unsigned char encrypted[4098] = {0};
	unsigned int encrypted_length = 0;
	unsigned char hash[33] = {0};

	md5sum((char *)data, file_size, (char *)&hash[0]);
	printf("Unencrypted MD5: %s\r\n", hash);
	printf("AES Encrypted size %d\r\n", ciphertext_len);

	printf("Performing RSA encryptions\r\n");
	rsa_encrypt((char *)public_key_filename, (unsigned char *)&rsa_plaintext[0], rsa_plaintext_len, encrypted, encrypted_length);


	printf("Encryption complete, connecting to server\r\n");
	delete[] data;

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	memset(&servaddr, 0, sizeof(struct sockaddr_in));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = inet_addr(ip_str);
	servaddr.sin_port = htons(port);

	// 3 way handshake
	printf("Attempting to connect to %s\n", ip_str);
	ret = connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr_in));
	if (ret == SOCKET_ERROR)
	{
#ifdef _WIN32
		ret = WSAGetLastError();

		switch (ret)
		{
		case WSAETIMEDOUT:
			printf("Fatal Error: Connection timed out.\n");
			break;
		case WSAECONNREFUSED:
			printf("Fatal Error: Connection refused\n");
			break;
		case WSAEHOSTUNREACH:
			printf("Fatal Error: Router sent ICMP packet (destination unreachable)\n");
			break;
		default:
			printf("Fatal Error: %d\n", ret);
			break;
		}
#else
		ret = errno;

	        switch(ret)
	        {
		case ENETUNREACH:
			printf("Fatal Error: The network is unreachable from this host at this time.\n(Bad IP address)\n");
			break;
	        case ETIMEDOUT:
	                printf("Fatal Error: Connecting timed out.\n");
	                break;
	        case ECONNREFUSED:
	                printf("Fatal Error: Connection refused\n");
	                break;
	        case EHOSTUNREACH:
	                printf("Fatal Error: router sent ICMP packet (destination unreachable)\n");
	                break;
	        default:
	                printf("Fatal Error: %d\n", ret);
	                break;
	        }
#endif
		return -1;
	}
	printf("TCP handshake completed\n");

	unsigned int magic = 0xF0F0F0F0;

	memcpy(file_name, file, MIN(127, strlen(file)));
	send(sock, (char *)&magic, sizeof(int), 0);
	send(sock, (char *)&ciphertext_len, sizeof(int), 0);
	send(sock, (char *)&file_name, 128, 0);
	send(sock, (char *)&file_size, sizeof(int), 0);
	send(sock, (char *)&hash, 32, 0);
	send(sock, (char *)&encrypted, 256, 0);
	send(sock, ciphertext, ciphertext_len, 0);
	closesocket(sock);
	free((void *)ciphertext);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1508, %rsp           # imm = 0x1508
movq %r9, -0x50(%rbp)
movq %r8, %r15
movq %rcx, %rbx
movl %edx, -0x34(%rbp)
movq %rsi, %r12
movq %rdi, %r14
leaq -0x2c(%rbp), %rsi
movl $0x0, (%rsi)
callq 0x2481
testq %rax, %rax
je 0x2aea
movq %rax, %r13
movl -0x2c(%rbp), %edx
leaq 0x838(%rip), %rdi       # 0x3088
movq %r14, -0x40(%rbp)
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movq %r12, -0x48(%rbp)
leaq -0x58(%rbp), %r12
xorl %eax, %eax
movq %rax, (%r12)
leaq -0x30(%rbp), %r14
movl $0x0, (%r14)
leaq 0x8d6(%rip), %rdi       # 0x3189
callq 0x2270
movl -0x2c(%rbp), %ecx
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %r8
movq %r14, %r9
callq 0x2771
leaq -0x520(%rbp), %r12
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
leaq 0x7bb(%rip), %rsi       # 0x30a7
movq %r12, %rdi
movq %r15, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2080
movq %r12, %rdi
callq 0x20d0
movq %rax, %rbx
leaq -0x1530(%rbp), %r14
movl $0x1002, %edx           # imm = 0x1002
movq %r14, %rdi
xorl %esi, %esi
callq 0x2110
leaq -0x120(%rbp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movb $0x0, 0x20(%r15)
movl -0x2c(%rbp), %esi
movq %r13, %rdi
movq %r15, %rdx
callq 0x23dc
leaq 0x763(%rip), %rdi       # 0x30ad
movq %r15, %rsi
xorl %eax, %eax
callq 0x2060
leaq -0x30(%rbp), %rax
movl (%rax), %esi
leaq 0x762(%rip), %rdi       # 0x30c3
xorl %eax, %eax
callq 0x2060
leaq 0x836(%rip), %rdi       # 0x31a5
callq 0x2270
leaq -0x60(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %r12, %rsi
xorl %r12d, %r12d
movl %ebx, %edx
movq %r14, %rcx
callq 0x25bc
leaq 0x82e(%rip), %rdi       # 0x31c1
callq 0x2270
movq %r13, %rdi
callq 0x2230
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x20a0
movl %eax, %r13d
leaq -0xf0(%rbp), %rbx
movq %r12, 0x8(%rbx)
movw $0x2, (%rbx)
movq -0x48(%rbp), %r15
movq %r15, %rdi
callq 0x21c0
movl %eax, 0x4(%rbx)
movl -0x34(%rbp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
leaq 0x6f3(%rip), %rdi       # 0x30db
movq %r15, %rsi
xorl %eax, %eax
callq 0x2060
movl %r13d, %edi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x2190
cmpl $-0x1, %eax
je 0x2afd
leaq 0x7da(%rip), %rdi       # 0x31ec
callq 0x2270
leaq -0x5c(%rbp), %rbx
movl $0xf0f0f0f0, (%rbx)     # imm = 0xF0F0F0F0
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x20d0
cmpq $0x7f, %rax
movl $0x7f, %edx
cmovbq %rax, %rdx
leaq -0xe0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2180
movl $0x4, %edx
movl %r13d, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x20f0
leaq -0x30(%rbp), %rbx
movl $0x4, %edx
movl %r13d, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x20f0
movl $0x80, %edx
movl %r13d, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20f0
leaq -0x2c(%rbp), %rsi
movl $0x4, %edx
movl %r13d, %edi
xorl %ecx, %ecx
callq 0x20f0
leaq -0x120(%rbp), %rsi
movl $0x20, %edx
movl %r13d, %edi
xorl %ecx, %ecx
callq 0x20f0
leaq -0x1530(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
movl %r13d, %edi
xorl %ecx, %ecx
callq 0x20f0
movq -0x58(%rbp), %r14
movl (%rbx), %edx
movl %r13d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x20f0
movl %r13d, %edi
callq 0x22c0
movq %r14, %rdi
callq 0x2210
jmp 0x2b5a
leaq 0x577(%rip), %rdi       # 0x3068
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
jmp 0x2b54
callq 0x2050
movl (%rax), %esi
cmpl $0x6e, %esi
jg 0x2b1c
cmpl $0x65, %esi
je 0x2b3f
cmpl $0x6e, %esi
jne 0x2b2f
leaq 0x749(%rip), %rdi       # 0x3263
jmp 0x2b4f
cmpl $0x6f, %esi
je 0x2b48
cmpl $0x71, %esi
jne 0x2b2f
leaq 0x6d7(%rip), %rdi       # 0x3204
jmp 0x2b4f
leaq 0x5c2(%rip), %rdi       # 0x30f8
xorl %eax, %eax
callq 0x2060
jmp 0x2b54
leaq 0x740(%rip), %rdi       # 0x3286
jmp 0x2b4f
leaq 0x6f4(%rip), %rdi       # 0x3243
callq 0x2270
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x1508, %rsp           # imm = 0x1508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq