// File: akw0088[P]openssl/aes_download.cpp
// Function: aes_decrypt(unsigned char*, unsigned char*, char*, unsigned int&, char*)
int aes_decrypt(unsigned char *key, unsigned char *iv, char *filename, unsigned int &decryptedtext_len, char *remote_hash)
{
	unsigned int size = 0;
	unsigned char *ciphertext = (unsigned char *)get_file(filename, &size);
	if (ciphertext == NULL)
	{
		printf("Unable to open %s\r\n", filename);
		return -1;
	}

	/* Buffer for the decrypted text */
	unsigned char *decryptedtext = (unsigned char *)malloc(size);
	if (decryptedtext == NULL)
	{
		perror("malloc failed");
		return -1;
	}

	unsigned int ciphertext_len = size;

	decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
	decryptedtext[decryptedtext_len] = '\0';

	char new_filename[256] = {0};



	unsigned char local_hash[33] = {0};

	md5sum((char *)decryptedtext, decryptedtext_len, (char *)&local_hash[0]);


	delete [] ciphertext;
	if (strcmp(remote_hash, (char *)&local_hash[0]) == 0)
	{
		printf("MD5 Hash Pass: %s\r\n", remote_hash);
	}
	else
	{
		printf("MD5 Hash Fail: local %s != remote %s\r\n", local_hash, remote_hash);
		free((void *)decryptedtext);
		return -1;
	}


	sprintf(new_filename, "%s_decrypted", filename);
	write_file(new_filename, (char *)decryptedtext, decryptedtext_len);
	free((void *)decryptedtext);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x24c0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x2b4c
movq -0x20(%rbp), %rsi
leaq 0x1698(%rip), %rdi      # 0x41d1
movb $0x0, %al
callq 0x2060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c99
movl -0x34(%rbp), %eax
movl %eax, %edi
callq 0x2270
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2b79
leaq 0x167d(%rip), %rdi      # 0x41e5
callq 0x21b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c99
movl -0x34(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %r8
callq 0x2a10
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
movb $0x0, (%rax,%rcx)
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2130
leaq -0x180(%rbp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x2130
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %esi
leaq -0x180(%rbp), %rdx
callq 0x2400
movq -0x40(%rbp), %rax
movq %rax, -0x188(%rbp)
cmpq $0x0, %rax
je 0x2c06
movq -0x188(%rbp), %rdi
callq 0x2250
movq -0x30(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x2280
cmpl $0x0, %eax
jne 0x2c2f
movq -0x30(%rbp), %rsi
leaq 0x15cd(%rip), %rdi      # 0x41f3
movb $0x0, %al
callq 0x2060
jmp 0x2c5a
leaq -0x180(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq 0x15c6(%rip), %rdi      # 0x4207
movb $0x0, %al
callq 0x2060
movq -0x48(%rbp), %rdi
callq 0x2230
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2c99
leaq -0x150(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x15c2(%rip), %rsi      # 0x422e
movb $0x0, %al
callq 0x2080
leaq -0x150(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x28(%rbp), %rax
movl (%rax), %edx
callq 0x25c0
movq -0x48(%rbp), %rdi
callq 0x2230
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopw %cs:(%rax,%rax)