// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movl %edx, -0x34(%rbp)
movq %rsi, %r12
movq %rdi, %r14
leaq -0x2c(%rbp), %rsi
andl $0x0, (%rsi)
callq 0x2483
testq %rax, %rax
je 0x2aad
movq %rax, %rbx
movl -0x2c(%rbp), %edx
leaq 0x860(%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
andq $0x0, (%r12)
leaq -0x30(%rbp), %r14
andl $0x0, (%r14)
leaq 0x902(%rip), %rdi       # 0x3189
callq 0x2270
movl -0x2c(%rbp), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %r8
movq %r14, %r9
callq 0x2750
leaq -0x520(%rbp), %r12
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
leaq 0x7e7(%rip), %rsi       # 0x30a7
movq %r12, %rdi
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2080
movq %r12, %rdi
callq 0x20d0
movq %rax, %r15
leaq -0x1530(%rbp), %r14
movl $0x1002, %edx           # imm = 0x1002
movq %r14, %rdi
xorl %esi, %esi
callq 0x2110
leaq -0x120(%rbp), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
movb $0x0, 0x20(%r13)
movl -0x2c(%rbp), %esi
movq %rbx, %rdi
movq %r13, %rdx
callq 0x23dc
leaq 0x78e(%rip), %rdi       # 0x30ad
movq %r13, %rsi
xorl %eax, %eax
callq 0x2060
leaq -0x30(%rbp), %rax
movl (%rax), %esi
leaq 0x78d(%rip), %rdi       # 0x30c3
xorl %eax, %eax
callq 0x2060
leaq 0x861(%rip), %rdi       # 0x31a5
callq 0x2270
leaq -0x60(%rbp), %r8
movq -0x50(%rbp), %rdi
movq %r12, %rsi
movl %r15d, %edx
movq %r14, %rcx
callq 0x25b5
leaq 0x85b(%rip), %rdi       # 0x31c1
callq 0x2270
movq %rbx, %rdi
callq 0x2230
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x20a0
movl %eax, %ebx
leaq -0xf0(%rbp), %r15
andq $0x0, 0x8(%r15)
movw $0x2, (%r15)
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x21c0
movl %eax, 0x4(%r15)
movl -0x34(%rbp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r15)
leaq 0x723(%rip), %rdi       # 0x30db
movq %r12, %rsi
xorl %eax, %eax
callq 0x2060
pushq $0x10
popq %rdx
movl %ebx, %edi
movq %r15, %rsi
callq 0x2190
cmpl $-0x1, %eax
je 0x2ac0
leaq 0x80d(%rip), %rdi       # 0x31ec
callq 0x2270
leaq -0x5c(%rbp), %r15
movl $0xf0f0f0f0, (%r15)     # imm = 0xF0F0F0F0
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x20d0
cmpq $0x7f, %rax
pushq $0x7f
popq %rdx
cmovbq %rax, %rdx
leaq -0xe0(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2180
pushq $0x4
popq %r13
movl %ebx, %edi
movq %r15, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20f0
leaq -0x30(%rbp), %r14
movl %ebx, %edi
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20f0
movl $0x80, %edx
movl %ebx, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x20f0
leaq -0x2c(%rbp), %rsi
movl %ebx, %edi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20f0
leaq -0x120(%rbp), %rsi
pushq $0x20
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20f0
leaq -0x1530(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20f0
movq -0x58(%rbp), %r15
movl (%r14), %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20f0
movl %ebx, %edi
callq 0x22c0
movq %r15, %rdi
callq 0x2210
xorl %eax, %eax
jmp 0x2b05
leaq 0x5b4(%rip), %rdi       # 0x3068
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
jmp 0x2b02
callq 0x2050
movl (%rax), %esi
cmpl $0x71, %esi
je 0x2af6
cmpl $0x6e, %esi
je 0x2ae4
cmpl $0x6f, %esi
je 0x2aed
cmpl $0x65, %esi
jne 0x2b17
leaq 0x7a4(%rip), %rdi       # 0x3286
jmp 0x2afd
leaq 0x778(%rip), %rdi       # 0x3263
jmp 0x2afd
leaq 0x74f(%rip), %rdi       # 0x3243
jmp 0x2afd
leaq 0x707(%rip), %rdi       # 0x3204
callq 0x2270
pushq $-0x1
popq %rax
addq $0x1508, %rsp           # imm = 0x1508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5da(%rip), %rdi       # 0x30f8
xorl %eax, %eax
callq 0x2060
jmp 0x2b02