// File: /workspace/github_cpp_makefile_star2_O1/akw0088[P]openssl/aes_upload.cpp
// Function: aes_file_upload(char*, unsigned short, unsigned char*, unsigned char*, unsigned char*)
int aes_file_upload(char *file, unsigned short port, unsigned char *key, unsigned char *iv, unsigned char *public_key_filename)
{
	int			connfd;
	unsigned int		size = sizeof(struct sockaddr_in);
	struct sockaddr_in	servaddr, client;
	time_t			ticks;
	int listenfd;

#ifdef _WIN32
	WSADATA		WSAData;

	WSAStartup(MAKEWORD(2, 0), &WSAData);
#endif

	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd == -1)
	{
		perror("socket error");
		return -1;
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(port);

	if ((::bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) == -1)
	{
		perror("bind error");
		return 0;
	}

	printf("Server listening on: %s:%d\n", inet_ntoa(servaddr.sin_addr), htons(servaddr.sin_port));

	if (listen(listenfd, 3) == -1)
	{
		perror("listen error");
		return -1;
	}

	for (;;)
	{
		char response[1024] = { 0 };

		printf("listening for connections...\n");
		connfd = accept(listenfd, (struct sockaddr *)&client, (socklen_t *)&size);
		if (connfd == INVALID_SOCKET)
			continue;

		ticks = time(NULL);
		snprintf(response, sizeof(response), "%.24s\r\n", ctime(&ticks));
		printf("Client: %s - %s\r\n", inet_ntoa(client.sin_addr), response);

		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);
			continue;
		}
		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);

		delete[] data;
		printf("Encryption complete, sending file\r\n");
		memcpy(file_name, file, MIN(127, strlen(file)));
		send(connfd, (char *)&ciphertext_len, sizeof(int), 0);
		send(connfd, (char *)&file_name, 128, 0);
		send(connfd, (char *)&file_size, sizeof(int), 0);
		send(connfd, (char *)&hash, 32, 0);
		send(connfd, (char *)&encrypted, 256, 0);
		send(connfd, ciphertext, ciphertext_len, 0);
		free((void *)ciphertext);
		closesocket(connfd);
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1928, %rsp           # imm = 0x1928
movq %r8, -0x68(%rbp)
movq %rcx, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
movl %esi, %r14d
movq %rdi, %r13
movl $0x10, -0x3c(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20a0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x2beb
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rsi
movaps %xmm0, (%rsi)
movw $0x2, (%rsi)
movl $0x0, 0x4(%rsi)
rolw $0x8, %r14w
movw %r14w, 0x2(%rsi)
movl -0x34(%rbp), %edi
movl $0x10, %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x2bf4
movl -0x7c(%rbp), %edi
callq 0x2100
movzwl -0x7e(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x79f(%rip), %rdi       # 0x3080
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
movl -0x34(%rbp), %edi
movl $0x3, %esi
callq 0x2220
cmpl $-0x1, %eax
je 0x2bff
leaq -0x940(%rbp), %rbx
leaq -0x2c(%rbp), %r14
movq %r13, -0x50(%rbp)
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
leaq 0x89f(%rip), %rdi       # 0x31c5
callq 0x22a0
movl -0x34(%rbp), %edi
leaq -0x90(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq 0x20b0
cmpl $-0x1, %eax
je 0x2910
movl %eax, %r15d
xorl %edi, %edi
callq 0x21d0
movq %rax, -0x70(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x20d0
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
leaq 0x740(%rip), %rdx       # 0x30a9
movq %rax, %rcx
xorl %eax, %eax
callq 0x22d0
movl -0x8c(%rbp), %edi
callq 0x2100
leaq 0x72c(%rip), %rdi       # 0x30b1
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2050
movl $0x0, -0x2c(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x24c1
testq %rax, %rax
je 0x2bd1
movq %rax, %r14
movl -0x2c(%rbp), %edx
leaq 0x729(%rip), %rdi       # 0x30e3
movq %r13, %rsi
xorl %eax, %eax
callq 0x2050
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movq $0x0, -0x48(%rbp)
xorl %eax, %eax
movl %eax, -0x30(%rbp)
leaq 0x7cf(%rip), %rdi       # 0x31e2
callq 0x22a0
movl -0x2c(%rbp), %ecx
movq -0x58(%rbp), %rbx
movq %rbx, %rdi
movq -0x60(%rbp), %r13
movq %r13, %rsi
movq %r14, %rdx
leaq -0x48(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x27af
movl $0x400, %edx            # imm = 0x400
leaq -0x540(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2140
movq %r12, %rdi
leaq 0x6a9(%rip), %rsi       # 0x3102
movq %r13, %rdx
movq %rbx, %rcx
leaq -0x940(%rbp), %rbx
xorl %eax, %eax
callq 0x2080
movq %r12, %rdi
callq 0x20f0
movq %rax, %r13
movl $0x1002, %edx           # imm = 0x1002
leaq -0x1950(%rbp), %rdi
xorl %esi, %esi
callq 0x2140
xorl %eax, %eax
movl %eax, -0x38(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movb $0x0, -0xa0(%rbp)
movl -0x2c(%rbp), %esi
movq %r14, %rdi
leaq -0xc0(%rbp), %r12
movq %r12, %rdx
callq 0x241c
leaq 0x644(%rip), %rdi       # 0x3108
movq %r12, %rsi
xorl %eax, %eax
callq 0x2050
movl -0x30(%rbp), %esi
leaq 0x646(%rip), %rdi       # 0x311e
xorl %eax, %eax
callq 0x2050
leaq 0x718(%rip), %rdi       # 0x31fe
callq 0x22a0
movq -0x68(%rbp), %rdi
leaq -0x540(%rbp), %rsi
movl %r13d, %edx
movq -0x50(%rbp), %r13
leaq -0x1950(%rbp), %rcx
leaq -0x38(%rbp), %r8
callq 0x25fa
movq %r14, %rdi
callq 0x2260
leaq 0x6fe(%rip), %rdi       # 0x321a
callq 0x22a0
movq %r13, %rdi
callq 0x20f0
cmpq $0x7f, %rax
movl $0x7f, %ecx
cmovaeq %rcx, %rax
leaq -0x140(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x21b0
movl $0x4, %edx
movl %r15d, %edi
leaq -0x30(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2120
movl $0x80, %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2120
movl $0x4, %edx
movl %r15d, %edi
leaq -0x2c(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2120
movl $0x20, %edx
movl %r15d, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x2120
movl $0x100, %edx            # imm = 0x100
movl %r15d, %edi
leaq -0x1950(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2120
movq -0x48(%rbp), %r14
movl -0x30(%rbp), %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2120
movq %r14, %rdi
callq 0x2240
movl %r15d, %edi
callq 0x2300
jmp 0x2be2
leaq 0x4eb(%rip), %rdi       # 0x30c3
movq %r13, %rsi
xorl %eax, %eax
callq 0x2050
leaq -0x2c(%rbp), %r14
jmp 0x2910
leaq 0x476(%rip), %rdi       # 0x3068
jmp 0x2c06
leaq 0x47a(%rip), %rdi       # 0x3075
xorl %ebx, %ebx
jmp 0x2c06
leaq 0x496(%rip), %rdi       # 0x309c
callq 0x21c0
movl %ebx, %eax
addq $0x1928, %rsp           # imm = 0x1928
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq