// File: /workspace/github_c_makefile_star2_O2/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 0x9b69(%rip), %rdi      # 0xf370
leaq 0x20(%rsp), %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x54fc
testl %ebx, %ebx
je 0x582e
cmpl $0x6, %ebx
je 0x5837
cmpl $0xa, %ebx
jne 0x5869
leaq 0x9cd4(%rip), %r12      # 0xf500
jmp 0x583e
leaq 0x9c9b(%rip), %r12      # 0xf4d0
jmp 0x583e
leaq 0x9ca2(%rip), %r12      # 0xf4e0
leaq 0x40(%rsp), %rsi
leaq 0x120(%rsp), %r13
movq %r13, %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x126c
pushq $0x1
popq %rsi
movq %rsp, %rcx
movq %r13, %rdi
movq %r12, %rdx
callq 0x169e
leaq 0x120(%rsp), %r12
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0x80, %edx
callq 0x1541
leaq 0x30(%rsp), %rcx
imull $0xc0, %ebx, %eax
cltq
leaq (%r15,%rax), %r8
addq $0x20, %r8
movl $0xc0, %edx
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %r9
callq 0x1de8
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0xa8, %edx
movq %r13, %rcx
movl $0x80, %r8d
callq 0x4ee8
movdqu 0xa8(%r14), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x590e
testl %ebx, %ebx
je 0x58f9
cmpl $0x6, %ebx
je 0x5902
cmpl $0xa, %ebx
jne 0x590e
leaq 0x6473(%rip), %rdi      # 0xbd6a
jmp 0x5909
leaq 0x64c0(%rip), %rdi      # 0xbdc0
jmp 0x5909
leaq 0x648c(%rip), %rdi      # 0xbd95
callq 0x1050
addq $0x350, %rsp            # imm = 0x350
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq