// File: /workspace/github_cpp_makefile_star2_O2/akw0088[P]openssl/aes_download_reverse.cpp
// Function: process_download(char*, char*, char*, int, unsigned char*, unsigned char*, unsigned int)
int process_download(char *file_name, char *remote_hash, char *response, int download_size, unsigned char *encrypted_key, unsigned char *prikey, unsigned int max_malloc_size)
{
	if (download_size == 0)
	{
		printf("Download failed\r\n");
		return 0;
	}

	printf("Download complete\r\n");
	printf("Got %d bytes file name %s remote md5sum %s\r\n", download_size, file_name, remote_hash);


	char new_filename[256] = {0};
	char strip_filename[256] = {0};

	StripChars(file_name, strip_filename, (char *)".\\/;:*?\"<>|");

	printf("Attempting to decrypt AES key with RSA\r\n");

	unsigned char decrypted[4098] = {0};

	RSA *rsa = createRSA(prikey, false);
	int decrypted_length = RSA_private_decrypt(256, encrypted_key, decrypted, rsa, RSA_PKCS1_PADDING);
	if (decrypted_length == -1)
	{
		char err[130];

		ERR_load_crypto_strings();
		ERR_error_string(ERR_get_error(), err);
		printf("ERROR: %s\n", err);
//		free((void *)response);
		return -1;
	}
	RSA_free(rsa);

//	printf("Decrypted Text: %s\n", decrypted);
//	printf("Decrypted Length: %d\n", decrypted_length);

	if (decrypted_length != 16 + 32 + 1)
	{
		printf("Unexpected AES key string size\r\n");
		//free((void *)response);
		return -1;
	}

	if (decrypted[16] != ' ')
	{
		printf("Unexpected AES key format: %s\r\n", decrypted);
		//free((void *)response);
		return -1;
	}



	unsigned char key[128] = {0};
	unsigned char iv[128] = {0};

	int ret = sscanf((char *)&decrypted[0], "%s %s", (char *)&iv[0], (char *)&key[0]);

	if (ret != 2)
	{
		printf("Unexpected AES key format: %s matched %d\r\n", decrypted, ret);
		//free((void *)response);
		return -1;
	}

	printf("Decrypted AES Key successfully\r\n");

	printf("Attempting to decrypt file with remote hash %s\r\n", remote_hash);
	aes_decrypt(key, iv, (unsigned char *)response, download_size, max_malloc_size, remote_hash, strip_filename);


	//free((void *)response);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1238, %rsp           # imm = 0x1238
testl %ecx, %ecx
je 0x2ac7
movq %r9, %r15
movq %r8, %r13
movl %ecx, %r14d
movq %rsi, %rbx
movq %rdi, %r12
movq %rdx, -0x38(%rbp)
leaq 0x962(%rip), %rdi       # 0x32b1
callq 0x22a0
leaq 0x830(%rip), %rdi       # 0x318b
movl %r14d, %esi
movq %r12, %rdx
movq %rbx, -0x30(%rbp)
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2050
leaq -0x240(%rbp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2140
leaq 0x82c(%rip), %rdx       # 0x31b8
movq %r12, %rdi
movq %rbx, %rsi
callq 0x240c
leaq 0x926(%rip), %rdi       # 0x32c4
callq 0x22a0
leaq -0x1250(%rbp), %r12
movl $0x1002, %edx           # imm = 0x1002
movq %r12, %rdi
xorl %esi, %esi
callq 0x2140
movq %r15, %rdi
xorl %esi, %esi
callq 0x2b5a
movq %rax, %r15
pushq $0x1
popq %r8
movl $0x100, %edi            # imm = 0x100
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x21a0
cmpl $-0x1, %eax
je 0x2ad8
movl %eax, %r12d
movq %r15, %rdi
callq 0x2200
cmpl $0x31, %r12d
jne 0x2b05
cmpb $0x20, -0x1240(%rbp)
jne 0x2b13
xorps %xmm0, %xmm0
leaq -0xc0(%rbp), %rcx
movaps %xmm0, 0x70(%rcx)
movaps %xmm0, 0x60(%rcx)
movaps %xmm0, 0x50(%rcx)
movaps %xmm0, 0x40(%rcx)
movaps %xmm0, 0x30(%rcx)
movaps %xmm0, 0x20(%rcx)
movaps %xmm0, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq -0x140(%rbp), %rdx
movaps %xmm0, 0x70(%rdx)
movaps %xmm0, 0x60(%rdx)
movaps %xmm0, 0x50(%rdx)
movaps %xmm0, 0x40(%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
leaq 0x791(%rip), %rsi       # 0x31ef
xorl %r13d, %r13d
leaq -0x1250(%rbp), %rdi
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, %eax
jne 0x2b41
leaq 0x86d(%rip), %rdi       # 0x32ec
callq 0x22a0
leaq 0x795(%rip), %rdi       # 0x3220
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
leaq -0x240(%rbp), %rax
movq %rax, (%rsp)
leaq -0xc0(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq 0x10(%rbp), %r8
movq -0x38(%rbp), %rdx
movl %r14d, %ecx
movq %rbx, %r9
callq 0x2c95
jmp 0x2b2c
leaq 0x85e(%rip), %rdi       # 0x332c
callq 0x22a0
xorl %r13d, %r13d
jmp 0x2b2c
pushq $0x2
popq %rdi
xorl %esi, %esi
callq 0x2120
callq 0x2260
leaq -0xc0(%rbp), %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x20c0
leaq 0x6c4(%rip), %rdi       # 0x31c4
movq %rbx, %rsi
jmp 0x2b21
leaq 0x800(%rip), %rdi       # 0x330c
callq 0x22a0
jmp 0x2b28
leaq 0x6b5(%rip), %rdi       # 0x31cf
leaq -0x1250(%rbp), %rsi
xorl %eax, %eax
callq 0x2050
pushq $-0x1
popq %r13
movl %r13d, %eax
addq $0x1238, %rsp           # imm = 0x1238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6ad(%rip), %rdi       # 0x31f5
leaq -0x1250(%rbp), %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x2050
jmp 0x2b28