// File: /workspace/github_c_makefile_star2_O1/zecoxao[P]ps3_decrypt_tools/lib/eid.c
// Function: eid0_decrypt_section
void eid0_decrypt_section(u8 *eid0_in, u8 *section_out, int i) {
    u8 indiv[INDIV_SIZE];
    u8 key[0x10];
	u8 key_priv[0x10];
	u8 zero_iv[0x10];
	memset (key_priv,0,0x10);
	memset (zero_iv,0,0x10);
    aes_context aes_ctxt;
	//aes_context aes_ctxt2;

    //Generate individuals.
    indiv_gen(eid0_indiv_seed, NULL, NULL, NULL, indiv);

    //Generate key.
    if (i == 0) {
        aes_setkey_enc(&aes_ctxt, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid0_keyseed_0, key);
    } else if (i == 6) {
        aes_setkey_enc(&aes_ctxt, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid0_keyseed_6, key);
		/*
		aes_setkey_enc(&aes_ctxt2, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt2, AES_ENCRYPT, eid0_keyseed_6_priv, key_priv);
		*/
    } else if (i == 10) {
        aes_setkey_enc(&aes_ctxt, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid0_keyseed_A, key);
    }
    //Decrypt section 0 of eid0.
    aes_setkey_dec(&aes_ctxt, key, 0x80);
    aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, 0xC0, indiv + INDIV_EID0_SEC_0_IV_OFFSET, eid0_in + 0x20 + 0xC0 * i, section_out);
	
    //Calculate aes omac1.
    u8 digest[AES_OMAC1_DIGEST_SIZE];
    aes_omac1(digest, section_out, 0xA8, key, 0x80);

    if (memcmp(digest, section_out + 0xA8, AES_OMAC1_DIGEST_SIZE) != 0){
		if (i == 0) {
			printf("warning: eid0 section 0 hash check failed!\n");
		} else if (i == 6) {
			printf("warning: eid0 section 6 hash check failed!\n");
		} else if (i == 10) {
			printf("warning: eid0 section A hash check failed!\n");
		}
		
	}
	
	/*
	if (i == 6) {
		//Decrypt section 0 of eid0.
		aes_setkey_dec(&aes_ctxt2, key_priv, 0x80);
		aes_crypt_cbc(&aes_ctxt2, AES_DECRYPT, 0x20, zero_iv, section_out + 0x88, section_out + 0x88);
		
		memset (zero_iv, 0, 0x10);
		
		if (memcmp(section_out + 0x9C, zero_iv, 0xC) != 0){
			printf("Failure with padding on section 6 priv key area!\n");
		}
		else{
			_hexdump(stdout, "Section 6:", 0x88, section_out + 0x88 , 0x20, TRUE);
		}
	}
	*/
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x350, %rsp            # imm = 0x350
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
leaq 0xa9a0(%rip), %rdi      # 0x10360
leaq 0x20(%rsp), %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x56a8
testl %ebx, %ebx
je 0x59e7
cmpl $0x6, %ebx
je 0x59f0
cmpl $0xa, %ebx
jne 0x5a24
leaq 0xab0b(%rip), %r12      # 0x104f0
jmp 0x59f7
leaq 0xaad2(%rip), %r12      # 0x104c0
jmp 0x59f7
leaq 0xaad9(%rip), %r12      # 0x104d0
leaq 0x40(%rsp), %rsi
leaq 0x120(%rsp), %r13
movq %r13, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x125c
movq %rsp, %rcx
movq %r13, %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0x16d7
leaq 0x120(%rsp), %r12
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0x80, %edx
callq 0x153b
leaq 0x30(%rsp), %rcx
movl %ebx, %eax
shll $0x6, %eax
leal (%rax,%rax,2), %eax
cltq
leaq (%r15,%rax), %r8
addq $0x20, %r8
movl $0xc0, %edx
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %r9
callq 0x1eae
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0xa8, %edx
movq %r13, %rcx
movl $0x80, %r8d
callq 0x504d
movdqu 0xa8(%r14), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x5acb
testl %ebx, %ebx
je 0x5ab6
cmpl $0x6, %ebx
je 0x5abf
cmpl $0xa, %ebx
jne 0x5acb
leaq 0x7296(%rip), %rdi      # 0xcd4a
jmp 0x5ac6
leaq 0x72e3(%rip), %rdi      # 0xcda0
jmp 0x5ac6
leaq 0x72af(%rip), %rdi      # 0xcd75
callq 0x1050
addq $0x350, %rsp            # imm = 0x350
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq