// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1918, %rsp           # imm = 0x1918
movq %r8, -0x60(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, -0x50(%rbp)
movl %esi, %ebx
movq %rdi, %r13
movl $0x10, -0x38(%rbp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x20a0
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x2ba5
leaq -0x7c(%rbp), %rsi
andq $0x0, 0x8(%rsi)
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
rolw $0x8, %bx
movw %bx, 0x2(%rsi)
pushq $0x10
popq %rdx
movl -0x34(%rbp), %edi
callq 0x2070
cmpl $-0x1, %eax
je 0x2bae
movl -0x78(%rbp), %edi
callq 0x2100
movzwl -0x7a(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %edx
leaq 0x7d4(%rip), %rdi       # 0x3080
movq %rax, %rsi
xorl %eax, %eax
callq 0x2050
pushq $0x3
popq %rsi
movl -0x34(%rbp), %edi
callq 0x2220
cmpl $-0x1, %eax
je 0x2bbe
leaq -0x930(%rbp), %rbx
leaq -0x2c(%rbp), %r14
movq %r13, -0x48(%rbp)
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
leaq 0x8d6(%rip), %rdi       # 0x31c5
callq 0x22a0
movl -0x34(%rbp), %edi
leaq -0xb0(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x20b0
cmpl $-0x1, %eax
je 0x28d9
movl %eax, %r15d
xorl %edi, %edi
callq 0x21d0
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x20d0
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
leaq 0x777(%rip), %rdx       # 0x30a9
movq %rax, %rcx
xorl %eax, %eax
callq 0x22d0
movl -0xac(%rbp), %edi
callq 0x2100
leaq 0x763(%rip), %rdi       # 0x30b1
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2050
andl $0x0, -0x2c(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x24c3
testq %rax, %rax
je 0x2b8b
movq %rax, %r14
movl -0x2c(%rbp), %edx
leaq 0x763(%rip), %rdi       # 0x30e3
movq %r13, %rsi
xorl %eax, %eax
callq 0x2050
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
andq $0x0, -0x40(%rbp)
movaps %xmm0, -0x130(%rbp)
andl $0x0, -0x30(%rbp)
leaq 0x80d(%rip), %rdi       # 0x31e2
callq 0x22a0
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq -0x58(%rbp), %r13
movq %r13, %rsi
movq %r14, %rdx
leaq -0x40(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x2790
movl $0x400, %edx            # imm = 0x400
leaq -0x530(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2140
movq %r12, %rdi
leaq 0x6e7(%rip), %rsi       # 0x3102
movq %r13, %rdx
movq %rbx, %rcx
leaq -0x930(%rbp), %rbx
xorl %eax, %eax
callq 0x2080
movq %r12, %rdi
callq 0x20f0
movq %rax, %r13
movl $0x1002, %edx           # imm = 0x1002
leaq -0x1940(%rbp), %rdi
xorl %esi, %esi
callq 0x2140
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movb $0x0, -0x80(%rbp)
movl -0x2c(%rbp), %esi
movq %r14, %rdi
leaq -0xa0(%rbp), %r12
movq %r12, %rdx
callq 0x241c
leaq 0x68a(%rip), %rdi       # 0x3108
movq %r12, %rsi
xorl %eax, %eax
callq 0x2050
movl -0x30(%rbp), %esi
leaq 0x68c(%rip), %rdi       # 0x311e
xorl %eax, %eax
callq 0x2050
leaq 0x75e(%rip), %rdi       # 0x31fe
callq 0x22a0
movq -0x60(%rbp), %rdi
leaq -0x530(%rbp), %rsi
movl %r13d, %edx
movq -0x48(%rbp), %r13
leaq -0x1940(%rbp), %rcx
leaq -0x6c(%rbp), %r8
callq 0x25f5
movq %r14, %rdi
callq 0x2260
leaq 0x744(%rip), %rdi       # 0x321a
callq 0x22a0
movq %r13, %rdi
callq 0x20f0
cmpq $0x7f, %rax
pushq $0x7f
popq %rcx
cmovaeq %rcx, %rax
leaq -0x130(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x21b0
movl %r15d, %edi
leaq -0x30(%rbp), %rsi
pushq $0x4
popq %r12
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x2120
movl $0x80, %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2120
movl %r15d, %edi
leaq -0x2c(%rbp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x2120
movl %r15d, %edi
leaq -0xa0(%rbp), %rsi
pushq $0x20
popq %rdx
xorl %ecx, %ecx
callq 0x2120
movl $0x100, %edx            # imm = 0x100
movl %r15d, %edi
leaq -0x1940(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2120
movq -0x40(%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 0x2b9c
leaq 0x531(%rip), %rdi       # 0x30c3
movq %r13, %rsi
xorl %eax, %eax
callq 0x2050
leaq -0x2c(%rbp), %r14
jmp 0x28d9
leaq 0x4bc(%rip), %rdi       # 0x3068
jmp 0x2bc5
leaq 0x4c0(%rip), %rdi       # 0x3075
callq 0x21c0
xorl %eax, %eax
jmp 0x2bcd
leaq 0x4d7(%rip), %rdi       # 0x309c
callq 0x21c0
pushq $-0x1
popq %rax
addq $0x1918, %rsp           # imm = 0x1918
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq